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