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