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();
193 if (prefs_common.inc_local)
194 new_msgs = inc_spool();
196 new_msgs += inc_account_mail(cur_account, mainwin);
199 inc_finished(mainwin, new_msgs > 0);
200 main_window_unlock(mainwin);
201 inc_notify_cmd(new_msgs, notify);
202 inc_autocheck_timer_set();
205 void inc_selective_download(MainWindow *mainwin, PrefsAccount *acc, gint session)
210 acc->session = session;
211 inc_account_mail(acc, mainwin);
212 acc->session = STYPE_NORMAL;
214 for (cur = acc->msg_list; cur != NULL; cur = cur->next) {
215 HeaderItems *items =(HeaderItems*)cur->data;
217 if (items->state == SD_DOWNLOADED &&
218 items->del_by_old_session == FALSE) {
224 inc_finished(mainwin, TRUE);
225 inc_notify_cmd(new_msgs, prefs_common.newmail_notify_manu);
229 static gint inc_account_mail(PrefsAccount *account, MainWindow *mainwin)
231 IncProgressDialog *inc_dialog;
235 switch (account->protocol) {
238 folderview_check_new(FOLDER(account->folder));
243 session = inc_session_new(account);
244 if (!session) return 0;
246 inc_dialog = inc_progress_dialog_create();
247 inc_dialog->queue_list = g_list_append(inc_dialog->queue_list,
249 inc_dialog->mainwin = mainwin;
250 session->data = inc_dialog;
253 text[1] = account->account_name;
254 text[2] = _("Standby");
255 gtk_clist_append(GTK_CLIST(inc_dialog->dialog->clist), text);
257 toolbar_set_sensitive(mainwin);
258 main_window_set_menu_sensitive(mainwin);
260 return inc_start(inc_dialog);
263 inc_spool_account(account);
269 void inc_all_account_mail(MainWindow *mainwin, gboolean notify)
271 GList *list, *queue_list = NULL;
272 IncProgressDialog *inc_dialog;
275 if (prefs_common.work_offline)
276 if (alertpanel(_("Offline warning"),
277 _("You're working offline. Override?"),
278 _("Yes"), _("No"), NULL) != G_ALERTDEFAULT)
281 if (inc_lock_count) return;
283 inc_autocheck_timer_remove();
284 main_window_lock(mainwin);
286 if (prefs_common.inc_local) {
287 new_msgs = inc_spool();
292 list = account_get_list();
294 inc_finished(mainwin, new_msgs > 0);
295 main_window_unlock(mainwin);
296 inc_notify_cmd(new_msgs, notify);
297 inc_autocheck_timer_set();
301 /* check local folders */
304 /* check IMAP4 folders */
305 for (; list != NULL; list = list->next) {
306 PrefsAccount *account = list->data;
307 if ((account->protocol == A_IMAP4 ||
308 account->protocol == A_NNTP) && account->recv_at_getall)
309 folderview_check_new(FOLDER(account->folder));
312 /* check POP3 accounts */
313 for (list = account_get_list(); list != NULL; list = list->next) {
315 PrefsAccount *account = list->data;
316 account->session = STYPE_NORMAL;
317 if (account->recv_at_getall) {
318 session = inc_session_new(account);
320 queue_list = g_list_append(queue_list, session);
325 inc_finished(mainwin, new_msgs > 0);
326 main_window_unlock(mainwin);
327 inc_notify_cmd(new_msgs, notify);
328 inc_autocheck_timer_set();
332 inc_dialog = inc_progress_dialog_create();
333 inc_dialog->queue_list = queue_list;
334 inc_dialog->mainwin = mainwin;
335 for (list = queue_list; list != NULL; list = list->next) {
336 IncSession *session = list->data;
339 session->data = inc_dialog;
342 text[1] = session->pop3_state->ac_prefs->account_name;
343 text[2] = _("Standby");
344 gtk_clist_append(GTK_CLIST(inc_dialog->dialog->clist), text);
347 toolbar_set_sensitive(mainwin);
348 main_window_set_menu_sensitive(mainwin);
350 new_msgs += inc_start(inc_dialog);
352 inc_finished(mainwin, new_msgs > 0);
353 main_window_unlock(mainwin);
354 inc_notify_cmd(new_msgs, notify);
355 inc_autocheck_timer_set();
358 static IncProgressDialog *inc_progress_dialog_create(void)
360 IncProgressDialog *dialog;
361 ProgressDialog *progress;
363 dialog = g_new0(IncProgressDialog, 1);
365 progress = progress_dialog_create();
366 gtk_window_set_title(GTK_WINDOW(progress->window),
367 _("Retrieving new messages"));
368 gtk_signal_connect(GTK_OBJECT(progress->cancel_btn), "clicked",
369 GTK_SIGNAL_FUNC(inc_cancel_cb), dialog);
370 gtk_signal_connect(GTK_OBJECT(progress->window), "delete_event",
371 GTK_SIGNAL_FUNC(gtk_true), NULL);
372 /* manage_window_set_transient(GTK_WINDOW(progress->window)); */
374 progress_dialog_set_value(progress, 0.0);
376 stock_pixmap_gdk(progress->clist, STOCK_PIXMAP_COMPLETE,
378 stock_pixmap_gdk(progress->clist, STOCK_PIXMAP_CONTINUE,
379 ¤txpm, ¤txpmmask);
380 stock_pixmap_gdk(progress->clist, STOCK_PIXMAP_ERROR,
381 &errorxpm, &errorxpmmask);
383 if (prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS ||
384 (prefs_common.recv_dialog_mode == RECV_DIALOG_ACTIVE &&
385 manage_window_get_focus_window())) {
386 dialog->show_dialog = TRUE;
387 gtk_widget_show_now(progress->window);
390 dialog->dialog = progress;
391 dialog->queue_list = NULL;
393 inc_dialog_list = g_list_append(inc_dialog_list, dialog);
398 static void inc_progress_dialog_clear(IncProgressDialog *inc_dialog)
400 progress_dialog_set_value(inc_dialog->dialog, 0.0);
401 progress_dialog_set_label(inc_dialog->dialog, "");
402 gtk_progress_bar_update
403 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar), 0.0);
406 static void inc_progress_dialog_destroy(IncProgressDialog *inc_dialog)
408 g_return_if_fail(inc_dialog != NULL);
410 inc_dialog_list = g_list_remove(inc_dialog_list, inc_dialog);
412 gtk_progress_bar_update
413 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar), 0.0);
414 progress_dialog_destroy(inc_dialog->dialog);
419 static IncSession *inc_session_new(PrefsAccount *account)
423 g_return_val_if_fail(account != NULL, NULL);
425 if (account->protocol != A_POP3 && account->protocol != A_APOP)
427 if (!account->recv_server || !account->userid)
430 session = g_new0(IncSession, 1);
431 session->pop3_state = inc_pop3_state_new(account);
432 session->pop3_state->session = session;
437 static void inc_session_destroy(IncSession *session)
439 g_return_if_fail(session != NULL);
441 inc_pop3_state_destroy(session->pop3_state);
445 static Pop3State *inc_pop3_state_new(PrefsAccount *account)
449 state = g_new0(Pop3State, 1);
451 state->ac_prefs = account;
452 state->folder_table = g_hash_table_new(NULL, NULL);
453 state->uidl_todelete_list = NULL;
454 state->uidl_table = inc_get_uidl_table(account);
455 state->inc_state = INC_SUCCESS;
456 state->current_time = time(NULL);
461 static void inc_pop3_state_destroy(Pop3State *state)
465 g_hash_table_destroy(state->folder_table);
467 for (n = 1; n <= state->count; n++)
468 g_free(state->msg[n].uidl);
471 if (state->uidl_table) {
472 hash_free_strings(state->uidl_table);
473 g_hash_table_destroy(state->uidl_table);
475 g_slist_free(state->uidl_todelete_list);
476 g_free(state->greeting);
479 g_free(state->prev_folder);
484 static gint inc_start(IncProgressDialog *inc_dialog)
487 GtkCList *clist = GTK_CLIST(inc_dialog->dialog->clist);
488 Pop3State *pop3_state;
494 while (inc_dialog->queue_list != NULL) {
495 session = inc_dialog->queue_list->data;
496 pop3_state = session->pop3_state;
498 inc_progress_dialog_clear(inc_dialog);
500 gtk_clist_moveto(clist, num, -1, 1.0, 0.0);
502 pop3_state->user = g_strdup(pop3_state->ac_prefs->userid);
503 if (pop3_state->ac_prefs->passwd)
505 g_strdup(pop3_state->ac_prefs->passwd);
506 else if (pop3_state->ac_prefs->tmp_pass)
508 g_strdup(pop3_state->ac_prefs->tmp_pass);
512 pass = input_dialog_query_password
513 (pop3_state->ac_prefs->recv_server,
516 if (inc_dialog->show_dialog)
517 manage_window_focus_in
518 (inc_dialog->mainwin->window,
521 pop3_state->ac_prefs->tmp_pass = g_strdup(pass);
522 pop3_state->pass = pass;
524 inc_session_destroy(session);
525 inc_dialog->queue_list = g_list_remove
526 (inc_dialog->queue_list, session);
531 gtk_clist_set_pixmap(clist, num, 0, currentxpm, currentxpmmask);
532 gtk_clist_set_text(clist, num, 2, _("Retrieving"));
534 /* begin POP3 session */
535 inc_state = inc_pop3_session_do(session);
537 if (inc_state == INC_SUCCESS) {
538 gtk_clist_set_pixmap(clist, num, 0, okxpm, okxpmmask);
539 gtk_clist_set_text(clist, num, 2, _("Done"));
540 } else if (inc_state == INC_CANCEL) {
541 gtk_clist_set_pixmap(clist, num, 0, okxpm, okxpmmask);
542 gtk_clist_set_text(clist, num, 2, _("Cancelled"));
544 gtk_clist_set_pixmap(clist, num, 0, errorxpm, errorxpmmask);
545 if (inc_state == INC_CONNECT_ERROR)
546 gtk_clist_set_text(clist, num, 2,
547 _("Connection failed"));
548 else if (inc_state == INC_AUTH_FAILED)
549 gtk_clist_set_text(clist, num, 2,
552 gtk_clist_set_text(clist, num, 2, _("Error"));
555 if (pop3_state->error_val == PS_AUTHFAIL) {
556 if(!prefs_common.no_recv_err_panel) {
557 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
558 ((prefs_common.recv_dialog_mode == RECV_DIALOG_ACTIVE) && focus_window)) {
559 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
562 (_("Authorization for %s on %s failed"),
564 pop3_state->ac_prefs->recv_server);
570 /* CLAWS: perform filtering actions on dropped message */
571 if (global_processing != NULL) {
572 FolderItem *processing, *inbox;
574 GSList *msglist, *msglist_element;
576 /* CLAWS: get default inbox (perhaps per account) */
577 if (pop3_state->ac_prefs->inbox) {
578 /* CLAWS: get destination folder / mailbox */
579 inbox = folder_find_item_from_identifier(pop3_state->ac_prefs->inbox);
581 inbox = folder_get_default_inbox();
583 inbox = folder_get_default_inbox();
585 /* get list of messages in processing */
586 processing = folder_get_default_processing();
587 folder_item_scan(processing);
588 msglist = folder_item_get_msg_list(processing);
590 /* process messages */
591 for(msglist_element = msglist; msglist_element != NULL; msglist_element = msglist_element->next) {
592 msginfo = (MsgInfo *) msglist_element->data;
593 /* filter if enabled in prefs or move to inbox if not */
594 if(pop3_state->ac_prefs->filter_on_recv) {
595 filter_message_by_msginfo_with_inbox(global_processing, msginfo,
596 pop3_state->folder_table,
599 folder_item_move_msg(inbox, msginfo);
600 g_hash_table_insert(pop3_state->folder_table, inbox,
603 procmsg_msginfo_free(msginfo);
605 g_slist_free(msglist);
609 new_msgs += pop3_state->cur_total_num;
611 folderview_update_item_foreach
612 (pop3_state->folder_table);
614 if (pop3_state->error_val == PS_AUTHFAIL &&
615 pop3_state->ac_prefs->tmp_pass) {
616 g_free(pop3_state->ac_prefs->tmp_pass);
617 pop3_state->ac_prefs->tmp_pass = NULL;
620 inc_write_uidl_list(pop3_state);
622 if (inc_state != INC_SUCCESS && inc_state != INC_CANCEL) {
624 if (inc_state == INC_NOSPACE) {
625 inc_put_error(inc_state);
630 inc_session_destroy(session);
631 inc_dialog->queue_list =
632 g_list_remove(inc_dialog->queue_list, session);
637 if (error_num && !prefs_common.no_recv_err_panel) {
638 if (inc_dialog->show_dialog)
639 manage_window_focus_in(inc_dialog->dialog->window,
641 alertpanel_error(_("Some errors occured while getting mail."));
642 if (inc_dialog->show_dialog)
643 manage_window_focus_out(inc_dialog->dialog->window,
647 while (inc_dialog->queue_list != NULL) {
648 session = inc_dialog->queue_list->data;
649 inc_session_destroy(session);
650 inc_dialog->queue_list =
651 g_list_remove(inc_dialog->queue_list, session);
654 inc_progress_dialog_destroy(inc_dialog);
659 static IncState inc_pop3_session_do(IncSession *session)
661 Pop3State *pop3_state = session->pop3_state;
662 IncProgressDialog *inc_dialog = (IncProgressDialog *)session->data;
669 static AtmHandler handlers[] = {
672 pop3_stls_send , pop3_stls_recv,
674 pop3_getauth_user_send , pop3_getauth_user_recv,
675 pop3_getauth_pass_send , pop3_getauth_pass_recv,
676 pop3_getauth_apop_send , pop3_getauth_apop_recv,
677 pop3_getrange_stat_send , pop3_getrange_stat_recv,
678 pop3_getrange_last_send , pop3_getrange_last_recv,
679 pop3_getrange_uidl_send , pop3_getrange_uidl_recv,
680 pop3_getsize_list_send , pop3_getsize_list_recv,
681 pop3_top_send , pop3_top_recv,
682 pop3_retr_send , pop3_retr_recv,
683 pop3_delete_send , pop3_delete_recv,
684 pop3_logout_send , pop3_logout_recv
687 debug_print(_("getting new messages of account %s...\n"),
688 pop3_state->ac_prefs->account_name);
690 atm = automaton_create(N_POP3_PHASE);
693 atm->data = pop3_state;
695 buf = g_strdup_printf(_("%s: Retrieving new messages"),
696 pop3_state->ac_prefs->recv_server);
697 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window), buf);
700 for (i = POP3_GREETING_RECV; i < N_POP3_PHASE; i++)
701 atm->state[i].handler = handlers[i];
702 atm->state[POP3_GREETING_RECV].condition = GDK_INPUT_READ;
703 for (i = POP3_GREETING_RECV + 1; i < N_POP3_PHASE; ) {
704 atm->state[i++].condition = GDK_INPUT_WRITE;
705 atm->state[i++].condition = GDK_INPUT_READ;
708 atm->terminate = (AtmHandler)pop3_automaton_terminate;
709 atm->ui_func = (AtmUIFunc)inc_progress_update;
711 atm->num = POP3_GREETING_RECV;
713 server = pop3_state->ac_prefs->recv_server;
715 port = pop3_state->ac_prefs->set_popport ?
716 pop3_state->ac_prefs->popport :
717 pop3_state->ac_prefs->ssl_pop == SSL_TUNNEL ? 995 : 110;
719 port = pop3_state->ac_prefs->set_popport ?
720 pop3_state->ac_prefs->popport : 110;
723 buf = g_strdup_printf(_("Connecting to POP3 server: %s ..."), server);
724 log_message("%s\n", buf);
725 progress_dialog_set_label(inc_dialog->dialog, buf);
730 if ((sockinfo = sock_connect(server, port)) == NULL) {
731 log_warning(_("Can't connect to POP3 server: %s:%d\n"),
733 if(!prefs_common.no_recv_err_panel) {
734 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
735 ((prefs_common.recv_dialog_mode == RECV_DIALOG_ACTIVE) && focus_window)) {
736 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
738 alertpanel_error(_("Can't connect to POP3 server: %s:%d"),
740 manage_window_focus_out(inc_dialog->dialog->window, NULL, NULL);
742 pop3_automaton_terminate(NULL, atm);
743 automaton_destroy(atm);
744 return INC_CONNECT_ERROR;
748 if (pop3_state->ac_prefs->ssl_pop == SSL_TUNNEL &&
749 !ssl_init_socket(sockinfo)) {
750 pop3_automaton_terminate(NULL, atm);
751 automaton_destroy(atm);
752 return INC_CONNECT_ERROR;
756 /* :WK: Hmmm, with the later sock_gdk_input, we have 2 references
757 * to the sock structure - implement a reference counter?? */
758 pop3_state->sockinfo = sockinfo;
759 atm->help_sock = sockinfo;
761 log_verbosity_set(TRUE);
762 /* oha: this messes up inc_progress update:
763 disabling this would avoid the label "Retrieve Header"
764 being overwritten by "Retrieve Message"
765 Setting inc_pop3_recv_func is not necessary
766 since atm already handles the progress dialog ui
769 recv_set_ui_func(inc_pop3_recv_func, session);
771 atm->tag = sock_gdk_input_add(sockinfo,
772 atm->state[atm->num].condition,
773 automaton_input_cb, atm);
775 while (!atm->terminated)
776 gtk_main_iteration();
778 log_verbosity_set(FALSE);
780 recv_set_ui_func(NULL, NULL);
782 automaton_destroy(atm);
784 return pop3_state->inc_state;
787 static gint pop3_automaton_terminate(SockInfo *source, Automaton *atm)
789 if (atm->terminated) return 0;
792 gdk_input_remove(atm->tag);
795 if (atm->timeout_tag > 0) {
796 gtk_timeout_remove(atm->timeout_tag);
797 atm->timeout_tag = 0;
802 atm->terminated = TRUE;
807 static GHashTable *inc_get_uidl_table(PrefsAccount *ac_prefs)
812 gchar buf[IDLEN + 3];
813 gchar uidl[IDLEN + 3];
817 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
818 "uidl", G_DIR_SEPARATOR_S, 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");
823 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
824 "uidl-", ac_prefs->recv_server,
825 "-", ac_prefs->userid, NULL);
826 if ((fp = fopen(path, "rb")) == NULL) {
827 if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
834 table = g_hash_table_new(g_str_hash, g_str_equal);
838 while (fgets(buf, sizeof(buf), fp) != NULL) {
841 if (sscanf(buf, "%s\t%ld", uidl, &recv_time) != 2) {
842 if (sscanf(buf, "%s", uidl) != 1)
849 g_hash_table_insert(table, g_strdup(uidl),
850 GINT_TO_POINTER(recv_time));
857 static void inc_write_uidl_list(Pop3State *state)
864 if (!state->uidl_is_valid) return;
866 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
867 "uidl", G_DIR_SEPARATOR_S,
868 state->ac_prefs->recv_server,
869 "-", state->ac_prefs->userid, NULL);
870 if ((fp = fopen(path, "wb")) == NULL) {
871 FILE_OP_ERROR(path, "fopen");
876 for (n = 1; n <= state->count; n++) {
877 msg = &state->msg[n];
878 if (msg->uidl && msg->received && !msg->deleted)
879 fprintf(fp, "%s\t%ld\n", msg->uidl, msg->recv_time);
882 if (fclose(fp) == EOF) FILE_OP_ERROR(path, "fclose");
886 static gboolean inc_pop3_recv_func(SockInfo *sock, gint count, gint read_bytes,
889 gchar buf[MSGBUFSIZE];
890 IncSession *session = (IncSession *)data;
891 Pop3State *state = session->pop3_state;
892 IncProgressDialog *inc_dialog = session->data;
893 ProgressDialog *dialog = inc_dialog->dialog;
897 cur_total = state->cur_total_bytes + read_bytes;
898 if (cur_total > state->total_bytes)
899 cur_total = state->total_bytes;
901 Xstrdup_a(total_size, to_human_readable(state->total_bytes),
903 g_snprintf(buf, sizeof(buf),
904 _("Retrieving message (%d / %d) (%s / %s)"),
905 state->cur_msg, state->count,
906 to_human_readable(cur_total), total_size);
907 progress_dialog_set_label(dialog, buf);
909 progress_dialog_set_percentage
910 (dialog, (gfloat)cur_total / (gfloat)state->total_bytes);
911 gtk_progress_bar_update
912 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar),
913 (gfloat)cur_total / (gfloat)state->total_bytes);
916 if (state->inc_state == INC_CANCEL)
922 void inc_progress_update(Pop3State *state, Pop3Phase phase)
924 gchar buf[MSGBUFSIZE];
925 IncProgressDialog *inc_dialog = state->session->data;
926 ProgressDialog *dialog = inc_dialog->dialog;
930 case POP3_GREETING_RECV:
932 case POP3_GETAUTH_USER_SEND:
933 case POP3_GETAUTH_PASS_SEND:
934 case POP3_GETAUTH_APOP_SEND:
935 progress_dialog_set_label(dialog, _("Authenticating..."));
937 case POP3_GETRANGE_STAT_SEND:
938 progress_dialog_set_label
939 (dialog, _("Getting the number of new messages (STAT)..."));
941 case POP3_GETRANGE_LAST_SEND:
942 progress_dialog_set_label
943 (dialog, _("Getting the number of new messages (LAST)..."));
945 case POP3_GETRANGE_UIDL_SEND:
946 progress_dialog_set_label
947 (dialog, _("Getting the number of new messages (UIDL)..."));
949 case POP3_GETSIZE_LIST_SEND:
950 progress_dialog_set_label
951 (dialog, _("Getting the size of messages (LIST)..."));
954 g_snprintf(buf, sizeof(buf),
955 _("Retrieving header (%d / %d)"),
956 state->cur_msg, state->count);
957 progress_dialog_set_label (dialog, buf);
958 progress_dialog_set_percentage
960 (gfloat)(state->cur_msg) /
961 (gfloat)(state->count));
962 gtk_progress_bar_update
963 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar),
964 (gfloat)(state->cur_msg) /
965 (gfloat)(state->count));
968 Xstrdup_a(total_size, to_human_readable(state->total_bytes), return);
969 g_snprintf(buf, sizeof(buf),
970 _("Retrieving message (%d / %d) (%s / %s)"),
971 state->cur_msg, state->count,
972 to_human_readable(state->cur_total_bytes),
974 progress_dialog_set_label(dialog, buf);
975 progress_dialog_set_percentage
977 (gfloat)(state->cur_total_bytes) /
978 (gfloat)(state->total_bytes));
979 gtk_progress_bar_update
980 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar),
981 (gfloat)(state->cur_total_bytes) /
982 (gfloat)(state->total_bytes));
984 case POP3_DELETE_SEND:
985 if (state->msg[state->cur_msg].recv_time < state->current_time) {
986 g_snprintf(buf, sizeof(buf), _("Deleting message %d"),
988 progress_dialog_set_label(dialog, buf);
991 case POP3_LOGOUT_SEND:
992 progress_dialog_set_label(dialog, _("Quitting"));
999 gint inc_drop_message(const gchar *file, Pop3State *state)
1002 FolderItem *dropfolder;
1006 /* CLAWS: get default inbox (perhaps per account) */
1007 if (state->ac_prefs->inbox) {
1008 /* CLAWS: get destination folder / mailbox */
1009 inbox = folder_find_item_from_identifier
1010 (state->ac_prefs->inbox);
1012 inbox = folder_get_default_inbox();
1014 inbox = folder_get_default_inbox();
1020 /* CLAWS: claws uses a global .processing folder for the filtering. */
1021 if (global_processing == NULL) {
1022 if (state->ac_prefs->filter_on_recv) {
1024 filter_get_dest_folder(prefs_common.fltlist, file);
1025 if (!dropfolder) dropfolder = inbox;
1026 else if (!strcmp(dropfolder->path, FILTER_NOT_RECEIVE)) {
1027 g_warning(_("a message won't be received\n"));
1033 dropfolder = folder_get_default_processing();
1036 val = GPOINTER_TO_INT(g_hash_table_lookup
1037 (state->folder_table, dropfolder));
1039 g_hash_table_insert(state->folder_table, dropfolder,
1040 GINT_TO_POINTER(1));
1043 /* add msg file to drop folder */
1044 if ((msgnum = folder_item_add_msg(dropfolder, file, TRUE)) < 0) {
1052 static void inc_put_error(IncState istate)
1056 if (!prefs_common.no_recv_err_panel)
1058 (_("Error occurred while processing mail."));
1061 alertpanel_error(_("No disk space left."));
1068 static void inc_cancel(IncProgressDialog *dialog)
1070 IncSession *session;
1073 g_return_if_fail(dialog != NULL);
1075 session = dialog->queue_list->data;
1076 sockinfo = session->pop3_state->sockinfo;
1078 if (!sockinfo || session->atm->terminated == TRUE) return;
1080 session->pop3_state->inc_state = INC_CANCEL;
1081 pop3_automaton_terminate(sockinfo, session->atm);
1082 session->pop3_state->sockinfo = NULL;
1085 gboolean inc_is_active(void)
1087 return (inc_dialog_list != NULL);
1090 void inc_cancel_all(void)
1094 for (cur = inc_dialog_list; cur != NULL; cur = cur->next)
1095 inc_cancel((IncProgressDialog *)cur->data);
1098 static void inc_cancel_cb(GtkWidget *widget, gpointer data)
1100 inc_cancel((IncProgressDialog *)data);
1103 static gint inc_spool(void)
1105 gchar *mbox, *logname;
1108 logname = g_get_user_name();
1109 mbox = g_strconcat(prefs_common.spool_path
1110 ? prefs_common.spool_path : DEFAULT_SPOOL_PATH,
1111 G_DIR_SEPARATOR_S, logname, NULL);
1112 msgs = get_spool(folder_get_default_inbox(), mbox);
1118 static void inc_spool_account(PrefsAccount *account)
1122 if (account->inbox) {
1123 inbox = folder_find_item_from_path(account->inbox);
1125 inbox = folder_get_default_inbox();
1127 inbox = folder_get_default_inbox();
1129 get_spool(inbox, account->local_mbox);
1132 static void inc_all_spool(void)
1136 list = account_get_list();
1139 for (; list != NULL; list = list->next) {
1140 PrefsAccount *account = list->data;
1142 if ((account->protocol == A_LOCAL) &&
1143 (account->recv_at_getall))
1144 inc_spool_account(account);
1148 static gint get_spool(FolderItem *dest, const gchar *mbox)
1152 gchar tmp_mbox[MAXPATHLEN + 1];
1153 GHashTable *folder_table = NULL;
1155 g_return_val_if_fail(dest != NULL, -1);
1156 g_return_val_if_fail(mbox != NULL, -1);
1158 if (!is_file_exist(mbox) || (size = get_file_size(mbox)) == 0) {
1159 debug_print(_("no messages in local mailbox.\n"));
1161 } else if (size < 0)
1164 if ((lockfd = lock_mbox(mbox, LOCK_FLOCK)) < 0)
1167 g_snprintf(tmp_mbox, sizeof(tmp_mbox), "%s%ctmpmbox%d",
1168 get_rc_dir(), G_DIR_SEPARATOR, (gint)mbox);
1170 if (copy_mbox(mbox, tmp_mbox) < 0) {
1171 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1175 debug_print(_("Getting new messages from %s into %s...\n"),
1178 if (prefs_common.filter_on_inc)
1179 folder_table = g_hash_table_new(NULL, NULL);
1180 msgs = proc_mbox(dest, tmp_mbox, folder_table);
1183 if (msgs >= 0) empty_mbox(mbox);
1184 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1187 if (!prefs_common.scan_all_after_inc) {
1188 g_hash_table_insert(folder_table, dest,
1189 GINT_TO_POINTER(1));
1190 folderview_update_item_foreach(folder_table);
1192 g_hash_table_destroy(folder_table);
1193 } else if (!prefs_common.scan_all_after_inc) {
1194 folderview_update_item(dest, FALSE);
1205 void inc_unlock(void)
1207 if (inc_lock_count > 0)
1211 static guint autocheck_timer = 0;
1212 static gpointer autocheck_data = NULL;
1214 static void inc_notify_cmd(gint new_msgs, gboolean notify)
1219 if (!(new_msgs && notify && prefs_common.newmail_notify_cmd &&
1220 *prefs_common.newmail_notify_cmd))
1222 if ((buf = strchr(prefs_common.newmail_notify_cmd, '%')) &&
1223 buf[1] == 'd' && !strchr(&buf[1], '%'))
1224 buf = g_strdup_printf(prefs_common.newmail_notify_cmd,
1227 buf = g_strdup(prefs_common.newmail_notify_cmd);
1229 execute_command_line(buf, TRUE);
1234 void inc_autocheck_timer_init(MainWindow *mainwin)
1236 autocheck_data = mainwin;
1237 inc_autocheck_timer_set();
1240 static void inc_autocheck_timer_set_interval(guint interval)
1242 inc_autocheck_timer_remove();
1243 /* last test is to avoid re-enabling auto_check after modifying
1244 the common preferences */
1245 if (prefs_common.autochk_newmail && autocheck_data
1246 && prefs_common.work_offline == FALSE) {
1247 autocheck_timer = gtk_timeout_add
1248 (interval, inc_autocheck_func, autocheck_data);
1249 debug_print("added timer = %d\n", autocheck_timer);
1253 void inc_autocheck_timer_set(void)
1255 inc_autocheck_timer_set_interval(prefs_common.autochk_itv * 60000);
1258 void inc_autocheck_timer_remove(void)
1260 if (autocheck_timer) {
1261 debug_print("removed timer = %d\n", autocheck_timer);
1262 gtk_timeout_remove(autocheck_timer);
1263 autocheck_timer = 0;
1267 static gint inc_autocheck_func(gpointer data)
1269 MainWindow *mainwin = (MainWindow *)data;
1271 if (inc_lock_count) {
1272 debug_print("autocheck is locked.\n");
1273 inc_autocheck_timer_set_interval(1000);
1277 inc_all_account_mail(mainwin, prefs_common.newmail_notify_auto);