b3170495769b2a51631d7a19a3fe42c65c0f1486
[claws.git] / src / inc.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2002 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 <gtk/gtkprogressbar.h>
31 #include <stdio.h>
32 #include <unistd.h>
33 #include <string.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <sys/wait.h>
37 #include <signal.h>
38 #include <errno.h>
39
40 #include "intl.h"
41 #include "main.h"
42 #include "inc.h"
43 #include "mainwindow.h"
44 #include "folderview.h"
45 #include "summaryview.h"
46 #include "prefs_common.h"
47 #include "prefs_account.h"
48 #include "account.h"
49 #include "procmsg.h"
50 #include "socket.h"
51 #include "ssl.h"
52 #include "pop.h"
53 #include "recv.h"
54 #include "mbox.h"
55 #include "utils.h"
56 #include "gtkutils.h"
57 #include "statusbar.h"
58 #include "manage_window.h"
59 #include "stock_pixmap.h"
60 #include "progressdialog.h"
61 #include "inputdialog.h"
62 #include "alertpanel.h"
63 #include "filter.h"
64 #include "automaton.h"
65 #include "folder.h"
66 #include "filtering.h"
67 #include "selective_download.h"
68
69 static GList *inc_dialog_list = NULL;
70
71 static guint inc_lock_count = 0;
72
73 static GdkPixmap *currentxpm;
74 static GdkBitmap *currentxpmmask;
75 static GdkPixmap *errorxpm;
76 static GdkBitmap *errorxpmmask;
77 static GdkPixmap *okxpm;
78 static GdkBitmap *okxpmmask;
79
80 #define MSGBUFSIZE      8192
81
82 static void inc_finished                (MainWindow             *mainwin,
83                                          gboolean                new_messages);
84 static gint inc_account_mail            (PrefsAccount           *account,
85                                          MainWindow             *mainwin);
86
87 static IncProgressDialog *inc_progress_dialog_create    (void);
88 static void inc_progress_dialog_destroy (IncProgressDialog      *inc_dialog);
89
90 static IncSession *inc_session_new      (PrefsAccount           *account);
91 static void inc_session_destroy         (IncSession             *session);
92 static Pop3State *inc_pop3_state_new    (PrefsAccount           *account);
93 static void inc_pop3_state_destroy      (Pop3State              *state);
94 static gint inc_start                   (IncProgressDialog      *inc_dialog);
95 static IncState inc_pop3_session_do     (IncSession             *session);
96 static gint pop3_automaton_terminate    (SockInfo               *source,
97                                          Automaton              *atm);
98
99 static GHashTable *inc_get_uidl_table   (PrefsAccount           *ac_prefs);
100 static void inc_write_uidl_list         (Pop3State              *state);
101
102 static gboolean inc_pop3_recv_func      (SockInfo       *sock,
103                                          gint            count,
104                                          gint            read_bytes,
105                                          gpointer        data);
106
107 static void inc_put_error               (IncState        istate);
108
109 static void inc_cancel_cb               (GtkWidget      *widget,
110                                          gpointer        data);
111
112 static gint inc_spool                   (void);
113 static gint get_spool                   (FolderItem     *dest,
114                                          const gchar    *mbox);
115
116 static void inc_spool_account(PrefsAccount *account);
117 static void inc_all_spool(void);
118 static void inc_autocheck_timer_set_interval    (guint           interval);
119 static gint inc_autocheck_func                  (gpointer        data);
120
121 static void inc_notify_cmd              (gint new_msgs, 
122                                          gboolean notify);
123
124 #define FOLDER_SUMMARY_MISMATCH(f, s) \
125         (f) && (s) ? ((s)->newmsgs != (f)->new) || ((f)->unread != (s)->unread) || ((f)->total != (s)->messages) \
126         : FALSE
127         
128 /**
129  * inc_finished:
130  * @mainwin: Main window.
131  * @new_messages: TRUE if some messages have been received.
132  * 
133  * Update the folder view and the summary view after receiving
134  * messages.  If @new_messages is FALSE, this function avoids unneeded
135  * updating.
136  **/
137 static void inc_finished(MainWindow *mainwin, gboolean new_messages)
138 {
139         FolderItem *item;
140
141         if (prefs_common.scan_all_after_inc)
142                 folderview_check_new(NULL);
143
144         if (!new_messages && !prefs_common.scan_all_after_inc) return;
145
146         if (prefs_common.open_inbox_on_inc) {
147                 item = cur_account && cur_account->inbox
148                         ? folder_find_item_from_identifier(cur_account->inbox)
149                         : folder_get_default_inbox();
150                 if (FOLDER_SUMMARY_MISMATCH(item, mainwin->summaryview)) {      
151                         folderview_unselect(mainwin->folderview);
152                         folderview_select(mainwin->folderview, item);
153                 }       
154         } else {
155                 item = mainwin->summaryview->folder_item;
156                 if (FOLDER_SUMMARY_MISMATCH(item, mainwin->summaryview)) {
157                         folderview_unselect(mainwin->folderview);
158                         folderview_select(mainwin->folderview, item);
159                 }       
160         }
161 }
162
163 void inc_mail(MainWindow *mainwin, gboolean notify)
164 {
165         gint new_msgs = 0;
166
167         if (inc_lock_count) return;
168
169         if (prefs_common.work_offline)
170                 if (alertpanel(_("Offline warning"), 
171                                _("You're working offline. Override?"),
172                                _("Yes"), _("No"), NULL) != G_ALERTDEFAULT)
173                 return;
174
175         inc_autocheck_timer_remove();
176         summary_write_cache(mainwin->summaryview);
177         main_window_lock(mainwin);
178
179         if (prefs_common.use_extinc && prefs_common.extinc_cmd) {
180                 /* external incorporating program */
181                 if (execute_command_line(prefs_common.extinc_cmd, FALSE) < 0) {
182                         main_window_unlock(mainwin);
183                         inc_autocheck_timer_set();
184                         return;
185                 }
186
187                 if (prefs_common.inc_local)
188                         new_msgs = inc_spool();
189         } else {
190                 if (prefs_common.inc_local)
191                         new_msgs = inc_spool();
192
193                 new_msgs += inc_account_mail(cur_account, mainwin);
194         }
195
196         inc_finished(mainwin, new_msgs > 0);
197         main_window_unlock(mainwin);
198         inc_notify_cmd(new_msgs, notify);
199         inc_autocheck_timer_set();
200 }
201
202 void inc_selective_download(MainWindow *mainwin, PrefsAccount *acc, gint session)
203 {
204         GSList *cur;
205         gint new_msgs = 0;
206
207         acc->session = session;
208         inc_account_mail(acc, mainwin);
209         acc->session = STYPE_NORMAL;
210         
211         for (cur = acc->msg_list; cur != NULL; cur = cur->next) {
212                 HeaderItems *items =(HeaderItems*)cur->data;
213
214                 if (items->state == SD_DOWNLOADED && 
215                     items->del_by_old_session == FALSE) {
216                         new_msgs++;                     
217                 }
218         }
219
220         if (new_msgs) {
221                 inc_finished(mainwin, TRUE);
222                 inc_notify_cmd(new_msgs, prefs_common.newmail_notify_manu);
223         }
224 }
225
226 static gint inc_account_mail(PrefsAccount *account, MainWindow *mainwin)
227 {
228         IncProgressDialog *inc_dialog;
229         IncSession *session;
230         gchar *text[3];
231
232         switch (account->protocol) {
233         case A_IMAP4:
234         case A_NNTP:
235                 folderview_check_new(FOLDER(account->folder));
236                 return 1;
237
238         case A_POP3:
239         case A_APOP:
240                 session = inc_session_new(account);
241                 if (!session) return 0;
242                 
243                 inc_dialog = inc_progress_dialog_create();
244                 inc_dialog->queue_list = g_list_append(inc_dialog->queue_list,
245                                                        session);
246                 inc_dialog->mainwin = mainwin;
247                 session->data = inc_dialog;
248         
249                 text[0] = NULL;
250                 text[1] = account->account_name;
251                 text[2] = _("Standby");
252                 gtk_clist_append(GTK_CLIST(inc_dialog->dialog->clist), text);
253                 
254                 main_window_set_toolbar_sensitive(mainwin);
255                 main_window_set_menu_sensitive(mainwin);
256                 
257                 return inc_start(inc_dialog);
258
259         case A_LOCAL:
260                 inc_spool_account(account);
261                 return 1;
262         }
263 }
264
265 void inc_all_account_mail(MainWindow *mainwin, gboolean notify)
266 {
267         GList *list, *queue_list = NULL;
268         IncProgressDialog *inc_dialog;
269         gint new_msgs = 0;
270         
271         if (prefs_common.work_offline)
272                 if (alertpanel(_("Offline warning"), 
273                                _("You're working offline. Override?"),
274                                _("Yes"), _("No"), NULL) != G_ALERTDEFAULT)
275                 return;
276
277         if (inc_lock_count) return;
278
279         inc_autocheck_timer_remove();
280         summary_write_cache(mainwin->summaryview);
281         main_window_lock(mainwin);
282
283         if (prefs_common.inc_local)
284                 new_msgs = inc_spool();
285
286         list = account_get_list();
287         if (!list) {
288                 inc_finished(mainwin, new_msgs > 0);
289                 main_window_unlock(mainwin);
290                 inc_notify_cmd(new_msgs, notify);
291                 inc_autocheck_timer_set();
292                 return;
293         }
294
295         /* check local folders */
296         inc_all_spool();
297
298         /* check IMAP4 folders */
299         for (; list != NULL; list = list->next) {
300                 PrefsAccount *account = list->data;
301                 if ((account->protocol == A_IMAP4 ||
302                      account->protocol == A_NNTP) && account->recv_at_getall)
303                         folderview_check_new(FOLDER(account->folder));
304         }
305
306         /* check POP3 accounts */
307         for (list = account_get_list(); list != NULL; list = list->next) {
308                 IncSession *session;
309                 PrefsAccount *account = list->data;
310                 account->session = STYPE_NORMAL;
311                 if (account->recv_at_getall) {
312                         session = inc_session_new(account);
313                         if (session)
314                                 queue_list = g_list_append(queue_list, session);
315                 }
316         }
317
318         if (!queue_list) {
319                 inc_finished(mainwin, new_msgs > 0);
320                 main_window_unlock(mainwin);
321                 inc_notify_cmd(new_msgs, notify);
322                 inc_autocheck_timer_set();
323                 return;
324         }
325
326         inc_dialog = inc_progress_dialog_create();
327         inc_dialog->queue_list = queue_list;
328         inc_dialog->mainwin = mainwin;
329         for (list = queue_list; list != NULL; list = list->next) {
330                 IncSession *session = list->data;
331                 gchar *text[3];
332
333                 session->data = inc_dialog;
334
335                 text[0] = NULL;
336                 text[1] = session->pop3_state->ac_prefs->account_name;
337                 text[2] = _("Standby");
338                 gtk_clist_append(GTK_CLIST(inc_dialog->dialog->clist), text);
339         }
340
341         main_window_set_toolbar_sensitive(mainwin);
342         main_window_set_menu_sensitive(mainwin);
343
344         new_msgs += inc_start(inc_dialog);
345
346         inc_finished(mainwin, new_msgs > 0);
347         main_window_unlock(mainwin);
348         inc_notify_cmd(new_msgs, notify);
349         inc_autocheck_timer_set();
350 }
351
352 static IncProgressDialog *inc_progress_dialog_create(void)
353 {
354         IncProgressDialog *dialog;
355         ProgressDialog *progress;
356
357         dialog = g_new0(IncProgressDialog, 1);
358
359         progress = progress_dialog_create();
360         gtk_window_set_title(GTK_WINDOW(progress->window),
361                              _("Retrieving new messages"));
362         gtk_signal_connect(GTK_OBJECT(progress->cancel_btn), "clicked",
363                            GTK_SIGNAL_FUNC(inc_cancel_cb), dialog);
364         gtk_signal_connect(GTK_OBJECT(progress->window), "delete_event",
365                            GTK_SIGNAL_FUNC(gtk_true), NULL);
366         /* manage_window_set_transient(GTK_WINDOW(progress->window)); */
367
368         progress_dialog_set_value(progress, 0.0);
369
370         stock_pixmap_gdk(progress->clist, STOCK_PIXMAP_COMPLETE,
371                          &okxpm, &okxpmmask);
372         stock_pixmap_gdk(progress->clist, STOCK_PIXMAP_CONTINUE,
373                          &currentxpm, &currentxpmmask);
374         stock_pixmap_gdk(progress->clist, STOCK_PIXMAP_ERROR,
375                          &errorxpm, &errorxpmmask);
376
377         if (prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS ||
378             (prefs_common.recv_dialog_mode == RECV_DIALOG_ACTIVE &&
379              manage_window_get_focus_window())) {
380                 dialog->show_dialog = TRUE;
381                 gtk_widget_show_now(progress->window);
382         }
383
384         dialog->dialog = progress;
385         dialog->queue_list = NULL;
386
387         inc_dialog_list = g_list_append(inc_dialog_list, dialog);
388
389         return dialog;
390 }
391
392 static void inc_progress_dialog_clear(IncProgressDialog *inc_dialog)
393 {
394         progress_dialog_set_value(inc_dialog->dialog, 0.0);
395         progress_dialog_set_label(inc_dialog->dialog, "");
396         gtk_progress_bar_update
397                 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar), 0.0);
398 }
399
400 static void inc_progress_dialog_destroy(IncProgressDialog *inc_dialog)
401 {
402         g_return_if_fail(inc_dialog != NULL);
403
404         inc_dialog_list = g_list_remove(inc_dialog_list, inc_dialog);
405
406         gtk_progress_bar_update
407                 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar), 0.0);
408         progress_dialog_destroy(inc_dialog->dialog);
409
410         g_free(inc_dialog);
411 }
412
413 static IncSession *inc_session_new(PrefsAccount *account)
414 {
415         IncSession *session;
416
417         g_return_val_if_fail(account != NULL, NULL);
418
419         if (account->protocol != A_POP3 && account->protocol != A_APOP)
420                 return NULL;
421         if (!account->recv_server || !account->userid)
422                 return NULL;
423
424         session = g_new0(IncSession, 1);
425         session->pop3_state = inc_pop3_state_new(account);
426         session->pop3_state->session = session;
427
428         return session;
429 }
430
431 static void inc_session_destroy(IncSession *session)
432 {
433         g_return_if_fail(session != NULL);
434
435         inc_pop3_state_destroy(session->pop3_state);
436         g_free(session);
437 }
438
439 static Pop3State *inc_pop3_state_new(PrefsAccount *account)
440 {
441         Pop3State *state;
442
443         state = g_new0(Pop3State, 1);
444
445         state->ac_prefs = account;
446         state->folder_table = g_hash_table_new(NULL, NULL);
447         state->uidl_todelete_list = NULL;
448         state->uidl_table = inc_get_uidl_table(account);
449         state->inc_state = INC_SUCCESS;
450
451         return state;
452 }
453
454 static void inc_pop3_state_destroy(Pop3State *state)
455 {
456         gint n;
457
458         g_hash_table_destroy(state->folder_table);
459
460         for (n = 1; n <= state->count; n++)
461                 g_free(state->msg[n].uidl);
462         g_free(state->msg);
463
464         if (state->uidl_table) {
465                 hash_free_strings(state->uidl_table);
466                 g_hash_table_destroy(state->uidl_table);
467         }
468         g_slist_free(state->uidl_todelete_list);
469         g_free(state->greeting);
470         g_free(state->user);
471         g_free(state->pass);
472         g_free(state->prev_folder);
473
474         g_free(state);
475 }
476
477 static gint inc_start(IncProgressDialog *inc_dialog)
478 {
479         IncSession *session;
480         GtkCList *clist = GTK_CLIST(inc_dialog->dialog->clist);
481         Pop3State *pop3_state;
482         IncState inc_state;
483         gint num = 0;
484         gint error_num = 0;
485         gint new_msgs = 0;
486
487         while (inc_dialog->queue_list != NULL) {
488                 session = inc_dialog->queue_list->data;
489                 pop3_state = session->pop3_state;
490
491                 inc_progress_dialog_clear(inc_dialog);
492
493                 gtk_clist_moveto(clist, num, -1, 1.0, 0.0);
494
495                 pop3_state->user = g_strdup(pop3_state->ac_prefs->userid);
496                 if (pop3_state->ac_prefs->passwd)
497                         pop3_state->pass =
498                                 g_strdup(pop3_state->ac_prefs->passwd);
499                 else if (pop3_state->ac_prefs->tmp_pass)
500                         pop3_state->pass =
501                                 g_strdup(pop3_state->ac_prefs->tmp_pass);
502                 else {
503                         gchar *pass;
504
505                         pass = input_dialog_query_password
506                                 (pop3_state->ac_prefs->recv_server,
507                                  pop3_state->user);
508
509                         if (inc_dialog->show_dialog)
510                                 manage_window_focus_in
511                                         (inc_dialog->mainwin->window,
512                                          NULL, NULL);
513                         if (pass) {
514                                 pop3_state->ac_prefs->tmp_pass = g_strdup(pass);
515                                 pop3_state->pass = pass;
516                         } else {
517                                 inc_session_destroy(session);
518                                 inc_dialog->queue_list = g_list_remove
519                                         (inc_dialog->queue_list, session);
520                                 continue;
521                         }
522                 }
523
524                 gtk_clist_set_pixmap(clist, num, 0, currentxpm, currentxpmmask);
525                 gtk_clist_set_text(clist, num, 2, _("Retrieving"));
526
527                 /* begin POP3 session */
528                 inc_state = inc_pop3_session_do(session);
529
530                 if (inc_state == INC_SUCCESS) {
531                         gtk_clist_set_pixmap(clist, num, 0, okxpm, okxpmmask);
532                         gtk_clist_set_text(clist, num, 2, _("Done"));
533                 } else if (inc_state == INC_CANCEL) {
534                         gtk_clist_set_pixmap(clist, num, 0, okxpm, okxpmmask);
535                         gtk_clist_set_text(clist, num, 2, _("Cancelled"));
536                 } else {
537                         gtk_clist_set_pixmap(clist, num, 0, errorxpm, errorxpmmask);
538                         if (inc_state == INC_CONNECT_ERROR)
539                                 gtk_clist_set_text(clist, num, 2,
540                                                    _("Connection failed"));
541                         else if (inc_state == INC_AUTH_FAILED)
542                                 gtk_clist_set_text(clist, num, 2,
543                                                    _("Auth failed"));
544                         else
545                                 gtk_clist_set_text(clist, num, 2, _("Error"));
546                 }
547
548                 if (pop3_state->error_val == PS_AUTHFAIL) {
549                         if(!prefs_common.noerrorpanel) {
550                                 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
551                                     ((prefs_common.recv_dialog_mode == RECV_DIALOG_ACTIVE) && focus_window)) {
552                                         manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
553                                 }
554                                 alertpanel_error
555                                         (_("Authorization for %s on %s failed"),
556                                          pop3_state->user,
557                                          pop3_state->ac_prefs->recv_server);
558                         }
559                 }
560
561                 statusbar_pop_all();
562
563                 /* CLAWS: perform filtering actions on dropped message */
564                 if (global_processing != NULL) {
565                         FolderItem *processing, *inbox;
566                         Folder *folder;
567                         MsgInfo *msginfo;
568                         GSList *msglist, *msglist_element;
569
570                         /* CLAWS: get default inbox (perhaps per account) */
571                         if (pop3_state->ac_prefs->inbox) {
572                                 /* CLAWS: get destination folder / mailbox */
573                                 inbox = folder_find_item_from_identifier(pop3_state->ac_prefs->inbox);
574                                 if (!inbox)
575                                         inbox = folder_get_default_inbox();
576                         } else
577                                 inbox = folder_get_default_inbox();
578
579                         /* get list of messages in processing */
580                         processing = folder_get_default_processing();
581                         folder_item_scan(processing);
582                         folder = processing->folder;
583                         msglist = folder->get_msg_list(folder, processing, FALSE);
584
585                         /* process messages */
586                         for(msglist_element = msglist; msglist_element != NULL; msglist_element = msglist_element->next) {
587                                 msginfo = (MsgInfo *) msglist_element->data;
588                                 /* filter if enabled in prefs or move to inbox if not */
589                                 if(pop3_state->ac_prefs->filter_on_recv) {
590                                         filter_message_by_msginfo_with_inbox(global_processing, msginfo,
591                                                                              pop3_state->folder_table,
592                                                                              inbox);
593                                 } else {
594                                         folder_item_move_msg(inbox, msginfo);
595                                         g_hash_table_insert(pop3_state->folder_table, inbox,
596                                                             GINT_TO_POINTER(1));
597                                 }
598                                 procmsg_msginfo_free(msginfo);
599                         }
600                         g_slist_free(msglist);
601                 }
602
603
604                 new_msgs += pop3_state->cur_total_num;
605
606                 if (!prefs_common.scan_all_after_inc) {
607                         folder_item_scan_foreach(pop3_state->folder_table);
608                         folderview_update_item_foreach
609                                 (pop3_state->folder_table);
610                 }
611
612                 if (pop3_state->error_val == PS_AUTHFAIL &&
613                     pop3_state->ac_prefs->tmp_pass) {
614                         g_free(pop3_state->ac_prefs->tmp_pass);
615                         pop3_state->ac_prefs->tmp_pass = NULL;
616                 }
617
618                 inc_write_uidl_list(pop3_state);
619
620                 if (inc_state != INC_SUCCESS && inc_state != INC_CANCEL) {
621                         error_num++;
622                         if (inc_state == INC_NOSPACE) {
623                                 inc_put_error(inc_state);
624                                 break;
625                         }
626                 }
627
628                 inc_session_destroy(session);
629                 inc_dialog->queue_list =
630                         g_list_remove(inc_dialog->queue_list, session);
631
632                 num++;
633         }
634
635         if (error_num && !prefs_common.noerrorpanel) {
636                 if (inc_dialog->show_dialog)
637                         manage_window_focus_in(inc_dialog->dialog->window,
638                                                NULL, NULL);
639                 alertpanel_error(_("Some errors occured while getting mail."));
640                 if (inc_dialog->show_dialog)
641                         manage_window_focus_out(inc_dialog->dialog->window,
642                                                 NULL, NULL);
643         }
644
645         while (inc_dialog->queue_list != NULL) {
646                 session = inc_dialog->queue_list->data;
647                 inc_session_destroy(session);
648                 inc_dialog->queue_list =
649                         g_list_remove(inc_dialog->queue_list, session);
650         }
651
652         inc_progress_dialog_destroy(inc_dialog);
653
654         return new_msgs;
655 }
656
657 static IncState inc_pop3_session_do(IncSession *session)
658 {
659         Pop3State *pop3_state = session->pop3_state;
660         IncProgressDialog *inc_dialog = (IncProgressDialog *)session->data;
661         Automaton *atm;
662         SockInfo *sockinfo;
663         gint i;
664         gchar *server;
665         gushort port;
666         gchar *buf;
667         static AtmHandler handlers[] = {
668                 pop3_greeting_recv      ,
669 #if USE_SSL
670                 pop3_stls_send          , pop3_stls_recv,
671 #endif
672                 pop3_getauth_user_send  , pop3_getauth_user_recv,
673                 pop3_getauth_pass_send  , pop3_getauth_pass_recv,
674                 pop3_getauth_apop_send  , pop3_getauth_apop_recv,
675                 pop3_getrange_stat_send , pop3_getrange_stat_recv,
676                 pop3_getrange_last_send , pop3_getrange_last_recv,
677                 pop3_getrange_uidl_send , pop3_getrange_uidl_recv,
678                 pop3_getsize_list_send  , pop3_getsize_list_recv,
679                 pop3_top_send           , pop3_top_recv,
680                 pop3_retr_send          , pop3_retr_recv,
681                 pop3_delete_send        , pop3_delete_recv,
682                 pop3_logout_send        , pop3_logout_recv
683         };
684
685         debug_print(_("getting new messages of account %s...\n"),
686                     pop3_state->ac_prefs->account_name);
687
688         atm = automaton_create(N_POP3_PHASE);
689
690         session->atm = atm;
691         atm->data = pop3_state;
692
693         buf = g_strdup_printf(_("%s: Retrieving new messages"),
694                               pop3_state->ac_prefs->recv_server);
695         gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window), buf);
696         g_free(buf);
697
698         for (i = POP3_GREETING_RECV; i < N_POP3_PHASE; i++)
699                 atm->state[i].handler = handlers[i];
700         atm->state[POP3_GREETING_RECV].condition = GDK_INPUT_READ;
701         for (i = POP3_GREETING_RECV + 1; i < N_POP3_PHASE; ) {
702                 atm->state[i++].condition = GDK_INPUT_WRITE;
703                 atm->state[i++].condition = GDK_INPUT_READ;
704         }
705
706         atm->terminate = (AtmHandler)pop3_automaton_terminate;
707         atm->ui_func = (AtmUIFunc)inc_progress_update;
708
709         atm->num = POP3_GREETING_RECV;
710
711         server = pop3_state->ac_prefs->recv_server;
712 #if USE_SSL
713         port = pop3_state->ac_prefs->set_popport ?
714                 pop3_state->ac_prefs->popport :
715                 pop3_state->ac_prefs->ssl_pop == SSL_TUNNEL ? 995 : 110;
716 #else
717         port = pop3_state->ac_prefs->set_popport ?
718                 pop3_state->ac_prefs->popport : 110;
719 #endif
720
721         buf = g_strdup_printf(_("Connecting to POP3 server: %s ..."), server);
722         log_message("%s\n", buf);
723         progress_dialog_set_label(inc_dialog->dialog, buf);
724         g_free(buf);
725         GTK_EVENTS_FLUSH();
726         statusbar_pop_all();
727
728         if ((sockinfo = sock_connect(server, port)) == NULL) {
729                 log_warning(_("Can't connect to POP3 server: %s:%d\n"),
730                             server, port);
731                 if(!prefs_common.noerrorpanel) {
732                         if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
733                             ((prefs_common.recv_dialog_mode == RECV_DIALOG_ACTIVE) && focus_window)) {
734                                 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
735                         }
736                         alertpanel_error(_("Can't connect to POP3 server: %s:%d"),
737                                          server, port);
738                         manage_window_focus_out(inc_dialog->dialog->window, NULL, NULL);
739                 }
740                 pop3_automaton_terminate(NULL, atm);
741                 automaton_destroy(atm);
742                 return INC_CONNECT_ERROR;
743         }
744
745 #if USE_SSL
746         if (pop3_state->ac_prefs->ssl_pop == SSL_TUNNEL &&
747             !ssl_init_socket(sockinfo)) {
748                 pop3_automaton_terminate(NULL, atm);
749                 automaton_destroy(atm);
750                 return INC_CONNECT_ERROR;
751         }
752 #endif
753
754         /* :WK: Hmmm, with the later sock_gdk_input, we have 2 references
755          * to the sock structure - implement a reference counter?? */
756         pop3_state->sockinfo = sockinfo;
757         atm->help_sock = sockinfo;
758
759         log_verbosity_set(TRUE);
760         /* oha: this messes up inc_progress update:
761            disabling this would avoid the label "Retrieve Header"
762            being overwritten by "Retrieve Message"
763            Setting inc_pop3_recv_func is not necessary
764            since atm already handles the progress dialog ui
765            just fine.
766         */
767         recv_set_ui_func(inc_pop3_recv_func, session);
768
769         atm->tag = sock_gdk_input_add(sockinfo,
770                                       atm->state[atm->num].condition,
771                                       automaton_input_cb, atm);
772
773         while (!atm->terminated)
774                 gtk_main_iteration();
775
776         log_verbosity_set(FALSE);
777         /* oha: see above */
778         recv_set_ui_func(NULL, NULL);
779
780         automaton_destroy(atm);
781
782         return pop3_state->inc_state;
783 }
784
785 static gint pop3_automaton_terminate(SockInfo *source, Automaton *atm)
786 {
787         if (atm->terminated) return 0;
788
789         if (atm->tag > 0) {
790                 gdk_input_remove(atm->tag);
791                 atm->tag = 0;
792         }
793         if (atm->timeout_tag > 0) {
794                 gtk_timeout_remove(atm->timeout_tag);
795                 atm->timeout_tag = 0;
796         }
797         if (source)
798                 sock_close(source);
799
800         atm->terminated = TRUE;
801
802         return 0;
803 }
804
805 static GHashTable *inc_get_uidl_table(PrefsAccount *ac_prefs)
806 {
807         GHashTable *table;
808         gchar *path;
809         FILE *fp;
810         gchar buf[IDLEN + 3];
811         GDate curdate;
812         gchar **data;
813
814         path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
815                            "uidl-", ac_prefs->recv_server,
816                            "-", ac_prefs->userid, NULL);
817                            
818         if ((fp = fopen(path, "rb")) == NULL) {
819                 if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
820                 g_free(path);
821                 return NULL;
822         }
823         g_free(path);
824
825         table = g_hash_table_new(g_str_hash, g_str_equal);
826
827         g_date_clear(&curdate, 1);
828
829         /*
830          * NOTE: g_date_set_time() has to be called inside this 
831          * loop, because a day change may happen??? That right?
832          */
833
834         while (fgets(buf, sizeof(buf), fp) != NULL) {
835                 strretchomp(buf);
836                 
837                 /* data[0] will contain uidl
838                  * data[1] will contain day of retrieval */
839
840                 /* 
841                  * FIXME: convoluted implementation. need to find
842                  * a better way to split the string.
843                  */
844                 if (strchr(buf, '\t')) {
845                         data = g_strsplit(buf, "\t", 2);
846                         if (data) {
847                                 g_hash_table_insert(table, g_strdup(data[0]), g_strdup(data[1]));
848                                 g_strfreev(data);
849                         }       
850                 } else {
851                         g_date_set_time(&curdate, time(NULL));  
852                         g_hash_table_insert(table, g_strdup(buf), 
853                                             g_strdup_printf("%d", g_date_day_of_year(&curdate)));
854                 }                           
855         }
856
857         fclose(fp);
858         return table;
859 }
860
861 static void inc_write_uidl_list(Pop3State *state)
862 {
863         gchar *path;
864         FILE *fp;
865         gint n;
866         GDate curdate;
867         const char *sdate;
868         int tdate;
869
870         if (!state->uidl_is_valid)
871                 return;
872         
873         path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
874                            "uidl-", state->ac_prefs->recv_server,
875                            "-", state->user, NULL);
876         if ((fp = fopen(path, "wb")) == NULL) {
877                 FILE_OP_ERROR(path, "fopen");
878                 g_free(path);
879                 return;
880         }
881
882         g_date_clear(&curdate, 1);
883
884         for (n = 1; n <= state->count; n++) {
885                 if (state->msg[n].uidl && state->msg[n].received &&
886                     !state->msg[n].deleted) {
887                         if (fputs(state->msg[n].uidl, fp) == EOF) {
888                                 FILE_OP_ERROR(path, "fputs");
889                                 break;
890                         }
891                         if (fputc('\t', fp) == EOF) {
892                                 FILE_OP_ERROR(path, "fputc");
893                                 break;
894                         }
895                         
896                         /*
897                          * NOTE: need to set time to watch for day changes??
898                          */
899                         g_date_set_time(&curdate, time(NULL));
900
901                         if (NULL != (sdate = g_hash_table_lookup(state->uidl_table, state->msg[n].uidl))) {
902                                 tdate = sdate != NULL ? atoi(sdate) : g_date_day_of_year(&curdate);
903                                 if (fprintf(fp, "%3d", tdate) == EOF) {
904                                         FILE_OP_ERROR(path, "fprintf");
905                                         break;
906                                 }
907                         } else {
908                                 if (fprintf(fp, "%d", g_date_day_of_year(&curdate)) == EOF) {
909                                         FILE_OP_ERROR(path, "fputs");
910                                         break;
911                                 }
912                         }
913
914                         if (fputc('\n', fp) == EOF) {
915                                 FILE_OP_ERROR(path, "fputc");
916                                 break;
917                         }               
918                 }
919         }
920
921         if (fclose(fp) == EOF) 
922                 FILE_OP_ERROR(path, "fclose");
923         g_free(path);
924 }
925
926 static gboolean inc_pop3_recv_func(SockInfo *sock, gint count, gint read_bytes,
927                                    gpointer data)
928 {
929         gchar buf[MSGBUFSIZE];
930         IncSession *session = (IncSession *)data;
931         Pop3State *state = session->pop3_state;
932         IncProgressDialog *inc_dialog = session->data;
933         ProgressDialog *dialog = inc_dialog->dialog;
934         gint cur_total;
935         gchar *total_size;
936
937         cur_total = state->cur_total_bytes + read_bytes;
938         if (cur_total > state->total_bytes)
939                 cur_total = state->total_bytes;
940
941         Xstrdup_a(total_size, to_human_readable(state->total_bytes),
942                   return FALSE);
943         g_snprintf(buf, sizeof(buf),
944                    _("Retrieving message (%d / %d) (%s / %s)"),
945                    state->cur_msg, state->count,
946                    to_human_readable(cur_total), total_size);
947         progress_dialog_set_label(dialog, buf);
948
949         progress_dialog_set_percentage
950                 (dialog, (gfloat)cur_total / (gfloat)state->total_bytes);
951         gtk_progress_bar_update
952                 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar),
953                  (gfloat)cur_total / (gfloat)state->total_bytes);
954         GTK_EVENTS_FLUSH();
955
956         if (state->inc_state == INC_CANCEL)
957                 return FALSE;
958         else
959                 return TRUE;
960 }
961
962 void inc_progress_update(Pop3State *state, Pop3Phase phase)
963 {
964         gchar buf[MSGBUFSIZE];
965         IncProgressDialog *inc_dialog = state->session->data;
966         ProgressDialog *dialog = inc_dialog->dialog;
967         gchar *total_size;
968
969         switch (phase) {
970         case POP3_GREETING_RECV:
971                 break;
972         case POP3_GETAUTH_USER_SEND:
973         case POP3_GETAUTH_PASS_SEND:
974         case POP3_GETAUTH_APOP_SEND:
975                 progress_dialog_set_label(dialog, _("Authenticating..."));
976                 break;
977         case POP3_GETRANGE_STAT_SEND:
978                 progress_dialog_set_label
979                         (dialog, _("Getting the number of new messages (STAT)..."));
980                 break;
981         case POP3_GETRANGE_LAST_SEND:
982                 progress_dialog_set_label
983                         (dialog, _("Getting the number of new messages (LAST)..."));
984                 break;
985         case POP3_GETRANGE_UIDL_SEND:
986                 progress_dialog_set_label
987                         (dialog, _("Getting the number of new messages (UIDL)..."));
988                 break;
989         case POP3_GETSIZE_LIST_SEND:
990                 progress_dialog_set_label
991                         (dialog, _("Getting the size of messages (LIST)..."));
992                 break;
993         case POP3_TOP_SEND:
994                 g_snprintf(buf, sizeof(buf),
995                            _("Retrieving header (%d / %d)"),
996                            state->cur_msg, state->count);
997                 progress_dialog_set_label (dialog, buf);
998                 progress_dialog_set_percentage
999                         (dialog,
1000                          (gfloat)(state->cur_msg) /
1001                          (gfloat)(state->count));
1002                 gtk_progress_bar_update 
1003                         (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar),
1004                          (gfloat)(state->cur_msg) /
1005                          (gfloat)(state->count));
1006                 break;
1007         case POP3_RETR_SEND:
1008                 Xstrdup_a(total_size, to_human_readable(state->total_bytes), return);
1009                 g_snprintf(buf, sizeof(buf),
1010                            _("Retrieving message (%d / %d) (%s / %s)"),
1011                            state->cur_msg, state->count,
1012                            to_human_readable(state->cur_total_bytes),
1013                            total_size);
1014                 progress_dialog_set_label(dialog, buf);
1015                 progress_dialog_set_percentage
1016                         (dialog,
1017                          (gfloat)(state->cur_total_bytes) /
1018                          (gfloat)(state->total_bytes));
1019                 gtk_progress_bar_update
1020                         (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar),
1021                          (gfloat)(state->cur_total_bytes) /
1022                          (gfloat)(state->total_bytes));
1023                 break;
1024 #if 0
1025         case POP3_DELETE_SEND:
1026                 progress_dialog_set_label(dialog, _("Deleting message"));
1027                 break;
1028 #endif
1029         case POP3_LOGOUT_SEND:
1030                 progress_dialog_set_label(dialog, _("Quitting"));
1031                 break;
1032         default:
1033                 break;
1034         }
1035 }
1036
1037 gint inc_drop_message(const gchar *file, Pop3State *state)
1038 {
1039         FolderItem *inbox;
1040         FolderItem *dropfolder;
1041         gint val;
1042         gint msgnum;
1043
1044         /* CLAWS: get default inbox (perhaps per account) */
1045         if (state->ac_prefs->inbox) {
1046                 /* CLAWS: get destination folder / mailbox */
1047                 inbox = folder_find_item_from_identifier
1048                         (state->ac_prefs->inbox);
1049                 if (!inbox)
1050                         inbox = folder_get_default_inbox();
1051         } else
1052                 inbox = folder_get_default_inbox();
1053         if (!inbox) {
1054                 unlink(file);
1055                 return -1;
1056         }
1057
1058         /* CLAWS: claws uses a global .processing folder for the filtering. */
1059         if (global_processing == NULL) {
1060                 if (state->ac_prefs->filter_on_recv) {
1061                         dropfolder =
1062                                 filter_get_dest_folder(prefs_common.fltlist, file);
1063                         if (!dropfolder) dropfolder = inbox;
1064                         else if (!strcmp(dropfolder->path, FILTER_NOT_RECEIVE)) {
1065                                 g_warning(_("a message won't be received\n"));
1066                                 return 1;
1067                         }
1068                 } else
1069                         dropfolder = inbox;
1070         } else {
1071                 dropfolder = folder_get_default_processing();
1072         }
1073
1074         val = GPOINTER_TO_INT(g_hash_table_lookup
1075                               (state->folder_table, dropfolder));
1076         if (val == 0) {
1077                 folder_item_scan(dropfolder);
1078                 g_hash_table_insert(state->folder_table, dropfolder,
1079                                     GINT_TO_POINTER(1));
1080         }
1081         
1082         /* add msg file to drop folder */
1083         if ((msgnum = folder_item_add_msg(dropfolder, file, TRUE)) < 0) {
1084                 unlink(file);
1085                 return -1;
1086         }
1087
1088         return 0;
1089 }
1090
1091 static void inc_put_error(IncState istate)
1092 {
1093         switch (istate) {
1094         case INC_ERROR:
1095                 if(!prefs_common.noerrorpanel) {
1096                         alertpanel_error(_("Error occurred while processing mail."));
1097                 }
1098                 break;
1099         case INC_NOSPACE:
1100                 alertpanel_error(_("No disk space left."));
1101                 break;
1102         default:
1103                 break;
1104         }
1105 }
1106
1107 static void inc_cancel(IncProgressDialog *dialog)
1108 {
1109         IncSession *session;
1110         SockInfo *sockinfo;
1111
1112         g_return_if_fail(dialog != NULL);
1113
1114         session = dialog->queue_list->data;
1115         sockinfo = session->pop3_state->sockinfo;
1116
1117         if (!sockinfo || session->atm->terminated == TRUE) return;
1118
1119         session->pop3_state->inc_state = INC_CANCEL;
1120         pop3_automaton_terminate(sockinfo, session->atm);
1121         session->pop3_state->sockinfo = NULL;
1122 }
1123
1124 gboolean inc_is_active(void)
1125 {
1126         return (inc_dialog_list != NULL);
1127 }
1128
1129 void inc_cancel_all(void)
1130 {
1131         GList *cur;
1132
1133         for (cur = inc_dialog_list; cur != NULL; cur = cur->next)
1134                 inc_cancel((IncProgressDialog *)cur->data);
1135 }
1136
1137 static void inc_cancel_cb(GtkWidget *widget, gpointer data)
1138 {
1139         inc_cancel((IncProgressDialog *)data);
1140 }
1141
1142 static gint inc_spool(void)
1143 {
1144         gchar *mbox, *logname;
1145         gint msgs;
1146
1147         logname = g_get_user_name();
1148         mbox = g_strconcat(prefs_common.spool_path
1149                            ? prefs_common.spool_path : DEFAULT_SPOOL_PATH,
1150                            G_DIR_SEPARATOR_S, logname, NULL);
1151         msgs = get_spool(folder_get_default_inbox(), mbox);
1152         g_free(mbox);
1153
1154         return msgs;
1155 }
1156
1157 static void inc_spool_account(PrefsAccount *account)
1158 {
1159         FolderItem *inbox;
1160         FolderItem *dropfolder;
1161         gint val;
1162
1163         if (account->inbox) {
1164                 inbox = folder_find_item_from_path(account->inbox);
1165                 if (!inbox)
1166                         inbox = folder_get_default_inbox();
1167         } else
1168                 inbox = folder_get_default_inbox();
1169
1170         get_spool(inbox, account->local_mbox);
1171 }
1172
1173 static void inc_all_spool(void)
1174 {
1175         GList *list = NULL;
1176
1177         list = account_get_list();
1178         if (!list) return;
1179
1180         for (; list != NULL; list = list->next) {
1181                 IncSession *session;
1182                 PrefsAccount *account = list->data;
1183
1184                 if ((account->protocol == A_LOCAL) &&
1185                     (account->recv_at_getall))
1186                         inc_spool_account(account);
1187         }
1188 }
1189
1190 static gint get_spool(FolderItem *dest, const gchar *mbox)
1191 {
1192         gint msgs, size;
1193         gint lockfd;
1194         gchar tmp_mbox[MAXPATHLEN + 1];
1195         GHashTable *folder_table = NULL;
1196
1197         g_return_val_if_fail(dest != NULL, -1);
1198         g_return_val_if_fail(mbox != NULL, -1);
1199
1200         if (!is_file_exist(mbox) || (size = get_file_size(mbox)) == 0) {
1201                 debug_print(_("no messages in local mailbox.\n"));
1202                 return 0;
1203         } else if (size < 0)
1204                 return -1;
1205
1206         if ((lockfd = lock_mbox(mbox, LOCK_FLOCK)) < 0)
1207                 return -1;
1208
1209         g_snprintf(tmp_mbox, sizeof(tmp_mbox), "%s%ctmpmbox%d",
1210                    get_rc_dir(), G_DIR_SEPARATOR, (gint)mbox);
1211
1212         if (copy_mbox(mbox, tmp_mbox) < 0) {
1213                 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1214                 return -1;
1215         }
1216
1217         debug_print(_("Getting new messages from %s into %s...\n"),
1218                     mbox, dest->path);
1219
1220         if (prefs_common.filter_on_inc)
1221                 folder_table = g_hash_table_new(NULL, NULL);
1222         msgs = proc_mbox(dest, tmp_mbox, folder_table);
1223
1224         unlink(tmp_mbox);
1225         if (msgs >= 0) empty_mbox(mbox);
1226         unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1227
1228         if (folder_table) {
1229                 if (!prefs_common.scan_all_after_inc) {
1230                 g_hash_table_insert(folder_table, dest,
1231                                     GINT_TO_POINTER(1));
1232                         folder_item_scan_foreach(folder_table);
1233                         folderview_update_item_foreach(folder_table);
1234                 }
1235                 g_hash_table_destroy(folder_table);
1236         } else if (!prefs_common.scan_all_after_inc) {
1237                 folder_item_scan(dest);
1238                 folderview_update_item(dest, FALSE);
1239         }
1240
1241         return msgs;
1242 }
1243
1244 void inc_lock(void)
1245 {
1246         inc_lock_count++;
1247 }
1248
1249 void inc_unlock(void)
1250 {
1251         if (inc_lock_count > 0)
1252                 inc_lock_count--;
1253 }
1254
1255 static guint autocheck_timer = 0;
1256 static gpointer autocheck_data = NULL;
1257
1258 static void inc_notify_cmd(gint new_msgs, gboolean notify)
1259 {
1260
1261         gchar *buf;
1262
1263         if (!(new_msgs && notify && prefs_common.newmail_notify_cmd &&
1264             *prefs_common.newmail_notify_cmd))
1265                      return;
1266         if ((buf = strchr(prefs_common.newmail_notify_cmd, '%')) &&
1267                 buf[1] == 'd' && !strchr(&buf[1], '%'))
1268                 buf = g_strdup_printf(prefs_common.newmail_notify_cmd, 
1269                                       new_msgs);
1270         else
1271                 buf = g_strdup(prefs_common.newmail_notify_cmd);
1272
1273         execute_command_line(buf, TRUE);
1274
1275         g_free(buf);
1276 }
1277  
1278 void inc_autocheck_timer_init(MainWindow *mainwin)
1279 {
1280         autocheck_data = mainwin;
1281         inc_autocheck_timer_set();
1282 }
1283
1284 static void inc_autocheck_timer_set_interval(guint interval)
1285 {
1286         inc_autocheck_timer_remove();
1287         /* last test is to avoid re-enabling auto_check after modifying 
1288            the common preferences */
1289         if (prefs_common.autochk_newmail && autocheck_data
1290             && prefs_common.work_offline == FALSE) {
1291                 autocheck_timer = gtk_timeout_add
1292                         (interval, inc_autocheck_func, autocheck_data);
1293                 debug_print("added timer = %d\n", autocheck_timer);
1294         }
1295 }
1296
1297 void inc_autocheck_timer_set(void)
1298 {
1299         inc_autocheck_timer_set_interval(prefs_common.autochk_itv * 60000);
1300 }
1301
1302 void inc_autocheck_timer_remove(void)
1303 {
1304         if (autocheck_timer) {
1305                 debug_print("removed timer = %d\n", autocheck_timer);
1306                 gtk_timeout_remove(autocheck_timer);
1307                 autocheck_timer = 0;
1308         }
1309 }
1310
1311 static gint inc_autocheck_func(gpointer data)
1312 {
1313         MainWindow *mainwin = (MainWindow *)data;
1314
1315         if (inc_lock_count) {
1316                 debug_print("autocheck is locked.\n");
1317                 inc_autocheck_timer_set_interval(1000);
1318                 return FALSE;
1319         }
1320
1321         inc_all_account_mail(mainwin, prefs_common.newmail_notify_auto);
1322
1323         return FALSE;
1324 }