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