2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2001 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>
33 #include <sys/types.h>
42 #include "mainwindow.h"
43 #include "folderview.h"
44 #include "summaryview.h"
45 #include "prefs_common.h"
46 #include "prefs_account.h"
55 #include "statusbar.h"
56 #include "manage_window.h"
57 #include "progressdialog.h"
58 #include "inputdialog.h"
59 #include "alertpanel.h"
61 #include "automaton.h"
64 #include "pixmaps/continue.xpm"
65 #include "pixmaps/complete.xpm"
66 #include "pixmaps/error.xpm"
68 GdkPixmap *currentxpm;
69 GdkBitmap *currentxpmmask;
71 GdkBitmap *errorxpmmask;
75 #define MSGBUFSIZE 8192
77 static void inc_finished (MainWindow *mainwin);
78 static void inc_account_mail (PrefsAccount *account,
81 static IncProgressDialog *inc_progress_dialog_create (void);
82 static void inc_progress_dialog_destroy (IncProgressDialog *inc_dialog);
84 static IncSession *inc_session_new (PrefsAccount *account);
85 static void inc_session_destroy (IncSession *session);
86 static Pop3State *inc_pop3_state_new (PrefsAccount *account);
87 static void inc_pop3_state_destroy (Pop3State *state);
88 static void inc_start (IncProgressDialog *inc_dialog);
89 static IncState inc_pop3_session_do (IncSession *session);
90 static gint pop3_automaton_terminate (SockInfo *source,
93 static GHashTable *inc_get_uidl_table (PrefsAccount *ac_prefs);
94 static void inc_write_uidl_list (Pop3State *state);
97 static gint connection_check_cb (Automaton *atm);
100 static void inc_pop3_recv_func (SockInfo *sock,
104 static void inc_put_error (IncState istate);
106 static void inc_cancel (GtkWidget *widget,
109 static gint inc_spool (void);
110 static gint get_spool (FolderItem *dest,
114 static void inc_finished(MainWindow *mainwin)
118 if (prefs_common.open_inbox_on_inc) {
119 item = cur_account && cur_account->inbox
120 ? folder_find_item_from_path(cur_account->inbox)
121 : folder_get_default_inbox();
122 folderview_unselect(mainwin->folderview);
123 folderview_select(mainwin->folderview, item);
125 item = mainwin->summaryview->folder_item;
126 folderview_unselect(mainwin->folderview);
127 folderview_select(mainwin->folderview, item);
131 void inc_mail(MainWindow *mainwin)
133 summary_write_cache(mainwin->summaryview);
135 if (prefs_common.use_extinc && prefs_common.extinc_path) {
138 /* external incorporating program */
139 if ((pid = fork()) < 0) {
145 execlp(prefs_common.extinc_path,
146 g_basename(prefs_common.extinc_path),
149 /* this will be called when failed */
154 /* wait until child process is terminated */
155 waitpid(pid, NULL, 0);
157 if (prefs_common.inc_local) inc_spool();
159 if (prefs_common.inc_local) inc_spool();
161 inc_account_mail(cur_account, mainwin);
164 inc_finished(mainwin);
167 static void inc_account_mail(PrefsAccount *account, MainWindow *mainwin)
169 IncProgressDialog *inc_dialog;
173 session = inc_session_new(account);
174 if (!session) return;
176 inc_dialog = inc_progress_dialog_create();
177 inc_dialog->queue_list = g_list_append(inc_dialog->queue_list, session);
178 inc_dialog->mainwin = mainwin;
179 session->data = inc_dialog;
182 text[1] = account->account_name;
183 text[2] = _("Standby");
184 gtk_clist_append(GTK_CLIST(inc_dialog->dialog->clist), text);
186 inc_start(inc_dialog);
189 void inc_all_account_mail(MainWindow *mainwin)
191 GList *list, *queue_list = NULL;
192 IncProgressDialog *inc_dialog;
194 summary_write_cache(mainwin->summaryview);
196 if (prefs_common.inc_local) inc_spool();
198 list = account_get_list();
201 for (; list != NULL; list = list->next) {
203 PrefsAccount *account = list->data;
205 if (account->recv_at_getall) {
206 session = inc_session_new(account);
208 queue_list = g_list_append(queue_list, session);
212 if (!queue_list) return;
214 inc_dialog = inc_progress_dialog_create();
215 inc_dialog->queue_list = queue_list;
216 inc_dialog->mainwin = mainwin;
217 for (list = queue_list; list != NULL; list = list->next) {
218 IncSession *session = list->data;
221 session->data = inc_dialog;
224 text[1] = session->pop3_state->ac_prefs->account_name;
225 text[2] = _("Standby");
226 gtk_clist_append(GTK_CLIST(inc_dialog->dialog->clist), text);
229 inc_start(inc_dialog);
231 inc_finished(mainwin);
234 static IncProgressDialog *inc_progress_dialog_create(void)
236 IncProgressDialog *dialog;
237 ProgressDialog *progress;
239 dialog = g_new0(IncProgressDialog, 1);
241 progress = progress_dialog_create();
242 gtk_window_set_title(GTK_WINDOW(progress->window),
243 _("Retrieving new messages"));
244 gtk_signal_connect(GTK_OBJECT(progress->cancel_btn), "clicked",
245 GTK_SIGNAL_FUNC(inc_cancel), dialog);
246 gtk_signal_connect(GTK_OBJECT(progress->window), "delete_event",
247 GTK_SIGNAL_FUNC(gtk_true), NULL);
248 manage_window_set_transient(GTK_WINDOW(progress->window));
250 progress_dialog_set_value(progress, 0.0);
252 gtk_widget_show(progress->window);
254 PIXMAP_CREATE(progress->clist, okxpm, okxpmmask, complete_xpm);
255 PIXMAP_CREATE(progress->clist,
256 currentxpm, currentxpmmask, continue_xpm);
257 PIXMAP_CREATE(progress->clist, errorxpm, errorxpmmask, error_xpm);
259 gtk_widget_show_now(progress->window);
261 dialog->dialog = progress;
262 dialog->queue_list = NULL;
267 static void inc_progress_dialog_clear(IncProgressDialog *inc_dialog)
269 progress_dialog_set_value(inc_dialog->dialog, 0.0);
270 progress_dialog_set_label(inc_dialog->dialog, "");
273 static void inc_progress_dialog_destroy(IncProgressDialog *inc_dialog)
275 g_return_if_fail(inc_dialog != NULL);
277 progress_dialog_destroy(inc_dialog->dialog);
282 static IncSession *inc_session_new(PrefsAccount *account)
286 g_return_val_if_fail(account != NULL, NULL);
288 if (account->protocol != A_POP3 && account->protocol != A_APOP)
290 if (!account->recv_server || !account->userid)
293 session = g_new0(IncSession, 1);
294 session->pop3_state = inc_pop3_state_new(account);
295 session->pop3_state->session = session;
300 static void inc_session_destroy(IncSession *session)
302 g_return_if_fail(session != NULL);
304 inc_pop3_state_destroy(session->pop3_state);
308 static Pop3State *inc_pop3_state_new(PrefsAccount *account)
312 state = g_new0(Pop3State, 1);
314 state->ac_prefs = account;
315 state->folder_table = g_hash_table_new(NULL, NULL);
316 state->id_table = inc_get_uidl_table(account);
317 state->id_list = NULL;
318 state->new_id_list = NULL;
319 state->inc_state = INC_SUCCESS;
324 static void inc_pop3_state_destroy(Pop3State *state)
326 g_hash_table_destroy(state->folder_table);
327 g_free(state->sizes);
329 if (state->id_table) {
330 hash_free_strings(state->id_table);
331 g_hash_table_destroy(state->id_table);
333 slist_free_strings(state->id_list);
334 slist_free_strings(state->new_id_list);
335 g_slist_free(state->id_list);
336 g_slist_free(state->new_id_list);
338 g_free(state->greeting);
341 g_free(state->prev_folder);
346 static void inc_start(IncProgressDialog *inc_dialog)
349 GtkCList *clist = GTK_CLIST(inc_dialog->dialog->clist);
350 Pop3State *pop3_state;
354 while (inc_dialog->queue_list != NULL) {
355 session = inc_dialog->queue_list->data;
356 pop3_state = session->pop3_state;
358 inc_progress_dialog_clear(inc_dialog);
360 pop3_state->user = g_strdup(pop3_state->ac_prefs->userid);
361 if (pop3_state->ac_prefs->passwd)
363 g_strdup(pop3_state->ac_prefs->passwd);
364 else if (pop3_state->ac_prefs->tmp_pass)
366 g_strdup(pop3_state->ac_prefs->tmp_pass);
371 message = g_strdup_printf
372 (_("Input password for %s on %s:"),
374 pop3_state->ac_prefs->recv_server);
376 pass = input_dialog_with_invisible(_("Input password"),
379 manage_window_focus_in(inc_dialog->mainwin->window,
382 pop3_state->ac_prefs->tmp_pass = g_strdup(pass);
383 pop3_state->pass = pass;
385 inc_session_destroy(session);
386 inc_dialog->queue_list = g_list_remove
387 (inc_dialog->queue_list, session);
392 gtk_clist_set_pixmap(clist, num, 0, currentxpm, currentxpmmask);
393 gtk_clist_set_text(clist, num, 2, _("Retrieving"));
395 /* begin POP3 session */
396 inc_state = inc_pop3_session_do(session);
398 if (inc_state == INC_SUCCESS || inc_state == INC_CANCEL) {
399 gtk_clist_set_pixmap(clist, num, 0, okxpm, okxpmmask);
400 gtk_clist_set_text(clist, num, 2, _("Done"));
402 gtk_clist_set_pixmap(clist, num, 0, errorxpm, errorxpmmask);
403 gtk_clist_set_text(clist, num, 2, _("Error"));
406 if (pop3_state->error_val == PS_AUTHFAIL) {
407 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
409 (_("Authorization for %s on %s failed"),
411 pop3_state->ac_prefs->recv_server);
415 manage_window_focus_in(inc_dialog->mainwin->window, NULL, NULL);
417 folder_item_scan_foreach(pop3_state->folder_table);
418 folderview_update_item_foreach(pop3_state->folder_table);
420 if (pop3_state->error_val == PS_AUTHFAIL &&
421 pop3_state->ac_prefs->tmp_pass) {
422 g_free(pop3_state->ac_prefs->tmp_pass);
423 pop3_state->ac_prefs->tmp_pass = NULL;
426 inc_write_uidl_list(pop3_state);
428 if (inc_state != INC_SUCCESS) {
429 inc_put_error(inc_state);
433 inc_session_destroy(session);
434 inc_dialog->queue_list =
435 g_list_remove(inc_dialog->queue_list, session);
440 while (inc_dialog->queue_list != NULL) {
441 session = inc_dialog->queue_list->data;
442 inc_session_destroy(session);
443 inc_dialog->queue_list =
444 g_list_remove(inc_dialog->queue_list, session);
447 inc_progress_dialog_destroy(inc_dialog);
451 static IncState inc_pop3_session_do(IncSession *session)
453 Pop3State *pop3_state = session->pop3_state;
454 IncProgressDialog *inc_dialog = (IncProgressDialog *)session->data;
461 static AtmHandler handlers[] = {
463 pop3_getauth_user_send , pop3_getauth_user_recv,
464 pop3_getauth_pass_send , pop3_getauth_pass_recv,
465 pop3_getauth_apop_send , pop3_getauth_apop_recv,
466 pop3_getrange_stat_send , pop3_getrange_stat_recv,
467 pop3_getrange_last_send , pop3_getrange_last_recv,
468 pop3_getrange_uidl_send , pop3_getrange_uidl_recv,
469 pop3_getsize_list_send , pop3_getsize_list_recv,
470 pop3_retr_send , pop3_retr_recv,
471 pop3_delete_send , pop3_delete_recv,
472 pop3_logout_send , pop3_logout_recv
475 debug_print(_("getting new messages of account %s...\n"),
476 pop3_state->ac_prefs->account_name);
478 atm = automaton_create(N_POP3_PHASE);
481 atm->data = pop3_state;
483 buf = g_strdup_printf(_("%s: Retrieving new messages"),
484 pop3_state->ac_prefs->recv_server);
485 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window), buf);
488 for (i = POP3_GREETING_RECV; i < N_POP3_PHASE; i++)
489 atm->state[i].handler = handlers[i];
490 atm->state[POP3_GREETING_RECV].condition = GDK_INPUT_READ;
491 for (i = POP3_GETAUTH_USER_SEND; i < N_POP3_PHASE; ) {
492 atm->state[i++].condition = GDK_INPUT_WRITE;
493 atm->state[i++].condition = GDK_INPUT_READ;
496 atm->terminate = (AtmHandler)pop3_automaton_terminate;
498 atm->num = POP3_GREETING_RECV;
500 server = pop3_state->ac_prefs->recv_server;
501 port = pop3_state->ac_prefs->set_popport ?
502 pop3_state->ac_prefs->popport : 110;
504 buf = g_strdup_printf(_("Connecting to POP3 server: %s ..."), server);
505 log_message("%s\n", buf);
506 progress_dialog_set_label(inc_dialog->dialog, buf);
511 if ((sockinfo = sock_connect_with_thread(server, port)) == NULL) {
513 if ((sockinfo = sock_connect_nb(server, port)) == NULL) {
515 log_warning(_("Can't connect to POP3 server: %s:%d\n"),
517 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
518 alertpanel_error(_("Can't connect to POP3 server: %s:%d"),
520 manage_window_focus_out(inc_dialog->dialog->window, NULL, NULL);
521 pop3_automaton_terminate(NULL, atm);
522 automaton_destroy(atm);
527 /* :WK: Hmmm, with the later sock_gdk_input, we have 2 references
528 * to the sock structure - implement a reference counter?? */
529 pop3_state->sockinfo = sockinfo;
530 atm->help_sock = sockinfo;
532 recv_set_ui_func(inc_pop3_recv_func, session);
535 atm->timeout_tag = gtk_timeout_add
536 (TIMEOUT_ITV, (GtkFunction)connection_check_cb, atm);
538 atm->tag = sock_gdk_input_add(sockinfo,
539 atm->state[atm->num].condition,
540 automaton_input_cb, atm);
545 recv_set_ui_func(NULL, NULL);
548 //pthread_join(sockinfo->connect_thr, NULL);
550 automaton_destroy(atm);
552 return pop3_state->inc_state;
555 static gint pop3_automaton_terminate(SockInfo *source, Automaton *atm)
558 gdk_input_remove(atm->tag);
561 if (atm->timeout_tag > 0) {
562 gtk_timeout_remove(atm->timeout_tag);
563 atm->timeout_tag = 0;
570 atm->terminated = TRUE;
575 static GHashTable *inc_get_uidl_table(PrefsAccount *ac_prefs)
580 gchar buf[IDLEN + 3];
582 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
583 "uidl-", ac_prefs->recv_server,
584 "-", ac_prefs->userid, NULL);
585 if ((fp = fopen(path, "r")) == NULL) {
586 if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
592 table = g_hash_table_new(g_str_hash, g_str_equal);
594 while (fgets(buf, sizeof(buf), fp) != NULL) {
596 g_hash_table_insert(table, g_strdup(buf), GINT_TO_POINTER(1));
604 static void inc_write_uidl_list(Pop3State *state)
610 if (!state->id_list) return;
612 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
613 "uidl-", state->ac_prefs->recv_server,
614 "-", state->user, NULL);
615 if ((fp = fopen(path, "w")) == NULL) {
616 FILE_OP_ERROR(path, "fopen");
621 for (cur = state->id_list; cur != NULL; cur = cur->next) {
622 if (fputs((gchar *)cur->data, fp) == EOF) {
623 FILE_OP_ERROR(path, "fputs");
626 if (fputc('\n', fp) == EOF) {
627 FILE_OP_ERROR(path, "fputc");
632 if (fclose(fp) == EOF) FILE_OP_ERROR(path, "fclose");
637 static gint connection_check_cb(Automaton *atm)
639 Pop3State *state = atm->data;
640 IncProgressDialog *inc_dialog = state->session->data;
641 SockInfo *sockinfo = state->sockinfo;
643 //g_print("connection check\n");
645 if (sockinfo->state == CONN_LOOKUPFAILED ||
646 sockinfo->state == CONN_FAILED) {
647 atm->timeout_tag = 0;
648 pop3_automaton_terminate(sockinfo->sock, atm);
649 log_warning(_("Can't connect to POP3 server: %s:%d\n"),
650 sockinfo->hostname, sockinfo->port);
651 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
652 alertpanel_error(_("Can't connect to POP3 server: %s:%d"),
653 sockinfo->hostname, sockinfo->port);
654 manage_window_focus_out(inc_dialog->dialog->window, NULL, NULL);
656 } else if (sockinfo->state == CONN_ESTABLISHED) {
657 atm->timeout_tag = 0;
658 atm->tag = gdk_input_add(sockinfo->sock,
659 atm->state[atm->num].condition,
660 automaton_input_cb, atm);
668 static void inc_pop3_recv_func(SockInfo *sock, gint read_len, gpointer data)
670 IncSession *session = (IncSession *)data;
671 Pop3State *state = session->pop3_state;
672 IncProgressDialog *inc_dialog = session->data;
673 ProgressDialog *dialog = inc_dialog->dialog;
675 state->cur_msg_bytes += read_len;
677 //g_snprintf(buf, sizeof(buf),
678 // _("Retrieving message (%d / %d)"),
679 // state->cur_msg, state->count);
680 //progress_dialog_set_label(dialog, buf);
682 progress_dialog_set_percentage
684 (gfloat)(state->cur_total_bytes + state->cur_msg_bytes) /
685 (gfloat)(state->total_bytes));
689 void inc_progress_update(Pop3State *state, Pop3Phase phase)
691 gchar buf[MSGBUFSIZE];
692 IncProgressDialog *inc_dialog = state->session->data;
693 ProgressDialog *dialog = inc_dialog->dialog;
696 case POP3_GREETING_RECV:
698 case POP3_GETAUTH_USER_SEND:
699 case POP3_GETAUTH_USER_RECV:
700 case POP3_GETAUTH_PASS_SEND:
701 case POP3_GETAUTH_PASS_RECV:
702 case POP3_GETAUTH_APOP_SEND:
703 case POP3_GETAUTH_APOP_RECV:
704 progress_dialog_set_label(dialog, _("Authorizing"));
706 case POP3_GETRANGE_STAT_SEND:
707 case POP3_GETRANGE_STAT_RECV:
708 case POP3_GETRANGE_LAST_SEND:
709 case POP3_GETRANGE_LAST_RECV:
710 case POP3_GETRANGE_UIDL_SEND:
711 case POP3_GETRANGE_UIDL_RECV:
712 progress_dialog_set_label(dialog,
713 _("Getting number of new messages"));
717 g_snprintf(buf, sizeof(buf),
718 _("Retrieving message (%d / %d)"),
719 state->cur_msg, state->count);
720 progress_dialog_set_label(dialog, buf);
721 //progress_dialog_set_percentage
722 // (dialog, (gfloat)state->cur_msg / state->count);
723 progress_dialog_set_percentage
725 (gfloat)(state->cur_total_bytes) /
726 (gfloat)(state->total_bytes));
728 case POP3_DELETE_SEND:
729 case POP3_DELETE_RECV:
730 progress_dialog_set_label(dialog, _("Deleting message"));
732 case POP3_LOGOUT_SEND:
733 case POP3_LOGOUT_RECV:
734 progress_dialog_set_label(dialog, _("Quitting"));
740 gint inc_drop_message(const gchar *file, Pop3State *state)
743 FolderItem *dropfolder;
746 if (state->ac_prefs->inbox) {
747 inbox = folder_find_item_from_path(state->ac_prefs->inbox);
749 inbox = folder_get_default_inbox();
751 inbox = folder_get_default_inbox();
757 if (state->ac_prefs->filter_on_recv) {
759 filter_get_dest_folder(prefs_common.fltlist, file);
760 if (!dropfolder) dropfolder = inbox;
761 else if (!strcmp(dropfolder->path, FILTER_NOT_RECEIVE)) {
762 g_warning(_("a message won't be received\n"));
768 val = GPOINTER_TO_INT(g_hash_table_lookup
769 (state->folder_table, dropfolder));
771 folder_item_scan(dropfolder);
772 g_hash_table_insert(state->folder_table, dropfolder,
776 if (folder_item_add_msg(dropfolder, file) < 0) {
785 static void inc_put_error(IncState istate)
789 alertpanel_error(_("Error occurred while processing mail."));
792 alertpanel_error(_("No disk space left."));
798 static void inc_cancel(GtkWidget *widget, gpointer data)
800 IncProgressDialog *dialog = data;
801 IncSession *session = dialog->queue_list->data;
802 SockInfo *sockinfo = session->pop3_state->sockinfo;
805 if (sockinfo->state == CONN_READY ||
806 sockinfo->state == CONN_LOOKUPSUCCESS) {
807 pthread_cancel(sockinfo->connect_thr);
808 //pthread_kill(sockinfo->connect_thr, SIGINT);
809 g_print("connection was cancelled.\n");
813 session->pop3_state->inc_state = INC_CANCEL;
814 pop3_automaton_terminate(sockinfo, session->atm);
817 static gint inc_spool(void)
819 gchar *mbox, *logname;
822 logname = g_get_user_name();
823 mbox = g_strconcat(prefs_common.spool_path
824 ? prefs_common.spool_path : DEFAULT_SPOOL_PATH,
825 G_DIR_SEPARATOR_S, logname, NULL);
826 msgs = get_spool(folder_get_default_inbox(), mbox);
832 static gint get_spool(FolderItem *dest, const gchar *mbox)
836 gchar *tmp_mbox = "/tmp/tmpmbox";
837 GHashTable *folder_table = NULL;
839 g_return_val_if_fail(dest != NULL, -1);
840 g_return_val_if_fail(mbox != NULL, -1);
842 if (!is_file_exist(mbox) || (size = get_file_size(mbox)) == 0) {
843 debug_print(_("no messages in local mailbox.\n"));
848 if ((lockfd = lock_mbox(mbox, LOCK_FLOCK)) < 0)
851 if (copy_mbox(mbox, tmp_mbox) < 0)
854 debug_print(_("Getting new messages from %s into %s...\n"),
857 if (prefs_common.filter_on_inc)
858 folder_table = g_hash_table_new(NULL, NULL);
859 msgs = proc_mbox(dest, tmp_mbox, folder_table);
862 if (msgs >= 0) empty_mbox(mbox);
863 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
866 folder_item_scan_foreach(folder_table);
867 folderview_update_item_foreach(folder_table);
868 g_hash_table_destroy(folder_table);
870 folder_item_scan(dest);
871 folderview_update_item(dest, FALSE);