Fix a typo error, thanks to Milan Obuch.
[claws.git] / src / inc.c
blobe14de453ee59f7cd4895b7c8030242b7a41552ea
1 /*
2 * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2022 the Claws Mail team and Hiroyuki Yamamoto
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/>.
19 #ifdef HAVE_CONFIG_H
20 # include "config.h"
21 #include "claws-features.h"
22 #endif
24 #include "defs.h"
26 #include <glib.h>
27 #include <glib/gi18n.h>
28 #include <gtk/gtk.h>
29 #include <stdio.h>
30 #include <unistd.h>
31 #include <string.h>
33 #include "main.h"
34 #include "inc.h"
35 #include "mainwindow.h"
36 #include "folderview.h"
37 #include "summaryview.h"
38 #include "prefs_common.h"
39 #include "prefs_account.h"
40 #include "account.h"
41 #include "procmsg.h"
42 #include "proxy.h"
43 #include "socket.h"
44 #include "ssl.h"
45 #include "pop.h"
46 #include "recv.h"
47 #include "mbox.h"
48 #include "file-utils.h"
49 #include "utils.h"
50 #include "gtkutils.h"
51 #include "statusbar.h"
52 #include "msgcache.h"
53 #include "manage_window.h"
54 #include "stock_pixmap.h"
55 #include "progressdialog.h"
56 #include "inputdialog.h"
57 #include "alertpanel.h"
58 #include "folder.h"
59 #include "filtering.h"
60 #include "log.h"
61 #include "hooks.h"
62 #include "logwindow.h"
63 #include "passwordstore.h"
64 #include "oauth2.h"
66 extern SessionStats session_stats;
68 static GList *inc_dialog_list = NULL;
70 static time_t inc_offline_overridden_yes = 0;
71 static time_t inc_offline_overridden_no = 0;
73 guint inc_lock_count = 0;
75 static GdkPixbuf *currentpix;
76 static GdkPixbuf *errorpix;
77 static GdkPixbuf *okpix;
79 #define MESSAGEBUFSIZE 8192
81 static void inc_update_stats(gint new_msgs);
82 static void inc_finished (MainWindow *mainwin,
83 gboolean new_messages,
84 gboolean autocheck);
85 static gint inc_account_mail_real (MainWindow *mainwin,
86 PrefsAccount *account);
88 static IncProgressDialog *inc_progress_dialog_create
89 (gboolean autocheck);
90 static void inc_progress_dialog_set_list(IncProgressDialog *inc_dialog);
91 static void inc_progress_dialog_destroy (IncProgressDialog *inc_dialog);
93 static IncSession *inc_session_new (PrefsAccount *account);
94 static void inc_session_destroy (IncSession *session);
95 static gint inc_start (IncProgressDialog *inc_dialog);
96 static IncState inc_pop3_session_do (IncSession *session);
98 static void inc_progress_dialog_update (IncProgressDialog *inc_dialog,
99 IncSession *inc_session);
101 static void inc_progress_dialog_set_label
102 (IncProgressDialog *inc_dialog,
103 IncSession *inc_session);
104 static void inc_progress_dialog_set_progress
105 (IncProgressDialog *inc_dialog,
106 IncSession *inc_session);
108 static void inc_progress_dialog_update_periodic
109 (IncProgressDialog *inc_dialog,
110 IncSession *inc_session);
112 static gint inc_recv_data_progressive (Session *session,
113 guint cur_len,
114 guint total_len,
115 gpointer data);
116 static gint inc_recv_data_finished (Session *session,
117 guint len,
118 gpointer data);
119 static gint inc_recv_message (Session *session,
120 const gchar *msg,
121 gpointer data);
122 static gint inc_drop_message (Pop3Session *session,
123 const gchar *file);
125 static void inc_put_error (IncState istate,
126 Pop3Session *session);
128 static void inc_showlog_cb (GtkWidget *widget,
129 gpointer data);
130 static void inc_cancel_cb (GtkWidget *widget,
131 gpointer data);
132 static gint inc_dialog_delete_cb (GtkWidget *widget,
133 GdkEventAny *event,
134 gpointer data);
136 static gint get_spool (FolderItem *dest,
137 const gchar *mbox,
138 PrefsAccount *account);
140 static gint inc_spool_account(PrefsAccount *account);
141 static void inc_autocheck_timer_set_interval (guint interval);
142 static gint inc_autocheck_func (gpointer data);
144 static void inc_notify_cmd (gint new_msgs,
145 gboolean notify);
147 static void inc_update_stats(gint new_msgs)
149 /* update session statistics */
150 session_stats.received += new_msgs;
154 * inc_finished:
155 * @mainwin: Main window.
156 * @new_messages: TRUE if some messages have been received.
158 * Update the folder view and the summary view after receiving
159 * messages. If @new_messages is FALSE, this function avoids unneeded
160 * updating.
162 static void inc_finished(MainWindow *mainwin, gboolean new_messages, gboolean autocheck)
164 if (prefs_common.scan_all_after_inc)
165 folderview_check_new(NULL);
167 if (!autocheck && new_messages && prefs_common.open_inbox_on_inc) {
168 FolderItem *item = NULL;
170 if (cur_account && cur_account->inbox)
171 item = folder_find_item_from_identifier(cur_account->inbox);
172 if (item == NULL && cur_account && cur_account->folder)
173 item = cur_account->folder->inbox;
174 if (item == NULL)
175 item = folder_get_default_inbox();
177 folderview_unselect(mainwin->folderview);
178 folderview_select(mainwin->folderview, item);
180 statusbar_progress_all(0,0,0);
183 void inc_mail(MainWindow *mainwin, gboolean notify)
185 gint new_msgs = 0;
186 gint account_new_msgs = 0;
188 if (inc_lock_count) return;
190 if (prefs_common.work_offline &&
191 !inc_offline_should_override(TRUE,
192 _("Claws Mail needs network access in order "
193 "to get mails.")))
194 return;
196 inc_lock();
197 inc_autocheck_timer_remove();
198 main_window_lock(mainwin);
200 if (prefs_common.use_extinc && prefs_common.extinc_cmd) {
201 /* external incorporating program */
202 if (execute_command_line(prefs_common.extinc_cmd, FALSE, NULL) < 0) {
203 main_window_unlock(mainwin);
204 inc_autocheck_timer_set();
205 inc_unlock();
206 return;
208 } else {
209 account_new_msgs = inc_account_mail_real(mainwin, cur_account);
210 if (account_new_msgs > 0)
211 new_msgs += account_new_msgs;
214 inc_update_stats(new_msgs);
215 inc_finished(mainwin, new_msgs > 0, FALSE);
216 main_window_unlock(mainwin);
217 inc_notify_cmd(new_msgs, notify);
218 inc_autocheck_timer_set();
219 inc_unlock();
222 void inc_pop_before_smtp(PrefsAccount *acc)
224 IncProgressDialog *inc_dialog;
225 IncSession *session;
226 MainWindow *mainwin;
228 mainwin = mainwindow_get_mainwindow();
230 session = inc_session_new(acc);
231 if (!session) return;
232 POP3_SESSION(session->session)->pop_before_smtp = TRUE;
234 inc_dialog = inc_progress_dialog_create(FALSE);
235 inc_dialog->queue_list = g_list_append(inc_dialog->queue_list,
236 session);
237 /* FIXME: assumes to attach to first main window */
238 inc_dialog->mainwin = mainwin;
239 inc_progress_dialog_set_list(inc_dialog);
241 if (mainwin) {
242 toolbar_main_set_sensitive(mainwin);
243 main_window_set_menu_sensitive(mainwin);
246 inc_start(inc_dialog);
249 static gint inc_account_mail_real(MainWindow *mainwin, PrefsAccount *account)
251 IncProgressDialog *inc_dialog;
252 IncSession *session;
254 switch (account->protocol) {
255 case A_IMAP4:
256 case A_NNTP:
257 /* Melvin: bug [14]
258 * FIXME: it should return foldeview_check_new() value.
259 * TODO: do it when bug [19] is fixed (IMAP folder sets
260 * an incorrect new message count)
262 folderview_check_new(FOLDER(account->folder));
263 return 0;
264 case A_POP3:
265 session = inc_session_new(account);
266 if (!session) return 0;
268 inc_dialog = inc_progress_dialog_create(FALSE);
269 inc_dialog->queue_list = g_list_append(inc_dialog->queue_list,
270 session);
271 inc_dialog->mainwin = mainwin;
272 inc_progress_dialog_set_list(inc_dialog);
274 if (mainwin) {
275 toolbar_main_set_sensitive(mainwin);
276 main_window_set_menu_sensitive(mainwin);
279 return inc_start(inc_dialog);
281 case A_LOCAL:
282 return inc_spool_account(account);
284 default:
285 break;
287 return 0;
290 gint inc_account_mail(MainWindow *mainwin, PrefsAccount *account)
292 gint new_msgs;
294 if (inc_lock_count) return 0;
296 if (account->receive_in_progress) return 0;
298 if (prefs_common.work_offline &&
299 !inc_offline_should_override(TRUE,
300 _("Claws Mail needs network access in order "
301 "to get mails.")))
302 return 0;
304 inc_autocheck_timer_remove();
305 main_window_lock(mainwin);
307 new_msgs = inc_account_mail_real(mainwin, account);
309 inc_update_stats(new_msgs);
310 inc_finished(mainwin, new_msgs > 0, FALSE);
311 main_window_unlock(mainwin);
312 inc_autocheck_timer_set();
314 return new_msgs;
317 void inc_account_list_mail(MainWindow *mainwin, GList *account_list, gboolean autocheck,
318 gboolean notify)
320 GList *list, *queue_list = NULL;
321 IncProgressDialog *inc_dialog;
322 gint new_msgs = 0, num;
324 if (prefs_common.work_offline &&
325 !inc_offline_should_override( (autocheck == FALSE),
326 _("Claws Mail needs network access in order "
327 "to get mails.")))
328 return;
330 if (inc_lock_count) return;
332 main_window_lock(mainwin);
334 if (!account_list) {
335 inc_update_stats(new_msgs);
336 inc_finished(mainwin, new_msgs > 0, autocheck);
337 main_window_unlock(mainwin);
338 inc_notify_cmd(new_msgs, notify);
339 return;
342 if (prefs_common.use_extinc && prefs_common.extinc_cmd) {
343 /* external incorporating program */
344 if (execute_command_line(prefs_common.extinc_cmd, FALSE, NULL) < 0) {
345 log_error(LOG_PROTOCOL, _("%s failed\n"), prefs_common.extinc_cmd);
347 main_window_unlock(mainwin);
348 return;
352 /* Check all accounts in the list, one by one. */
353 for (list = account_list; list != NULL; list = list->next) {
354 PrefsAccount *account = list->data;
356 if (account == NULL) {
357 debug_print("INC: Huh? inc_account_list_mail() got a NULL account, this should not happen!\n");
358 continue;
361 debug_print("INC: checking account %d\n", account->account_id);
362 switch (account->protocol) {
363 case A_POP3:
364 if (!(account->receive_in_progress)) {
365 IncSession *session = inc_session_new(account);
367 if (session != NULL) {
368 debug_print("INC: adding POP3 account %d to inc queue\n",
369 account->account_id);
370 queue_list = g_list_append(queue_list, session);
373 break;
375 case A_IMAP4:
376 case A_NNTP:
377 new_msgs += folderview_check_new(FOLDER(account->folder));
378 break;
380 case A_LOCAL:
381 num = inc_spool_account(account);
382 if (num > 0)
383 new_msgs += num;
384 break;
386 case A_NONE:
387 /* Nothing to do here, it's a SMTP-only account. */
388 break;
390 default:
391 debug_print("INC: encountered account %d with unknown protocol %d, ignoring\n",
392 account->account_id, account->protocol);
393 break;
399 if (queue_list) {
400 inc_dialog = inc_progress_dialog_create(autocheck);
401 inc_dialog->queue_list = queue_list;
402 inc_dialog->mainwin = mainwin;
403 inc_progress_dialog_set_list(inc_dialog);
405 toolbar_main_set_sensitive(mainwin);
406 main_window_set_menu_sensitive(mainwin);
407 new_msgs += inc_start(inc_dialog);
410 inc_update_stats(new_msgs);
411 inc_finished(mainwin, new_msgs > 0, autocheck);
412 main_window_unlock(mainwin);
413 inc_notify_cmd(new_msgs, notify);
416 void inc_all_account_mail(MainWindow *mainwin, gboolean autocheck,
417 gboolean check_at_startup, gboolean notify)
419 GList *list, *list2 = NULL;
420 gboolean condition = FALSE;
421 gboolean hide_dialog = FALSE;
423 debug_print("INC: inc_all_account_mail(), autocheck: %s\n",
424 autocheck ? "YES" : "NO");
426 /* Collect list of accounts which use the global autocheck interval. */
427 for (list = account_get_list(); list != NULL; list = list->next) {
428 PrefsAccount *account = list->data;
430 /* Nothing to do for SMTP-only accounts. */
431 if (account->protocol == A_NONE)
432 continue;
434 /* Set up condition which decides whether or not to check
435 * this account, based on whether we're doing global autocheck
436 * or a check at startup or a manual 'Get all' check. */
437 if (autocheck)
438 condition = prefs_common_get_prefs()->autochk_newmail
439 && account->autochk_use_default;
440 else if (check_at_startup || (!check_at_startup && !autocheck))
441 condition = account->recv_at_getall;
443 if (condition) {
444 debug_print("INC: will check account %d\n", account->account_id);
445 list2 = g_list_append(list2, account);
449 /* Do the check on the collected accounts. */
450 if (list2 != NULL) {
451 if (autocheck || check_at_startup)
452 hide_dialog = TRUE;
453 inc_account_list_mail(mainwin, list2, hide_dialog, notify);
454 g_list_free(list2);
458 static void inc_progress_dialog_size_allocate_cb(GtkWidget *widget,
459 GtkAllocation *allocation)
461 cm_return_if_fail(allocation != NULL);
463 gtk_window_get_size(GTK_WINDOW(widget),
464 &prefs_common.receivewin_width, &prefs_common.receivewin_height);
467 static IncProgressDialog *inc_progress_dialog_create(gboolean autocheck)
469 IncProgressDialog *dialog;
470 ProgressDialog *progress;
471 static GdkGeometry geometry;
473 dialog = g_new0(IncProgressDialog, 1);
475 progress = progress_dialog_create();
476 gtk_window_set_title(GTK_WINDOW(progress->window),
477 _("Retrieving new messages"));
478 g_signal_connect(G_OBJECT(progress->showlog_btn), "clicked",
479 G_CALLBACK(inc_showlog_cb), dialog);
480 g_signal_connect(G_OBJECT(progress->cancel_btn), "clicked",
481 G_CALLBACK(inc_cancel_cb), dialog);
482 g_signal_connect(G_OBJECT(progress->window), "delete_event",
483 G_CALLBACK(inc_dialog_delete_cb), dialog);
484 g_signal_connect(G_OBJECT(progress->window), "size_allocate",
485 G_CALLBACK(inc_progress_dialog_size_allocate_cb), NULL);
486 /* manage_window_set_transient(GTK_WINDOW(progress->window)); */
488 progress_dialog_get_fraction(progress);
490 stock_pixbuf_gdk(STOCK_PIXMAP_COMPLETE, &okpix);
491 stock_pixbuf_gdk(STOCK_PIXMAP_CONTINUE, &currentpix);
492 stock_pixbuf_gdk(STOCK_PIXMAP_ERROR, &errorpix);
494 if (!geometry.min_height) {
495 geometry.min_width = 460;
496 geometry.min_height = 250;
499 gtk_window_set_geometry_hints(GTK_WINDOW(progress->window), NULL, &geometry,
500 GDK_HINT_MIN_SIZE);
501 gtk_widget_set_size_request(progress->window, prefs_common.receivewin_width,
502 prefs_common.receivewin_height);
504 if (prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS ||
505 (prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL &&
506 !autocheck)) {
507 dialog->show_dialog = TRUE;
508 gtk_widget_show_now(progress->window);
511 dialog->dialog = progress;
512 dialog->progress_tv = g_date_time_new_now_local();
513 dialog->folder_tv = g_date_time_new_now_local();
514 dialog->queue_list = NULL;
515 dialog->cur_row = 0;
517 inc_dialog_list = g_list_append(inc_dialog_list, dialog);
519 return dialog;
522 static void inc_progress_dialog_set_list(IncProgressDialog *inc_dialog)
524 GList *list;
526 for (list = inc_dialog->queue_list; list != NULL; list = list->next) {
527 IncSession *session = list->data;
528 Pop3Session *pop3_session = POP3_SESSION(session->session);
530 session->data = inc_dialog;
532 progress_dialog_list_set(inc_dialog->dialog,
533 -1, NULL,
534 pop3_session->ac_prefs->account_name,
535 _("Standby"));
539 static void inc_progress_dialog_clear(IncProgressDialog *inc_dialog)
541 progress_dialog_get_fraction(inc_dialog->dialog);
542 progress_dialog_set_label(inc_dialog->dialog, "");
543 if (inc_dialog->mainwin)
544 main_window_progress_off(inc_dialog->mainwin);
547 static void inc_progress_dialog_destroy(IncProgressDialog *inc_dialog)
549 cm_return_if_fail(inc_dialog != NULL);
551 inc_dialog_list = g_list_remove(inc_dialog_list, inc_dialog);
553 if (inc_dialog->mainwin)
554 main_window_progress_off(inc_dialog->mainwin);
555 progress_dialog_destroy(inc_dialog->dialog);
557 g_date_time_unref(inc_dialog->progress_tv);
558 g_date_time_unref(inc_dialog->folder_tv);
560 g_free(inc_dialog);
563 static IncSession *inc_session_new(PrefsAccount *account)
565 IncSession *session;
567 cm_return_val_if_fail(account != NULL, NULL);
569 if (account->protocol != A_POP3)
570 return NULL;
571 if (!account->recv_server || !account->userid)
572 return NULL;
574 session = g_new0(IncSession, 1);
576 session->session = pop3_session_new(account);
577 session->session->data = session;
578 POP3_SESSION(session->session)->drop_message = inc_drop_message;
579 session_set_recv_message_notify(session->session,
580 inc_recv_message, session);
581 session_set_recv_data_progressive_notify(session->session,
582 inc_recv_data_progressive,
583 session);
584 session_set_recv_data_notify(session->session,
585 inc_recv_data_finished, session);
587 return session;
590 static void inc_session_destroy(IncSession *session)
592 cm_return_if_fail(session != NULL);
594 session_destroy(session->session);
595 g_free(session);
598 static gint pop3_get_port(Pop3Session *pop3_session)
600 #ifdef USE_GNUTLS
601 return pop3_session->ac_prefs->set_popport ?
602 pop3_session->ac_prefs->popport :
603 pop3_session->ac_prefs->ssl_pop == SSL_TUNNEL ? 995 : 110;
604 #else
605 return pop3_session->ac_prefs->set_popport ?
606 pop3_session->ac_prefs->popport : 110;
607 #endif
610 static gint inc_start(IncProgressDialog *inc_dialog)
612 IncSession *session;
613 GList *qlist;
614 Pop3Session *pop3_session;
615 IncState inc_state;
616 gint error_num = 0;
617 gint new_msgs = 0;
618 gchar *msg;
619 gchar *fin_msg;
620 FolderItem *processing, *inbox;
621 GSList *msglist, *msglist_element;
622 gboolean cancelled = FALSE;
624 qlist = inc_dialog->queue_list;
625 while (qlist != NULL) {
626 GList *next = qlist->next;
628 session = qlist->data;
629 pop3_session = POP3_SESSION(session->session);
630 pop3_session->user = g_strdup(pop3_session->ac_prefs->userid);
632 if (inc_dialog->show_dialog)
633 manage_window_focus_in
634 (inc_dialog->dialog->window,
635 NULL, NULL);
637 if(pop3_session->ac_prefs->use_pop_auth &&
638 pop3_session->ac_prefs->pop_auth_type == POPAUTH_OAUTH2)
639 oauth2_check_passwds (pop3_session->ac_prefs);
641 if (password_get(pop3_session->user,
642 pop3_session->ac_prefs->recv_server,
643 "pop3", pop3_get_port(pop3_session),
644 &(pop3_session->pass))) {
645 /* NOP */;
646 } else if ((pop3_session->pass = passwd_store_get_account(
647 pop3_session->ac_prefs->account_id, PWS_ACCOUNT_RECV)) == NULL) {
648 gchar *pass;
650 pass = input_dialog_query_password_keep
651 (pop3_session->ac_prefs->recv_server,
652 pop3_session->user,
653 &(pop3_session->ac_prefs->session_passwd));
655 if (pass) {
656 pop3_session->pass = pass;
660 if (inc_dialog->show_dialog)
661 manage_window_focus_out
662 (inc_dialog->dialog->window,
663 NULL, NULL);
665 qlist = next;
668 #define SET_PIXMAP_AND_TEXT(pix, str) \
670 progress_dialog_list_set(inc_dialog->dialog, \
671 inc_dialog->cur_row, \
672 pix, \
673 NULL, \
674 str); \
677 for (; inc_dialog->queue_list != NULL && !cancelled; inc_dialog->cur_row++) {
678 session = inc_dialog->queue_list->data;
679 pop3_session = POP3_SESSION(session->session);
680 GSList *filtered, *unfiltered;
682 if (pop3_session->pass == NULL) {
683 SET_PIXMAP_AND_TEXT(okpix, _("Cancelled"));
684 inc_dialog->queue_list =
685 g_list_remove(inc_dialog->queue_list, session);
686 inc_session_destroy(session);
687 continue;
690 inc_progress_dialog_clear(inc_dialog);
691 progress_dialog_scroll_to_row(inc_dialog->dialog,
692 inc_dialog->cur_row);
694 SET_PIXMAP_AND_TEXT(currentpix, _("Retrieving"));
696 /* begin POP3 session */
697 inc_state = inc_pop3_session_do(session);
699 switch (inc_state) {
700 case INC_SUCCESS:
701 if (pop3_session->cur_total_num > 0)
702 msg = g_strdup_printf(
703 ngettext("Done (%d message (%s) received)",
704 "Done (%d messages (%s) received)",
705 pop3_session->cur_total_num),
706 pop3_session->cur_total_num,
707 to_human_readable((goffset)pop3_session->cur_total_recv_bytes));
708 else
709 msg = g_strdup_printf(_("Done (no new messages)"));
710 SET_PIXMAP_AND_TEXT(okpix, msg);
711 g_free(msg);
712 break;
713 case INC_CONNECT_ERROR:
714 SET_PIXMAP_AND_TEXT(errorpix, _("Connection failed"));
715 break;
716 case INC_AUTH_FAILED:
717 SET_PIXMAP_AND_TEXT(errorpix, _("Auth failed"));
718 if (pop3_session->ac_prefs->session_passwd) {
719 g_free(pop3_session->ac_prefs->session_passwd);
720 pop3_session->ac_prefs->session_passwd = NULL;
722 break;
723 case INC_LOCKED:
724 SET_PIXMAP_AND_TEXT(errorpix, _("Locked"));
725 break;
726 case INC_ERROR:
727 case INC_NO_SPACE:
728 case INC_IO_ERROR:
729 case INC_SOCKET_ERROR:
730 case INC_EOF:
731 SET_PIXMAP_AND_TEXT(errorpix, _("Error"));
732 break;
733 case INC_TIMEOUT:
734 SET_PIXMAP_AND_TEXT(errorpix, _("Timeout"));
735 break;
736 case INC_CANCEL:
737 SET_PIXMAP_AND_TEXT(okpix, _("Cancelled"));
738 if (!inc_dialog->show_dialog)
739 cancelled = TRUE;
740 break;
741 default:
742 break;
745 if (pop3_session->error_val == PS_AUTHFAIL) {
746 if(!prefs_common.no_recv_err_panel) {
747 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
748 ((prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL) && focus_window))
749 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
753 /* CLAWS: perform filtering actions on dropped message */
754 /* CLAWS: get default inbox (perhaps per account) */
755 if (pop3_session->ac_prefs->inbox) {
756 /* CLAWS: get destination folder / mailbox */
757 inbox = folder_find_item_from_identifier(pop3_session->ac_prefs->inbox);
758 if (!inbox)
759 inbox = folder_get_default_inbox();
760 } else
761 inbox = folder_get_default_inbox();
763 /* get list of messages in processing */
764 processing = folder_get_default_processing(pop3_session->ac_prefs->account_id);
765 folder_item_scan(processing);
766 msglist = folder_item_get_msg_list(processing);
768 /* process messages */
769 folder_item_update_freeze();
771 procmsg_msglist_filter(msglist, pop3_session->ac_prefs,
772 &filtered, &unfiltered,
773 pop3_session->ac_prefs->filter_on_recv);
775 filtering_move_and_copy_msgs(msglist);
776 if (unfiltered != NULL)
777 folder_item_move_msgs(inbox, unfiltered);
779 for(msglist_element = msglist; msglist_element != NULL;
780 msglist_element = msglist_element->next) {
781 procmsg_msginfo_free((MsgInfo**)&(msglist_element->data));
783 folder_item_update_thaw();
785 g_slist_free(msglist);
786 g_slist_free(filtered);
787 g_slist_free(unfiltered);
789 statusbar_pop_all();
791 new_msgs += pop3_session->cur_total_num;
793 pop3_write_uidl_list(pop3_session);
795 if (inc_state != INC_SUCCESS && inc_state != INC_CANCEL) {
796 error_num++;
797 if (inc_dialog->show_dialog)
798 manage_window_focus_in
799 (inc_dialog->dialog->window,
800 NULL, NULL);
801 inc_put_error(inc_state, pop3_session);
802 if (inc_dialog->show_dialog)
803 manage_window_focus_out
804 (inc_dialog->dialog->window,
805 NULL, NULL);
806 if (inc_state == INC_NO_SPACE ||
807 inc_state == INC_IO_ERROR)
808 break;
810 folder_item_free_cache(processing, TRUE);
812 inc_session_destroy(session);
813 inc_dialog->queue_list =
814 g_list_remove(inc_dialog->queue_list, session);
817 #undef SET_PIXMAP_AND_TEXT
819 if (new_msgs > 0)
820 fin_msg = g_strdup_printf(ngettext("Finished (%d new message)",
821 "Finished (%d new messages)",
822 new_msgs), new_msgs);
823 else
824 fin_msg = g_strdup_printf(_("Finished (no new messages)"));
826 progress_dialog_set_label(inc_dialog->dialog, fin_msg);
828 while (inc_dialog->queue_list != NULL) {
829 session = inc_dialog->queue_list->data;
830 inc_session_destroy(session);
831 inc_dialog->queue_list =
832 g_list_remove(inc_dialog->queue_list, session);
835 if (prefs_common.close_recv_dialog || !inc_dialog->show_dialog)
836 inc_progress_dialog_destroy(inc_dialog);
837 else {
838 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window),
839 fin_msg);
840 gtk_button_set_label(GTK_BUTTON(inc_dialog->dialog->cancel_btn),
841 _("_Close"));
842 gtk_button_set_image(GTK_BUTTON(inc_dialog->dialog->cancel_btn),
843 gtk_image_new_from_icon_name("window-close", GTK_ICON_SIZE_BUTTON));
846 g_free(fin_msg);
848 return new_msgs;
851 static IncState inc_pop3_session_do(IncSession *session)
853 Pop3Session *pop3_session = POP3_SESSION(session->session);
854 IncProgressDialog *inc_dialog = (IncProgressDialog *)session->data;
855 PrefsAccount *ac = pop3_session->ac_prefs;
856 gchar *server;
857 gchar *account_name;
858 gushort port;
859 gchar *buf;
860 ProxyInfo *proxy_info = NULL;
862 debug_print("getting new messages of account %s...\n",
863 ac->account_name);
865 ac->last_pop_login_time = time(NULL);
867 buf = g_strdup_printf(_("%s: Retrieving new messages"),
868 ac->recv_server);
869 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window), buf);
870 g_free(buf);
872 server = ac->recv_server;
873 account_name = ac->account_name;
874 port = pop3_get_port(pop3_session);
876 #ifdef USE_GNUTLS
877 SESSION(pop3_session)->ssl_type = ac->ssl_pop;
878 if (ac->ssl_pop != SSL_NONE)
879 SESSION(pop3_session)->nonblocking =
880 ac->use_nonblocking_ssl;
881 #else
882 if (ac->ssl_pop != SSL_NONE) {
883 if (alertpanel_full(_("Insecure connection"),
884 _("This connection is configured to be secured "
885 "using TLS, but TLS is not available "
886 "in this build of Claws Mail. \n\n"
887 "Do you want to continue connecting to this "
888 "server? The communication would not be "
889 "secure."),
890 NULL, _("_Cancel"), NULL, _("Con_tinue connecting"), NULL, NULL,
891 ALERTFOCUS_FIRST, FALSE, NULL, ALERT_WARNING) != G_ALERTALTERNATE)
892 return INC_CANCEL;
894 #endif
896 buf = g_strdup_printf(_("Account '%s': Connecting to POP3 server: %s:%d..."),
897 account_name, server, port);
898 statusbar_print_all("%s", buf);
899 log_message(LOG_PROTOCOL, "%s\n", buf);
901 progress_dialog_set_label(inc_dialog->dialog, buf);
903 if (ac->use_proxy) {
904 if (ac->use_default_proxy) {
905 proxy_info = (ProxyInfo *)&(prefs_common.proxy_info);
906 if (proxy_info->use_proxy_auth)
907 proxy_info->proxy_pass = passwd_store_get(PWS_CORE, PWS_CORE_PROXY,
908 PWS_CORE_PROXY_PASS);
909 } else {
910 proxy_info = (ProxyInfo *)&(ac->proxy_info);
911 if (proxy_info->use_proxy_auth)
912 proxy_info->proxy_pass = passwd_store_get_account(ac->account_id,
913 PWS_ACCOUNT_PROXY_PASS);
916 SESSION(pop3_session)->proxy_info = proxy_info;
918 GTK_EVENTS_FLUSH();
919 g_free(buf);
921 session_set_timeout(SESSION(pop3_session),
922 prefs_common.io_timeout_secs * 1000);
924 if (session_connect(SESSION(pop3_session), server, port) < 0) {
925 if(!prefs_common.no_recv_err_panel) {
926 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
927 ((prefs_common.recv_dialog_mode == RECV_DIALOG_MANUAL) && focus_window)) {
928 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
930 alertpanel_error(_("Can't connect to POP3 server: %s:%d"),
931 server, port);
932 manage_window_focus_out(inc_dialog->dialog->window, NULL, NULL);
933 } else {
934 log_error(LOG_PROTOCOL, _("Can't connect to POP3 server: %s:%d\n"),
935 server, port);
937 session->inc_state = INC_CONNECT_ERROR;
938 statusbar_pop_all();
939 return INC_CONNECT_ERROR;
942 while (session_is_running(SESSION(pop3_session)) &&
943 session->inc_state != INC_CANCEL)
944 gtk_main_iteration();
946 if (session->inc_state == INC_SUCCESS) {
947 switch (pop3_session->error_val) {
948 case PS_SUCCESS:
949 switch (SESSION(pop3_session)->state) {
950 case SESSION_ERROR:
951 if (pop3_session->state == POP3_READY)
952 session->inc_state = INC_CONNECT_ERROR;
953 else
954 session->inc_state = INC_ERROR;
955 break;
956 case SESSION_EOF:
957 session->inc_state = INC_EOF;
958 break;
959 case SESSION_TIMEOUT:
960 session->inc_state = INC_TIMEOUT;
961 break;
962 default:
963 session->inc_state = INC_SUCCESS;
964 break;
966 break;
967 case PS_AUTHFAIL:
968 session->inc_state = INC_AUTH_FAILED;
969 break;
970 case PS_IOERR:
971 session->inc_state = INC_IO_ERROR;
972 break;
973 case PS_SOCKET:
974 session->inc_state = INC_SOCKET_ERROR;
975 break;
976 case PS_LOCKBUSY:
977 session->inc_state = INC_LOCKED;
978 break;
979 default:
980 session->inc_state = INC_ERROR;
981 break;
985 session_disconnect(SESSION(pop3_session));
986 statusbar_pop_all();
988 return session->inc_state;
991 static void inc_progress_dialog_update(IncProgressDialog *inc_dialog,
992 IncSession *inc_session)
994 inc_progress_dialog_set_label(inc_dialog, inc_session);
995 inc_progress_dialog_set_progress(inc_dialog, inc_session);
998 static void inc_progress_dialog_set_label(IncProgressDialog *inc_dialog,
999 IncSession *inc_session)
1001 ProgressDialog *dialog = inc_dialog->dialog;
1002 Pop3Session *session;
1004 cm_return_if_fail(inc_session != NULL);
1006 session = POP3_SESSION(inc_session->session);
1008 switch (session->state) {
1009 case POP3_GREETING:
1010 break;
1011 case POP3_GETAUTH_USER:
1012 case POP3_GETAUTH_PASS:
1013 case POP3_GETAUTH_APOP:
1014 progress_dialog_set_label(dialog, _("Authenticating..."));
1015 statusbar_pop_all();
1016 statusbar_print_all(_("Retrieving messages from %s (%s)..."),
1017 SESSION(session)->server,
1018 session->ac_prefs->account_name);
1019 break;
1020 case POP3_GETRANGE_STAT:
1021 progress_dialog_set_label
1022 (dialog, _("Getting the number of new messages (STAT)..."));
1023 break;
1024 case POP3_GETRANGE_LAST:
1025 progress_dialog_set_label
1026 (dialog, _("Getting the number of new messages (LAST)..."));
1027 break;
1028 case POP3_GETRANGE_UIDL:
1029 progress_dialog_set_label
1030 (dialog, _("Getting the number of new messages (UIDL)..."));
1031 break;
1032 case POP3_GETSIZE_LIST:
1033 progress_dialog_set_label
1034 (dialog, _("Getting the size of messages (LIST)..."));
1035 break;
1036 case POP3_RETR:
1037 case POP3_RETR_RECV:
1038 case POP3_DELETE:
1039 break;
1040 case POP3_LOGOUT:
1041 progress_dialog_set_label(dialog, _("Quitting"));
1042 break;
1043 default:
1044 break;
1048 static void inc_progress_dialog_set_progress(IncProgressDialog *inc_dialog,
1049 IncSession *inc_session)
1051 gchar buf[MESSAGEBUFSIZE];
1052 Pop3Session *pop3_session = POP3_SESSION(inc_session->session);
1053 gchar *total_size_str;
1054 gint cur_total;
1055 gint total;
1057 if (!pop3_session->new_msg_exist) return;
1059 cur_total = inc_session->cur_total_bytes;
1060 total = pop3_session->total_bytes;
1061 if (pop3_session->state == POP3_RETR ||
1062 pop3_session->state == POP3_RETR_RECV ||
1063 pop3_session->state == POP3_DELETE) {
1064 Xstrdup_a(total_size_str, to_human_readable((goffset)total), return);
1065 g_snprintf(buf, sizeof(buf),
1066 _("Retrieving message (%d / %d) (%s / %s)"),
1067 pop3_session->cur_msg, pop3_session->count,
1068 to_human_readable((goffset)cur_total), total_size_str);
1069 progress_dialog_set_label(inc_dialog->dialog, buf);
1072 progress_dialog_set_fraction
1073 (inc_dialog->dialog, (total == 0) ? 0: (gfloat)cur_total / (gfloat)total);
1075 statusbar_progress_all(pop3_session->cur_msg, pop3_session->count, 1);
1077 if (pop3_session->cur_total_num > 0) {
1078 g_snprintf(buf, sizeof(buf),
1079 ngettext("Retrieving (%d message (%s) received)",
1080 "Retrieving (%d messages (%s) received)",
1081 pop3_session->cur_total_num),
1082 pop3_session->cur_total_num,
1083 to_human_readable
1084 ((goffset)pop3_session->cur_total_recv_bytes));
1085 progress_dialog_list_set_status(inc_dialog->dialog,
1086 inc_dialog->cur_row,
1087 buf);
1091 static void inc_progress_dialog_update_periodic(IncProgressDialog *inc_dialog,
1092 IncSession *inc_session)
1094 GDateTime *tv_cur = g_date_time_new_now_local();
1095 GTimeSpan tv_result;
1097 tv_result = g_date_time_difference(tv_cur, inc_dialog->progress_tv);
1098 g_date_time_unref(tv_cur);
1099 if (tv_result < 0) {
1100 tv_result += G_USEC_PER_SEC;
1103 if (tv_result > PROGRESS_UPDATE_INTERVAL) {
1104 inc_progress_dialog_update(inc_dialog, inc_session);
1105 tv_cur = g_date_time_add(inc_dialog->progress_tv, tv_result);
1106 g_date_time_unref(inc_dialog->progress_tv);
1107 inc_dialog->progress_tv = tv_cur;
1111 static gint inc_recv_data_progressive(Session *session, guint cur_len,
1112 guint total_len, gpointer data)
1114 IncSession *inc_session = (IncSession *)data;
1115 Pop3Session *pop3_session = POP3_SESSION(session);
1116 IncProgressDialog *inc_dialog;
1117 gint cur_total;
1119 cm_return_val_if_fail(inc_session != NULL, -1);
1121 if (pop3_session->state != POP3_RETR &&
1122 pop3_session->state != POP3_RETR_RECV &&
1123 pop3_session->state != POP3_DELETE &&
1124 pop3_session->state != POP3_LOGOUT) return 0;
1126 if (!pop3_session->new_msg_exist) return 0;
1128 cur_total = pop3_session->cur_total_bytes + cur_len;
1129 if (cur_total > pop3_session->total_bytes)
1130 cur_total = pop3_session->total_bytes;
1131 inc_session->cur_total_bytes = cur_total;
1133 inc_dialog = (IncProgressDialog *)inc_session->data;
1134 inc_progress_dialog_update_periodic(inc_dialog, inc_session);
1136 return 0;
1139 static gint inc_recv_data_finished(Session *session, guint len, gpointer data)
1141 IncSession *inc_session = (IncSession *)data;
1142 IncProgressDialog *inc_dialog;
1144 cm_return_val_if_fail(inc_session != NULL, -1);
1146 inc_dialog = (IncProgressDialog *)inc_session->data;
1148 inc_recv_data_progressive(session, 0, 0, inc_session);
1150 if (POP3_SESSION(session)->state == POP3_LOGOUT) {
1151 inc_progress_dialog_update(inc_dialog, inc_session);
1154 return 0;
1157 static gint inc_recv_message(Session *session, const gchar *msg, gpointer data)
1159 IncSession *inc_session = (IncSession *)data;
1160 IncProgressDialog *inc_dialog;
1162 cm_return_val_if_fail(inc_session != NULL, -1);
1164 inc_dialog = (IncProgressDialog *)inc_session->data;
1166 switch (POP3_SESSION(session)->state) {
1167 case POP3_GETAUTH_USER:
1168 case POP3_GETAUTH_PASS:
1169 case POP3_GETAUTH_APOP:
1170 case POP3_GETRANGE_STAT:
1171 case POP3_GETRANGE_LAST:
1172 case POP3_GETRANGE_UIDL:
1173 case POP3_GETSIZE_LIST:
1174 inc_progress_dialog_update(inc_dialog, inc_session);
1175 break;
1176 case POP3_RETR:
1177 inc_recv_data_progressive(session, 0, 0, inc_session);
1178 break;
1179 case POP3_LOGOUT:
1180 inc_progress_dialog_update(inc_dialog, inc_session);
1181 break;
1182 default:
1183 break;
1186 return 0;
1189 static gint inc_drop_message(Pop3Session *session, const gchar *file)
1191 FolderItem *inbox;
1192 FolderItem *dropfolder;
1193 IncSession *inc_session = (IncSession *)(SESSION(session)->data);
1194 gint msgnum;
1196 cm_return_val_if_fail(inc_session != NULL, -1);
1198 if (session->ac_prefs->inbox) {
1199 inbox = folder_find_item_from_identifier
1200 (session->ac_prefs->inbox);
1201 if (!inbox)
1202 inbox = folder_get_default_inbox();
1203 } else
1204 inbox = folder_get_default_inbox();
1205 if (!inbox) {
1206 claws_unlink(file);
1207 return -1;
1210 /* CLAWS: claws uses a global .processing folder for the filtering. */
1211 dropfolder = folder_get_default_processing(session->ac_prefs->account_id);
1213 /* add msg file to drop folder */
1214 if ((msgnum = folder_item_add_msg(
1215 dropfolder, file, NULL, TRUE)) < 0) {
1216 claws_unlink(file);
1217 return -1;
1220 return 0;
1223 static void inc_put_error(IncState istate, Pop3Session *session)
1225 gchar *log_msg = NULL;
1226 gchar *err_msg = NULL;
1227 gboolean fatal_error = FALSE;
1229 switch (istate) {
1230 case INC_CONNECT_ERROR:
1231 fatal_error = TRUE;
1232 if (prefs_common.no_recv_err_panel)
1233 break;
1234 err_msg = g_strdup_printf(_("Connection to %s:%d failed."),
1235 SESSION(session)->server,
1236 SESSION(session)->port);
1237 break;
1238 case INC_ERROR:
1239 log_msg = _("Error occurred while processing mail.");
1240 fatal_error = TRUE;
1241 if (prefs_common.no_recv_err_panel)
1242 break;
1243 if (session->error_msg)
1244 err_msg = g_strdup_printf
1245 (_("Error occurred while processing mail:\n%s"),
1246 session->error_msg);
1247 else
1248 err_msg = g_strdup(log_msg);
1249 break;
1250 case INC_NO_SPACE:
1251 log_msg = _("No disk space left.");
1252 err_msg = g_strdup(log_msg);
1253 fatal_error = TRUE;
1254 break;
1255 case INC_IO_ERROR:
1256 log_msg = _("Can't write file.");
1257 err_msg = g_strdup(log_msg);
1258 fatal_error = TRUE;
1259 break;
1260 case INC_SOCKET_ERROR:
1261 log_msg = _("Socket error.");
1262 if (prefs_common.no_recv_err_panel)
1263 break;
1264 err_msg = g_strdup_printf(_("Socket error on connection to %s:%d."),
1265 SESSION(session)->server,
1266 SESSION(session)->port);
1267 break;
1268 case INC_EOF:
1269 log_msg = _("Connection closed by the remote host.");
1270 if (prefs_common.no_recv_err_panel)
1271 break;
1272 err_msg = g_strdup_printf(_("Connection to %s:%d closed by the remote host."),
1273 SESSION(session)->server,
1274 SESSION(session)->port);
1275 break;
1276 case INC_LOCKED:
1277 log_msg = _("Mailbox is locked.");
1278 if (prefs_common.no_recv_err_panel)
1279 break;
1280 if (session->error_msg)
1281 err_msg = g_strdup_printf(_("Mailbox is locked:\n%s"),
1282 session->error_msg);
1283 else
1284 err_msg = g_strdup(log_msg);
1285 break;
1286 case INC_AUTH_FAILED:
1287 log_msg = _("Authentication failed.");
1288 fatal_error = TRUE;
1289 if (prefs_common.no_recv_err_panel)
1290 break;
1291 if (session->error_msg)
1292 err_msg = g_strdup_printf
1293 (_("Authentication failed:\n%s"), session->error_msg);
1294 else
1295 err_msg = g_strdup(log_msg);
1296 break;
1297 case INC_TIMEOUT:
1298 log_msg = _("Session timed out. You may be able to "
1299 "recover by increasing the timeout value in "
1300 "Preferences/Other/Miscellaneous.");
1301 if (prefs_common.no_recv_err_panel)
1302 break;
1303 err_msg = g_strdup_printf(_("Connection to %s:%d timed out."),
1304 SESSION(session)->server,
1305 SESSION(session)->port);
1306 break;
1307 default:
1308 break;
1311 if (log_msg) {
1312 if (fatal_error)
1313 log_error(LOG_PROTOCOL, "%s\n", log_msg);
1314 else
1315 log_warning(LOG_PROTOCOL, "%s\n", log_msg);
1317 if (prefs_common.no_recv_err_panel && fatal_error)
1318 mainwindow_show_error();
1320 if (err_msg) {
1321 alertpanel_error_log("%s", err_msg);
1322 g_free(err_msg);
1326 static void inc_cancel(IncProgressDialog *dialog)
1328 IncSession *session;
1330 cm_return_if_fail(dialog != NULL);
1332 if (dialog->queue_list == NULL) {
1333 inc_progress_dialog_destroy(dialog);
1334 return;
1337 session = dialog->queue_list->data;
1339 session->inc_state = INC_CANCEL;
1341 log_message(LOG_PROTOCOL, _("Incorporation cancelled\n"));
1344 gboolean inc_is_active(void)
1346 return (inc_dialog_list != NULL);
1349 void inc_cancel_all(void)
1351 GList *cur;
1353 for (cur = inc_dialog_list; cur != NULL; cur = cur->next)
1354 inc_cancel((IncProgressDialog *)cur->data);
1357 static void inc_showlog_cb(GtkWidget *widget, gpointer data)
1359 MainWindow *mainwin = mainwindow_get_mainwindow();
1361 log_window_show(mainwin->logwin);
1364 static void inc_cancel_cb(GtkWidget *widget, gpointer data)
1366 inc_cancel((IncProgressDialog *)data);
1369 static gint inc_dialog_delete_cb(GtkWidget *widget, GdkEventAny *event,
1370 gpointer data)
1372 IncProgressDialog *dialog = (IncProgressDialog *)data;
1374 if (dialog->queue_list == NULL)
1375 inc_progress_dialog_destroy(dialog);
1377 return TRUE;
1380 static gint inc_spool_account(PrefsAccount *account)
1382 FolderItem *inbox;
1383 gchar *mbox;
1384 gint result;
1386 if (account->local_inbox) {
1387 inbox = folder_find_item_from_identifier(account->local_inbox);
1388 if (!inbox)
1389 inbox = folder_get_default_inbox();
1390 } else
1391 inbox = folder_get_default_inbox();
1393 if (account->local_mbox) {
1394 if (is_file_exist(account->local_mbox))
1395 mbox = g_strdup(account->local_mbox);
1396 else if (is_dir_exist(account->local_mbox))
1397 mbox = g_strconcat(account->local_mbox, G_DIR_SEPARATOR_S,
1398 g_get_user_name(), NULL);
1399 else {
1400 debug_print("%s: local mailbox not found.\n",
1401 account->local_mbox);
1402 return -1;
1404 } else {
1405 debug_print("local mailbox not set in account info.\n");
1406 return -1;
1409 result = get_spool(inbox, mbox, account);
1410 g_free(mbox);
1412 statusbar_pop_all();
1414 return result;
1417 static gint get_spool(FolderItem *dest, const gchar *mbox, PrefsAccount *account)
1419 gint msgs, size;
1420 gint lockfd;
1421 gchar tmp_mbox[MAXPATHLEN + 1];
1423 cm_return_val_if_fail(dest != NULL, -1);
1424 cm_return_val_if_fail(mbox != NULL, -1);
1425 cm_return_val_if_fail(account != NULL, -1);
1427 if (!is_file_exist(mbox) || (size = get_file_size(mbox)) == 0) {
1428 debug_print("%s: no messages in local mailbox.\n", mbox);
1429 return 0;
1430 } else if (size < 0)
1431 return -1;
1433 if ((lockfd = lock_mbox(mbox, LOCK_FLOCK)) < 0)
1434 return -1;
1436 g_snprintf(tmp_mbox, sizeof(tmp_mbox), "%s%ctmpmbox.%p",
1437 get_tmp_dir(), G_DIR_SEPARATOR, mbox);
1439 if (copy_mbox(lockfd, tmp_mbox) < 0) {
1440 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1441 return -1;
1444 debug_print("Getting new messages from %s into %s...\n",
1445 mbox, dest->path);
1447 msgs = proc_mbox(dest, tmp_mbox, account->filter_on_recv, account);
1449 claws_unlink(tmp_mbox);
1450 if (msgs >= 0) empty_mbox(mbox);
1451 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1453 return msgs;
1456 void inc_lock_real(void)
1458 inc_lock_count++;
1461 void inc_unlock_real(void)
1463 if (inc_lock_count > 0)
1464 inc_lock_count--;
1467 static guint autocheck_timer = 0;
1468 static gpointer autocheck_data = NULL;
1470 static void inc_notify_cmd(gint new_msgs, gboolean notify)
1472 gchar *buf, *numpos, *ret_str;
1473 gssize by_read = 0, by_written = 0;
1475 if (!(new_msgs && notify && prefs_common.newmail_notify_cmd &&
1476 *prefs_common.newmail_notify_cmd))
1477 return;
1479 buf = g_strdup(prefs_common.newmail_notify_cmd);
1480 if ((numpos = strstr(buf, "%d")) != NULL) {
1481 gchar *buf2;
1483 *numpos = '\0';
1484 buf2 = g_strdup_printf("%s%d%s", buf, new_msgs, numpos + 2);
1485 g_free(buf);
1486 buf = buf2;
1489 ret_str = g_locale_from_utf8(buf, strlen(buf), &by_read, &by_written,
1490 NULL);
1491 if (ret_str) {
1492 if (by_written) {
1493 g_free(buf);
1494 buf = ret_str;
1495 } else
1496 g_free(ret_str);
1498 debug_print("executing new mail notification command: %s\n", buf);
1499 execute_command_line(buf, TRUE, NULL);
1501 g_free(buf);
1504 void inc_autocheck_timer_init(MainWindow *mainwin)
1506 autocheck_data = mainwin;
1507 inc_autocheck_timer_set();
1510 static void inc_autocheck_timer_set_interval(guint _interval)
1512 guint interval = _interval;
1514 /* Convert the interval to seconds if needed. */
1515 if (_interval % 1000 == 0)
1516 interval /= 1000;
1518 inc_autocheck_timer_remove();
1519 /* last test is to avoid re-enabling auto_check after modifying
1520 the common preferences */
1521 if (prefs_common.autochk_newmail && autocheck_data
1522 && prefs_common.work_offline == FALSE) {
1523 autocheck_timer =
1524 g_timeout_add_seconds(interval, inc_autocheck_func, autocheck_data);
1525 debug_print("added global inc timer %d at %u seconds\n",
1526 autocheck_timer, interval);
1530 void inc_autocheck_timer_set(void)
1532 inc_autocheck_timer_set_interval(prefs_common.autochk_itv * 1000);
1535 void inc_autocheck_timer_remove(void)
1537 if (autocheck_timer) {
1538 debug_print("removed global inc timer %d\n", autocheck_timer);
1539 g_source_remove(autocheck_timer);
1540 autocheck_timer = 0;
1544 static gint inc_autocheck_func(gpointer data)
1546 MainWindow *mainwin = (MainWindow *)data;
1548 if (inc_lock_count) {
1549 debug_print("global inc: autocheck is locked.\n");
1550 inc_autocheck_timer_set_interval(1000);
1551 return FALSE;
1554 inc_all_account_mail(mainwin, TRUE, FALSE, prefs_common.newmail_notify_auto);
1555 inc_autocheck_timer_set();
1557 return FALSE;
1560 static gboolean inc_account_autocheck_func(gpointer data)
1562 PrefsAccount *account = (PrefsAccount *)data;
1563 GList *list = NULL;
1565 cm_return_val_if_fail(account != NULL, FALSE);
1567 debug_print("account %d: inc_account_autocheck_func\n",
1568 account->account_id);
1570 list = g_list_append(list, account);
1571 inc_account_list_mail(mainwindow_get_mainwindow(),
1572 list, TRUE, prefs_common.newmail_notify_auto);
1573 g_list_free(list);
1575 inc_account_autocheck_timer_set_interval(account);
1577 return FALSE;
1580 void inc_account_autocheck_timer_remove(PrefsAccount *account)
1582 cm_return_if_fail(account != NULL);
1584 if (account->autocheck_timer != 0) {
1585 g_source_remove(account->autocheck_timer);
1586 debug_print("INC: account %d: removed inc timer %d\n", account->account_id,
1587 account->autocheck_timer);
1588 account->autocheck_timer = 0;
1592 void inc_account_autocheck_timer_set_interval(PrefsAccount *account)
1594 cm_return_if_fail(account != NULL);
1596 inc_account_autocheck_timer_remove(account);
1598 if (account->autochk_use_default
1599 || !account->autochk_use_custom
1600 || account->autochk_itv == 0)
1601 return;
1603 account->autocheck_timer = g_timeout_add_seconds(
1604 account->autochk_itv, inc_account_autocheck_func, account);
1605 debug_print("INC: account %d: added inc timer %d at %u seconds\n",
1606 account->account_id, account->autocheck_timer, account->autochk_itv);
1609 gboolean inc_offline_should_override(gboolean force_ask, const gchar *msg)
1611 gint length = 10; /* seconds */
1612 gint answer = G_ALERTDEFAULT;
1614 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1615 /* If no network connection is available, override is not possible */
1616 if(!networkmanager_is_online(NULL))
1617 return FALSE;
1618 #endif
1620 if (prefs_common.autochk_newmail)
1621 length = prefs_common.autochk_itv; /* seconds */
1623 if (force_ask) {
1624 inc_offline_overridden_no = (time_t)0;
1627 if (prefs_common.work_offline) {
1628 gchar *tmp = NULL;
1630 if (time(NULL) - inc_offline_overridden_yes < length * 60) /* seconds */
1631 return TRUE;
1632 else if (time(NULL) - inc_offline_overridden_no < length * 60) /* seconds */
1633 return FALSE;
1635 if (!force_ask) {
1636 gchar *unit = _("seconds");
1638 /* show the offline override time (length) using the must appropriate unit:
1639 the biggest unit possible (hours, minutes, seconds), provided that there
1640 is not inferior unit involved: 1 hour, 150 minutes, 25 minutes, 90 minutes,
1641 30 seconds, 90 seconds. */
1642 if ((length / 3600) > 0) { /* hours? */
1643 if (((length % 3600) % 60) == 0) { /* no seconds left? */
1644 if ((length % 3600) > 0) { /* minutes left? */
1645 length = length / 60;
1646 unit = ngettext("minute", "minutes", length);
1647 } else {
1648 length = length / 3600;
1649 unit = ngettext("hour", "hours", length);
1651 } /* else: seconds */
1652 } else {
1653 if ((length / 60) > 0) { /* minutes left? */
1654 if ((length % 60) == 0) {
1655 length = length / 60;
1656 unit = ngettext("minute", "minutes", length);
1658 } /* else: seconds */
1660 tmp = g_strdup_printf(
1661 _("%s%sYou're working offline. Override for %d %s?"),
1662 msg?msg:"",
1663 msg?"\n\n":"",
1664 length, unit);
1665 } else
1666 tmp = g_strdup_printf(
1667 _("%s%sYou're working offline. Override?"),
1668 msg?msg:"",
1669 msg?"\n\n":"");
1671 answer = alertpanel(_("Offline warning"),
1672 tmp,
1673 NULL, _("_No"), NULL, _("_Yes"),
1674 NULL, !force_ask? _("On_ly once"):NULL, ALERTFOCUS_SECOND);
1675 g_free(tmp);
1676 if (answer == G_ALERTALTERNATE) {
1677 inc_offline_overridden_yes = time(NULL);
1678 return TRUE;
1679 } else if (answer == G_ALERTDEFAULT) {
1680 if (!force_ask)
1681 inc_offline_overridden_no = time(NULL);
1682 return FALSE;
1683 } else {
1684 inc_reset_offline_override_timers();
1685 return TRUE;
1688 return TRUE;
1691 void inc_reset_offline_override_timers()
1693 debug_print("resetting offline override timers\n");
1694 inc_offline_overridden_yes = (time_t)0;
1695 inc_offline_overridden_no = (time_t)0;