2006-09-04 [colin] 2.4.0cvs143
[claws.git] / src / inc.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include "defs.h"
25
26 #include <glib.h>
27 #include <glib/gi18n.h>
28 #include <gtk/gtkmain.h>
29 #include <gtk/gtkwindow.h>
30 #include <gtk/gtksignal.h>
31 #include <gtk/gtkprogressbar.h>
32 #include <stdio.h>
33 #include <unistd.h>
34 #include <string.h>
35
36 #include "main.h"
37 #include "inc.h"
38 #include "mainwindow.h"
39 #include "folderview.h"
40 #include "summaryview.h"
41 #include "prefs_common.h"
42 #include "prefs_account.h"
43 #include "account.h"
44 #include "procmsg.h"
45 #include "socket.h"
46 #include "ssl.h"
47 #include "pop.h"
48 #include "recv.h"
49 #include "mbox.h"
50 #include "utils.h"
51 #include "gtkutils.h"
52 #include "statusbar.h"
53 #include "msgcache.h"
54 #include "manage_window.h"
55 #include "stock_pixmap.h"
56 #include "progressdialog.h"
57 #include "inputdialog.h"
58 #include "alertpanel.h"
59 #include "folder.h"
60 #include "filtering.h"
61 #include "log.h"
62 #include "hooks.h"
63
64 static GList *inc_dialog_list = NULL;
65
66 static guint inc_lock_count = 0;
67
68 static GdkPixbuf *currentpix;
69 static GdkPixbuf *errorpix;
70 static GdkPixbuf *okpix;
71
72 #define MSGBUFSIZE      8192
73
74 static void inc_finished                (MainWindow             *mainwin,
75                                          gboolean                new_messages);
76 static gint inc_account_mail_real       (MainWindow             *mainwin,
77                                          PrefsAccount           *account);
78
79 static IncProgressDialog *inc_progress_dialog_create
80                                         (gboolean                autocheck);
81 static void inc_progress_dialog_set_list(IncProgressDialog      *inc_dialog);
82 static void inc_progress_dialog_destroy (IncProgressDialog      *inc_dialog);
83
84 static IncSession *inc_session_new      (PrefsAccount           *account);
85 static void inc_session_destroy         (IncSession             *session);
86 static gint inc_start                   (IncProgressDialog      *inc_dialog);
87 static IncState inc_pop3_session_do     (IncSession             *session);
88
89 static void inc_progress_dialog_update  (IncProgressDialog      *inc_dialog,
90                                          IncSession             *inc_session);
91
92 static void inc_progress_dialog_set_label
93                                         (IncProgressDialog      *inc_dialog,
94                                          IncSession             *inc_session);
95 static void inc_progress_dialog_set_progress
96                                         (IncProgressDialog      *inc_dialog,
97                                          IncSession             *inc_session);
98
99 static void inc_progress_dialog_update_periodic
100                                         (IncProgressDialog      *inc_dialog,
101                                          IncSession             *inc_session);
102
103 static gint inc_recv_data_progressive   (Session        *session,
104                                          guint           cur_len,
105                                          guint           total_len,
106                                          gpointer        data);
107 static gint inc_recv_data_finished      (Session        *session,
108                                          guint           len,
109                                          gpointer        data);
110 static gint inc_recv_message            (Session        *session,
111                                          const gchar    *msg,
112                                          gpointer        data);
113 static gint inc_drop_message            (Pop3Session    *session,
114                                          const gchar    *file);
115
116 static void inc_put_error               (IncState        istate,
117                                          Pop3Session    *session);
118
119 static void inc_cancel_cb               (GtkWidget      *widget,
120                                          gpointer        data);
121 static gint inc_dialog_delete_cb        (GtkWidget      *widget,
122                                          GdkEventAny    *event,
123                                          gpointer        data);
124
125 static gint get_spool                   (FolderItem     *dest,
126                                          const gchar    *mbox,
127                                          PrefsAccount   *account);
128
129 static gint inc_spool_account(PrefsAccount *account);
130 static gint inc_all_spool(void);
131 static void inc_autocheck_timer_set_interval    (guint           interval);
132 static gint inc_autocheck_func                  (gpointer        data);
133
134 static void inc_notify_cmd              (gint new_msgs, 
135                                          gboolean notify);
136         
137 /**
138  * inc_finished:
139  * @mainwin: Main window.
140  * @new_messages: TRUE if some messages have been received.
141  * 
142  * Update the folder view and the summary view after receiving
143  * messages.  If @new_messages is FALSE, this function avoids unneeded
144  * updating.
145  **/
146 static void inc_finished(MainWindow *mainwin, gboolean new_messages)
147 {
148         FolderItem *item;
149
150         if (prefs_common.scan_all_after_inc)
151                 folderview_check_new(NULL);
152
153         if (!new_messages && !prefs_common.scan_all_after_inc) return;
154
155         if (prefs_common.open_inbox_on_inc) {
156                 item = cur_account && cur_account->inbox
157                         ? folder_find_item_from_identifier(cur_account->inbox)
158                         : folder_get_default_inbox();
159                 folderview_unselect(mainwin->folderview);
160                 folderview_select(mainwin->folderview, item);
161         }
162 }
163
164 void inc_mail(MainWindow *mainwin, gboolean notify)
165 {
166         gint new_msgs = 0;
167         gint account_new_msgs = 0;
168
169         if (inc_lock_count) return;
170
171         if (prefs_common.work_offline && 
172             !inc_offline_should_override(
173                 _("Sylpheed-Claws needs network access in order "
174                   "to get mails.")))
175                 return;
176
177         inc_lock();
178         inc_autocheck_timer_remove();
179         main_window_lock(mainwin);
180
181         if (prefs_common.use_extinc && prefs_common.extinc_cmd) {
182                 /* external incorporating program */
183                 if (execute_command_line(prefs_common.extinc_cmd, FALSE) < 0) {
184                         main_window_unlock(mainwin);
185                         inc_autocheck_timer_set();
186                         return;
187                 }
188         } else {
189                 account_new_msgs = inc_account_mail_real(mainwin, cur_account);
190                 if (account_new_msgs > 0)
191                         new_msgs += account_new_msgs;
192         }
193
194         inc_finished(mainwin, new_msgs > 0);
195         main_window_unlock(mainwin);
196         inc_notify_cmd(new_msgs, notify);
197         inc_autocheck_timer_set();
198         inc_unlock();
199 }
200
201 void inc_pop_before_smtp(PrefsAccount *acc)
202 {
203         IncProgressDialog *inc_dialog;
204         IncSession *session;
205         MainWindow *mainwin;
206
207         mainwin = mainwindow_get_mainwindow();
208
209         session = inc_session_new(acc);
210         if (!session) return;
211         POP3_SESSION(session->session)->pop_before_smtp = TRUE;
212                 
213         inc_dialog = inc_progress_dialog_create(FALSE);
214         inc_dialog->queue_list = g_list_append(inc_dialog->queue_list,
215                                                session);
216         /* FIXME: assumes to attach to first main window */
217         inc_dialog->mainwin = mainwin;
218         inc_progress_dialog_set_list(inc_dialog);
219
220         if (mainwin) {
221                 toolbar_main_set_sensitive(mainwin);
222                 main_window_set_menu_sensitive(mainwin);
223         }
224                         
225         inc_start(inc_dialog);
226 }
227
228 static gint inc_account_mail_real(MainWindow *mainwin, PrefsAccount *account)
229 {
230         IncProgressDialog *inc_dialog;
231         IncSession *session;
232         
233         switch (account->protocol) {
234         case A_IMAP4:
235         case A_NNTP:
236                 /* Melvin: bug [14]
237                  * FIXME: it should return foldeview_check_new() value.
238                  * TODO: do it when bug [19] is fixed (IMAP folder sets 
239                  * an incorrect new message count)
240                  */
241                 folderview_check_new(FOLDER(account->folder));
242                 return 0;
243         case A_POP3:
244         case A_APOP:
245                 session = inc_session_new(account);
246                 if (!session) return 0;
247                 
248                 inc_dialog = inc_progress_dialog_create(FALSE);
249                 inc_dialog->queue_list = g_list_append(inc_dialog->queue_list,
250                                                        session);
251                 inc_dialog->mainwin = mainwin;
252                 inc_progress_dialog_set_list(inc_dialog);
253
254                 if (mainwin) {
255                         toolbar_main_set_sensitive(mainwin);
256                         main_window_set_menu_sensitive(mainwin);
257                 }
258                         
259                 return inc_start(inc_dialog);
260
261         case A_LOCAL:
262                 return inc_spool_account(account);
263
264         default:
265                 break;
266         }
267         return 0;
268 }
269
270 gint inc_account_mail(MainWindow *mainwin, PrefsAccount *account)
271 {
272         gint new_msgs;
273
274         if (inc_lock_count) return 0;
275
276         if (prefs_common.work_offline && 
277             !inc_offline_should_override(
278                 _("Sylpheed-Claws needs network access in order "
279                   "to get mails.")))
280                 return 0;
281
282         inc_autocheck_timer_remove();
283         main_window_lock(mainwin);
284
285         new_msgs = inc_account_mail_real(mainwin, account);
286
287         inc_finished(mainwin, new_msgs > 0);
288         main_window_unlock(mainwin);
289         inc_autocheck_timer_set();
290
291         return new_msgs;
292 }
293
294 void inc_all_account_mail(MainWindow *mainwin, gboolean autocheck,
295                           gboolean notify)
296 {
297         GList *list, *queue_list = NULL;
298         IncProgressDialog *inc_dialog;
299         gint new_msgs = 0;
300         gint account_new_msgs = 0;
301         
302         if (prefs_common.work_offline && 
303             !inc_offline_should_override(
304                 _("Sylpheed-Claws needs network access in order "
305                   "to get mails.")))
306                 return;
307
308         if (inc_lock_count) return;
309
310         inc_autocheck_timer_remove();
311         main_window_lock(mainwin);
312
313         list = account_get_list();
314         if (!list) {
315                 inc_finished(mainwin, new_msgs > 0);
316                 main_window_unlock(mainwin);
317                 inc_notify_cmd(new_msgs, notify);
318                 inc_autocheck_timer_set();
319                 return;
320         }
321
322         /* check local folders */
323         account_new_msgs = inc_all_spool();
324         if (account_new_msgs > 0)
325                 new_msgs += account_new_msgs;
326
327         /* check IMAP4 / News folders */
328         for (list = account_get_list(); list != NULL; list = list->next) {
329                 PrefsAccount *account = list->data;
330                 if ((account->protocol == A_IMAP4 ||
331                      account->protocol == A_NNTP) && account->recv_at_getall) {
332                         new_msgs += folderview_check_new(FOLDER(account->folder));
333                 }
334         }
335
336         /* check POP3 accounts */
337         for (list = account_get_list(); list != NULL; list = list->next) {
338                 IncSession *session;
339                 PrefsAccount *account = list->data;
340
341                 if (account->recv_at_getall) {
342                         session = inc_session_new(account);
343                         if (session)
344                                 queue_list = g_list_append(queue_list, session);
345                 }
346         }
347
348         if (queue_list) {
349                 inc_dialog = inc_progress_dialog_create(autocheck);
350                 inc_dialog->queue_list = queue_list;
351                 inc_dialog->mainwin = mainwin;
352                 inc_progress_dialog_set_list(inc_dialog);
353
354                 toolbar_main_set_sensitive(mainwin);
355                 main_window_set_menu_sensitive(mainwin);
356                 new_msgs += inc_start(inc_dialog);
357         }
358
359         inc_finished(mainwin, new_msgs > 0);
360         main_window_unlock(mainwin);
361         inc_notify_cmd(new_msgs, notify);
362         inc_autocheck_timer_set();
363 }
364
365 static IncProgressDialog *inc_progress_dialog_create(gboolean autocheck)
366 {
367         IncProgressDialog *dialog;
368         ProgressDialog *progress;
369
370         dialog = g_new0(IncProgressDialog, 1);
371
372         progress = progress_dialog_create();
373         gtk_window_set_title(GTK_WINDOW(progress->window),
374                              _("Retrieving new messages"));
375         g_signal_connect(G_OBJECT(progress->cancel_btn), "clicked",
376                          G_CALLBACK(inc_cancel_cb), dialog);
377         g_signal_connect(G_OBJECT(progress->window), "delete_event",
378                          G_CALLBACK(inc_dialog_delete_cb), dialog);
379         /* manage_window_set_transient(GTK_WINDOW(progress->window)); */
380
381         progress_dialog_get_fraction(progress);
382
383         stock_pixbuf_gdk(progress->treeview, STOCK_PIXMAP_COMPLETE,
384                          &okpix);
385         stock_pixbuf_gdk(progress->treeview, STOCK_PIXMAP_CONTINUE,
386                          &currentpix);
387         stock_pixbuf_gdk(progress->treeview, STOCK_PIXMAP_ERROR,
388                          &errorpix);
389
390         if (prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS ||
391             (prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL &&
392              !autocheck)) {
393                 dialog->show_dialog = TRUE;
394                 gtk_widget_show_now(progress->window);
395         }
396
397         dialog->dialog = progress;
398         g_get_current_time(&dialog->progress_tv);
399         g_get_current_time(&dialog->folder_tv);
400         dialog->queue_list = NULL;
401         dialog->cur_row = 0;
402
403         inc_dialog_list = g_list_append(inc_dialog_list, dialog);
404
405         return dialog;
406 }
407
408 static void inc_progress_dialog_set_list(IncProgressDialog *inc_dialog)
409 {
410         GList *list;
411
412         for (list = inc_dialog->queue_list; list != NULL; list = list->next) {
413                 IncSession *session = list->data;
414                 Pop3Session *pop3_session = POP3_SESSION(session->session);
415
416                 session->data = inc_dialog;
417
418                 progress_dialog_list_set(inc_dialog->dialog,
419                                          -1, NULL,
420                                          pop3_session->ac_prefs->account_name,
421                                          _("Standby"));
422         }
423 }
424
425 static void inc_progress_dialog_clear(IncProgressDialog *inc_dialog)
426 {
427         progress_dialog_get_fraction(inc_dialog->dialog);
428         progress_dialog_set_label(inc_dialog->dialog, "");
429         if (inc_dialog->mainwin)
430                 main_window_progress_off(inc_dialog->mainwin);
431 }
432
433 static void inc_progress_dialog_destroy(IncProgressDialog *inc_dialog)
434 {
435         g_return_if_fail(inc_dialog != NULL);
436
437         inc_dialog_list = g_list_remove(inc_dialog_list, inc_dialog);
438
439         if (inc_dialog->mainwin)
440                 main_window_progress_off(inc_dialog->mainwin);
441         progress_dialog_destroy(inc_dialog->dialog);
442
443         g_free(inc_dialog);
444 }
445
446 static IncSession *inc_session_new(PrefsAccount *account)
447 {
448         IncSession *session;
449
450         g_return_val_if_fail(account != NULL, NULL);
451
452         if (account->protocol != A_POP3)
453                 return NULL;
454         if (!account->recv_server || !account->userid)
455                 return NULL;
456
457         session = g_new0(IncSession, 1);
458
459         session->session = pop3_session_new(account);
460         session->session->data = session;
461         POP3_SESSION(session->session)->drop_message = inc_drop_message;
462         session_set_recv_message_notify(session->session,
463                                         inc_recv_message, session);
464         session_set_recv_data_progressive_notify(session->session,
465                                                  inc_recv_data_progressive,
466                                                  session);
467         session_set_recv_data_notify(session->session,
468                                      inc_recv_data_finished, session);
469
470         return session;
471 }
472
473 static void inc_session_destroy(IncSession *session)
474 {
475         g_return_if_fail(session != NULL);
476
477         session_destroy(session->session);
478         g_free(session);
479 }
480
481 static gint inc_start(IncProgressDialog *inc_dialog)
482 {
483         IncSession *session;
484         GList *qlist;
485         Pop3Session *pop3_session;
486         IncState inc_state;
487         gint error_num = 0;
488         gint new_msgs = 0;
489         gchar *msg;
490         gchar *fin_msg;
491         FolderItem *processing, *inbox;
492         GSList *msglist, *msglist_element;
493         gboolean cancelled = FALSE;
494
495         qlist = inc_dialog->queue_list;
496         while (qlist != NULL) {
497                 GList *next = qlist->next;
498
499                 session = qlist->data;
500                 pop3_session = POP3_SESSION(session->session); 
501                 pop3_session->user = g_strdup(pop3_session->ac_prefs->userid);
502                 if (pop3_session->ac_prefs->passwd)
503                         pop3_session->pass =
504                                 g_strdup(pop3_session->ac_prefs->passwd);
505                 else if (pop3_session->ac_prefs->tmp_pass)
506                         pop3_session->pass =
507                                 g_strdup(pop3_session->ac_prefs->tmp_pass);
508                 else {
509                         gchar *pass;
510
511                         if (inc_dialog->show_dialog)
512                                 manage_window_focus_in
513                                         (inc_dialog->dialog->window,
514                                          NULL, NULL);
515
516                         pass = input_dialog_query_password
517                                 (pop3_session->ac_prefs->recv_server,
518                                  pop3_session->user);
519
520                         if (inc_dialog->show_dialog)
521                                 manage_window_focus_out
522                                         (inc_dialog->dialog->window,
523                                          NULL, NULL);
524
525                         if (pass) {
526                                 pop3_session->ac_prefs->tmp_pass =
527                                         g_strdup(pass);
528                                 pop3_session->pass = pass;
529                         }
530                 }
531
532                 qlist = next;
533         }
534
535 #define SET_PIXMAP_AND_TEXT(pix, str)                                      \
536 {                                                                          \
537         progress_dialog_list_set(inc_dialog->dialog,                       \
538                                  inc_dialog->cur_row,                      \
539                                  pix,                                      \
540                                  NULL,                                     \
541                                  str);                                     \
542 }
543
544         for (; inc_dialog->queue_list != NULL && !cancelled; inc_dialog->cur_row++) {
545                 session = inc_dialog->queue_list->data;
546                 pop3_session = POP3_SESSION(session->session);
547                 GSList *filtered, *unfiltered;
548
549                 if (pop3_session->pass == NULL) {
550                         SET_PIXMAP_AND_TEXT(okpix, _("Cancelled"));
551                         inc_session_destroy(session);
552                         inc_dialog->queue_list =
553                                 g_list_remove(inc_dialog->queue_list, session);
554                         continue;
555                 }
556
557                 inc_progress_dialog_clear(inc_dialog);
558                 progress_dialog_scroll_to_row(inc_dialog->dialog,
559                                               inc_dialog->cur_row);
560
561                 SET_PIXMAP_AND_TEXT(currentpix, _("Retrieving"));
562
563                 /* begin POP3 session */
564                 inc_state = inc_pop3_session_do(session);
565
566                 switch (inc_state) {
567                 case INC_SUCCESS:
568                         if (pop3_session->cur_total_num > 0)
569                                 msg = g_strdup_printf(
570                                         ngettext("Done (%d message (%s) received)",
571                                                  "Done (%d messages (%s) received)",
572                                          pop3_session->cur_total_num),
573                                          pop3_session->cur_total_num,
574                                          to_human_readable(pop3_session->cur_total_recv_bytes));
575                         else
576                                 msg = g_strdup_printf(_("Done (no new messages)"));
577                         SET_PIXMAP_AND_TEXT(okpix, msg);
578                         g_free(msg);
579                         break;
580                 case INC_CONNECT_ERROR:
581                         SET_PIXMAP_AND_TEXT(errorpix, _("Connection failed"));
582                         break;
583                 case INC_AUTH_FAILED:
584                         SET_PIXMAP_AND_TEXT(errorpix, _("Auth failed"));
585                         break;
586                 case INC_LOCKED:
587                         SET_PIXMAP_AND_TEXT(errorpix, _("Locked"));
588                         break;
589                 case INC_ERROR:
590                 case INC_NO_SPACE:
591                 case INC_IO_ERROR:
592                 case INC_SOCKET_ERROR:
593                 case INC_EOF:
594                         SET_PIXMAP_AND_TEXT(errorpix, _("Error"));
595                         break;
596                 case INC_TIMEOUT:
597                         SET_PIXMAP_AND_TEXT(errorpix, _("Timeout"));
598                         break;
599                 case INC_CANCEL:
600                         SET_PIXMAP_AND_TEXT(okpix, _("Cancelled"));
601                         if (!inc_dialog->show_dialog)
602                                 cancelled = TRUE;
603                         break;
604                 default:
605                         break;
606                 }
607                 
608                 if (pop3_session->error_val == PS_AUTHFAIL) {
609                         if(!prefs_common.no_recv_err_panel) {
610                                 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
611                                     ((prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL) && focus_window))
612                                         manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
613                         }
614                 }
615
616                 /* CLAWS: perform filtering actions on dropped message */
617                 /* CLAWS: get default inbox (perhaps per account) */
618                 if (pop3_session->ac_prefs->inbox) {
619                         /* CLAWS: get destination folder / mailbox */
620                         inbox = folder_find_item_from_identifier(pop3_session->ac_prefs->inbox);
621                         if (!inbox)
622                                 inbox = folder_get_default_inbox();
623                 } else
624                         inbox = folder_get_default_inbox();
625
626                 /* get list of messages in processing */
627                 processing = folder_get_default_processing();
628                 folder_item_scan(processing);
629                 msglist = folder_item_get_msg_list(processing);
630
631                 /* process messages */
632                 folder_item_update_freeze();
633                 
634                 procmsg_msglist_filter(msglist, pop3_session->ac_prefs, 
635                                 &filtered, &unfiltered, 
636                                 pop3_session->ac_prefs->filter_on_recv);
637
638                 if (filtered != NULL)
639                         filtering_move_and_copy_msgs(filtered);
640                 if (unfiltered != NULL)
641                         folder_item_move_msgs(inbox, unfiltered);
642
643                 for(msglist_element = msglist; msglist_element != NULL; 
644                     msglist_element = msglist_element->next) {
645                         MsgInfo *msginfo = (MsgInfo *)msglist_element->data;
646                         procmsg_msginfo_free(msginfo);
647                 }
648                 folder_item_update_thaw();
649                 
650                 g_slist_free(msglist);
651                 g_slist_free(filtered);
652                 g_slist_free(unfiltered);
653
654                 statusbar_pop_all();
655
656                 new_msgs += pop3_session->cur_total_num;
657
658                 if (pop3_session->error_val == PS_AUTHFAIL &&
659                     pop3_session->ac_prefs->tmp_pass) {
660                         g_free(pop3_session->ac_prefs->tmp_pass);
661                         pop3_session->ac_prefs->tmp_pass = NULL;
662                 }
663
664                 pop3_write_uidl_list(pop3_session);
665
666                 if (inc_state != INC_SUCCESS && inc_state != INC_CANCEL) {
667                         error_num++;
668                         if (inc_dialog->show_dialog)
669                                 manage_window_focus_in
670                                         (inc_dialog->dialog->window,
671                                          NULL, NULL);
672                         inc_put_error(inc_state, pop3_session);
673                         if (inc_dialog->show_dialog)
674                                 manage_window_focus_out
675                                         (inc_dialog->dialog->window,
676                                          NULL, NULL);
677                         if (inc_state == INC_NO_SPACE ||
678                             inc_state == INC_IO_ERROR)
679                                 break;
680                 }
681                 folder_item_free_cache(processing, TRUE);
682
683                 inc_session_destroy(session);
684                 inc_dialog->queue_list =
685                         g_list_remove(inc_dialog->queue_list, session);
686         }
687
688 #undef SET_PIXMAP_AND_TEXT
689
690         if (new_msgs > 0)
691                 fin_msg = g_strdup_printf(ngettext("Finished (%d new message)",
692                                                    "Finished (%d new messages)",
693                                                    new_msgs), new_msgs);
694         else
695                 fin_msg = g_strdup_printf(_("Finished (no new messages)"));
696
697         progress_dialog_set_label(inc_dialog->dialog, fin_msg);
698
699         while (inc_dialog->queue_list != NULL) {
700                 session = inc_dialog->queue_list->data;
701                 inc_session_destroy(session);
702                 inc_dialog->queue_list =
703                         g_list_remove(inc_dialog->queue_list, session);
704         }
705
706         if (prefs_common.close_recv_dialog || !inc_dialog->show_dialog)
707                 inc_progress_dialog_destroy(inc_dialog);
708         else {
709                 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window),
710                                      fin_msg);
711                 gtk_button_set_label(GTK_BUTTON(inc_dialog->dialog->cancel_btn),
712                                      GTK_STOCK_CLOSE);
713         }
714
715         g_free(fin_msg);
716
717         return new_msgs;
718 }
719
720 static IncState inc_pop3_session_do(IncSession *session)
721 {
722         Pop3Session *pop3_session = POP3_SESSION(session->session);
723         IncProgressDialog *inc_dialog = (IncProgressDialog *)session->data;
724         gchar *server;
725         gushort port;
726         gchar *buf;
727
728         debug_print("getting new messages of account %s...\n",
729                     pop3_session->ac_prefs->account_name);
730                     
731         pop3_session->ac_prefs->last_pop_login_time = time(NULL);
732
733         buf = g_strdup_printf(_("%s: Retrieving new messages"),
734                               pop3_session->ac_prefs->recv_server);
735         gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window), buf);
736         g_free(buf);
737
738         server = pop3_session->ac_prefs->recv_server;
739 #if USE_OPENSSL
740         port = pop3_session->ac_prefs->set_popport ?
741                 pop3_session->ac_prefs->popport :
742                 pop3_session->ac_prefs->ssl_pop == SSL_TUNNEL ? 995 : 110;
743         SESSION(pop3_session)->ssl_type = pop3_session->ac_prefs->ssl_pop;
744         if (pop3_session->ac_prefs->ssl_pop != SSL_NONE)
745                 SESSION(pop3_session)->nonblocking =
746                         pop3_session->ac_prefs->use_nonblocking_ssl;
747 #else
748         if (pop3_session->ac_prefs->ssl_pop != SSL_NONE) {
749                 if (alertpanel_full(_("Insecure connection"),
750                         _("This connection is configured to be secured "
751                           "using SSL, but SSL is not available in this "
752                           "build of Sylpheed-Claws. \n\n"
753                           "Do you want to continue connecting to this "
754                           "server? The communication would not be "
755                           "secure."),
756                           GTK_STOCK_CANCEL, _("Con_tinue connecting"), 
757                           NULL, FALSE, NULL, ALERT_WARNING,
758                           G_ALERTDEFAULT) != G_ALERTALTERNATE)
759                         return INC_CANCEL;
760         }
761         port = pop3_session->ac_prefs->set_popport ?
762                 pop3_session->ac_prefs->popport : 110;
763 #endif
764
765         buf = g_strdup_printf(_("Connecting to POP3 server: %s..."), server);
766         statusbar_print_all("%s", buf);
767         log_message("%s\n", buf);
768
769         progress_dialog_set_label(inc_dialog->dialog, buf);
770         GTK_EVENTS_FLUSH();
771         g_free(buf);
772
773         session_set_timeout(SESSION(pop3_session),
774                             prefs_common.io_timeout_secs * 1000);
775         
776         if (session_connect(SESSION(pop3_session), server, port) < 0) {
777                 if(!prefs_common.no_recv_err_panel) {
778                         if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
779                             ((prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL) && focus_window)) {
780                                 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
781                         }
782                         alertpanel_error(_("Can't connect to POP3 server: %s:%d"),
783                                          server, port);
784                         manage_window_focus_out(inc_dialog->dialog->window, NULL, NULL);
785                 } else {
786                         log_error(_("Can't connect to POP3 server: %s:%d\n"),
787                             server, port);
788                 }
789                 session->inc_state = INC_CONNECT_ERROR;
790                 statusbar_pop_all();
791                 return INC_CONNECT_ERROR;
792         }
793
794         while (session_is_connected(SESSION(pop3_session)) &&
795                session->inc_state != INC_CANCEL)
796                 gtk_main_iteration();
797
798         if (session->inc_state == INC_SUCCESS) {
799                 switch (pop3_session->error_val) {
800                 case PS_SUCCESS:
801                         switch (SESSION(pop3_session)->state) {
802                         case SESSION_ERROR:
803                                 if (pop3_session->state == POP3_READY)
804                                         session->inc_state = INC_CONNECT_ERROR;
805                                 else
806                                         session->inc_state = INC_ERROR;
807                                 break;
808                         case SESSION_EOF:
809                                 session->inc_state = INC_EOF;
810                                 break;
811                         case SESSION_TIMEOUT:
812                                 session->inc_state = INC_TIMEOUT;
813                                 break;
814                         default:
815                                 session->inc_state = INC_SUCCESS;
816                                 break;
817                         }
818                         break;
819                 case PS_AUTHFAIL:
820                         session->inc_state = INC_AUTH_FAILED;
821                         break;
822                 case PS_IOERR:
823                         session->inc_state = INC_IO_ERROR;
824                         break;
825                 case PS_SOCKET:
826                         session->inc_state = INC_SOCKET_ERROR;
827                         break;
828                 case PS_LOCKBUSY:
829                         session->inc_state = INC_LOCKED;
830                         break;
831                 default:
832                         session->inc_state = INC_ERROR;
833                         break;
834                 }
835         }
836
837         session_disconnect(SESSION(pop3_session));
838         statusbar_pop_all();
839
840         return session->inc_state;
841 }
842
843 static void inc_progress_dialog_update(IncProgressDialog *inc_dialog,
844                                        IncSession *inc_session)
845 {
846         inc_progress_dialog_set_label(inc_dialog, inc_session);
847         inc_progress_dialog_set_progress(inc_dialog, inc_session);
848 }
849
850 static void inc_progress_dialog_set_label(IncProgressDialog *inc_dialog,
851                                           IncSession *inc_session)
852 {
853         ProgressDialog *dialog = inc_dialog->dialog;
854         Pop3Session *session;
855
856         g_return_if_fail(inc_session != NULL);
857
858         session = POP3_SESSION(inc_session->session);
859
860         switch (session->state) {
861         case POP3_GREETING:
862                 break;
863         case POP3_GETAUTH_USER:
864         case POP3_GETAUTH_PASS:
865         case POP3_GETAUTH_APOP:
866                 progress_dialog_set_label(dialog, _("Authenticating..."));
867                 statusbar_pop_all();
868                 statusbar_print_all(_("Retrieving messages from %s (%s) ..."),
869                                     SESSION(session)->server,
870                                     session->ac_prefs->account_name);
871                 break;
872         case POP3_GETRANGE_STAT:
873                 progress_dialog_set_label
874                         (dialog, _("Getting the number of new messages (STAT)..."));
875                 break;
876         case POP3_GETRANGE_LAST:
877                 progress_dialog_set_label
878                         (dialog, _("Getting the number of new messages (LAST)..."));
879                 break;
880         case POP3_GETRANGE_UIDL:
881                 progress_dialog_set_label
882                         (dialog, _("Getting the number of new messages (UIDL)..."));
883                 break;
884         case POP3_GETSIZE_LIST:
885                 progress_dialog_set_label
886                         (dialog, _("Getting the size of messages (LIST)..."));
887                 break;
888         case POP3_RETR:
889         case POP3_RETR_RECV:
890         case POP3_DELETE:
891                 break;
892         case POP3_LOGOUT:
893                 progress_dialog_set_label(dialog, _("Quitting"));
894                 break;
895         default:
896                 break;
897         }
898 }
899
900 static void inc_progress_dialog_set_progress(IncProgressDialog *inc_dialog,
901                                              IncSession *inc_session)
902 {
903         gchar buf[MSGBUFSIZE];
904         Pop3Session *pop3_session = POP3_SESSION(inc_session->session);
905         gchar *total_size_str;
906         gint cur_total;
907         gint total;
908
909         if (!pop3_session->new_msg_exist) return;
910
911         cur_total = inc_session->cur_total_bytes;
912         total = pop3_session->total_bytes;
913         if (pop3_session->state == POP3_RETR ||
914             pop3_session->state == POP3_RETR_RECV ||
915             pop3_session->state == POP3_DELETE) {
916                 Xstrdup_a(total_size_str, to_human_readable(total), return);
917                 g_snprintf(buf, sizeof(buf),
918                            _("Retrieving message (%d / %d) (%s / %s)"),
919                            pop3_session->cur_msg, pop3_session->count,
920                            to_human_readable(cur_total), total_size_str);
921                 progress_dialog_set_label(inc_dialog->dialog, buf);
922         }
923
924         progress_dialog_set_fraction
925                 (inc_dialog->dialog, (total == 0) ? 0: (gfloat)cur_total / (gfloat)total);
926
927         g_snprintf(buf, sizeof(buf), "%d / %d",
928                    pop3_session->cur_msg, pop3_session->count);
929         gtk_progress_bar_set_text
930                 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar), buf);
931         gtk_progress_bar_set_fraction
932                 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar),
933                  (total == 0) ? 0 : (gfloat)cur_total / (gfloat)total);
934
935         if (pop3_session->cur_total_num > 0) {
936                 g_snprintf(buf, sizeof(buf),
937                            ngettext("Retrieving (%d message (%s) received)",
938                                     "Retrieving (%d messages (%s) received)",
939                                     pop3_session->cur_total_num),
940                            pop3_session->cur_total_num,
941                            to_human_readable
942                            (pop3_session->cur_total_recv_bytes));
943                 progress_dialog_list_set_status(inc_dialog->dialog,
944                                                 inc_dialog->cur_row,
945                                                 buf);
946         }
947 }
948
949 static void inc_progress_dialog_update_periodic(IncProgressDialog *inc_dialog,
950                                                 IncSession *inc_session)
951 {
952         GTimeVal tv_cur;
953         GTimeVal tv_result;
954         gint msec;
955
956         g_get_current_time(&tv_cur);
957
958         tv_result.tv_sec = tv_cur.tv_sec - inc_dialog->progress_tv.tv_sec;
959         tv_result.tv_usec = tv_cur.tv_usec - inc_dialog->progress_tv.tv_usec;
960         if (tv_result.tv_usec < 0) {
961                 tv_result.tv_sec--;
962                 tv_result.tv_usec += G_USEC_PER_SEC;
963         }
964
965         msec = tv_result.tv_sec * 1000 + tv_result.tv_usec / 1000;
966         if (msec > PROGRESS_UPDATE_INTERVAL) {
967                 inc_progress_dialog_update(inc_dialog, inc_session);
968                 inc_dialog->progress_tv.tv_sec = tv_cur.tv_sec;
969                 inc_dialog->progress_tv.tv_usec = tv_cur.tv_usec;
970         }
971 }
972
973 static gint inc_recv_data_progressive(Session *session, guint cur_len,
974                                       guint total_len, gpointer data)
975 {
976         IncSession *inc_session = (IncSession *)data;
977         Pop3Session *pop3_session = POP3_SESSION(session);
978         IncProgressDialog *inc_dialog;
979         gint cur_total;
980
981         g_return_val_if_fail(inc_session != NULL, -1);
982
983         if (pop3_session->state != POP3_RETR &&
984             pop3_session->state != POP3_RETR_RECV &&
985             pop3_session->state != POP3_DELETE &&
986             pop3_session->state != POP3_LOGOUT) return 0;
987
988         if (!pop3_session->new_msg_exist) return 0;
989
990         cur_total = pop3_session->cur_total_bytes + cur_len;
991         if (cur_total > pop3_session->total_bytes)
992                 cur_total = pop3_session->total_bytes;
993         inc_session->cur_total_bytes = cur_total;
994
995         inc_dialog = (IncProgressDialog *)inc_session->data;
996         inc_progress_dialog_update_periodic(inc_dialog, inc_session);
997
998         return 0;
999 }
1000
1001 static gint inc_recv_data_finished(Session *session, guint len, gpointer data)
1002 {
1003         IncSession *inc_session = (IncSession *)data;
1004         IncProgressDialog *inc_dialog;
1005
1006         g_return_val_if_fail(inc_session != NULL, -1);
1007
1008         inc_dialog = (IncProgressDialog *)inc_session->data;
1009
1010         inc_recv_data_progressive(session, 0, 0, inc_session);
1011
1012         if (POP3_SESSION(session)->state == POP3_LOGOUT) {
1013                 inc_progress_dialog_update(inc_dialog, inc_session);
1014         }
1015
1016         return 0;
1017 }
1018
1019 static gint inc_recv_message(Session *session, const gchar *msg, gpointer data)
1020 {
1021         IncSession *inc_session = (IncSession *)data;
1022         IncProgressDialog *inc_dialog;
1023
1024         g_return_val_if_fail(inc_session != NULL, -1);
1025
1026         inc_dialog = (IncProgressDialog *)inc_session->data;
1027
1028         switch (POP3_SESSION(session)->state) {
1029         case POP3_GETAUTH_USER:
1030         case POP3_GETAUTH_PASS:
1031         case POP3_GETAUTH_APOP:
1032         case POP3_GETRANGE_STAT:
1033         case POP3_GETRANGE_LAST:
1034         case POP3_GETRANGE_UIDL:
1035         case POP3_GETSIZE_LIST:
1036                 inc_progress_dialog_update(inc_dialog, inc_session);
1037                 break;
1038         case POP3_RETR:
1039                 inc_recv_data_progressive(session, 0, 0, inc_session);
1040                 break;
1041         case POP3_LOGOUT:
1042                 inc_progress_dialog_update(inc_dialog, inc_session);
1043                 break;
1044         default:
1045                 break;
1046         }
1047
1048         return 0;
1049 }
1050
1051 static gint inc_drop_message(Pop3Session *session, const gchar *file)
1052 {
1053         FolderItem *inbox;
1054         FolderItem *dropfolder;
1055         IncSession *inc_session = (IncSession *)(SESSION(session)->data);
1056         gint msgnum;
1057
1058         g_return_val_if_fail(inc_session != NULL, -1);
1059
1060         if (session->ac_prefs->inbox) {
1061                 inbox = folder_find_item_from_identifier
1062                         (session->ac_prefs->inbox);
1063                 if (!inbox)
1064                         inbox = folder_get_default_inbox();
1065         } else
1066                 inbox = folder_get_default_inbox();
1067         if (!inbox) {
1068                 g_unlink(file);
1069                 return -1;
1070         }
1071
1072         /* CLAWS: claws uses a global .processing folder for the filtering. */
1073         dropfolder = folder_get_default_processing();
1074
1075         /* add msg file to drop folder */
1076         if ((msgnum = folder_item_add_msg(
1077                         dropfolder, file, NULL, TRUE)) < 0) {
1078                 g_unlink(file);
1079                 return -1;
1080         }
1081
1082         return 0;
1083 }
1084
1085 static void inc_put_error(IncState istate, Pop3Session *session)
1086 {
1087         gchar *log_msg = NULL;
1088         gchar *err_msg = NULL;
1089         gboolean fatal_error = FALSE;
1090
1091         switch (istate) {
1092         case INC_CONNECT_ERROR:
1093                 log_msg = _("Connection failed.");
1094                 fatal_error = TRUE;
1095                 if (prefs_common.no_recv_err_panel)
1096                         break;
1097                 err_msg = g_strdup_printf(_("Connection to %s:%d failed."),
1098                                           SESSION(session)->server, 
1099                                           SESSION(session)->port);
1100                 break;
1101         case INC_ERROR:
1102                 log_msg = _("Error occurred while processing mail.");
1103                 fatal_error = TRUE;
1104                 if (prefs_common.no_recv_err_panel)
1105                         break;
1106                 if (session->error_msg)
1107                         err_msg = g_strdup_printf
1108                                 (_("Error occurred while processing mail:\n%s"),
1109                                  session->error_msg);
1110                 else
1111                         err_msg = g_strdup(log_msg);
1112                 break;
1113         case INC_NO_SPACE:
1114                 log_msg = _("No disk space left.");
1115                 err_msg = g_strdup(log_msg);
1116                 fatal_error = TRUE;
1117                 break;
1118         case INC_IO_ERROR:
1119                 log_msg = _("Can't write file.");
1120                 err_msg = g_strdup(log_msg);
1121                 fatal_error = TRUE;
1122                 break;
1123         case INC_SOCKET_ERROR:
1124                 log_msg = _("Socket error.");
1125                 if (prefs_common.no_recv_err_panel)
1126                         break;
1127                 err_msg = g_strdup_printf(_("Socket error on connection to %s:%d."),
1128                                           SESSION(session)->server, 
1129                                           SESSION(session)->port);
1130                 break;
1131         case INC_EOF:
1132                 log_msg = _("Connection closed by the remote host.");
1133                 if (prefs_common.no_recv_err_panel)
1134                         break;
1135                 err_msg = g_strdup_printf(_("Connection to %s:%d closed by the remote host."), 
1136                                           SESSION(session)->server, 
1137                                           SESSION(session)->port);
1138                 break;
1139         case INC_LOCKED:
1140                 log_msg = _("Mailbox is locked.");
1141                 if (prefs_common.no_recv_err_panel)
1142                         break;
1143                 if (session->error_msg)
1144                         err_msg = g_strdup_printf(_("Mailbox is locked:\n%s"),
1145                                                   session->error_msg);
1146                 else
1147                         err_msg = g_strdup(log_msg);
1148                 break;
1149         case INC_AUTH_FAILED:
1150                 log_msg = _("Authentication failed.");
1151                 fatal_error = TRUE;
1152                 if (prefs_common.no_recv_err_panel)
1153                         break;
1154                 if (session->error_msg)
1155                         err_msg = g_strdup_printf
1156                                 (_("Authentication failed:\n%s"), session->error_msg);
1157                 else
1158                         err_msg = g_strdup(log_msg);
1159                 break;
1160         case INC_TIMEOUT:
1161                 log_msg = _("Session timed out.");
1162                 if (prefs_common.no_recv_err_panel)
1163                         break;
1164                 err_msg = g_strdup_printf(_("Connection to %s:%d timed out."), 
1165                                           SESSION(session)->server, 
1166                                           SESSION(session)->port);
1167                 break;
1168         default:
1169                 break;
1170         }
1171
1172         if (log_msg) {
1173                 if (fatal_error)
1174                         log_error("%s\n", log_msg);
1175                 else
1176                         log_warning("%s\n", log_msg);
1177         }
1178         if (prefs_common.no_recv_err_panel && fatal_error)
1179                 mainwindow_show_error();
1180
1181         if (err_msg) {
1182                 alertpanel_error_log(err_msg);
1183                 g_free(err_msg);
1184         }
1185 }
1186
1187 static void inc_cancel(IncProgressDialog *dialog)
1188 {
1189         IncSession *session;
1190
1191         g_return_if_fail(dialog != NULL);
1192
1193         if (dialog->queue_list == NULL) {
1194                 inc_progress_dialog_destroy(dialog);
1195                 return;
1196         }
1197
1198         session = dialog->queue_list->data;
1199
1200         session->inc_state = INC_CANCEL;
1201
1202         log_message(_("Incorporation cancelled\n"));
1203 }
1204
1205 gboolean inc_is_active(void)
1206 {
1207         return (inc_dialog_list != NULL);
1208 }
1209
1210 void inc_cancel_all(void)
1211 {
1212         GList *cur;
1213
1214         for (cur = inc_dialog_list; cur != NULL; cur = cur->next)
1215                 inc_cancel((IncProgressDialog *)cur->data);
1216 }
1217
1218 static void inc_cancel_cb(GtkWidget *widget, gpointer data)
1219 {
1220         inc_cancel((IncProgressDialog *)data);
1221 }
1222
1223 static gint inc_dialog_delete_cb(GtkWidget *widget, GdkEventAny *event,
1224                                  gpointer data)
1225 {
1226         IncProgressDialog *dialog = (IncProgressDialog *)data;
1227
1228         if (dialog->queue_list == NULL)
1229                 inc_progress_dialog_destroy(dialog);
1230
1231         return TRUE;
1232 }
1233
1234 static gint inc_spool_account(PrefsAccount *account)
1235 {
1236         FolderItem *inbox;
1237         gchar *mbox;
1238         gint result;
1239
1240         if (account->local_inbox) {
1241                 inbox = folder_find_item_from_identifier(account->local_inbox);
1242                 if (!inbox)
1243                         inbox = folder_get_default_inbox();
1244         } else
1245                 inbox = folder_get_default_inbox();
1246
1247         if (is_file_exist(account->local_mbox))
1248                 mbox = g_strdup(account->local_mbox);
1249         else if (is_dir_exist(account->local_mbox)) 
1250                 mbox = g_strconcat(account->local_mbox, G_DIR_SEPARATOR_S,
1251                                    g_get_user_name(), NULL);
1252         else {
1253                 debug_print("%s: local mailbox not found.\n", 
1254                             account->local_mbox);
1255                 return -1;
1256         }
1257         
1258         result = get_spool(inbox, mbox, account);
1259         g_free(mbox);
1260         
1261         statusbar_pop_all();
1262         
1263         return result;
1264 }
1265
1266 static gint inc_all_spool(void)
1267 {
1268         GList *list = NULL;
1269         gint new_msgs = 0;
1270         gint account_new_msgs = 0;
1271
1272         list = account_get_list();
1273         if (!list) return 0;
1274
1275         for (; list != NULL; list = list->next) {
1276                 PrefsAccount *account = list->data;
1277
1278                 if ((account->protocol == A_LOCAL) &&
1279                     (account->recv_at_getall)) {
1280                         account_new_msgs = inc_spool_account(account);
1281                         if (account_new_msgs > 0)
1282                                 new_msgs += account_new_msgs;
1283                 }
1284         }
1285
1286         return new_msgs;
1287 }
1288
1289 static gint get_spool(FolderItem *dest, const gchar *mbox, PrefsAccount *account)
1290 {
1291         gint msgs, size;
1292         gint lockfd;
1293         gchar tmp_mbox[MAXPATHLEN + 1];
1294
1295         g_return_val_if_fail(dest != NULL, -1);
1296         g_return_val_if_fail(mbox != NULL, -1);
1297
1298         if (!is_file_exist(mbox) || (size = get_file_size(mbox)) == 0) {
1299                 debug_print("%s: no messages in local mailbox.\n", mbox);
1300                 return 0;
1301         } else if (size < 0)
1302                 return -1;
1303
1304         if ((lockfd = lock_mbox(mbox, LOCK_FLOCK)) < 0)
1305                 return -1;
1306
1307         g_snprintf(tmp_mbox, sizeof(tmp_mbox), "%s%ctmpmbox.%08x",
1308                    get_tmp_dir(), G_DIR_SEPARATOR, (gint)mbox);
1309
1310         if (copy_mbox(mbox, tmp_mbox) < 0) {
1311                 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1312                 return -1;
1313         }
1314
1315         debug_print("Getting new messages from %s into %s...\n",
1316                     mbox, dest->path);
1317
1318         msgs = proc_mbox(dest, tmp_mbox, TRUE, account);
1319
1320         g_unlink(tmp_mbox);
1321         if (msgs >= 0) empty_mbox(mbox);
1322         unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1323
1324         return msgs;
1325 }
1326
1327 void inc_lock(void)
1328 {
1329         inc_lock_count++;
1330 }
1331
1332 void inc_unlock(void)
1333 {
1334         if (inc_lock_count > 0)
1335                 inc_lock_count--;
1336 }
1337
1338 static guint autocheck_timer = 0;
1339 static gpointer autocheck_data = NULL;
1340
1341 static void inc_notify_cmd(gint new_msgs, gboolean notify)
1342 {
1343
1344         gchar *buf, *numpos, *ret_str;
1345         gssize by_read = 0, by_written = 0;
1346
1347         if (!(new_msgs && notify && prefs_common.newmail_notify_cmd &&
1348             *prefs_common.newmail_notify_cmd))
1349                      return;
1350         buf = g_strdup(prefs_common.newmail_notify_cmd);
1351         if ((numpos = strstr(buf, "%d")) != NULL) {
1352                 gchar *buf2;
1353
1354                 *numpos = '\0';
1355                 buf2 = g_strdup_printf("%s%d%s", buf, new_msgs, numpos + 2);
1356                 g_free(buf);
1357                 buf = buf2;
1358         }
1359
1360         ret_str = g_locale_from_utf8(buf, strlen(buf), &by_read, &by_written,
1361                                      NULL);
1362         if (ret_str && by_written) {
1363                 g_free(buf);
1364                 buf = ret_str;
1365         }
1366         debug_print("executing new mail notification command: %s\n", buf);
1367         execute_command_line(buf, TRUE);
1368
1369         g_free(buf);
1370 }
1371  
1372 void inc_autocheck_timer_init(MainWindow *mainwin)
1373 {
1374         autocheck_data = mainwin;
1375         inc_autocheck_timer_set();
1376 }
1377
1378 static void inc_autocheck_timer_set_interval(guint interval)
1379 {
1380         inc_autocheck_timer_remove();
1381         /* last test is to avoid re-enabling auto_check after modifying 
1382            the common preferences */
1383         if (prefs_common.autochk_newmail && autocheck_data
1384             && prefs_common.work_offline == FALSE) {
1385                 autocheck_timer = gtk_timeout_add
1386                         (interval, inc_autocheck_func, autocheck_data);
1387                 debug_print("added timer = %d\n", autocheck_timer);
1388         }
1389 }
1390
1391 void inc_autocheck_timer_set(void)
1392 {
1393         inc_autocheck_timer_set_interval(prefs_common.autochk_itv * 60000);
1394 }
1395
1396 void inc_autocheck_timer_remove(void)
1397 {
1398         if (autocheck_timer) {
1399                 debug_print("removed timer = %d\n", autocheck_timer);
1400                 gtk_timeout_remove(autocheck_timer);
1401                 autocheck_timer = 0;
1402         }
1403 }
1404
1405 static gint inc_autocheck_func(gpointer data)
1406 {
1407         MainWindow *mainwin = (MainWindow *)data;
1408
1409         if (inc_lock_count) {
1410                 debug_print("autocheck is locked.\n");
1411                 inc_autocheck_timer_set_interval(1000);
1412                 return FALSE;
1413         }
1414
1415         inc_all_account_mail(mainwin, TRUE, prefs_common.newmail_notify_auto);
1416
1417         return FALSE;
1418 }
1419
1420 gboolean inc_offline_should_override(const gchar *msg)
1421 {
1422         static time_t overridden_yes = 0;
1423         static time_t overridden_no  = 0;
1424         int length = 10; /* minutes */
1425         gint answer = G_ALERTDEFAULT;
1426         
1427         if (prefs_common.autochk_newmail)
1428                 length = prefs_common.autochk_itv; /* minutes */
1429
1430         if (prefs_common.work_offline) {
1431                 gchar *tmp = NULL;
1432                 
1433                 if (time(NULL) - overridden_yes < length * 60) /* seconds */
1434                          return TRUE;
1435                 else if (time(NULL) - overridden_no < 3) /* seconds */
1436                          return FALSE;
1437
1438                 tmp = g_strdup_printf(
1439                                 _("%s%sYou're working offline. Override for %d minutes?"),
1440                                 msg?msg:"", 
1441                                 msg?"\n\n":"",
1442                                 length);
1443
1444                 answer = alertpanel(_("Offline warning"), 
1445                                tmp,
1446                                GTK_STOCK_NO, "+" GTK_STOCK_YES, _("On_ly once"));
1447                 g_free(tmp);
1448                 if (answer == G_ALERTALTERNATE) {
1449                         overridden_yes = time(NULL);
1450                         return TRUE;
1451                 } else if (answer == G_ALERTDEFAULT) {
1452                         overridden_no  = time(NULL);
1453                         return FALSE;
1454                 } else {
1455                         overridden_yes = (time_t)0;
1456                         overridden_no  = (time_t)0;
1457                         return TRUE;
1458                 }
1459         }
1460         return TRUE;
1461 }