4d9f3411719402a466941c02a537a558982e0dc7
[claws.git] / src / inc.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2016 Hiroyuki Yamamoto and the Claws Mail team
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 3 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, see <http://www.gnu.org/licenses/>.
17  */
18
19 #ifdef HAVE_CONFIG_H
20 #  include "config.h"
21 #include "claws-features.h"
22 #endif
23
24 #include "defs.h"
25
26 #include <glib.h>
27 #include <glib/gi18n.h>
28 #include <gtk/gtk.h>
29 #include <stdio.h>
30 #include <unistd.h>
31 #include <string.h>
32
33 #include "main.h"
34 #include "inc.h"
35 #include "mainwindow.h"
36 #include "folderview.h"
37 #include "summaryview.h"
38 #include "prefs_common.h"
39 #include "prefs_account.h"
40 #include "account.h"
41 #include "procmsg.h"
42 #include "proxy.h"
43 #include "socket.h"
44 #include "ssl.h"
45 #include "pop.h"
46 #include "recv.h"
47 #include "mbox.h"
48 #include "utils.h"
49 #include "gtkutils.h"
50 #include "statusbar.h"
51 #include "msgcache.h"
52 #include "manage_window.h"
53 #include "stock_pixmap.h"
54 #include "progressdialog.h"
55 #include "inputdialog.h"
56 #include "alertpanel.h"
57 #include "folder.h"
58 #include "filtering.h"
59 #include "log.h"
60 #include "hooks.h"
61 #include "logwindow.h"
62 #include "passwordstore.h"
63
64 extern SessionStats session_stats;
65
66 static GList *inc_dialog_list = NULL;
67
68 guint inc_lock_count = 0;
69
70 static GdkPixbuf *currentpix;
71 static GdkPixbuf *errorpix;
72 static GdkPixbuf *okpix;
73
74 #define MESSAGEBUFSIZE  8192
75
76 static void inc_update_stats(gint new_msgs);
77 static void inc_finished                (MainWindow             *mainwin,
78                                          gboolean                new_messages,
79                                          gboolean                autocheck);
80 static gint inc_account_mail_real       (MainWindow             *mainwin,
81                                          PrefsAccount           *account);
82
83 static IncProgressDialog *inc_progress_dialog_create
84                                         (gboolean                autocheck);
85 static void inc_progress_dialog_set_list(IncProgressDialog      *inc_dialog);
86 static void inc_progress_dialog_destroy (IncProgressDialog      *inc_dialog);
87
88 static IncSession *inc_session_new      (PrefsAccount           *account);
89 static void inc_session_destroy         (IncSession             *session);
90 static gint inc_start                   (IncProgressDialog      *inc_dialog);
91 static IncState inc_pop3_session_do     (IncSession             *session);
92
93 static void inc_progress_dialog_update  (IncProgressDialog      *inc_dialog,
94                                          IncSession             *inc_session);
95
96 static void inc_progress_dialog_set_label
97                                         (IncProgressDialog      *inc_dialog,
98                                          IncSession             *inc_session);
99 static void inc_progress_dialog_set_progress
100                                         (IncProgressDialog      *inc_dialog,
101                                          IncSession             *inc_session);
102
103 static void inc_progress_dialog_update_periodic
104                                         (IncProgressDialog      *inc_dialog,
105                                          IncSession             *inc_session);
106
107 static gint inc_recv_data_progressive   (Session        *session,
108                                          guint           cur_len,
109                                          guint           total_len,
110                                          gpointer        data);
111 static gint inc_recv_data_finished      (Session        *session,
112                                          guint           len,
113                                          gpointer        data);
114 static gint inc_recv_message            (Session        *session,
115                                          const gchar    *msg,
116                                          gpointer        data);
117 static gint inc_drop_message            (Pop3Session    *session,
118                                          const gchar    *file);
119
120 static void inc_put_error               (IncState        istate,
121                                          Pop3Session    *session);
122
123 static void inc_showlog_cb              (GtkWidget      *widget,
124                                          gpointer        data);
125 static void inc_cancel_cb               (GtkWidget      *widget,
126                                          gpointer        data);
127 static gint inc_dialog_delete_cb        (GtkWidget      *widget,
128                                          GdkEventAny    *event,
129                                          gpointer        data);
130
131 static gint get_spool                   (FolderItem     *dest,
132                                          const gchar    *mbox,
133                                          PrefsAccount   *account);
134
135 static gint inc_spool_account(PrefsAccount *account);
136 static gint inc_all_spool(void);
137 static void inc_autocheck_timer_set_interval    (guint           interval);
138 static gint inc_autocheck_func                  (gpointer        data);
139
140 static void inc_notify_cmd              (gint new_msgs, 
141                                          gboolean notify);
142
143 static void inc_update_stats(gint new_msgs)
144 {
145         /* update session statistics */
146         session_stats.received += new_msgs;
147 }
148
149 /**
150  * inc_finished:
151  * @mainwin: Main window.
152  * @new_messages: TRUE if some messages have been received.
153  * 
154  * Update the folder view and the summary view after receiving
155  * messages.  If @new_messages is FALSE, this function avoids unneeded
156  * updating.
157  **/
158 static void inc_finished(MainWindow *mainwin, gboolean new_messages, gboolean autocheck)
159 {
160         if (prefs_common.scan_all_after_inc)
161                 folderview_check_new(NULL);
162
163         if (!autocheck && new_messages && prefs_common.open_inbox_on_inc) {
164                 FolderItem *item = NULL;
165
166                 if (cur_account && cur_account->inbox)
167                         item = folder_find_item_from_identifier(cur_account->inbox);
168                 if (item == NULL && cur_account && cur_account->folder)
169                         item = cur_account->folder->inbox;
170                 if (item == NULL)
171                         item = folder_get_default_inbox();
172
173                 folderview_unselect(mainwin->folderview);
174                 folderview_select(mainwin->folderview, item);
175         }
176         statusbar_progress_all(0,0,0);
177 }
178
179 void inc_mail(MainWindow *mainwin, gboolean notify)
180 {
181         gint new_msgs = 0;
182         gint account_new_msgs = 0;
183
184         if (inc_lock_count) return;
185
186         if (prefs_common.work_offline && 
187             !inc_offline_should_override(TRUE,
188                 _("Claws Mail needs network access in order "
189                   "to get mails.")))
190                 return;
191
192         inc_lock();
193         inc_autocheck_timer_remove();
194         main_window_lock(mainwin);
195
196         if (prefs_common.use_extinc && prefs_common.extinc_cmd) {
197                 /* external incorporating program */
198                 if (execute_command_line(prefs_common.extinc_cmd, FALSE, NULL) < 0) {
199                         main_window_unlock(mainwin);
200                         inc_autocheck_timer_set();
201                         inc_unlock();
202                         return;
203                 }
204         } else {
205                 account_new_msgs = inc_account_mail_real(mainwin, cur_account);
206                 if (account_new_msgs > 0)
207                         new_msgs += account_new_msgs;
208         }
209
210         inc_update_stats(new_msgs);
211         inc_finished(mainwin, new_msgs > 0, FALSE);
212         main_window_unlock(mainwin);
213         inc_notify_cmd(new_msgs, notify);
214         inc_autocheck_timer_set();
215         inc_unlock();
216 }
217
218 void inc_pop_before_smtp(PrefsAccount *acc)
219 {
220         IncProgressDialog *inc_dialog;
221         IncSession *session;
222         MainWindow *mainwin;
223
224         mainwin = mainwindow_get_mainwindow();
225
226         session = inc_session_new(acc);
227         if (!session) return;
228         POP3_SESSION(session->session)->pop_before_smtp = TRUE;
229                 
230         inc_dialog = inc_progress_dialog_create(FALSE);
231         inc_dialog->queue_list = g_list_append(inc_dialog->queue_list,
232                                                session);
233         /* FIXME: assumes to attach to first main window */
234         inc_dialog->mainwin = mainwin;
235         inc_progress_dialog_set_list(inc_dialog);
236
237         if (mainwin) {
238                 toolbar_main_set_sensitive(mainwin);
239                 main_window_set_menu_sensitive(mainwin);
240         }
241                         
242         inc_start(inc_dialog);
243 }
244
245 static gint inc_account_mail_real(MainWindow *mainwin, PrefsAccount *account)
246 {
247         IncProgressDialog *inc_dialog;
248         IncSession *session;
249         
250         switch (account->protocol) {
251         case A_IMAP4:
252         case A_NNTP:
253                 /* Melvin: bug [14]
254                  * FIXME: it should return foldeview_check_new() value.
255                  * TODO: do it when bug [19] is fixed (IMAP folder sets 
256                  * an incorrect new message count)
257                  */
258                 folderview_check_new(FOLDER(account->folder));
259                 return 0;
260         case A_POP3:
261                 session = inc_session_new(account);
262                 if (!session) return 0;
263                 
264                 inc_dialog = inc_progress_dialog_create(FALSE);
265                 inc_dialog->queue_list = g_list_append(inc_dialog->queue_list,
266                                                        session);
267                 inc_dialog->mainwin = mainwin;
268                 inc_progress_dialog_set_list(inc_dialog);
269
270                 if (mainwin) {
271                         toolbar_main_set_sensitive(mainwin);
272                         main_window_set_menu_sensitive(mainwin);
273                 }
274                         
275                 return inc_start(inc_dialog);
276
277         case A_LOCAL:
278                 return inc_spool_account(account);
279
280         default:
281                 break;
282         }
283         return 0;
284 }
285
286 gint inc_account_mail(MainWindow *mainwin, PrefsAccount *account)
287 {
288         gint new_msgs;
289
290         if (inc_lock_count) return 0;
291
292         if (account->receive_in_progress) return 0;
293
294         if (prefs_common.work_offline && 
295             !inc_offline_should_override(TRUE,
296                 _("Claws Mail needs network access in order "
297                   "to get mails.")))
298                 return 0;
299
300         inc_autocheck_timer_remove();
301         main_window_lock(mainwin);
302
303         new_msgs = inc_account_mail_real(mainwin, account);
304
305         inc_update_stats(new_msgs);
306         inc_finished(mainwin, new_msgs > 0, FALSE);
307         main_window_unlock(mainwin);
308         inc_autocheck_timer_set();
309
310         return new_msgs;
311 }
312
313 void inc_all_account_mail(MainWindow *mainwin, gboolean autocheck,
314                           gboolean notify)
315 {
316         GList *list, *queue_list = NULL;
317         IncProgressDialog *inc_dialog;
318         gint new_msgs = 0;
319         gint account_new_msgs = 0;
320         
321         if (prefs_common.work_offline && 
322             !inc_offline_should_override( (autocheck == FALSE),
323                 _("Claws Mail needs network access in order "
324                   "to get mails.")))
325                 return;
326
327         if (inc_lock_count) return;
328
329         inc_autocheck_timer_remove();
330         main_window_lock(mainwin);
331
332         list = account_get_list();
333         if (!list) {
334                 inc_update_stats(new_msgs);
335                 inc_finished(mainwin, new_msgs > 0, autocheck);
336                 main_window_unlock(mainwin);
337                 inc_notify_cmd(new_msgs, notify);
338                 inc_autocheck_timer_set();
339                 return;
340         }
341
342         if (prefs_common.use_extinc && prefs_common.extinc_cmd) {
343                 /* external incorporating program */
344                 if (execute_command_line(prefs_common.extinc_cmd, FALSE, NULL) < 0) {
345                         log_error(LOG_PROTOCOL, _("%s failed\n"), prefs_common.extinc_cmd);
346                         
347                         main_window_unlock(mainwin);
348                         inc_autocheck_timer_set();
349                         return;
350                 }
351         }
352         
353         /* check local folders */
354         account_new_msgs = inc_all_spool();
355         if (account_new_msgs > 0)
356                 new_msgs += account_new_msgs;
357
358         /* check IMAP4 / News folders */
359         for (list = account_get_list(); list != NULL; list = list->next) {
360                 PrefsAccount *account = list->data;
361                 if ((account->protocol == A_IMAP4 ||
362                      account->protocol == A_NNTP) && account->recv_at_getall) {
363                         new_msgs += folderview_check_new(FOLDER(account->folder));
364                 }
365         }
366
367         /* check POP3 accounts */
368         for (list = account_get_list(); list != NULL; list = list->next) {
369                 IncSession *session;
370                 PrefsAccount *account = list->data;
371
372                 if (account->recv_at_getall) {
373                         if (!(account->receive_in_progress)) {
374                                 session = inc_session_new(account);
375                                 if (session)
376                                         queue_list = g_list_append(queue_list, session);
377                         }
378                 }
379         }
380
381         if (queue_list) {
382                 inc_dialog = inc_progress_dialog_create(autocheck);
383                 inc_dialog->queue_list = queue_list;
384                 inc_dialog->mainwin = mainwin;
385                 inc_progress_dialog_set_list(inc_dialog);
386
387                 toolbar_main_set_sensitive(mainwin);
388                 main_window_set_menu_sensitive(mainwin);
389                 new_msgs += inc_start(inc_dialog);
390         }
391
392         inc_update_stats(new_msgs);
393         inc_finished(mainwin, new_msgs > 0, autocheck);
394         main_window_unlock(mainwin);
395         inc_notify_cmd(new_msgs, notify);
396         inc_autocheck_timer_set();
397 }
398
399 static void inc_progress_dialog_size_allocate_cb(GtkWidget *widget,
400                                          GtkAllocation *allocation)
401 {
402         cm_return_if_fail(allocation != NULL);
403
404         prefs_common.receivewin_width = allocation->width;
405         prefs_common.receivewin_height = allocation->height;
406 }
407
408 static IncProgressDialog *inc_progress_dialog_create(gboolean autocheck)
409 {
410         IncProgressDialog *dialog;
411         ProgressDialog *progress;
412         static GdkGeometry geometry;
413
414         dialog = g_new0(IncProgressDialog, 1);
415
416         progress = progress_dialog_create();
417         gtk_window_set_title(GTK_WINDOW(progress->window),
418                              _("Retrieving new messages"));
419         g_signal_connect(G_OBJECT(progress->showlog_btn), "clicked",
420                          G_CALLBACK(inc_showlog_cb), dialog);
421         g_signal_connect(G_OBJECT(progress->cancel_btn), "clicked",
422                          G_CALLBACK(inc_cancel_cb), dialog);
423         g_signal_connect(G_OBJECT(progress->window), "delete_event",
424                          G_CALLBACK(inc_dialog_delete_cb), dialog);
425         g_signal_connect(G_OBJECT(progress->window), "size_allocate",
426                          G_CALLBACK(inc_progress_dialog_size_allocate_cb), NULL);
427         /* manage_window_set_transient(GTK_WINDOW(progress->window)); */
428
429         progress_dialog_get_fraction(progress);
430
431         stock_pixbuf_gdk(STOCK_PIXMAP_COMPLETE, &okpix);
432         stock_pixbuf_gdk(STOCK_PIXMAP_CONTINUE, &currentpix);
433         stock_pixbuf_gdk(STOCK_PIXMAP_ERROR, &errorpix);
434
435         if (!geometry.min_height) {
436                 geometry.min_width = 460;
437                 geometry.min_height = 250;
438         }
439
440         gtk_window_set_geometry_hints(GTK_WINDOW(progress->window), NULL, &geometry,
441                                       GDK_HINT_MIN_SIZE);
442         gtk_widget_set_size_request(progress->window, prefs_common.receivewin_width,
443                                     prefs_common.receivewin_height);
444
445         if (prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS ||
446             (prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL &&
447              !autocheck)) {
448                 dialog->show_dialog = TRUE;
449                 gtk_widget_show_now(progress->window);
450         }
451
452         dialog->dialog = progress;
453         g_get_current_time(&dialog->progress_tv);
454         g_get_current_time(&dialog->folder_tv);
455         dialog->queue_list = NULL;
456         dialog->cur_row = 0;
457
458         inc_dialog_list = g_list_append(inc_dialog_list, dialog);
459
460         return dialog;
461 }
462
463 static void inc_progress_dialog_set_list(IncProgressDialog *inc_dialog)
464 {
465         GList *list;
466
467         for (list = inc_dialog->queue_list; list != NULL; list = list->next) {
468                 IncSession *session = list->data;
469                 Pop3Session *pop3_session = POP3_SESSION(session->session);
470
471                 session->data = inc_dialog;
472
473                 progress_dialog_list_set(inc_dialog->dialog,
474                                          -1, NULL,
475                                          pop3_session->ac_prefs->account_name,
476                                          _("Standby"));
477         }
478 }
479
480 static void inc_progress_dialog_clear(IncProgressDialog *inc_dialog)
481 {
482         progress_dialog_get_fraction(inc_dialog->dialog);
483         progress_dialog_set_label(inc_dialog->dialog, "");
484         if (inc_dialog->mainwin)
485                 main_window_progress_off(inc_dialog->mainwin);
486 }
487
488 static void inc_progress_dialog_destroy(IncProgressDialog *inc_dialog)
489 {
490         cm_return_if_fail(inc_dialog != NULL);
491
492         inc_dialog_list = g_list_remove(inc_dialog_list, inc_dialog);
493
494         if (inc_dialog->mainwin)
495                 main_window_progress_off(inc_dialog->mainwin);
496         progress_dialog_destroy(inc_dialog->dialog);
497
498         g_free(inc_dialog);
499 }
500
501 static IncSession *inc_session_new(PrefsAccount *account)
502 {
503         IncSession *session;
504
505         cm_return_val_if_fail(account != NULL, NULL);
506
507         if (account->protocol != A_POP3)
508                 return NULL;
509         if (!account->recv_server || !account->userid)
510                 return NULL;
511
512         session = g_new0(IncSession, 1);
513
514         session->session = pop3_session_new(account);
515         session->session->data = session;
516         POP3_SESSION(session->session)->drop_message = inc_drop_message;
517         session_set_recv_message_notify(session->session,
518                                         inc_recv_message, session);
519         session_set_recv_data_progressive_notify(session->session,
520                                                  inc_recv_data_progressive,
521                                                  session);
522         session_set_recv_data_notify(session->session,
523                                      inc_recv_data_finished, session);
524
525         return session;
526 }
527
528 static void inc_session_destroy(IncSession *session)
529 {
530         cm_return_if_fail(session != NULL);
531
532         session_destroy(session->session);
533         g_free(session);
534 }
535
536 static gint pop3_get_port(Pop3Session *pop3_session)
537 {
538 #ifdef USE_GNUTLS
539         return pop3_session->ac_prefs->set_popport ?
540                 pop3_session->ac_prefs->popport :
541                 pop3_session->ac_prefs->ssl_pop == SSL_TUNNEL ? 995 : 110;
542 #else
543         return pop3_session->ac_prefs->set_popport ?
544                 pop3_session->ac_prefs->popport : 110;
545 #endif
546 }
547
548 static gint inc_start(IncProgressDialog *inc_dialog)
549 {
550         IncSession *session;
551         GList *qlist;
552         Pop3Session *pop3_session;
553         IncState inc_state;
554         gint error_num = 0;
555         gint new_msgs = 0;
556         gchar *msg;
557         gchar *fin_msg;
558         FolderItem *processing, *inbox;
559         GSList *msglist, *msglist_element;
560         gboolean cancelled = FALSE;
561
562         qlist = inc_dialog->queue_list;
563         while (qlist != NULL) {
564                 GList *next = qlist->next;
565
566                 session = qlist->data;
567                 pop3_session = POP3_SESSION(session->session); 
568                 pop3_session->user = g_strdup(pop3_session->ac_prefs->userid);
569
570                 if (inc_dialog->show_dialog)
571                         manage_window_focus_in
572                                 (inc_dialog->dialog->window,
573                                  NULL, NULL);
574
575                 if (password_get(pop3_session->user,
576                                         pop3_session->ac_prefs->recv_server,
577                                         "pop3", pop3_get_port(pop3_session),
578                                         &(pop3_session->pass))) {
579                         /* NOP */;
580                 } else if ((pop3_session->pass = passwd_store_get_account(
581                                                 pop3_session->ac_prefs->account_id, PWS_ACCOUNT_RECV)) == NULL) {
582                         gchar *pass;
583
584                         pass = input_dialog_query_password_keep
585                                 (pop3_session->ac_prefs->recv_server,
586                                  pop3_session->user,
587                                  &(pop3_session->ac_prefs->session_passwd));
588
589                         if (pass) {
590                                 pop3_session->pass = pass;
591                         }
592                 }
593
594                 if (inc_dialog->show_dialog)
595                         manage_window_focus_out
596                                 (inc_dialog->dialog->window,
597                                  NULL, NULL);
598
599                 qlist = next;
600         }
601
602 #define SET_PIXMAP_AND_TEXT(pix, str)                                      \
603 {                                                                          \
604         progress_dialog_list_set(inc_dialog->dialog,                       \
605                                  inc_dialog->cur_row,                      \
606                                  pix,                                      \
607                                  NULL,                                     \
608                                  str);                                     \
609 }
610
611         for (; inc_dialog->queue_list != NULL && !cancelled; inc_dialog->cur_row++) {
612                 session = inc_dialog->queue_list->data;
613                 pop3_session = POP3_SESSION(session->session);
614                 GSList *filtered, *unfiltered;
615
616                 if (pop3_session->pass == NULL) {
617                         SET_PIXMAP_AND_TEXT(okpix, _("Cancelled"));
618                         inc_session_destroy(session);
619                         inc_dialog->queue_list =
620                                 g_list_remove(inc_dialog->queue_list, session);
621                         continue;
622                 }
623
624                 inc_progress_dialog_clear(inc_dialog);
625                 progress_dialog_scroll_to_row(inc_dialog->dialog,
626                                               inc_dialog->cur_row);
627
628                 SET_PIXMAP_AND_TEXT(currentpix, _("Retrieving"));
629
630                 /* begin POP3 session */
631                 inc_state = inc_pop3_session_do(session);
632
633                 switch (inc_state) {
634                 case INC_SUCCESS:
635                         if (pop3_session->cur_total_num > 0)
636                                 msg = g_strdup_printf(
637                                         ngettext("Done (%d message (%s) received)",
638                                                  "Done (%d messages (%s) received)",
639                                          pop3_session->cur_total_num),
640                                          pop3_session->cur_total_num,
641                                          to_human_readable((goffset)pop3_session->cur_total_recv_bytes));
642                         else
643                                 msg = g_strdup_printf(_("Done (no new messages)"));
644                         SET_PIXMAP_AND_TEXT(okpix, msg);
645                         g_free(msg);
646                         break;
647                 case INC_CONNECT_ERROR:
648                         SET_PIXMAP_AND_TEXT(errorpix, _("Connection failed"));
649                         break;
650                 case INC_AUTH_FAILED:
651                         SET_PIXMAP_AND_TEXT(errorpix, _("Auth failed"));
652                         if (pop3_session->ac_prefs->session_passwd) {
653                                 g_free(pop3_session->ac_prefs->session_passwd);
654                                 pop3_session->ac_prefs->session_passwd = NULL;
655                         }
656                         break;
657                 case INC_LOCKED:
658                         SET_PIXMAP_AND_TEXT(errorpix, _("Locked"));
659                         break;
660                 case INC_ERROR:
661                 case INC_NO_SPACE:
662                 case INC_IO_ERROR:
663                 case INC_SOCKET_ERROR:
664                 case INC_EOF:
665                         SET_PIXMAP_AND_TEXT(errorpix, _("Error"));
666                         break;
667                 case INC_TIMEOUT:
668                         SET_PIXMAP_AND_TEXT(errorpix, _("Timeout"));
669                         break;
670                 case INC_CANCEL:
671                         SET_PIXMAP_AND_TEXT(okpix, _("Cancelled"));
672                         if (!inc_dialog->show_dialog)
673                                 cancelled = TRUE;
674                         break;
675                 default:
676                         break;
677                 }
678                 
679                 if (pop3_session->error_val == PS_AUTHFAIL) {
680                         if(!prefs_common.no_recv_err_panel) {
681                                 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
682                                     ((prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL) && focus_window))
683                                         manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
684                         }
685                 }
686
687                 /* CLAWS: perform filtering actions on dropped message */
688                 /* CLAWS: get default inbox (perhaps per account) */
689                 if (pop3_session->ac_prefs->inbox) {
690                         /* CLAWS: get destination folder / mailbox */
691                         inbox = folder_find_item_from_identifier(pop3_session->ac_prefs->inbox);
692                         if (!inbox)
693                                 inbox = folder_get_default_inbox();
694                 } else
695                         inbox = folder_get_default_inbox();
696
697                 /* get list of messages in processing */
698                 processing = folder_get_default_processing(pop3_session->ac_prefs->account_id);
699                 folder_item_scan(processing);
700                 msglist = folder_item_get_msg_list(processing);
701
702                 /* process messages */
703                 folder_item_update_freeze();
704                 
705                 procmsg_msglist_filter(msglist, pop3_session->ac_prefs, 
706                                 &filtered, &unfiltered, 
707                                 pop3_session->ac_prefs->filter_on_recv);
708
709                 filtering_move_and_copy_msgs(msglist);
710                 if (unfiltered != NULL)         
711                         folder_item_move_msgs(inbox, unfiltered);
712
713                 for(msglist_element = msglist; msglist_element != NULL; 
714                     msglist_element = msglist_element->next) {
715                         procmsg_msginfo_free((MsgInfo**)&(msglist_element->data));
716                 }
717                 folder_item_update_thaw();
718                 
719                 g_slist_free(msglist);
720                 g_slist_free(filtered);
721                 g_slist_free(unfiltered);
722
723                 statusbar_pop_all();
724
725                 new_msgs += pop3_session->cur_total_num;
726
727                 pop3_write_uidl_list(pop3_session);
728
729                 if (inc_state != INC_SUCCESS && inc_state != INC_CANCEL) {
730                         error_num++;
731                         if (inc_dialog->show_dialog)
732                                 manage_window_focus_in
733                                         (inc_dialog->dialog->window,
734                                          NULL, NULL);
735                         inc_put_error(inc_state, pop3_session);
736                         if (inc_dialog->show_dialog)
737                                 manage_window_focus_out
738                                         (inc_dialog->dialog->window,
739                                          NULL, NULL);
740                         if (inc_state == INC_NO_SPACE ||
741                             inc_state == INC_IO_ERROR)
742                                 break;
743                 }
744                 folder_item_free_cache(processing, TRUE);
745
746                 inc_session_destroy(session);
747                 inc_dialog->queue_list =
748                         g_list_remove(inc_dialog->queue_list, session);
749         }
750
751 #undef SET_PIXMAP_AND_TEXT
752
753         if (new_msgs > 0)
754                 fin_msg = g_strdup_printf(ngettext("Finished (%d new message)",
755                                                    "Finished (%d new messages)",
756                                                    new_msgs), new_msgs);
757         else
758                 fin_msg = g_strdup_printf(_("Finished (no new messages)"));
759
760         progress_dialog_set_label(inc_dialog->dialog, fin_msg);
761
762         while (inc_dialog->queue_list != NULL) {
763                 session = inc_dialog->queue_list->data;
764                 inc_session_destroy(session);
765                 inc_dialog->queue_list =
766                         g_list_remove(inc_dialog->queue_list, session);
767         }
768
769         if (prefs_common.close_recv_dialog || !inc_dialog->show_dialog)
770                 inc_progress_dialog_destroy(inc_dialog);
771         else {
772                 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window),
773                                      fin_msg);
774                 gtk_button_set_label(GTK_BUTTON(inc_dialog->dialog->cancel_btn),
775                                      GTK_STOCK_CLOSE);
776         }
777
778         g_free(fin_msg);
779
780         return new_msgs;
781 }
782
783 static IncState inc_pop3_session_do(IncSession *session)
784 {
785         Pop3Session *pop3_session = POP3_SESSION(session->session);
786         IncProgressDialog *inc_dialog = (IncProgressDialog *)session->data;
787         PrefsAccount *ac = pop3_session->ac_prefs;
788         gchar *server;
789         gchar *account_name;
790         gushort port;
791         gchar *buf;
792         ProxyInfo *proxy_info = NULL;
793
794         debug_print("getting new messages of account %s...\n",
795                     ac->account_name);
796                     
797         ac->last_pop_login_time = time(NULL);
798
799         buf = g_strdup_printf(_("%s: Retrieving new messages"),
800                               ac->recv_server);
801         gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window), buf);
802         g_free(buf);
803
804         server = ac->recv_server;
805         account_name = ac->account_name;
806         port = pop3_get_port(pop3_session);
807
808 #ifdef USE_GNUTLS
809         SESSION(pop3_session)->ssl_type = ac->ssl_pop;
810         if (ac->ssl_pop != SSL_NONE)
811                 SESSION(pop3_session)->nonblocking =
812                         ac->use_nonblocking_ssl;
813 #else
814         if (ac->ssl_pop != SSL_NONE) {
815                 if (alertpanel_full(_("Insecure connection"),
816                         _("This connection is configured to be secured "
817                           "using SSL/TLS, but SSL/TLS is not available "
818                           "in this build of Claws Mail. \n\n"
819                           "Do you want to continue connecting to this "
820                           "server? The communication would not be "
821                           "secure."),
822                           GTK_STOCK_CANCEL, _("Con_tinue connecting"), 
823                           NULL, FALSE, NULL, ALERT_WARNING,
824                           G_ALERTDEFAULT) != G_ALERTALTERNATE)
825                         return INC_CANCEL;
826         }
827 #endif
828
829         buf = g_strdup_printf(_("Account '%s': Connecting to POP3 server: %s:%d..."),
830                                 account_name, server, port);
831         statusbar_print_all("%s", buf);
832         log_message(LOG_PROTOCOL, "%s\n", buf);
833
834         progress_dialog_set_label(inc_dialog->dialog, buf);
835
836         if (ac->use_proxy) {
837                 if (ac->use_default_proxy) {
838                         proxy_info = (ProxyInfo *)&(prefs_common.proxy_info);
839                         if (proxy_info->use_proxy_auth)
840                                 proxy_info->proxy_pass = passwd_store_get(PWS_CORE, PWS_CORE_PROXY,
841                                         PWS_CORE_PROXY_PASS);
842                 } else {
843                         proxy_info = (ProxyInfo *)&(ac->proxy_info);
844                         if (proxy_info->use_proxy_auth)
845                                 proxy_info->proxy_pass = passwd_store_get_account(ac->account_id,
846                                         PWS_ACCOUNT_PROXY_PASS);
847                 }
848         }
849         SESSION(pop3_session)->proxy_info = proxy_info;
850
851         GTK_EVENTS_FLUSH();
852         g_free(buf);
853
854         session_set_timeout(SESSION(pop3_session),
855                             prefs_common.io_timeout_secs * 1000);
856         
857         if (session_connect(SESSION(pop3_session), server, port) < 0) {
858                 if(!prefs_common.no_recv_err_panel) {
859                         if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
860                             ((prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL) && focus_window)) {
861                                 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
862                         }
863                         alertpanel_error(_("Can't connect to POP3 server: %s:%d"),
864                                          server, port);
865                         manage_window_focus_out(inc_dialog->dialog->window, NULL, NULL);
866                 } else {
867                         log_error(LOG_PROTOCOL, _("Can't connect to POP3 server: %s:%d\n"),
868                             server, port);
869                 }
870                 session->inc_state = INC_CONNECT_ERROR;
871                 statusbar_pop_all();
872                 return INC_CONNECT_ERROR;
873         }
874
875         while (session_is_running(SESSION(pop3_session)) &&
876                session->inc_state != INC_CANCEL)
877                 gtk_main_iteration();
878
879         if (session->inc_state == INC_SUCCESS) {
880                 switch (pop3_session->error_val) {
881                 case PS_SUCCESS:
882                         switch (SESSION(pop3_session)->state) {
883                         case SESSION_ERROR:
884                                 if (pop3_session->state == POP3_READY)
885                                         session->inc_state = INC_CONNECT_ERROR;
886                                 else
887                                         session->inc_state = INC_ERROR;
888                                 break;
889                         case SESSION_EOF:
890                                 session->inc_state = INC_EOF;
891                                 break;
892                         case SESSION_TIMEOUT:
893                                 session->inc_state = INC_TIMEOUT;
894                                 break;
895                         default:
896                                 session->inc_state = INC_SUCCESS;
897                                 break;
898                         }
899                         break;
900                 case PS_AUTHFAIL:
901                         session->inc_state = INC_AUTH_FAILED;
902                         break;
903                 case PS_IOERR:
904                         session->inc_state = INC_IO_ERROR;
905                         break;
906                 case PS_SOCKET:
907                         session->inc_state = INC_SOCKET_ERROR;
908                         break;
909                 case PS_LOCKBUSY:
910                         session->inc_state = INC_LOCKED;
911                         break;
912                 default:
913                         session->inc_state = INC_ERROR;
914                         break;
915                 }
916         }
917
918         session_disconnect(SESSION(pop3_session));
919         statusbar_pop_all();
920
921         return session->inc_state;
922 }
923
924 static void inc_progress_dialog_update(IncProgressDialog *inc_dialog,
925                                        IncSession *inc_session)
926 {
927         inc_progress_dialog_set_label(inc_dialog, inc_session);
928         inc_progress_dialog_set_progress(inc_dialog, inc_session);
929 }
930
931 static void inc_progress_dialog_set_label(IncProgressDialog *inc_dialog,
932                                           IncSession *inc_session)
933 {
934         ProgressDialog *dialog = inc_dialog->dialog;
935         Pop3Session *session;
936
937         cm_return_if_fail(inc_session != NULL);
938
939         session = POP3_SESSION(inc_session->session);
940
941         switch (session->state) {
942         case POP3_GREETING:
943                 break;
944         case POP3_GETAUTH_USER:
945         case POP3_GETAUTH_PASS:
946         case POP3_GETAUTH_APOP:
947                 progress_dialog_set_label(dialog, _("Authenticating..."));
948                 statusbar_pop_all();
949                 statusbar_print_all(_("Retrieving messages from %s (%s)..."),
950                                     SESSION(session)->server,
951                                     session->ac_prefs->account_name);
952                 break;
953         case POP3_GETRANGE_STAT:
954                 progress_dialog_set_label
955                         (dialog, _("Getting the number of new messages (STAT)..."));
956                 break;
957         case POP3_GETRANGE_LAST:
958                 progress_dialog_set_label
959                         (dialog, _("Getting the number of new messages (LAST)..."));
960                 break;
961         case POP3_GETRANGE_UIDL:
962                 progress_dialog_set_label
963                         (dialog, _("Getting the number of new messages (UIDL)..."));
964                 break;
965         case POP3_GETSIZE_LIST:
966                 progress_dialog_set_label
967                         (dialog, _("Getting the size of messages (LIST)..."));
968                 break;
969         case POP3_RETR:
970         case POP3_RETR_RECV:
971         case POP3_DELETE:
972                 break;
973         case POP3_LOGOUT:
974                 progress_dialog_set_label(dialog, _("Quitting"));
975                 break;
976         default:
977                 break;
978         }
979 }
980
981 static void inc_progress_dialog_set_progress(IncProgressDialog *inc_dialog,
982                                              IncSession *inc_session)
983 {
984         gchar buf[MESSAGEBUFSIZE];
985         Pop3Session *pop3_session = POP3_SESSION(inc_session->session);
986         gchar *total_size_str;
987         gint cur_total;
988         gint total;
989
990         if (!pop3_session->new_msg_exist) return;
991
992         cur_total = inc_session->cur_total_bytes;
993         total = pop3_session->total_bytes;
994         if (pop3_session->state == POP3_RETR ||
995             pop3_session->state == POP3_RETR_RECV ||
996             pop3_session->state == POP3_DELETE) {
997                 Xstrdup_a(total_size_str, to_human_readable((goffset)total), return);
998                 g_snprintf(buf, sizeof(buf),
999                            _("Retrieving message (%d / %d) (%s / %s)"),
1000                            pop3_session->cur_msg, pop3_session->count,
1001                            to_human_readable((goffset)cur_total), total_size_str);
1002                 progress_dialog_set_label(inc_dialog->dialog, buf);
1003         }
1004
1005         progress_dialog_set_fraction
1006                 (inc_dialog->dialog, (total == 0) ? 0: (gfloat)cur_total / (gfloat)total);
1007
1008         statusbar_progress_all(pop3_session->cur_msg, pop3_session->count, 1);
1009
1010         if (pop3_session->cur_total_num > 0) {
1011                 g_snprintf(buf, sizeof(buf),
1012                            ngettext("Retrieving (%d message (%s) received)",
1013                                     "Retrieving (%d messages (%s) received)",
1014                                     pop3_session->cur_total_num),
1015                            pop3_session->cur_total_num,
1016                            to_human_readable
1017                            ((goffset)pop3_session->cur_total_recv_bytes));
1018                 progress_dialog_list_set_status(inc_dialog->dialog,
1019                                                 inc_dialog->cur_row,
1020                                                 buf);
1021         }
1022 }
1023
1024 static void inc_progress_dialog_update_periodic(IncProgressDialog *inc_dialog,
1025                                                 IncSession *inc_session)
1026 {
1027         GTimeVal tv_cur;
1028         GTimeVal tv_result;
1029         gint msec;
1030
1031         g_get_current_time(&tv_cur);
1032
1033         tv_result.tv_sec = tv_cur.tv_sec - inc_dialog->progress_tv.tv_sec;
1034         tv_result.tv_usec = tv_cur.tv_usec - inc_dialog->progress_tv.tv_usec;
1035         if (tv_result.tv_usec < 0) {
1036                 tv_result.tv_sec--;
1037                 tv_result.tv_usec += G_USEC_PER_SEC;
1038         }
1039
1040         msec = tv_result.tv_sec * 1000 + tv_result.tv_usec / 1000;
1041         if (msec > PROGRESS_UPDATE_INTERVAL) {
1042                 inc_progress_dialog_update(inc_dialog, inc_session);
1043                 inc_dialog->progress_tv.tv_sec = tv_cur.tv_sec;
1044                 inc_dialog->progress_tv.tv_usec = tv_cur.tv_usec;
1045         }
1046 }
1047
1048 static gint inc_recv_data_progressive(Session *session, guint cur_len,
1049                                       guint total_len, gpointer data)
1050 {
1051         IncSession *inc_session = (IncSession *)data;
1052         Pop3Session *pop3_session = POP3_SESSION(session);
1053         IncProgressDialog *inc_dialog;
1054         gint cur_total;
1055
1056         cm_return_val_if_fail(inc_session != NULL, -1);
1057
1058         if (pop3_session->state != POP3_RETR &&
1059             pop3_session->state != POP3_RETR_RECV &&
1060             pop3_session->state != POP3_DELETE &&
1061             pop3_session->state != POP3_LOGOUT) return 0;
1062
1063         if (!pop3_session->new_msg_exist) return 0;
1064
1065         cur_total = pop3_session->cur_total_bytes + cur_len;
1066         if (cur_total > pop3_session->total_bytes)
1067                 cur_total = pop3_session->total_bytes;
1068         inc_session->cur_total_bytes = cur_total;
1069
1070         inc_dialog = (IncProgressDialog *)inc_session->data;
1071         inc_progress_dialog_update_periodic(inc_dialog, inc_session);
1072
1073         return 0;
1074 }
1075
1076 static gint inc_recv_data_finished(Session *session, guint len, gpointer data)
1077 {
1078         IncSession *inc_session = (IncSession *)data;
1079         IncProgressDialog *inc_dialog;
1080
1081         cm_return_val_if_fail(inc_session != NULL, -1);
1082
1083         inc_dialog = (IncProgressDialog *)inc_session->data;
1084
1085         inc_recv_data_progressive(session, 0, 0, inc_session);
1086
1087         if (POP3_SESSION(session)->state == POP3_LOGOUT) {
1088                 inc_progress_dialog_update(inc_dialog, inc_session);
1089         }
1090
1091         return 0;
1092 }
1093
1094 static gint inc_recv_message(Session *session, const gchar *msg, gpointer data)
1095 {
1096         IncSession *inc_session = (IncSession *)data;
1097         IncProgressDialog *inc_dialog;
1098
1099         cm_return_val_if_fail(inc_session != NULL, -1);
1100
1101         inc_dialog = (IncProgressDialog *)inc_session->data;
1102
1103         switch (POP3_SESSION(session)->state) {
1104         case POP3_GETAUTH_USER:
1105         case POP3_GETAUTH_PASS:
1106         case POP3_GETAUTH_APOP:
1107         case POP3_GETRANGE_STAT:
1108         case POP3_GETRANGE_LAST:
1109         case POP3_GETRANGE_UIDL:
1110         case POP3_GETSIZE_LIST:
1111                 inc_progress_dialog_update(inc_dialog, inc_session);
1112                 break;
1113         case POP3_RETR:
1114                 inc_recv_data_progressive(session, 0, 0, inc_session);
1115                 break;
1116         case POP3_LOGOUT:
1117                 inc_progress_dialog_update(inc_dialog, inc_session);
1118                 break;
1119         default:
1120                 break;
1121         }
1122
1123         return 0;
1124 }
1125
1126 static gint inc_drop_message(Pop3Session *session, const gchar *file)
1127 {
1128         FolderItem *inbox;
1129         FolderItem *dropfolder;
1130         IncSession *inc_session = (IncSession *)(SESSION(session)->data);
1131         gint msgnum;
1132
1133         cm_return_val_if_fail(inc_session != NULL, -1);
1134
1135         if (session->ac_prefs->inbox) {
1136                 inbox = folder_find_item_from_identifier
1137                         (session->ac_prefs->inbox);
1138                 if (!inbox)
1139                         inbox = folder_get_default_inbox();
1140         } else
1141                 inbox = folder_get_default_inbox();
1142         if (!inbox) {
1143                 claws_unlink(file);
1144                 return -1;
1145         }
1146
1147         /* CLAWS: claws uses a global .processing folder for the filtering. */
1148         dropfolder = folder_get_default_processing(session->ac_prefs->account_id);
1149
1150         /* add msg file to drop folder */
1151         if ((msgnum = folder_item_add_msg(
1152                         dropfolder, file, NULL, TRUE)) < 0) {
1153                 claws_unlink(file);
1154                 return -1;
1155         }
1156
1157         return 0;
1158 }
1159
1160 static void inc_put_error(IncState istate, Pop3Session *session)
1161 {
1162         gchar *log_msg = NULL;
1163         gchar *err_msg = NULL;
1164         gboolean fatal_error = FALSE;
1165
1166         switch (istate) {
1167         case INC_CONNECT_ERROR:
1168                 fatal_error = TRUE;
1169                 if (prefs_common.no_recv_err_panel)
1170                         break;
1171                 err_msg = g_strdup_printf(_("Connection to %s:%d failed."),
1172                                           SESSION(session)->server, 
1173                                           SESSION(session)->port);
1174                 break;
1175         case INC_ERROR:
1176                 log_msg = _("Error occurred while processing mail.");
1177                 fatal_error = TRUE;
1178                 if (prefs_common.no_recv_err_panel)
1179                         break;
1180                 if (session->error_msg)
1181                         err_msg = g_strdup_printf
1182                                 (_("Error occurred while processing mail:\n%s"),
1183                                  session->error_msg);
1184                 else
1185                         err_msg = g_strdup(log_msg);
1186                 break;
1187         case INC_NO_SPACE:
1188                 log_msg = _("No disk space left.");
1189                 err_msg = g_strdup(log_msg);
1190                 fatal_error = TRUE;
1191                 break;
1192         case INC_IO_ERROR:
1193                 log_msg = _("Can't write file.");
1194                 err_msg = g_strdup(log_msg);
1195                 fatal_error = TRUE;
1196                 break;
1197         case INC_SOCKET_ERROR:
1198                 log_msg = _("Socket error.");
1199                 if (prefs_common.no_recv_err_panel)
1200                         break;
1201                 err_msg = g_strdup_printf(_("Socket error on connection to %s:%d."),
1202                                           SESSION(session)->server, 
1203                                           SESSION(session)->port);
1204                 break;
1205         case INC_EOF:
1206                 log_msg = _("Connection closed by the remote host.");
1207                 if (prefs_common.no_recv_err_panel)
1208                         break;
1209                 err_msg = g_strdup_printf(_("Connection to %s:%d closed by the remote host."), 
1210                                           SESSION(session)->server, 
1211                                           SESSION(session)->port);
1212                 break;
1213         case INC_LOCKED:
1214                 log_msg = _("Mailbox is locked.");
1215                 if (prefs_common.no_recv_err_panel)
1216                         break;
1217                 if (session->error_msg)
1218                         err_msg = g_strdup_printf(_("Mailbox is locked:\n%s"),
1219                                                   session->error_msg);
1220                 else
1221                         err_msg = g_strdup(log_msg);
1222                 break;
1223         case INC_AUTH_FAILED:
1224                 log_msg = _("Authentication failed.");
1225                 fatal_error = TRUE;
1226                 if (prefs_common.no_recv_err_panel)
1227                         break;
1228                 if (session->error_msg)
1229                         err_msg = g_strdup_printf
1230                                 (_("Authentication failed:\n%s"), session->error_msg);
1231                 else
1232                         err_msg = g_strdup(log_msg);
1233                 break;
1234         case INC_TIMEOUT:
1235                 log_msg = _("Session timed out. You may be able to "
1236                             "recover by increasing the timeout value in "
1237                             "Preferences/Other/Miscellaneous.");
1238                 if (prefs_common.no_recv_err_panel)
1239                         break;
1240                 err_msg = g_strdup_printf(_("Connection to %s:%d timed out."), 
1241                                           SESSION(session)->server, 
1242                                           SESSION(session)->port);
1243                 break;
1244         default:
1245                 break;
1246         }
1247
1248         if (log_msg) {
1249                 if (fatal_error)
1250                         log_error(LOG_PROTOCOL, "%s\n", log_msg);
1251                 else
1252                         log_warning(LOG_PROTOCOL, "%s\n", log_msg);
1253         }
1254         if (prefs_common.no_recv_err_panel && fatal_error)
1255                 mainwindow_show_error();
1256
1257         if (err_msg) {
1258                 alertpanel_error_log("%s", err_msg);
1259                 g_free(err_msg);
1260         }
1261 }
1262
1263 static void inc_cancel(IncProgressDialog *dialog)
1264 {
1265         IncSession *session;
1266
1267         cm_return_if_fail(dialog != NULL);
1268
1269         if (dialog->queue_list == NULL) {
1270                 inc_progress_dialog_destroy(dialog);
1271                 return;
1272         }
1273
1274         session = dialog->queue_list->data;
1275
1276         session->inc_state = INC_CANCEL;
1277
1278         log_message(LOG_PROTOCOL, _("Incorporation cancelled\n"));
1279 }
1280
1281 gboolean inc_is_active(void)
1282 {
1283         return (inc_dialog_list != NULL);
1284 }
1285
1286 void inc_cancel_all(void)
1287 {
1288         GList *cur;
1289
1290         for (cur = inc_dialog_list; cur != NULL; cur = cur->next)
1291                 inc_cancel((IncProgressDialog *)cur->data);
1292 }
1293
1294 static void inc_showlog_cb(GtkWidget *widget, gpointer data)
1295 {
1296         MainWindow *mainwin = mainwindow_get_mainwindow();
1297
1298         log_window_show(mainwin->logwin);
1299 }
1300
1301 static void inc_cancel_cb(GtkWidget *widget, gpointer data)
1302 {
1303         inc_cancel((IncProgressDialog *)data);
1304 }
1305
1306 static gint inc_dialog_delete_cb(GtkWidget *widget, GdkEventAny *event,
1307                                  gpointer data)
1308 {
1309         IncProgressDialog *dialog = (IncProgressDialog *)data;
1310
1311         if (dialog->queue_list == NULL)
1312                 inc_progress_dialog_destroy(dialog);
1313
1314         return TRUE;
1315 }
1316
1317 static gint inc_spool_account(PrefsAccount *account)
1318 {
1319         FolderItem *inbox;
1320         gchar *mbox;
1321         gint result;
1322
1323         if (account->local_inbox) {
1324                 inbox = folder_find_item_from_identifier(account->local_inbox);
1325                 if (!inbox)
1326                         inbox = folder_get_default_inbox();
1327         } else
1328                 inbox = folder_get_default_inbox();
1329
1330         if (account->local_mbox) {
1331                 if (is_file_exist(account->local_mbox))
1332                         mbox = g_strdup(account->local_mbox);
1333                 else if (is_dir_exist(account->local_mbox)) 
1334                         mbox = g_strconcat(account->local_mbox, G_DIR_SEPARATOR_S,
1335                                            g_get_user_name(), NULL);
1336                 else {
1337                         debug_print("%s: local mailbox not found.\n", 
1338                                     account->local_mbox);
1339                         return -1;
1340                 }
1341         } else {
1342                 debug_print("local mailbox not set in account info.\n");
1343                 return -1;
1344         }       
1345
1346         result = get_spool(inbox, mbox, account);
1347         g_free(mbox);
1348         
1349         statusbar_pop_all();
1350         
1351         return result;
1352 }
1353
1354 static gint inc_all_spool(void)
1355 {
1356         GList *list = NULL;
1357         gint new_msgs = 0;
1358         gint account_new_msgs = 0;
1359
1360         list = account_get_list();
1361         if (!list) return 0;
1362
1363         for (; list != NULL; list = list->next) {
1364                 PrefsAccount *account = list->data;
1365
1366                 if ((account->protocol == A_LOCAL) &&
1367                     (account->recv_at_getall)) {
1368                         account_new_msgs = inc_spool_account(account);
1369                         if (account_new_msgs > 0)
1370                                 new_msgs += account_new_msgs;
1371                 }
1372         }
1373
1374         return new_msgs;
1375 }
1376
1377 static gint get_spool(FolderItem *dest, const gchar *mbox, PrefsAccount *account)
1378 {
1379         gint msgs, size;
1380         gint lockfd;
1381         gchar tmp_mbox[MAXPATHLEN + 1];
1382
1383         cm_return_val_if_fail(dest != NULL, -1);
1384         cm_return_val_if_fail(mbox != NULL, -1);
1385         cm_return_val_if_fail(account != NULL, -1);
1386
1387         if (!is_file_exist(mbox) || (size = get_file_size(mbox)) == 0) {
1388                 debug_print("%s: no messages in local mailbox.\n", mbox);
1389                 return 0;
1390         } else if (size < 0)
1391                 return -1;
1392
1393         if ((lockfd = lock_mbox(mbox, LOCK_FLOCK)) < 0)
1394                 return -1;
1395
1396         g_snprintf(tmp_mbox, sizeof(tmp_mbox), "%s%ctmpmbox.%p",
1397                    get_tmp_dir(), G_DIR_SEPARATOR, mbox);
1398
1399         if (copy_mbox(lockfd, tmp_mbox) < 0) {
1400                 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1401                 return -1;
1402         }
1403
1404         debug_print("Getting new messages from %s into %s...\n",
1405                     mbox, dest->path);
1406
1407         msgs = proc_mbox(dest, tmp_mbox, account->filter_on_recv, account);
1408
1409         claws_unlink(tmp_mbox);
1410         if (msgs >= 0) empty_mbox(mbox);
1411         unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1412
1413         return msgs;
1414 }
1415
1416 void inc_lock_real(void)
1417 {
1418         inc_lock_count++;
1419 }
1420
1421 void inc_unlock_real(void)
1422 {
1423         if (inc_lock_count > 0)
1424                 inc_lock_count--;
1425 }
1426
1427 static guint autocheck_timer = 0;
1428 static gpointer autocheck_data = NULL;
1429
1430 static void inc_notify_cmd(gint new_msgs, gboolean notify)
1431 {
1432         gchar *buf, *numpos, *ret_str;
1433         gssize by_read = 0, by_written = 0;
1434
1435         if (!(new_msgs && notify && prefs_common.newmail_notify_cmd &&
1436             *prefs_common.newmail_notify_cmd))
1437                      return;
1438
1439         buf = g_strdup(prefs_common.newmail_notify_cmd);
1440         if ((numpos = strstr(buf, "%d")) != NULL) {
1441                 gchar *buf2;
1442
1443                 *numpos = '\0';
1444                 buf2 = g_strdup_printf("%s%d%s", buf, new_msgs, numpos + 2);
1445                 g_free(buf);
1446                 buf = buf2;
1447         }
1448
1449         ret_str = g_locale_from_utf8(buf, strlen(buf), &by_read, &by_written,
1450                                      NULL);
1451         if (ret_str && by_written) {
1452                 g_free(buf);
1453                 buf = ret_str;
1454         }
1455         debug_print("executing new mail notification command: %s\n", buf);
1456         execute_command_line(buf, TRUE, NULL);
1457
1458         g_free(buf);
1459 }
1460
1461 void inc_autocheck_timer_init(MainWindow *mainwin)
1462 {
1463         autocheck_data = mainwin;
1464         inc_autocheck_timer_set();
1465 }
1466
1467 static void inc_autocheck_timer_set_interval(guint interval)
1468 {
1469         inc_autocheck_timer_remove();
1470         /* last test is to avoid re-enabling auto_check after modifying 
1471            the common preferences */
1472         if (prefs_common.autochk_newmail && autocheck_data
1473             && prefs_common.work_offline == FALSE) {
1474                 if (interval % 1000 == 0)
1475                         autocheck_timer =
1476                                 g_timeout_add_seconds(interval/1000, inc_autocheck_func, autocheck_data);
1477                 else
1478                         autocheck_timer = g_timeout_add
1479                                 (interval, inc_autocheck_func, autocheck_data);
1480                 debug_print("added timer = %d\n", autocheck_timer);
1481         }
1482 }
1483
1484 void inc_autocheck_timer_set(void)
1485 {
1486         inc_autocheck_timer_set_interval(prefs_common.autochk_itv * 1000);
1487 }
1488
1489 void inc_autocheck_timer_remove(void)
1490 {
1491         if (autocheck_timer) {
1492                 debug_print("removed timer = %d\n", autocheck_timer);
1493                 g_source_remove(autocheck_timer);
1494                 autocheck_timer = 0;
1495         }
1496 }
1497
1498 static gint inc_autocheck_func(gpointer data)
1499 {
1500         MainWindow *mainwin = (MainWindow *)data;
1501
1502         if (inc_lock_count) {
1503                 debug_print("autocheck is locked.\n");
1504                 inc_autocheck_timer_set_interval(1000);
1505                 return FALSE;
1506         }
1507
1508         inc_all_account_mail(mainwin, TRUE, prefs_common.newmail_notify_auto);
1509
1510         return FALSE;
1511 }
1512
1513 gboolean inc_offline_should_override(gboolean force_ask, const gchar *msg)
1514 {
1515         static time_t overridden_yes = 0;
1516         static time_t overridden_no  = 0;
1517         int length = 10; /* minutes */
1518         gint answer = G_ALERTDEFAULT;
1519
1520 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1521         /* If no network connection is available, override is not possible */
1522         if(!networkmanager_is_online(NULL))
1523                 return FALSE;
1524 #endif
1525
1526         if (prefs_common.autochk_newmail)
1527                 length = prefs_common.autochk_itv; /* minutes */
1528
1529         if (force_ask) {
1530                 overridden_no = (time_t)0;
1531         }
1532
1533         if (prefs_common.work_offline) {
1534                 gchar *tmp = NULL;
1535                 
1536                 if (time(NULL) - overridden_yes < length * 60) /* seconds */
1537                          return TRUE;
1538                 else if (time(NULL) - overridden_no < length * 60) /* seconds */
1539                          return FALSE;
1540
1541                 if (!force_ask)
1542                         tmp = g_strdup_printf(
1543                                 _("%s%sYou're working offline. Override for %d minutes?"),
1544                                 msg?msg:"", 
1545                                 msg?"\n\n":"",
1546                                 length);
1547                 else
1548                         tmp = g_strdup_printf(
1549                                 _("%s%sYou're working offline. Override?"),
1550                                 msg?msg:"", 
1551                                 msg?"\n\n":"");
1552
1553                 answer = alertpanel(_("Offline warning"), 
1554                                tmp,
1555                                GTK_STOCK_NO, GTK_STOCK_YES,
1556                                 !force_ask? _("On_ly once"):NULL, ALERTFOCUS_SECOND);
1557                 g_free(tmp);
1558                 if (answer == G_ALERTALTERNATE) {
1559                         overridden_yes = time(NULL);
1560                         return TRUE;
1561                 } else if (answer == G_ALERTDEFAULT) {
1562                         if (!force_ask)
1563                                 overridden_no  = time(NULL);
1564                         return FALSE;
1565                 } else {
1566                         overridden_yes = (time_t)0;
1567                         overridden_no  = (time_t)0;
1568                         return TRUE;
1569                 }
1570         }
1571         return TRUE;
1572 }