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