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