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