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