initial message templates support
[claws.git] / src / inc.c
blob35d1bc63e6832e4240ce8953a97cd0498dedeb27
1 /*
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.
20 #ifdef HAVE_CONFIG_H
21 # include "config.h"
22 #endif
24 #include "defs.h"
26 #include <glib.h>
27 #include <gtk/gtkmain.h>
28 #include <gtk/gtkwindow.h>
29 #include <gtk/gtksignal.h>
30 #include <stdio.h>
31 #include <unistd.h>
32 #include <string.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <sys/wait.h>
36 #include <signal.h>
37 #include <errno.h>
39 #include "intl.h"
40 #include "main.h"
41 #include "inc.h"
42 #include "mainwindow.h"
43 #include "folderview.h"
44 #include "summaryview.h"
45 #include "prefs_common.h"
46 #include "prefs_account.h"
47 #include "account.h"
48 #include "procmsg.h"
49 #include "socket.h"
50 #include "pop.h"
51 #include "recv.h"
52 #include "mbox.h"
53 #include "utils.h"
54 #include "gtkutils.h"
55 #include "statusbar.h"
56 #include "manage_window.h"
57 #include "progressdialog.h"
58 #include "inputdialog.h"
59 #include "alertpanel.h"
60 #include "filter.h"
61 #include "automaton.h"
62 #include "folder.h"
63 #include "filtering.h"
65 #include "pixmaps/continue.xpm"
66 #include "pixmaps/complete.xpm"
67 #include "pixmaps/error.xpm"
69 GdkPixmap *currentxpm;
70 GdkBitmap *currentxpmmask;
71 GdkPixmap *errorxpm;
72 GdkBitmap *errorxpmmask;
73 GdkPixmap *okxpm;
74 GdkBitmap *okxpmmask;
76 #define MSGBUFSIZE 8192
78 static void inc_finished (MainWindow *mainwin,
79 gboolean new_messages);
80 static gint inc_account_mail (PrefsAccount *account,
81 MainWindow *mainwin);
83 static IncProgressDialog *inc_progress_dialog_create (void);
84 static void inc_progress_dialog_destroy (IncProgressDialog *inc_dialog);
86 static IncSession *inc_session_new (PrefsAccount *account);
87 static void inc_session_destroy (IncSession *session);
88 static Pop3State *inc_pop3_state_new (PrefsAccount *account);
89 static void inc_pop3_state_destroy (Pop3State *state);
90 static gint inc_start (IncProgressDialog *inc_dialog);
91 static IncState inc_pop3_session_do (IncSession *session);
92 static gint pop3_automaton_terminate (SockInfo *source,
93 Automaton *atm);
95 static GHashTable *inc_get_uidl_table (PrefsAccount *ac_prefs);
96 static void inc_write_uidl_list (Pop3State *state);
98 #if USE_THREADS
99 static gint connection_check_cb (Automaton *atm);
100 #endif
102 static void inc_pop3_recv_func (SockInfo *sock,
103 gint count,
104 gint read_bytes,
105 gpointer data);
107 static void inc_put_error (IncState istate);
109 static void inc_cancel (GtkWidget *widget,
110 gpointer data);
112 static gint inc_spool (void);
113 static gint get_spool (FolderItem *dest,
114 const gchar *mbox);
116 static void inc_all_spool(void);
118 static gint inc_autocheck_func (gpointer data);
121 * inc_finished:
122 * @mainwin: Main window.
123 * @new_messages: TRUE if some messages have been received.
125 * Update the folder view and the summary view after receiving
126 * messages. If @new_messages is FALSE, this function avoids unneeded
127 * updating.
129 static void inc_finished(MainWindow *mainwin, gboolean new_messages)
131 FolderItem *item;
133 if (prefs_common.scan_all_after_inc)
134 folderview_update_all_node();
135 /* XXX: major problems right here. if we change marks after
136 * incorporation of mail, folderview_select() rewrites it
137 * right under our nose. folderview_select() eventually
138 * calls summary_show(), which rewrites the cache twice:
139 * one for the previously selected FolderItem*, and one
140 * for the newly selected FolderItem*
142 * since filtering also allows changing mark files,
143 * i've solved this by using a global variable (in
144 * SummmaryView*). a better solution is to use the folder
145 * hash table, and see whether the newly and currently
146 * selected FolderItem* where updated by the filtering. */
148 mainwin->summaryview->filtering_happened = TRUE;
150 /* XXX: filtering_happened is reset by summary_show() */
152 if (!new_messages && !prefs_common.scan_all_after_inc) return;
154 if (prefs_common.open_inbox_on_inc) {
155 item = cur_account && cur_account->inbox
156 ? folder_find_item_from_path(cur_account->inbox)
157 : folder_get_default_inbox();
158 folderview_unselect(mainwin->folderview);
159 folderview_select(mainwin->folderview, item);
160 } else {
161 item = mainwin->summaryview->folder_item;
162 folderview_unselect(mainwin->folderview);
163 folderview_select(mainwin->folderview, item);
167 void inc_mail(MainWindow *mainwin)
169 gint new_msgs = 0;
171 inc_autocheck_timer_remove();
172 summary_write_cache(mainwin->summaryview);
173 main_window_lock(mainwin);
175 if (prefs_common.use_extinc && prefs_common.extinc_path) {
176 gint pid;
178 /* external incorporating program */
179 if ((pid = fork()) < 0) {
180 perror("fork");
181 main_window_unlock(mainwin);
182 inc_autocheck_timer_set();
183 return;
186 if (pid == 0) {
187 execlp(prefs_common.extinc_path,
188 g_basename(prefs_common.extinc_path),
189 NULL);
191 /* this will be called when failed */
192 perror("exec");
193 _exit(1);
196 /* wait until child process is terminated */
197 waitpid(pid, NULL, 0);
199 if (prefs_common.inc_local)
200 new_msgs = inc_spool();
201 } else {
202 if (prefs_common.inc_local)
203 new_msgs = inc_spool();
205 new_msgs += inc_account_mail(cur_account, mainwin);
208 inc_finished(mainwin, new_msgs > 0);
209 main_window_unlock(mainwin);
210 inc_autocheck_timer_set();
213 static gint inc_account_mail(PrefsAccount *account, MainWindow *mainwin)
215 IncProgressDialog *inc_dialog;
216 IncSession *session;
217 gchar *text[3];
219 session = inc_session_new(account);
220 if (!session) return 0;
222 inc_dialog = inc_progress_dialog_create();
223 inc_dialog->queue_list = g_list_append(inc_dialog->queue_list, session);
224 inc_dialog->mainwin = mainwin;
225 session->data = inc_dialog;
227 text[0] = NULL;
228 text[1] = account->account_name;
229 text[2] = _("Standby");
230 gtk_clist_append(GTK_CLIST(inc_dialog->dialog->clist), text);
232 return inc_start(inc_dialog);
235 void inc_all_account_mail(MainWindow *mainwin)
237 GList *list, *queue_list = NULL;
238 IncProgressDialog *inc_dialog;
239 gint new_msgs = 0;
241 inc_autocheck_timer_remove();
242 summary_write_cache(mainwin->summaryview);
243 main_window_lock(mainwin);
245 if (prefs_common.inc_local)
246 new_msgs = inc_spool();
248 list = account_get_list();
249 if (!list) {
250 inc_finished(mainwin, new_msgs > 0);
251 main_window_unlock(mainwin);
252 inc_autocheck_timer_set();
253 return;
256 for (; list != NULL; list = list->next) {
257 IncSession *session;
258 PrefsAccount *account = list->data;
260 if (account->recv_at_getall) {
261 session = inc_session_new(account);
262 if (session)
263 queue_list = g_list_append(queue_list, session);
267 if (!queue_list) {
268 inc_finished(mainwin, new_msgs > 0);
269 main_window_unlock(mainwin);
270 inc_autocheck_timer_set();
271 return;
274 inc_dialog = inc_progress_dialog_create();
275 inc_dialog->queue_list = queue_list;
276 inc_dialog->mainwin = mainwin;
277 for (list = queue_list; list != NULL; list = list->next) {
278 IncSession *session = list->data;
279 gchar *text[3];
281 session->data = inc_dialog;
283 text[0] = NULL;
284 text[1] = session->pop3_state->ac_prefs->account_name;
285 text[2] = _("Standby");
286 gtk_clist_append(GTK_CLIST(inc_dialog->dialog->clist), text);
289 new_msgs += inc_start(inc_dialog);
291 inc_finished(mainwin, new_msgs > 0);
292 main_window_unlock(mainwin);
293 inc_autocheck_timer_set();
296 static IncProgressDialog *inc_progress_dialog_create(void)
298 IncProgressDialog *dialog;
299 ProgressDialog *progress;
301 dialog = g_new0(IncProgressDialog, 1);
303 progress = progress_dialog_create();
304 gtk_window_set_title(GTK_WINDOW(progress->window),
305 _("Retrieving new messages"));
306 gtk_signal_connect(GTK_OBJECT(progress->cancel_btn), "clicked",
307 GTK_SIGNAL_FUNC(inc_cancel), dialog);
308 gtk_signal_connect(GTK_OBJECT(progress->window), "delete_event",
309 GTK_SIGNAL_FUNC(gtk_true), NULL);
311 progress_dialog_set_value(progress, 0.0);
313 PIXMAP_CREATE(progress->clist, okxpm, okxpmmask, complete_xpm);
314 PIXMAP_CREATE(progress->clist,
315 currentxpm, currentxpmmask, continue_xpm);
316 PIXMAP_CREATE(progress->clist, errorxpm, errorxpmmask, error_xpm);
318 if (prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS ||
319 (prefs_common.recv_dialog_mode == RECV_DIALOG_ACTIVE &&
320 manage_window_get_focus_window())) {
321 dialog->show_dialog = TRUE;
322 gtk_widget_show_now(progress->window);
325 dialog->dialog = progress;
326 dialog->queue_list = NULL;
328 return dialog;
331 static void inc_progress_dialog_clear(IncProgressDialog *inc_dialog)
333 progress_dialog_set_value(inc_dialog->dialog, 0.0);
334 progress_dialog_set_label(inc_dialog->dialog, "");
337 static void inc_progress_dialog_destroy(IncProgressDialog *inc_dialog)
339 g_return_if_fail(inc_dialog != NULL);
341 progress_dialog_destroy(inc_dialog->dialog);
343 g_free(inc_dialog);
346 static IncSession *inc_session_new(PrefsAccount *account)
348 IncSession *session;
350 g_return_val_if_fail(account != NULL, NULL);
352 if (account->protocol != A_POP3 && account->protocol != A_APOP)
353 return NULL;
354 if (!account->recv_server || !account->userid)
355 return NULL;
357 session = g_new0(IncSession, 1);
358 session->pop3_state = inc_pop3_state_new(account);
359 session->pop3_state->session = session;
361 return session;
364 static void inc_session_destroy(IncSession *session)
366 g_return_if_fail(session != NULL);
368 inc_pop3_state_destroy(session->pop3_state);
369 g_free(session);
372 static Pop3State *inc_pop3_state_new(PrefsAccount *account)
374 Pop3State *state;
376 state = g_new0(Pop3State, 1);
378 state->ac_prefs = account;
379 state->folder_table = g_hash_table_new(NULL, NULL);
380 state->id_table = inc_get_uidl_table(account);
381 state->id_list = NULL;
382 state->new_id_list = NULL;
383 state->inc_state = INC_SUCCESS;
385 return state;
388 static void inc_pop3_state_destroy(Pop3State *state)
390 g_hash_table_destroy(state->folder_table);
391 g_free(state->sizes);
393 if (state->id_table) {
394 hash_free_strings(state->id_table);
395 g_hash_table_destroy(state->id_table);
397 slist_free_strings(state->id_list);
398 slist_free_strings(state->new_id_list);
399 g_slist_free(state->id_list);
400 g_slist_free(state->new_id_list);
402 g_free(state->greeting);
403 g_free(state->user);
404 g_free(state->pass);
405 g_free(state->prev_folder);
407 g_free(state);
410 static gint inc_start(IncProgressDialog *inc_dialog)
412 IncSession *session;
413 GtkCList *clist = GTK_CLIST(inc_dialog->dialog->clist);
414 Pop3State *pop3_state;
415 IncState inc_state;
416 gint num = 0;
417 gint error_num = 0;
418 gint new_msgs = 0;
420 while (inc_dialog->queue_list != NULL) {
421 session = inc_dialog->queue_list->data;
422 pop3_state = session->pop3_state;
424 inc_progress_dialog_clear(inc_dialog);
426 pop3_state->user = g_strdup(pop3_state->ac_prefs->userid);
427 if (pop3_state->ac_prefs->passwd)
428 pop3_state->pass =
429 g_strdup(pop3_state->ac_prefs->passwd);
430 else if (pop3_state->ac_prefs->tmp_pass)
431 pop3_state->pass =
432 g_strdup(pop3_state->ac_prefs->tmp_pass);
433 else {
434 gchar *pass;
435 gchar *message;
437 message = g_strdup_printf
438 (_("Input password for %s on %s:"),
439 pop3_state->user,
440 pop3_state->ac_prefs->recv_server);
442 pass = input_dialog_with_invisible(_("Input password"),
443 message, NULL);
444 g_free(message);
445 if (inc_dialog->show_dialog)
446 manage_window_focus_in
447 (inc_dialog->mainwin->window,
448 NULL, NULL);
449 if (pass) {
450 pop3_state->ac_prefs->tmp_pass = g_strdup(pass);
451 pop3_state->pass = pass;
452 } else {
453 inc_session_destroy(session);
454 inc_dialog->queue_list = g_list_remove
455 (inc_dialog->queue_list, session);
456 continue;
460 gtk_clist_set_pixmap(clist, num, 0, currentxpm, currentxpmmask);
461 gtk_clist_set_text(clist, num, 2, _("Retrieving"));
463 /* begin POP3 session */
464 inc_state = inc_pop3_session_do(session);
466 if (inc_state == INC_SUCCESS) {
467 gtk_clist_set_pixmap(clist, num, 0, okxpm, okxpmmask);
468 gtk_clist_set_text(clist, num, 2, _("Done"));
469 } else if (inc_state == INC_CANCEL) {
470 gtk_clist_set_pixmap(clist, num, 0, okxpm, okxpmmask);
471 gtk_clist_set_text(clist, num, 2, _("Cancelled"));
472 } else {
473 gtk_clist_set_pixmap(clist, num, 0, errorxpm, errorxpmmask);
474 if (inc_state == INC_CONNECT_ERROR)
475 gtk_clist_set_text(clist, num, 2,
476 _("Connection failed"));
477 else if (inc_state == INC_AUTH_FAILED)
478 gtk_clist_set_text(clist, num, 2,
479 _("Auth failed"));
480 else
481 gtk_clist_set_text(clist, num, 2, _("Error"));
484 if (pop3_state->error_val == PS_AUTHFAIL) {
485 if(!prefs_common.noerrorpanel) {
486 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
487 ((prefs_common.recv_dialog_mode == RECV_DIALOG_ACTIVE) && focus_window)) {
488 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
490 alertpanel_error
491 (_("Authorization for %s on %s failed"),
492 pop3_state->user,
493 pop3_state->ac_prefs->recv_server);
497 statusbar_pop_all();
499 new_msgs += pop3_state->cur_total_num;
501 if (!prefs_common.scan_all_after_inc) {
502 folder_item_scan_foreach(pop3_state->folder_table);
503 folderview_update_item_foreach
504 (pop3_state->folder_table);
507 if (pop3_state->error_val == PS_AUTHFAIL &&
508 pop3_state->ac_prefs->tmp_pass) {
509 g_free(pop3_state->ac_prefs->tmp_pass);
510 pop3_state->ac_prefs->tmp_pass = NULL;
513 inc_write_uidl_list(pop3_state);
515 if (inc_state != INC_SUCCESS && inc_state != INC_CANCEL) {
516 error_num++;
517 if (inc_state == INC_NOSPACE) {
518 inc_put_error(inc_state);
519 break;
523 inc_session_destroy(session);
524 inc_dialog->queue_list =
525 g_list_remove(inc_dialog->queue_list, session);
527 num++;
530 if (error_num) {
531 if (inc_dialog->show_dialog)
532 manage_window_focus_in(inc_dialog->dialog->window,
533 NULL, NULL);
534 alertpanel_error(_("Some errors occured while getting mail."));
535 if (inc_dialog->show_dialog)
536 manage_window_focus_out(inc_dialog->dialog->window,
537 NULL, NULL);
540 while (inc_dialog->queue_list != NULL) {
541 session = inc_dialog->queue_list->data;
542 inc_session_destroy(session);
543 inc_dialog->queue_list =
544 g_list_remove(inc_dialog->queue_list, session);
547 inc_progress_dialog_destroy(inc_dialog);
549 return new_msgs;
553 static IncState inc_pop3_session_do(IncSession *session)
555 Pop3State *pop3_state = session->pop3_state;
556 IncProgressDialog *inc_dialog = (IncProgressDialog *)session->data;
557 Automaton *atm;
558 SockInfo *sockinfo;
559 gint i;
560 gchar *server;
561 gushort port;
562 gchar *buf;
563 static AtmHandler handlers[] = {
564 pop3_greeting_recv ,
565 pop3_getauth_user_send , pop3_getauth_user_recv,
566 pop3_getauth_pass_send , pop3_getauth_pass_recv,
567 pop3_getauth_apop_send , pop3_getauth_apop_recv,
568 pop3_getrange_stat_send , pop3_getrange_stat_recv,
569 pop3_getrange_last_send , pop3_getrange_last_recv,
570 pop3_getrange_uidl_send , pop3_getrange_uidl_recv,
571 pop3_getsize_list_send , pop3_getsize_list_recv,
572 pop3_retr_send , pop3_retr_recv,
573 pop3_delete_send , pop3_delete_recv,
574 pop3_logout_send , pop3_logout_recv
577 debug_print(_("getting new messages of account %s...\n"),
578 pop3_state->ac_prefs->account_name);
580 atm = automaton_create(N_POP3_PHASE);
582 session->atm = atm;
583 atm->data = pop3_state;
585 buf = g_strdup_printf(_("%s: Retrieving new messages"),
586 pop3_state->ac_prefs->recv_server);
587 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window), buf);
588 g_free(buf);
590 for (i = POP3_GREETING_RECV; i < N_POP3_PHASE; i++)
591 atm->state[i].handler = handlers[i];
592 atm->state[POP3_GREETING_RECV].condition = GDK_INPUT_READ;
593 for (i = POP3_GETAUTH_USER_SEND; i < N_POP3_PHASE; ) {
594 atm->state[i++].condition = GDK_INPUT_WRITE;
595 atm->state[i++].condition = GDK_INPUT_READ;
598 atm->terminate = (AtmHandler)pop3_automaton_terminate;
600 atm->num = POP3_GREETING_RECV;
602 server = pop3_state->ac_prefs->recv_server;
603 #if USE_SSL
604 port = pop3_state->ac_prefs->set_popport ?
605 pop3_state->ac_prefs->popport : (pop3_state->ac_prefs->ssl_pop ? 995 : 110);
606 #else
607 port = pop3_state->ac_prefs->set_popport ?
608 pop3_state->ac_prefs->popport : 110;
609 #endif
611 buf = g_strdup_printf(_("Connecting to POP3 server: %s ..."), server);
612 log_message("%s\n", buf);
613 progress_dialog_set_label(inc_dialog->dialog, buf);
614 g_free(buf);
615 GTK_EVENTS_FLUSH();
617 #if USE_THREADS
618 if ((sockinfo = sock_connect_with_thread(server, port)) == NULL) {
619 #else
620 if ((sockinfo = sock_connect(server, port)) == NULL) {
621 #endif
622 log_warning(_("Can't connect to POP3 server: %s:%d\n"),
623 server, port);
624 if(!prefs_common.noerrorpanel) {
625 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
626 ((prefs_common.recv_dialog_mode == RECV_DIALOG_ACTIVE) && focus_window)) {
627 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
629 alertpanel_error(_("Can't connect to POP3 server: %s:%d"),
630 server, port);
631 manage_window_focus_out(inc_dialog->dialog->window, NULL, NULL);
633 pop3_automaton_terminate(NULL, atm);
634 automaton_destroy(atm);
636 return INC_CONNECT_ERROR;
639 /* :WK: Hmmm, with the later sock_gdk_input, we have 2 references
640 * to the sock structure - implement a reference counter?? */
641 pop3_state->sockinfo = sockinfo;
642 atm->help_sock = sockinfo;
644 #ifdef USE_SSL
645 if(pop3_state->ac_prefs->ssl_pop) {
646 if(!ssl_init_socket(sockinfo)) {
647 pop3_automaton_terminate(NULL, atm);
648 automaton_destroy(atm);
650 return INC_CONNECT_ERROR;
652 } else {
653 sockinfo->ssl = NULL;
655 #endif
658 recv_set_ui_func(inc_pop3_recv_func, session);
660 #if USE_THREADS
661 atm->timeout_tag = gtk_timeout_add
662 (TIMEOUT_ITV, (GtkFunction)connection_check_cb, atm);
663 #else
664 atm->tag = sock_gdk_input_add(sockinfo,
665 atm->state[atm->num].condition,
666 automaton_input_cb, atm);
667 #endif
669 gtk_main();
671 recv_set_ui_func(NULL, NULL);
673 #if USE_THREADS
675 pthread_join(sockinfo->connect_thr, NULL);
677 #endif
679 #if USE_SSL
680 ssl_done_socket(sockinfo);
681 #endif
682 automaton_destroy(atm);
684 return pop3_state->inc_state;
687 static gint pop3_automaton_terminate(SockInfo *source, Automaton *atm)
689 if (atm->tag > 0) {
690 gdk_input_remove(atm->tag);
691 atm->tag = 0;
693 if (atm->timeout_tag > 0) {
694 gtk_timeout_remove(atm->timeout_tag);
695 atm->timeout_tag = 0;
697 if (source) {
698 sock_close(source);
699 gtk_main_quit();
702 atm->terminated = TRUE;
704 return 0;
707 static GHashTable *inc_get_uidl_table(PrefsAccount *ac_prefs)
709 GHashTable *table;
710 gchar *path;
711 FILE *fp;
712 gchar buf[IDLEN + 3];
714 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
715 "uidl-", ac_prefs->recv_server,
716 "-", ac_prefs->userid, NULL);
717 if ((fp = fopen(path, "r")) == NULL) {
718 if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
719 g_free(path);
720 return NULL;
722 g_free(path);
724 table = g_hash_table_new(g_str_hash, g_str_equal);
726 while (fgets(buf, sizeof(buf), fp) != NULL) {
727 strretchomp(buf);
728 g_hash_table_insert(table, g_strdup(buf), GINT_TO_POINTER(1));
731 fclose(fp);
733 return table;
736 static void inc_write_uidl_list(Pop3State *state)
738 gchar *path;
739 FILE *fp;
740 GSList *cur;
742 if (!state->id_list) return;
744 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
745 "uidl-", state->ac_prefs->recv_server,
746 "-", state->user, NULL);
747 if ((fp = fopen(path, "w")) == NULL) {
748 FILE_OP_ERROR(path, "fopen");
749 g_free(path);
750 return;
753 for (cur = state->id_list; cur != NULL; cur = cur->next) {
754 if (fputs((gchar *)cur->data, fp) == EOF) {
755 FILE_OP_ERROR(path, "fputs");
756 break;
758 if (fputc('\n', fp) == EOF) {
759 FILE_OP_ERROR(path, "fputc");
760 break;
764 if (fclose(fp) == EOF) FILE_OP_ERROR(path, "fclose");
765 g_free(path);
768 #if USE_THREADS
769 static gint connection_check_cb(Automaton *atm)
771 Pop3State *state = atm->data;
772 IncProgressDialog *inc_dialog = state->session->data;
773 SockInfo *sockinfo = state->sockinfo;
775 /* g_print("connection check\n"); */
777 if (sockinfo->state == CONN_LOOKUPFAILED ||
778 sockinfo->state == CONN_FAILED) {
779 atm->timeout_tag = 0;
780 log_warning(_("Can't connect to POP3 server: %s:%d\n"),
781 sockinfo->hostname, sockinfo->port);
782 if(!prefs_common.noerrorpanel) {
783 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
784 ((prefs_common.recv_dialog_mode == RECV_DIALOG_ACTIVE) && focus_window)) {
785 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
787 alertpanel_error(_("Can't connect to POP3 server: %s:%d"),
788 sockinfo->hostname, sockinfo->port);
789 manage_window_focus_out(inc_dialog->dialog->window, NULL, NULL);
791 pop3_automaton_terminate(sockinfo, atm);
792 return FALSE;
793 } else if (sockinfo->state == CONN_ESTABLISHED) {
794 atm->timeout_tag = 0;
795 atm->tag = sock_gdk_input_add(sockinfo,
796 atm->state[atm->num].condition,
797 automaton_input_cb, atm);
798 return FALSE;
799 } else {
800 return TRUE;
803 #endif
805 static void inc_pop3_recv_func(SockInfo *sock, gint count, gint read_bytes,
806 gpointer data)
808 gchar buf[MSGBUFSIZE];
809 IncSession *session = (IncSession *)data;
810 Pop3State *state = session->pop3_state;
811 IncProgressDialog *inc_dialog = session->data;
812 ProgressDialog *dialog = inc_dialog->dialog;
813 gint cur_total;
814 gchar *total_size;
816 cur_total = state->cur_total_bytes + read_bytes;
817 if (cur_total > state->total_bytes)
818 cur_total = state->total_bytes;
820 Xstrdup_a(total_size, to_human_readable(state->total_bytes), return);
821 g_snprintf(buf, sizeof(buf),
822 _("Retrieving message (%d / %d) (%s / %s)"),
823 state->cur_msg, state->count,
824 to_human_readable(cur_total), total_size);
825 progress_dialog_set_label(dialog, buf);
827 progress_dialog_set_percentage
828 (dialog, (gfloat)cur_total / (gfloat)state->total_bytes);
829 GTK_EVENTS_FLUSH();
832 void inc_progress_update(Pop3State *state, Pop3Phase phase)
834 gchar buf[MSGBUFSIZE];
835 IncProgressDialog *inc_dialog = state->session->data;
836 ProgressDialog *dialog = inc_dialog->dialog;
837 gchar *total_size;
839 switch (phase) {
840 case POP3_GREETING_RECV:
841 break;
842 case POP3_GETAUTH_USER_SEND:
843 case POP3_GETAUTH_USER_RECV:
844 case POP3_GETAUTH_PASS_SEND:
845 case POP3_GETAUTH_PASS_RECV:
846 case POP3_GETAUTH_APOP_SEND:
847 case POP3_GETAUTH_APOP_RECV:
848 progress_dialog_set_label(dialog, _("Authorizing..."));
849 break;
850 case POP3_GETRANGE_STAT_SEND:
851 case POP3_GETRANGE_STAT_RECV:
852 progress_dialog_set_label
853 (dialog, _("Getting the number of new messages (STAT)..."));
854 break;
855 case POP3_GETRANGE_LAST_SEND:
856 case POP3_GETRANGE_LAST_RECV:
857 progress_dialog_set_label
858 (dialog, _("Getting the number of new messages (LAST)..."));
859 break;
860 case POP3_GETRANGE_UIDL_SEND:
861 case POP3_GETRANGE_UIDL_RECV:
862 progress_dialog_set_label
863 (dialog, _("Getting the number of new messages (UIDL)..."));
864 break;
865 case POP3_GETSIZE_LIST_SEND:
866 case POP3_GETSIZE_LIST_RECV:
867 progress_dialog_set_label
868 (dialog, _("Getting the size of messages (LIST)..."));
869 break;
870 case POP3_RETR_SEND:
871 case POP3_RETR_RECV:
872 Xstrdup_a(total_size, to_human_readable(state->total_bytes), return);
873 g_snprintf(buf, sizeof(buf),
874 _("Retrieving message (%d / %d) (%s / %s)"),
875 state->cur_msg, state->count,
876 to_human_readable(state->cur_total_bytes),
877 total_size);
878 progress_dialog_set_label(dialog, buf);
879 progress_dialog_set_percentage
880 (dialog,
881 (gfloat)(state->cur_total_bytes) /
882 (gfloat)(state->total_bytes));
883 break;
884 case POP3_DELETE_SEND:
885 case POP3_DELETE_RECV:
886 progress_dialog_set_label(dialog, _("Deleting message"));
887 break;
888 case POP3_LOGOUT_SEND:
889 case POP3_LOGOUT_RECV:
890 progress_dialog_set_label(dialog, _("Quitting"));
891 break;
892 default:
893 break;
897 gint inc_drop_message(const gchar *file, Pop3State *state)
899 FolderItem *inbox;
900 FolderItem *dropfolder;
901 gint val;
902 gint msgnum;
904 if (state->ac_prefs->inbox) {
905 inbox = folder_find_item_from_path(state->ac_prefs->inbox);
906 if (!inbox)
907 inbox = folder_get_default_inbox();
908 } else
909 inbox = folder_get_default_inbox();
910 if (!inbox) {
911 unlink(file);
912 return -1;
915 if (prefs_filtering == NULL) {
916 /* old filtering */
917 if (state->ac_prefs->filter_on_recv) {
918 dropfolder =
919 filter_get_dest_folder(prefs_common.fltlist, file);
920 if (!dropfolder) dropfolder = inbox;
921 else if (!strcmp(dropfolder->path, FILTER_NOT_RECEIVE)) {
922 g_warning(_("a message won't be received\n"));
923 return 1;
925 } else
926 dropfolder = inbox;
927 } else {
928 /* new filtering */
929 dropfolder = inbox;
932 val = GPOINTER_TO_INT(g_hash_table_lookup
933 (state->folder_table, dropfolder));
934 if (val == 0) {
935 folder_item_scan(dropfolder);
936 g_hash_table_insert(state->folder_table, dropfolder,
937 GINT_TO_POINTER(1));
940 if (prefs_filtering == NULL || !state->ac_prefs->filter_on_recv) {
941 if ((msgnum = folder_item_add_msg(dropfolder, file, TRUE)) < 0) {
942 unlink(file);
943 return -1;
946 else {
947 filter_incoming_message(dropfolder, file, state->folder_table);
950 return 0;
953 static void inc_put_error(IncState istate)
955 switch (istate) {
956 case INC_ERROR:
957 if(!prefs_common.noerrorpanel) {
958 alertpanel_error(_("Error occurred while processing mail."));
960 break;
961 case INC_NOSPACE:
962 alertpanel_error(_("No disk space left."));
963 break;
964 default:
965 break;
969 static void inc_cancel(GtkWidget *widget, gpointer data)
971 IncProgressDialog *dialog = data;
972 IncSession *session = dialog->queue_list->data;
973 SockInfo *sockinfo = session->pop3_state->sockinfo;
975 #if USE_THREADS
976 if (sockinfo->state == CONN_READY ||
977 sockinfo->state == CONN_LOOKUPSUCCESS) {
978 pthread_cancel(sockinfo->connect_thr);
979 /* pthread_kill(sockinfo->connect_thr, SIGINT); */
980 g_print("connection was cancelled.\n");
982 #endif
984 session->pop3_state->inc_state = INC_CANCEL;
985 pop3_automaton_terminate(sockinfo, session->atm);
988 static gint inc_spool(void)
990 gchar *mbox, *logname;
991 gint msgs;
993 logname = g_get_user_name();
994 mbox = g_strconcat(prefs_common.spool_path
995 ? prefs_common.spool_path : DEFAULT_SPOOL_PATH,
996 G_DIR_SEPARATOR_S, logname, NULL);
997 msgs = get_spool(folder_get_default_inbox(), mbox);
998 g_free(mbox);
1000 return msgs;
1003 static void inc_spool_account(PrefsAccount *account)
1005 FolderItem *inbox;
1006 FolderItem *dropfolder;
1007 gint val;
1009 if (account->inbox) {
1010 inbox = folder_find_item_from_path(account->inbox);
1011 if (!inbox)
1012 inbox = folder_get_default_inbox();
1013 } else
1014 inbox = folder_get_default_inbox();
1016 get_spool(inbox, account->local_mbox);
1019 static void inc_all_spool(void)
1021 GList *list = NULL;
1023 list = account_get_list();
1024 if (!list) return;
1026 for (; list != NULL; list = list->next) {
1027 IncSession *session;
1028 PrefsAccount *account = list->data;
1030 if (account->protocol == A_LOCAL)
1031 inc_spool_account(account);
1035 static gint get_spool(FolderItem *dest, const gchar *mbox)
1037 gint msgs, size;
1038 gint lockfd;
1039 gchar tmp_mbox[MAXPATHLEN + 1];
1040 GHashTable *folder_table = NULL;
1042 g_return_val_if_fail(dest != NULL, -1);
1043 g_return_val_if_fail(mbox != NULL, -1);
1045 if (!is_file_exist(mbox) || (size = get_file_size(mbox)) == 0) {
1046 debug_print(_("no messages in local mailbox.\n"));
1047 return 0;
1048 } else if (size < 0)
1049 return -1;
1051 if ((lockfd = lock_mbox(mbox, LOCK_FLOCK)) < 0)
1052 return -1;
1054 g_snprintf(tmp_mbox, sizeof(tmp_mbox), "%s%ctmpmbox%d",
1055 get_rc_dir(), G_DIR_SEPARATOR, (gint)mbox);
1057 if (copy_mbox(mbox, tmp_mbox) < 0)
1058 return -1;
1060 debug_print(_("Getting new messages from %s into %s...\n"),
1061 mbox, dest->path);
1063 if (prefs_common.filter_on_inc)
1064 folder_table = g_hash_table_new(NULL, NULL);
1065 msgs = proc_mbox(dest, tmp_mbox, folder_table);
1067 unlink(tmp_mbox);
1068 if (msgs >= 0) empty_mbox(mbox);
1069 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1071 if (folder_table) {
1072 if (!prefs_common.scan_all_after_inc) {
1073 g_hash_table_insert(folder_table, dest,
1074 GINT_TO_POINTER(1));
1075 folder_item_scan_foreach(folder_table);
1076 folderview_update_item_foreach(folder_table);
1078 g_hash_table_destroy(folder_table);
1079 } else if (!prefs_common.scan_all_after_inc) {
1080 folder_item_scan(dest);
1081 folderview_update_item(dest, FALSE);
1084 return msgs;
1087 static guint autocheck_timer = 0;
1088 static gpointer autocheck_data = NULL;
1090 void inc_autocheck_timer_init(MainWindow *mainwin)
1092 autocheck_data = mainwin;
1093 inc_autocheck_timer_set();
1096 void inc_autocheck_timer_set(void)
1098 inc_autocheck_timer_remove();
1100 if (prefs_common.autochk_newmail && autocheck_data) {
1101 autocheck_timer = gtk_timeout_add
1102 (prefs_common.autochk_itv * 60000,
1103 inc_autocheck_func,
1104 autocheck_data);
1108 void inc_autocheck_timer_remove(void)
1110 if (autocheck_timer) {
1111 gtk_timeout_remove(autocheck_timer);
1112 autocheck_timer = 0;
1116 static gint inc_autocheck_func(gpointer data)
1118 MainWindow *mainwin = (MainWindow *)data;
1120 inc_all_account_mail(mainwin);
1122 return FALSE;