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);
155 } else if (prefs_common.scan_all_after_inc) {
156 item = mainwin->summaryview->folder_item;
157 if (FOLDER_SUMMARY_MISMATCH(item, mainwin->summaryview)) {
158 folderview_update_item(item, TRUE);
163 void inc_mail(MainWindow *mainwin, gboolean notify)
167 if (inc_lock_count) return;
169 if (prefs_common.work_offline)
170 if (alertpanel(_("Offline warning"),
171 _("You're working offline. Override?"),
172 _("Yes"), _("No"), NULL) != G_ALERTDEFAULT)
175 inc_autocheck_timer_remove();
176 main_window_lock(mainwin);
178 if (prefs_common.use_extinc && prefs_common.extinc_cmd) {
179 /* external incorporating program */
180 if (execute_command_line(prefs_common.extinc_cmd, FALSE) < 0) {
181 main_window_unlock(mainwin);
182 inc_autocheck_timer_set();
186 if (prefs_common.inc_local)
187 new_msgs = inc_spool();
192 if (prefs_common.inc_local)
193 new_msgs = inc_spool();
195 new_msgs += inc_account_mail(cur_account, mainwin);
198 inc_finished(mainwin, new_msgs > 0);
199 main_window_unlock(mainwin);
200 inc_notify_cmd(new_msgs, notify);
201 inc_autocheck_timer_set();
204 void inc_selective_download(MainWindow *mainwin, PrefsAccount *acc, gint session)
209 acc->session = session;
210 inc_account_mail(acc, mainwin);
211 acc->session = STYPE_NORMAL;
213 for (cur = acc->msg_list; cur != NULL; cur = cur->next) {
214 HeaderItems *items =(HeaderItems*)cur->data;
216 if (items->state == SD_DOWNLOADED &&
217 items->del_by_old_session == FALSE) {
223 inc_finished(mainwin, TRUE);
224 inc_notify_cmd(new_msgs, prefs_common.newmail_notify_manu);
228 static gint inc_account_mail(PrefsAccount *account, MainWindow *mainwin)
230 IncProgressDialog *inc_dialog;
234 switch (account->protocol) {
237 folderview_check_new(FOLDER(account->folder));
242 session = inc_session_new(account);
243 if (!session) return 0;
245 inc_dialog = inc_progress_dialog_create();
246 inc_dialog->queue_list = g_list_append(inc_dialog->queue_list,
248 inc_dialog->mainwin = mainwin;
249 session->data = inc_dialog;
252 text[1] = account->account_name;
253 text[2] = _("Standby");
254 gtk_clist_append(GTK_CLIST(inc_dialog->dialog->clist), text);
256 toolbar_set_sensitive(mainwin);
257 main_window_set_menu_sensitive(mainwin);
259 return inc_start(inc_dialog);
262 inc_spool_account(account);
268 void inc_all_account_mail(MainWindow *mainwin, gboolean notify)
270 GList *list, *queue_list = NULL;
271 IncProgressDialog *inc_dialog;
274 if (prefs_common.work_offline)
275 if (alertpanel(_("Offline warning"),
276 _("You're working offline. Override?"),
277 _("Yes"), _("No"), NULL) != G_ALERTDEFAULT)
280 if (inc_lock_count) return;
282 inc_autocheck_timer_remove();
283 main_window_lock(mainwin);
285 if (prefs_common.inc_local) {
286 new_msgs = inc_spool();
291 list = account_get_list();
293 inc_finished(mainwin, new_msgs > 0);
294 main_window_unlock(mainwin);
295 inc_notify_cmd(new_msgs, notify);
296 inc_autocheck_timer_set();
300 /* check local folders */
303 /* check IMAP4 folders */
304 for (; list != NULL; list = list->next) {
305 PrefsAccount *account = list->data;
306 if ((account->protocol == A_IMAP4 ||
307 account->protocol == A_NNTP) && account->recv_at_getall)
308 folderview_check_new(FOLDER(account->folder));
311 /* check POP3 accounts */
312 for (list = account_get_list(); list != NULL; list = list->next) {
314 PrefsAccount *account = list->data;
315 account->session = STYPE_NORMAL;
316 if (account->recv_at_getall) {
317 session = inc_session_new(account);
319 queue_list = g_list_append(queue_list, session);
324 inc_finished(mainwin, new_msgs > 0);
325 main_window_unlock(mainwin);
326 inc_notify_cmd(new_msgs, notify);
327 inc_autocheck_timer_set();
331 inc_dialog = inc_progress_dialog_create();
332 inc_dialog->queue_list = queue_list;
333 inc_dialog->mainwin = mainwin;
334 for (list = queue_list; list != NULL; list = list->next) {
335 IncSession *session = list->data;
338 session->data = inc_dialog;
341 text[1] = session->pop3_state->ac_prefs->account_name;
342 text[2] = _("Standby");
343 gtk_clist_append(GTK_CLIST(inc_dialog->dialog->clist), text);
346 toolbar_set_sensitive(mainwin);
347 main_window_set_menu_sensitive(mainwin);
349 new_msgs += inc_start(inc_dialog);
351 inc_finished(mainwin, new_msgs > 0);
352 main_window_unlock(mainwin);
353 inc_notify_cmd(new_msgs, notify);
354 inc_autocheck_timer_set();
357 static IncProgressDialog *inc_progress_dialog_create(void)
359 IncProgressDialog *dialog;
360 ProgressDialog *progress;
362 dialog = g_new0(IncProgressDialog, 1);
364 progress = progress_dialog_create();
365 gtk_window_set_title(GTK_WINDOW(progress->window),
366 _("Retrieving new messages"));
367 gtk_signal_connect(GTK_OBJECT(progress->cancel_btn), "clicked",
368 GTK_SIGNAL_FUNC(inc_cancel_cb), dialog);
369 gtk_signal_connect(GTK_OBJECT(progress->window), "delete_event",
370 GTK_SIGNAL_FUNC(gtk_true), NULL);
371 /* manage_window_set_transient(GTK_WINDOW(progress->window)); */
373 progress_dialog_set_value(progress, 0.0);
375 stock_pixmap_gdk(progress->clist, STOCK_PIXMAP_COMPLETE,
377 stock_pixmap_gdk(progress->clist, STOCK_PIXMAP_CONTINUE,
378 ¤txpm, ¤txpmmask);
379 stock_pixmap_gdk(progress->clist, STOCK_PIXMAP_ERROR,
380 &errorxpm, &errorxpmmask);
382 if (prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS ||
383 (prefs_common.recv_dialog_mode == RECV_DIALOG_ACTIVE &&
384 manage_window_get_focus_window())) {
385 dialog->show_dialog = TRUE;
386 gtk_widget_show_now(progress->window);
389 dialog->dialog = progress;
390 dialog->queue_list = NULL;
392 inc_dialog_list = g_list_append(inc_dialog_list, dialog);
397 static void inc_progress_dialog_clear(IncProgressDialog *inc_dialog)
399 progress_dialog_set_value(inc_dialog->dialog, 0.0);
400 progress_dialog_set_label(inc_dialog->dialog, "");
401 gtk_progress_bar_update
402 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar), 0.0);
405 static void inc_progress_dialog_destroy(IncProgressDialog *inc_dialog)
407 g_return_if_fail(inc_dialog != NULL);
409 inc_dialog_list = g_list_remove(inc_dialog_list, inc_dialog);
411 gtk_progress_bar_update
412 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar), 0.0);
413 progress_dialog_destroy(inc_dialog->dialog);
418 static IncSession *inc_session_new(PrefsAccount *account)
422 g_return_val_if_fail(account != NULL, NULL);
424 if (account->protocol != A_POP3 && account->protocol != A_APOP)
426 if (!account->recv_server || !account->userid)
429 session = g_new0(IncSession, 1);
430 session->pop3_state = inc_pop3_state_new(account);
431 session->pop3_state->session = session;
436 static void inc_session_destroy(IncSession *session)
438 g_return_if_fail(session != NULL);
440 inc_pop3_state_destroy(session->pop3_state);
444 static Pop3State *inc_pop3_state_new(PrefsAccount *account)
448 state = g_new0(Pop3State, 1);
450 state->ac_prefs = account;
451 state->folder_table = g_hash_table_new(NULL, NULL);
452 state->uidl_todelete_list = NULL;
453 state->uidl_table = inc_get_uidl_table(account);
454 state->inc_state = INC_SUCCESS;
455 state->current_time = time(NULL);
460 static void inc_pop3_state_destroy(Pop3State *state)
464 g_hash_table_destroy(state->folder_table);
466 for (n = 1; n <= state->count; n++)
467 g_free(state->msg[n].uidl);
470 if (state->uidl_table) {
471 hash_free_strings(state->uidl_table);
472 g_hash_table_destroy(state->uidl_table);
474 g_slist_free(state->uidl_todelete_list);
475 g_free(state->greeting);
478 g_free(state->prev_folder);
483 static gint inc_start(IncProgressDialog *inc_dialog)
486 GtkCList *clist = GTK_CLIST(inc_dialog->dialog->clist);
487 Pop3State *pop3_state;
493 while (inc_dialog->queue_list != NULL) {
494 session = inc_dialog->queue_list->data;
495 pop3_state = session->pop3_state;
497 inc_progress_dialog_clear(inc_dialog);
499 gtk_clist_moveto(clist, num, -1, 1.0, 0.0);
501 pop3_state->user = g_strdup(pop3_state->ac_prefs->userid);
502 if (pop3_state->ac_prefs->passwd)
504 g_strdup(pop3_state->ac_prefs->passwd);
505 else if (pop3_state->ac_prefs->tmp_pass)
507 g_strdup(pop3_state->ac_prefs->tmp_pass);
511 pass = input_dialog_query_password
512 (pop3_state->ac_prefs->recv_server,
515 if (inc_dialog->show_dialog)
516 manage_window_focus_in
517 (inc_dialog->mainwin->window,
520 pop3_state->ac_prefs->tmp_pass = g_strdup(pass);
521 pop3_state->pass = pass;
523 inc_session_destroy(session);
524 inc_dialog->queue_list = g_list_remove
525 (inc_dialog->queue_list, session);
530 gtk_clist_set_pixmap(clist, num, 0, currentxpm, currentxpmmask);
531 gtk_clist_set_text(clist, num, 2, _("Retrieving"));
533 /* begin POP3 session */
534 inc_state = inc_pop3_session_do(session);
536 if (inc_state == INC_SUCCESS) {
537 gtk_clist_set_pixmap(clist, num, 0, okxpm, okxpmmask);
538 gtk_clist_set_text(clist, num, 2, _("Done"));
539 } else if (inc_state == INC_CANCEL) {
540 gtk_clist_set_pixmap(clist, num, 0, okxpm, okxpmmask);
541 gtk_clist_set_text(clist, num, 2, _("Cancelled"));
543 gtk_clist_set_pixmap(clist, num, 0, errorxpm, errorxpmmask);
544 if (inc_state == INC_CONNECT_ERROR)
545 gtk_clist_set_text(clist, num, 2,
546 _("Connection failed"));
547 else if (inc_state == INC_AUTH_FAILED)
548 gtk_clist_set_text(clist, num, 2,
551 gtk_clist_set_text(clist, num, 2, _("Error"));
554 if (pop3_state->error_val == PS_AUTHFAIL) {
555 if(!prefs_common.no_recv_err_panel) {
556 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
557 ((prefs_common.recv_dialog_mode == RECV_DIALOG_ACTIVE) && focus_window)) {
558 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
561 (_("Authorization for %s on %s failed"),
563 pop3_state->ac_prefs->recv_server);
569 /* CLAWS: perform filtering actions on dropped message */
570 if (global_processing != NULL) {
571 FolderItem *processing, *inbox;
573 GSList *msglist, *msglist_element;
575 /* CLAWS: get default inbox (perhaps per account) */
576 if (pop3_state->ac_prefs->inbox) {
577 /* CLAWS: get destination folder / mailbox */
578 inbox = folder_find_item_from_identifier(pop3_state->ac_prefs->inbox);
580 inbox = folder_get_default_inbox();
582 inbox = folder_get_default_inbox();
584 /* get list of messages in processing */
585 processing = folder_get_default_processing();
586 folder_item_scan(processing);
587 msglist = folder_item_get_msg_list(processing);
589 /* process messages */
590 for(msglist_element = msglist; msglist_element != NULL; msglist_element = msglist_element->next) {
591 msginfo = (MsgInfo *) msglist_element->data;
592 /* filter if enabled in prefs or move to inbox if not */
593 if(pop3_state->ac_prefs->filter_on_recv) {
594 filter_message_by_msginfo_with_inbox(global_processing, msginfo,
595 pop3_state->folder_table,
598 folder_item_move_msg(inbox, msginfo);
599 g_hash_table_insert(pop3_state->folder_table, inbox,
602 procmsg_msginfo_free(msginfo);
604 g_slist_free(msglist);
608 new_msgs += pop3_state->cur_total_num;
610 folderview_update_item_foreach
611 (pop3_state->folder_table, TRUE);
613 if (pop3_state->error_val == PS_AUTHFAIL &&
614 pop3_state->ac_prefs->tmp_pass) {
615 g_free(pop3_state->ac_prefs->tmp_pass);
616 pop3_state->ac_prefs->tmp_pass = NULL;
619 inc_write_uidl_list(pop3_state);
621 if (inc_state != INC_SUCCESS && inc_state != INC_CANCEL) {
623 if (inc_state == INC_NOSPACE) {
624 inc_put_error(inc_state);
629 inc_session_destroy(session);
630 inc_dialog->queue_list =
631 g_list_remove(inc_dialog->queue_list, session);
636 if (error_num && !prefs_common.no_recv_err_panel) {
637 if (inc_dialog->show_dialog)
638 manage_window_focus_in(inc_dialog->dialog->window,
640 alertpanel_error(_("Some errors occured while getting mail."));
641 if (inc_dialog->show_dialog)
642 manage_window_focus_out(inc_dialog->dialog->window,
646 while (inc_dialog->queue_list != NULL) {
647 session = inc_dialog->queue_list->data;
648 inc_session_destroy(session);
649 inc_dialog->queue_list =
650 g_list_remove(inc_dialog->queue_list, session);
653 inc_progress_dialog_destroy(inc_dialog);
658 static IncState inc_pop3_session_do(IncSession *session)
660 Pop3State *pop3_state = session->pop3_state;
661 IncProgressDialog *inc_dialog = (IncProgressDialog *)session->data;
668 static AtmHandler handlers[] = {
671 pop3_stls_send , pop3_stls_recv,
673 pop3_getauth_user_send , pop3_getauth_user_recv,
674 pop3_getauth_pass_send , pop3_getauth_pass_recv,
675 pop3_getauth_apop_send , pop3_getauth_apop_recv,
676 pop3_getrange_stat_send , pop3_getrange_stat_recv,
677 pop3_getrange_last_send , pop3_getrange_last_recv,
678 pop3_getrange_uidl_send , pop3_getrange_uidl_recv,
679 pop3_getsize_list_send , pop3_getsize_list_recv,
680 pop3_top_send , pop3_top_recv,
681 pop3_retr_send , pop3_retr_recv,
682 pop3_delete_send , pop3_delete_recv,
683 pop3_logout_send , pop3_logout_recv
686 debug_print("getting new messages of account %s...\n",
687 pop3_state->ac_prefs->account_name);
689 atm = automaton_create(N_POP3_PHASE);
692 atm->data = pop3_state;
694 buf = g_strdup_printf(_("%s: Retrieving new messages"),
695 pop3_state->ac_prefs->recv_server);
696 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window), buf);
699 for (i = POP3_GREETING_RECV; i < N_POP3_PHASE; i++)
700 atm->state[i].handler = handlers[i];
701 atm->state[POP3_GREETING_RECV].condition = GDK_INPUT_READ;
702 for (i = POP3_GREETING_RECV + 1; i < N_POP3_PHASE; ) {
703 atm->state[i++].condition = GDK_INPUT_WRITE;
704 atm->state[i++].condition = GDK_INPUT_READ;
707 atm->terminate = (AtmHandler)pop3_automaton_terminate;
708 atm->ui_func = (AtmUIFunc)inc_progress_update;
710 atm->num = POP3_GREETING_RECV;
712 server = pop3_state->ac_prefs->recv_server;
714 port = pop3_state->ac_prefs->set_popport ?
715 pop3_state->ac_prefs->popport :
716 pop3_state->ac_prefs->ssl_pop == SSL_TUNNEL ? 995 : 110;
718 port = pop3_state->ac_prefs->set_popport ?
719 pop3_state->ac_prefs->popport : 110;
722 buf = g_strdup_printf(_("Connecting to POP3 server: %s ..."), server);
723 log_message("%s\n", buf);
724 progress_dialog_set_label(inc_dialog->dialog, buf);
729 if ((sockinfo = sock_connect(server, port)) == NULL) {
730 log_warning(_("Can't connect to POP3 server: %s:%d\n"),
732 if(!prefs_common.no_recv_err_panel) {
733 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
734 ((prefs_common.recv_dialog_mode == RECV_DIALOG_ACTIVE) && focus_window)) {
735 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
737 alertpanel_error(_("Can't connect to POP3 server: %s:%d"),
739 manage_window_focus_out(inc_dialog->dialog->window, NULL, NULL);
741 pop3_automaton_terminate(NULL, atm);
742 automaton_destroy(atm);
743 return INC_CONNECT_ERROR;
747 if (pop3_state->ac_prefs->ssl_pop == SSL_TUNNEL &&
748 !ssl_init_socket(sockinfo)) {
749 pop3_automaton_terminate(NULL, atm);
750 automaton_destroy(atm);
751 return INC_CONNECT_ERROR;
755 /* :WK: Hmmm, with the later sock_gdk_input, we have 2 references
756 * to the sock structure - implement a reference counter?? */
757 pop3_state->sockinfo = sockinfo;
758 atm->help_sock = sockinfo;
760 log_verbosity_set(TRUE);
761 /* oha: this messes up inc_progress update:
762 disabling this would avoid the label "Retrieve Header"
763 being overwritten by "Retrieve Message"
764 Setting inc_pop3_recv_func is not necessary
765 since atm already handles the progress dialog ui
768 recv_set_ui_func(inc_pop3_recv_func, session);
770 atm->tag = sock_gdk_input_add(sockinfo,
771 atm->state[atm->num].condition,
772 automaton_input_cb, atm);
774 while (!atm->terminated)
775 gtk_main_iteration();
777 log_verbosity_set(FALSE);
779 recv_set_ui_func(NULL, NULL);
781 automaton_destroy(atm);
783 return pop3_state->inc_state;
786 static gint pop3_automaton_terminate(SockInfo *source, Automaton *atm)
788 if (atm->terminated) return 0;
791 gdk_input_remove(atm->tag);
794 if (atm->timeout_tag > 0) {
795 gtk_timeout_remove(atm->timeout_tag);
796 atm->timeout_tag = 0;
801 atm->terminated = TRUE;
806 static GHashTable *inc_get_uidl_table(PrefsAccount *ac_prefs)
811 gchar buf[IDLEN + 3];
812 gchar uidl[IDLEN + 3];
816 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
817 "uidl", G_DIR_SEPARATOR_S, ac_prefs->recv_server,
818 "-", ac_prefs->userid, NULL);
819 if ((fp = fopen(path, "rb")) == NULL) {
820 if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
822 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
823 "uidl-", ac_prefs->recv_server,
824 "-", ac_prefs->userid, NULL);
825 if ((fp = fopen(path, "rb")) == NULL) {
826 if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
833 table = g_hash_table_new(g_str_hash, g_str_equal);
837 while (fgets(buf, sizeof(buf), fp) != NULL) {
840 if (sscanf(buf, "%s\t%ld", uidl, &recv_time) != 2) {
841 if (sscanf(buf, "%s", uidl) != 1)
848 g_hash_table_insert(table, g_strdup(uidl),
849 GINT_TO_POINTER(recv_time));
856 static void inc_write_uidl_list(Pop3State *state)
863 if (!state->uidl_is_valid) return;
865 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
866 "uidl", G_DIR_SEPARATOR_S,
867 state->ac_prefs->recv_server,
868 "-", state->ac_prefs->userid, NULL);
869 if ((fp = fopen(path, "wb")) == NULL) {
870 FILE_OP_ERROR(path, "fopen");
875 for (n = 1; n <= state->count; n++) {
876 msg = &state->msg[n];
877 if (msg->uidl && msg->received && !msg->deleted)
878 fprintf(fp, "%s\t%ld\n", msg->uidl, msg->recv_time);
881 if (fclose(fp) == EOF) FILE_OP_ERROR(path, "fclose");
885 static gboolean inc_pop3_recv_func(SockInfo *sock, gint count, gint read_bytes,
888 gchar buf[MSGBUFSIZE];
889 IncSession *session = (IncSession *)data;
890 Pop3State *state = session->pop3_state;
891 IncProgressDialog *inc_dialog = session->data;
892 ProgressDialog *dialog = inc_dialog->dialog;
896 cur_total = state->cur_total_bytes + read_bytes;
897 if (cur_total > state->total_bytes)
898 cur_total = state->total_bytes;
900 Xstrdup_a(total_size, to_human_readable(state->total_bytes),
902 g_snprintf(buf, sizeof(buf),
903 _("Retrieving message (%d / %d) (%s / %s)"),
904 state->cur_msg, state->count,
905 to_human_readable(cur_total), total_size);
906 progress_dialog_set_label(dialog, buf);
908 progress_dialog_set_percentage
909 (dialog, (gfloat)cur_total / (gfloat)state->total_bytes);
910 gtk_progress_bar_update
911 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar),
912 (gfloat)cur_total / (gfloat)state->total_bytes);
915 if (state->inc_state == INC_CANCEL)
921 void inc_progress_update(Pop3State *state, Pop3Phase phase)
923 gchar buf[MSGBUFSIZE];
924 IncProgressDialog *inc_dialog = state->session->data;
925 ProgressDialog *dialog = inc_dialog->dialog;
929 case POP3_GREETING_RECV:
931 case POP3_GETAUTH_USER_SEND:
932 case POP3_GETAUTH_PASS_SEND:
933 case POP3_GETAUTH_APOP_SEND:
934 progress_dialog_set_label(dialog, _("Authenticating..."));
936 case POP3_GETRANGE_STAT_SEND:
937 progress_dialog_set_label
938 (dialog, _("Getting the number of new messages (STAT)..."));
940 case POP3_GETRANGE_LAST_SEND:
941 progress_dialog_set_label
942 (dialog, _("Getting the number of new messages (LAST)..."));
944 case POP3_GETRANGE_UIDL_SEND:
945 progress_dialog_set_label
946 (dialog, _("Getting the number of new messages (UIDL)..."));
948 case POP3_GETSIZE_LIST_SEND:
949 progress_dialog_set_label
950 (dialog, _("Getting the size of messages (LIST)..."));
953 g_snprintf(buf, sizeof(buf),
954 _("Retrieving header (%d / %d)"),
955 state->cur_msg, state->count);
956 progress_dialog_set_label (dialog, buf);
957 progress_dialog_set_percentage
959 (gfloat)(state->cur_msg) /
960 (gfloat)(state->count));
961 gtk_progress_bar_update
962 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar),
963 (gfloat)(state->cur_msg) /
964 (gfloat)(state->count));
967 Xstrdup_a(total_size, to_human_readable(state->total_bytes), return);
968 g_snprintf(buf, sizeof(buf),
969 _("Retrieving message (%d / %d) (%s / %s)"),
970 state->cur_msg, state->count,
971 to_human_readable(state->cur_total_bytes),
973 progress_dialog_set_label(dialog, buf);
974 progress_dialog_set_percentage
976 (gfloat)(state->cur_total_bytes) /
977 (gfloat)(state->total_bytes));
978 gtk_progress_bar_update
979 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar),
980 (gfloat)(state->cur_total_bytes) /
981 (gfloat)(state->total_bytes));
983 case POP3_DELETE_SEND:
984 if (state->msg[state->cur_msg].recv_time < state->current_time) {
985 g_snprintf(buf, sizeof(buf), _("Deleting message %d"),
987 progress_dialog_set_label(dialog, buf);
990 case POP3_LOGOUT_SEND:
991 progress_dialog_set_label(dialog, _("Quitting"));
998 gint inc_drop_message(const gchar *file, Pop3State *state)
1001 FolderItem *dropfolder;
1005 /* CLAWS: get default inbox (perhaps per account) */
1006 if (state->ac_prefs->inbox) {
1007 /* CLAWS: get destination folder / mailbox */
1008 inbox = folder_find_item_from_identifier
1009 (state->ac_prefs->inbox);
1011 inbox = folder_get_default_inbox();
1013 inbox = folder_get_default_inbox();
1019 /* CLAWS: claws uses a global .processing folder for the filtering. */
1020 if (global_processing == NULL) {
1021 if (state->ac_prefs->filter_on_recv) {
1023 filter_get_dest_folder(prefs_common.fltlist, file);
1024 if (!dropfolder) dropfolder = inbox;
1025 else if (!strcmp(dropfolder->path, FILTER_NOT_RECEIVE)) {
1026 g_warning(_("a message won't be received\n"));
1032 dropfolder = folder_get_default_processing();
1035 val = GPOINTER_TO_INT(g_hash_table_lookup
1036 (state->folder_table, dropfolder));
1038 folder_item_scan(dropfolder);
1039 /* force updating */
1040 if (FOLDER_IS_LOCAL(dropfolder->folder))
1041 dropfolder->mtime = 0;
1042 g_hash_table_insert(state->folder_table, dropfolder,
1043 GINT_TO_POINTER(1));
1046 /* add msg file to drop folder */
1047 if ((msgnum = folder_item_add_msg(dropfolder, file, TRUE)) < 0) {
1055 static void inc_put_error(IncState istate)
1059 if (!prefs_common.no_recv_err_panel)
1061 (_("Error occurred while processing mail."));
1064 alertpanel_error(_("No disk space left."));
1071 static void inc_cancel(IncProgressDialog *dialog)
1073 IncSession *session;
1076 g_return_if_fail(dialog != NULL);
1078 session = dialog->queue_list->data;
1079 sockinfo = session->pop3_state->sockinfo;
1081 if (!sockinfo || session->atm->terminated == TRUE) return;
1083 session->pop3_state->inc_state = INC_CANCEL;
1084 pop3_automaton_terminate(sockinfo, session->atm);
1085 session->pop3_state->sockinfo = NULL;
1088 gboolean inc_is_active(void)
1090 return (inc_dialog_list != NULL);
1093 void inc_cancel_all(void)
1097 for (cur = inc_dialog_list; cur != NULL; cur = cur->next)
1098 inc_cancel((IncProgressDialog *)cur->data);
1101 static void inc_cancel_cb(GtkWidget *widget, gpointer data)
1103 inc_cancel((IncProgressDialog *)data);
1106 static gint inc_spool(void)
1108 gchar *mbox, *logname;
1111 logname = g_get_user_name();
1112 mbox = g_strconcat(prefs_common.spool_path
1113 ? prefs_common.spool_path : DEFAULT_SPOOL_PATH,
1114 G_DIR_SEPARATOR_S, logname, NULL);
1115 msgs = get_spool(folder_get_default_inbox(), mbox);
1121 static void inc_spool_account(PrefsAccount *account)
1125 if (account->inbox) {
1126 inbox = folder_find_item_from_path(account->inbox);
1128 inbox = folder_get_default_inbox();
1130 inbox = folder_get_default_inbox();
1132 get_spool(inbox, account->local_mbox);
1135 static void inc_all_spool(void)
1139 list = account_get_list();
1142 for (; list != NULL; list = list->next) {
1143 PrefsAccount *account = list->data;
1145 if ((account->protocol == A_LOCAL) &&
1146 (account->recv_at_getall))
1147 inc_spool_account(account);
1151 static gint get_spool(FolderItem *dest, const gchar *mbox)
1155 gchar tmp_mbox[MAXPATHLEN + 1];
1156 GHashTable *folder_table = NULL;
1158 g_return_val_if_fail(dest != NULL, -1);
1159 g_return_val_if_fail(mbox != NULL, -1);
1161 if (!is_file_exist(mbox) || (size = get_file_size(mbox)) == 0) {
1162 debug_print("no messages in local mailbox.\n");
1164 } else if (size < 0)
1167 if ((lockfd = lock_mbox(mbox, LOCK_FLOCK)) < 0)
1170 g_snprintf(tmp_mbox, sizeof(tmp_mbox), "%s%ctmpmbox.%08x",
1171 get_tmp_dir(), G_DIR_SEPARATOR, (gint)mbox);
1173 if (copy_mbox(mbox, tmp_mbox) < 0) {
1174 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1178 debug_print("Getting new messages from %s into %s...\n",
1181 if (prefs_common.filter_on_inc)
1182 folder_table = g_hash_table_new(NULL, NULL);
1183 msgs = proc_mbox(dest, tmp_mbox, folder_table);
1186 if (msgs >= 0) empty_mbox(mbox);
1187 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1190 if (!prefs_common.scan_all_after_inc) {
1191 g_hash_table_insert(folder_table, dest,
1192 GINT_TO_POINTER(1));
1193 folderview_update_item_foreach(folder_table, TRUE);
1195 g_hash_table_destroy(folder_table);
1196 } else if (!prefs_common.scan_all_after_inc) {
1197 folderview_update_item(dest, TRUE);
1208 void inc_unlock(void)
1210 if (inc_lock_count > 0)
1214 static guint autocheck_timer = 0;
1215 static gpointer autocheck_data = NULL;
1217 static void inc_notify_cmd(gint new_msgs, gboolean notify)
1222 if (!(new_msgs && notify && prefs_common.newmail_notify_cmd &&
1223 *prefs_common.newmail_notify_cmd))
1225 if ((buf = strchr(prefs_common.newmail_notify_cmd, '%')) &&
1226 buf[1] == 'd' && !strchr(&buf[1], '%'))
1227 buf = g_strdup_printf(prefs_common.newmail_notify_cmd,
1230 buf = g_strdup(prefs_common.newmail_notify_cmd);
1232 execute_command_line(buf, TRUE);
1237 void inc_autocheck_timer_init(MainWindow *mainwin)
1239 autocheck_data = mainwin;
1240 inc_autocheck_timer_set();
1243 static void inc_autocheck_timer_set_interval(guint interval)
1245 inc_autocheck_timer_remove();
1246 /* last test is to avoid re-enabling auto_check after modifying
1247 the common preferences */
1248 if (prefs_common.autochk_newmail && autocheck_data
1249 && prefs_common.work_offline == FALSE) {
1250 autocheck_timer = gtk_timeout_add
1251 (interval, inc_autocheck_func, autocheck_data);
1252 debug_print("added timer = %d\n", autocheck_timer);
1256 void inc_autocheck_timer_set(void)
1258 inc_autocheck_timer_set_interval(prefs_common.autochk_itv * 60000);
1261 void inc_autocheck_timer_remove(void)
1263 if (autocheck_timer) {
1264 debug_print("removed timer = %d\n", autocheck_timer);
1265 gtk_timeout_remove(autocheck_timer);
1266 autocheck_timer = 0;
1270 static gint inc_autocheck_func(gpointer data)
1272 MainWindow *mainwin = (MainWindow *)data;
1274 if (inc_lock_count) {
1275 debug_print("autocheck is locked.\n");
1276 inc_autocheck_timer_set_interval(1000);
1280 inc_all_account_mail(mainwin, prefs_common.newmail_notify_auto);