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