new filering
[claws.git] / src / inc.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2001 Hiroyuki Yamamoto
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include "defs.h"
25
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>
38
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"
64
65 #include "pixmaps/continue.xpm"
66 #include "pixmaps/complete.xpm"
67 #include "pixmaps/error.xpm"
68
69 GdkPixmap *currentxpm;
70 GdkBitmap *currentxpmmask;
71 GdkPixmap *errorxpm;
72 GdkBitmap *errorxpmmask;
73 GdkPixmap *okxpm;
74 GdkBitmap *okxpmmask;
75
76 #define MSGBUFSIZE      8192
77
78 static void inc_finished                (MainWindow             *mainwin);
79 static void inc_account_mail            (PrefsAccount           *account,
80                                          MainWindow             *mainwin);
81
82 static IncProgressDialog *inc_progress_dialog_create    (void);
83 static void inc_progress_dialog_destroy (IncProgressDialog      *inc_dialog);
84
85 static IncSession *inc_session_new      (PrefsAccount           *account);
86 static void inc_session_destroy         (IncSession             *session);
87 static Pop3State *inc_pop3_state_new    (PrefsAccount           *account);
88 static void inc_pop3_state_destroy      (Pop3State              *state);
89 static void inc_start                   (IncProgressDialog      *inc_dialog);
90 static IncState inc_pop3_session_do     (IncSession             *session);
91 static gint pop3_automaton_terminate    (SockInfo               *source,
92                                          Automaton              *atm);
93
94 static GHashTable *inc_get_uidl_table   (PrefsAccount           *ac_prefs);
95 static void inc_write_uidl_list         (Pop3State              *state);
96
97 #if USE_THREADS
98 static gint connection_check_cb         (Automaton      *atm);
99 #endif
100
101 static void inc_pop3_recv_func          (SockInfo       *sock,
102                                          gint            read_bytes,
103                                          gpointer        data);
104
105 static void inc_put_error               (IncState        istate);
106
107 static void inc_cancel                  (GtkWidget      *widget,
108                                          gpointer        data);
109
110 static gint inc_spool                   (void);
111 static gint get_spool                   (FolderItem     *dest,
112                                          const gchar    *mbox);
113
114 static void inc_all_spool(void);
115
116 static gint inc_autocheck_func          (gpointer        data);
117
118 static void inc_finished(MainWindow *mainwin)
119 {
120         FolderItem *item;
121
122         if (prefs_common.open_inbox_on_inc) {
123                 item = cur_account && cur_account->inbox
124                         ? folder_find_item_from_path(cur_account->inbox)
125                         : folder_get_default_inbox();
126                 folderview_unselect(mainwin->folderview);
127                 folderview_select(mainwin->folderview, item);
128         } else {
129                 item = mainwin->summaryview->folder_item;
130                 folderview_unselect(mainwin->folderview);
131                 folderview_select(mainwin->folderview, item);
132         }
133 }
134
135 void inc_mail(MainWindow *mainwin)
136 {
137         inc_autocheck_timer_remove();
138         summary_write_cache(mainwin->summaryview);
139
140         if (prefs_common.use_extinc && prefs_common.extinc_path) {
141                 gint pid;
142
143                 /* external incorporating program */
144                 if ((pid = fork()) < 0) {
145                         perror("fork");
146                         inc_autocheck_timer_set();                      
147                         return;
148                 }
149
150                 if (pid == 0) {
151                         execlp(prefs_common.extinc_path,
152                                g_basename(prefs_common.extinc_path),
153                                NULL);
154
155                         /* this will be called when failed */
156                         perror("exec");
157                         _exit(1);
158                 }
159
160                 /* wait until child process is terminated */
161                 waitpid(pid, NULL, 0);
162
163                 if (prefs_common.inc_local) inc_spool();
164                 inc_all_spool();
165         } else {
166                 if (prefs_common.inc_local) inc_spool();
167                 inc_all_spool();
168
169                 inc_account_mail(cur_account, mainwin);
170         }
171
172         inc_finished(mainwin);
173         inc_autocheck_timer_set();      
174 }
175
176 static void inc_account_mail(PrefsAccount *account, MainWindow *mainwin)
177 {
178         IncProgressDialog *inc_dialog;
179         IncSession *session;
180         gchar *text[3];
181
182         session = inc_session_new(account);
183         if (!session) return;
184
185         inc_dialog = inc_progress_dialog_create();
186         inc_dialog->queue_list = g_list_append(inc_dialog->queue_list, session);
187         inc_dialog->mainwin = mainwin;
188         session->data = inc_dialog;
189
190         text[0] = NULL;
191         text[1] = account->account_name;
192         text[2] = _("Standby");
193         gtk_clist_append(GTK_CLIST(inc_dialog->dialog->clist), text);
194
195         inc_start(inc_dialog);
196 }
197
198 void inc_all_account_mail(MainWindow *mainwin)
199 {
200         GList *list, *queue_list = NULL;
201         IncProgressDialog *inc_dialog;
202
203         inc_autocheck_timer_remove();
204         summary_write_cache(mainwin->summaryview);
205
206         if (prefs_common.inc_local) inc_spool();
207
208         list = account_get_list();
209         if (!list) {
210                 inc_autocheck_timer_set();
211                 return;
212         }               
213
214         for (; list != NULL; list = list->next) {
215                 IncSession *session;
216                 PrefsAccount *account = list->data;
217
218                 if (account->recv_at_getall) {
219                         session = inc_session_new(account);
220                         if (session)
221                                 queue_list = g_list_append(queue_list, session);
222                 }
223         }
224
225         if (!queue_list) {
226                 inc_autocheck_timer_set();
227                 return;
228         }       
229
230         inc_dialog = inc_progress_dialog_create();
231         inc_dialog->queue_list = queue_list;
232         inc_dialog->mainwin = mainwin;
233         for (list = queue_list; list != NULL; list = list->next) {
234                 IncSession *session = list->data;
235                 gchar *text[3];
236
237                 session->data = inc_dialog;
238
239                 text[0] = NULL;
240                 text[1] = session->pop3_state->ac_prefs->account_name;
241                 text[2] = _("Standby");
242                 gtk_clist_append(GTK_CLIST(inc_dialog->dialog->clist), text);
243         }
244
245         inc_start(inc_dialog);
246
247         inc_finished(mainwin);
248         inc_autocheck_timer_set();
249 }
250
251 static IncProgressDialog *inc_progress_dialog_create(void)
252 {
253         IncProgressDialog *dialog;
254         ProgressDialog *progress;
255
256         dialog = g_new0(IncProgressDialog, 1);
257
258         progress = progress_dialog_create();
259         gtk_window_set_title(GTK_WINDOW(progress->window),
260                              _("Retrieving new messages"));
261         gtk_signal_connect(GTK_OBJECT(progress->cancel_btn), "clicked",
262                            GTK_SIGNAL_FUNC(inc_cancel), dialog);
263         gtk_signal_connect(GTK_OBJECT(progress->window), "delete_event",
264                            GTK_SIGNAL_FUNC(gtk_true), NULL);
265         manage_window_set_transient(GTK_WINDOW(progress->window));
266
267         progress_dialog_set_value(progress, 0.0);
268
269         gtk_widget_show(progress->window);
270
271         PIXMAP_CREATE(progress->clist, okxpm, okxpmmask, complete_xpm);
272         PIXMAP_CREATE(progress->clist,
273                       currentxpm, currentxpmmask, continue_xpm);
274         PIXMAP_CREATE(progress->clist, errorxpm, errorxpmmask, error_xpm);
275
276         gtk_widget_show_now(progress->window);
277
278         dialog->dialog = progress;
279         dialog->queue_list = NULL;
280
281         return dialog;
282 }
283
284 static void inc_progress_dialog_clear(IncProgressDialog *inc_dialog)
285 {
286         progress_dialog_set_value(inc_dialog->dialog, 0.0);
287         progress_dialog_set_label(inc_dialog->dialog, "");
288 }
289
290 static void inc_progress_dialog_destroy(IncProgressDialog *inc_dialog)
291 {
292         g_return_if_fail(inc_dialog != NULL);
293
294         progress_dialog_destroy(inc_dialog->dialog);
295
296         g_free(inc_dialog);
297 }
298
299 static IncSession *inc_session_new(PrefsAccount *account)
300 {
301         IncSession *session;
302
303         g_return_val_if_fail(account != NULL, NULL);
304
305         if (account->protocol != A_POP3 && account->protocol != A_APOP)
306                 return NULL;
307         if (!account->recv_server || !account->userid)
308                 return NULL;
309
310         session = g_new0(IncSession, 1);
311         session->pop3_state = inc_pop3_state_new(account);
312         session->pop3_state->session = session;
313
314         return session;
315 }
316
317 static void inc_session_destroy(IncSession *session)
318 {
319         g_return_if_fail(session != NULL);
320
321         inc_pop3_state_destroy(session->pop3_state);
322         g_free(session);
323 }
324
325 static Pop3State *inc_pop3_state_new(PrefsAccount *account)
326 {
327         Pop3State *state;
328
329         state = g_new0(Pop3State, 1);
330
331         state->ac_prefs = account;
332         state->folder_table = g_hash_table_new(NULL, NULL);
333         state->id_table = inc_get_uidl_table(account);
334         state->id_list = NULL;
335         state->new_id_list = NULL;
336         state->inc_state = INC_SUCCESS;
337
338         return state;
339 }
340
341 static void inc_pop3_state_destroy(Pop3State *state)
342 {
343         g_hash_table_destroy(state->folder_table);
344         g_free(state->sizes);
345
346         if (state->id_table) {
347                 hash_free_strings(state->id_table);
348                 g_hash_table_destroy(state->id_table);
349         }
350         slist_free_strings(state->id_list);
351         slist_free_strings(state->new_id_list);
352         g_slist_free(state->id_list);
353         g_slist_free(state->new_id_list);
354
355         g_free(state->greeting);
356         g_free(state->user);
357         g_free(state->pass);
358         g_free(state->prev_folder);
359
360         g_free(state);
361 }
362
363 static void inc_start(IncProgressDialog *inc_dialog)
364 {
365         IncSession *session;
366         GtkCList *clist = GTK_CLIST(inc_dialog->dialog->clist);
367         Pop3State *pop3_state;
368         IncState inc_state;
369         gint num = 0;
370
371         while (inc_dialog->queue_list != NULL) {
372                 session = inc_dialog->queue_list->data;
373                 pop3_state = session->pop3_state;
374
375                 inc_progress_dialog_clear(inc_dialog);
376
377                 pop3_state->user = g_strdup(pop3_state->ac_prefs->userid);
378                 if (pop3_state->ac_prefs->passwd)
379                         pop3_state->pass =
380                                 g_strdup(pop3_state->ac_prefs->passwd);
381                 else if (pop3_state->ac_prefs->tmp_pass)
382                         pop3_state->pass =
383                                 g_strdup(pop3_state->ac_prefs->tmp_pass);
384                 else {
385                         gchar *pass;
386                         gchar *message;
387
388                         message = g_strdup_printf
389                                 (_("Input password for %s on %s:"),
390                                  pop3_state->user,
391                                  pop3_state->ac_prefs->recv_server);
392
393                         pass = input_dialog_with_invisible(_("Input password"),
394                                                            message, NULL);
395                         g_free(message);
396                         manage_window_focus_in(inc_dialog->mainwin->window,
397                                                NULL, NULL);
398                         if (pass) {
399                                 pop3_state->ac_prefs->tmp_pass = g_strdup(pass);
400                                 pop3_state->pass = pass;
401                         } else {
402                                 inc_session_destroy(session);
403                                 inc_dialog->queue_list = g_list_remove
404                                         (inc_dialog->queue_list, session);
405                                 continue;
406                         }
407                 }
408
409                 gtk_clist_set_pixmap(clist, num, 0, currentxpm, currentxpmmask);
410                 gtk_clist_set_text(clist, num, 2, _("Retrieving"));
411
412                 /* begin POP3 session */
413                 inc_state = inc_pop3_session_do(session);
414
415                 if (inc_state == INC_SUCCESS || inc_state == INC_CANCEL) {
416                         gtk_clist_set_pixmap(clist, num, 0, okxpm, okxpmmask);
417                         gtk_clist_set_text(clist, num, 2, _("Done"));
418                 } else {
419                         gtk_clist_set_pixmap(clist, num, 0, errorxpm, errorxpmmask);
420                         gtk_clist_set_text(clist, num, 2, _("Error"));
421                 }
422
423                 if (pop3_state->error_val == PS_AUTHFAIL) {
424                         manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
425                         alertpanel_error
426                                 (_("Authorization for %s on %s failed"),
427                                  pop3_state->user,
428                                  pop3_state->ac_prefs->recv_server);
429                 }
430
431                 statusbar_pop_all();
432                 manage_window_focus_in(inc_dialog->mainwin->window, NULL, NULL);
433
434                 folder_item_scan_foreach(pop3_state->folder_table);
435                 folderview_update_item_foreach(pop3_state->folder_table);
436
437                 if (pop3_state->error_val == PS_AUTHFAIL &&
438                     pop3_state->ac_prefs->tmp_pass) {
439                         g_free(pop3_state->ac_prefs->tmp_pass);
440                         pop3_state->ac_prefs->tmp_pass = NULL;
441                 }
442
443                 inc_write_uidl_list(pop3_state);
444
445                 if (inc_state != INC_SUCCESS) {
446                         inc_put_error(inc_state);
447                         break;
448                 }
449
450                 inc_session_destroy(session);
451                 inc_dialog->queue_list =
452                         g_list_remove(inc_dialog->queue_list, session);
453
454                 num++;
455         }
456
457         while (inc_dialog->queue_list != NULL) {
458                 session = inc_dialog->queue_list->data;
459                 inc_session_destroy(session);
460                 inc_dialog->queue_list =
461                         g_list_remove(inc_dialog->queue_list, session);
462         }
463
464         inc_progress_dialog_destroy(inc_dialog);
465 }
466
467
468 static IncState inc_pop3_session_do(IncSession *session)
469 {
470         Pop3State *pop3_state = session->pop3_state;
471         IncProgressDialog *inc_dialog = (IncProgressDialog *)session->data;
472         Automaton *atm;
473         SockInfo *sockinfo;
474         gint i;
475         gchar *server;
476         gushort port;
477         gchar *buf;
478         static AtmHandler handlers[] = {
479                 pop3_greeting_recv      ,
480                 pop3_getauth_user_send  , pop3_getauth_user_recv,
481                 pop3_getauth_pass_send  , pop3_getauth_pass_recv,
482                 pop3_getauth_apop_send  , pop3_getauth_apop_recv,
483                 pop3_getrange_stat_send , pop3_getrange_stat_recv,
484                 pop3_getrange_last_send , pop3_getrange_last_recv,
485                 pop3_getrange_uidl_send , pop3_getrange_uidl_recv,
486                 pop3_getsize_list_send  , pop3_getsize_list_recv,
487                 pop3_retr_send          , pop3_retr_recv,
488                 pop3_delete_send        , pop3_delete_recv,
489                 pop3_logout_send        , pop3_logout_recv
490         };
491
492         debug_print(_("getting new messages of account %s...\n"),
493                     pop3_state->ac_prefs->account_name);
494
495         atm = automaton_create(N_POP3_PHASE);
496
497         session->atm = atm;
498         atm->data = pop3_state;
499
500         buf = g_strdup_printf(_("%s: Retrieving new messages"),
501                               pop3_state->ac_prefs->recv_server);
502         gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window), buf);
503         g_free(buf);
504
505         for (i = POP3_GREETING_RECV; i < N_POP3_PHASE; i++)
506                 atm->state[i].handler = handlers[i];
507         atm->state[POP3_GREETING_RECV].condition = GDK_INPUT_READ;
508         for (i = POP3_GETAUTH_USER_SEND; i < N_POP3_PHASE; ) {
509                 atm->state[i++].condition = GDK_INPUT_WRITE;
510                 atm->state[i++].condition = GDK_INPUT_READ;
511         }
512
513         atm->terminate = (AtmHandler)pop3_automaton_terminate;
514
515         atm->num = POP3_GREETING_RECV;
516
517         server = pop3_state->ac_prefs->recv_server;
518         port = pop3_state->ac_prefs->set_popport ?
519                 pop3_state->ac_prefs->popport : 110;
520
521         buf = g_strdup_printf(_("Connecting to POP3 server: %s ..."), server);
522         log_message("%s\n", buf);
523         progress_dialog_set_label(inc_dialog->dialog, buf);
524         g_free(buf);
525         GTK_EVENTS_FLUSH();
526
527 #if USE_THREADS
528         if ((sockinfo = sock_connect_with_thread(server, port)) == NULL) {
529 #else
530         if ((sockinfo = sock_connect(server, port)) == NULL) {
531 #endif
532                 log_warning(_("Can't connect to POP3 server: %s:%d\n"),
533                             server, port);
534                 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
535                 alertpanel_error(_("Can't connect to POP3 server: %s:%d"),
536                                  server, port);
537                 manage_window_focus_out(inc_dialog->dialog->window, NULL, NULL);
538                 pop3_automaton_terminate(NULL, atm);
539                 automaton_destroy(atm);
540
541                 return INC_ERROR;
542         }
543
544         /* :WK: Hmmm, with the later sock_gdk_input, we have 2 references
545          * to the sock structure - implement a reference counter?? */
546         pop3_state->sockinfo = sockinfo;
547         atm->help_sock = sockinfo;
548
549         recv_set_ui_func(inc_pop3_recv_func, session);
550
551 #if USE_THREADS
552         atm->timeout_tag = gtk_timeout_add
553                 (TIMEOUT_ITV, (GtkFunction)connection_check_cb, atm);
554 #else
555         atm->tag = sock_gdk_input_add(sockinfo,
556                                       atm->state[atm->num].condition,
557                                       automaton_input_cb, atm);
558 #endif
559
560         gtk_main();
561
562         recv_set_ui_func(NULL, NULL);
563
564 #if USE_THREADS
565         //pthread_join(sockinfo->connect_thr, NULL);
566 #endif
567         automaton_destroy(atm);
568
569         return pop3_state->inc_state;
570 }
571
572 static gint pop3_automaton_terminate(SockInfo *source, Automaton *atm)
573 {
574         if (atm->tag > 0) {
575                 gdk_input_remove(atm->tag);
576                 atm->tag = 0;
577         }
578         if (atm->timeout_tag > 0) {
579                 gtk_timeout_remove(atm->timeout_tag);
580                 atm->timeout_tag = 0;
581         }
582         if (source) {
583                 sock_close(source);
584                 gtk_main_quit();
585         }
586
587         atm->terminated = TRUE;
588
589         return 0;
590 }
591
592 static GHashTable *inc_get_uidl_table(PrefsAccount *ac_prefs)
593 {
594         GHashTable *table;
595         gchar *path;
596         FILE *fp;
597         gchar buf[IDLEN + 3];
598
599         path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
600                            "uidl-", ac_prefs->recv_server,
601                            "-", ac_prefs->userid, NULL);
602         if ((fp = fopen(path, "r")) == NULL) {
603                 if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
604                 g_free(path);
605                 return NULL;
606         }
607         g_free(path);
608
609         table = g_hash_table_new(g_str_hash, g_str_equal);
610
611         while (fgets(buf, sizeof(buf), fp) != NULL) {
612                 strretchomp(buf);
613                 g_hash_table_insert(table, g_strdup(buf), GINT_TO_POINTER(1));
614         }
615
616         fclose(fp);
617
618         return table;
619 }
620
621 static void inc_write_uidl_list(Pop3State *state)
622 {
623         gchar *path;
624         FILE *fp;
625         GSList *cur;
626
627         if (!state->id_list) return;
628
629         path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
630                            "uidl-", state->ac_prefs->recv_server,
631                            "-", state->user, NULL);
632         if ((fp = fopen(path, "w")) == NULL) {
633                 FILE_OP_ERROR(path, "fopen");
634                 g_free(path);
635                 return;
636         }
637
638         for (cur = state->id_list; cur != NULL; cur = cur->next) {
639                 if (fputs((gchar *)cur->data, fp) == EOF) {
640                         FILE_OP_ERROR(path, "fputs");
641                         break;
642                 }
643                 if (fputc('\n', fp) == EOF) {
644                         FILE_OP_ERROR(path, "fputc");
645                         break;
646                 }
647         }
648
649         if (fclose(fp) == EOF) FILE_OP_ERROR(path, "fclose");
650         g_free(path);
651 }
652
653 #if USE_THREADS
654 static gint connection_check_cb(Automaton *atm)
655 {
656         Pop3State *state = atm->data;
657         IncProgressDialog *inc_dialog = state->session->data;
658         SockInfo *sockinfo = state->sockinfo;
659
660         //g_print("connection check\n");
661
662         if (sockinfo->state == CONN_LOOKUPFAILED ||
663             sockinfo->state == CONN_FAILED) {
664                 atm->timeout_tag = 0;
665                 log_warning(_("Can't connect to POP3 server: %s:%d\n"),
666                             sockinfo->hostname, sockinfo->port);
667                 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
668                 alertpanel_error(_("Can't connect to POP3 server: %s:%d"),
669                                  sockinfo->hostname, sockinfo->port);
670                 manage_window_focus_out(inc_dialog->dialog->window, NULL, NULL);
671                 pop3_automaton_terminate(sockinfo, atm);
672                 return FALSE;
673         } else if (sockinfo->state == CONN_ESTABLISHED) {
674                 atm->timeout_tag = 0;
675                 atm->tag = sock_gdk_input_add(sockinfo,
676                                               atm->state[atm->num].condition,
677                                               automaton_input_cb, atm);
678                 return FALSE;
679         } else {
680                 return TRUE;
681         }
682 }
683 #endif
684
685 static void inc_pop3_recv_func(SockInfo *sock, gint read_bytes, gpointer data)
686 {
687         gchar buf[MSGBUFSIZE];
688         IncSession *session = (IncSession *)data;
689         Pop3State *state = session->pop3_state;
690         IncProgressDialog *inc_dialog = session->data;
691         ProgressDialog *dialog = inc_dialog->dialog;
692         gint cur_total;
693
694         cur_total = state->cur_total_bytes + read_bytes;
695         if (cur_total > state->total_bytes)
696                 cur_total = state->total_bytes;
697
698         g_snprintf(buf, sizeof(buf),
699                    _("Retrieving message (%d / %d) (%d / %d bytes)"),
700                    state->cur_msg, state->count,
701                    cur_total, state->total_bytes);
702         progress_dialog_set_label(dialog, buf);
703
704         progress_dialog_set_percentage
705                 (dialog, (gfloat)cur_total / (gfloat)state->total_bytes);
706         GTK_EVENTS_FLUSH();
707 }
708
709 void inc_progress_update(Pop3State *state, Pop3Phase phase)
710 {
711         gchar buf[MSGBUFSIZE];
712         IncProgressDialog *inc_dialog = state->session->data;
713         ProgressDialog *dialog = inc_dialog->dialog;
714
715         switch (phase) {
716         case POP3_GREETING_RECV:
717                 break;
718         case POP3_GETAUTH_USER_SEND:
719         case POP3_GETAUTH_USER_RECV:
720         case POP3_GETAUTH_PASS_SEND:
721         case POP3_GETAUTH_PASS_RECV:
722         case POP3_GETAUTH_APOP_SEND:
723         case POP3_GETAUTH_APOP_RECV:
724                 progress_dialog_set_label(dialog, _("Authorizing..."));         
725                 break;
726         case POP3_GETRANGE_STAT_SEND:
727         case POP3_GETRANGE_STAT_RECV:
728                 progress_dialog_set_label
729                         (dialog, _("Getting the number of new messages (STAT)..."));
730                 break;
731         case POP3_GETRANGE_LAST_SEND:
732         case POP3_GETRANGE_LAST_RECV:
733                 progress_dialog_set_label
734                         (dialog, _("Getting the number of new messages (LAST)..."));
735                 break;
736         case POP3_GETRANGE_UIDL_SEND:
737         case POP3_GETRANGE_UIDL_RECV:
738                 progress_dialog_set_label
739                         (dialog, _("Getting the number of new messages (UIDL)..."));
740                 break;
741         case POP3_GETSIZE_LIST_SEND:
742         case POP3_GETSIZE_LIST_RECV:
743                 progress_dialog_set_label
744                         (dialog, _("Getting the size of messages (LIST)..."));
745                 break;
746         case POP3_RETR_SEND:
747         case POP3_RETR_RECV:
748                 g_snprintf(buf, sizeof(buf),
749                            _("Retrieving message (%d / %d) (%d / %d bytes)"),
750                            state->cur_msg, state->count,
751                            state->cur_total_bytes, state->total_bytes);
752                 progress_dialog_set_label(dialog, buf);
753                 progress_dialog_set_percentage
754                         (dialog,
755                          (gfloat)(state->cur_total_bytes) /
756                          (gfloat)(state->total_bytes));
757                 break;
758         case POP3_DELETE_SEND:
759         case POP3_DELETE_RECV:
760                 progress_dialog_set_label(dialog, _("Deleting message"));
761                 break;
762         case POP3_LOGOUT_SEND:
763         case POP3_LOGOUT_RECV:
764                 progress_dialog_set_label(dialog, _("Quitting"));
765                 break;
766         default:
767         }
768 }
769
770 gint inc_drop_message(const gchar *file, Pop3State *state)
771 {
772         FolderItem *inbox;
773         FolderItem *dropfolder;
774         gint val;
775         gint msgnum;
776
777         if (state->ac_prefs->inbox) {
778                 inbox = folder_find_item_from_path(state->ac_prefs->inbox);
779                 if (!inbox)
780                         inbox = folder_get_default_inbox();
781         } else
782                 inbox = folder_get_default_inbox();
783         if (!inbox) {
784                 unlink(file);
785                 return -1;
786         }
787
788
789         if (prefs_filtering == NULL) {
790                 /* old filtering */
791                 if (state->ac_prefs->filter_on_recv) {
792                         dropfolder =
793                                 filter_get_dest_folder(prefs_common.fltlist, file);
794                         if (!dropfolder) dropfolder = inbox;
795                         else if (!strcmp(dropfolder->path, FILTER_NOT_RECEIVE)) {
796                                 g_warning(_("a message won't be received\n"));
797                                 return 1;
798                         }
799                 } else
800                         dropfolder = inbox;
801         }
802         else {
803                 /* new filtering */
804                 dropfolder = inbox;
805         }
806
807         val = GPOINTER_TO_INT(g_hash_table_lookup
808                               (state->folder_table, dropfolder));
809         if (val == 0) {
810                 folder_item_scan(dropfolder);
811                 g_hash_table_insert(state->folder_table, dropfolder,
812                                     GINT_TO_POINTER(1));
813         }
814
815         if ((msgnum = folder_item_add_msg(dropfolder, file)) < 0) {
816                 unlink(file);
817                 return -1;
818         }
819
820         unlink(file);
821
822         if (prefs_filtering != NULL) {
823                 /* new filtering */
824                 if (state->ac_prefs->filter_on_recv) {
825                         filter_message(prefs_filtering, dropfolder, msgnum,
826                                        state->folder_table);
827                 }
828         }
829
830         return 0;
831 }
832
833 static void inc_put_error(IncState istate)
834 {
835         switch (istate) {
836         case INC_ERROR:
837                 alertpanel_error(_("Error occurred while processing mail."));
838                 break;
839         case INC_NOSPACE:
840                 alertpanel_error(_("No disk space left."));
841                 break;
842         default:
843         }
844 }
845
846 static void inc_cancel(GtkWidget *widget, gpointer data)
847 {
848         IncProgressDialog *dialog = data;
849         IncSession *session = dialog->queue_list->data;
850         SockInfo *sockinfo = session->pop3_state->sockinfo;
851
852 #if USE_THREADS
853         if (sockinfo->state == CONN_READY ||
854             sockinfo->state == CONN_LOOKUPSUCCESS) {
855                 pthread_cancel(sockinfo->connect_thr);
856                 //pthread_kill(sockinfo->connect_thr, SIGINT);
857                 g_print("connection was cancelled.\n");
858         }
859 #endif
860
861         session->pop3_state->inc_state = INC_CANCEL;
862         pop3_automaton_terminate(sockinfo, session->atm);
863 }
864
865 static gint inc_spool(void)
866 {
867         gchar *mbox, *logname;
868         gint msgs;
869
870         logname = g_get_user_name();
871         mbox = g_strconcat(prefs_common.spool_path
872                            ? prefs_common.spool_path : DEFAULT_SPOOL_PATH,
873                            G_DIR_SEPARATOR_S, logname, NULL);
874         msgs = get_spool(folder_get_default_inbox(), mbox);
875         g_free(mbox);
876
877         return msgs;
878 }
879
880 static void inc_spool_account(PrefsAccount *account)
881 {
882         FolderItem *inbox;
883         FolderItem *dropfolder;
884         gint val;
885
886         if (account->inbox) {
887                 inbox = folder_find_item_from_path(account->inbox);
888                 if (!inbox)
889                         inbox = folder_get_default_inbox();
890         } else
891                 inbox = folder_get_default_inbox();
892
893         get_spool(inbox, account->local_mbox);
894 }
895
896 static void inc_all_spool(void)
897 {
898         GList *list = NULL;
899
900         list = account_get_list();
901         if (!list) return;
902
903         for (; list != NULL; list = list->next) {
904                 IncSession *session;
905                 PrefsAccount *account = list->data;
906
907                 if (account->protocol == A_LOCAL)
908                         inc_spool_account(account);
909         }
910 }
911
912 static gint get_spool(FolderItem *dest, const gchar *mbox)
913 {
914         gint msgs, size;
915         gint lockfd;
916         gchar tmp_mbox[MAXPATHLEN + 1];
917         GHashTable *folder_table = NULL;
918
919         g_return_val_if_fail(dest != NULL, -1);
920         g_return_val_if_fail(mbox != NULL, -1);
921
922         if (!is_file_exist(mbox) || (size = get_file_size(mbox)) == 0) {
923                 debug_print(_("no messages in local mailbox.\n"));
924                 return 0;
925         } else if (size < 0)
926                 return -1;
927
928         if ((lockfd = lock_mbox(mbox, LOCK_FLOCK)) < 0)
929                 return -1;
930
931         g_snprintf(tmp_mbox, sizeof(tmp_mbox), "%s%ctmpmbox%d",
932                    get_rc_dir(), G_DIR_SEPARATOR, (gint)mbox);
933
934         if (copy_mbox(mbox, tmp_mbox) < 0)
935                 return -1;
936
937         debug_print(_("Getting new messages from %s into %s...\n"),
938                     mbox, dest->path);
939
940         if (prefs_common.filter_on_inc)
941                 folder_table = g_hash_table_new(NULL, NULL);
942         msgs = proc_mbox(dest, tmp_mbox, folder_table);
943
944         unlink(tmp_mbox);
945         if (msgs >= 0) empty_mbox(mbox);
946         unlock_mbox(mbox, lockfd, LOCK_FLOCK);
947
948         if (folder_table) {
949                 folder_item_scan_foreach(folder_table);
950                 folderview_update_item_foreach(folder_table);
951                 g_hash_table_destroy(folder_table);
952         } else {
953                 folder_item_scan(dest);
954                 folderview_update_item(dest, FALSE);
955         }
956
957         return msgs;
958 }
959
960 static guint autocheck_timer = 0;
961 static gpointer autocheck_data = NULL;
962
963 void inc_autocheck_timer_init(MainWindow *mainwin)
964 {
965         autocheck_data = mainwin;
966         inc_autocheck_timer_set();
967 }
968
969 void inc_autocheck_timer_set(void)
970 {
971         inc_autocheck_timer_remove();
972
973         if (prefs_common.autochk_newmail && autocheck_data) {
974                 autocheck_timer = gtk_timeout_add
975                         (prefs_common.autochk_itv * 60000,
976                          inc_autocheck_func,
977                          autocheck_data);
978         }
979 }
980
981 void inc_autocheck_timer_remove(void)
982 {
983         if (autocheck_timer) {
984                 gtk_timeout_remove(autocheck_timer);
985                 autocheck_timer = 0;
986         }
987 }
988
989 static gint inc_autocheck_func(gpointer data)
990 {
991         MainWindow *mainwin = (MainWindow *)data;
992
993         inc_all_account_mail(mainwin);
994
995         return FALSE;
996 }