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