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