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_identifier(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 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 summary_write_cache(mainwin->summaryview);
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);
265 void inc_all_account_mail(MainWindow *mainwin, gboolean notify)
267 GList *list, *queue_list = NULL;
268 IncProgressDialog *inc_dialog;
271 if (prefs_common.work_offline)
272 if (alertpanel(_("Offline warning"),
273 _("You're working offline. Override?"),
274 _("Yes"), _("No"), NULL) != G_ALERTDEFAULT)
277 if (inc_lock_count) return;
279 inc_autocheck_timer_remove();
280 summary_write_cache(mainwin->summaryview);
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;
454 static void inc_pop3_state_destroy(Pop3State *state)
458 g_hash_table_destroy(state->folder_table);
460 for (n = 1; n <= state->count; n++)
461 g_free(state->msg[n].uidl);
464 if (state->uidl_table) {
465 hash_free_strings(state->uidl_table);
466 g_hash_table_destroy(state->uidl_table);
468 g_slist_free(state->uidl_todelete_list);
469 g_free(state->greeting);
472 g_free(state->prev_folder);
477 static gint inc_start(IncProgressDialog *inc_dialog)
480 GtkCList *clist = GTK_CLIST(inc_dialog->dialog->clist);
481 Pop3State *pop3_state;
487 while (inc_dialog->queue_list != NULL) {
488 session = inc_dialog->queue_list->data;
489 pop3_state = session->pop3_state;
491 inc_progress_dialog_clear(inc_dialog);
493 gtk_clist_moveto(clist, num, -1, 1.0, 0.0);
495 pop3_state->user = g_strdup(pop3_state->ac_prefs->userid);
496 if (pop3_state->ac_prefs->passwd)
498 g_strdup(pop3_state->ac_prefs->passwd);
499 else if (pop3_state->ac_prefs->tmp_pass)
501 g_strdup(pop3_state->ac_prefs->tmp_pass);
505 pass = input_dialog_query_password
506 (pop3_state->ac_prefs->recv_server,
509 if (inc_dialog->show_dialog)
510 manage_window_focus_in
511 (inc_dialog->mainwin->window,
514 pop3_state->ac_prefs->tmp_pass = g_strdup(pass);
515 pop3_state->pass = pass;
517 inc_session_destroy(session);
518 inc_dialog->queue_list = g_list_remove
519 (inc_dialog->queue_list, session);
524 gtk_clist_set_pixmap(clist, num, 0, currentxpm, currentxpmmask);
525 gtk_clist_set_text(clist, num, 2, _("Retrieving"));
527 /* begin POP3 session */
528 inc_state = inc_pop3_session_do(session);
530 if (inc_state == INC_SUCCESS) {
531 gtk_clist_set_pixmap(clist, num, 0, okxpm, okxpmmask);
532 gtk_clist_set_text(clist, num, 2, _("Done"));
533 } else if (inc_state == INC_CANCEL) {
534 gtk_clist_set_pixmap(clist, num, 0, okxpm, okxpmmask);
535 gtk_clist_set_text(clist, num, 2, _("Cancelled"));
537 gtk_clist_set_pixmap(clist, num, 0, errorxpm, errorxpmmask);
538 if (inc_state == INC_CONNECT_ERROR)
539 gtk_clist_set_text(clist, num, 2,
540 _("Connection failed"));
541 else if (inc_state == INC_AUTH_FAILED)
542 gtk_clist_set_text(clist, num, 2,
545 gtk_clist_set_text(clist, num, 2, _("Error"));
548 if (pop3_state->error_val == PS_AUTHFAIL) {
549 if(!prefs_common.noerrorpanel) {
550 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
551 ((prefs_common.recv_dialog_mode == RECV_DIALOG_ACTIVE) && focus_window)) {
552 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
555 (_("Authorization for %s on %s failed"),
557 pop3_state->ac_prefs->recv_server);
563 /* CLAWS: perform filtering actions on dropped message */
564 if (global_processing != NULL) {
565 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 folder = processing->folder;
583 msglist = folder->get_msg_list(folder, processing, FALSE);
585 /* process messages */
586 for(msglist_element = msglist; msglist_element != NULL; msglist_element = msglist_element->next) {
587 msginfo = (MsgInfo *) msglist_element->data;
588 /* filter if enabled in prefs or move to inbox if not */
589 if(pop3_state->ac_prefs->filter_on_recv) {
590 filter_message_by_msginfo_with_inbox(global_processing, msginfo,
591 pop3_state->folder_table,
594 folder_item_move_msg(inbox, msginfo);
595 g_hash_table_insert(pop3_state->folder_table, inbox,
598 procmsg_msginfo_free(msginfo);
600 g_slist_free(msglist);
604 new_msgs += pop3_state->cur_total_num;
606 if (!prefs_common.scan_all_after_inc) {
607 folder_item_scan_foreach(pop3_state->folder_table);
608 folderview_update_item_foreach
609 (pop3_state->folder_table);
612 if (pop3_state->error_val == PS_AUTHFAIL &&
613 pop3_state->ac_prefs->tmp_pass) {
614 g_free(pop3_state->ac_prefs->tmp_pass);
615 pop3_state->ac_prefs->tmp_pass = NULL;
618 inc_write_uidl_list(pop3_state);
620 if (inc_state != INC_SUCCESS && inc_state != INC_CANCEL) {
622 if (inc_state == INC_NOSPACE) {
623 inc_put_error(inc_state);
628 inc_session_destroy(session);
629 inc_dialog->queue_list =
630 g_list_remove(inc_dialog->queue_list, session);
635 if (error_num && !prefs_common.noerrorpanel) {
636 if (inc_dialog->show_dialog)
637 manage_window_focus_in(inc_dialog->dialog->window,
639 alertpanel_error(_("Some errors occured while getting mail."));
640 if (inc_dialog->show_dialog)
641 manage_window_focus_out(inc_dialog->dialog->window,
645 while (inc_dialog->queue_list != NULL) {
646 session = inc_dialog->queue_list->data;
647 inc_session_destroy(session);
648 inc_dialog->queue_list =
649 g_list_remove(inc_dialog->queue_list, session);
652 inc_progress_dialog_destroy(inc_dialog);
657 static IncState inc_pop3_session_do(IncSession *session)
659 Pop3State *pop3_state = session->pop3_state;
660 IncProgressDialog *inc_dialog = (IncProgressDialog *)session->data;
667 static AtmHandler handlers[] = {
670 pop3_stls_send , pop3_stls_recv,
672 pop3_getauth_user_send , pop3_getauth_user_recv,
673 pop3_getauth_pass_send , pop3_getauth_pass_recv,
674 pop3_getauth_apop_send , pop3_getauth_apop_recv,
675 pop3_getrange_stat_send , pop3_getrange_stat_recv,
676 pop3_getrange_last_send , pop3_getrange_last_recv,
677 pop3_getrange_uidl_send , pop3_getrange_uidl_recv,
678 pop3_getsize_list_send , pop3_getsize_list_recv,
679 pop3_top_send , pop3_top_recv,
680 pop3_retr_send , pop3_retr_recv,
681 pop3_delete_send , pop3_delete_recv,
682 pop3_logout_send , pop3_logout_recv
685 debug_print(_("getting new messages of account %s...\n"),
686 pop3_state->ac_prefs->account_name);
688 atm = automaton_create(N_POP3_PHASE);
691 atm->data = pop3_state;
693 buf = g_strdup_printf(_("%s: Retrieving new messages"),
694 pop3_state->ac_prefs->recv_server);
695 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window), buf);
698 for (i = POP3_GREETING_RECV; i < N_POP3_PHASE; i++)
699 atm->state[i].handler = handlers[i];
700 atm->state[POP3_GREETING_RECV].condition = GDK_INPUT_READ;
701 for (i = POP3_GREETING_RECV + 1; i < N_POP3_PHASE; ) {
702 atm->state[i++].condition = GDK_INPUT_WRITE;
703 atm->state[i++].condition = GDK_INPUT_READ;
706 atm->terminate = (AtmHandler)pop3_automaton_terminate;
707 atm->ui_func = (AtmUIFunc)inc_progress_update;
709 atm->num = POP3_GREETING_RECV;
711 server = pop3_state->ac_prefs->recv_server;
713 port = pop3_state->ac_prefs->set_popport ?
714 pop3_state->ac_prefs->popport :
715 pop3_state->ac_prefs->ssl_pop == SSL_TUNNEL ? 995 : 110;
717 port = pop3_state->ac_prefs->set_popport ?
718 pop3_state->ac_prefs->popport : 110;
721 buf = g_strdup_printf(_("Connecting to POP3 server: %s ..."), server);
722 log_message("%s\n", buf);
723 progress_dialog_set_label(inc_dialog->dialog, buf);
728 if ((sockinfo = sock_connect(server, port)) == NULL) {
729 log_warning(_("Can't connect to POP3 server: %s:%d\n"),
731 if(!prefs_common.noerrorpanel) {
732 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
733 ((prefs_common.recv_dialog_mode == RECV_DIALOG_ACTIVE) && focus_window)) {
734 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
736 alertpanel_error(_("Can't connect to POP3 server: %s:%d"),
738 manage_window_focus_out(inc_dialog->dialog->window, NULL, NULL);
740 pop3_automaton_terminate(NULL, atm);
741 automaton_destroy(atm);
742 return INC_CONNECT_ERROR;
746 if (pop3_state->ac_prefs->ssl_pop == SSL_TUNNEL &&
747 !ssl_init_socket(sockinfo)) {
748 pop3_automaton_terminate(NULL, atm);
749 automaton_destroy(atm);
750 return INC_CONNECT_ERROR;
754 /* :WK: Hmmm, with the later sock_gdk_input, we have 2 references
755 * to the sock structure - implement a reference counter?? */
756 pop3_state->sockinfo = sockinfo;
757 atm->help_sock = sockinfo;
759 log_verbosity_set(TRUE);
760 /* oha: this messes up inc_progress update:
761 disabling this would avoid the label "Retrieve Header"
762 being overwritten by "Retrieve Message"
763 Setting inc_pop3_recv_func is not necessary
764 since atm already handles the progress dialog ui
767 recv_set_ui_func(inc_pop3_recv_func, session);
769 atm->tag = sock_gdk_input_add(sockinfo,
770 atm->state[atm->num].condition,
771 automaton_input_cb, atm);
773 while (!atm->terminated)
774 gtk_main_iteration();
776 log_verbosity_set(FALSE);
778 recv_set_ui_func(NULL, NULL);
780 automaton_destroy(atm);
782 return pop3_state->inc_state;
785 static gint pop3_automaton_terminate(SockInfo *source, Automaton *atm)
787 if (atm->terminated) return 0;
790 gdk_input_remove(atm->tag);
793 if (atm->timeout_tag > 0) {
794 gtk_timeout_remove(atm->timeout_tag);
795 atm->timeout_tag = 0;
800 atm->terminated = TRUE;
805 static GHashTable *inc_get_uidl_table(PrefsAccount *ac_prefs)
810 gchar buf[IDLEN + 3];
814 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
815 "uidl-", ac_prefs->recv_server,
816 "-", ac_prefs->userid, NULL);
818 if ((fp = fopen(path, "rb")) == NULL) {
819 if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
825 table = g_hash_table_new(g_str_hash, g_str_equal);
827 g_date_clear(&curdate, 1);
830 * NOTE: g_date_set_time() has to be called inside this
831 * loop, because a day change may happen??? That right?
834 while (fgets(buf, sizeof(buf), fp) != NULL) {
837 /* data[0] will contain uidl
838 * data[1] will contain day of retrieval */
841 * FIXME: convoluted implementation. need to find
842 * a better way to split the string.
844 if (strchr(buf, '\t')) {
845 data = g_strsplit(buf, "\t", 2);
847 g_hash_table_insert(table, g_strdup(data[0]), g_strdup(data[1]));
851 g_date_set_time(&curdate, time(NULL));
852 g_hash_table_insert(table, g_strdup(buf),
853 g_strdup_printf("%d", g_date_day_of_year(&curdate)));
861 static void inc_write_uidl_list(Pop3State *state)
870 if (!state->uidl_is_valid)
873 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
874 "uidl-", state->ac_prefs->recv_server,
875 "-", state->user, NULL);
876 if ((fp = fopen(path, "wb")) == NULL) {
877 FILE_OP_ERROR(path, "fopen");
882 g_date_clear(&curdate, 1);
884 for (n = 1; n <= state->count; n++) {
885 if (state->msg[n].uidl && state->msg[n].received &&
886 !state->msg[n].deleted) {
887 if (fputs(state->msg[n].uidl, fp) == EOF) {
888 FILE_OP_ERROR(path, "fputs");
891 if (fputc('\t', fp) == EOF) {
892 FILE_OP_ERROR(path, "fputc");
897 * NOTE: need to set time to watch for day changes??
899 g_date_set_time(&curdate, time(NULL));
901 if (NULL != (sdate = g_hash_table_lookup(state->uidl_table, state->msg[n].uidl))) {
902 tdate = sdate != NULL ? atoi(sdate) : g_date_day_of_year(&curdate);
903 if (fprintf(fp, "%3d", tdate) == EOF) {
904 FILE_OP_ERROR(path, "fprintf");
908 if (fprintf(fp, "%d", g_date_day_of_year(&curdate)) == EOF) {
909 FILE_OP_ERROR(path, "fputs");
914 if (fputc('\n', fp) == EOF) {
915 FILE_OP_ERROR(path, "fputc");
921 if (fclose(fp) == EOF)
922 FILE_OP_ERROR(path, "fclose");
926 static gboolean inc_pop3_recv_func(SockInfo *sock, gint count, gint read_bytes,
929 gchar buf[MSGBUFSIZE];
930 IncSession *session = (IncSession *)data;
931 Pop3State *state = session->pop3_state;
932 IncProgressDialog *inc_dialog = session->data;
933 ProgressDialog *dialog = inc_dialog->dialog;
937 cur_total = state->cur_total_bytes + read_bytes;
938 if (cur_total > state->total_bytes)
939 cur_total = state->total_bytes;
941 Xstrdup_a(total_size, to_human_readable(state->total_bytes),
943 g_snprintf(buf, sizeof(buf),
944 _("Retrieving message (%d / %d) (%s / %s)"),
945 state->cur_msg, state->count,
946 to_human_readable(cur_total), total_size);
947 progress_dialog_set_label(dialog, buf);
949 progress_dialog_set_percentage
950 (dialog, (gfloat)cur_total / (gfloat)state->total_bytes);
951 gtk_progress_bar_update
952 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar),
953 (gfloat)cur_total / (gfloat)state->total_bytes);
956 if (state->inc_state == INC_CANCEL)
962 void inc_progress_update(Pop3State *state, Pop3Phase phase)
964 gchar buf[MSGBUFSIZE];
965 IncProgressDialog *inc_dialog = state->session->data;
966 ProgressDialog *dialog = inc_dialog->dialog;
970 case POP3_GREETING_RECV:
972 case POP3_GETAUTH_USER_SEND:
973 case POP3_GETAUTH_PASS_SEND:
974 case POP3_GETAUTH_APOP_SEND:
975 progress_dialog_set_label(dialog, _("Authenticating..."));
977 case POP3_GETRANGE_STAT_SEND:
978 progress_dialog_set_label
979 (dialog, _("Getting the number of new messages (STAT)..."));
981 case POP3_GETRANGE_LAST_SEND:
982 progress_dialog_set_label
983 (dialog, _("Getting the number of new messages (LAST)..."));
985 case POP3_GETRANGE_UIDL_SEND:
986 progress_dialog_set_label
987 (dialog, _("Getting the number of new messages (UIDL)..."));
989 case POP3_GETSIZE_LIST_SEND:
990 progress_dialog_set_label
991 (dialog, _("Getting the size of messages (LIST)..."));
994 g_snprintf(buf, sizeof(buf),
995 _("Retrieving header (%d / %d)"),
996 state->cur_msg, state->count);
997 progress_dialog_set_label (dialog, buf);
998 progress_dialog_set_percentage
1000 (gfloat)(state->cur_msg) /
1001 (gfloat)(state->count));
1002 gtk_progress_bar_update
1003 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar),
1004 (gfloat)(state->cur_msg) /
1005 (gfloat)(state->count));
1007 case POP3_RETR_SEND:
1008 Xstrdup_a(total_size, to_human_readable(state->total_bytes), return);
1009 g_snprintf(buf, sizeof(buf),
1010 _("Retrieving message (%d / %d) (%s / %s)"),
1011 state->cur_msg, state->count,
1012 to_human_readable(state->cur_total_bytes),
1014 progress_dialog_set_label(dialog, buf);
1015 progress_dialog_set_percentage
1017 (gfloat)(state->cur_total_bytes) /
1018 (gfloat)(state->total_bytes));
1019 gtk_progress_bar_update
1020 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar),
1021 (gfloat)(state->cur_total_bytes) /
1022 (gfloat)(state->total_bytes));
1025 case POP3_DELETE_SEND:
1026 progress_dialog_set_label(dialog, _("Deleting message"));
1029 case POP3_LOGOUT_SEND:
1030 progress_dialog_set_label(dialog, _("Quitting"));
1037 gint inc_drop_message(const gchar *file, Pop3State *state)
1040 FolderItem *dropfolder;
1044 /* CLAWS: get default inbox (perhaps per account) */
1045 if (state->ac_prefs->inbox) {
1046 /* CLAWS: get destination folder / mailbox */
1047 inbox = folder_find_item_from_identifier
1048 (state->ac_prefs->inbox);
1050 inbox = folder_get_default_inbox();
1052 inbox = folder_get_default_inbox();
1058 /* CLAWS: claws uses a global .processing folder for the filtering. */
1059 if (global_processing == NULL) {
1060 if (state->ac_prefs->filter_on_recv) {
1062 filter_get_dest_folder(prefs_common.fltlist, file);
1063 if (!dropfolder) dropfolder = inbox;
1064 else if (!strcmp(dropfolder->path, FILTER_NOT_RECEIVE)) {
1065 g_warning(_("a message won't be received\n"));
1071 dropfolder = folder_get_default_processing();
1074 val = GPOINTER_TO_INT(g_hash_table_lookup
1075 (state->folder_table, dropfolder));
1077 folder_item_scan(dropfolder);
1078 g_hash_table_insert(state->folder_table, dropfolder,
1079 GINT_TO_POINTER(1));
1082 /* add msg file to drop folder */
1083 if ((msgnum = folder_item_add_msg(dropfolder, file, TRUE)) < 0) {
1091 static void inc_put_error(IncState istate)
1095 if(!prefs_common.noerrorpanel) {
1096 alertpanel_error(_("Error occurred while processing mail."));
1100 alertpanel_error(_("No disk space left."));
1107 static void inc_cancel(IncProgressDialog *dialog)
1109 IncSession *session;
1112 g_return_if_fail(dialog != NULL);
1114 session = dialog->queue_list->data;
1115 sockinfo = session->pop3_state->sockinfo;
1117 if (!sockinfo || session->atm->terminated == TRUE) return;
1119 session->pop3_state->inc_state = INC_CANCEL;
1120 pop3_automaton_terminate(sockinfo, session->atm);
1121 session->pop3_state->sockinfo = NULL;
1124 gboolean inc_is_active(void)
1126 return (inc_dialog_list != NULL);
1129 void inc_cancel_all(void)
1133 for (cur = inc_dialog_list; cur != NULL; cur = cur->next)
1134 inc_cancel((IncProgressDialog *)cur->data);
1137 static void inc_cancel_cb(GtkWidget *widget, gpointer data)
1139 inc_cancel((IncProgressDialog *)data);
1142 static gint inc_spool(void)
1144 gchar *mbox, *logname;
1147 logname = g_get_user_name();
1148 mbox = g_strconcat(prefs_common.spool_path
1149 ? prefs_common.spool_path : DEFAULT_SPOOL_PATH,
1150 G_DIR_SEPARATOR_S, logname, NULL);
1151 msgs = get_spool(folder_get_default_inbox(), mbox);
1157 static void inc_spool_account(PrefsAccount *account)
1160 FolderItem *dropfolder;
1163 if (account->inbox) {
1164 inbox = folder_find_item_from_path(account->inbox);
1166 inbox = folder_get_default_inbox();
1168 inbox = folder_get_default_inbox();
1170 get_spool(inbox, account->local_mbox);
1173 static void inc_all_spool(void)
1177 list = account_get_list();
1180 for (; list != NULL; list = list->next) {
1181 IncSession *session;
1182 PrefsAccount *account = list->data;
1184 if ((account->protocol == A_LOCAL) &&
1185 (account->recv_at_getall))
1186 inc_spool_account(account);
1190 static gint get_spool(FolderItem *dest, const gchar *mbox)
1194 gchar tmp_mbox[MAXPATHLEN + 1];
1195 GHashTable *folder_table = NULL;
1197 g_return_val_if_fail(dest != NULL, -1);
1198 g_return_val_if_fail(mbox != NULL, -1);
1200 if (!is_file_exist(mbox) || (size = get_file_size(mbox)) == 0) {
1201 debug_print(_("no messages in local mailbox.\n"));
1203 } else if (size < 0)
1206 if ((lockfd = lock_mbox(mbox, LOCK_FLOCK)) < 0)
1209 g_snprintf(tmp_mbox, sizeof(tmp_mbox), "%s%ctmpmbox%d",
1210 get_rc_dir(), G_DIR_SEPARATOR, (gint)mbox);
1212 if (copy_mbox(mbox, tmp_mbox) < 0) {
1213 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1217 debug_print(_("Getting new messages from %s into %s...\n"),
1220 if (prefs_common.filter_on_inc)
1221 folder_table = g_hash_table_new(NULL, NULL);
1222 msgs = proc_mbox(dest, tmp_mbox, folder_table);
1225 if (msgs >= 0) empty_mbox(mbox);
1226 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1229 if (!prefs_common.scan_all_after_inc) {
1230 g_hash_table_insert(folder_table, dest,
1231 GINT_TO_POINTER(1));
1232 folder_item_scan_foreach(folder_table);
1233 folderview_update_item_foreach(folder_table);
1235 g_hash_table_destroy(folder_table);
1236 } else if (!prefs_common.scan_all_after_inc) {
1237 folder_item_scan(dest);
1238 folderview_update_item(dest, FALSE);
1249 void inc_unlock(void)
1251 if (inc_lock_count > 0)
1255 static guint autocheck_timer = 0;
1256 static gpointer autocheck_data = NULL;
1258 static void inc_notify_cmd(gint new_msgs, gboolean notify)
1263 if (!(new_msgs && notify && prefs_common.newmail_notify_cmd &&
1264 *prefs_common.newmail_notify_cmd))
1266 if ((buf = strchr(prefs_common.newmail_notify_cmd, '%')) &&
1267 buf[1] == 'd' && !strchr(&buf[1], '%'))
1268 buf = g_strdup_printf(prefs_common.newmail_notify_cmd,
1271 buf = g_strdup(prefs_common.newmail_notify_cmd);
1273 execute_command_line(buf, TRUE);
1278 void inc_autocheck_timer_init(MainWindow *mainwin)
1280 autocheck_data = mainwin;
1281 inc_autocheck_timer_set();
1284 static void inc_autocheck_timer_set_interval(guint interval)
1286 inc_autocheck_timer_remove();
1287 /* last test is to avoid re-enabling auto_check after modifying
1288 the common preferences */
1289 if (prefs_common.autochk_newmail && autocheck_data
1290 && prefs_common.work_offline == FALSE) {
1291 autocheck_timer = gtk_timeout_add
1292 (interval, inc_autocheck_func, autocheck_data);
1293 debug_print("added timer = %d\n", autocheck_timer);
1297 void inc_autocheck_timer_set(void)
1299 inc_autocheck_timer_set_interval(prefs_common.autochk_itv * 60000);
1302 void inc_autocheck_timer_remove(void)
1304 if (autocheck_timer) {
1305 debug_print("removed timer = %d\n", autocheck_timer);
1306 gtk_timeout_remove(autocheck_timer);
1307 autocheck_timer = 0;
1311 static gint inc_autocheck_func(gpointer data)
1313 MainWindow *mainwin = (MainWindow *)data;
1315 if (inc_lock_count) {
1316 debug_print("autocheck is locked.\n");
1317 inc_autocheck_timer_set_interval(1000);
1321 inc_all_account_mail(mainwin, prefs_common.newmail_notify_auto);