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