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>
34 #include <sys/types.h>
43 #include "mainwindow.h"
44 #include "folderview.h"
45 #include "summaryview.h"
46 #include "prefs_common.h"
47 #include "prefs_account.h"
57 #include "statusbar.h"
58 #include "manage_window.h"
59 #include "stock_pixmap.h"
60 #include "progressdialog.h"
61 #include "inputdialog.h"
62 #include "alertpanel.h"
64 #include "automaton.h"
66 #include "filtering.h"
67 #include "selective_download.h"
69 static GList *inc_dialog_list = NULL;
71 static guint inc_lock_count = 0;
73 static GdkPixmap *currentxpm;
74 static GdkBitmap *currentxpmmask;
75 static GdkPixmap *errorxpm;
76 static GdkBitmap *errorxpmmask;
77 static GdkPixmap *okxpm;
78 static GdkBitmap *okxpmmask;
80 #define MSGBUFSIZE 8192
82 static void inc_finished (MainWindow *mainwin,
83 gboolean new_messages);
84 static gint inc_account_mail (PrefsAccount *account,
87 static IncProgressDialog *inc_progress_dialog_create (void);
88 static void inc_progress_dialog_destroy (IncProgressDialog *inc_dialog);
90 static IncSession *inc_session_new (PrefsAccount *account);
91 static void inc_session_destroy (IncSession *session);
92 static Pop3State *inc_pop3_state_new (PrefsAccount *account);
93 static void inc_pop3_state_destroy (Pop3State *state);
94 static gint inc_start (IncProgressDialog *inc_dialog);
95 static IncState inc_pop3_session_do (IncSession *session);
96 static gint pop3_automaton_terminate (SockInfo *source,
99 static GHashTable *inc_get_uidl_table (PrefsAccount *ac_prefs);
100 static void inc_write_uidl_list (Pop3State *state);
102 static gboolean inc_pop3_recv_func (SockInfo *sock,
107 static void inc_put_error (IncState istate);
109 static void inc_cancel_cb (GtkWidget *widget,
112 static gint inc_spool (void);
113 static gint get_spool (FolderItem *dest,
116 static void inc_spool_account(PrefsAccount *account);
117 static void inc_all_spool(void);
118 static void inc_autocheck_timer_set_interval (guint interval);
119 static gint inc_autocheck_func (gpointer data);
121 static void inc_notify_cmd (gint new_msgs,
124 #define FOLDER_SUMMARY_MISMATCH(f, s) \
125 (f) && (s) ? ((s)->newmsgs != (f)->new) || ((f)->unread != (s)->unread) || ((f)->total != (s)->messages) \
130 * @mainwin: Main window.
131 * @new_messages: TRUE if some messages have been received.
133 * Update the folder view and the summary view after receiving
134 * messages. If @new_messages is FALSE, this function avoids unneeded
137 static void inc_finished(MainWindow *mainwin, gboolean new_messages)
141 if (prefs_common.scan_all_after_inc)
142 folderview_check_new(NULL);
144 if (!new_messages && !prefs_common.scan_all_after_inc) return;
146 if (prefs_common.open_inbox_on_inc) {
147 item = cur_account && cur_account->inbox
148 ? folder_find_item_from_path(cur_account->inbox)
149 : folder_get_default_inbox();
150 if (FOLDER_SUMMARY_MISMATCH(item, mainwin->summaryview)) {
151 folderview_unselect(mainwin->folderview);
152 folderview_select(mainwin->folderview, item);
155 item = mainwin->summaryview->folder_item;
156 if (FOLDER_SUMMARY_MISMATCH(item, mainwin->summaryview)) {
157 folderview_unselect(mainwin->folderview);
158 folderview_select(mainwin->folderview, item);
163 void inc_mail(MainWindow *mainwin, gboolean notify)
167 if (inc_lock_count) return;
169 inc_autocheck_timer_remove();
170 summary_write_cache(mainwin->summaryview);
171 main_window_lock(mainwin);
173 if (prefs_common.use_extinc && prefs_common.extinc_cmd) {
174 /* external incorporating program */
175 if (execute_command_line(prefs_common.extinc_cmd, FALSE) < 0) {
176 main_window_unlock(mainwin);
177 inc_autocheck_timer_set();
181 if (prefs_common.inc_local)
182 new_msgs = inc_spool();
184 if (prefs_common.inc_local)
185 new_msgs = inc_spool();
187 new_msgs += inc_account_mail(cur_account, mainwin);
190 inc_finished(mainwin, new_msgs > 0);
191 main_window_unlock(mainwin);
192 inc_notify_cmd(new_msgs, notify);
193 inc_autocheck_timer_set();
196 gint inc_selective_download(MainWindow *mainwin, gint session_type)
198 PrefsAccount *account = cur_account;
201 account->session_type = session_type;
202 new_msgs = inc_account_mail(account, mainwin);
203 account->session_type = RETR_NORMAL;
208 static gint inc_account_mail(PrefsAccount *account, MainWindow *mainwin)
210 IncProgressDialog *inc_dialog;
214 switch (account->protocol) {
217 folderview_check_new(FOLDER(account->folder));
222 session = inc_session_new(account);
223 if (!session) return 0;
225 inc_dialog = inc_progress_dialog_create();
226 inc_dialog->queue_list = g_list_append(inc_dialog->queue_list,
228 inc_dialog->mainwin = mainwin;
229 session->data = inc_dialog;
232 text[1] = account->account_name;
233 text[2] = _("Standby");
234 gtk_clist_append(GTK_CLIST(inc_dialog->dialog->clist), text);
236 main_window_set_toolbar_sensitive(mainwin);
237 main_window_set_menu_sensitive(mainwin);
239 return inc_start(inc_dialog);
242 inc_spool_account(account);
247 void inc_all_account_mail(MainWindow *mainwin, gboolean notify)
249 GList *list, *queue_list = NULL;
250 IncProgressDialog *inc_dialog;
253 if (inc_lock_count) return;
255 inc_autocheck_timer_remove();
256 summary_write_cache(mainwin->summaryview);
257 main_window_lock(mainwin);
259 if (prefs_common.inc_local)
260 new_msgs = inc_spool();
262 list = account_get_list();
264 inc_finished(mainwin, new_msgs > 0);
265 main_window_unlock(mainwin);
266 inc_notify_cmd(new_msgs, notify);
267 inc_autocheck_timer_set();
271 /* check local folders */
274 /* check IMAP4 folders */
275 for (; list != NULL; list = list->next) {
276 PrefsAccount *account = list->data;
277 if ((account->protocol == A_IMAP4 ||
278 account->protocol == A_NNTP) && account->recv_at_getall)
279 folderview_check_new(FOLDER(account->folder));
282 /* check POP3 accounts */
283 for (list = account_get_list(); list != NULL; list = list->next) {
285 PrefsAccount *account = list->data;
286 account->session_type = RETR_NORMAL;
287 if (account->recv_at_getall) {
288 session = inc_session_new(account);
290 queue_list = g_list_append(queue_list, session);
295 inc_finished(mainwin, new_msgs > 0);
296 main_window_unlock(mainwin);
297 inc_notify_cmd(new_msgs, notify);
298 inc_autocheck_timer_set();
302 inc_dialog = inc_progress_dialog_create();
303 inc_dialog->queue_list = queue_list;
304 inc_dialog->mainwin = mainwin;
305 for (list = queue_list; list != NULL; list = list->next) {
306 IncSession *session = list->data;
309 session->data = inc_dialog;
312 text[1] = session->pop3_state->ac_prefs->account_name;
313 text[2] = _("Standby");
314 gtk_clist_append(GTK_CLIST(inc_dialog->dialog->clist), text);
317 main_window_set_toolbar_sensitive(mainwin);
318 main_window_set_menu_sensitive(mainwin);
320 new_msgs += inc_start(inc_dialog);
322 inc_finished(mainwin, new_msgs > 0);
323 main_window_unlock(mainwin);
324 inc_notify_cmd(new_msgs, notify);
325 inc_autocheck_timer_set();
328 static IncProgressDialog *inc_progress_dialog_create(void)
330 IncProgressDialog *dialog;
331 ProgressDialog *progress;
333 dialog = g_new0(IncProgressDialog, 1);
335 progress = progress_dialog_create();
336 gtk_window_set_title(GTK_WINDOW(progress->window),
337 _("Retrieving new messages"));
338 gtk_signal_connect(GTK_OBJECT(progress->cancel_btn), "clicked",
339 GTK_SIGNAL_FUNC(inc_cancel_cb), dialog);
340 gtk_signal_connect(GTK_OBJECT(progress->window), "delete_event",
341 GTK_SIGNAL_FUNC(gtk_true), NULL);
342 /* manage_window_set_transient(GTK_WINDOW(progress->window)); */
344 progress_dialog_set_value(progress, 0.0);
346 stock_pixmap_gdk(progress->clist, STOCK_PIXMAP_COMPLETE,
348 stock_pixmap_gdk(progress->clist, STOCK_PIXMAP_CONTINUE,
349 ¤txpm, ¤txpmmask);
350 stock_pixmap_gdk(progress->clist, STOCK_PIXMAP_ERROR,
351 &errorxpm, &errorxpmmask);
353 if (prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS ||
354 (prefs_common.recv_dialog_mode == RECV_DIALOG_ACTIVE &&
355 manage_window_get_focus_window())) {
356 dialog->show_dialog = TRUE;
357 gtk_widget_show_now(progress->window);
360 dialog->dialog = progress;
361 dialog->queue_list = NULL;
363 inc_dialog_list = g_list_append(inc_dialog_list, dialog);
368 static void inc_progress_dialog_clear(IncProgressDialog *inc_dialog)
370 progress_dialog_set_value(inc_dialog->dialog, 0.0);
371 progress_dialog_set_label(inc_dialog->dialog, "");
372 gtk_progress_bar_update
373 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar), 0.0);
376 static void inc_progress_dialog_destroy(IncProgressDialog *inc_dialog)
378 g_return_if_fail(inc_dialog != NULL);
380 inc_dialog_list = g_list_remove(inc_dialog_list, inc_dialog);
382 gtk_progress_bar_update
383 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar), 0.0);
384 progress_dialog_destroy(inc_dialog->dialog);
389 static IncSession *inc_session_new(PrefsAccount *account)
393 g_return_val_if_fail(account != NULL, NULL);
395 if (account->protocol != A_POP3 && account->protocol != A_APOP)
397 if (!account->recv_server || !account->userid)
400 session = g_new0(IncSession, 1);
401 session->pop3_state = inc_pop3_state_new(account);
402 session->pop3_state->session = session;
407 static void inc_session_destroy(IncSession *session)
409 g_return_if_fail(session != NULL);
411 inc_pop3_state_destroy(session->pop3_state);
415 static Pop3State *inc_pop3_state_new(PrefsAccount *account)
419 state = g_new0(Pop3State, 1);
421 state->ac_prefs = account;
422 state->folder_table = g_hash_table_new(NULL, NULL);
423 state->uidl_todelete_list = NULL;
424 state->uidl_table = inc_get_uidl_table(account);
425 state->inc_state = INC_SUCCESS;
430 static void inc_pop3_state_destroy(Pop3State *state)
434 g_hash_table_destroy(state->folder_table);
436 for (n = 1; n <= state->count; n++)
437 g_free(state->msg[n].uidl);
440 if (state->uidl_table) {
441 hash_free_strings(state->uidl_table);
442 g_hash_table_destroy(state->uidl_table);
444 g_slist_free(state->uidl_todelete_list);
445 g_free(state->greeting);
448 g_free(state->prev_folder);
453 static gint inc_start(IncProgressDialog *inc_dialog)
456 GtkCList *clist = GTK_CLIST(inc_dialog->dialog->clist);
457 Pop3State *pop3_state;
463 while (inc_dialog->queue_list != NULL) {
464 session = inc_dialog->queue_list->data;
465 pop3_state = session->pop3_state;
467 inc_progress_dialog_clear(inc_dialog);
469 gtk_clist_moveto(clist, num, -1, 1.0, 0.0);
471 pop3_state->user = g_strdup(pop3_state->ac_prefs->userid);
472 if (pop3_state->ac_prefs->passwd)
474 g_strdup(pop3_state->ac_prefs->passwd);
475 else if (pop3_state->ac_prefs->tmp_pass)
477 g_strdup(pop3_state->ac_prefs->tmp_pass);
481 pass = input_dialog_query_password
482 (pop3_state->ac_prefs->recv_server,
485 if (inc_dialog->show_dialog)
486 manage_window_focus_in
487 (inc_dialog->mainwin->window,
490 pop3_state->ac_prefs->tmp_pass = g_strdup(pass);
491 pop3_state->pass = pass;
493 inc_session_destroy(session);
494 inc_dialog->queue_list = g_list_remove
495 (inc_dialog->queue_list, session);
500 gtk_clist_set_pixmap(clist, num, 0, currentxpm, currentxpmmask);
501 gtk_clist_set_text(clist, num, 2, _("Retrieving"));
503 /* begin POP3 session */
504 inc_state = inc_pop3_session_do(session);
506 if (inc_state == INC_SUCCESS) {
507 gtk_clist_set_pixmap(clist, num, 0, okxpm, okxpmmask);
508 gtk_clist_set_text(clist, num, 2, _("Done"));
509 } else if (inc_state == INC_CANCEL) {
510 gtk_clist_set_pixmap(clist, num, 0, okxpm, okxpmmask);
511 gtk_clist_set_text(clist, num, 2, _("Cancelled"));
513 gtk_clist_set_pixmap(clist, num, 0, errorxpm, errorxpmmask);
514 if (inc_state == INC_CONNECT_ERROR)
515 gtk_clist_set_text(clist, num, 2,
516 _("Connection failed"));
517 else if (inc_state == INC_AUTH_FAILED)
518 gtk_clist_set_text(clist, num, 2,
521 gtk_clist_set_text(clist, num, 2, _("Error"));
524 if (pop3_state->error_val == PS_AUTHFAIL) {
525 if(!prefs_common.noerrorpanel) {
526 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
527 ((prefs_common.recv_dialog_mode == RECV_DIALOG_ACTIVE) && focus_window)) {
528 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
531 (_("Authorization for %s on %s failed"),
533 pop3_state->ac_prefs->recv_server);
539 /* CLAWS: perform filtering actions on dropped message */
540 if (global_processing != NULL) {
541 FolderItem *processing, *inbox;
544 GSList *msglist, *msglist_element;
546 /* CLAWS: get default inbox (perhaps per account) */
547 if (pop3_state->ac_prefs->inbox) {
548 /* CLAWS: get destination folder / mailbox */
549 inbox = folder_find_item_from_identifier(pop3_state->ac_prefs->inbox);
551 inbox = folder_get_default_inbox();
553 inbox = folder_get_default_inbox();
555 /* get list of messages in processing */
556 processing = folder_get_default_processing();
557 folder_item_scan(processing);
558 folder = processing->folder;
559 msglist = folder->get_msg_list(folder, processing, FALSE);
561 /* process messages */
562 for(msglist_element = msglist; msglist_element != NULL; msglist_element = msglist_element->next) {
563 msginfo = (MsgInfo *) msglist_element->data;
564 /* filter if enabled in prefs or move to inbox if not */
565 if(pop3_state->ac_prefs->filter_on_recv) {
566 filter_message_by_msginfo_with_inbox(global_processing, msginfo,
567 pop3_state->folder_table,
570 folder_item_move_msg(inbox, msginfo);
571 g_hash_table_insert(pop3_state->folder_table, inbox,
574 procmsg_msginfo_free(msginfo);
576 g_slist_free(msglist);
580 new_msgs += pop3_state->cur_total_num;
582 if (!prefs_common.scan_all_after_inc) {
583 folder_item_scan_foreach(pop3_state->folder_table);
584 folderview_update_item_foreach
585 (pop3_state->folder_table);
588 if (pop3_state->error_val == PS_AUTHFAIL &&
589 pop3_state->ac_prefs->tmp_pass) {
590 g_free(pop3_state->ac_prefs->tmp_pass);
591 pop3_state->ac_prefs->tmp_pass = NULL;
594 inc_write_uidl_list(pop3_state);
596 if (inc_state != INC_SUCCESS && inc_state != INC_CANCEL) {
598 if (inc_state == INC_NOSPACE) {
599 inc_put_error(inc_state);
604 inc_session_destroy(session);
605 inc_dialog->queue_list =
606 g_list_remove(inc_dialog->queue_list, session);
611 if (error_num && !prefs_common.noerrorpanel) {
612 if (inc_dialog->show_dialog)
613 manage_window_focus_in(inc_dialog->dialog->window,
615 alertpanel_error(_("Some errors occured while getting mail."));
616 if (inc_dialog->show_dialog)
617 manage_window_focus_out(inc_dialog->dialog->window,
621 while (inc_dialog->queue_list != NULL) {
622 session = inc_dialog->queue_list->data;
623 inc_session_destroy(session);
624 inc_dialog->queue_list =
625 g_list_remove(inc_dialog->queue_list, session);
628 inc_progress_dialog_destroy(inc_dialog);
633 static IncState inc_pop3_session_do(IncSession *session)
635 Pop3State *pop3_state = session->pop3_state;
636 IncProgressDialog *inc_dialog = (IncProgressDialog *)session->data;
643 static AtmHandler handlers[] = {
645 pop3_getauth_user_send , pop3_getauth_user_recv,
646 pop3_getauth_pass_send , pop3_getauth_pass_recv,
647 pop3_getauth_apop_send , pop3_getauth_apop_recv,
648 pop3_getrange_stat_send , pop3_getrange_stat_recv,
649 pop3_getrange_last_send , pop3_getrange_last_recv,
650 pop3_getrange_uidl_send , pop3_getrange_uidl_recv,
651 pop3_getsize_list_send , pop3_getsize_list_recv,
652 pop3_top_send , pop3_top_recv,
653 pop3_retr_send , pop3_retr_recv,
654 pop3_delete_send , pop3_delete_recv,
655 pop3_logout_send , pop3_logout_recv
658 debug_print(_("getting new messages of account %s...\n"),
659 pop3_state->ac_prefs->account_name);
661 atm = automaton_create(N_POP3_PHASE);
664 atm->data = pop3_state;
666 buf = g_strdup_printf(_("%s: Retrieving new messages"),
667 pop3_state->ac_prefs->recv_server);
668 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window), buf);
671 for (i = POP3_GREETING_RECV; i < N_POP3_PHASE; i++)
672 atm->state[i].handler = handlers[i];
673 atm->state[POP3_GREETING_RECV].condition = GDK_INPUT_READ;
674 for (i = POP3_GETAUTH_USER_SEND; i < N_POP3_PHASE; ) {
675 atm->state[i++].condition = GDK_INPUT_WRITE;
676 atm->state[i++].condition = GDK_INPUT_READ;
679 atm->terminate = (AtmHandler)pop3_automaton_terminate;
681 atm->num = POP3_GREETING_RECV;
683 server = pop3_state->ac_prefs->recv_server;
685 port = pop3_state->ac_prefs->set_popport ?
686 pop3_state->ac_prefs->popport :
687 pop3_state->ac_prefs->ssl_pop ? 995 : 110;
689 port = pop3_state->ac_prefs->set_popport ?
690 pop3_state->ac_prefs->popport : 110;
693 buf = g_strdup_printf(_("Connecting to POP3 server: %s ..."), server);
694 log_message("%s\n", buf);
695 progress_dialog_set_label(inc_dialog->dialog, buf);
700 if ((sockinfo = sock_connect(server, port)) == NULL) {
701 log_warning(_("Can't connect to POP3 server: %s:%d\n"),
703 if(!prefs_common.noerrorpanel) {
704 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
705 ((prefs_common.recv_dialog_mode == RECV_DIALOG_ACTIVE) && focus_window)) {
706 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
708 alertpanel_error(_("Can't connect to POP3 server: %s:%d"),
710 manage_window_focus_out(inc_dialog->dialog->window, NULL, NULL);
712 pop3_automaton_terminate(NULL, atm);
713 automaton_destroy(atm);
714 return INC_CONNECT_ERROR;
718 if (pop3_state->ac_prefs->ssl_pop && !ssl_init_socket(sockinfo)) {
719 pop3_automaton_terminate(NULL, atm);
720 automaton_destroy(atm);
721 return INC_CONNECT_ERROR;
725 /* :WK: Hmmm, with the later sock_gdk_input, we have 2 references
726 * to the sock structure - implement a reference counter?? */
727 pop3_state->sockinfo = sockinfo;
728 atm->help_sock = sockinfo;
730 log_verbosity_set(TRUE);
731 recv_set_ui_func(inc_pop3_recv_func, session);
733 atm->tag = sock_gdk_input_add(sockinfo,
734 atm->state[atm->num].condition,
735 automaton_input_cb, atm);
737 while (!atm->terminated)
738 gtk_main_iteration();
740 log_verbosity_set(FALSE);
741 recv_set_ui_func(NULL, NULL);
743 automaton_destroy(atm);
745 return pop3_state->inc_state;
748 static gint pop3_automaton_terminate(SockInfo *source, Automaton *atm)
750 if (atm->terminated) return 0;
753 gdk_input_remove(atm->tag);
756 if (atm->timeout_tag > 0) {
757 gtk_timeout_remove(atm->timeout_tag);
758 atm->timeout_tag = 0;
763 atm->terminated = TRUE;
768 static GHashTable *inc_get_uidl_table(PrefsAccount *ac_prefs)
773 gchar buf[IDLEN + 3];
775 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
776 "uidl-", ac_prefs->recv_server,
777 "-", ac_prefs->userid, NULL);
778 if ((fp = fopen(path, "rb")) == NULL) {
779 if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
785 table = g_hash_table_new(g_str_hash, g_str_equal);
787 while (fgets(buf, sizeof(buf), fp) != NULL) {
789 GDate *curdate = g_date_new();
791 g_date_set_time(curdate, time(NULL));
794 /* data[0] will contain uidl
795 data[1] will contain day of retrieval */
796 if( strchr(buf, '\t') ) {
797 data = g_strsplit(buf,"\t",2);
798 snprintf(data[1], sizeof(data[1]), "%d", g_date_day_of_year(curdate) );
801 data[0] = g_strdup(buf);
802 snprintf(data[1], sizeof(data[1]), "%d", g_date_day_of_year(curdate) );
804 g_hash_table_insert(table, g_strdup(data[0]), g_strdup(data[1]));
812 static void inc_write_uidl_list(Pop3State *state)
817 GDate *curdate = g_date_new();
818 g_date_set_time(curdate, time(NULL));
819 if (!state->uidl_is_valid) return;
821 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
822 "uidl-", state->ac_prefs->recv_server,
823 "-", state->user, NULL);
824 if ((fp = fopen(path, "wb")) == NULL) {
825 FILE_OP_ERROR(path, "fopen");
830 for (n = 1; n <= state->count; n++) {
831 if (state->msg[n].uidl && state->msg[n].received &&
832 !state->msg[n].deleted) {
833 if (fputs(state->msg[n].uidl, fp) == EOF) {
834 FILE_OP_ERROR(path, "fputs");
837 if (fputc('\t', fp) == EOF) {
838 FILE_OP_ERROR(path, "fputc");
841 if(g_hash_table_lookup(state->uidl_table, state->msg[n].uidl ) != NULL) {
842 const char *sdate = g_hash_table_lookup(state->uidl_table, state->msg[n].uidl);
843 int tdate = g_date_day_of_year(curdate);
847 if(fprintf(fp, "%3d", tdate) == EOF) {
848 FILE_OP_ERROR(path, "fprintf");
852 if(fprintf(fp, "%d", g_date_day_of_year(curdate)) == EOF) {
853 FILE_OP_ERROR(path, "fputs");
859 if (fputc('\n', fp) == EOF) {
860 FILE_OP_ERROR(path, "fputc");
867 if (fclose(fp) == EOF) FILE_OP_ERROR(path, "fclose");
871 static gboolean inc_pop3_recv_func(SockInfo *sock, gint count, gint read_bytes,
874 gchar buf[MSGBUFSIZE];
875 IncSession *session = (IncSession *)data;
876 Pop3State *state = session->pop3_state;
877 IncProgressDialog *inc_dialog = session->data;
878 ProgressDialog *dialog = inc_dialog->dialog;
882 cur_total = state->cur_total_bytes + read_bytes;
883 if (cur_total > state->total_bytes)
884 cur_total = state->total_bytes;
886 Xstrdup_a(total_size, to_human_readable(state->total_bytes),
888 g_snprintf(buf, sizeof(buf),
889 _("Retrieving message (%d / %d) (%s / %s)"),
890 state->cur_msg, state->count,
891 to_human_readable(cur_total), total_size);
892 progress_dialog_set_label(dialog, buf);
894 progress_dialog_set_percentage
895 (dialog, (gfloat)cur_total / (gfloat)state->total_bytes);
896 gtk_progress_bar_update
897 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar),
898 (gfloat)cur_total / (gfloat)state->total_bytes);
901 if (state->inc_state == INC_CANCEL)
907 void inc_progress_update(Pop3State *state, Pop3Phase phase)
909 gchar buf[MSGBUFSIZE];
910 IncProgressDialog *inc_dialog = state->session->data;
911 ProgressDialog *dialog = inc_dialog->dialog;
915 case POP3_GREETING_RECV:
917 case POP3_GETAUTH_USER_SEND:
918 case POP3_GETAUTH_USER_RECV:
919 case POP3_GETAUTH_PASS_SEND:
920 case POP3_GETAUTH_PASS_RECV:
921 case POP3_GETAUTH_APOP_SEND:
922 case POP3_GETAUTH_APOP_RECV:
923 progress_dialog_set_label(dialog, _("Authenticating..."));
925 case POP3_GETRANGE_STAT_SEND:
926 case POP3_GETRANGE_STAT_RECV:
927 progress_dialog_set_label
928 (dialog, _("Getting the number of new messages (STAT)..."));
930 case POP3_GETRANGE_LAST_SEND:
931 case POP3_GETRANGE_LAST_RECV:
932 progress_dialog_set_label
933 (dialog, _("Getting the number of new messages (LAST)..."));
935 case POP3_GETRANGE_UIDL_SEND:
936 case POP3_GETRANGE_UIDL_RECV:
937 progress_dialog_set_label
938 (dialog, _("Getting the number of new messages (UIDL)..."));
940 case POP3_GETSIZE_LIST_SEND:
941 case POP3_GETSIZE_LIST_RECV:
942 progress_dialog_set_label
943 (dialog, _("Getting the size of messages (LIST)..."));
947 g_snprintf(buf, sizeof(buf),
948 _("Retrieving header (%d / %d)"),
949 state->cur_msg, state->count);
950 progress_dialog_set_label (dialog, buf);
951 progress_dialog_set_percentage
953 (gfloat)(state->cur_msg) /
954 (gfloat)(state->count));
955 gtk_progress_bar_update
956 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar),
957 (gfloat)(state->cur_msg) /
958 (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 case POP3_DELETE_RECV:
980 progress_dialog_set_label(dialog, _("Deleting message"));
982 case POP3_LOGOUT_SEND:
983 case POP3_LOGOUT_RECV:
984 progress_dialog_set_label(dialog, _("Quitting"));
991 gint inc_drop_message(const gchar *file, Pop3State *state)
994 FolderItem *dropfolder;
998 /* CLAWS: get default inbox (perhaps per account) */
999 if (state->ac_prefs->inbox) {
1000 /* CLAWS: get destination folder / mailbox */
1001 inbox = folder_find_item_from_identifier(state->ac_prefs->inbox);
1003 inbox = folder_get_default_inbox();
1005 inbox = folder_get_default_inbox();
1011 /* CLAWS: claws uses a global .processing folder for the filtering. */
1012 if (global_processing == NULL) {
1013 if (state->ac_prefs->filter_on_recv) {
1015 filter_get_dest_folder(prefs_common.fltlist, file);
1016 if (!dropfolder) dropfolder = inbox;
1017 else if (!strcmp(dropfolder->path, FILTER_NOT_RECEIVE)) {
1018 g_warning(_("a message won't be received\n"));
1024 dropfolder = folder_get_default_processing();
1027 val = GPOINTER_TO_INT(g_hash_table_lookup
1028 (state->folder_table, dropfolder));
1030 folder_item_scan(dropfolder);
1031 g_hash_table_insert(state->folder_table, dropfolder,
1032 GINT_TO_POINTER(1));
1035 /* add msg file to drop folder */
1036 if ((msgnum = folder_item_add_msg(dropfolder, file, TRUE)) < 0) {
1044 static void inc_put_error(IncState istate)
1048 if(!prefs_common.noerrorpanel) {
1049 alertpanel_error(_("Error occurred while processing mail."));
1053 alertpanel_error(_("No disk space left."));
1060 static void inc_cancel(IncProgressDialog *dialog)
1062 IncSession *session;
1065 g_return_if_fail(dialog != NULL);
1067 session = dialog->queue_list->data;
1068 sockinfo = session->pop3_state->sockinfo;
1070 if (!sockinfo || session->atm->terminated == TRUE) return;
1072 session->pop3_state->inc_state = INC_CANCEL;
1073 pop3_automaton_terminate(sockinfo, session->atm);
1074 session->pop3_state->sockinfo = NULL;
1077 gboolean inc_is_active(void)
1079 return (inc_dialog_list != NULL);
1082 void inc_cancel_all(void)
1086 for (cur = inc_dialog_list; cur != NULL; cur = cur->next)
1087 inc_cancel((IncProgressDialog *)cur->data);
1090 static void inc_cancel_cb(GtkWidget *widget, gpointer data)
1092 inc_cancel((IncProgressDialog *)data);
1095 static gint inc_spool(void)
1097 gchar *mbox, *logname;
1100 logname = g_get_user_name();
1101 mbox = g_strconcat(prefs_common.spool_path
1102 ? prefs_common.spool_path : DEFAULT_SPOOL_PATH,
1103 G_DIR_SEPARATOR_S, logname, NULL);
1104 msgs = get_spool(folder_get_default_inbox(), mbox);
1110 static void inc_spool_account(PrefsAccount *account)
1113 FolderItem *dropfolder;
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 IncSession *session;
1135 PrefsAccount *account = list->data;
1137 if ((account->protocol == A_LOCAL) &&
1138 (account->recv_at_getall))
1139 inc_spool_account(account);
1143 static gint get_spool(FolderItem *dest, const gchar *mbox)
1147 gchar tmp_mbox[MAXPATHLEN + 1];
1148 GHashTable *folder_table = NULL;
1150 g_return_val_if_fail(dest != NULL, -1);
1151 g_return_val_if_fail(mbox != NULL, -1);
1153 if (!is_file_exist(mbox) || (size = get_file_size(mbox)) == 0) {
1154 debug_print(_("no messages in local mailbox.\n"));
1156 } else if (size < 0)
1159 if ((lockfd = lock_mbox(mbox, LOCK_FLOCK)) < 0)
1162 g_snprintf(tmp_mbox, sizeof(tmp_mbox), "%s%ctmpmbox%d",
1163 get_rc_dir(), G_DIR_SEPARATOR, (gint)mbox);
1165 if (copy_mbox(mbox, tmp_mbox) < 0) {
1166 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1170 debug_print(_("Getting new messages from %s into %s...\n"),
1173 if (prefs_common.filter_on_inc)
1174 folder_table = g_hash_table_new(NULL, NULL);
1175 msgs = proc_mbox(dest, tmp_mbox, folder_table);
1178 if (msgs >= 0) empty_mbox(mbox);
1179 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1182 if (!prefs_common.scan_all_after_inc) {
1183 g_hash_table_insert(folder_table, dest,
1184 GINT_TO_POINTER(1));
1185 folder_item_scan_foreach(folder_table);
1186 folderview_update_item_foreach(folder_table);
1188 g_hash_table_destroy(folder_table);
1189 } else if (!prefs_common.scan_all_after_inc) {
1190 folder_item_scan(dest);
1191 folderview_update_item(dest, FALSE);
1202 void inc_unlock(void)
1204 if (inc_lock_count > 0)
1208 static guint autocheck_timer = 0;
1209 static gpointer autocheck_data = NULL;
1211 static void inc_notify_cmd(gint new_msgs, gboolean notify)
1216 if (!(new_msgs && notify && prefs_common.newmail_notify_cmd &&
1217 *prefs_common.newmail_notify_cmd))
1219 if ((buf = strchr(prefs_common.newmail_notify_cmd, '%')) &&
1220 buf[1] == 'd' && !strchr(&buf[1], '%'))
1221 buf = g_strdup_printf(prefs_common.newmail_notify_cmd,
1224 buf = g_strdup(prefs_common.newmail_notify_cmd);
1231 void inc_autocheck_timer_init(MainWindow *mainwin)
1233 autocheck_data = mainwin;
1234 inc_autocheck_timer_set();
1237 static void inc_autocheck_timer_set_interval(guint interval)
1239 inc_autocheck_timer_remove();
1241 if (prefs_common.autochk_newmail && autocheck_data) {
1242 autocheck_timer = gtk_timeout_add
1243 (interval, inc_autocheck_func, autocheck_data);
1244 debug_print("added timer = %d\n", autocheck_timer);
1248 void inc_autocheck_timer_set(void)
1250 inc_autocheck_timer_set_interval(prefs_common.autochk_itv * 60000);
1253 void inc_autocheck_timer_remove(void)
1255 if (autocheck_timer) {
1256 debug_print("removed timer = %d\n", autocheck_timer);
1257 gtk_timeout_remove(autocheck_timer);
1258 autocheck_timer = 0;
1262 static gint inc_autocheck_func(gpointer data)
1264 MainWindow *mainwin = (MainWindow *)data;
1266 if (inc_lock_count) {
1267 debug_print("autocheck is locked.\n");
1268 inc_autocheck_timer_set_interval(1000);
1272 inc_all_account_mail(mainwin, prefs_common.newmail_notify_auto);