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