2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2002 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 2 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, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 #include <gtk/gtkmain.h>
28 #include <gtk/gtkwindow.h>
29 #include <gtk/gtksignal.h>
30 #include <gtk/gtkprogressbar.h>
35 #include <sys/types.h>
44 #include "mainwindow.h"
45 #include "folderview.h"
46 #include "summaryview.h"
47 #include "prefs_common.h"
48 #include "prefs_account.h"
58 #include "statusbar.h"
59 #include "manage_window.h"
60 #include "stock_pixmap.h"
61 #include "progressdialog.h"
62 #include "inputdialog.h"
63 #include "alertpanel.h"
65 #include "automaton.h"
67 #include "filtering.h"
68 #include "selective_download.h"
70 static GList *inc_dialog_list = NULL;
72 static guint inc_lock_count = 0;
74 static GdkPixmap *currentxpm;
75 static GdkBitmap *currentxpmmask;
76 static GdkPixmap *errorxpm;
77 static GdkBitmap *errorxpmmask;
78 static GdkPixmap *okxpm;
79 static GdkBitmap *okxpmmask;
81 #define MSGBUFSIZE 8192
83 static void inc_finished (MainWindow *mainwin,
84 gboolean new_messages);
85 static gint inc_account_mail (PrefsAccount *account,
88 static IncProgressDialog *inc_progress_dialog_create (void);
89 static void inc_progress_dialog_destroy (IncProgressDialog *inc_dialog);
91 static IncSession *inc_session_new (PrefsAccount *account);
92 static void inc_session_destroy (IncSession *session);
93 static Pop3State *inc_pop3_state_new (PrefsAccount *account);
94 static void inc_pop3_state_destroy (Pop3State *state);
95 static gint inc_start (IncProgressDialog *inc_dialog);
96 static IncState inc_pop3_session_do (IncSession *session);
97 static gint pop3_automaton_terminate (SockInfo *source,
100 static GHashTable *inc_get_uidl_table (PrefsAccount *ac_prefs);
101 static void inc_write_uidl_list (Pop3State *state);
103 static gboolean inc_pop3_recv_func (SockInfo *sock,
108 static void inc_put_error (IncState istate);
110 static void inc_cancel_cb (GtkWidget *widget,
113 static gint inc_spool (void);
114 static gint get_spool (FolderItem *dest,
117 static void inc_spool_account(PrefsAccount *account);
118 static void inc_all_spool(void);
119 static void inc_autocheck_timer_set_interval (guint interval);
120 static gint inc_autocheck_func (gpointer data);
122 static void inc_notify_cmd (gint new_msgs,
125 #define FOLDER_SUMMARY_MISMATCH(f, s) \
126 (f) && (s) ? ((s)->newmsgs != (f)->new) || ((f)->unread != (s)->unread) || ((f)->total != (s)->messages) \
131 * @mainwin: Main window.
132 * @new_messages: TRUE if some messages have been received.
134 * Update the folder view and the summary view after receiving
135 * messages. If @new_messages is FALSE, this function avoids unneeded
138 static void inc_finished(MainWindow *mainwin, gboolean new_messages)
142 if (prefs_common.scan_all_after_inc)
143 folderview_check_new(NULL);
145 if (!new_messages && !prefs_common.scan_all_after_inc) return;
147 if (prefs_common.open_inbox_on_inc) {
148 item = cur_account && cur_account->inbox
149 ? folder_find_item_from_identifier(cur_account->inbox)
150 : folder_get_default_inbox();
151 if (FOLDER_SUMMARY_MISMATCH(item, mainwin->summaryview)) {
152 folderview_unselect(mainwin->folderview);
153 folderview_select(mainwin->folderview, item);
156 item = mainwin->summaryview->folder_item;
157 if (FOLDER_SUMMARY_MISMATCH(item, mainwin->summaryview)) {
158 folderview_unselect(mainwin->folderview);
159 folderview_select(mainwin->folderview, item);
164 void inc_mail(MainWindow *mainwin, gboolean notify)
168 if (inc_lock_count) return;
170 if (prefs_common.work_offline)
171 if (alertpanel(_("Offline warning"),
172 _("You're working offline. Override?"),
173 _("Yes"), _("No"), NULL) != G_ALERTDEFAULT)
176 inc_autocheck_timer_remove();
177 main_window_lock(mainwin);
179 if (prefs_common.use_extinc && prefs_common.extinc_cmd) {
180 /* external incorporating program */
181 if (execute_command_line(prefs_common.extinc_cmd, FALSE) < 0) {
182 main_window_unlock(mainwin);
183 inc_autocheck_timer_set();
187 if (prefs_common.inc_local)
188 new_msgs = inc_spool();
190 if (prefs_common.inc_local)
191 new_msgs = inc_spool();
193 new_msgs += inc_account_mail(cur_account, mainwin);
196 inc_finished(mainwin, new_msgs > 0);
197 main_window_unlock(mainwin);
198 inc_notify_cmd(new_msgs, notify);
199 inc_autocheck_timer_set();
202 void inc_selective_download(MainWindow *mainwin, PrefsAccount *acc, gint session)
207 acc->session = session;
208 inc_account_mail(acc, mainwin);
209 acc->session = STYPE_NORMAL;
211 for (cur = acc->msg_list; cur != NULL; cur = cur->next) {
212 HeaderItems *items =(HeaderItems*)cur->data;
214 if (items->state == SD_DOWNLOADED &&
215 items->del_by_old_session == FALSE) {
221 inc_finished(mainwin, TRUE);
222 inc_notify_cmd(new_msgs, prefs_common.newmail_notify_manu);
226 static gint inc_account_mail(PrefsAccount *account, MainWindow *mainwin)
228 IncProgressDialog *inc_dialog;
232 switch (account->protocol) {
235 folderview_check_new(FOLDER(account->folder));
240 session = inc_session_new(account);
241 if (!session) return 0;
243 inc_dialog = inc_progress_dialog_create();
244 inc_dialog->queue_list = g_list_append(inc_dialog->queue_list,
246 inc_dialog->mainwin = mainwin;
247 session->data = inc_dialog;
250 text[1] = account->account_name;
251 text[2] = _("Standby");
252 gtk_clist_append(GTK_CLIST(inc_dialog->dialog->clist), text);
254 main_window_set_toolbar_sensitive(mainwin);
255 main_window_set_menu_sensitive(mainwin);
257 return inc_start(inc_dialog);
260 inc_spool_account(account);
266 void inc_all_account_mail(MainWindow *mainwin, gboolean notify)
268 GList *list, *queue_list = NULL;
269 IncProgressDialog *inc_dialog;
272 if (prefs_common.work_offline)
273 if (alertpanel(_("Offline warning"),
274 _("You're working offline. Override?"),
275 _("Yes"), _("No"), NULL) != G_ALERTDEFAULT)
278 if (inc_lock_count) return;
280 inc_autocheck_timer_remove();
281 main_window_lock(mainwin);
283 if (prefs_common.inc_local)
284 new_msgs = inc_spool();
286 list = account_get_list();
288 inc_finished(mainwin, new_msgs > 0);
289 main_window_unlock(mainwin);
290 inc_notify_cmd(new_msgs, notify);
291 inc_autocheck_timer_set();
295 /* check local folders */
298 /* check IMAP4 folders */
299 for (; list != NULL; list = list->next) {
300 PrefsAccount *account = list->data;
301 if ((account->protocol == A_IMAP4 ||
302 account->protocol == A_NNTP) && account->recv_at_getall)
303 folderview_check_new(FOLDER(account->folder));
306 /* check POP3 accounts */
307 for (list = account_get_list(); list != NULL; list = list->next) {
309 PrefsAccount *account = list->data;
310 account->session = STYPE_NORMAL;
311 if (account->recv_at_getall) {
312 session = inc_session_new(account);
314 queue_list = g_list_append(queue_list, session);
319 inc_finished(mainwin, new_msgs > 0);
320 main_window_unlock(mainwin);
321 inc_notify_cmd(new_msgs, notify);
322 inc_autocheck_timer_set();
326 inc_dialog = inc_progress_dialog_create();
327 inc_dialog->queue_list = queue_list;
328 inc_dialog->mainwin = mainwin;
329 for (list = queue_list; list != NULL; list = list->next) {
330 IncSession *session = list->data;
333 session->data = inc_dialog;
336 text[1] = session->pop3_state->ac_prefs->account_name;
337 text[2] = _("Standby");
338 gtk_clist_append(GTK_CLIST(inc_dialog->dialog->clist), text);
341 main_window_set_toolbar_sensitive(mainwin);
342 main_window_set_menu_sensitive(mainwin);
344 new_msgs += inc_start(inc_dialog);
346 inc_finished(mainwin, new_msgs > 0);
347 main_window_unlock(mainwin);
348 inc_notify_cmd(new_msgs, notify);
349 inc_autocheck_timer_set();
352 static IncProgressDialog *inc_progress_dialog_create(void)
354 IncProgressDialog *dialog;
355 ProgressDialog *progress;
357 dialog = g_new0(IncProgressDialog, 1);
359 progress = progress_dialog_create();
360 gtk_window_set_title(GTK_WINDOW(progress->window),
361 _("Retrieving new messages"));
362 gtk_signal_connect(GTK_OBJECT(progress->cancel_btn), "clicked",
363 GTK_SIGNAL_FUNC(inc_cancel_cb), dialog);
364 gtk_signal_connect(GTK_OBJECT(progress->window), "delete_event",
365 GTK_SIGNAL_FUNC(gtk_true), NULL);
366 /* manage_window_set_transient(GTK_WINDOW(progress->window)); */
368 progress_dialog_set_value(progress, 0.0);
370 stock_pixmap_gdk(progress->clist, STOCK_PIXMAP_COMPLETE,
372 stock_pixmap_gdk(progress->clist, STOCK_PIXMAP_CONTINUE,
373 ¤txpm, ¤txpmmask);
374 stock_pixmap_gdk(progress->clist, STOCK_PIXMAP_ERROR,
375 &errorxpm, &errorxpmmask);
377 if (prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS ||
378 (prefs_common.recv_dialog_mode == RECV_DIALOG_ACTIVE &&
379 manage_window_get_focus_window())) {
380 dialog->show_dialog = TRUE;
381 gtk_widget_show_now(progress->window);
384 dialog->dialog = progress;
385 dialog->queue_list = NULL;
387 inc_dialog_list = g_list_append(inc_dialog_list, dialog);
392 static void inc_progress_dialog_clear(IncProgressDialog *inc_dialog)
394 progress_dialog_set_value(inc_dialog->dialog, 0.0);
395 progress_dialog_set_label(inc_dialog->dialog, "");
396 gtk_progress_bar_update
397 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar), 0.0);
400 static void inc_progress_dialog_destroy(IncProgressDialog *inc_dialog)
402 g_return_if_fail(inc_dialog != NULL);
404 inc_dialog_list = g_list_remove(inc_dialog_list, inc_dialog);
406 gtk_progress_bar_update
407 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar), 0.0);
408 progress_dialog_destroy(inc_dialog->dialog);
413 static IncSession *inc_session_new(PrefsAccount *account)
417 g_return_val_if_fail(account != NULL, NULL);
419 if (account->protocol != A_POP3 && account->protocol != A_APOP)
421 if (!account->recv_server || !account->userid)
424 session = g_new0(IncSession, 1);
425 session->pop3_state = inc_pop3_state_new(account);
426 session->pop3_state->session = session;
431 static void inc_session_destroy(IncSession *session)
433 g_return_if_fail(session != NULL);
435 inc_pop3_state_destroy(session->pop3_state);
439 static Pop3State *inc_pop3_state_new(PrefsAccount *account)
443 state = g_new0(Pop3State, 1);
445 state->ac_prefs = account;
446 state->folder_table = g_hash_table_new(NULL, NULL);
447 state->uidl_todelete_list = NULL;
448 state->uidl_table = inc_get_uidl_table(account);
449 state->inc_state = INC_SUCCESS;
450 state->current_time = time(NULL);
455 static void inc_pop3_state_destroy(Pop3State *state)
459 g_hash_table_destroy(state->folder_table);
461 for (n = 1; n <= state->count; n++)
462 g_free(state->msg[n].uidl);
465 if (state->uidl_table) {
466 hash_free_strings(state->uidl_table);
467 g_hash_table_destroy(state->uidl_table);
469 g_slist_free(state->uidl_todelete_list);
470 g_free(state->greeting);
473 g_free(state->prev_folder);
478 static gint inc_start(IncProgressDialog *inc_dialog)
481 GtkCList *clist = GTK_CLIST(inc_dialog->dialog->clist);
482 Pop3State *pop3_state;
488 while (inc_dialog->queue_list != NULL) {
489 session = inc_dialog->queue_list->data;
490 pop3_state = session->pop3_state;
492 inc_progress_dialog_clear(inc_dialog);
494 gtk_clist_moveto(clist, num, -1, 1.0, 0.0);
496 pop3_state->user = g_strdup(pop3_state->ac_prefs->userid);
497 if (pop3_state->ac_prefs->passwd)
499 g_strdup(pop3_state->ac_prefs->passwd);
500 else if (pop3_state->ac_prefs->tmp_pass)
502 g_strdup(pop3_state->ac_prefs->tmp_pass);
506 pass = input_dialog_query_password
507 (pop3_state->ac_prefs->recv_server,
510 if (inc_dialog->show_dialog)
511 manage_window_focus_in
512 (inc_dialog->mainwin->window,
515 pop3_state->ac_prefs->tmp_pass = g_strdup(pass);
516 pop3_state->pass = pass;
518 inc_session_destroy(session);
519 inc_dialog->queue_list = g_list_remove
520 (inc_dialog->queue_list, session);
525 gtk_clist_set_pixmap(clist, num, 0, currentxpm, currentxpmmask);
526 gtk_clist_set_text(clist, num, 2, _("Retrieving"));
528 /* begin POP3 session */
529 inc_state = inc_pop3_session_do(session);
531 if (inc_state == INC_SUCCESS) {
532 gtk_clist_set_pixmap(clist, num, 0, okxpm, okxpmmask);
533 gtk_clist_set_text(clist, num, 2, _("Done"));
534 } else if (inc_state == INC_CANCEL) {
535 gtk_clist_set_pixmap(clist, num, 0, okxpm, okxpmmask);
536 gtk_clist_set_text(clist, num, 2, _("Cancelled"));
538 gtk_clist_set_pixmap(clist, num, 0, errorxpm, errorxpmmask);
539 if (inc_state == INC_CONNECT_ERROR)
540 gtk_clist_set_text(clist, num, 2,
541 _("Connection failed"));
542 else if (inc_state == INC_AUTH_FAILED)
543 gtk_clist_set_text(clist, num, 2,
546 gtk_clist_set_text(clist, num, 2, _("Error"));
549 if (pop3_state->error_val == PS_AUTHFAIL) {
550 if(!prefs_common.no_recv_err_panel) {
551 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
552 ((prefs_common.recv_dialog_mode == RECV_DIALOG_ACTIVE) && focus_window)) {
553 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
556 (_("Authorization for %s on %s failed"),
558 pop3_state->ac_prefs->recv_server);
564 /* CLAWS: perform filtering actions on dropped message */
565 if (global_processing != NULL) {
566 FolderItem *processing, *inbox;
568 GSList *msglist, *msglist_element;
570 /* CLAWS: get default inbox (perhaps per account) */
571 if (pop3_state->ac_prefs->inbox) {
572 /* CLAWS: get destination folder / mailbox */
573 inbox = folder_find_item_from_identifier(pop3_state->ac_prefs->inbox);
575 inbox = folder_get_default_inbox();
577 inbox = folder_get_default_inbox();
579 /* get list of messages in processing */
580 processing = folder_get_default_processing();
581 folder_item_scan(processing);
582 msglist = folder_item_get_msg_list(processing);
584 /* process messages */
585 for(msglist_element = msglist; msglist_element != NULL; msglist_element = msglist_element->next) {
586 msginfo = (MsgInfo *) msglist_element->data;
587 /* filter if enabled in prefs or move to inbox if not */
588 if(pop3_state->ac_prefs->filter_on_recv) {
589 filter_message_by_msginfo_with_inbox(global_processing, msginfo,
590 pop3_state->folder_table,
593 folder_item_move_msg(inbox, msginfo);
594 g_hash_table_insert(pop3_state->folder_table, inbox,
597 procmsg_msginfo_free(msginfo);
599 g_slist_free(msglist);
603 new_msgs += pop3_state->cur_total_num;
605 folderview_update_item_foreach
606 (pop3_state->folder_table);
608 if (pop3_state->error_val == PS_AUTHFAIL &&
609 pop3_state->ac_prefs->tmp_pass) {
610 g_free(pop3_state->ac_prefs->tmp_pass);
611 pop3_state->ac_prefs->tmp_pass = NULL;
614 inc_write_uidl_list(pop3_state);
616 if (inc_state != INC_SUCCESS && inc_state != INC_CANCEL) {
618 if (inc_state == INC_NOSPACE) {
619 inc_put_error(inc_state);
624 inc_session_destroy(session);
625 inc_dialog->queue_list =
626 g_list_remove(inc_dialog->queue_list, session);
631 if (error_num && !prefs_common.no_recv_err_panel) {
632 if (inc_dialog->show_dialog)
633 manage_window_focus_in(inc_dialog->dialog->window,
635 alertpanel_error(_("Some errors occured while getting mail."));
636 if (inc_dialog->show_dialog)
637 manage_window_focus_out(inc_dialog->dialog->window,
641 while (inc_dialog->queue_list != NULL) {
642 session = inc_dialog->queue_list->data;
643 inc_session_destroy(session);
644 inc_dialog->queue_list =
645 g_list_remove(inc_dialog->queue_list, session);
648 inc_progress_dialog_destroy(inc_dialog);
653 static IncState inc_pop3_session_do(IncSession *session)
655 Pop3State *pop3_state = session->pop3_state;
656 IncProgressDialog *inc_dialog = (IncProgressDialog *)session->data;
663 static AtmHandler handlers[] = {
666 pop3_stls_send , pop3_stls_recv,
668 pop3_getauth_user_send , pop3_getauth_user_recv,
669 pop3_getauth_pass_send , pop3_getauth_pass_recv,
670 pop3_getauth_apop_send , pop3_getauth_apop_recv,
671 pop3_getrange_stat_send , pop3_getrange_stat_recv,
672 pop3_getrange_last_send , pop3_getrange_last_recv,
673 pop3_getrange_uidl_send , pop3_getrange_uidl_recv,
674 pop3_getsize_list_send , pop3_getsize_list_recv,
675 pop3_top_send , pop3_top_recv,
676 pop3_retr_send , pop3_retr_recv,
677 pop3_delete_send , pop3_delete_recv,
678 pop3_logout_send , pop3_logout_recv
681 debug_print(_("getting new messages of account %s...\n"),
682 pop3_state->ac_prefs->account_name);
684 atm = automaton_create(N_POP3_PHASE);
687 atm->data = pop3_state;
689 buf = g_strdup_printf(_("%s: Retrieving new messages"),
690 pop3_state->ac_prefs->recv_server);
691 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window), buf);
694 for (i = POP3_GREETING_RECV; i < N_POP3_PHASE; i++)
695 atm->state[i].handler = handlers[i];
696 atm->state[POP3_GREETING_RECV].condition = GDK_INPUT_READ;
697 for (i = POP3_GREETING_RECV + 1; i < N_POP3_PHASE; ) {
698 atm->state[i++].condition = GDK_INPUT_WRITE;
699 atm->state[i++].condition = GDK_INPUT_READ;
702 atm->terminate = (AtmHandler)pop3_automaton_terminate;
703 atm->ui_func = (AtmUIFunc)inc_progress_update;
705 atm->num = POP3_GREETING_RECV;
707 server = pop3_state->ac_prefs->recv_server;
709 port = pop3_state->ac_prefs->set_popport ?
710 pop3_state->ac_prefs->popport :
711 pop3_state->ac_prefs->ssl_pop == SSL_TUNNEL ? 995 : 110;
713 port = pop3_state->ac_prefs->set_popport ?
714 pop3_state->ac_prefs->popport : 110;
717 buf = g_strdup_printf(_("Connecting to POP3 server: %s ..."), server);
718 log_message("%s\n", buf);
719 progress_dialog_set_label(inc_dialog->dialog, buf);
724 if ((sockinfo = sock_connect(server, port)) == NULL) {
725 log_warning(_("Can't connect to POP3 server: %s:%d\n"),
727 if(!prefs_common.no_recv_err_panel) {
728 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
729 ((prefs_common.recv_dialog_mode == RECV_DIALOG_ACTIVE) && focus_window)) {
730 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
732 alertpanel_error(_("Can't connect to POP3 server: %s:%d"),
734 manage_window_focus_out(inc_dialog->dialog->window, NULL, NULL);
736 pop3_automaton_terminate(NULL, atm);
737 automaton_destroy(atm);
738 return INC_CONNECT_ERROR;
742 if (pop3_state->ac_prefs->ssl_pop == SSL_TUNNEL &&
743 !ssl_init_socket(sockinfo)) {
744 pop3_automaton_terminate(NULL, atm);
745 automaton_destroy(atm);
746 return INC_CONNECT_ERROR;
750 /* :WK: Hmmm, with the later sock_gdk_input, we have 2 references
751 * to the sock structure - implement a reference counter?? */
752 pop3_state->sockinfo = sockinfo;
753 atm->help_sock = sockinfo;
755 log_verbosity_set(TRUE);
756 /* oha: this messes up inc_progress update:
757 disabling this would avoid the label "Retrieve Header"
758 being overwritten by "Retrieve Message"
759 Setting inc_pop3_recv_func is not necessary
760 since atm already handles the progress dialog ui
763 recv_set_ui_func(inc_pop3_recv_func, session);
765 atm->tag = sock_gdk_input_add(sockinfo,
766 atm->state[atm->num].condition,
767 automaton_input_cb, atm);
769 while (!atm->terminated)
770 gtk_main_iteration();
772 log_verbosity_set(FALSE);
774 recv_set_ui_func(NULL, NULL);
776 automaton_destroy(atm);
778 return pop3_state->inc_state;
781 static gint pop3_automaton_terminate(SockInfo *source, Automaton *atm)
783 if (atm->terminated) return 0;
786 gdk_input_remove(atm->tag);
789 if (atm->timeout_tag > 0) {
790 gtk_timeout_remove(atm->timeout_tag);
791 atm->timeout_tag = 0;
796 atm->terminated = TRUE;
801 static GHashTable *inc_get_uidl_table(PrefsAccount *ac_prefs)
806 gchar buf[IDLEN + 3];
807 gchar uidl[IDLEN + 3];
811 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
812 "uidl", G_DIR_SEPARATOR_S, ac_prefs->recv_server,
813 "-", ac_prefs->userid, NULL);
814 if ((fp = fopen(path, "rb")) == NULL) {
815 if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
817 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
818 "uidl-", ac_prefs->recv_server,
819 "-", ac_prefs->userid, NULL);
820 if ((fp = fopen(path, "rb")) == NULL) {
821 if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
828 table = g_hash_table_new(g_str_hash, g_str_equal);
832 while (fgets(buf, sizeof(buf), fp) != NULL) {
835 if (sscanf(buf, "%s\t%ld", uidl, &recv_time) != 2) {
836 if (sscanf(buf, "%s", uidl) != 1)
843 g_hash_table_insert(table, g_strdup(uidl),
844 GINT_TO_POINTER(recv_time));
851 static void inc_write_uidl_list(Pop3State *state)
858 if (!state->uidl_is_valid) return;
860 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
861 "uidl", G_DIR_SEPARATOR_S,
862 state->ac_prefs->recv_server,
863 "-", state->ac_prefs->userid, NULL);
864 if ((fp = fopen(path, "wb")) == NULL) {
865 FILE_OP_ERROR(path, "fopen");
870 for (n = 1; n <= state->count; n++) {
871 msg = &state->msg[n];
872 if (msg->uidl && msg->received && !msg->deleted)
873 fprintf(fp, "%s\t%ld\n", msg->uidl, msg->recv_time);
876 if (fclose(fp) == EOF) FILE_OP_ERROR(path, "fclose");
880 static gboolean inc_pop3_recv_func(SockInfo *sock, gint count, gint read_bytes,
883 gchar buf[MSGBUFSIZE];
884 IncSession *session = (IncSession *)data;
885 Pop3State *state = session->pop3_state;
886 IncProgressDialog *inc_dialog = session->data;
887 ProgressDialog *dialog = inc_dialog->dialog;
891 cur_total = state->cur_total_bytes + read_bytes;
892 if (cur_total > state->total_bytes)
893 cur_total = state->total_bytes;
895 Xstrdup_a(total_size, to_human_readable(state->total_bytes),
897 g_snprintf(buf, sizeof(buf),
898 _("Retrieving message (%d / %d) (%s / %s)"),
899 state->cur_msg, state->count,
900 to_human_readable(cur_total), total_size);
901 progress_dialog_set_label(dialog, buf);
903 progress_dialog_set_percentage
904 (dialog, (gfloat)cur_total / (gfloat)state->total_bytes);
905 gtk_progress_bar_update
906 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar),
907 (gfloat)cur_total / (gfloat)state->total_bytes);
910 if (state->inc_state == INC_CANCEL)
916 void inc_progress_update(Pop3State *state, Pop3Phase phase)
918 gchar buf[MSGBUFSIZE];
919 IncProgressDialog *inc_dialog = state->session->data;
920 ProgressDialog *dialog = inc_dialog->dialog;
924 case POP3_GREETING_RECV:
926 case POP3_GETAUTH_USER_SEND:
927 case POP3_GETAUTH_PASS_SEND:
928 case POP3_GETAUTH_APOP_SEND:
929 progress_dialog_set_label(dialog, _("Authenticating..."));
931 case POP3_GETRANGE_STAT_SEND:
932 progress_dialog_set_label
933 (dialog, _("Getting the number of new messages (STAT)..."));
935 case POP3_GETRANGE_LAST_SEND:
936 progress_dialog_set_label
937 (dialog, _("Getting the number of new messages (LAST)..."));
939 case POP3_GETRANGE_UIDL_SEND:
940 progress_dialog_set_label
941 (dialog, _("Getting the number of new messages (UIDL)..."));
943 case POP3_GETSIZE_LIST_SEND:
944 progress_dialog_set_label
945 (dialog, _("Getting the size of messages (LIST)..."));
948 g_snprintf(buf, sizeof(buf),
949 _("Retrieving header (%d / %d)"),
950 state->cur_msg, state->count);
951 progress_dialog_set_label (dialog, buf);
952 progress_dialog_set_percentage
954 (gfloat)(state->cur_msg) /
955 (gfloat)(state->count));
956 gtk_progress_bar_update
957 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar),
958 (gfloat)(state->cur_msg) /
959 (gfloat)(state->count));
962 Xstrdup_a(total_size, to_human_readable(state->total_bytes), return);
963 g_snprintf(buf, sizeof(buf),
964 _("Retrieving message (%d / %d) (%s / %s)"),
965 state->cur_msg, state->count,
966 to_human_readable(state->cur_total_bytes),
968 progress_dialog_set_label(dialog, buf);
969 progress_dialog_set_percentage
971 (gfloat)(state->cur_total_bytes) /
972 (gfloat)(state->total_bytes));
973 gtk_progress_bar_update
974 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar),
975 (gfloat)(state->cur_total_bytes) /
976 (gfloat)(state->total_bytes));
978 case POP3_DELETE_SEND:
979 if (state->msg[state->cur_msg].recv_time < state->current_time) {
980 g_snprintf(buf, sizeof(buf), _("Deleting message %d"),
982 progress_dialog_set_label(dialog, buf);
985 case POP3_LOGOUT_SEND:
986 progress_dialog_set_label(dialog, _("Quitting"));
993 gint inc_drop_message(const gchar *file, Pop3State *state)
996 FolderItem *dropfolder;
1000 /* CLAWS: get default inbox (perhaps per account) */
1001 if (state->ac_prefs->inbox) {
1002 /* CLAWS: get destination folder / mailbox */
1003 inbox = folder_find_item_from_identifier
1004 (state->ac_prefs->inbox);
1006 inbox = folder_get_default_inbox();
1008 inbox = folder_get_default_inbox();
1014 /* CLAWS: claws uses a global .processing folder for the filtering. */
1015 if (global_processing == NULL) {
1016 if (state->ac_prefs->filter_on_recv) {
1018 filter_get_dest_folder(prefs_common.fltlist, file);
1019 if (!dropfolder) dropfolder = inbox;
1020 else if (!strcmp(dropfolder->path, FILTER_NOT_RECEIVE)) {
1021 g_warning(_("a message won't be received\n"));
1027 dropfolder = folder_get_default_processing();
1030 val = GPOINTER_TO_INT(g_hash_table_lookup
1031 (state->folder_table, dropfolder));
1033 g_hash_table_insert(state->folder_table, dropfolder,
1034 GINT_TO_POINTER(1));
1037 /* add msg file to drop folder */
1038 if ((msgnum = folder_item_add_msg(dropfolder, file, TRUE)) < 0) {
1046 static void inc_put_error(IncState istate)
1050 if (!prefs_common.no_recv_err_panel)
1052 (_("Error occurred while processing mail."));
1055 alertpanel_error(_("No disk space left."));
1062 static void inc_cancel(IncProgressDialog *dialog)
1064 IncSession *session;
1067 g_return_if_fail(dialog != NULL);
1069 session = dialog->queue_list->data;
1070 sockinfo = session->pop3_state->sockinfo;
1072 if (!sockinfo || session->atm->terminated == TRUE) return;
1074 session->pop3_state->inc_state = INC_CANCEL;
1075 pop3_automaton_terminate(sockinfo, session->atm);
1076 session->pop3_state->sockinfo = NULL;
1079 gboolean inc_is_active(void)
1081 return (inc_dialog_list != NULL);
1084 void inc_cancel_all(void)
1088 for (cur = inc_dialog_list; cur != NULL; cur = cur->next)
1089 inc_cancel((IncProgressDialog *)cur->data);
1092 static void inc_cancel_cb(GtkWidget *widget, gpointer data)
1094 inc_cancel((IncProgressDialog *)data);
1097 static gint inc_spool(void)
1099 gchar *mbox, *logname;
1102 logname = g_get_user_name();
1103 mbox = g_strconcat(prefs_common.spool_path
1104 ? prefs_common.spool_path : DEFAULT_SPOOL_PATH,
1105 G_DIR_SEPARATOR_S, logname, NULL);
1106 msgs = get_spool(folder_get_default_inbox(), mbox);
1112 static void inc_spool_account(PrefsAccount *account)
1116 if (account->inbox) {
1117 inbox = folder_find_item_from_path(account->inbox);
1119 inbox = folder_get_default_inbox();
1121 inbox = folder_get_default_inbox();
1123 get_spool(inbox, account->local_mbox);
1126 static void inc_all_spool(void)
1130 list = account_get_list();
1133 for (; list != NULL; list = list->next) {
1134 PrefsAccount *account = list->data;
1136 if ((account->protocol == A_LOCAL) &&
1137 (account->recv_at_getall))
1138 inc_spool_account(account);
1142 static gint get_spool(FolderItem *dest, const gchar *mbox)
1146 gchar tmp_mbox[MAXPATHLEN + 1];
1147 GHashTable *folder_table = NULL;
1149 g_return_val_if_fail(dest != NULL, -1);
1150 g_return_val_if_fail(mbox != NULL, -1);
1152 if (!is_file_exist(mbox) || (size = get_file_size(mbox)) == 0) {
1153 debug_print(_("no messages in local mailbox.\n"));
1155 } else if (size < 0)
1158 if ((lockfd = lock_mbox(mbox, LOCK_FLOCK)) < 0)
1161 g_snprintf(tmp_mbox, sizeof(tmp_mbox), "%s%ctmpmbox%d",
1162 get_rc_dir(), G_DIR_SEPARATOR, (gint)mbox);
1164 if (copy_mbox(mbox, tmp_mbox) < 0) {
1165 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1169 debug_print(_("Getting new messages from %s into %s...\n"),
1172 if (prefs_common.filter_on_inc)
1173 folder_table = g_hash_table_new(NULL, NULL);
1174 msgs = proc_mbox(dest, tmp_mbox, folder_table);
1177 if (msgs >= 0) empty_mbox(mbox);
1178 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1181 if (!prefs_common.scan_all_after_inc) {
1182 g_hash_table_insert(folder_table, dest,
1183 GINT_TO_POINTER(1));
1184 folderview_update_item_foreach(folder_table);
1186 g_hash_table_destroy(folder_table);
1187 } else if (!prefs_common.scan_all_after_inc) {
1188 folderview_update_item(dest, FALSE);
1199 void inc_unlock(void)
1201 if (inc_lock_count > 0)
1205 static guint autocheck_timer = 0;
1206 static gpointer autocheck_data = NULL;
1208 static void inc_notify_cmd(gint new_msgs, gboolean notify)
1213 if (!(new_msgs && notify && prefs_common.newmail_notify_cmd &&
1214 *prefs_common.newmail_notify_cmd))
1216 if ((buf = strchr(prefs_common.newmail_notify_cmd, '%')) &&
1217 buf[1] == 'd' && !strchr(&buf[1], '%'))
1218 buf = g_strdup_printf(prefs_common.newmail_notify_cmd,
1221 buf = g_strdup(prefs_common.newmail_notify_cmd);
1223 execute_command_line(buf, TRUE);
1228 void inc_autocheck_timer_init(MainWindow *mainwin)
1230 autocheck_data = mainwin;
1231 inc_autocheck_timer_set();
1234 static void inc_autocheck_timer_set_interval(guint interval)
1236 inc_autocheck_timer_remove();
1237 /* last test is to avoid re-enabling auto_check after modifying
1238 the common preferences */
1239 if (prefs_common.autochk_newmail && autocheck_data
1240 && prefs_common.work_offline == FALSE) {
1241 autocheck_timer = gtk_timeout_add
1242 (interval, inc_autocheck_func, autocheck_data);
1243 debug_print("added timer = %d\n", autocheck_timer);
1247 void inc_autocheck_timer_set(void)
1249 inc_autocheck_timer_set_interval(prefs_common.autochk_itv * 60000);
1252 void inc_autocheck_timer_remove(void)
1254 if (autocheck_timer) {
1255 debug_print("removed timer = %d\n", autocheck_timer);
1256 gtk_timeout_remove(autocheck_timer);
1257 autocheck_timer = 0;
1261 static gint inc_autocheck_func(gpointer data)
1263 MainWindow *mainwin = (MainWindow *)data;
1265 if (inc_lock_count) {
1266 debug_print("autocheck is locked.\n");
1267 inc_autocheck_timer_set_interval(1000);
1271 inc_all_account_mail(mainwin, prefs_common.newmail_notify_auto);