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 gint inc_selective_download(MainWindow *mainwin, gint session_type)
204 PrefsAccount *account = cur_account;
207 account->session_type = session_type;
208 new_msgs = inc_account_mail(account, mainwin);
209 account->session_type = RETR_NORMAL;
214 static gint inc_account_mail(PrefsAccount *account, MainWindow *mainwin)
216 IncProgressDialog *inc_dialog;
220 switch (account->protocol) {
223 folderview_check_new(FOLDER(account->folder));
228 session = inc_session_new(account);
229 if (!session) return 0;
231 inc_dialog = inc_progress_dialog_create();
232 inc_dialog->queue_list = g_list_append(inc_dialog->queue_list,
234 inc_dialog->mainwin = mainwin;
235 session->data = inc_dialog;
238 text[1] = account->account_name;
239 text[2] = _("Standby");
240 gtk_clist_append(GTK_CLIST(inc_dialog->dialog->clist), text);
242 main_window_set_toolbar_sensitive(mainwin);
243 main_window_set_menu_sensitive(mainwin);
245 return inc_start(inc_dialog);
248 inc_spool_account(account);
253 void inc_all_account_mail(MainWindow *mainwin, gboolean notify)
255 GList *list, *queue_list = NULL;
256 IncProgressDialog *inc_dialog;
259 if (prefs_common.work_offline)
260 if (alertpanel(_("Offline warning"),
261 _("You're working offline. Override?"),
262 _("Yes"), _("No"), NULL) != G_ALERTDEFAULT)
265 if (inc_lock_count) return;
267 inc_autocheck_timer_remove();
268 summary_write_cache(mainwin->summaryview);
269 main_window_lock(mainwin);
271 if (prefs_common.inc_local)
272 new_msgs = inc_spool();
274 list = account_get_list();
276 inc_finished(mainwin, new_msgs > 0);
277 main_window_unlock(mainwin);
278 inc_notify_cmd(new_msgs, notify);
279 inc_autocheck_timer_set();
283 /* check local folders */
286 /* check IMAP4 folders */
287 for (; list != NULL; list = list->next) {
288 PrefsAccount *account = list->data;
289 if ((account->protocol == A_IMAP4 ||
290 account->protocol == A_NNTP) && account->recv_at_getall)
291 folderview_check_new(FOLDER(account->folder));
294 /* check POP3 accounts */
295 for (list = account_get_list(); list != NULL; list = list->next) {
297 PrefsAccount *account = list->data;
298 account->session_type = RETR_NORMAL;
299 if (account->recv_at_getall) {
300 session = inc_session_new(account);
302 queue_list = g_list_append(queue_list, session);
307 inc_finished(mainwin, new_msgs > 0);
308 main_window_unlock(mainwin);
309 inc_notify_cmd(new_msgs, notify);
310 inc_autocheck_timer_set();
314 inc_dialog = inc_progress_dialog_create();
315 inc_dialog->queue_list = queue_list;
316 inc_dialog->mainwin = mainwin;
317 for (list = queue_list; list != NULL; list = list->next) {
318 IncSession *session = list->data;
321 session->data = inc_dialog;
324 text[1] = session->pop3_state->ac_prefs->account_name;
325 text[2] = _("Standby");
326 gtk_clist_append(GTK_CLIST(inc_dialog->dialog->clist), text);
329 main_window_set_toolbar_sensitive(mainwin);
330 main_window_set_menu_sensitive(mainwin);
332 new_msgs += inc_start(inc_dialog);
334 inc_finished(mainwin, new_msgs > 0);
335 main_window_unlock(mainwin);
336 inc_notify_cmd(new_msgs, notify);
337 inc_autocheck_timer_set();
340 static IncProgressDialog *inc_progress_dialog_create(void)
342 IncProgressDialog *dialog;
343 ProgressDialog *progress;
345 dialog = g_new0(IncProgressDialog, 1);
347 progress = progress_dialog_create();
348 gtk_window_set_title(GTK_WINDOW(progress->window),
349 _("Retrieving new messages"));
350 gtk_signal_connect(GTK_OBJECT(progress->cancel_btn), "clicked",
351 GTK_SIGNAL_FUNC(inc_cancel_cb), dialog);
352 gtk_signal_connect(GTK_OBJECT(progress->window), "delete_event",
353 GTK_SIGNAL_FUNC(gtk_true), NULL);
354 /* manage_window_set_transient(GTK_WINDOW(progress->window)); */
356 progress_dialog_set_value(progress, 0.0);
358 stock_pixmap_gdk(progress->clist, STOCK_PIXMAP_COMPLETE,
360 stock_pixmap_gdk(progress->clist, STOCK_PIXMAP_CONTINUE,
361 ¤txpm, ¤txpmmask);
362 stock_pixmap_gdk(progress->clist, STOCK_PIXMAP_ERROR,
363 &errorxpm, &errorxpmmask);
365 if (prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS ||
366 (prefs_common.recv_dialog_mode == RECV_DIALOG_ACTIVE &&
367 manage_window_get_focus_window())) {
368 dialog->show_dialog = TRUE;
369 gtk_widget_show_now(progress->window);
372 dialog->dialog = progress;
373 dialog->queue_list = NULL;
375 inc_dialog_list = g_list_append(inc_dialog_list, dialog);
380 static void inc_progress_dialog_clear(IncProgressDialog *inc_dialog)
382 progress_dialog_set_value(inc_dialog->dialog, 0.0);
383 progress_dialog_set_label(inc_dialog->dialog, "");
384 gtk_progress_bar_update
385 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar), 0.0);
388 static void inc_progress_dialog_destroy(IncProgressDialog *inc_dialog)
390 g_return_if_fail(inc_dialog != NULL);
392 inc_dialog_list = g_list_remove(inc_dialog_list, inc_dialog);
394 gtk_progress_bar_update
395 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar), 0.0);
396 progress_dialog_destroy(inc_dialog->dialog);
401 static IncSession *inc_session_new(PrefsAccount *account)
405 g_return_val_if_fail(account != NULL, NULL);
407 if (account->protocol != A_POP3 && account->protocol != A_APOP)
409 if (!account->recv_server || !account->userid)
412 session = g_new0(IncSession, 1);
413 session->pop3_state = inc_pop3_state_new(account);
414 session->pop3_state->session = session;
419 static void inc_session_destroy(IncSession *session)
421 g_return_if_fail(session != NULL);
423 inc_pop3_state_destroy(session->pop3_state);
427 static Pop3State *inc_pop3_state_new(PrefsAccount *account)
431 state = g_new0(Pop3State, 1);
433 state->ac_prefs = account;
434 state->folder_table = g_hash_table_new(NULL, NULL);
435 state->uidl_todelete_list = NULL;
436 state->uidl_table = inc_get_uidl_table(account);
437 state->inc_state = INC_SUCCESS;
442 static void inc_pop3_state_destroy(Pop3State *state)
446 g_hash_table_destroy(state->folder_table);
448 for (n = 1; n <= state->count; n++)
449 g_free(state->msg[n].uidl);
452 if (state->uidl_table) {
453 hash_free_strings(state->uidl_table);
454 g_hash_table_destroy(state->uidl_table);
456 g_slist_free(state->uidl_todelete_list);
457 g_free(state->greeting);
460 g_free(state->prev_folder);
465 static gint inc_start(IncProgressDialog *inc_dialog)
468 GtkCList *clist = GTK_CLIST(inc_dialog->dialog->clist);
469 Pop3State *pop3_state;
475 while (inc_dialog->queue_list != NULL) {
476 session = inc_dialog->queue_list->data;
477 pop3_state = session->pop3_state;
479 inc_progress_dialog_clear(inc_dialog);
481 gtk_clist_moveto(clist, num, -1, 1.0, 0.0);
483 pop3_state->user = g_strdup(pop3_state->ac_prefs->userid);
484 if (pop3_state->ac_prefs->passwd)
486 g_strdup(pop3_state->ac_prefs->passwd);
487 else if (pop3_state->ac_prefs->tmp_pass)
489 g_strdup(pop3_state->ac_prefs->tmp_pass);
493 pass = input_dialog_query_password
494 (pop3_state->ac_prefs->recv_server,
497 if (inc_dialog->show_dialog)
498 manage_window_focus_in
499 (inc_dialog->mainwin->window,
502 pop3_state->ac_prefs->tmp_pass = g_strdup(pass);
503 pop3_state->pass = pass;
505 inc_session_destroy(session);
506 inc_dialog->queue_list = g_list_remove
507 (inc_dialog->queue_list, session);
512 gtk_clist_set_pixmap(clist, num, 0, currentxpm, currentxpmmask);
513 gtk_clist_set_text(clist, num, 2, _("Retrieving"));
515 /* begin POP3 session */
516 inc_state = inc_pop3_session_do(session);
518 if (inc_state == INC_SUCCESS) {
519 gtk_clist_set_pixmap(clist, num, 0, okxpm, okxpmmask);
520 gtk_clist_set_text(clist, num, 2, _("Done"));
521 } else if (inc_state == INC_CANCEL) {
522 gtk_clist_set_pixmap(clist, num, 0, okxpm, okxpmmask);
523 gtk_clist_set_text(clist, num, 2, _("Cancelled"));
525 gtk_clist_set_pixmap(clist, num, 0, errorxpm, errorxpmmask);
526 if (inc_state == INC_CONNECT_ERROR)
527 gtk_clist_set_text(clist, num, 2,
528 _("Connection failed"));
529 else if (inc_state == INC_AUTH_FAILED)
530 gtk_clist_set_text(clist, num, 2,
533 gtk_clist_set_text(clist, num, 2, _("Error"));
536 if (pop3_state->error_val == PS_AUTHFAIL) {
537 if(!prefs_common.noerrorpanel) {
538 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
539 ((prefs_common.recv_dialog_mode == RECV_DIALOG_ACTIVE) && focus_window)) {
540 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
543 (_("Authorization for %s on %s failed"),
545 pop3_state->ac_prefs->recv_server);
551 /* CLAWS: perform filtering actions on dropped message */
552 if (global_processing != NULL) {
553 FolderItem *processing, *inbox;
556 GSList *msglist, *msglist_element;
558 /* CLAWS: get default inbox (perhaps per account) */
559 if (pop3_state->ac_prefs->inbox) {
560 /* CLAWS: get destination folder / mailbox */
561 inbox = folder_find_item_from_identifier(pop3_state->ac_prefs->inbox);
563 inbox = folder_get_default_inbox();
565 inbox = folder_get_default_inbox();
567 /* get list of messages in processing */
568 processing = folder_get_default_processing();
569 folder_item_scan(processing);
570 folder = processing->folder;
571 msglist = folder->get_msg_list(folder, processing, FALSE);
573 /* process messages */
574 for(msglist_element = msglist; msglist_element != NULL; msglist_element = msglist_element->next) {
575 msginfo = (MsgInfo *) msglist_element->data;
576 /* filter if enabled in prefs or move to inbox if not */
577 if(pop3_state->ac_prefs->filter_on_recv) {
578 filter_message_by_msginfo_with_inbox(global_processing, msginfo,
579 pop3_state->folder_table,
582 folder_item_move_msg(inbox, msginfo);
583 g_hash_table_insert(pop3_state->folder_table, inbox,
586 procmsg_msginfo_free(msginfo);
588 g_slist_free(msglist);
592 new_msgs += pop3_state->cur_total_num;
594 if (!prefs_common.scan_all_after_inc) {
595 folder_item_scan_foreach(pop3_state->folder_table);
596 folderview_update_item_foreach
597 (pop3_state->folder_table);
600 if (pop3_state->error_val == PS_AUTHFAIL &&
601 pop3_state->ac_prefs->tmp_pass) {
602 g_free(pop3_state->ac_prefs->tmp_pass);
603 pop3_state->ac_prefs->tmp_pass = NULL;
606 inc_write_uidl_list(pop3_state);
608 if (inc_state != INC_SUCCESS && inc_state != INC_CANCEL) {
610 if (inc_state == INC_NOSPACE) {
611 inc_put_error(inc_state);
616 inc_session_destroy(session);
617 inc_dialog->queue_list =
618 g_list_remove(inc_dialog->queue_list, session);
623 if (error_num && !prefs_common.noerrorpanel) {
624 if (inc_dialog->show_dialog)
625 manage_window_focus_in(inc_dialog->dialog->window,
627 alertpanel_error(_("Some errors occured while getting mail."));
628 if (inc_dialog->show_dialog)
629 manage_window_focus_out(inc_dialog->dialog->window,
633 while (inc_dialog->queue_list != NULL) {
634 session = inc_dialog->queue_list->data;
635 inc_session_destroy(session);
636 inc_dialog->queue_list =
637 g_list_remove(inc_dialog->queue_list, session);
640 inc_progress_dialog_destroy(inc_dialog);
645 static IncState inc_pop3_session_do(IncSession *session)
647 Pop3State *pop3_state = session->pop3_state;
648 IncProgressDialog *inc_dialog = (IncProgressDialog *)session->data;
655 static AtmHandler handlers[] = {
657 pop3_getauth_user_send , pop3_getauth_user_recv,
658 pop3_getauth_pass_send , pop3_getauth_pass_recv,
659 pop3_getauth_apop_send , pop3_getauth_apop_recv,
660 pop3_getrange_stat_send , pop3_getrange_stat_recv,
661 pop3_getrange_last_send , pop3_getrange_last_recv,
662 pop3_getrange_uidl_send , pop3_getrange_uidl_recv,
663 pop3_getsize_list_send , pop3_getsize_list_recv,
664 pop3_top_send , pop3_top_recv,
665 pop3_retr_send , pop3_retr_recv,
666 pop3_delete_send , pop3_delete_recv,
667 pop3_logout_send , pop3_logout_recv
670 debug_print(_("getting new messages of account %s...\n"),
671 pop3_state->ac_prefs->account_name);
673 atm = automaton_create(N_POP3_PHASE);
676 atm->data = pop3_state;
678 buf = g_strdup_printf(_("%s: Retrieving new messages"),
679 pop3_state->ac_prefs->recv_server);
680 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window), buf);
683 for (i = POP3_GREETING_RECV; i < N_POP3_PHASE; i++)
684 atm->state[i].handler = handlers[i];
685 atm->state[POP3_GREETING_RECV].condition = GDK_INPUT_READ;
686 for (i = POP3_GETAUTH_USER_SEND; i < N_POP3_PHASE; ) {
687 atm->state[i++].condition = GDK_INPUT_WRITE;
688 atm->state[i++].condition = GDK_INPUT_READ;
691 atm->terminate = (AtmHandler)pop3_automaton_terminate;
693 atm->num = POP3_GREETING_RECV;
695 server = pop3_state->ac_prefs->recv_server;
697 port = pop3_state->ac_prefs->set_popport ?
698 pop3_state->ac_prefs->popport :
699 pop3_state->ac_prefs->ssl_pop ? 995 : 110;
701 port = pop3_state->ac_prefs->set_popport ?
702 pop3_state->ac_prefs->popport : 110;
705 buf = g_strdup_printf(_("Connecting to POP3 server: %s ..."), server);
706 log_message("%s\n", buf);
707 progress_dialog_set_label(inc_dialog->dialog, buf);
712 if ((sockinfo = sock_connect(server, port)) == NULL) {
713 log_warning(_("Can't connect to POP3 server: %s:%d\n"),
715 if(!prefs_common.noerrorpanel) {
716 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
717 ((prefs_common.recv_dialog_mode == RECV_DIALOG_ACTIVE) && focus_window)) {
718 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
720 alertpanel_error(_("Can't connect to POP3 server: %s:%d"),
722 manage_window_focus_out(inc_dialog->dialog->window, NULL, NULL);
724 pop3_automaton_terminate(NULL, atm);
725 automaton_destroy(atm);
726 return INC_CONNECT_ERROR;
730 if (pop3_state->ac_prefs->ssl_pop && !ssl_init_socket(sockinfo)) {
731 pop3_automaton_terminate(NULL, atm);
732 automaton_destroy(atm);
733 return INC_CONNECT_ERROR;
737 /* :WK: Hmmm, with the later sock_gdk_input, we have 2 references
738 * to the sock structure - implement a reference counter?? */
739 pop3_state->sockinfo = sockinfo;
740 atm->help_sock = sockinfo;
742 log_verbosity_set(TRUE);
743 recv_set_ui_func(inc_pop3_recv_func, session);
745 atm->tag = sock_gdk_input_add(sockinfo,
746 atm->state[atm->num].condition,
747 automaton_input_cb, atm);
749 while (!atm->terminated)
750 gtk_main_iteration();
752 log_verbosity_set(FALSE);
753 recv_set_ui_func(NULL, NULL);
755 automaton_destroy(atm);
757 return pop3_state->inc_state;
760 static gint pop3_automaton_terminate(SockInfo *source, Automaton *atm)
762 if (atm->terminated) return 0;
765 gdk_input_remove(atm->tag);
768 if (atm->timeout_tag > 0) {
769 gtk_timeout_remove(atm->timeout_tag);
770 atm->timeout_tag = 0;
775 atm->terminated = TRUE;
780 static GHashTable *inc_get_uidl_table(PrefsAccount *ac_prefs)
785 gchar buf[IDLEN + 3];
789 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
790 "uidl-", ac_prefs->recv_server,
791 "-", ac_prefs->userid, NULL);
793 if ((fp = fopen(path, "rb")) == NULL) {
794 if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
800 table = g_hash_table_new(g_str_hash, g_str_equal);
802 g_date_clear(&curdate, 1);
805 * NOTE: g_date_set_time() has to be called inside this
806 * loop, because a day change may happen??? That right?
809 while (fgets(buf, sizeof(buf), fp) != NULL) {
812 /* data[0] will contain uidl
813 * data[1] will contain day of retrieval */
816 * FIXME: convoluted implementation. need to find
817 * a better way to split the string.
819 if (strchr(buf, '\t')) {
820 data = g_strsplit(buf, "\t", 2);
822 g_hash_table_insert(table, g_strdup(data[0]), g_strdup(data[1]));
826 g_date_set_time(&curdate, time(NULL));
827 g_hash_table_insert(table, g_strdup(buf),
828 g_strdup_printf("%d", g_date_day_of_year(&curdate)));
836 static void inc_write_uidl_list(Pop3State *state)
845 if (!state->uidl_is_valid)
848 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
849 "uidl-", state->ac_prefs->recv_server,
850 "-", state->user, NULL);
851 if ((fp = fopen(path, "wb")) == NULL) {
852 FILE_OP_ERROR(path, "fopen");
857 g_date_clear(&curdate, 1);
859 for (n = 1; n <= state->count; n++) {
860 if (state->msg[n].uidl && state->msg[n].received &&
861 !state->msg[n].deleted) {
862 if (fputs(state->msg[n].uidl, fp) == EOF) {
863 FILE_OP_ERROR(path, "fputs");
866 if (fputc('\t', fp) == EOF) {
867 FILE_OP_ERROR(path, "fputc");
872 * NOTE: need to set time to watch for day changes??
874 g_date_set_time(&curdate, time(NULL));
876 if (NULL != (sdate = g_hash_table_lookup(state->uidl_table, state->msg[n].uidl))) {
877 tdate = sdate != NULL ? atoi(sdate) : g_date_day_of_year(&curdate);
878 if (fprintf(fp, "%3d", tdate) == EOF) {
879 FILE_OP_ERROR(path, "fprintf");
883 if (fprintf(fp, "%d", g_date_day_of_year(&curdate)) == EOF) {
884 FILE_OP_ERROR(path, "fputs");
889 if (fputc('\n', fp) == EOF) {
890 FILE_OP_ERROR(path, "fputc");
896 if (fclose(fp) == EOF)
897 FILE_OP_ERROR(path, "fclose");
901 static gboolean inc_pop3_recv_func(SockInfo *sock, gint count, gint read_bytes,
904 gchar buf[MSGBUFSIZE];
905 IncSession *session = (IncSession *)data;
906 Pop3State *state = session->pop3_state;
907 IncProgressDialog *inc_dialog = session->data;
908 ProgressDialog *dialog = inc_dialog->dialog;
912 cur_total = state->cur_total_bytes + read_bytes;
913 if (cur_total > state->total_bytes)
914 cur_total = state->total_bytes;
916 Xstrdup_a(total_size, to_human_readable(state->total_bytes),
918 g_snprintf(buf, sizeof(buf),
919 _("Retrieving message (%d / %d) (%s / %s)"),
920 state->cur_msg, state->count,
921 to_human_readable(cur_total), total_size);
922 progress_dialog_set_label(dialog, buf);
924 progress_dialog_set_percentage
925 (dialog, (gfloat)cur_total / (gfloat)state->total_bytes);
926 gtk_progress_bar_update
927 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar),
928 (gfloat)cur_total / (gfloat)state->total_bytes);
931 if (state->inc_state == INC_CANCEL)
937 void inc_progress_update(Pop3State *state, Pop3Phase phase)
939 gchar buf[MSGBUFSIZE];
940 IncProgressDialog *inc_dialog = state->session->data;
941 ProgressDialog *dialog = inc_dialog->dialog;
945 case POP3_GREETING_RECV:
947 case POP3_GETAUTH_USER_SEND:
948 case POP3_GETAUTH_USER_RECV:
949 case POP3_GETAUTH_PASS_SEND:
950 case POP3_GETAUTH_PASS_RECV:
951 case POP3_GETAUTH_APOP_SEND:
952 case POP3_GETAUTH_APOP_RECV:
953 progress_dialog_set_label(dialog, _("Authenticating..."));
955 case POP3_GETRANGE_STAT_SEND:
956 case POP3_GETRANGE_STAT_RECV:
957 progress_dialog_set_label
958 (dialog, _("Getting the number of new messages (STAT)..."));
960 case POP3_GETRANGE_LAST_SEND:
961 case POP3_GETRANGE_LAST_RECV:
962 progress_dialog_set_label
963 (dialog, _("Getting the number of new messages (LAST)..."));
965 case POP3_GETRANGE_UIDL_SEND:
966 case POP3_GETRANGE_UIDL_RECV:
967 progress_dialog_set_label
968 (dialog, _("Getting the number of new messages (UIDL)..."));
970 case POP3_GETSIZE_LIST_SEND:
971 case POP3_GETSIZE_LIST_RECV:
972 progress_dialog_set_label
973 (dialog, _("Getting the size of messages (LIST)..."));
977 g_snprintf(buf, sizeof(buf),
978 _("Retrieving header (%d / %d)"),
979 state->cur_msg, state->count);
980 progress_dialog_set_label (dialog, buf);
981 progress_dialog_set_percentage
983 (gfloat)(state->cur_msg) /
984 (gfloat)(state->count));
985 gtk_progress_bar_update
986 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar),
987 (gfloat)(state->cur_msg) /
988 (gfloat)(state->count));
992 Xstrdup_a(total_size, to_human_readable(state->total_bytes), return);
993 g_snprintf(buf, sizeof(buf),
994 _("Retrieving message (%d / %d) (%s / %s)"),
995 state->cur_msg, state->count,
996 to_human_readable(state->cur_total_bytes),
998 progress_dialog_set_label(dialog, buf);
999 progress_dialog_set_percentage
1001 (gfloat)(state->cur_total_bytes) /
1002 (gfloat)(state->total_bytes));
1003 gtk_progress_bar_update
1004 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar),
1005 (gfloat)(state->cur_total_bytes) /
1006 (gfloat)(state->total_bytes));
1008 case POP3_DELETE_SEND:
1009 case POP3_DELETE_RECV:
1010 progress_dialog_set_label(dialog, _("Deleting message"));
1012 case POP3_LOGOUT_SEND:
1013 case POP3_LOGOUT_RECV:
1014 progress_dialog_set_label(dialog, _("Quitting"));
1021 gint inc_drop_message(const gchar *file, Pop3State *state)
1024 FolderItem *dropfolder;
1028 /* CLAWS: get default inbox (perhaps per account) */
1029 if (state->ac_prefs->inbox) {
1030 /* CLAWS: get destination folder / mailbox */
1031 inbox = folder_find_item_from_identifier
1032 (state->ac_prefs->inbox);
1034 inbox = folder_get_default_inbox();
1036 inbox = folder_get_default_inbox();
1042 /* CLAWS: claws uses a global .processing folder for the filtering. */
1043 if (global_processing == NULL) {
1044 if (state->ac_prefs->filter_on_recv) {
1046 filter_get_dest_folder(prefs_common.fltlist, file);
1047 if (!dropfolder) dropfolder = inbox;
1048 else if (!strcmp(dropfolder->path, FILTER_NOT_RECEIVE)) {
1049 g_warning(_("a message won't be received\n"));
1055 dropfolder = folder_get_default_processing();
1058 val = GPOINTER_TO_INT(g_hash_table_lookup
1059 (state->folder_table, dropfolder));
1061 folder_item_scan(dropfolder);
1062 g_hash_table_insert(state->folder_table, dropfolder,
1063 GINT_TO_POINTER(1));
1066 /* add msg file to drop folder */
1067 if ((msgnum = folder_item_add_msg(dropfolder, file, TRUE)) < 0) {
1075 static void inc_put_error(IncState istate)
1079 if(!prefs_common.noerrorpanel) {
1080 alertpanel_error(_("Error occurred while processing mail."));
1084 alertpanel_error(_("No disk space left."));
1091 static void inc_cancel(IncProgressDialog *dialog)
1093 IncSession *session;
1096 g_return_if_fail(dialog != NULL);
1098 session = dialog->queue_list->data;
1099 sockinfo = session->pop3_state->sockinfo;
1101 if (!sockinfo || session->atm->terminated == TRUE) return;
1103 session->pop3_state->inc_state = INC_CANCEL;
1104 pop3_automaton_terminate(sockinfo, session->atm);
1105 session->pop3_state->sockinfo = NULL;
1108 gboolean inc_is_active(void)
1110 return (inc_dialog_list != NULL);
1113 void inc_cancel_all(void)
1117 for (cur = inc_dialog_list; cur != NULL; cur = cur->next)
1118 inc_cancel((IncProgressDialog *)cur->data);
1121 static void inc_cancel_cb(GtkWidget *widget, gpointer data)
1123 inc_cancel((IncProgressDialog *)data);
1126 static gint inc_spool(void)
1128 gchar *mbox, *logname;
1131 logname = g_get_user_name();
1132 mbox = g_strconcat(prefs_common.spool_path
1133 ? prefs_common.spool_path : DEFAULT_SPOOL_PATH,
1134 G_DIR_SEPARATOR_S, logname, NULL);
1135 msgs = get_spool(folder_get_default_inbox(), mbox);
1141 static void inc_spool_account(PrefsAccount *account)
1144 FolderItem *dropfolder;
1147 if (account->inbox) {
1148 inbox = folder_find_item_from_path(account->inbox);
1150 inbox = folder_get_default_inbox();
1152 inbox = folder_get_default_inbox();
1154 get_spool(inbox, account->local_mbox);
1157 static void inc_all_spool(void)
1161 list = account_get_list();
1164 for (; list != NULL; list = list->next) {
1165 IncSession *session;
1166 PrefsAccount *account = list->data;
1168 if ((account->protocol == A_LOCAL) &&
1169 (account->recv_at_getall))
1170 inc_spool_account(account);
1174 static gint get_spool(FolderItem *dest, const gchar *mbox)
1178 gchar tmp_mbox[MAXPATHLEN + 1];
1179 GHashTable *folder_table = NULL;
1181 g_return_val_if_fail(dest != NULL, -1);
1182 g_return_val_if_fail(mbox != NULL, -1);
1184 if (!is_file_exist(mbox) || (size = get_file_size(mbox)) == 0) {
1185 debug_print(_("no messages in local mailbox.\n"));
1187 } else if (size < 0)
1190 if ((lockfd = lock_mbox(mbox, LOCK_FLOCK)) < 0)
1193 g_snprintf(tmp_mbox, sizeof(tmp_mbox), "%s%ctmpmbox%d",
1194 get_rc_dir(), G_DIR_SEPARATOR, (gint)mbox);
1196 if (copy_mbox(mbox, tmp_mbox) < 0) {
1197 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1201 debug_print(_("Getting new messages from %s into %s...\n"),
1204 if (prefs_common.filter_on_inc)
1205 folder_table = g_hash_table_new(NULL, NULL);
1206 msgs = proc_mbox(dest, tmp_mbox, folder_table);
1209 if (msgs >= 0) empty_mbox(mbox);
1210 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1213 if (!prefs_common.scan_all_after_inc) {
1214 g_hash_table_insert(folder_table, dest,
1215 GINT_TO_POINTER(1));
1216 folder_item_scan_foreach(folder_table);
1217 folderview_update_item_foreach(folder_table);
1219 g_hash_table_destroy(folder_table);
1220 } else if (!prefs_common.scan_all_after_inc) {
1221 folder_item_scan(dest);
1222 folderview_update_item(dest, FALSE);
1233 void inc_unlock(void)
1235 if (inc_lock_count > 0)
1239 static guint autocheck_timer = 0;
1240 static gpointer autocheck_data = NULL;
1242 static void inc_notify_cmd(gint new_msgs, gboolean notify)
1247 if (!(new_msgs && notify && prefs_common.newmail_notify_cmd &&
1248 *prefs_common.newmail_notify_cmd))
1250 if ((buf = strchr(prefs_common.newmail_notify_cmd, '%')) &&
1251 buf[1] == 'd' && !strchr(&buf[1], '%'))
1252 buf = g_strdup_printf(prefs_common.newmail_notify_cmd,
1255 buf = g_strdup(prefs_common.newmail_notify_cmd);
1257 execute_command_line(buf, TRUE);
1262 void inc_autocheck_timer_init(MainWindow *mainwin)
1264 autocheck_data = mainwin;
1265 inc_autocheck_timer_set();
1268 static void inc_autocheck_timer_set_interval(guint interval)
1270 inc_autocheck_timer_remove();
1271 /* last test is to avoid re-enabling auto_check after modifying
1272 the common preferences */
1273 if (prefs_common.autochk_newmail && autocheck_data
1274 && prefs_common.work_offline == FALSE) {
1275 autocheck_timer = gtk_timeout_add
1276 (interval, inc_autocheck_func, autocheck_data);
1277 debug_print("added timer = %d\n", autocheck_timer);
1281 void inc_autocheck_timer_set(void)
1283 inc_autocheck_timer_set_interval(prefs_common.autochk_itv * 60000);
1286 void inc_autocheck_timer_remove(void)
1288 if (autocheck_timer) {
1289 debug_print("removed timer = %d\n", autocheck_timer);
1290 gtk_timeout_remove(autocheck_timer);
1291 autocheck_timer = 0;
1295 static gint inc_autocheck_func(gpointer data)
1297 MainWindow *mainwin = (MainWindow *)data;
1299 if (inc_lock_count) {
1300 debug_print("autocheck is locked.\n");
1301 inc_autocheck_timer_set_interval(1000);
1305 inc_all_account_mail(mainwin, prefs_common.newmail_notify_auto);