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_put_error (IncState istate);
102 static void inc_cancel (GtkWidget *widget,
105 static gint inc_spool (void);
106 static gint get_spool (FolderItem *dest,
110 static void inc_finished(MainWindow *mainwin)
114 if (prefs_common.open_inbox_on_inc) {
115 item = cur_account && cur_account->inbox
116 ? folder_find_item_from_path(cur_account->inbox)
117 : folder_get_default_inbox();
118 folderview_unselect(mainwin->folderview);
119 folderview_select(mainwin->folderview, item);
121 item = mainwin->summaryview->folder_item;
122 folderview_unselect(mainwin->folderview);
123 folderview_select(mainwin->folderview, item);
127 void inc_mail(MainWindow *mainwin)
129 summary_write_cache(mainwin->summaryview);
131 if (prefs_common.use_extinc && prefs_common.extinc_path) {
134 /* external incorporating program */
135 if ((pid = fork()) < 0) {
141 execlp(prefs_common.extinc_path,
142 g_basename(prefs_common.extinc_path),
145 /* this will be called when failed */
150 /* wait until child process is terminated */
151 waitpid(pid, NULL, 0);
153 if (prefs_common.inc_local) inc_spool();
155 if (prefs_common.inc_local) inc_spool();
157 inc_account_mail(cur_account, mainwin);
160 inc_finished(mainwin);
163 static void inc_account_mail(PrefsAccount *account, MainWindow *mainwin)
165 IncProgressDialog *inc_dialog;
169 session = inc_session_new(account);
170 if (!session) return;
172 inc_dialog = inc_progress_dialog_create();
173 inc_dialog->queue_list = g_list_append(inc_dialog->queue_list, session);
174 inc_dialog->mainwin = mainwin;
175 session->data = inc_dialog;
178 text[1] = account->account_name;
179 text[2] = _("Standby");
180 gtk_clist_append(GTK_CLIST(inc_dialog->dialog->clist), text);
182 inc_start(inc_dialog);
185 void inc_all_account_mail(MainWindow *mainwin)
187 GList *list, *queue_list = NULL;
188 IncProgressDialog *inc_dialog;
190 summary_write_cache(mainwin->summaryview);
192 if (prefs_common.inc_local) inc_spool();
194 list = account_get_list();
197 for (; list != NULL; list = list->next) {
199 PrefsAccount *account = list->data;
201 if (account->recv_at_getall) {
202 session = inc_session_new(account);
204 queue_list = g_list_append(queue_list, session);
208 if (!queue_list) return;
210 inc_dialog = inc_progress_dialog_create();
211 inc_dialog->queue_list = queue_list;
212 inc_dialog->mainwin = mainwin;
213 for (list = queue_list; list != NULL; list = list->next) {
214 IncSession *session = list->data;
217 session->data = inc_dialog;
220 text[1] = session->pop3_state->ac_prefs->account_name;
221 text[2] = _("Standby");
222 gtk_clist_append(GTK_CLIST(inc_dialog->dialog->clist), text);
225 inc_start(inc_dialog);
227 inc_finished(mainwin);
230 static IncProgressDialog *inc_progress_dialog_create(void)
232 IncProgressDialog *dialog;
233 ProgressDialog *progress;
235 dialog = g_new0(IncProgressDialog, 1);
237 progress = progress_dialog_create();
238 gtk_window_set_title(GTK_WINDOW(progress->window),
239 _("Retrieving new messages"));
240 gtk_signal_connect(GTK_OBJECT(progress->cancel_btn), "clicked",
241 GTK_SIGNAL_FUNC(inc_cancel), dialog);
242 gtk_signal_connect(GTK_OBJECT(progress->window), "delete_event",
243 GTK_SIGNAL_FUNC(gtk_true), NULL);
244 manage_window_set_transient(GTK_WINDOW(progress->window));
246 progress_dialog_set_value(progress, 0.0);
248 gtk_widget_show(progress->window);
250 PIXMAP_CREATE(progress->clist, okxpm, okxpmmask, complete_xpm);
251 PIXMAP_CREATE(progress->clist,
252 currentxpm, currentxpmmask, continue_xpm);
253 PIXMAP_CREATE(progress->clist, errorxpm, errorxpmmask, error_xpm);
255 gtk_widget_show_now(progress->window);
257 dialog->dialog = progress;
258 dialog->queue_list = NULL;
263 static void inc_progress_dialog_clear(IncProgressDialog *inc_dialog)
265 progress_dialog_set_value(inc_dialog->dialog, 0.0);
266 progress_dialog_set_label(inc_dialog->dialog, "");
269 static void inc_progress_dialog_destroy(IncProgressDialog *inc_dialog)
271 g_return_if_fail(inc_dialog != NULL);
273 progress_dialog_destroy(inc_dialog->dialog);
278 static IncSession *inc_session_new(PrefsAccount *account)
282 g_return_val_if_fail(account != NULL, NULL);
284 if (account->protocol != A_POP3 && account->protocol != A_APOP)
286 if (!account->recv_server || !account->userid)
289 session = g_new0(IncSession, 1);
290 session->pop3_state = inc_pop3_state_new(account);
291 session->pop3_state->session = session;
296 static void inc_session_destroy(IncSession *session)
298 g_return_if_fail(session != NULL);
300 inc_pop3_state_destroy(session->pop3_state);
304 static Pop3State *inc_pop3_state_new(PrefsAccount *account)
308 state = g_new0(Pop3State, 1);
310 state->ac_prefs = account;
311 state->folder_table = g_hash_table_new(NULL, NULL);
312 state->id_table = inc_get_uidl_table(account);
313 state->id_list = NULL;
314 state->new_id_list = NULL;
315 state->inc_state = INC_SUCCESS;
320 static void inc_pop3_state_destroy(Pop3State *state)
322 g_hash_table_destroy(state->folder_table);
323 if (state->id_table) {
324 hash_free_strings(state->id_table);
325 g_hash_table_destroy(state->id_table);
327 slist_free_strings(state->id_list);
328 slist_free_strings(state->new_id_list);
329 g_slist_free(state->id_list);
330 g_slist_free(state->new_id_list);
332 g_free(state->greeting);
335 g_free(state->prev_folder);
340 static void inc_start(IncProgressDialog *inc_dialog)
343 GtkCList *clist = GTK_CLIST(inc_dialog->dialog->clist);
344 Pop3State *pop3_state;
348 while (inc_dialog->queue_list != NULL) {
349 session = inc_dialog->queue_list->data;
350 pop3_state = session->pop3_state;
352 inc_progress_dialog_clear(inc_dialog);
354 pop3_state->user = g_strdup(pop3_state->ac_prefs->userid);
355 if (pop3_state->ac_prefs->passwd)
357 g_strdup(pop3_state->ac_prefs->passwd);
358 else if (pop3_state->ac_prefs->tmp_pass)
360 g_strdup(pop3_state->ac_prefs->tmp_pass);
365 message = g_strdup_printf
366 (_("Input password for %s on %s:"),
368 pop3_state->ac_prefs->recv_server);
370 pass = input_dialog_with_invisible(_("Input password"),
373 manage_window_focus_in(inc_dialog->mainwin->window,
376 pop3_state->ac_prefs->tmp_pass = g_strdup(pass);
377 pop3_state->pass = pass;
379 inc_session_destroy(session);
380 inc_dialog->queue_list = g_list_remove
381 (inc_dialog->queue_list, session);
386 gtk_clist_set_pixmap(clist, num, 0, currentxpm, currentxpmmask);
387 gtk_clist_set_text(clist, num, 2, _("Retrieving"));
389 /* begin POP3 session */
390 inc_state = inc_pop3_session_do(session);
392 if (inc_state == INC_SUCCESS || inc_state == INC_CANCEL) {
393 gtk_clist_set_pixmap(clist, num, 0, okxpm, okxpmmask);
394 gtk_clist_set_text(clist, num, 2, _("Done"));
396 gtk_clist_set_pixmap(clist, num, 0, errorxpm, errorxpmmask);
397 gtk_clist_set_text(clist, num, 2, _("Error"));
400 if (pop3_state->error_val == PS_AUTHFAIL) {
401 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
403 (_("Authorization for %s on %s failed"),
405 pop3_state->ac_prefs->recv_server);
409 manage_window_focus_in(inc_dialog->mainwin->window, NULL, NULL);
411 folder_item_scan_foreach(pop3_state->folder_table);
412 folderview_update_item_foreach(pop3_state->folder_table);
414 if (pop3_state->error_val == PS_AUTHFAIL &&
415 pop3_state->ac_prefs->tmp_pass) {
416 g_free(pop3_state->ac_prefs->tmp_pass);
417 pop3_state->ac_prefs->tmp_pass = NULL;
420 inc_write_uidl_list(pop3_state);
422 if (inc_state != INC_SUCCESS) {
423 inc_put_error(inc_state);
427 inc_session_destroy(session);
428 inc_dialog->queue_list =
429 g_list_remove(inc_dialog->queue_list, session);
434 while (inc_dialog->queue_list != NULL) {
435 session = inc_dialog->queue_list->data;
436 inc_session_destroy(session);
437 inc_dialog->queue_list =
438 g_list_remove(inc_dialog->queue_list, session);
441 inc_progress_dialog_destroy(inc_dialog);
445 static IncState inc_pop3_session_do(IncSession *session)
447 Pop3State *pop3_state = session->pop3_state;
448 IncProgressDialog *inc_dialog = (IncProgressDialog *)session->data;
455 AtmHandler handlers[] = {
457 pop3_getauth_user_send , pop3_getauth_user_recv,
458 pop3_getauth_pass_send , pop3_getauth_pass_recv,
459 pop3_getauth_apop_send , pop3_getauth_apop_recv,
460 pop3_getrange_stat_send , pop3_getrange_stat_recv,
461 pop3_getrange_last_send , pop3_getrange_last_recv,
462 pop3_getrange_uidl_send , pop3_getrange_uidl_recv,
463 pop3_retr_send , pop3_retr_recv,
464 pop3_delete_send , pop3_delete_recv,
465 pop3_logout_send , pop3_logout_recv
468 debug_print(_("getting new messages of account %s...\n"),
469 pop3_state->ac_prefs->account_name);
471 atm = automaton_create(N_POP3_PHASE);
474 atm->data = pop3_state;
476 buf = g_strdup_printf(_("%s: Retrieving new messages"),
477 pop3_state->ac_prefs->recv_server);
478 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window), buf);
481 for (i = POP3_GREETING_RECV; i < N_POP3_PHASE; i++)
482 atm->state[i].handler = handlers[i];
483 atm->state[POP3_GREETING_RECV].condition = GDK_INPUT_READ;
484 for (i = POP3_GETAUTH_USER_SEND; i < N_POP3_PHASE; ) {
485 atm->state[i++].condition = GDK_INPUT_WRITE;
486 atm->state[i++].condition = GDK_INPUT_READ;
489 atm->terminate = (AtmHandler)pop3_automaton_terminate;
491 atm->num = POP3_GREETING_RECV;
493 server = pop3_state->ac_prefs->recv_server;
494 port = pop3_state->ac_prefs->set_popport ?
495 pop3_state->ac_prefs->popport : 110;
497 buf = g_strdup_printf(_("Connecting to POP3 server: %s ..."), server);
498 log_message("%s\n", buf);
499 progress_dialog_set_label(inc_dialog->dialog, buf);
504 if ((sockinfo = sock_connect_with_thread(server, port)) == NULL) {
506 if ((sockinfo = sock_connect_nb(server, port)) == NULL) {
508 log_warning(_("Can't connect to POP3 server: %s:%d\n"),
510 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
511 alertpanel_error(_("Can't connect to POP3 server: %s:%d"),
513 manage_window_focus_out(inc_dialog->dialog->window, NULL, NULL);
514 pop3_automaton_terminate(NULL, atm);
515 automaton_destroy(atm);
520 /* :WK: Hmmm, with the later sock_gdk_input, we have 2 references
521 * to the sock structure - implement a reference counter?? */
522 pop3_state->sockinfo = sockinfo;
523 atm->help_sock = sockinfo;
526 atm->timeout_tag = gtk_timeout_add
527 (TIMEOUT_ITV, (GtkFunction)connection_check_cb, atm);
529 atm->tag = sock_gdk_input_add(sockinfo,
530 atm->state[atm->num].condition,
531 automaton_input_cb, atm);
537 //pthread_join(sockinfo->connect_thr, NULL);
539 automaton_destroy(atm);
541 return pop3_state->inc_state;
544 static gint pop3_automaton_terminate(SockInfo *source, Automaton *atm)
547 gdk_input_remove(atm->tag);
550 if (atm->timeout_tag > 0) {
551 gtk_timeout_remove(atm->timeout_tag);
552 atm->timeout_tag = 0;
559 atm->terminated = TRUE;
564 static GHashTable *inc_get_uidl_table(PrefsAccount *ac_prefs)
569 gchar buf[IDLEN + 3];
571 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
572 "uidl-", ac_prefs->recv_server,
573 "-", ac_prefs->userid, NULL);
574 if ((fp = fopen(path, "r")) == NULL) {
575 if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
581 table = g_hash_table_new(g_str_hash, g_str_equal);
583 while (fgets(buf, sizeof(buf), fp) != NULL) {
585 g_hash_table_insert(table, g_strdup(buf), GINT_TO_POINTER(1));
593 static void inc_write_uidl_list(Pop3State *state)
599 if (!state->id_list) return;
601 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
602 "uidl-", state->ac_prefs->recv_server,
603 "-", state->user, NULL);
604 if ((fp = fopen(path, "w")) == NULL) {
605 FILE_OP_ERROR(path, "fopen");
610 for (cur = state->id_list; cur != NULL; cur = cur->next) {
611 if (fputs((gchar *)cur->data, fp) == EOF) {
612 FILE_OP_ERROR(path, "fputs");
615 if (fputc('\n', fp) == EOF) {
616 FILE_OP_ERROR(path, "fputc");
621 if (fclose(fp) == EOF) FILE_OP_ERROR(path, "fclose");
626 static gint connection_check_cb(Automaton *atm)
628 Pop3State *state = atm->data;
629 IncProgressDialog *inc_dialog = state->session->data;
630 SockInfo *sockinfo = state->sockinfo;
632 //g_print("connection check\n");
634 if (sockinfo->state == CONN_LOOKUPFAILED ||
635 sockinfo->state == CONN_FAILED) {
636 atm->timeout_tag = 0;
637 pop3_automaton_terminate(sockinfo->sock, atm);
638 log_warning(_("Can't connect to POP3 server: %s:%d\n"),
639 sockinfo->hostname, sockinfo->port);
640 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
641 alertpanel_error(_("Can't connect to POP3 server: %s:%d"),
642 sockinfo->hostname, sockinfo->port);
643 manage_window_focus_out(inc_dialog->dialog->window, NULL, NULL);
645 } else if (sockinfo->state == CONN_ESTABLISHED) {
646 atm->timeout_tag = 0;
647 atm->tag = gdk_input_add(sockinfo->sock,
648 atm->state[atm->num].condition,
649 automaton_input_cb, atm);
657 void inc_progress_update(Pop3State *state, Pop3Phase phase)
659 gchar buf[MSGBUFSIZE];
660 IncProgressDialog *inc_dialog = state->session->data;
661 ProgressDialog *dialog = inc_dialog->dialog;
664 case POP3_GREETING_RECV:
666 case POP3_GETAUTH_USER_SEND:
667 case POP3_GETAUTH_USER_RECV:
668 case POP3_GETAUTH_PASS_SEND:
669 case POP3_GETAUTH_PASS_RECV:
670 case POP3_GETAUTH_APOP_SEND:
671 case POP3_GETAUTH_APOP_RECV:
672 progress_dialog_set_label(dialog, _("Authorizing"));
674 case POP3_GETRANGE_STAT_SEND:
675 case POP3_GETRANGE_STAT_RECV:
676 case POP3_GETRANGE_LAST_SEND:
677 case POP3_GETRANGE_LAST_RECV:
678 case POP3_GETRANGE_UIDL_SEND:
679 case POP3_GETRANGE_UIDL_RECV:
680 progress_dialog_set_label(dialog,
681 _("Getting number of new messages"));
685 g_snprintf(buf, sizeof(buf),
686 _("Retrieving message (%d / %d)"),
687 state->cur_msg, state->count);
688 progress_dialog_set_label(dialog, buf);
689 progress_dialog_set_percentage
690 (dialog, (gfloat)state->cur_msg / state->count);
692 case POP3_DELETE_SEND:
693 case POP3_DELETE_RECV:
694 progress_dialog_set_label(dialog, _("Deleting message"));
696 case POP3_LOGOUT_SEND:
697 case POP3_LOGOUT_RECV:
698 progress_dialog_set_label(dialog, _("Quitting"));
704 gint inc_drop_message(const gchar *file, Pop3State *state)
707 FolderItem *dropfolder;
710 if (state->ac_prefs->inbox) {
711 inbox = folder_find_item_from_path(state->ac_prefs->inbox);
713 inbox = folder_get_default_inbox();
715 inbox = folder_get_default_inbox();
721 if (state->ac_prefs->filter_on_recv) {
723 filter_get_dest_folder(prefs_common.fltlist, file);
724 if (!dropfolder) dropfolder = inbox;
725 else if (!strcmp(dropfolder->path, FILTER_NOT_RECEIVE)) {
726 g_warning(_("a message won't be received\n"));
732 val = GPOINTER_TO_INT(g_hash_table_lookup
733 (state->folder_table, dropfolder));
735 folder_item_scan(dropfolder);
736 g_hash_table_insert(state->folder_table, dropfolder,
740 if (folder_item_add_msg(dropfolder, file) < 0) {
749 static void inc_put_error(IncState istate)
753 alertpanel_error(_("Error occurred while processing mail."));
756 alertpanel_error(_("No disk space left."));
762 static void inc_cancel(GtkWidget *widget, gpointer data)
764 IncProgressDialog *dialog = data;
765 IncSession *session = dialog->queue_list->data;
766 SockInfo *sockinfo = session->pop3_state->sockinfo;
769 if (sockinfo->state == CONN_READY ||
770 sockinfo->state == CONN_LOOKUPSUCCESS) {
771 pthread_cancel(sockinfo->connect_thr);
772 //pthread_kill(sockinfo->connect_thr, SIGINT);
773 g_print("connection was cancelled.\n");
777 session->pop3_state->inc_state = INC_CANCEL;
778 pop3_automaton_terminate(sockinfo, session->atm);
781 static gint inc_spool(void)
783 gchar *mbox, *logname;
786 logname = g_get_user_name();
787 mbox = g_strconcat(prefs_common.spool_path
788 ? prefs_common.spool_path : DEFAULT_SPOOL_PATH,
789 G_DIR_SEPARATOR_S, logname, NULL);
790 msgs = get_spool(folder_get_default_inbox(), mbox);
796 static gint get_spool(FolderItem *dest, const gchar *mbox)
800 gchar *tmp_mbox = "/tmp/tmpmbox";
801 GHashTable *folder_table = NULL;
803 g_return_val_if_fail(dest != NULL, -1);
804 g_return_val_if_fail(mbox != NULL, -1);
806 if (!is_file_exist(mbox) || (size = get_file_size(mbox)) == 0) {
807 debug_print(_("no messages in local mailbox.\n"));
812 if ((lockfd = lock_mbox(mbox, LOCK_FLOCK)) < 0)
815 if (copy_mbox(mbox, tmp_mbox) < 0)
818 debug_print(_("Getting new messages from %s into %s...\n"),
821 if (prefs_common.filter_on_inc)
822 folder_table = g_hash_table_new(NULL, NULL);
823 msgs = proc_mbox(dest, tmp_mbox, folder_table);
826 if (msgs >= 0) empty_mbox(mbox);
827 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
830 folder_item_scan_foreach(folder_table);
831 folderview_update_item_foreach(folder_table);
832 g_hash_table_destroy(folder_table);
834 folder_item_scan(dest);
835 folderview_update_item(dest, FALSE);