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