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"
50 #include "statusbar.h"
52 #include "manage_window.h"
53 #include "stock_pixmap.h"
54 #include "progressdialog.h"
55 #include "inputdialog.h"
56 #include "alertpanel.h"
58 #include "filtering.h"
61 #include "logwindow.h"
62 #include "passwordstore.h"
64 extern SessionStats session_stats;
66 static GList *inc_dialog_list = NULL;
68 guint inc_lock_count = 0;
70 static GdkPixbuf *currentpix;
71 static GdkPixbuf *errorpix;
72 static GdkPixbuf *okpix;
74 #define MESSAGEBUFSIZE 8192
76 static void inc_update_stats(gint new_msgs);
77 static void inc_finished (MainWindow *mainwin,
78 gboolean new_messages,
80 static gint inc_account_mail_real (MainWindow *mainwin,
81 PrefsAccount *account);
83 static IncProgressDialog *inc_progress_dialog_create
85 static void inc_progress_dialog_set_list(IncProgressDialog *inc_dialog);
86 static void inc_progress_dialog_destroy (IncProgressDialog *inc_dialog);
88 static IncSession *inc_session_new (PrefsAccount *account);
89 static void inc_session_destroy (IncSession *session);
90 static gint inc_start (IncProgressDialog *inc_dialog);
91 static IncState inc_pop3_session_do (IncSession *session);
93 static void inc_progress_dialog_update (IncProgressDialog *inc_dialog,
94 IncSession *inc_session);
96 static void inc_progress_dialog_set_label
97 (IncProgressDialog *inc_dialog,
98 IncSession *inc_session);
99 static void inc_progress_dialog_set_progress
100 (IncProgressDialog *inc_dialog,
101 IncSession *inc_session);
103 static void inc_progress_dialog_update_periodic
104 (IncProgressDialog *inc_dialog,
105 IncSession *inc_session);
107 static gint inc_recv_data_progressive (Session *session,
111 static gint inc_recv_data_finished (Session *session,
114 static gint inc_recv_message (Session *session,
117 static gint inc_drop_message (Pop3Session *session,
120 static void inc_put_error (IncState istate,
121 Pop3Session *session);
123 static void inc_showlog_cb (GtkWidget *widget,
125 static void inc_cancel_cb (GtkWidget *widget,
127 static gint inc_dialog_delete_cb (GtkWidget *widget,
131 static gint get_spool (FolderItem *dest,
133 PrefsAccount *account);
135 static gint inc_spool_account(PrefsAccount *account);
136 static void inc_autocheck_timer_set_interval (guint interval);
137 static gint inc_autocheck_func (gpointer data);
139 static void inc_notify_cmd (gint new_msgs,
142 static void inc_update_stats(gint new_msgs)
144 /* update session statistics */
145 session_stats.received += new_msgs;
150 * @mainwin: Main window.
151 * @new_messages: TRUE if some messages have been received.
153 * Update the folder view and the summary view after receiving
154 * messages. If @new_messages is FALSE, this function avoids unneeded
157 static void inc_finished(MainWindow *mainwin, gboolean new_messages, gboolean autocheck)
159 if (prefs_common.scan_all_after_inc)
160 folderview_check_new(NULL);
162 if (!autocheck && new_messages && prefs_common.open_inbox_on_inc) {
163 FolderItem *item = NULL;
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;
170 item = folder_get_default_inbox();
172 folderview_unselect(mainwin->folderview);
173 folderview_select(mainwin->folderview, item);
175 statusbar_progress_all(0,0,0);
178 void inc_mail(MainWindow *mainwin, gboolean notify)
181 gint account_new_msgs = 0;
183 if (inc_lock_count) return;
185 if (prefs_common.work_offline &&
186 !inc_offline_should_override(TRUE,
187 _("Claws Mail needs network access in order "
192 inc_autocheck_timer_remove();
193 main_window_lock(mainwin);
195 if (prefs_common.use_extinc && prefs_common.extinc_cmd) {
196 /* external incorporating program */
197 if (execute_command_line(prefs_common.extinc_cmd, FALSE, NULL) < 0) {
198 main_window_unlock(mainwin);
199 inc_autocheck_timer_set();
204 account_new_msgs = inc_account_mail_real(mainwin, cur_account);
205 if (account_new_msgs > 0)
206 new_msgs += account_new_msgs;
209 inc_update_stats(new_msgs);
210 inc_finished(mainwin, new_msgs > 0, FALSE);
211 main_window_unlock(mainwin);
212 inc_notify_cmd(new_msgs, notify);
213 inc_autocheck_timer_set();
217 void inc_pop_before_smtp(PrefsAccount *acc)
219 IncProgressDialog *inc_dialog;
223 mainwin = mainwindow_get_mainwindow();
225 session = inc_session_new(acc);
226 if (!session) return;
227 POP3_SESSION(session->session)->pop_before_smtp = TRUE;
229 inc_dialog = inc_progress_dialog_create(FALSE);
230 inc_dialog->queue_list = g_list_append(inc_dialog->queue_list,
232 /* FIXME: assumes to attach to first main window */
233 inc_dialog->mainwin = mainwin;
234 inc_progress_dialog_set_list(inc_dialog);
237 toolbar_main_set_sensitive(mainwin);
238 main_window_set_menu_sensitive(mainwin);
241 inc_start(inc_dialog);
244 static gint inc_account_mail_real(MainWindow *mainwin, PrefsAccount *account)
246 IncProgressDialog *inc_dialog;
249 switch (account->protocol) {
253 * FIXME: it should return foldeview_check_new() value.
254 * TODO: do it when bug [19] is fixed (IMAP folder sets
255 * an incorrect new message count)
257 folderview_check_new(FOLDER(account->folder));
260 session = inc_session_new(account);
261 if (!session) return 0;
263 inc_dialog = inc_progress_dialog_create(FALSE);
264 inc_dialog->queue_list = g_list_append(inc_dialog->queue_list,
266 inc_dialog->mainwin = mainwin;
267 inc_progress_dialog_set_list(inc_dialog);
270 toolbar_main_set_sensitive(mainwin);
271 main_window_set_menu_sensitive(mainwin);
274 return inc_start(inc_dialog);
277 return inc_spool_account(account);
285 gint inc_account_mail(MainWindow *mainwin, PrefsAccount *account)
289 if (inc_lock_count) return 0;
291 if (account->receive_in_progress) return 0;
293 if (prefs_common.work_offline &&
294 !inc_offline_should_override(TRUE,
295 _("Claws Mail needs network access in order "
299 inc_autocheck_timer_remove();
300 main_window_lock(mainwin);
302 new_msgs = inc_account_mail_real(mainwin, account);
304 inc_update_stats(new_msgs);
305 inc_finished(mainwin, new_msgs > 0, FALSE);
306 main_window_unlock(mainwin);
307 inc_autocheck_timer_set();
312 void inc_account_list_mail(MainWindow *mainwin, GList *account_list, gboolean autocheck,
315 GList *list, *queue_list = NULL;
316 IncProgressDialog *inc_dialog;
317 gint new_msgs = 0, num;
319 if (prefs_common.work_offline &&
320 !inc_offline_should_override( (autocheck == FALSE),
321 _("Claws Mail needs network access in order "
325 if (inc_lock_count) return;
327 main_window_lock(mainwin);
330 inc_update_stats(new_msgs);
331 inc_finished(mainwin, new_msgs > 0, autocheck);
332 main_window_unlock(mainwin);
333 inc_notify_cmd(new_msgs, notify);
337 if (prefs_common.use_extinc && prefs_common.extinc_cmd) {
338 /* external incorporating program */
339 if (execute_command_line(prefs_common.extinc_cmd, FALSE, NULL) < 0) {
340 log_error(LOG_PROTOCOL, _("%s failed\n"), prefs_common.extinc_cmd);
342 main_window_unlock(mainwin);
347 /* Check all accounts in the list, one by one. */
348 for (list = account_list; list != NULL; list = list->next) {
349 PrefsAccount *account = list->data;
351 if (account == NULL) {
352 debug_print("INC: Huh? inc_account_list_mail() got a NULL account, this should not happen!\n");
356 debug_print("INC: checking account %d\n", account->account_id);
357 switch (account->protocol) {
359 if (!(account->receive_in_progress)) {
360 IncSession *session = inc_session_new(account);
362 if (session != NULL) {
363 debug_print("INC: adding POP3 account %d to inc queue\n",
364 account->account_id);
365 queue_list = g_list_append(queue_list, session);
372 new_msgs += folderview_check_new(FOLDER(account->folder));
376 num = inc_spool_account(account);
382 /* Nothing to do here, it's a SMTP-only account. */
386 debug_print("INC: encountered account %d with unknown protocol %d, ignoring\n",
387 account->account_id, account->protocol);
395 inc_dialog = inc_progress_dialog_create(autocheck);
396 inc_dialog->queue_list = queue_list;
397 inc_dialog->mainwin = mainwin;
398 inc_progress_dialog_set_list(inc_dialog);
400 toolbar_main_set_sensitive(mainwin);
401 main_window_set_menu_sensitive(mainwin);
402 new_msgs += inc_start(inc_dialog);
405 inc_update_stats(new_msgs);
406 inc_finished(mainwin, new_msgs > 0, autocheck);
407 main_window_unlock(mainwin);
408 inc_notify_cmd(new_msgs, notify);
411 void inc_all_account_mail(MainWindow *mainwin, gboolean autocheck,
414 GList *list, *list2 = NULL;
417 debug_print("INC: inc_all_account_mail(), autocheck: %s\n",
418 autocheck ? "YES" : "NO");
420 /* Collect list of accounts which use the global autocheck interval. */
421 for (list = account_get_list(); list != NULL; list = list->next) {
422 PrefsAccount *account = list->data;
424 /* Nothing to do for SMTP-only accounts. */
425 if (account->protocol == A_NONE)
428 /* Set up condition which decides whether or not to check
429 * this account, based on whether we're doing global autocheck
430 * or a manual 'Get all' check. */
432 condition = prefs_common_get_prefs()->autochk_newmail
433 && account->autochk_use_default;
435 condition = account->recv_at_getall;
438 debug_print("INC: will check account %d\n", account->account_id);
439 list2 = g_list_append(list2, account);
443 /* Do the check on the collected accounts. */
445 inc_account_list_mail(mainwin, list2, autocheck, notify);
450 static void inc_progress_dialog_size_allocate_cb(GtkWidget *widget,
451 GtkAllocation *allocation)
453 cm_return_if_fail(allocation != NULL);
455 prefs_common.receivewin_width = allocation->width;
456 prefs_common.receivewin_height = allocation->height;
459 static IncProgressDialog *inc_progress_dialog_create(gboolean autocheck)
461 IncProgressDialog *dialog;
462 ProgressDialog *progress;
463 static GdkGeometry geometry;
465 dialog = g_new0(IncProgressDialog, 1);
467 progress = progress_dialog_create();
468 gtk_window_set_title(GTK_WINDOW(progress->window),
469 _("Retrieving new messages"));
470 g_signal_connect(G_OBJECT(progress->showlog_btn), "clicked",
471 G_CALLBACK(inc_showlog_cb), dialog);
472 g_signal_connect(G_OBJECT(progress->cancel_btn), "clicked",
473 G_CALLBACK(inc_cancel_cb), dialog);
474 g_signal_connect(G_OBJECT(progress->window), "delete_event",
475 G_CALLBACK(inc_dialog_delete_cb), dialog);
476 g_signal_connect(G_OBJECT(progress->window), "size_allocate",
477 G_CALLBACK(inc_progress_dialog_size_allocate_cb), NULL);
478 /* manage_window_set_transient(GTK_WINDOW(progress->window)); */
480 progress_dialog_get_fraction(progress);
482 stock_pixbuf_gdk(STOCK_PIXMAP_COMPLETE, &okpix);
483 stock_pixbuf_gdk(STOCK_PIXMAP_CONTINUE, ¤tpix);
484 stock_pixbuf_gdk(STOCK_PIXMAP_ERROR, &errorpix);
486 if (!geometry.min_height) {
487 geometry.min_width = 460;
488 geometry.min_height = 250;
491 gtk_window_set_geometry_hints(GTK_WINDOW(progress->window), NULL, &geometry,
493 gtk_widget_set_size_request(progress->window, prefs_common.receivewin_width,
494 prefs_common.receivewin_height);
496 if (prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS ||
497 (prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL &&
499 dialog->show_dialog = TRUE;
500 gtk_widget_show_now(progress->window);
503 dialog->dialog = progress;
504 g_get_current_time(&dialog->progress_tv);
505 g_get_current_time(&dialog->folder_tv);
506 dialog->queue_list = NULL;
509 inc_dialog_list = g_list_append(inc_dialog_list, dialog);
514 static void inc_progress_dialog_set_list(IncProgressDialog *inc_dialog)
518 for (list = inc_dialog->queue_list; list != NULL; list = list->next) {
519 IncSession *session = list->data;
520 Pop3Session *pop3_session = POP3_SESSION(session->session);
522 session->data = inc_dialog;
524 progress_dialog_list_set(inc_dialog->dialog,
526 pop3_session->ac_prefs->account_name,
531 static void inc_progress_dialog_clear(IncProgressDialog *inc_dialog)
533 progress_dialog_get_fraction(inc_dialog->dialog);
534 progress_dialog_set_label(inc_dialog->dialog, "");
535 if (inc_dialog->mainwin)
536 main_window_progress_off(inc_dialog->mainwin);
539 static void inc_progress_dialog_destroy(IncProgressDialog *inc_dialog)
541 cm_return_if_fail(inc_dialog != NULL);
543 inc_dialog_list = g_list_remove(inc_dialog_list, inc_dialog);
545 if (inc_dialog->mainwin)
546 main_window_progress_off(inc_dialog->mainwin);
547 progress_dialog_destroy(inc_dialog->dialog);
552 static IncSession *inc_session_new(PrefsAccount *account)
556 cm_return_val_if_fail(account != NULL, NULL);
558 if (account->protocol != A_POP3)
560 if (!account->recv_server || !account->userid)
563 session = g_new0(IncSession, 1);
565 session->session = pop3_session_new(account);
566 session->session->data = session;
567 POP3_SESSION(session->session)->drop_message = inc_drop_message;
568 session_set_recv_message_notify(session->session,
569 inc_recv_message, session);
570 session_set_recv_data_progressive_notify(session->session,
571 inc_recv_data_progressive,
573 session_set_recv_data_notify(session->session,
574 inc_recv_data_finished, session);
579 static void inc_session_destroy(IncSession *session)
581 cm_return_if_fail(session != NULL);
583 session_destroy(session->session);
587 static gint pop3_get_port(Pop3Session *pop3_session)
590 return pop3_session->ac_prefs->set_popport ?
591 pop3_session->ac_prefs->popport :
592 pop3_session->ac_prefs->ssl_pop == SSL_TUNNEL ? 995 : 110;
594 return pop3_session->ac_prefs->set_popport ?
595 pop3_session->ac_prefs->popport : 110;
599 static gint inc_start(IncProgressDialog *inc_dialog)
603 Pop3Session *pop3_session;
609 FolderItem *processing, *inbox;
610 GSList *msglist, *msglist_element;
611 gboolean cancelled = FALSE;
613 qlist = inc_dialog->queue_list;
614 while (qlist != NULL) {
615 GList *next = qlist->next;
617 session = qlist->data;
618 pop3_session = POP3_SESSION(session->session);
619 pop3_session->user = g_strdup(pop3_session->ac_prefs->userid);
621 if (inc_dialog->show_dialog)
622 manage_window_focus_in
623 (inc_dialog->dialog->window,
626 if (password_get(pop3_session->user,
627 pop3_session->ac_prefs->recv_server,
628 "pop3", pop3_get_port(pop3_session),
629 &(pop3_session->pass))) {
631 } else if ((pop3_session->pass = passwd_store_get_account(
632 pop3_session->ac_prefs->account_id, PWS_ACCOUNT_RECV)) == NULL) {
635 pass = input_dialog_query_password_keep
636 (pop3_session->ac_prefs->recv_server,
638 &(pop3_session->ac_prefs->session_passwd));
641 pop3_session->pass = pass;
645 if (inc_dialog->show_dialog)
646 manage_window_focus_out
647 (inc_dialog->dialog->window,
653 #define SET_PIXMAP_AND_TEXT(pix, str) \
655 progress_dialog_list_set(inc_dialog->dialog, \
656 inc_dialog->cur_row, \
662 for (; inc_dialog->queue_list != NULL && !cancelled; inc_dialog->cur_row++) {
663 session = inc_dialog->queue_list->data;
664 pop3_session = POP3_SESSION(session->session);
665 GSList *filtered, *unfiltered;
667 if (pop3_session->pass == NULL) {
668 SET_PIXMAP_AND_TEXT(okpix, _("Cancelled"));
669 inc_session_destroy(session);
670 inc_dialog->queue_list =
671 g_list_remove(inc_dialog->queue_list, session);
675 inc_progress_dialog_clear(inc_dialog);
676 progress_dialog_scroll_to_row(inc_dialog->dialog,
677 inc_dialog->cur_row);
679 SET_PIXMAP_AND_TEXT(currentpix, _("Retrieving"));
681 /* begin POP3 session */
682 inc_state = inc_pop3_session_do(session);
686 if (pop3_session->cur_total_num > 0)
687 msg = g_strdup_printf(
688 ngettext("Done (%d message (%s) received)",
689 "Done (%d messages (%s) received)",
690 pop3_session->cur_total_num),
691 pop3_session->cur_total_num,
692 to_human_readable((goffset)pop3_session->cur_total_recv_bytes));
694 msg = g_strdup_printf(_("Done (no new messages)"));
695 SET_PIXMAP_AND_TEXT(okpix, msg);
698 case INC_CONNECT_ERROR:
699 SET_PIXMAP_AND_TEXT(errorpix, _("Connection failed"));
701 case INC_AUTH_FAILED:
702 SET_PIXMAP_AND_TEXT(errorpix, _("Auth failed"));
703 if (pop3_session->ac_prefs->session_passwd) {
704 g_free(pop3_session->ac_prefs->session_passwd);
705 pop3_session->ac_prefs->session_passwd = NULL;
709 SET_PIXMAP_AND_TEXT(errorpix, _("Locked"));
714 case INC_SOCKET_ERROR:
716 SET_PIXMAP_AND_TEXT(errorpix, _("Error"));
719 SET_PIXMAP_AND_TEXT(errorpix, _("Timeout"));
722 SET_PIXMAP_AND_TEXT(okpix, _("Cancelled"));
723 if (!inc_dialog->show_dialog)
730 if (pop3_session->error_val == PS_AUTHFAIL) {
731 if(!prefs_common.no_recv_err_panel) {
732 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
733 ((prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL) && focus_window))
734 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
738 /* CLAWS: perform filtering actions on dropped message */
739 /* CLAWS: get default inbox (perhaps per account) */
740 if (pop3_session->ac_prefs->inbox) {
741 /* CLAWS: get destination folder / mailbox */
742 inbox = folder_find_item_from_identifier(pop3_session->ac_prefs->inbox);
744 inbox = folder_get_default_inbox();
746 inbox = folder_get_default_inbox();
748 /* get list of messages in processing */
749 processing = folder_get_default_processing(pop3_session->ac_prefs->account_id);
750 folder_item_scan(processing);
751 msglist = folder_item_get_msg_list(processing);
753 /* process messages */
754 folder_item_update_freeze();
756 procmsg_msglist_filter(msglist, pop3_session->ac_prefs,
757 &filtered, &unfiltered,
758 pop3_session->ac_prefs->filter_on_recv);
760 filtering_move_and_copy_msgs(msglist);
761 if (unfiltered != NULL)
762 folder_item_move_msgs(inbox, unfiltered);
764 for(msglist_element = msglist; msglist_element != NULL;
765 msglist_element = msglist_element->next) {
766 procmsg_msginfo_free((MsgInfo**)&(msglist_element->data));
768 folder_item_update_thaw();
770 g_slist_free(msglist);
771 g_slist_free(filtered);
772 g_slist_free(unfiltered);
776 new_msgs += pop3_session->cur_total_num;
778 pop3_write_uidl_list(pop3_session);
780 if (inc_state != INC_SUCCESS && inc_state != INC_CANCEL) {
782 if (inc_dialog->show_dialog)
783 manage_window_focus_in
784 (inc_dialog->dialog->window,
786 inc_put_error(inc_state, pop3_session);
787 if (inc_dialog->show_dialog)
788 manage_window_focus_out
789 (inc_dialog->dialog->window,
791 if (inc_state == INC_NO_SPACE ||
792 inc_state == INC_IO_ERROR)
795 folder_item_free_cache(processing, TRUE);
797 inc_session_destroy(session);
798 inc_dialog->queue_list =
799 g_list_remove(inc_dialog->queue_list, session);
802 #undef SET_PIXMAP_AND_TEXT
805 fin_msg = g_strdup_printf(ngettext("Finished (%d new message)",
806 "Finished (%d new messages)",
807 new_msgs), new_msgs);
809 fin_msg = g_strdup_printf(_("Finished (no new messages)"));
811 progress_dialog_set_label(inc_dialog->dialog, fin_msg);
813 while (inc_dialog->queue_list != NULL) {
814 session = inc_dialog->queue_list->data;
815 inc_session_destroy(session);
816 inc_dialog->queue_list =
817 g_list_remove(inc_dialog->queue_list, session);
820 if (prefs_common.close_recv_dialog || !inc_dialog->show_dialog)
821 inc_progress_dialog_destroy(inc_dialog);
823 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window),
825 gtk_button_set_label(GTK_BUTTON(inc_dialog->dialog->cancel_btn),
834 static IncState inc_pop3_session_do(IncSession *session)
836 Pop3Session *pop3_session = POP3_SESSION(session->session);
837 IncProgressDialog *inc_dialog = (IncProgressDialog *)session->data;
838 PrefsAccount *ac = pop3_session->ac_prefs;
843 ProxyInfo *proxy_info = NULL;
845 debug_print("getting new messages of account %s...\n",
848 ac->last_pop_login_time = time(NULL);
850 buf = g_strdup_printf(_("%s: Retrieving new messages"),
852 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window), buf);
855 server = ac->recv_server;
856 account_name = ac->account_name;
857 port = pop3_get_port(pop3_session);
860 SESSION(pop3_session)->ssl_type = ac->ssl_pop;
861 if (ac->ssl_pop != SSL_NONE)
862 SESSION(pop3_session)->nonblocking =
863 ac->use_nonblocking_ssl;
865 if (ac->ssl_pop != SSL_NONE) {
866 if (alertpanel_full(_("Insecure connection"),
867 _("This connection is configured to be secured "
868 "using SSL/TLS, but SSL/TLS is not available "
869 "in this build of Claws Mail. \n\n"
870 "Do you want to continue connecting to this "
871 "server? The communication would not be "
873 GTK_STOCK_CANCEL, _("Con_tinue connecting"),
874 NULL, FALSE, NULL, ALERT_WARNING,
875 G_ALERTDEFAULT) != G_ALERTALTERNATE)
880 buf = g_strdup_printf(_("Account '%s': Connecting to POP3 server: %s:%d..."),
881 account_name, server, port);
882 statusbar_print_all("%s", buf);
883 log_message(LOG_PROTOCOL, "%s\n", buf);
885 progress_dialog_set_label(inc_dialog->dialog, buf);
888 if (ac->use_default_proxy) {
889 proxy_info = (ProxyInfo *)&(prefs_common.proxy_info);
890 if (proxy_info->use_proxy_auth)
891 proxy_info->proxy_pass = passwd_store_get(PWS_CORE, PWS_CORE_PROXY,
892 PWS_CORE_PROXY_PASS);
894 proxy_info = (ProxyInfo *)&(ac->proxy_info);
895 if (proxy_info->use_proxy_auth)
896 proxy_info->proxy_pass = passwd_store_get_account(ac->account_id,
897 PWS_ACCOUNT_PROXY_PASS);
900 SESSION(pop3_session)->proxy_info = proxy_info;
905 session_set_timeout(SESSION(pop3_session),
906 prefs_common.io_timeout_secs * 1000);
908 if (session_connect(SESSION(pop3_session), server, port) < 0) {
909 if(!prefs_common.no_recv_err_panel) {
910 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
911 ((prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL) && focus_window)) {
912 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
914 alertpanel_error(_("Can't connect to POP3 server: %s:%d"),
916 manage_window_focus_out(inc_dialog->dialog->window, NULL, NULL);
918 log_error(LOG_PROTOCOL, _("Can't connect to POP3 server: %s:%d\n"),
921 session->inc_state = INC_CONNECT_ERROR;
923 return INC_CONNECT_ERROR;
926 while (session_is_running(SESSION(pop3_session)) &&
927 session->inc_state != INC_CANCEL)
928 gtk_main_iteration();
930 if (session->inc_state == INC_SUCCESS) {
931 switch (pop3_session->error_val) {
933 switch (SESSION(pop3_session)->state) {
935 if (pop3_session->state == POP3_READY)
936 session->inc_state = INC_CONNECT_ERROR;
938 session->inc_state = INC_ERROR;
941 session->inc_state = INC_EOF;
943 case SESSION_TIMEOUT:
944 session->inc_state = INC_TIMEOUT;
947 session->inc_state = INC_SUCCESS;
952 session->inc_state = INC_AUTH_FAILED;
955 session->inc_state = INC_IO_ERROR;
958 session->inc_state = INC_SOCKET_ERROR;
961 session->inc_state = INC_LOCKED;
964 session->inc_state = INC_ERROR;
969 session_disconnect(SESSION(pop3_session));
972 return session->inc_state;
975 static void inc_progress_dialog_update(IncProgressDialog *inc_dialog,
976 IncSession *inc_session)
978 inc_progress_dialog_set_label(inc_dialog, inc_session);
979 inc_progress_dialog_set_progress(inc_dialog, inc_session);
982 static void inc_progress_dialog_set_label(IncProgressDialog *inc_dialog,
983 IncSession *inc_session)
985 ProgressDialog *dialog = inc_dialog->dialog;
986 Pop3Session *session;
988 cm_return_if_fail(inc_session != NULL);
990 session = POP3_SESSION(inc_session->session);
992 switch (session->state) {
995 case POP3_GETAUTH_USER:
996 case POP3_GETAUTH_PASS:
997 case POP3_GETAUTH_APOP:
998 progress_dialog_set_label(dialog, _("Authenticating..."));
1000 statusbar_print_all(_("Retrieving messages from %s (%s)..."),
1001 SESSION(session)->server,
1002 session->ac_prefs->account_name);
1004 case POP3_GETRANGE_STAT:
1005 progress_dialog_set_label
1006 (dialog, _("Getting the number of new messages (STAT)..."));
1008 case POP3_GETRANGE_LAST:
1009 progress_dialog_set_label
1010 (dialog, _("Getting the number of new messages (LAST)..."));
1012 case POP3_GETRANGE_UIDL:
1013 progress_dialog_set_label
1014 (dialog, _("Getting the number of new messages (UIDL)..."));
1016 case POP3_GETSIZE_LIST:
1017 progress_dialog_set_label
1018 (dialog, _("Getting the size of messages (LIST)..."));
1021 case POP3_RETR_RECV:
1025 progress_dialog_set_label(dialog, _("Quitting"));
1032 static void inc_progress_dialog_set_progress(IncProgressDialog *inc_dialog,
1033 IncSession *inc_session)
1035 gchar buf[MESSAGEBUFSIZE];
1036 Pop3Session *pop3_session = POP3_SESSION(inc_session->session);
1037 gchar *total_size_str;
1041 if (!pop3_session->new_msg_exist) return;
1043 cur_total = inc_session->cur_total_bytes;
1044 total = pop3_session->total_bytes;
1045 if (pop3_session->state == POP3_RETR ||
1046 pop3_session->state == POP3_RETR_RECV ||
1047 pop3_session->state == POP3_DELETE) {
1048 Xstrdup_a(total_size_str, to_human_readable((goffset)total), return);
1049 g_snprintf(buf, sizeof(buf),
1050 _("Retrieving message (%d / %d) (%s / %s)"),
1051 pop3_session->cur_msg, pop3_session->count,
1052 to_human_readable((goffset)cur_total), total_size_str);
1053 progress_dialog_set_label(inc_dialog->dialog, buf);
1056 progress_dialog_set_fraction
1057 (inc_dialog->dialog, (total == 0) ? 0: (gfloat)cur_total / (gfloat)total);
1059 statusbar_progress_all(pop3_session->cur_msg, pop3_session->count, 1);
1061 if (pop3_session->cur_total_num > 0) {
1062 g_snprintf(buf, sizeof(buf),
1063 ngettext("Retrieving (%d message (%s) received)",
1064 "Retrieving (%d messages (%s) received)",
1065 pop3_session->cur_total_num),
1066 pop3_session->cur_total_num,
1068 ((goffset)pop3_session->cur_total_recv_bytes));
1069 progress_dialog_list_set_status(inc_dialog->dialog,
1070 inc_dialog->cur_row,
1075 static void inc_progress_dialog_update_periodic(IncProgressDialog *inc_dialog,
1076 IncSession *inc_session)
1082 g_get_current_time(&tv_cur);
1084 tv_result.tv_sec = tv_cur.tv_sec - inc_dialog->progress_tv.tv_sec;
1085 tv_result.tv_usec = tv_cur.tv_usec - inc_dialog->progress_tv.tv_usec;
1086 if (tv_result.tv_usec < 0) {
1088 tv_result.tv_usec += G_USEC_PER_SEC;
1091 msec = tv_result.tv_sec * 1000 + tv_result.tv_usec / 1000;
1092 if (msec > PROGRESS_UPDATE_INTERVAL) {
1093 inc_progress_dialog_update(inc_dialog, inc_session);
1094 inc_dialog->progress_tv.tv_sec = tv_cur.tv_sec;
1095 inc_dialog->progress_tv.tv_usec = tv_cur.tv_usec;
1099 static gint inc_recv_data_progressive(Session *session, guint cur_len,
1100 guint total_len, gpointer data)
1102 IncSession *inc_session = (IncSession *)data;
1103 Pop3Session *pop3_session = POP3_SESSION(session);
1104 IncProgressDialog *inc_dialog;
1107 cm_return_val_if_fail(inc_session != NULL, -1);
1109 if (pop3_session->state != POP3_RETR &&
1110 pop3_session->state != POP3_RETR_RECV &&
1111 pop3_session->state != POP3_DELETE &&
1112 pop3_session->state != POP3_LOGOUT) return 0;
1114 if (!pop3_session->new_msg_exist) return 0;
1116 cur_total = pop3_session->cur_total_bytes + cur_len;
1117 if (cur_total > pop3_session->total_bytes)
1118 cur_total = pop3_session->total_bytes;
1119 inc_session->cur_total_bytes = cur_total;
1121 inc_dialog = (IncProgressDialog *)inc_session->data;
1122 inc_progress_dialog_update_periodic(inc_dialog, inc_session);
1127 static gint inc_recv_data_finished(Session *session, guint len, gpointer data)
1129 IncSession *inc_session = (IncSession *)data;
1130 IncProgressDialog *inc_dialog;
1132 cm_return_val_if_fail(inc_session != NULL, -1);
1134 inc_dialog = (IncProgressDialog *)inc_session->data;
1136 inc_recv_data_progressive(session, 0, 0, inc_session);
1138 if (POP3_SESSION(session)->state == POP3_LOGOUT) {
1139 inc_progress_dialog_update(inc_dialog, inc_session);
1145 static gint inc_recv_message(Session *session, const gchar *msg, gpointer data)
1147 IncSession *inc_session = (IncSession *)data;
1148 IncProgressDialog *inc_dialog;
1150 cm_return_val_if_fail(inc_session != NULL, -1);
1152 inc_dialog = (IncProgressDialog *)inc_session->data;
1154 switch (POP3_SESSION(session)->state) {
1155 case POP3_GETAUTH_USER:
1156 case POP3_GETAUTH_PASS:
1157 case POP3_GETAUTH_APOP:
1158 case POP3_GETRANGE_STAT:
1159 case POP3_GETRANGE_LAST:
1160 case POP3_GETRANGE_UIDL:
1161 case POP3_GETSIZE_LIST:
1162 inc_progress_dialog_update(inc_dialog, inc_session);
1165 inc_recv_data_progressive(session, 0, 0, inc_session);
1168 inc_progress_dialog_update(inc_dialog, inc_session);
1177 static gint inc_drop_message(Pop3Session *session, const gchar *file)
1180 FolderItem *dropfolder;
1181 IncSession *inc_session = (IncSession *)(SESSION(session)->data);
1184 cm_return_val_if_fail(inc_session != NULL, -1);
1186 if (session->ac_prefs->inbox) {
1187 inbox = folder_find_item_from_identifier
1188 (session->ac_prefs->inbox);
1190 inbox = folder_get_default_inbox();
1192 inbox = folder_get_default_inbox();
1198 /* CLAWS: claws uses a global .processing folder for the filtering. */
1199 dropfolder = folder_get_default_processing(session->ac_prefs->account_id);
1201 /* add msg file to drop folder */
1202 if ((msgnum = folder_item_add_msg(
1203 dropfolder, file, NULL, TRUE)) < 0) {
1211 static void inc_put_error(IncState istate, Pop3Session *session)
1213 gchar *log_msg = NULL;
1214 gchar *err_msg = NULL;
1215 gboolean fatal_error = FALSE;
1218 case INC_CONNECT_ERROR:
1220 if (prefs_common.no_recv_err_panel)
1222 err_msg = g_strdup_printf(_("Connection to %s:%d failed."),
1223 SESSION(session)->server,
1224 SESSION(session)->port);
1227 log_msg = _("Error occurred while processing mail.");
1229 if (prefs_common.no_recv_err_panel)
1231 if (session->error_msg)
1232 err_msg = g_strdup_printf
1233 (_("Error occurred while processing mail:\n%s"),
1234 session->error_msg);
1236 err_msg = g_strdup(log_msg);
1239 log_msg = _("No disk space left.");
1240 err_msg = g_strdup(log_msg);
1244 log_msg = _("Can't write file.");
1245 err_msg = g_strdup(log_msg);
1248 case INC_SOCKET_ERROR:
1249 log_msg = _("Socket error.");
1250 if (prefs_common.no_recv_err_panel)
1252 err_msg = g_strdup_printf(_("Socket error on connection to %s:%d."),
1253 SESSION(session)->server,
1254 SESSION(session)->port);
1257 log_msg = _("Connection closed by the remote host.");
1258 if (prefs_common.no_recv_err_panel)
1260 err_msg = g_strdup_printf(_("Connection to %s:%d closed by the remote host."),
1261 SESSION(session)->server,
1262 SESSION(session)->port);
1265 log_msg = _("Mailbox is locked.");
1266 if (prefs_common.no_recv_err_panel)
1268 if (session->error_msg)
1269 err_msg = g_strdup_printf(_("Mailbox is locked:\n%s"),
1270 session->error_msg);
1272 err_msg = g_strdup(log_msg);
1274 case INC_AUTH_FAILED:
1275 log_msg = _("Authentication failed.");
1277 if (prefs_common.no_recv_err_panel)
1279 if (session->error_msg)
1280 err_msg = g_strdup_printf
1281 (_("Authentication failed:\n%s"), session->error_msg);
1283 err_msg = g_strdup(log_msg);
1286 log_msg = _("Session timed out. You may be able to "
1287 "recover by increasing the timeout value in "
1288 "Preferences/Other/Miscellaneous.");
1289 if (prefs_common.no_recv_err_panel)
1291 err_msg = g_strdup_printf(_("Connection to %s:%d timed out."),
1292 SESSION(session)->server,
1293 SESSION(session)->port);
1301 log_error(LOG_PROTOCOL, "%s\n", log_msg);
1303 log_warning(LOG_PROTOCOL, "%s\n", log_msg);
1305 if (prefs_common.no_recv_err_panel && fatal_error)
1306 mainwindow_show_error();
1309 alertpanel_error_log("%s", err_msg);
1314 static void inc_cancel(IncProgressDialog *dialog)
1316 IncSession *session;
1318 cm_return_if_fail(dialog != NULL);
1320 if (dialog->queue_list == NULL) {
1321 inc_progress_dialog_destroy(dialog);
1325 session = dialog->queue_list->data;
1327 session->inc_state = INC_CANCEL;
1329 log_message(LOG_PROTOCOL, _("Incorporation cancelled\n"));
1332 gboolean inc_is_active(void)
1334 return (inc_dialog_list != NULL);
1337 void inc_cancel_all(void)
1341 for (cur = inc_dialog_list; cur != NULL; cur = cur->next)
1342 inc_cancel((IncProgressDialog *)cur->data);
1345 static void inc_showlog_cb(GtkWidget *widget, gpointer data)
1347 MainWindow *mainwin = mainwindow_get_mainwindow();
1349 log_window_show(mainwin->logwin);
1352 static void inc_cancel_cb(GtkWidget *widget, gpointer data)
1354 inc_cancel((IncProgressDialog *)data);
1357 static gint inc_dialog_delete_cb(GtkWidget *widget, GdkEventAny *event,
1360 IncProgressDialog *dialog = (IncProgressDialog *)data;
1362 if (dialog->queue_list == NULL)
1363 inc_progress_dialog_destroy(dialog);
1368 static gint inc_spool_account(PrefsAccount *account)
1374 if (account->local_inbox) {
1375 inbox = folder_find_item_from_identifier(account->local_inbox);
1377 inbox = folder_get_default_inbox();
1379 inbox = folder_get_default_inbox();
1381 if (account->local_mbox) {
1382 if (is_file_exist(account->local_mbox))
1383 mbox = g_strdup(account->local_mbox);
1384 else if (is_dir_exist(account->local_mbox))
1385 mbox = g_strconcat(account->local_mbox, G_DIR_SEPARATOR_S,
1386 g_get_user_name(), NULL);
1388 debug_print("%s: local mailbox not found.\n",
1389 account->local_mbox);
1393 debug_print("local mailbox not set in account info.\n");
1397 result = get_spool(inbox, mbox, account);
1400 statusbar_pop_all();
1405 static gint get_spool(FolderItem *dest, const gchar *mbox, PrefsAccount *account)
1409 gchar tmp_mbox[MAXPATHLEN + 1];
1411 cm_return_val_if_fail(dest != NULL, -1);
1412 cm_return_val_if_fail(mbox != NULL, -1);
1413 cm_return_val_if_fail(account != NULL, -1);
1415 if (!is_file_exist(mbox) || (size = get_file_size(mbox)) == 0) {
1416 debug_print("%s: no messages in local mailbox.\n", mbox);
1418 } else if (size < 0)
1421 if ((lockfd = lock_mbox(mbox, LOCK_FLOCK)) < 0)
1424 g_snprintf(tmp_mbox, sizeof(tmp_mbox), "%s%ctmpmbox.%p",
1425 get_tmp_dir(), G_DIR_SEPARATOR, mbox);
1427 if (copy_mbox(lockfd, tmp_mbox) < 0) {
1428 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1432 debug_print("Getting new messages from %s into %s...\n",
1435 msgs = proc_mbox(dest, tmp_mbox, account->filter_on_recv, account);
1437 claws_unlink(tmp_mbox);
1438 if (msgs >= 0) empty_mbox(mbox);
1439 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1444 void inc_lock_real(void)
1449 void inc_unlock_real(void)
1451 if (inc_lock_count > 0)
1455 static guint autocheck_timer = 0;
1456 static gpointer autocheck_data = NULL;
1458 static void inc_notify_cmd(gint new_msgs, gboolean notify)
1460 gchar *buf, *numpos, *ret_str;
1461 gssize by_read = 0, by_written = 0;
1463 if (!(new_msgs && notify && prefs_common.newmail_notify_cmd &&
1464 *prefs_common.newmail_notify_cmd))
1467 buf = g_strdup(prefs_common.newmail_notify_cmd);
1468 if ((numpos = strstr(buf, "%d")) != NULL) {
1472 buf2 = g_strdup_printf("%s%d%s", buf, new_msgs, numpos + 2);
1477 ret_str = g_locale_from_utf8(buf, strlen(buf), &by_read, &by_written,
1479 if (ret_str && by_written) {
1483 debug_print("executing new mail notification command: %s\n", buf);
1484 execute_command_line(buf, TRUE, NULL);
1489 void inc_autocheck_timer_init(MainWindow *mainwin)
1491 autocheck_data = mainwin;
1492 inc_autocheck_timer_set();
1495 static void inc_autocheck_timer_set_interval(guint _interval)
1497 guint interval = _interval;
1499 /* Convert the interval to seconds if needed. */
1500 if (_interval % 1000 == 0)
1503 inc_autocheck_timer_remove();
1504 /* last test is to avoid re-enabling auto_check after modifying
1505 the common preferences */
1506 if (prefs_common.autochk_newmail && autocheck_data
1507 && prefs_common.work_offline == FALSE) {
1509 g_timeout_add_seconds(interval, inc_autocheck_func, autocheck_data);
1510 debug_print("added global inc timer %d at %u seconds\n",
1511 autocheck_timer, interval);
1515 void inc_autocheck_timer_set(void)
1517 inc_autocheck_timer_set_interval(prefs_common.autochk_itv * 1000);
1520 void inc_autocheck_timer_remove(void)
1522 if (autocheck_timer) {
1523 debug_print("removed global inc timer %d\n", autocheck_timer);
1524 g_source_remove(autocheck_timer);
1525 autocheck_timer = 0;
1529 static gint inc_autocheck_func(gpointer data)
1531 MainWindow *mainwin = (MainWindow *)data;
1533 if (inc_lock_count) {
1534 debug_print("global inc: autocheck is locked.\n");
1535 inc_autocheck_timer_set_interval(1000);
1539 inc_all_account_mail(mainwin, TRUE, prefs_common.newmail_notify_auto);
1540 inc_autocheck_timer_set();
1545 static gboolean inc_account_autocheck_func(gpointer data)
1547 PrefsAccount *account = (PrefsAccount *)data;
1550 cm_return_val_if_fail(account != NULL, FALSE);
1552 debug_print("account %d: inc_account_autocheck_func\n",
1553 account->account_id);
1555 list = g_list_append(list, account);
1556 inc_account_list_mail(mainwindow_get_mainwindow(),
1557 list, TRUE, prefs_common.newmail_notify_auto);
1560 inc_account_autocheck_timer_set_interval(account);
1565 void inc_account_autocheck_timer_remove(PrefsAccount *account)
1567 cm_return_if_fail(account != NULL);
1569 if (account->autocheck_timer != 0) {
1570 g_source_remove(account->autocheck_timer);
1571 debug_print("INC: account %d: removed inc timer %d\n", account->account_id,
1572 account->autocheck_timer);
1573 account->autocheck_timer = 0;
1577 void inc_account_autocheck_timer_set_interval(PrefsAccount *account)
1579 cm_return_if_fail(account != NULL);
1581 inc_account_autocheck_timer_remove(account);
1583 if (account->autochk_use_default
1584 || !account->autochk_use_custom
1585 || account->autochk_itv == 0)
1588 account->autocheck_timer = g_timeout_add_seconds(
1589 account->autochk_itv, inc_account_autocheck_func, account);
1590 debug_print("INC: account %d: added inc timer %d at %u seconds\n",
1591 account->account_id, account->autocheck_timer, account->autochk_itv);
1594 gboolean inc_offline_should_override(gboolean force_ask, const gchar *msg)
1596 static time_t overridden_yes = 0;
1597 static time_t overridden_no = 0;
1598 int length = 10; /* minutes */
1599 gint answer = G_ALERTDEFAULT;
1601 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1602 /* If no network connection is available, override is not possible */
1603 if(!networkmanager_is_online(NULL))
1607 if (prefs_common.autochk_newmail)
1608 length = prefs_common.autochk_itv; /* minutes */
1611 overridden_no = (time_t)0;
1614 if (prefs_common.work_offline) {
1617 if (time(NULL) - overridden_yes < length * 60) /* seconds */
1619 else if (time(NULL) - overridden_no < length * 60) /* seconds */
1623 tmp = g_strdup_printf(
1624 _("%s%sYou're working offline. Override for %d minutes?"),
1629 tmp = g_strdup_printf(
1630 _("%s%sYou're working offline. Override?"),
1634 answer = alertpanel(_("Offline warning"),
1636 GTK_STOCK_NO, GTK_STOCK_YES,
1637 !force_ask? _("On_ly once"):NULL, ALERTFOCUS_SECOND);
1639 if (answer == G_ALERTALTERNATE) {
1640 overridden_yes = time(NULL);
1642 } else if (answer == G_ALERTDEFAULT) {
1644 overridden_no = time(NULL);
1647 overridden_yes = (time_t)0;
1648 overridden_no = (time_t)0;