mail retrieval dialog can be enabled, disabled
[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         if((prefs_common.receive_dialog == RECVDIALOG_ALWAYS) ||
270             ((prefs_common.receive_dialog == RECVDIALOG_WINDOW_ACTIVE) && focus_window)) {
271                 gtk_widget_show(progress->window);
272         }
273
274         PIXMAP_CREATE(progress->clist, okxpm, okxpmmask, complete_xpm);
275         PIXMAP_CREATE(progress->clist,
276                       currentxpm, currentxpmmask, continue_xpm);
277         PIXMAP_CREATE(progress->clist, errorxpm, errorxpmmask, error_xpm);
278
279         if((prefs_common.receive_dialog == RECVDIALOG_ALWAYS) ||
280             ((prefs_common.receive_dialog == RECVDIALOG_WINDOW_ACTIVE) && focus_window)) {
281                 gtk_widget_show_now(progress->window);
282         }
283
284         dialog->dialog = progress;
285         dialog->queue_list = NULL;
286
287         return dialog;
288 }
289
290 static void inc_progress_dialog_clear(IncProgressDialog *inc_dialog)
291 {
292         progress_dialog_set_value(inc_dialog->dialog, 0.0);
293         progress_dialog_set_label(inc_dialog->dialog, "");
294 }
295
296 static void inc_progress_dialog_destroy(IncProgressDialog *inc_dialog)
297 {
298         g_return_if_fail(inc_dialog != NULL);
299
300         progress_dialog_destroy(inc_dialog->dialog);
301
302         g_free(inc_dialog);
303 }
304
305 static IncSession *inc_session_new(PrefsAccount *account)
306 {
307         IncSession *session;
308
309         g_return_val_if_fail(account != NULL, NULL);
310
311         if (account->protocol != A_POP3 && account->protocol != A_APOP)
312                 return NULL;
313         if (!account->recv_server || !account->userid)
314                 return NULL;
315
316         session = g_new0(IncSession, 1);
317         session->pop3_state = inc_pop3_state_new(account);
318         session->pop3_state->session = session;
319
320         return session;
321 }
322
323 static void inc_session_destroy(IncSession *session)
324 {
325         g_return_if_fail(session != NULL);
326
327         inc_pop3_state_destroy(session->pop3_state);
328         g_free(session);
329 }
330
331 static Pop3State *inc_pop3_state_new(PrefsAccount *account)
332 {
333         Pop3State *state;
334
335         state = g_new0(Pop3State, 1);
336
337         state->ac_prefs = account;
338         state->folder_table = g_hash_table_new(NULL, NULL);
339         state->id_table = inc_get_uidl_table(account);
340         state->id_list = NULL;
341         state->new_id_list = NULL;
342         state->inc_state = INC_SUCCESS;
343
344         return state;
345 }
346
347 static void inc_pop3_state_destroy(Pop3State *state)
348 {
349         g_hash_table_destroy(state->folder_table);
350         g_free(state->sizes);
351
352         if (state->id_table) {
353                 hash_free_strings(state->id_table);
354                 g_hash_table_destroy(state->id_table);
355         }
356         slist_free_strings(state->id_list);
357         slist_free_strings(state->new_id_list);
358         g_slist_free(state->id_list);
359         g_slist_free(state->new_id_list);
360
361         g_free(state->greeting);
362         g_free(state->user);
363         g_free(state->pass);
364         g_free(state->prev_folder);
365
366         g_free(state);
367 }
368
369 static void inc_start(IncProgressDialog *inc_dialog)
370 {
371         IncSession *session;
372         GtkCList *clist = GTK_CLIST(inc_dialog->dialog->clist);
373         Pop3State *pop3_state;
374         IncState inc_state;
375         gint num = 0;
376
377         while (inc_dialog->queue_list != NULL) {
378                 session = inc_dialog->queue_list->data;
379                 pop3_state = session->pop3_state;
380
381                 inc_progress_dialog_clear(inc_dialog);
382
383                 gtk_clist_moveto(clist, num, 0, 1.0, 0.0);
384
385                 pop3_state->user = g_strdup(pop3_state->ac_prefs->userid);
386                 if (pop3_state->ac_prefs->passwd)
387                         pop3_state->pass =
388                                 g_strdup(pop3_state->ac_prefs->passwd);
389                 else if (pop3_state->ac_prefs->tmp_pass)
390                         pop3_state->pass =
391                                 g_strdup(pop3_state->ac_prefs->tmp_pass);
392                 else {
393                         gchar *pass;
394                         gchar *message;
395
396                         message = g_strdup_printf
397                                 (_("Input password for %s on %s:"),
398                                  pop3_state->user,
399                                  pop3_state->ac_prefs->recv_server);
400
401                         pass = input_dialog_with_invisible(_("Input password"),
402                                                            message, NULL);
403                         g_free(message);
404                         manage_window_focus_in(inc_dialog->mainwin->window,
405                                                NULL, NULL);
406                         if (pass) {
407                                 pop3_state->ac_prefs->tmp_pass = g_strdup(pass);
408                                 pop3_state->pass = pass;
409                         } else {
410                                 inc_session_destroy(session);
411                                 inc_dialog->queue_list = g_list_remove
412                                         (inc_dialog->queue_list, session);
413                                 continue;
414                         }
415                 }
416
417                 gtk_clist_set_pixmap(clist, num, 0, currentxpm, currentxpmmask);
418                 gtk_clist_set_text(clist, num, 2, _("Retrieving"));
419
420                 /* begin POP3 session */
421                 inc_state = inc_pop3_session_do(session);
422
423                 if (inc_state == INC_SUCCESS || inc_state == INC_CANCEL) {
424                         gtk_clist_set_pixmap(clist, num, 0, okxpm, okxpmmask);
425                         gtk_clist_set_text(clist, num, 2, _("Done"));
426                 } else {
427                         gtk_clist_set_pixmap(clist, num, 0, errorxpm, errorxpmmask);
428                         gtk_clist_set_text(clist, num, 2, _("Error"));
429                 }
430
431                 if (pop3_state->error_val == PS_AUTHFAIL) {
432                         if(!prefs_common.noerrorpanel) {
433                                 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
434                                 alertpanel_error
435                                         (_("Authorization for %s on %s failed"),
436                                          pop3_state->user,
437                                          pop3_state->ac_prefs->recv_server);
438                         }
439                 }
440
441                 statusbar_pop_all();
442                 manage_window_focus_in(inc_dialog->mainwin->window, NULL, NULL);
443
444                 folder_item_scan_foreach(pop3_state->folder_table);
445                 folderview_update_item_foreach(pop3_state->folder_table);
446
447                 if (pop3_state->error_val == PS_AUTHFAIL &&
448                     pop3_state->ac_prefs->tmp_pass) {
449                         g_free(pop3_state->ac_prefs->tmp_pass);
450                         pop3_state->ac_prefs->tmp_pass = NULL;
451                 }
452
453                 inc_write_uidl_list(pop3_state);
454
455                 if (inc_state != INC_SUCCESS) {
456                         inc_put_error(inc_state);
457                         break;
458                 }
459
460                 inc_session_destroy(session);
461                 inc_dialog->queue_list =
462                         g_list_remove(inc_dialog->queue_list, session);
463
464                 num++;
465         }
466
467         while (inc_dialog->queue_list != NULL) {
468                 session = inc_dialog->queue_list->data;
469                 inc_session_destroy(session);
470                 inc_dialog->queue_list =
471                         g_list_remove(inc_dialog->queue_list, session);
472         }
473
474         inc_progress_dialog_destroy(inc_dialog);
475 }
476
477
478 static IncState inc_pop3_session_do(IncSession *session)
479 {
480         Pop3State *pop3_state = session->pop3_state;
481         IncProgressDialog *inc_dialog = (IncProgressDialog *)session->data;
482         Automaton *atm;
483         SockInfo *sockinfo;
484         gint i;
485         gchar *server;
486         gushort port;
487         gchar *buf;
488         static AtmHandler handlers[] = {
489                 pop3_greeting_recv      ,
490                 pop3_getauth_user_send  , pop3_getauth_user_recv,
491                 pop3_getauth_pass_send  , pop3_getauth_pass_recv,
492                 pop3_getauth_apop_send  , pop3_getauth_apop_recv,
493                 pop3_getrange_stat_send , pop3_getrange_stat_recv,
494                 pop3_getrange_last_send , pop3_getrange_last_recv,
495                 pop3_getrange_uidl_send , pop3_getrange_uidl_recv,
496                 pop3_getsize_list_send  , pop3_getsize_list_recv,
497                 pop3_retr_send          , pop3_retr_recv,
498                 pop3_delete_send        , pop3_delete_recv,
499                 pop3_logout_send        , pop3_logout_recv
500         };
501
502         debug_print(_("getting new messages of account %s...\n"),
503                     pop3_state->ac_prefs->account_name);
504
505         atm = automaton_create(N_POP3_PHASE);
506
507         session->atm = atm;
508         atm->data = pop3_state;
509
510         buf = g_strdup_printf(_("%s: Retrieving new messages"),
511                               pop3_state->ac_prefs->recv_server);
512         gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window), buf);
513         g_free(buf);
514
515         for (i = POP3_GREETING_RECV; i < N_POP3_PHASE; i++)
516                 atm->state[i].handler = handlers[i];
517         atm->state[POP3_GREETING_RECV].condition = GDK_INPUT_READ;
518         for (i = POP3_GETAUTH_USER_SEND; i < N_POP3_PHASE; ) {
519                 atm->state[i++].condition = GDK_INPUT_WRITE;
520                 atm->state[i++].condition = GDK_INPUT_READ;
521         }
522
523         atm->terminate = (AtmHandler)pop3_automaton_terminate;
524
525         atm->num = POP3_GREETING_RECV;
526
527         server = pop3_state->ac_prefs->recv_server;
528         port = pop3_state->ac_prefs->set_popport ?
529                 pop3_state->ac_prefs->popport : 110;
530
531         buf = g_strdup_printf(_("Connecting to POP3 server: %s ..."), server);
532         log_message("%s\n", buf);
533         progress_dialog_set_label(inc_dialog->dialog, buf);
534         g_free(buf);
535         GTK_EVENTS_FLUSH();
536
537 #if USE_THREADS
538         if ((sockinfo = sock_connect_with_thread(server, port)) == NULL) {
539 #else
540         if ((sockinfo = sock_connect(server, port)) == NULL) {
541 #endif
542                 log_warning(_("Can't connect to POP3 server: %s:%d\n"),
543                             server, port);
544                 if(!prefs_common.noerrorpanel) {
545                         manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
546                         alertpanel_error(_("Can't connect to POP3 server: %s:%d"),
547                                          server, port);
548                         manage_window_focus_out(inc_dialog->dialog->window, NULL, NULL);
549                 }
550                 pop3_automaton_terminate(NULL, atm);
551                 automaton_destroy(atm);
552
553                 return INC_ERROR;
554         }
555
556         /* :WK: Hmmm, with the later sock_gdk_input, we have 2 references
557          * to the sock structure - implement a reference counter?? */
558         pop3_state->sockinfo = sockinfo;
559         atm->help_sock = sockinfo;
560
561         recv_set_ui_func(inc_pop3_recv_func, session);
562
563 #if USE_THREADS
564         atm->timeout_tag = gtk_timeout_add
565                 (TIMEOUT_ITV, (GtkFunction)connection_check_cb, atm);
566 #else
567         atm->tag = sock_gdk_input_add(sockinfo,
568                                       atm->state[atm->num].condition,
569                                       automaton_input_cb, atm);
570 #endif
571
572         gtk_main();
573
574         recv_set_ui_func(NULL, NULL);
575
576 #if USE_THREADS
577         //pthread_join(sockinfo->connect_thr, NULL);
578 #endif
579         automaton_destroy(atm);
580
581         return pop3_state->inc_state;
582 }
583
584 static gint pop3_automaton_terminate(SockInfo *source, Automaton *atm)
585 {
586         if (atm->tag > 0) {
587                 gdk_input_remove(atm->tag);
588                 atm->tag = 0;
589         }
590         if (atm->timeout_tag > 0) {
591                 gtk_timeout_remove(atm->timeout_tag);
592                 atm->timeout_tag = 0;
593         }
594         if (source) {
595                 sock_close(source);
596                 gtk_main_quit();
597         }
598
599         atm->terminated = TRUE;
600
601         return 0;
602 }
603
604 static GHashTable *inc_get_uidl_table(PrefsAccount *ac_prefs)
605 {
606         GHashTable *table;
607         gchar *path;
608         FILE *fp;
609         gchar buf[IDLEN + 3];
610
611         path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
612                            "uidl-", ac_prefs->recv_server,
613                            "-", ac_prefs->userid, NULL);
614         if ((fp = fopen(path, "r")) == NULL) {
615                 if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
616                 g_free(path);
617                 return NULL;
618         }
619         g_free(path);
620
621         table = g_hash_table_new(g_str_hash, g_str_equal);
622
623         while (fgets(buf, sizeof(buf), fp) != NULL) {
624                 strretchomp(buf);
625                 g_hash_table_insert(table, g_strdup(buf), GINT_TO_POINTER(1));
626         }
627
628         fclose(fp);
629
630         return table;
631 }
632
633 static void inc_write_uidl_list(Pop3State *state)
634 {
635         gchar *path;
636         FILE *fp;
637         GSList *cur;
638
639         if (!state->id_list) return;
640
641         path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
642                            "uidl-", state->ac_prefs->recv_server,
643                            "-", state->user, NULL);
644         if ((fp = fopen(path, "w")) == NULL) {
645                 FILE_OP_ERROR(path, "fopen");
646                 g_free(path);
647                 return;
648         }
649
650         for (cur = state->id_list; cur != NULL; cur = cur->next) {
651                 if (fputs((gchar *)cur->data, fp) == EOF) {
652                         FILE_OP_ERROR(path, "fputs");
653                         break;
654                 }
655                 if (fputc('\n', fp) == EOF) {
656                         FILE_OP_ERROR(path, "fputc");
657                         break;
658                 }
659         }
660
661         if (fclose(fp) == EOF) FILE_OP_ERROR(path, "fclose");
662         g_free(path);
663 }
664
665 #if USE_THREADS
666 static gint connection_check_cb(Automaton *atm)
667 {
668         Pop3State *state = atm->data;
669         IncProgressDialog *inc_dialog = state->session->data;
670         SockInfo *sockinfo = state->sockinfo;
671
672         //g_print("connection check\n");
673
674         if (sockinfo->state == CONN_LOOKUPFAILED ||
675             sockinfo->state == CONN_FAILED) {
676                 atm->timeout_tag = 0;
677                 log_warning(_("Can't connect to POP3 server: %s:%d\n"),
678                             sockinfo->hostname, sockinfo->port);
679                 if(!prefs_common.noerrorpanel) {
680                         manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
681                         alertpanel_error(_("Can't connect to POP3 server: %s:%d"),
682                                          sockinfo->hostname, sockinfo->port);
683                         manage_window_focus_out(inc_dialog->dialog->window, NULL, NULL);
684                 }
685                 pop3_automaton_terminate(sockinfo, atm);
686                 return FALSE;
687         } else if (sockinfo->state == CONN_ESTABLISHED) {
688                 atm->timeout_tag = 0;
689                 atm->tag = sock_gdk_input_add(sockinfo,
690                                               atm->state[atm->num].condition,
691                                               automaton_input_cb, atm);
692                 return FALSE;
693         } else {
694                 return TRUE;
695         }
696 }
697 #endif
698
699 static void inc_pop3_recv_func(SockInfo *sock, gint read_bytes, gpointer data)
700 {
701         gchar buf[MSGBUFSIZE];
702         IncSession *session = (IncSession *)data;
703         Pop3State *state = session->pop3_state;
704         IncProgressDialog *inc_dialog = session->data;
705         ProgressDialog *dialog = inc_dialog->dialog;
706         gint cur_total;
707
708         cur_total = state->cur_total_bytes + read_bytes;
709         if (cur_total > state->total_bytes)
710                 cur_total = state->total_bytes;
711
712         g_snprintf(buf, sizeof(buf),
713                    _("Retrieving message (%d / %d) (%d / %d bytes)"),
714                    state->cur_msg, state->count,
715                    cur_total, state->total_bytes);
716         progress_dialog_set_label(dialog, buf);
717
718         progress_dialog_set_percentage
719                 (dialog, (gfloat)cur_total / (gfloat)state->total_bytes);
720         GTK_EVENTS_FLUSH();
721 }
722
723 void inc_progress_update(Pop3State *state, Pop3Phase phase)
724 {
725         gchar buf[MSGBUFSIZE];
726         IncProgressDialog *inc_dialog = state->session->data;
727         ProgressDialog *dialog = inc_dialog->dialog;
728
729         switch (phase) {
730         case POP3_GREETING_RECV:
731                 break;
732         case POP3_GETAUTH_USER_SEND:
733         case POP3_GETAUTH_USER_RECV:
734         case POP3_GETAUTH_PASS_SEND:
735         case POP3_GETAUTH_PASS_RECV:
736         case POP3_GETAUTH_APOP_SEND:
737         case POP3_GETAUTH_APOP_RECV:
738                 progress_dialog_set_label(dialog, _("Authorizing..."));
739                 break;
740         case POP3_GETRANGE_STAT_SEND:
741         case POP3_GETRANGE_STAT_RECV:
742                 progress_dialog_set_label
743                         (dialog, _("Getting the number of new messages (STAT)..."));
744                 break;
745         case POP3_GETRANGE_LAST_SEND:
746         case POP3_GETRANGE_LAST_RECV:
747                 progress_dialog_set_label
748                         (dialog, _("Getting the number of new messages (LAST)..."));
749                 break;
750         case POP3_GETRANGE_UIDL_SEND:
751         case POP3_GETRANGE_UIDL_RECV:
752                 progress_dialog_set_label
753                         (dialog, _("Getting the number of new messages (UIDL)..."));
754                 break;
755         case POP3_GETSIZE_LIST_SEND:
756         case POP3_GETSIZE_LIST_RECV:
757                 progress_dialog_set_label
758                         (dialog, _("Getting the size of messages (LIST)..."));
759                 break;
760         case POP3_RETR_SEND:
761         case POP3_RETR_RECV:
762                 g_snprintf(buf, sizeof(buf),
763                            _("Retrieving message (%d / %d) (%d / %d bytes)"),
764                            state->cur_msg, state->count,
765                            state->cur_total_bytes, state->total_bytes);
766                 progress_dialog_set_label(dialog, buf);
767                 progress_dialog_set_percentage
768                         (dialog,
769                          (gfloat)(state->cur_total_bytes) /
770                          (gfloat)(state->total_bytes));
771                 break;
772         case POP3_DELETE_SEND:
773         case POP3_DELETE_RECV:
774                 progress_dialog_set_label(dialog, _("Deleting message"));
775                 break;
776         case POP3_LOGOUT_SEND:
777         case POP3_LOGOUT_RECV:
778                 progress_dialog_set_label(dialog, _("Quitting"));
779                 break;
780         default:
781         }
782 }
783
784 gint inc_drop_message(const gchar *file, Pop3State *state)
785 {
786         FolderItem *inbox;
787         FolderItem *dropfolder;
788         gint val;
789         gint msgnum;
790
791         if (state->ac_prefs->inbox) {
792                 inbox = folder_find_item_from_path(state->ac_prefs->inbox);
793                 if (!inbox)
794                         inbox = folder_get_default_inbox();
795         } else
796                 inbox = folder_get_default_inbox();
797         if (!inbox) {
798                 unlink(file);
799                 return -1;
800         }
801
802         if (prefs_filtering == NULL) {
803                 /* old filtering */
804                 if (state->ac_prefs->filter_on_recv) {
805                         dropfolder =
806                                 filter_get_dest_folder(prefs_common.fltlist, file);
807                         if (!dropfolder) dropfolder = inbox;
808                         else if (!strcmp(dropfolder->path, FILTER_NOT_RECEIVE)) {
809                                 g_warning(_("a message won't be received\n"));
810                                 return 1;
811                         }
812                 } else
813                         dropfolder = inbox;
814         } else {
815                 /* new filtering */
816                 dropfolder = inbox;
817         }
818
819         val = GPOINTER_TO_INT(g_hash_table_lookup
820                               (state->folder_table, dropfolder));
821         if (val == 0) {
822                 folder_item_scan(dropfolder);
823                 g_hash_table_insert(state->folder_table, dropfolder,
824                                     GINT_TO_POINTER(1));
825         }
826
827         if ((msgnum = folder_item_add_msg(dropfolder, file, TRUE)) < 0) {
828                 unlink(file);
829                 return -1;
830         }
831
832         if (prefs_filtering != NULL) {
833                 /* new filtering */
834                 if (state->ac_prefs->filter_on_recv) {
835                         filter_message(prefs_filtering, dropfolder, msgnum,
836                                        state->folder_table);
837                 }
838         }
839
840         return 0;
841 }
842
843 static void inc_put_error(IncState istate)
844 {
845         switch (istate) {
846         case INC_ERROR:
847                 if(!prefs_common.noerrorpanel) {
848                         alertpanel_error(_("Error occurred while processing mail."));
849                 }
850                 break;
851         case INC_NOSPACE:
852                 alertpanel_error(_("No disk space left."));
853                 break;
854         default:
855         }
856 }
857
858 static void inc_cancel(GtkWidget *widget, gpointer data)
859 {
860         IncProgressDialog *dialog = data;
861         IncSession *session = dialog->queue_list->data;
862         SockInfo *sockinfo = session->pop3_state->sockinfo;
863
864 #if USE_THREADS
865         if (sockinfo->state == CONN_READY ||
866             sockinfo->state == CONN_LOOKUPSUCCESS) {
867                 pthread_cancel(sockinfo->connect_thr);
868                 //pthread_kill(sockinfo->connect_thr, SIGINT);
869                 g_print("connection was cancelled.\n");
870         }
871 #endif
872
873         session->pop3_state->inc_state = INC_CANCEL;
874         pop3_automaton_terminate(sockinfo, session->atm);
875 }
876
877 static gint inc_spool(void)
878 {
879         gchar *mbox, *logname;
880         gint msgs;
881
882         logname = g_get_user_name();
883         mbox = g_strconcat(prefs_common.spool_path
884                            ? prefs_common.spool_path : DEFAULT_SPOOL_PATH,
885                            G_DIR_SEPARATOR_S, logname, NULL);
886         msgs = get_spool(folder_get_default_inbox(), mbox);
887         g_free(mbox);
888
889         return msgs;
890 }
891
892 static void inc_spool_account(PrefsAccount *account)
893 {
894         FolderItem *inbox;
895         FolderItem *dropfolder;
896         gint val;
897
898         if (account->inbox) {
899                 inbox = folder_find_item_from_path(account->inbox);
900                 if (!inbox)
901                         inbox = folder_get_default_inbox();
902         } else
903                 inbox = folder_get_default_inbox();
904
905         get_spool(inbox, account->local_mbox);
906 }
907
908 static void inc_all_spool(void)
909 {
910         GList *list = NULL;
911
912         list = account_get_list();
913         if (!list) return;
914
915         for (; list != NULL; list = list->next) {
916                 IncSession *session;
917                 PrefsAccount *account = list->data;
918
919                 if (account->protocol == A_LOCAL)
920                         inc_spool_account(account);
921         }
922 }
923
924 static gint get_spool(FolderItem *dest, const gchar *mbox)
925 {
926         gint msgs, size;
927         gint lockfd;
928         gchar tmp_mbox[MAXPATHLEN + 1];
929         GHashTable *folder_table = NULL;
930
931         g_return_val_if_fail(dest != NULL, -1);
932         g_return_val_if_fail(mbox != NULL, -1);
933
934         if (!is_file_exist(mbox) || (size = get_file_size(mbox)) == 0) {
935                 debug_print(_("no messages in local mailbox.\n"));
936                 return 0;
937         } else if (size < 0)
938                 return -1;
939
940         if ((lockfd = lock_mbox(mbox, LOCK_FLOCK)) < 0)
941                 return -1;
942
943         g_snprintf(tmp_mbox, sizeof(tmp_mbox), "%s%ctmpmbox%d",
944                    get_rc_dir(), G_DIR_SEPARATOR, (gint)mbox);
945
946         if (copy_mbox(mbox, tmp_mbox) < 0)
947                 return -1;
948
949         debug_print(_("Getting new messages from %s into %s...\n"),
950                     mbox, dest->path);
951
952         if (prefs_common.filter_on_inc)
953                 folder_table = g_hash_table_new(NULL, NULL);
954         msgs = proc_mbox(dest, tmp_mbox, folder_table);
955
956         unlink(tmp_mbox);
957         if (msgs >= 0) empty_mbox(mbox);
958         unlock_mbox(mbox, lockfd, LOCK_FLOCK);
959
960         if (folder_table) {
961                 folder_item_scan_foreach(folder_table);
962                 folderview_update_item_foreach(folder_table);
963                 g_hash_table_destroy(folder_table);
964         } else {
965                 folder_item_scan(dest);
966                 folderview_update_item(dest, FALSE);
967         }
968
969         return msgs;
970 }
971
972 static guint autocheck_timer = 0;
973 static gpointer autocheck_data = NULL;
974
975 void inc_autocheck_timer_init(MainWindow *mainwin)
976 {
977         autocheck_data = mainwin;
978         inc_autocheck_timer_set();
979 }
980
981 void inc_autocheck_timer_set(void)
982 {
983         inc_autocheck_timer_remove();
984
985         if (prefs_common.autochk_newmail && autocheck_data) {
986                 autocheck_timer = gtk_timeout_add
987                         (prefs_common.autochk_itv * 60000,
988                          inc_autocheck_func,
989                          autocheck_data);
990         }
991 }
992
993 void inc_autocheck_timer_remove(void)
994 {
995         if (autocheck_timer) {
996                 gtk_timeout_remove(autocheck_timer);
997                 autocheck_timer = 0;
998         }
999 }
1000
1001 static gint inc_autocheck_func(gpointer data)
1002 {
1003         MainWindow *mainwin = (MainWindow *)data;
1004
1005         inc_all_account_mail(mainwin);
1006
1007         return FALSE;
1008 }