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