2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2016 Hiroyuki Yamamoto and the Claws Mail team
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.
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.
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/>.
21 #include "claws-features.h"
27 #include <glib/gi18n.h>
35 #include "mainwindow.h"
36 #include "folderview.h"
37 #include "summaryview.h"
38 #include "prefs_common.h"
39 #include "prefs_account.h"
48 #include "file-utils.h"
51 #include "statusbar.h"
53 #include "manage_window.h"
54 #include "stock_pixmap.h"
55 #include "progressdialog.h"
56 #include "inputdialog.h"
57 #include "alertpanel.h"
59 #include "filtering.h"
62 #include "logwindow.h"
63 #include "passwordstore.h"
65 extern SessionStats session_stats;
67 static GList *inc_dialog_list = NULL;
69 static time_t inc_offline_overridden_yes = 0;
70 static time_t inc_offline_overridden_no = 0;
72 guint inc_lock_count = 0;
74 static GdkPixbuf *currentpix;
75 static GdkPixbuf *errorpix;
76 static GdkPixbuf *okpix;
78 #define MESSAGEBUFSIZE 8192
80 static void inc_update_stats(gint new_msgs);
81 static void inc_finished (MainWindow *mainwin,
82 gboolean new_messages,
84 static gint inc_account_mail_real (MainWindow *mainwin,
85 PrefsAccount *account);
87 static IncProgressDialog *inc_progress_dialog_create
89 static void inc_progress_dialog_set_list(IncProgressDialog *inc_dialog);
90 static void inc_progress_dialog_destroy (IncProgressDialog *inc_dialog);
92 static IncSession *inc_session_new (PrefsAccount *account);
93 static void inc_session_destroy (IncSession *session);
94 static gint inc_start (IncProgressDialog *inc_dialog);
95 static IncState inc_pop3_session_do (IncSession *session);
97 static void inc_progress_dialog_update (IncProgressDialog *inc_dialog,
98 IncSession *inc_session);
100 static void inc_progress_dialog_set_label
101 (IncProgressDialog *inc_dialog,
102 IncSession *inc_session);
103 static void inc_progress_dialog_set_progress
104 (IncProgressDialog *inc_dialog,
105 IncSession *inc_session);
107 static void inc_progress_dialog_update_periodic
108 (IncProgressDialog *inc_dialog,
109 IncSession *inc_session);
111 static gint inc_recv_data_progressive (Session *session,
115 static gint inc_recv_data_finished (Session *session,
118 static gint inc_recv_message (Session *session,
121 static gint inc_drop_message (Pop3Session *session,
124 static void inc_put_error (IncState istate,
125 Pop3Session *session);
127 static void inc_showlog_cb (GtkWidget *widget,
129 static void inc_cancel_cb (GtkWidget *widget,
131 static gint inc_dialog_delete_cb (GtkWidget *widget,
135 static gint get_spool (FolderItem *dest,
137 PrefsAccount *account);
139 static gint inc_spool_account(PrefsAccount *account);
140 static void inc_autocheck_timer_set_interval (guint interval);
141 static gint inc_autocheck_func (gpointer data);
143 static void inc_notify_cmd (gint new_msgs,
146 static void inc_update_stats(gint new_msgs)
148 /* update session statistics */
149 session_stats.received += new_msgs;
154 * @mainwin: Main window.
155 * @new_messages: TRUE if some messages have been received.
157 * Update the folder view and the summary view after receiving
158 * messages. If @new_messages is FALSE, this function avoids unneeded
161 static void inc_finished(MainWindow *mainwin, gboolean new_messages, gboolean autocheck)
163 if (prefs_common.scan_all_after_inc)
164 folderview_check_new(NULL);
166 if (!autocheck && new_messages && prefs_common.open_inbox_on_inc) {
167 FolderItem *item = NULL;
169 if (cur_account && cur_account->inbox)
170 item = folder_find_item_from_identifier(cur_account->inbox);
171 if (item == NULL && cur_account && cur_account->folder)
172 item = cur_account->folder->inbox;
174 item = folder_get_default_inbox();
176 folderview_unselect(mainwin->folderview);
177 folderview_select(mainwin->folderview, item);
179 statusbar_progress_all(0,0,0);
182 void inc_mail(MainWindow *mainwin, gboolean notify)
185 gint account_new_msgs = 0;
187 if (inc_lock_count) return;
189 if (prefs_common.work_offline &&
190 !inc_offline_should_override(TRUE,
191 _("Claws Mail needs network access in order "
196 inc_autocheck_timer_remove();
197 main_window_lock(mainwin);
199 if (prefs_common.use_extinc && prefs_common.extinc_cmd) {
200 /* external incorporating program */
201 if (execute_command_line(prefs_common.extinc_cmd, FALSE, NULL) < 0) {
202 main_window_unlock(mainwin);
203 inc_autocheck_timer_set();
208 account_new_msgs = inc_account_mail_real(mainwin, cur_account);
209 if (account_new_msgs > 0)
210 new_msgs += account_new_msgs;
213 inc_update_stats(new_msgs);
214 inc_finished(mainwin, new_msgs > 0, FALSE);
215 main_window_unlock(mainwin);
216 inc_notify_cmd(new_msgs, notify);
217 inc_autocheck_timer_set();
221 void inc_pop_before_smtp(PrefsAccount *acc)
223 IncProgressDialog *inc_dialog;
227 mainwin = mainwindow_get_mainwindow();
229 session = inc_session_new(acc);
230 if (!session) return;
231 POP3_SESSION(session->session)->pop_before_smtp = TRUE;
233 inc_dialog = inc_progress_dialog_create(FALSE);
234 inc_dialog->queue_list = g_list_append(inc_dialog->queue_list,
236 /* FIXME: assumes to attach to first main window */
237 inc_dialog->mainwin = mainwin;
238 inc_progress_dialog_set_list(inc_dialog);
241 toolbar_main_set_sensitive(mainwin);
242 main_window_set_menu_sensitive(mainwin);
245 inc_start(inc_dialog);
248 static gint inc_account_mail_real(MainWindow *mainwin, PrefsAccount *account)
250 IncProgressDialog *inc_dialog;
253 switch (account->protocol) {
257 * FIXME: it should return foldeview_check_new() value.
258 * TODO: do it when bug [19] is fixed (IMAP folder sets
259 * an incorrect new message count)
261 folderview_check_new(FOLDER(account->folder));
264 session = inc_session_new(account);
265 if (!session) return 0;
267 inc_dialog = inc_progress_dialog_create(FALSE);
268 inc_dialog->queue_list = g_list_append(inc_dialog->queue_list,
270 inc_dialog->mainwin = mainwin;
271 inc_progress_dialog_set_list(inc_dialog);
274 toolbar_main_set_sensitive(mainwin);
275 main_window_set_menu_sensitive(mainwin);
278 return inc_start(inc_dialog);
281 return inc_spool_account(account);
289 gint inc_account_mail(MainWindow *mainwin, PrefsAccount *account)
293 if (inc_lock_count) return 0;
295 if (account->receive_in_progress) return 0;
297 if (prefs_common.work_offline &&
298 !inc_offline_should_override(TRUE,
299 _("Claws Mail needs network access in order "
303 inc_autocheck_timer_remove();
304 main_window_lock(mainwin);
306 new_msgs = inc_account_mail_real(mainwin, account);
308 inc_update_stats(new_msgs);
309 inc_finished(mainwin, new_msgs > 0, FALSE);
310 main_window_unlock(mainwin);
311 inc_autocheck_timer_set();
316 void inc_account_list_mail(MainWindow *mainwin, GList *account_list, gboolean autocheck,
319 GList *list, *queue_list = NULL;
320 IncProgressDialog *inc_dialog;
321 gint new_msgs = 0, num;
323 if (prefs_common.work_offline &&
324 !inc_offline_should_override( (autocheck == FALSE),
325 _("Claws Mail needs network access in order "
329 if (inc_lock_count) return;
331 main_window_lock(mainwin);
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);
341 if (prefs_common.use_extinc && prefs_common.extinc_cmd) {
342 /* external incorporating program */
343 if (execute_command_line(prefs_common.extinc_cmd, FALSE, NULL) < 0) {
344 log_error(LOG_PROTOCOL, _("%s failed\n"), prefs_common.extinc_cmd);
346 main_window_unlock(mainwin);
351 /* Check all accounts in the list, one by one. */
352 for (list = account_list; list != NULL; list = list->next) {
353 PrefsAccount *account = list->data;
355 if (account == NULL) {
356 debug_print("INC: Huh? inc_account_list_mail() got a NULL account, this should not happen!\n");
360 debug_print("INC: checking account %d\n", account->account_id);
361 switch (account->protocol) {
363 if (!(account->receive_in_progress)) {
364 IncSession *session = inc_session_new(account);
366 if (session != NULL) {
367 debug_print("INC: adding POP3 account %d to inc queue\n",
368 account->account_id);
369 queue_list = g_list_append(queue_list, session);
376 new_msgs += folderview_check_new(FOLDER(account->folder));
380 num = inc_spool_account(account);
386 /* Nothing to do here, it's a SMTP-only account. */
390 debug_print("INC: encountered account %d with unknown protocol %d, ignoring\n",
391 account->account_id, account->protocol);
399 inc_dialog = inc_progress_dialog_create(autocheck);
400 inc_dialog->queue_list = queue_list;
401 inc_dialog->mainwin = mainwin;
402 inc_progress_dialog_set_list(inc_dialog);
404 toolbar_main_set_sensitive(mainwin);
405 main_window_set_menu_sensitive(mainwin);
406 new_msgs += inc_start(inc_dialog);
409 inc_update_stats(new_msgs);
410 inc_finished(mainwin, new_msgs > 0, autocheck);
411 main_window_unlock(mainwin);
412 inc_notify_cmd(new_msgs, notify);
415 void inc_all_account_mail(MainWindow *mainwin, gboolean autocheck,
416 gboolean check_at_startup, gboolean notify)
418 GList *list, *list2 = NULL;
420 gboolean hide_dialog = FALSE;
422 debug_print("INC: inc_all_account_mail(), autocheck: %s\n",
423 autocheck ? "YES" : "NO");
425 /* Collect list of accounts which use the global autocheck interval. */
426 for (list = account_get_list(); list != NULL; list = list->next) {
427 PrefsAccount *account = list->data;
429 /* Nothing to do for SMTP-only accounts. */
430 if (account->protocol == A_NONE)
433 /* Set up condition which decides whether or not to check
434 * this account, based on whether we're doing global autocheck
435 * or a check at startup or a manual 'Get all' check. */
437 condition = prefs_common_get_prefs()->autochk_newmail
438 && account->autochk_use_default;
439 else if (check_at_startup || (!check_at_startup && !autocheck))
440 condition = account->recv_at_getall;
443 debug_print("INC: will check account %d\n", account->account_id);
444 list2 = g_list_append(list2, account);
448 /* Do the check on the collected accounts. */
450 if (autocheck || check_at_startup)
452 inc_account_list_mail(mainwin, list2, hide_dialog, notify);
457 static void inc_progress_dialog_size_allocate_cb(GtkWidget *widget,
458 GtkAllocation *allocation)
460 cm_return_if_fail(allocation != NULL);
462 prefs_common.receivewin_width = allocation->width;
463 prefs_common.receivewin_height = allocation->height;
466 static IncProgressDialog *inc_progress_dialog_create(gboolean autocheck)
468 IncProgressDialog *dialog;
469 ProgressDialog *progress;
470 static GdkGeometry geometry;
472 dialog = g_new0(IncProgressDialog, 1);
474 progress = progress_dialog_create();
475 gtk_window_set_title(GTK_WINDOW(progress->window),
476 _("Retrieving new messages"));
477 g_signal_connect(G_OBJECT(progress->showlog_btn), "clicked",
478 G_CALLBACK(inc_showlog_cb), dialog);
479 g_signal_connect(G_OBJECT(progress->cancel_btn), "clicked",
480 G_CALLBACK(inc_cancel_cb), dialog);
481 g_signal_connect(G_OBJECT(progress->window), "delete_event",
482 G_CALLBACK(inc_dialog_delete_cb), dialog);
483 g_signal_connect(G_OBJECT(progress->window), "size_allocate",
484 G_CALLBACK(inc_progress_dialog_size_allocate_cb), NULL);
485 /* manage_window_set_transient(GTK_WINDOW(progress->window)); */
487 progress_dialog_get_fraction(progress);
489 stock_pixbuf_gdk(STOCK_PIXMAP_COMPLETE, &okpix);
490 stock_pixbuf_gdk(STOCK_PIXMAP_CONTINUE, ¤tpix);
491 stock_pixbuf_gdk(STOCK_PIXMAP_ERROR, &errorpix);
493 if (!geometry.min_height) {
494 geometry.min_width = 460;
495 geometry.min_height = 250;
498 gtk_window_set_geometry_hints(GTK_WINDOW(progress->window), NULL, &geometry,
500 gtk_widget_set_size_request(progress->window, prefs_common.receivewin_width,
501 prefs_common.receivewin_height);
503 if (prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS ||
504 (prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL &&
506 dialog->show_dialog = TRUE;
507 gtk_widget_show_now(progress->window);
510 dialog->dialog = progress;
511 g_get_current_time(&dialog->progress_tv);
512 g_get_current_time(&dialog->folder_tv);
513 dialog->queue_list = NULL;
516 inc_dialog_list = g_list_append(inc_dialog_list, dialog);
521 static void inc_progress_dialog_set_list(IncProgressDialog *inc_dialog)
525 for (list = inc_dialog->queue_list; list != NULL; list = list->next) {
526 IncSession *session = list->data;
527 Pop3Session *pop3_session = POP3_SESSION(session->session);
529 session->data = inc_dialog;
531 progress_dialog_list_set(inc_dialog->dialog,
533 pop3_session->ac_prefs->account_name,
538 static void inc_progress_dialog_clear(IncProgressDialog *inc_dialog)
540 progress_dialog_get_fraction(inc_dialog->dialog);
541 progress_dialog_set_label(inc_dialog->dialog, "");
542 if (inc_dialog->mainwin)
543 main_window_progress_off(inc_dialog->mainwin);
546 static void inc_progress_dialog_destroy(IncProgressDialog *inc_dialog)
548 cm_return_if_fail(inc_dialog != NULL);
550 inc_dialog_list = g_list_remove(inc_dialog_list, inc_dialog);
552 if (inc_dialog->mainwin)
553 main_window_progress_off(inc_dialog->mainwin);
554 progress_dialog_destroy(inc_dialog->dialog);
559 static IncSession *inc_session_new(PrefsAccount *account)
563 cm_return_val_if_fail(account != NULL, NULL);
565 if (account->protocol != A_POP3)
567 if (!account->recv_server || !account->userid)
570 session = g_new0(IncSession, 1);
572 session->session = pop3_session_new(account);
573 session->session->data = session;
574 POP3_SESSION(session->session)->drop_message = inc_drop_message;
575 session_set_recv_message_notify(session->session,
576 inc_recv_message, session);
577 session_set_recv_data_progressive_notify(session->session,
578 inc_recv_data_progressive,
580 session_set_recv_data_notify(session->session,
581 inc_recv_data_finished, session);
586 static void inc_session_destroy(IncSession *session)
588 cm_return_if_fail(session != NULL);
590 session_destroy(session->session);
594 static gint pop3_get_port(Pop3Session *pop3_session)
597 return pop3_session->ac_prefs->set_popport ?
598 pop3_session->ac_prefs->popport :
599 pop3_session->ac_prefs->ssl_pop == SSL_TUNNEL ? 995 : 110;
601 return pop3_session->ac_prefs->set_popport ?
602 pop3_session->ac_prefs->popport : 110;
606 static gint inc_start(IncProgressDialog *inc_dialog)
610 Pop3Session *pop3_session;
616 FolderItem *processing, *inbox;
617 GSList *msglist, *msglist_element;
618 gboolean cancelled = FALSE;
620 qlist = inc_dialog->queue_list;
621 while (qlist != NULL) {
622 GList *next = qlist->next;
624 session = qlist->data;
625 pop3_session = POP3_SESSION(session->session);
626 pop3_session->user = g_strdup(pop3_session->ac_prefs->userid);
628 if (inc_dialog->show_dialog)
629 manage_window_focus_in
630 (inc_dialog->dialog->window,
633 if (password_get(pop3_session->user,
634 pop3_session->ac_prefs->recv_server,
635 "pop3", pop3_get_port(pop3_session),
636 &(pop3_session->pass))) {
638 } else if ((pop3_session->pass = passwd_store_get_account(
639 pop3_session->ac_prefs->account_id, PWS_ACCOUNT_RECV)) == NULL) {
642 pass = input_dialog_query_password_keep
643 (pop3_session->ac_prefs->recv_server,
645 &(pop3_session->ac_prefs->session_passwd));
648 pop3_session->pass = pass;
652 if (inc_dialog->show_dialog)
653 manage_window_focus_out
654 (inc_dialog->dialog->window,
660 #define SET_PIXMAP_AND_TEXT(pix, str) \
662 progress_dialog_list_set(inc_dialog->dialog, \
663 inc_dialog->cur_row, \
669 for (; inc_dialog->queue_list != NULL && !cancelled; inc_dialog->cur_row++) {
670 session = inc_dialog->queue_list->data;
671 pop3_session = POP3_SESSION(session->session);
672 GSList *filtered, *unfiltered;
674 if (pop3_session->pass == NULL) {
675 SET_PIXMAP_AND_TEXT(okpix, _("Cancelled"));
676 inc_session_destroy(session);
677 inc_dialog->queue_list =
678 g_list_remove(inc_dialog->queue_list, session);
682 inc_progress_dialog_clear(inc_dialog);
683 progress_dialog_scroll_to_row(inc_dialog->dialog,
684 inc_dialog->cur_row);
686 SET_PIXMAP_AND_TEXT(currentpix, _("Retrieving"));
688 /* begin POP3 session */
689 inc_state = inc_pop3_session_do(session);
693 if (pop3_session->cur_total_num > 0)
694 msg = g_strdup_printf(
695 ngettext("Done (%d message (%s) received)",
696 "Done (%d messages (%s) received)",
697 pop3_session->cur_total_num),
698 pop3_session->cur_total_num,
699 to_human_readable((goffset)pop3_session->cur_total_recv_bytes));
701 msg = g_strdup_printf(_("Done (no new messages)"));
702 SET_PIXMAP_AND_TEXT(okpix, msg);
705 case INC_CONNECT_ERROR:
706 SET_PIXMAP_AND_TEXT(errorpix, _("Connection failed"));
708 case INC_AUTH_FAILED:
709 SET_PIXMAP_AND_TEXT(errorpix, _("Auth failed"));
710 if (pop3_session->ac_prefs->session_passwd) {
711 g_free(pop3_session->ac_prefs->session_passwd);
712 pop3_session->ac_prefs->session_passwd = NULL;
716 SET_PIXMAP_AND_TEXT(errorpix, _("Locked"));
721 case INC_SOCKET_ERROR:
723 SET_PIXMAP_AND_TEXT(errorpix, _("Error"));
726 SET_PIXMAP_AND_TEXT(errorpix, _("Timeout"));
729 SET_PIXMAP_AND_TEXT(okpix, _("Cancelled"));
730 if (!inc_dialog->show_dialog)
737 if (pop3_session->error_val == PS_AUTHFAIL) {
738 if(!prefs_common.no_recv_err_panel) {
739 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
740 ((prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL) && focus_window))
741 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
745 /* CLAWS: perform filtering actions on dropped message */
746 /* CLAWS: get default inbox (perhaps per account) */
747 if (pop3_session->ac_prefs->inbox) {
748 /* CLAWS: get destination folder / mailbox */
749 inbox = folder_find_item_from_identifier(pop3_session->ac_prefs->inbox);
751 inbox = folder_get_default_inbox();
753 inbox = folder_get_default_inbox();
755 /* get list of messages in processing */
756 processing = folder_get_default_processing(pop3_session->ac_prefs->account_id);
757 folder_item_scan(processing);
758 msglist = folder_item_get_msg_list(processing);
760 /* process messages */
761 folder_item_update_freeze();
763 procmsg_msglist_filter(msglist, pop3_session->ac_prefs,
764 &filtered, &unfiltered,
765 pop3_session->ac_prefs->filter_on_recv);
767 filtering_move_and_copy_msgs(msglist);
768 if (unfiltered != NULL)
769 folder_item_move_msgs(inbox, unfiltered);
771 for(msglist_element = msglist; msglist_element != NULL;
772 msglist_element = msglist_element->next) {
773 procmsg_msginfo_free((MsgInfo**)&(msglist_element->data));
775 folder_item_update_thaw();
777 g_slist_free(msglist);
778 g_slist_free(filtered);
779 g_slist_free(unfiltered);
783 new_msgs += pop3_session->cur_total_num;
785 pop3_write_uidl_list(pop3_session);
787 if (inc_state != INC_SUCCESS && inc_state != INC_CANCEL) {
789 if (inc_dialog->show_dialog)
790 manage_window_focus_in
791 (inc_dialog->dialog->window,
793 inc_put_error(inc_state, pop3_session);
794 if (inc_dialog->show_dialog)
795 manage_window_focus_out
796 (inc_dialog->dialog->window,
798 if (inc_state == INC_NO_SPACE ||
799 inc_state == INC_IO_ERROR)
802 folder_item_free_cache(processing, TRUE);
804 inc_session_destroy(session);
805 inc_dialog->queue_list =
806 g_list_remove(inc_dialog->queue_list, session);
809 #undef SET_PIXMAP_AND_TEXT
812 fin_msg = g_strdup_printf(ngettext("Finished (%d new message)",
813 "Finished (%d new messages)",
814 new_msgs), new_msgs);
816 fin_msg = g_strdup_printf(_("Finished (no new messages)"));
818 progress_dialog_set_label(inc_dialog->dialog, fin_msg);
820 while (inc_dialog->queue_list != NULL) {
821 session = inc_dialog->queue_list->data;
822 inc_session_destroy(session);
823 inc_dialog->queue_list =
824 g_list_remove(inc_dialog->queue_list, session);
827 if (prefs_common.close_recv_dialog || !inc_dialog->show_dialog)
828 inc_progress_dialog_destroy(inc_dialog);
830 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window),
832 gtk_button_set_label(GTK_BUTTON(inc_dialog->dialog->cancel_btn),
841 static IncState inc_pop3_session_do(IncSession *session)
843 Pop3Session *pop3_session = POP3_SESSION(session->session);
844 IncProgressDialog *inc_dialog = (IncProgressDialog *)session->data;
845 PrefsAccount *ac = pop3_session->ac_prefs;
850 ProxyInfo *proxy_info = NULL;
852 debug_print("getting new messages of account %s...\n",
855 ac->last_pop_login_time = time(NULL);
857 buf = g_strdup_printf(_("%s: Retrieving new messages"),
859 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window), buf);
862 server = ac->recv_server;
863 account_name = ac->account_name;
864 port = pop3_get_port(pop3_session);
867 SESSION(pop3_session)->ssl_type = ac->ssl_pop;
868 if (ac->ssl_pop != SSL_NONE)
869 SESSION(pop3_session)->nonblocking =
870 ac->use_nonblocking_ssl;
872 if (ac->ssl_pop != SSL_NONE) {
873 if (alertpanel_full(_("Insecure connection"),
874 _("This connection is configured to be secured "
875 "using SSL/TLS, but SSL/TLS is not available "
876 "in this build of Claws Mail. \n\n"
877 "Do you want to continue connecting to this "
878 "server? The communication would not be "
880 GTK_STOCK_CANCEL, _("Con_tinue connecting"), NULL,
881 ALERTFOCUS_FIRST, FALSE, NULL, ALERT_WARNING) != G_ALERTALTERNATE)
886 buf = g_strdup_printf(_("Account '%s': Connecting to POP3 server: %s:%d..."),
887 account_name, server, port);
888 statusbar_print_all("%s", buf);
889 log_message(LOG_PROTOCOL, "%s\n", buf);
891 progress_dialog_set_label(inc_dialog->dialog, buf);
894 if (ac->use_default_proxy) {
895 proxy_info = (ProxyInfo *)&(prefs_common.proxy_info);
896 if (proxy_info->use_proxy_auth)
897 proxy_info->proxy_pass = passwd_store_get(PWS_CORE, PWS_CORE_PROXY,
898 PWS_CORE_PROXY_PASS);
900 proxy_info = (ProxyInfo *)&(ac->proxy_info);
901 if (proxy_info->use_proxy_auth)
902 proxy_info->proxy_pass = passwd_store_get_account(ac->account_id,
903 PWS_ACCOUNT_PROXY_PASS);
906 SESSION(pop3_session)->proxy_info = proxy_info;
911 session_set_timeout(SESSION(pop3_session),
912 prefs_common.io_timeout_secs * 1000);
914 if (session_connect(SESSION(pop3_session), server, port) < 0) {
915 if(!prefs_common.no_recv_err_panel) {
916 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
917 ((prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL) && focus_window)) {
918 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
920 alertpanel_error(_("Can't connect to POP3 server: %s:%d"),
922 manage_window_focus_out(inc_dialog->dialog->window, NULL, NULL);
924 log_error(LOG_PROTOCOL, _("Can't connect to POP3 server: %s:%d\n"),
927 session->inc_state = INC_CONNECT_ERROR;
929 return INC_CONNECT_ERROR;
932 while (session_is_running(SESSION(pop3_session)) &&
933 session->inc_state != INC_CANCEL)
934 gtk_main_iteration();
936 if (session->inc_state == INC_SUCCESS) {
937 switch (pop3_session->error_val) {
939 switch (SESSION(pop3_session)->state) {
941 if (pop3_session->state == POP3_READY)
942 session->inc_state = INC_CONNECT_ERROR;
944 session->inc_state = INC_ERROR;
947 session->inc_state = INC_EOF;
949 case SESSION_TIMEOUT:
950 session->inc_state = INC_TIMEOUT;
953 session->inc_state = INC_SUCCESS;
958 session->inc_state = INC_AUTH_FAILED;
961 session->inc_state = INC_IO_ERROR;
964 session->inc_state = INC_SOCKET_ERROR;
967 session->inc_state = INC_LOCKED;
970 session->inc_state = INC_ERROR;
975 session_disconnect(SESSION(pop3_session));
978 return session->inc_state;
981 static void inc_progress_dialog_update(IncProgressDialog *inc_dialog,
982 IncSession *inc_session)
984 inc_progress_dialog_set_label(inc_dialog, inc_session);
985 inc_progress_dialog_set_progress(inc_dialog, inc_session);
988 static void inc_progress_dialog_set_label(IncProgressDialog *inc_dialog,
989 IncSession *inc_session)
991 ProgressDialog *dialog = inc_dialog->dialog;
992 Pop3Session *session;
994 cm_return_if_fail(inc_session != NULL);
996 session = POP3_SESSION(inc_session->session);
998 switch (session->state) {
1001 case POP3_GETAUTH_USER:
1002 case POP3_GETAUTH_PASS:
1003 case POP3_GETAUTH_APOP:
1004 progress_dialog_set_label(dialog, _("Authenticating..."));
1005 statusbar_pop_all();
1006 statusbar_print_all(_("Retrieving messages from %s (%s)..."),
1007 SESSION(session)->server,
1008 session->ac_prefs->account_name);
1010 case POP3_GETRANGE_STAT:
1011 progress_dialog_set_label
1012 (dialog, _("Getting the number of new messages (STAT)..."));
1014 case POP3_GETRANGE_LAST:
1015 progress_dialog_set_label
1016 (dialog, _("Getting the number of new messages (LAST)..."));
1018 case POP3_GETRANGE_UIDL:
1019 progress_dialog_set_label
1020 (dialog, _("Getting the number of new messages (UIDL)..."));
1022 case POP3_GETSIZE_LIST:
1023 progress_dialog_set_label
1024 (dialog, _("Getting the size of messages (LIST)..."));
1027 case POP3_RETR_RECV:
1031 progress_dialog_set_label(dialog, _("Quitting"));
1038 static void inc_progress_dialog_set_progress(IncProgressDialog *inc_dialog,
1039 IncSession *inc_session)
1041 gchar buf[MESSAGEBUFSIZE];
1042 Pop3Session *pop3_session = POP3_SESSION(inc_session->session);
1043 gchar *total_size_str;
1047 if (!pop3_session->new_msg_exist) return;
1049 cur_total = inc_session->cur_total_bytes;
1050 total = pop3_session->total_bytes;
1051 if (pop3_session->state == POP3_RETR ||
1052 pop3_session->state == POP3_RETR_RECV ||
1053 pop3_session->state == POP3_DELETE) {
1054 Xstrdup_a(total_size_str, to_human_readable((goffset)total), return);
1055 g_snprintf(buf, sizeof(buf),
1056 _("Retrieving message (%d / %d) (%s / %s)"),
1057 pop3_session->cur_msg, pop3_session->count,
1058 to_human_readable((goffset)cur_total), total_size_str);
1059 progress_dialog_set_label(inc_dialog->dialog, buf);
1062 progress_dialog_set_fraction
1063 (inc_dialog->dialog, (total == 0) ? 0: (gfloat)cur_total / (gfloat)total);
1065 statusbar_progress_all(pop3_session->cur_msg, pop3_session->count, 1);
1067 if (pop3_session->cur_total_num > 0) {
1068 g_snprintf(buf, sizeof(buf),
1069 ngettext("Retrieving (%d message (%s) received)",
1070 "Retrieving (%d messages (%s) received)",
1071 pop3_session->cur_total_num),
1072 pop3_session->cur_total_num,
1074 ((goffset)pop3_session->cur_total_recv_bytes));
1075 progress_dialog_list_set_status(inc_dialog->dialog,
1076 inc_dialog->cur_row,
1081 static void inc_progress_dialog_update_periodic(IncProgressDialog *inc_dialog,
1082 IncSession *inc_session)
1088 g_get_current_time(&tv_cur);
1090 tv_result.tv_sec = tv_cur.tv_sec - inc_dialog->progress_tv.tv_sec;
1091 tv_result.tv_usec = tv_cur.tv_usec - inc_dialog->progress_tv.tv_usec;
1092 if (tv_result.tv_usec < 0) {
1094 tv_result.tv_usec += G_USEC_PER_SEC;
1097 msec = tv_result.tv_sec * 1000 + tv_result.tv_usec / 1000;
1098 if (msec > PROGRESS_UPDATE_INTERVAL) {
1099 inc_progress_dialog_update(inc_dialog, inc_session);
1100 inc_dialog->progress_tv.tv_sec = tv_cur.tv_sec;
1101 inc_dialog->progress_tv.tv_usec = tv_cur.tv_usec;
1105 static gint inc_recv_data_progressive(Session *session, guint cur_len,
1106 guint total_len, gpointer data)
1108 IncSession *inc_session = (IncSession *)data;
1109 Pop3Session *pop3_session = POP3_SESSION(session);
1110 IncProgressDialog *inc_dialog;
1113 cm_return_val_if_fail(inc_session != NULL, -1);
1115 if (pop3_session->state != POP3_RETR &&
1116 pop3_session->state != POP3_RETR_RECV &&
1117 pop3_session->state != POP3_DELETE &&
1118 pop3_session->state != POP3_LOGOUT) return 0;
1120 if (!pop3_session->new_msg_exist) return 0;
1122 cur_total = pop3_session->cur_total_bytes + cur_len;
1123 if (cur_total > pop3_session->total_bytes)
1124 cur_total = pop3_session->total_bytes;
1125 inc_session->cur_total_bytes = cur_total;
1127 inc_dialog = (IncProgressDialog *)inc_session->data;
1128 inc_progress_dialog_update_periodic(inc_dialog, inc_session);
1133 static gint inc_recv_data_finished(Session *session, guint len, gpointer data)
1135 IncSession *inc_session = (IncSession *)data;
1136 IncProgressDialog *inc_dialog;
1138 cm_return_val_if_fail(inc_session != NULL, -1);
1140 inc_dialog = (IncProgressDialog *)inc_session->data;
1142 inc_recv_data_progressive(session, 0, 0, inc_session);
1144 if (POP3_SESSION(session)->state == POP3_LOGOUT) {
1145 inc_progress_dialog_update(inc_dialog, inc_session);
1151 static gint inc_recv_message(Session *session, const gchar *msg, gpointer data)
1153 IncSession *inc_session = (IncSession *)data;
1154 IncProgressDialog *inc_dialog;
1156 cm_return_val_if_fail(inc_session != NULL, -1);
1158 inc_dialog = (IncProgressDialog *)inc_session->data;
1160 switch (POP3_SESSION(session)->state) {
1161 case POP3_GETAUTH_USER:
1162 case POP3_GETAUTH_PASS:
1163 case POP3_GETAUTH_APOP:
1164 case POP3_GETRANGE_STAT:
1165 case POP3_GETRANGE_LAST:
1166 case POP3_GETRANGE_UIDL:
1167 case POP3_GETSIZE_LIST:
1168 inc_progress_dialog_update(inc_dialog, inc_session);
1171 inc_recv_data_progressive(session, 0, 0, inc_session);
1174 inc_progress_dialog_update(inc_dialog, inc_session);
1183 static gint inc_drop_message(Pop3Session *session, const gchar *file)
1186 FolderItem *dropfolder;
1187 IncSession *inc_session = (IncSession *)(SESSION(session)->data);
1190 cm_return_val_if_fail(inc_session != NULL, -1);
1192 if (session->ac_prefs->inbox) {
1193 inbox = folder_find_item_from_identifier
1194 (session->ac_prefs->inbox);
1196 inbox = folder_get_default_inbox();
1198 inbox = folder_get_default_inbox();
1204 /* CLAWS: claws uses a global .processing folder for the filtering. */
1205 dropfolder = folder_get_default_processing(session->ac_prefs->account_id);
1207 /* add msg file to drop folder */
1208 if ((msgnum = folder_item_add_msg(
1209 dropfolder, file, NULL, TRUE)) < 0) {
1217 static void inc_put_error(IncState istate, Pop3Session *session)
1219 gchar *log_msg = NULL;
1220 gchar *err_msg = NULL;
1221 gboolean fatal_error = FALSE;
1224 case INC_CONNECT_ERROR:
1226 if (prefs_common.no_recv_err_panel)
1228 err_msg = g_strdup_printf(_("Connection to %s:%d failed."),
1229 SESSION(session)->server,
1230 SESSION(session)->port);
1233 log_msg = _("Error occurred while processing mail.");
1235 if (prefs_common.no_recv_err_panel)
1237 if (session->error_msg)
1238 err_msg = g_strdup_printf
1239 (_("Error occurred while processing mail:\n%s"),
1240 session->error_msg);
1242 err_msg = g_strdup(log_msg);
1245 log_msg = _("No disk space left.");
1246 err_msg = g_strdup(log_msg);
1250 log_msg = _("Can't write file.");
1251 err_msg = g_strdup(log_msg);
1254 case INC_SOCKET_ERROR:
1255 log_msg = _("Socket error.");
1256 if (prefs_common.no_recv_err_panel)
1258 err_msg = g_strdup_printf(_("Socket error on connection to %s:%d."),
1259 SESSION(session)->server,
1260 SESSION(session)->port);
1263 log_msg = _("Connection closed by the remote host.");
1264 if (prefs_common.no_recv_err_panel)
1266 err_msg = g_strdup_printf(_("Connection to %s:%d closed by the remote host."),
1267 SESSION(session)->server,
1268 SESSION(session)->port);
1271 log_msg = _("Mailbox is locked.");
1272 if (prefs_common.no_recv_err_panel)
1274 if (session->error_msg)
1275 err_msg = g_strdup_printf(_("Mailbox is locked:\n%s"),
1276 session->error_msg);
1278 err_msg = g_strdup(log_msg);
1280 case INC_AUTH_FAILED:
1281 log_msg = _("Authentication failed.");
1283 if (prefs_common.no_recv_err_panel)
1285 if (session->error_msg)
1286 err_msg = g_strdup_printf
1287 (_("Authentication failed:\n%s"), session->error_msg);
1289 err_msg = g_strdup(log_msg);
1292 log_msg = _("Session timed out. You may be able to "
1293 "recover by increasing the timeout value in "
1294 "Preferences/Other/Miscellaneous.");
1295 if (prefs_common.no_recv_err_panel)
1297 err_msg = g_strdup_printf(_("Connection to %s:%d timed out."),
1298 SESSION(session)->server,
1299 SESSION(session)->port);
1307 log_error(LOG_PROTOCOL, "%s\n", log_msg);
1309 log_warning(LOG_PROTOCOL, "%s\n", log_msg);
1311 if (prefs_common.no_recv_err_panel && fatal_error)
1312 mainwindow_show_error();
1315 alertpanel_error_log("%s", err_msg);
1320 static void inc_cancel(IncProgressDialog *dialog)
1322 IncSession *session;
1324 cm_return_if_fail(dialog != NULL);
1326 if (dialog->queue_list == NULL) {
1327 inc_progress_dialog_destroy(dialog);
1331 session = dialog->queue_list->data;
1333 session->inc_state = INC_CANCEL;
1335 log_message(LOG_PROTOCOL, _("Incorporation cancelled\n"));
1338 gboolean inc_is_active(void)
1340 return (inc_dialog_list != NULL);
1343 void inc_cancel_all(void)
1347 for (cur = inc_dialog_list; cur != NULL; cur = cur->next)
1348 inc_cancel((IncProgressDialog *)cur->data);
1351 static void inc_showlog_cb(GtkWidget *widget, gpointer data)
1353 MainWindow *mainwin = mainwindow_get_mainwindow();
1355 log_window_show(mainwin->logwin);
1358 static void inc_cancel_cb(GtkWidget *widget, gpointer data)
1360 inc_cancel((IncProgressDialog *)data);
1363 static gint inc_dialog_delete_cb(GtkWidget *widget, GdkEventAny *event,
1366 IncProgressDialog *dialog = (IncProgressDialog *)data;
1368 if (dialog->queue_list == NULL)
1369 inc_progress_dialog_destroy(dialog);
1374 static gint inc_spool_account(PrefsAccount *account)
1380 if (account->local_inbox) {
1381 inbox = folder_find_item_from_identifier(account->local_inbox);
1383 inbox = folder_get_default_inbox();
1385 inbox = folder_get_default_inbox();
1387 if (account->local_mbox) {
1388 if (is_file_exist(account->local_mbox))
1389 mbox = g_strdup(account->local_mbox);
1390 else if (is_dir_exist(account->local_mbox))
1391 mbox = g_strconcat(account->local_mbox, G_DIR_SEPARATOR_S,
1392 g_get_user_name(), NULL);
1394 debug_print("%s: local mailbox not found.\n",
1395 account->local_mbox);
1399 debug_print("local mailbox not set in account info.\n");
1403 result = get_spool(inbox, mbox, account);
1406 statusbar_pop_all();
1411 static gint get_spool(FolderItem *dest, const gchar *mbox, PrefsAccount *account)
1415 gchar tmp_mbox[MAXPATHLEN + 1];
1417 cm_return_val_if_fail(dest != NULL, -1);
1418 cm_return_val_if_fail(mbox != NULL, -1);
1419 cm_return_val_if_fail(account != NULL, -1);
1421 if (!is_file_exist(mbox) || (size = get_file_size(mbox)) == 0) {
1422 debug_print("%s: no messages in local mailbox.\n", mbox);
1424 } else if (size < 0)
1427 if ((lockfd = lock_mbox(mbox, LOCK_FLOCK)) < 0)
1430 g_snprintf(tmp_mbox, sizeof(tmp_mbox), "%s%ctmpmbox.%p",
1431 get_tmp_dir(), G_DIR_SEPARATOR, mbox);
1433 if (copy_mbox(lockfd, tmp_mbox) < 0) {
1434 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1438 debug_print("Getting new messages from %s into %s...\n",
1441 msgs = proc_mbox(dest, tmp_mbox, account->filter_on_recv, account);
1443 claws_unlink(tmp_mbox);
1444 if (msgs >= 0) empty_mbox(mbox);
1445 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1450 void inc_lock_real(void)
1455 void inc_unlock_real(void)
1457 if (inc_lock_count > 0)
1461 static guint autocheck_timer = 0;
1462 static gpointer autocheck_data = NULL;
1464 static void inc_notify_cmd(gint new_msgs, gboolean notify)
1466 gchar *buf, *numpos, *ret_str;
1467 gssize by_read = 0, by_written = 0;
1469 if (!(new_msgs && notify && prefs_common.newmail_notify_cmd &&
1470 *prefs_common.newmail_notify_cmd))
1473 buf = g_strdup(prefs_common.newmail_notify_cmd);
1474 if ((numpos = strstr(buf, "%d")) != NULL) {
1478 buf2 = g_strdup_printf("%s%d%s", buf, new_msgs, numpos + 2);
1483 ret_str = g_locale_from_utf8(buf, strlen(buf), &by_read, &by_written,
1485 if (ret_str && by_written) {
1489 debug_print("executing new mail notification command: %s\n", buf);
1490 execute_command_line(buf, TRUE, NULL);
1495 void inc_autocheck_timer_init(MainWindow *mainwin)
1497 autocheck_data = mainwin;
1498 inc_autocheck_timer_set();
1501 static void inc_autocheck_timer_set_interval(guint _interval)
1503 guint interval = _interval;
1505 /* Convert the interval to seconds if needed. */
1506 if (_interval % 1000 == 0)
1509 inc_autocheck_timer_remove();
1510 /* last test is to avoid re-enabling auto_check after modifying
1511 the common preferences */
1512 if (prefs_common.autochk_newmail && autocheck_data
1513 && prefs_common.work_offline == FALSE) {
1515 g_timeout_add_seconds(interval, inc_autocheck_func, autocheck_data);
1516 debug_print("added global inc timer %d at %u seconds\n",
1517 autocheck_timer, interval);
1521 void inc_autocheck_timer_set(void)
1523 inc_autocheck_timer_set_interval(prefs_common.autochk_itv * 1000);
1526 void inc_autocheck_timer_remove(void)
1528 if (autocheck_timer) {
1529 debug_print("removed global inc timer %d\n", autocheck_timer);
1530 g_source_remove(autocheck_timer);
1531 autocheck_timer = 0;
1535 static gint inc_autocheck_func(gpointer data)
1537 MainWindow *mainwin = (MainWindow *)data;
1539 if (inc_lock_count) {
1540 debug_print("global inc: autocheck is locked.\n");
1541 inc_autocheck_timer_set_interval(1000);
1545 inc_all_account_mail(mainwin, TRUE, FALSE, prefs_common.newmail_notify_auto);
1546 inc_autocheck_timer_set();
1551 static gboolean inc_account_autocheck_func(gpointer data)
1553 PrefsAccount *account = (PrefsAccount *)data;
1556 cm_return_val_if_fail(account != NULL, FALSE);
1558 debug_print("account %d: inc_account_autocheck_func\n",
1559 account->account_id);
1561 list = g_list_append(list, account);
1562 inc_account_list_mail(mainwindow_get_mainwindow(),
1563 list, TRUE, prefs_common.newmail_notify_auto);
1566 inc_account_autocheck_timer_set_interval(account);
1571 void inc_account_autocheck_timer_remove(PrefsAccount *account)
1573 cm_return_if_fail(account != NULL);
1575 if (account->autocheck_timer != 0) {
1576 g_source_remove(account->autocheck_timer);
1577 debug_print("INC: account %d: removed inc timer %d\n", account->account_id,
1578 account->autocheck_timer);
1579 account->autocheck_timer = 0;
1583 void inc_account_autocheck_timer_set_interval(PrefsAccount *account)
1585 cm_return_if_fail(account != NULL);
1587 inc_account_autocheck_timer_remove(account);
1589 if (account->autochk_use_default
1590 || !account->autochk_use_custom
1591 || account->autochk_itv == 0)
1594 account->autocheck_timer = g_timeout_add_seconds(
1595 account->autochk_itv, inc_account_autocheck_func, account);
1596 debug_print("INC: account %d: added inc timer %d at %u seconds\n",
1597 account->account_id, account->autocheck_timer, account->autochk_itv);
1600 gboolean inc_offline_should_override(gboolean force_ask, const gchar *msg)
1602 gint length = 10; /* seconds */
1603 gint answer = G_ALERTDEFAULT;
1605 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1606 /* If no network connection is available, override is not possible */
1607 if(!networkmanager_is_online(NULL))
1611 if (prefs_common.autochk_newmail)
1612 length = prefs_common.autochk_itv; /* seconds */
1615 inc_offline_overridden_no = (time_t)0;
1618 if (prefs_common.work_offline) {
1621 if (time(NULL) - inc_offline_overridden_yes < length * 60) /* seconds */
1623 else if (time(NULL) - inc_offline_overridden_no < length * 60) /* seconds */
1627 gchar *unit = _("seconds");
1629 /* show the offline override time (length) using the must appropriate unit:
1630 the biggest unit possible (hours, minutes, seconds), provided that there
1631 is not inferior unit involved: 1 hour, 150 minutes, 25 minutes, 90 minutes,
1632 30 seconds, 90 seconds. */
1633 if ((length / 3600) > 0) { /* hours? */
1634 if (((length % 3600) % 60) == 0) { /* no seconds left? */
1635 if ((length % 3600) > 0) { /* minutes left? */
1636 length = length / 60;
1637 unit = ngettext("minute", "minutes", length);
1639 length = length / 3600;
1640 unit = ngettext("hour", "hours", length);
1642 } /* else: seconds */
1644 if ((length / 60) > 0) { /* minutes left? */
1645 if ((length % 60) == 0) {
1646 length = length / 60;
1647 unit = ngettext("minute", "minutes", length);
1649 } /* else: seconds */
1651 tmp = g_strdup_printf(
1652 _("%s%sYou're working offline. Override for %d %s?"),
1657 tmp = g_strdup_printf(
1658 _("%s%sYou're working offline. Override?"),
1662 answer = alertpanel(_("Offline warning"),
1664 GTK_STOCK_NO, GTK_STOCK_YES,
1665 !force_ask? _("On_ly once"):NULL, ALERTFOCUS_SECOND);
1667 if (answer == G_ALERTALTERNATE) {
1668 inc_offline_overridden_yes = time(NULL);
1670 } else if (answer == G_ALERTDEFAULT) {
1672 inc_offline_overridden_no = time(NULL);
1675 inc_reset_offline_override_timers();
1682 void inc_reset_offline_override_timers()
1684 debug_print("resetting offline override timers\n");
1685 inc_offline_overridden_yes = (time_t)0;
1686 inc_offline_overridden_no = (time_t)0;