* src/filter.c
[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 <time.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <sys/wait.h>
38 #include <signal.h>
39 #include <errno.h>
40
41 #include "intl.h"
42 #include "main.h"
43 #include "inc.h"
44 #include "mainwindow.h"
45 #include "folderview.h"
46 #include "summaryview.h"
47 #include "prefs_common.h"
48 #include "prefs_account.h"
49 #include "account.h"
50 #include "procmsg.h"
51 #include "socket.h"
52 #include "ssl.h"
53 #include "pop.h"
54 #include "recv.h"
55 #include "mbox.h"
56 #include "utils.h"
57 #include "gtkutils.h"
58 #include "statusbar.h"
59 #include "manage_window.h"
60 #include "stock_pixmap.h"
61 #include "progressdialog.h"
62 #include "inputdialog.h"
63 #include "alertpanel.h"
64 #include "filter.h"
65 #include "automaton.h"
66 #include "folder.h"
67 #include "filtering.h"
68 #include "selective_download.h"
69
70 static GList *inc_dialog_list = NULL;
71
72 static guint inc_lock_count = 0;
73
74 static GdkPixmap *currentxpm;
75 static GdkBitmap *currentxpmmask;
76 static GdkPixmap *errorxpm;
77 static GdkBitmap *errorxpmmask;
78 static GdkPixmap *okxpm;
79 static GdkBitmap *okxpmmask;
80
81 #define MSGBUFSIZE      8192
82
83 static void inc_finished                (MainWindow             *mainwin,
84                                          gboolean                new_messages);
85 static gint inc_account_mail            (PrefsAccount           *account,
86                                          MainWindow             *mainwin);
87
88 static IncProgressDialog *inc_progress_dialog_create    (void);
89 static void inc_progress_dialog_destroy (IncProgressDialog      *inc_dialog);
90
91 static IncSession *inc_session_new      (PrefsAccount           *account);
92 static void inc_session_destroy         (IncSession             *session);
93 static Pop3State *inc_pop3_state_new    (PrefsAccount           *account);
94 static void inc_pop3_state_destroy      (Pop3State              *state);
95 static gint inc_start                   (IncProgressDialog      *inc_dialog);
96 static IncState inc_pop3_session_do     (IncSession             *session);
97 static gint pop3_automaton_terminate    (SockInfo               *source,
98                                          Automaton              *atm);
99
100 static GHashTable *inc_get_uidl_table   (PrefsAccount           *ac_prefs);
101 static void inc_write_uidl_list         (Pop3State              *state);
102
103 static gboolean inc_pop3_recv_func      (SockInfo       *sock,
104                                          gint            count,
105                                          gint            read_bytes,
106                                          gpointer        data);
107
108 static void inc_put_error               (IncState        istate);
109
110 static void inc_cancel_cb               (GtkWidget      *widget,
111                                          gpointer        data);
112
113 static gint inc_spool                   (void);
114 static gint get_spool                   (FolderItem     *dest,
115                                          const gchar    *mbox);
116
117 static void inc_spool_account(PrefsAccount *account);
118 static void inc_all_spool(void);
119 static void inc_autocheck_timer_set_interval    (guint           interval);
120 static gint inc_autocheck_func                  (gpointer        data);
121
122 static void inc_notify_cmd              (gint new_msgs, 
123                                          gboolean notify);
124
125 #define FOLDER_SUMMARY_MISMATCH(f, s) \
126         (f) && (s) ? ((s)->newmsgs != (f)->new) || ((f)->unread != (s)->unread) || ((f)->total != (s)->messages) \
127         : FALSE
128         
129 /**
130  * inc_finished:
131  * @mainwin: Main window.
132  * @new_messages: TRUE if some messages have been received.
133  * 
134  * Update the folder view and the summary view after receiving
135  * messages.  If @new_messages is FALSE, this function avoids unneeded
136  * updating.
137  **/
138 static void inc_finished(MainWindow *mainwin, gboolean new_messages)
139 {
140         FolderItem *item;
141
142         if (prefs_common.scan_all_after_inc)
143                 folderview_check_new(NULL);
144
145         if (!new_messages && !prefs_common.scan_all_after_inc) return;
146
147         if (prefs_common.open_inbox_on_inc) {
148                 item = cur_account && cur_account->inbox
149                         ? folder_find_item_from_identifier(cur_account->inbox)
150                         : folder_get_default_inbox();
151                 if (FOLDER_SUMMARY_MISMATCH(item, mainwin->summaryview)) {      
152                         folderview_unselect(mainwin->folderview);
153                         folderview_select(mainwin->folderview, item);
154                 }       
155         } else {
156                 item = mainwin->summaryview->folder_item;
157                 if (FOLDER_SUMMARY_MISMATCH(item, mainwin->summaryview)) {
158                         folderview_unselect(mainwin->folderview);
159                         folderview_select(mainwin->folderview, item);
160                 }       
161         }
162 }
163
164 void inc_mail(MainWindow *mainwin, gboolean notify)
165 {
166         gint new_msgs = 0;
167
168         if (inc_lock_count) return;
169
170         if (prefs_common.work_offline)
171                 if (alertpanel(_("Offline warning"), 
172                                _("You're working offline. Override?"),
173                                _("Yes"), _("No"), NULL) != G_ALERTDEFAULT)
174                 return;
175
176         inc_autocheck_timer_remove();
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         return 0;
264 }
265
266 void inc_all_account_mail(MainWindow *mainwin, gboolean notify)
267 {
268         GList *list, *queue_list = NULL;
269         IncProgressDialog *inc_dialog;
270         gint new_msgs = 0;
271         
272         if (prefs_common.work_offline)
273                 if (alertpanel(_("Offline warning"), 
274                                _("You're working offline. Override?"),
275                                _("Yes"), _("No"), NULL) != G_ALERTDEFAULT)
276                 return;
277
278         if (inc_lock_count) return;
279
280         inc_autocheck_timer_remove();
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         state->current_time = time(NULL);
451
452         return state;
453 }
454
455 static void inc_pop3_state_destroy(Pop3State *state)
456 {
457         gint n;
458
459         g_hash_table_destroy(state->folder_table);
460
461         for (n = 1; n <= state->count; n++)
462                 g_free(state->msg[n].uidl);
463         g_free(state->msg);
464
465         if (state->uidl_table) {
466                 hash_free_strings(state->uidl_table);
467                 g_hash_table_destroy(state->uidl_table);
468         }
469         g_slist_free(state->uidl_todelete_list);
470         g_free(state->greeting);
471         g_free(state->user);
472         g_free(state->pass);
473         g_free(state->prev_folder);
474
475         g_free(state);
476 }
477
478 static gint inc_start(IncProgressDialog *inc_dialog)
479 {
480         IncSession *session;
481         GtkCList *clist = GTK_CLIST(inc_dialog->dialog->clist);
482         Pop3State *pop3_state;
483         IncState inc_state;
484         gint num = 0;
485         gint error_num = 0;
486         gint new_msgs = 0;
487
488         while (inc_dialog->queue_list != NULL) {
489                 session = inc_dialog->queue_list->data;
490                 pop3_state = session->pop3_state;
491
492                 inc_progress_dialog_clear(inc_dialog);
493
494                 gtk_clist_moveto(clist, num, -1, 1.0, 0.0);
495
496                 pop3_state->user = g_strdup(pop3_state->ac_prefs->userid);
497                 if (pop3_state->ac_prefs->passwd)
498                         pop3_state->pass =
499                                 g_strdup(pop3_state->ac_prefs->passwd);
500                 else if (pop3_state->ac_prefs->tmp_pass)
501                         pop3_state->pass =
502                                 g_strdup(pop3_state->ac_prefs->tmp_pass);
503                 else {
504                         gchar *pass;
505
506                         pass = input_dialog_query_password
507                                 (pop3_state->ac_prefs->recv_server,
508                                  pop3_state->user);
509
510                         if (inc_dialog->show_dialog)
511                                 manage_window_focus_in
512                                         (inc_dialog->mainwin->window,
513                                          NULL, NULL);
514                         if (pass) {
515                                 pop3_state->ac_prefs->tmp_pass = g_strdup(pass);
516                                 pop3_state->pass = pass;
517                         } else {
518                                 inc_session_destroy(session);
519                                 inc_dialog->queue_list = g_list_remove
520                                         (inc_dialog->queue_list, session);
521                                 continue;
522                         }
523                 }
524
525                 gtk_clist_set_pixmap(clist, num, 0, currentxpm, currentxpmmask);
526                 gtk_clist_set_text(clist, num, 2, _("Retrieving"));
527
528                 /* begin POP3 session */
529                 inc_state = inc_pop3_session_do(session);
530
531                 if (inc_state == INC_SUCCESS) {
532                         gtk_clist_set_pixmap(clist, num, 0, okxpm, okxpmmask);
533                         gtk_clist_set_text(clist, num, 2, _("Done"));
534                 } else if (inc_state == INC_CANCEL) {
535                         gtk_clist_set_pixmap(clist, num, 0, okxpm, okxpmmask);
536                         gtk_clist_set_text(clist, num, 2, _("Cancelled"));
537                 } else {
538                         gtk_clist_set_pixmap(clist, num, 0, errorxpm, errorxpmmask);
539                         if (inc_state == INC_CONNECT_ERROR)
540                                 gtk_clist_set_text(clist, num, 2,
541                                                    _("Connection failed"));
542                         else if (inc_state == INC_AUTH_FAILED)
543                                 gtk_clist_set_text(clist, num, 2,
544                                                    _("Auth failed"));
545                         else
546                                 gtk_clist_set_text(clist, num, 2, _("Error"));
547                 }
548
549                 if (pop3_state->error_val == PS_AUTHFAIL) {
550                         if(!prefs_common.no_recv_err_panel) {
551                                 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
552                                     ((prefs_common.recv_dialog_mode == RECV_DIALOG_ACTIVE) && focus_window)) {
553                                         manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
554                                 }
555                                 alertpanel_error
556                                         (_("Authorization for %s on %s failed"),
557                                          pop3_state->user,
558                                          pop3_state->ac_prefs->recv_server);
559                         }
560                 }
561
562                 statusbar_pop_all();
563
564                 /* CLAWS: perform filtering actions on dropped message */
565                 if (global_processing != NULL) {
566                         FolderItem *processing, *inbox;
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                         msglist = folder_item_get_msg_list(processing);
583
584                         /* process messages */
585                         for(msglist_element = msglist; msglist_element != NULL; msglist_element = msglist_element->next) {
586                                 msginfo = (MsgInfo *) msglist_element->data;
587                                 /* filter if enabled in prefs or move to inbox if not */
588                                 if(pop3_state->ac_prefs->filter_on_recv) {
589                                         filter_message_by_msginfo_with_inbox(global_processing, msginfo,
590                                                                              pop3_state->folder_table,
591                                                                              inbox);
592                                 } else {
593                                         folder_item_move_msg(inbox, msginfo);
594                                         g_hash_table_insert(pop3_state->folder_table, inbox,
595                                                             GINT_TO_POINTER(1));
596                                 }
597                                 procmsg_msginfo_free(msginfo);
598                         }
599                         g_slist_free(msglist);
600                 }
601
602
603                 new_msgs += pop3_state->cur_total_num;
604
605                 folderview_update_item_foreach
606                         (pop3_state->folder_table);
607
608                 if (pop3_state->error_val == PS_AUTHFAIL &&
609                     pop3_state->ac_prefs->tmp_pass) {
610                         g_free(pop3_state->ac_prefs->tmp_pass);
611                         pop3_state->ac_prefs->tmp_pass = NULL;
612                 }
613
614                 inc_write_uidl_list(pop3_state);
615
616                 if (inc_state != INC_SUCCESS && inc_state != INC_CANCEL) {
617                         error_num++;
618                         if (inc_state == INC_NOSPACE) {
619                                 inc_put_error(inc_state);
620                                 break;
621                         }
622                 }
623
624                 inc_session_destroy(session);
625                 inc_dialog->queue_list =
626                         g_list_remove(inc_dialog->queue_list, session);
627
628                 num++;
629         }
630
631         if (error_num && !prefs_common.no_recv_err_panel) {
632                 if (inc_dialog->show_dialog)
633                         manage_window_focus_in(inc_dialog->dialog->window,
634                                                NULL, NULL);
635                 alertpanel_error(_("Some errors occured while getting mail."));
636                 if (inc_dialog->show_dialog)
637                         manage_window_focus_out(inc_dialog->dialog->window,
638                                                 NULL, NULL);
639         }
640
641         while (inc_dialog->queue_list != NULL) {
642                 session = inc_dialog->queue_list->data;
643                 inc_session_destroy(session);
644                 inc_dialog->queue_list =
645                         g_list_remove(inc_dialog->queue_list, session);
646         }
647
648         inc_progress_dialog_destroy(inc_dialog);
649
650         return new_msgs;
651 }
652
653 static IncState inc_pop3_session_do(IncSession *session)
654 {
655         Pop3State *pop3_state = session->pop3_state;
656         IncProgressDialog *inc_dialog = (IncProgressDialog *)session->data;
657         Automaton *atm;
658         SockInfo *sockinfo;
659         gint i;
660         gchar *server;
661         gushort port;
662         gchar *buf;
663         static AtmHandler handlers[] = {
664                 pop3_greeting_recv      ,
665 #if USE_SSL
666                 pop3_stls_send          , pop3_stls_recv,
667 #endif
668                 pop3_getauth_user_send  , pop3_getauth_user_recv,
669                 pop3_getauth_pass_send  , pop3_getauth_pass_recv,
670                 pop3_getauth_apop_send  , pop3_getauth_apop_recv,
671                 pop3_getrange_stat_send , pop3_getrange_stat_recv,
672                 pop3_getrange_last_send , pop3_getrange_last_recv,
673                 pop3_getrange_uidl_send , pop3_getrange_uidl_recv,
674                 pop3_getsize_list_send  , pop3_getsize_list_recv,
675                 pop3_top_send           , pop3_top_recv,
676                 pop3_retr_send          , pop3_retr_recv,
677                 pop3_delete_send        , pop3_delete_recv,
678                 pop3_logout_send        , pop3_logout_recv
679         };
680
681         debug_print(_("getting new messages of account %s...\n"),
682                     pop3_state->ac_prefs->account_name);
683
684         atm = automaton_create(N_POP3_PHASE);
685
686         session->atm = atm;
687         atm->data = pop3_state;
688
689         buf = g_strdup_printf(_("%s: Retrieving new messages"),
690                               pop3_state->ac_prefs->recv_server);
691         gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window), buf);
692         g_free(buf);
693
694         for (i = POP3_GREETING_RECV; i < N_POP3_PHASE; i++)
695                 atm->state[i].handler = handlers[i];
696         atm->state[POP3_GREETING_RECV].condition = GDK_INPUT_READ;
697         for (i = POP3_GREETING_RECV + 1; i < N_POP3_PHASE; ) {
698                 atm->state[i++].condition = GDK_INPUT_WRITE;
699                 atm->state[i++].condition = GDK_INPUT_READ;
700         }
701
702         atm->terminate = (AtmHandler)pop3_automaton_terminate;
703         atm->ui_func = (AtmUIFunc)inc_progress_update;
704
705         atm->num = POP3_GREETING_RECV;
706
707         server = pop3_state->ac_prefs->recv_server;
708 #if USE_SSL
709         port = pop3_state->ac_prefs->set_popport ?
710                 pop3_state->ac_prefs->popport :
711                 pop3_state->ac_prefs->ssl_pop == SSL_TUNNEL ? 995 : 110;
712 #else
713         port = pop3_state->ac_prefs->set_popport ?
714                 pop3_state->ac_prefs->popport : 110;
715 #endif
716
717         buf = g_strdup_printf(_("Connecting to POP3 server: %s ..."), server);
718         log_message("%s\n", buf);
719         progress_dialog_set_label(inc_dialog->dialog, buf);
720         g_free(buf);
721         GTK_EVENTS_FLUSH();
722         statusbar_pop_all();
723
724         if ((sockinfo = sock_connect(server, port)) == NULL) {
725                 log_warning(_("Can't connect to POP3 server: %s:%d\n"),
726                             server, port);
727                 if(!prefs_common.no_recv_err_panel) {
728                         if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
729                             ((prefs_common.recv_dialog_mode == RECV_DIALOG_ACTIVE) && focus_window)) {
730                                 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
731                         }
732                         alertpanel_error(_("Can't connect to POP3 server: %s:%d"),
733                                          server, port);
734                         manage_window_focus_out(inc_dialog->dialog->window, NULL, NULL);
735                 }
736                 pop3_automaton_terminate(NULL, atm);
737                 automaton_destroy(atm);
738                 return INC_CONNECT_ERROR;
739         }
740
741 #if USE_SSL
742         if (pop3_state->ac_prefs->ssl_pop == SSL_TUNNEL &&
743             !ssl_init_socket(sockinfo)) {
744                 pop3_automaton_terminate(NULL, atm);
745                 automaton_destroy(atm);
746                 return INC_CONNECT_ERROR;
747         }
748 #endif
749
750         /* :WK: Hmmm, with the later sock_gdk_input, we have 2 references
751          * to the sock structure - implement a reference counter?? */
752         pop3_state->sockinfo = sockinfo;
753         atm->help_sock = sockinfo;
754
755         log_verbosity_set(TRUE);
756         /* oha: this messes up inc_progress update:
757            disabling this would avoid the label "Retrieve Header"
758            being overwritten by "Retrieve Message"
759            Setting inc_pop3_recv_func is not necessary
760            since atm already handles the progress dialog ui
761            just fine.
762         */
763         recv_set_ui_func(inc_pop3_recv_func, session);
764
765         atm->tag = sock_gdk_input_add(sockinfo,
766                                       atm->state[atm->num].condition,
767                                       automaton_input_cb, atm);
768
769         while (!atm->terminated)
770                 gtk_main_iteration();
771
772         log_verbosity_set(FALSE);
773         /* oha: see above */
774         recv_set_ui_func(NULL, NULL);
775
776         automaton_destroy(atm);
777
778         return pop3_state->inc_state;
779 }
780
781 static gint pop3_automaton_terminate(SockInfo *source, Automaton *atm)
782 {
783         if (atm->terminated) return 0;
784
785         if (atm->tag > 0) {
786                 gdk_input_remove(atm->tag);
787                 atm->tag = 0;
788         }
789         if (atm->timeout_tag > 0) {
790                 gtk_timeout_remove(atm->timeout_tag);
791                 atm->timeout_tag = 0;
792         }
793         if (source)
794                 sock_close(source);
795
796         atm->terminated = TRUE;
797
798         return 0;
799 }
800
801 static GHashTable *inc_get_uidl_table(PrefsAccount *ac_prefs)
802 {
803         GHashTable *table;
804         gchar *path;
805         FILE *fp;
806         gchar buf[IDLEN + 3];
807         gchar uidl[IDLEN + 3];
808         time_t recv_time;
809         time_t now;
810
811         path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
812                            "uidl", G_DIR_SEPARATOR_S, ac_prefs->recv_server,
813                            "-", ac_prefs->userid, NULL);
814         if ((fp = fopen(path, "rb")) == NULL) {
815                 if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
816                 g_free(path);
817                 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
818                                    "uidl-", ac_prefs->recv_server,
819                                    "-", ac_prefs->userid, NULL);
820                 if ((fp = fopen(path, "rb")) == NULL) {
821                         if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
822                         g_free(path);
823                         return NULL;
824                 }
825                 g_free(path);
826         }
827
828         table = g_hash_table_new(g_str_hash, g_str_equal);
829
830         now = time(NULL);
831
832         while (fgets(buf, sizeof(buf), fp) != NULL) {
833                 strretchomp(buf);
834                 recv_time = 0;
835                 if (sscanf(buf, "%s\t%ld", uidl, &recv_time) != 2) {
836                         if (sscanf(buf, "%s", uidl) != 1)
837                                 continue;
838                         else
839                                 recv_time = now;
840                 }
841                 if (recv_time == 0)
842                         recv_time = 1;
843                 g_hash_table_insert(table, g_strdup(uidl),
844                                     GINT_TO_POINTER(recv_time));
845         }
846
847         fclose(fp);
848         return table;
849 }
850
851 static void inc_write_uidl_list(Pop3State *state)
852 {
853         gchar *path;
854         FILE *fp;
855         Pop3MsgInfo *msg;
856         gint n;
857
858         if (!state->uidl_is_valid) return;
859
860         path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
861                            "uidl", G_DIR_SEPARATOR_S,
862                            state->ac_prefs->recv_server,
863                            "-", state->ac_prefs->userid, NULL);
864         if ((fp = fopen(path, "wb")) == NULL) {
865                 FILE_OP_ERROR(path, "fopen");
866                 g_free(path);
867                 return;
868         }
869
870         for (n = 1; n <= state->count; n++) {
871                 msg = &state->msg[n];
872                 if (msg->uidl && msg->received && !msg->deleted)
873                         fprintf(fp, "%s\t%ld\n", msg->uidl, msg->recv_time);
874         }
875
876         if (fclose(fp) == EOF) FILE_OP_ERROR(path, "fclose");
877         g_free(path);
878 }
879
880 static gboolean inc_pop3_recv_func(SockInfo *sock, gint count, gint read_bytes,
881                                    gpointer data)
882 {
883         gchar buf[MSGBUFSIZE];
884         IncSession *session = (IncSession *)data;
885         Pop3State *state = session->pop3_state;
886         IncProgressDialog *inc_dialog = session->data;
887         ProgressDialog *dialog = inc_dialog->dialog;
888         gint cur_total;
889         gchar *total_size;
890
891         cur_total = state->cur_total_bytes + read_bytes;
892         if (cur_total > state->total_bytes)
893                 cur_total = state->total_bytes;
894
895         Xstrdup_a(total_size, to_human_readable(state->total_bytes),
896                   return FALSE);
897         g_snprintf(buf, sizeof(buf),
898                    _("Retrieving message (%d / %d) (%s / %s)"),
899                    state->cur_msg, state->count,
900                    to_human_readable(cur_total), total_size);
901         progress_dialog_set_label(dialog, buf);
902
903         progress_dialog_set_percentage
904                 (dialog, (gfloat)cur_total / (gfloat)state->total_bytes);
905         gtk_progress_bar_update
906                 (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar),
907                  (gfloat)cur_total / (gfloat)state->total_bytes);
908         GTK_EVENTS_FLUSH();
909
910         if (state->inc_state == INC_CANCEL)
911                 return FALSE;
912         else
913                 return TRUE;
914 }
915
916 void inc_progress_update(Pop3State *state, Pop3Phase phase)
917 {
918         gchar buf[MSGBUFSIZE];
919         IncProgressDialog *inc_dialog = state->session->data;
920         ProgressDialog *dialog = inc_dialog->dialog;
921         gchar *total_size;
922
923         switch (phase) {
924         case POP3_GREETING_RECV:
925                 break;
926         case POP3_GETAUTH_USER_SEND:
927         case POP3_GETAUTH_PASS_SEND:
928         case POP3_GETAUTH_APOP_SEND:
929                 progress_dialog_set_label(dialog, _("Authenticating..."));
930                 break;
931         case POP3_GETRANGE_STAT_SEND:
932                 progress_dialog_set_label
933                         (dialog, _("Getting the number of new messages (STAT)..."));
934                 break;
935         case POP3_GETRANGE_LAST_SEND:
936                 progress_dialog_set_label
937                         (dialog, _("Getting the number of new messages (LAST)..."));
938                 break;
939         case POP3_GETRANGE_UIDL_SEND:
940                 progress_dialog_set_label
941                         (dialog, _("Getting the number of new messages (UIDL)..."));
942                 break;
943         case POP3_GETSIZE_LIST_SEND:
944                 progress_dialog_set_label
945                         (dialog, _("Getting the size of messages (LIST)..."));
946                 break;
947         case POP3_TOP_SEND:
948                 g_snprintf(buf, sizeof(buf),
949                            _("Retrieving header (%d / %d)"),
950                            state->cur_msg, state->count);
951                 progress_dialog_set_label (dialog, buf);
952                 progress_dialog_set_percentage
953                         (dialog,
954                          (gfloat)(state->cur_msg) /
955                          (gfloat)(state->count));
956                 gtk_progress_bar_update 
957                         (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar),
958                          (gfloat)(state->cur_msg) /
959                          (gfloat)(state->count));
960                 break;
961         case POP3_RETR_SEND:
962                 Xstrdup_a(total_size, to_human_readable(state->total_bytes), return);
963                 g_snprintf(buf, sizeof(buf),
964                            _("Retrieving message (%d / %d) (%s / %s)"),
965                            state->cur_msg, state->count,
966                            to_human_readable(state->cur_total_bytes),
967                            total_size);
968                 progress_dialog_set_label(dialog, buf);
969                 progress_dialog_set_percentage
970                         (dialog,
971                          (gfloat)(state->cur_total_bytes) /
972                          (gfloat)(state->total_bytes));
973                 gtk_progress_bar_update
974                         (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar),
975                          (gfloat)(state->cur_total_bytes) /
976                          (gfloat)(state->total_bytes));
977                 break;
978         case POP3_DELETE_SEND:
979                 if (state->msg[state->cur_msg].recv_time < state->current_time) {
980                         g_snprintf(buf, sizeof(buf), _("Deleting message %d"),
981                                    state->cur_msg);
982                         progress_dialog_set_label(dialog, buf);
983                 }
984                 break;
985         case POP3_LOGOUT_SEND:
986                 progress_dialog_set_label(dialog, _("Quitting"));
987                 break;
988         default:
989                 break;
990         }
991 }
992
993 gint inc_drop_message(const gchar *file, Pop3State *state)
994 {
995         FolderItem *inbox;
996         FolderItem *dropfolder;
997         gint val;
998         gint msgnum;
999
1000         /* CLAWS: get default inbox (perhaps per account) */
1001         if (state->ac_prefs->inbox) {
1002                 /* CLAWS: get destination folder / mailbox */
1003                 inbox = folder_find_item_from_identifier
1004                         (state->ac_prefs->inbox);
1005                 if (!inbox)
1006                         inbox = folder_get_default_inbox();
1007         } else
1008                 inbox = folder_get_default_inbox();
1009         if (!inbox) {
1010                 unlink(file);
1011                 return -1;
1012         }
1013
1014         /* CLAWS: claws uses a global .processing folder for the filtering. */
1015         if (global_processing == NULL) {
1016                 if (state->ac_prefs->filter_on_recv) {
1017                         dropfolder =
1018                                 filter_get_dest_folder(prefs_common.fltlist, file);
1019                         if (!dropfolder) dropfolder = inbox;
1020                         else if (!strcmp(dropfolder->path, FILTER_NOT_RECEIVE)) {
1021                                 g_warning(_("a message won't be received\n"));
1022                                 return 1;
1023                         }
1024                 } else
1025                         dropfolder = inbox;
1026         } else {
1027                 dropfolder = folder_get_default_processing();
1028         }
1029
1030         val = GPOINTER_TO_INT(g_hash_table_lookup
1031                               (state->folder_table, dropfolder));
1032         if (val == 0) {
1033                 g_hash_table_insert(state->folder_table, dropfolder,
1034                                     GINT_TO_POINTER(1));
1035         }
1036         
1037         /* add msg file to drop folder */
1038         if ((msgnum = folder_item_add_msg(dropfolder, file, TRUE)) < 0) {
1039                 unlink(file);
1040                 return -1;
1041         }
1042
1043         return 0;
1044 }
1045
1046 static void inc_put_error(IncState istate)
1047 {
1048         switch (istate) {
1049         case INC_ERROR:
1050                 if (!prefs_common.no_recv_err_panel)
1051                         alertpanel_error
1052                                 (_("Error occurred while processing mail."));
1053                 break;
1054         case INC_NOSPACE:
1055                 alertpanel_error(_("No disk space left."));
1056                 break;
1057         default:
1058                 break;
1059         }
1060 }
1061
1062 static void inc_cancel(IncProgressDialog *dialog)
1063 {
1064         IncSession *session;
1065         SockInfo *sockinfo;
1066
1067         g_return_if_fail(dialog != NULL);
1068
1069         session = dialog->queue_list->data;
1070         sockinfo = session->pop3_state->sockinfo;
1071
1072         if (!sockinfo || session->atm->terminated == TRUE) return;
1073
1074         session->pop3_state->inc_state = INC_CANCEL;
1075         pop3_automaton_terminate(sockinfo, session->atm);
1076         session->pop3_state->sockinfo = NULL;
1077 }
1078
1079 gboolean inc_is_active(void)
1080 {
1081         return (inc_dialog_list != NULL);
1082 }
1083
1084 void inc_cancel_all(void)
1085 {
1086         GList *cur;
1087
1088         for (cur = inc_dialog_list; cur != NULL; cur = cur->next)
1089                 inc_cancel((IncProgressDialog *)cur->data);
1090 }
1091
1092 static void inc_cancel_cb(GtkWidget *widget, gpointer data)
1093 {
1094         inc_cancel((IncProgressDialog *)data);
1095 }
1096
1097 static gint inc_spool(void)
1098 {
1099         gchar *mbox, *logname;
1100         gint msgs;
1101
1102         logname = g_get_user_name();
1103         mbox = g_strconcat(prefs_common.spool_path
1104                            ? prefs_common.spool_path : DEFAULT_SPOOL_PATH,
1105                            G_DIR_SEPARATOR_S, logname, NULL);
1106         msgs = get_spool(folder_get_default_inbox(), mbox);
1107         g_free(mbox);
1108
1109         return msgs;
1110 }
1111
1112 static void inc_spool_account(PrefsAccount *account)
1113 {
1114         FolderItem *inbox;
1115
1116         if (account->inbox) {
1117                 inbox = folder_find_item_from_path(account->inbox);
1118                 if (!inbox)
1119                         inbox = folder_get_default_inbox();
1120         } else
1121                 inbox = folder_get_default_inbox();
1122
1123         get_spool(inbox, account->local_mbox);
1124 }
1125
1126 static void inc_all_spool(void)
1127 {
1128         GList *list = NULL;
1129
1130         list = account_get_list();
1131         if (!list) return;
1132
1133         for (; list != NULL; list = list->next) {
1134                 PrefsAccount *account = list->data;
1135
1136                 if ((account->protocol == A_LOCAL) &&
1137                     (account->recv_at_getall))
1138                         inc_spool_account(account);
1139         }
1140 }
1141
1142 static gint get_spool(FolderItem *dest, const gchar *mbox)
1143 {
1144         gint msgs, size;
1145         gint lockfd;
1146         gchar tmp_mbox[MAXPATHLEN + 1];
1147         GHashTable *folder_table = NULL;
1148
1149         g_return_val_if_fail(dest != NULL, -1);
1150         g_return_val_if_fail(mbox != NULL, -1);
1151
1152         if (!is_file_exist(mbox) || (size = get_file_size(mbox)) == 0) {
1153                 debug_print(_("no messages in local mailbox.\n"));
1154                 return 0;
1155         } else if (size < 0)
1156                 return -1;
1157
1158         if ((lockfd = lock_mbox(mbox, LOCK_FLOCK)) < 0)
1159                 return -1;
1160
1161         g_snprintf(tmp_mbox, sizeof(tmp_mbox), "%s%ctmpmbox%d",
1162                    get_rc_dir(), G_DIR_SEPARATOR, (gint)mbox);
1163
1164         if (copy_mbox(mbox, tmp_mbox) < 0) {
1165                 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1166                 return -1;
1167         }
1168
1169         debug_print(_("Getting new messages from %s into %s...\n"),
1170                     mbox, dest->path);
1171
1172         if (prefs_common.filter_on_inc)
1173                 folder_table = g_hash_table_new(NULL, NULL);
1174         msgs = proc_mbox(dest, tmp_mbox, folder_table);
1175
1176         unlink(tmp_mbox);
1177         if (msgs >= 0) empty_mbox(mbox);
1178         unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1179
1180         if (folder_table) {
1181                 if (!prefs_common.scan_all_after_inc) {
1182                 g_hash_table_insert(folder_table, dest,
1183                                     GINT_TO_POINTER(1));
1184                         folderview_update_item_foreach(folder_table);
1185                 }
1186                 g_hash_table_destroy(folder_table);
1187         } else if (!prefs_common.scan_all_after_inc) {
1188                 folderview_update_item(dest, FALSE);
1189         }
1190
1191         return msgs;
1192 }
1193
1194 void inc_lock(void)
1195 {
1196         inc_lock_count++;
1197 }
1198
1199 void inc_unlock(void)
1200 {
1201         if (inc_lock_count > 0)
1202                 inc_lock_count--;
1203 }
1204
1205 static guint autocheck_timer = 0;
1206 static gpointer autocheck_data = NULL;
1207
1208 static void inc_notify_cmd(gint new_msgs, gboolean notify)
1209 {
1210
1211         gchar *buf;
1212
1213         if (!(new_msgs && notify && prefs_common.newmail_notify_cmd &&
1214             *prefs_common.newmail_notify_cmd))
1215                      return;
1216         if ((buf = strchr(prefs_common.newmail_notify_cmd, '%')) &&
1217                 buf[1] == 'd' && !strchr(&buf[1], '%'))
1218                 buf = g_strdup_printf(prefs_common.newmail_notify_cmd, 
1219                                       new_msgs);
1220         else
1221                 buf = g_strdup(prefs_common.newmail_notify_cmd);
1222
1223         execute_command_line(buf, TRUE);
1224
1225         g_free(buf);
1226 }
1227  
1228 void inc_autocheck_timer_init(MainWindow *mainwin)
1229 {
1230         autocheck_data = mainwin;
1231         inc_autocheck_timer_set();
1232 }
1233
1234 static void inc_autocheck_timer_set_interval(guint interval)
1235 {
1236         inc_autocheck_timer_remove();
1237         /* last test is to avoid re-enabling auto_check after modifying 
1238            the common preferences */
1239         if (prefs_common.autochk_newmail && autocheck_data
1240             && prefs_common.work_offline == FALSE) {
1241                 autocheck_timer = gtk_timeout_add
1242                         (interval, inc_autocheck_func, autocheck_data);
1243                 debug_print("added timer = %d\n", autocheck_timer);
1244         }
1245 }
1246
1247 void inc_autocheck_timer_set(void)
1248 {
1249         inc_autocheck_timer_set_interval(prefs_common.autochk_itv * 60000);
1250 }
1251
1252 void inc_autocheck_timer_remove(void)
1253 {
1254         if (autocheck_timer) {
1255                 debug_print("removed timer = %d\n", autocheck_timer);
1256                 gtk_timeout_remove(autocheck_timer);
1257                 autocheck_timer = 0;
1258         }
1259 }
1260
1261 static gint inc_autocheck_func(gpointer data)
1262 {
1263         MainWindow *mainwin = (MainWindow *)data;
1264
1265         if (inc_lock_count) {
1266                 debug_print("autocheck is locked.\n");
1267                 inc_autocheck_timer_set_interval(1000);
1268                 return FALSE;
1269         }
1270
1271         inc_all_account_mail(mainwin, prefs_common.newmail_notify_auto);
1272
1273         return FALSE;
1274 }