cd2a2be63711aa7d2440bdce78efe90f42181c2f
[claws.git] / src / inc.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2003 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 "folder.h"
65 #include "filtering.h"
66 #include "log.h"
67 #include "hooks.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 gint inc_start                   (IncProgressDialog      *inc_dialog);
93 static IncState inc_pop3_session_do     (IncSession             *session);
94
95 static gint inc_recv_data_progressive   (Session        *session,
96                                          guint           cur_len,
97                                          guint           total_len,
98                                          gpointer        data);
99 static gint inc_recv_data_finished      (Session        *session,
100                                          guint           len,
101                                          gpointer        data);
102 static gint inc_recv_message            (Session        *session,
103                                          const gchar    *msg,
104                                          gpointer        data);
105
106 static void inc_put_error               (IncState        istate,
107                                          const gchar    *msg);
108
109 static void inc_cancel_cb               (GtkWidget      *widget,
110                                          gpointer        data);
111 static gint inc_dialog_delete_cb        (GtkWidget      *widget,
112                                          GdkEventAny    *event,
113                                          gpointer        data);
114
115 static gint inc_spool                   (void);
116 static gint get_spool                   (FolderItem     *dest,
117                                          const gchar    *mbox);
118
119 static gint inc_spool_account(PrefsAccount *account);
120 static gint inc_all_spool(void);
121 static void inc_autocheck_timer_set_interval    (guint           interval);
122 static gint inc_autocheck_func                  (gpointer        data);
123
124 static void inc_notify_cmd              (gint new_msgs, 
125                                          gboolean notify);
126
127 #define FOLDER_SUMMARY_MISMATCH(f, s) \
128         (f) && (s) ? ((s)->newmsgs != (f)->new_msgs) || ((f)->unread_msgs != (s)->unread) || ((f)->total_msgs != (s)->messages) \
129         : FALSE
130         
131 /**
132  * inc_finished:
133  * @mainwin: Main window.
134  * @new_messages: TRUE if some messages have been received.
135  * 
136  * Update the folder view and the summary view after receiving
137  * messages.  If @new_messages is FALSE, this function avoids unneeded
138  * updating.
139  **/
140 static void inc_finished(MainWindow *mainwin, gboolean new_messages)
141 {
142         FolderItem *item;
143
144         if (prefs_common.scan_all_after_inc)
145                 folderview_check_new(NULL);
146
147         if (!new_messages && !prefs_common.scan_all_after_inc) return;
148
149         if (prefs_common.open_inbox_on_inc) {
150                 item = cur_account && cur_account->inbox
151                         ? folder_find_item_from_identifier(cur_account->inbox)
152                         : folder_get_default_inbox();
153                 if (FOLDER_SUMMARY_MISMATCH(item, mainwin->summaryview)) {      
154                         folderview_unselect(mainwin->folderview);
155                         folderview_select(mainwin->folderview, item);
156                 }       
157         }
158 }
159
160 void inc_mail(MainWindow *mainwin, gboolean notify)
161 {
162         gint new_msgs = 0;
163         gint account_new_msgs = 0;
164
165         if (inc_lock_count) return;
166
167         if (prefs_common.work_offline)
168                 if (alertpanel(_("Offline warning"), 
169                                _("You're working offline. Override?"),
170                                _("Yes"), _("No"), NULL) != G_ALERTDEFAULT)
171                 return;
172
173         inc_lock();
174         inc_autocheck_timer_remove();
175         main_window_lock(mainwin);
176
177         if (prefs_common.use_extinc && prefs_common.extinc_cmd) {
178                 /* external incorporating program */
179                 if (execute_command_line(prefs_common.extinc_cmd, FALSE) < 0) {
180                         main_window_unlock(mainwin);
181                         inc_autocheck_timer_set();
182                         return;
183                 }
184
185                 if (prefs_common.inc_local) {
186                         account_new_msgs = inc_spool();
187                         if (account_new_msgs > 0)
188                                 new_msgs += account_new_msgs;
189                 }
190         } else {
191                 if (prefs_common.inc_local) {
192                         account_new_msgs = inc_spool();
193                         if (account_new_msgs > 0)
194                                 new_msgs += account_new_msgs;
195                 }
196
197                 account_new_msgs = inc_account_mail(cur_account, mainwin);
198                 if (account_new_msgs > 0)
199                         new_msgs += account_new_msgs;
200         }
201
202         inc_finished(mainwin, new_msgs > 0);
203         main_window_unlock(mainwin);
204         inc_notify_cmd(new_msgs, notify);
205         inc_autocheck_timer_set();
206         inc_unlock();
207 }
208
209 void inc_pop_before_smtp(PrefsAccount *acc)
210 {
211         inc_account_mail(acc, NULL);
212 }
213
214 static gint inc_account_mail(PrefsAccount *account, MainWindow *mainwin)
215 {
216         IncProgressDialog *inc_dialog;
217         IncSession *session;
218         gchar *text[3];
219         FolderItem *item = NULL;
220         
221         if(mainwin && mainwin->summaryview)
222                 item = mainwin->summaryview->folder_item;
223
224         switch (account->protocol) {
225         case A_IMAP4:
226         case A_NNTP:
227                 /* Melvin: bug [14]
228                  * FIXME: it should return foldeview_check_new() value.
229                  * TODO: do it when bug [19] is fixed (IMAP folder sets 
230                  * an incorrect new message count)
231                  */
232                 folderview_check_new(FOLDER(account->folder));
233                 return 0;
234         case A_POP3:
235         case A_APOP:
236                 session = inc_session_new(account);
237                 if (!session) return 0;
238                 
239                 inc_dialog = inc_progress_dialog_create();
240                 inc_dialog->queue_list = g_list_append(inc_dialog->queue_list,
241                                                        session);
242                 inc_dialog->mainwin = mainwin;
243                 session->data = inc_dialog;
244         
245                 text[0] = NULL;
246                 text[1] = account->account_name;
247                 text[2] = _("Standby");
248                 gtk_clist_append(GTK_CLIST(inc_dialog->dialog->clist), text);
249                 
250                 if (mainwin) {
251                         toolbar_main_set_sensitive(mainwin);
252                         main_window_set_menu_sensitive(mainwin);
253                 }
254                         
255                 return inc_start(inc_dialog);
256
257         case A_LOCAL:
258                 return inc_spool_account(account);
259
260         default:
261                 break;
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         gint account_new_msgs = 0;
272         
273         if (prefs_common.work_offline)
274                 if (alertpanel(_("Offline warning"), 
275                                _("You're working offline. Override?"),
276                                _("Yes"), _("No"), NULL) != G_ALERTDEFAULT)
277                 return;
278
279         if (inc_lock_count) return;
280
281         inc_autocheck_timer_remove();
282         main_window_lock(mainwin);
283
284         if (prefs_common.inc_local) {
285                 account_new_msgs = inc_spool();
286                 if (account_new_msgs > 0)
287                         new_msgs += account_new_msgs;   
288         }
289
290         list = account_get_list();
291         if (!list) {
292                 inc_finished(mainwin, new_msgs > 0);
293                 main_window_unlock(mainwin);
294                 inc_notify_cmd(new_msgs, notify);
295                 inc_autocheck_timer_set();
296                 return;
297         }
298
299         /* check local folders */
300         account_new_msgs = inc_all_spool();
301         if (account_new_msgs > 0)
302                 new_msgs += account_new_msgs;
303
304         /* check IMAP4 folders */
305         for (; list != NULL; list = list->next) {
306                 PrefsAccount *account = list->data;
307                 if ((account->protocol == A_IMAP4 ||
308                      account->protocol == A_NNTP) && account->recv_at_getall) {
309                         new_msgs += folderview_check_new(FOLDER(account->folder));
310                 }
311         }
312
313         /* check POP3 accounts */
314         for (list = account_get_list(); list != NULL; list = list->next) {
315                 IncSession *session;
316                 PrefsAccount *account = list->data;
317
318                 if (account->recv_at_getall) {
319                         session = inc_session_new(account);
320                         if (session)
321                                 queue_list = g_list_append(queue_list, session);
322                 }
323         }
324
325         if (!queue_list) {
326                 inc_finished(mainwin, new_msgs > 0);
327                 main_window_unlock(mainwin);
328                 inc_notify_cmd(new_msgs, notify);
329                 inc_autocheck_timer_set();
330                 return;
331         }
332
333         inc_dialog = inc_progress_dialog_create();
334         inc_dialog->queue_list = queue_list;
335         inc_dialog->mainwin = mainwin;
336         for (list = queue_list; list != NULL; list = list->next) {
337                 IncSession *session = list->data;
338                 Pop3Session *pop3_session = POP3_SESSION(session->session);
339                 gchar *text[3];
340
341                 session->data = inc_dialog;
342
343                 text[0] = NULL;
344                 text[1] = pop3_session->ac_prefs->account_name;
345                 text[2] = _("Standby");
346                 gtk_clist_append(GTK_CLIST(inc_dialog->dialog->clist), text);
347         }
348
349         toolbar_main_set_sensitive(mainwin);
350         main_window_set_menu_sensitive(mainwin);
351
352         new_msgs += inc_start(inc_dialog);
353         inc_finished(mainwin, new_msgs > 0);
354         main_window_unlock(mainwin);
355         inc_notify_cmd(new_msgs, notify);
356         inc_autocheck_timer_set();
357 }
358
359 static IncProgressDialog *inc_progress_dialog_create(void)
360 {
361         IncProgressDialog *dialog;
362         ProgressDialog *progress;
363
364         dialog = g_new0(IncProgressDialog, 1);
365
366         progress = progress_dialog_create();
367         gtk_window_set_title(GTK_WINDOW(progress->window),
368                              _("Retrieving new messages"));
369         gtk_signal_connect(GTK_OBJECT(progress->cancel_btn), "clicked",
370                            GTK_SIGNAL_FUNC(inc_cancel_cb), dialog);
371         gtk_signal_connect(GTK_OBJECT(progress->window), "delete_event",
372                            GTK_SIGNAL_FUNC(inc_dialog_delete_cb), dialog);
373         /* manage_window_set_transient(GTK_WINDOW(progress->window)); */
374
375         progress_dialog_set_value(progress, 0.0);
376
377         stock_pixmap_gdk(progress->clist, STOCK_PIXMAP_COMPLETE,
378                          &okxpm, &okxpmmask);
379         stock_pixmap_gdk(progress->clist, STOCK_PIXMAP_CONTINUE,
380                          &currentxpm, &currentxpmmask);
381         stock_pixmap_gdk(progress->clist, STOCK_PIXMAP_ERROR,
382                          &errorxpm, &errorxpmmask);
383
384         if (prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS ||
385             (prefs_common.recv_dialog_mode == RECV_DIALOG_ACTIVE &&
386              manage_window_get_focus_window())) {
387                 dialog->show_dialog = TRUE;
388                 gtk_widget_show_now(progress->window);
389         }
390
391         dialog->dialog = progress;
392         dialog->queue_list = NULL;
393
394         inc_dialog_list = g_list_append(inc_dialog_list, dialog);
395
396         return dialog;
397 }
398
399 static void inc_progress_dialog_clear(IncProgressDialog *inc_dialog)
400 {
401         progress_dialog_set_value(inc_dialog->dialog, 0.0);
402         progress_dialog_set_label(inc_dialog->dialog, "");
403         if (inc_dialog->mainwin)
404                 gtk_progress_bar_update
405                         (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar), 0.0);
406 }
407
408 static void inc_progress_dialog_destroy(IncProgressDialog *inc_dialog)
409 {
410         g_return_if_fail(inc_dialog != NULL);
411
412         inc_dialog_list = g_list_remove(inc_dialog_list, inc_dialog);
413         if (inc_dialog->mainwin)
414                 gtk_progress_bar_update
415                         (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar), 0.0);
416         progress_dialog_destroy(inc_dialog->dialog);
417
418         g_free(inc_dialog);
419 }
420
421 static IncSession *inc_session_new(PrefsAccount *account)
422 {
423         IncSession *session;
424
425         g_return_val_if_fail(account != NULL, NULL);
426
427         if (account->protocol != A_POP3 && account->protocol != A_APOP)
428                 return NULL;
429         if (!account->recv_server || !account->userid)
430                 return NULL;
431
432         session = g_new0(IncSession, 1);
433         session->session = pop3_session_new(account);
434         session->session->data = session;
435
436         return session;
437 }
438
439 static void inc_session_destroy(IncSession *session)
440 {
441         g_return_if_fail(session != NULL);
442
443         session_destroy(session->session);
444         g_free(session);
445 }
446
447 static gint inc_start(IncProgressDialog *inc_dialog)
448 {
449         IncSession *session;
450         GtkCList *clist = GTK_CLIST(inc_dialog->dialog->clist);
451         Pop3Session *pop3_session;
452         IncState inc_state;
453         gint num = 0;
454         gint error_num = 0;
455         gint new_msgs = 0;
456         gchar *msg;
457         gchar *fin_msg;
458         FolderItem *processing, *inbox;
459         MsgInfo *msginfo;
460         GSList *msglist, *msglist_element;
461
462         while (inc_dialog->queue_list != NULL) {
463                 session = inc_dialog->queue_list->data;
464                 pop3_session = POP3_SESSION(session->session);
465
466                 inc_progress_dialog_clear(inc_dialog);
467
468                 gtk_clist_moveto(clist, num, -1, 1.0, 0.0);
469
470                 pop3_session->user = g_strdup(pop3_session->ac_prefs->userid);
471                 if (pop3_session->ac_prefs->passwd)
472                         pop3_session->pass =
473                                 g_strdup(pop3_session->ac_prefs->passwd);
474                 else if (pop3_session->ac_prefs->tmp_pass)
475                         pop3_session->pass =
476                                 g_strdup(pop3_session->ac_prefs->tmp_pass);
477                 else {
478                         gchar *pass;
479
480                         pass = input_dialog_query_password
481                                 (pop3_session->ac_prefs->recv_server,
482                                  pop3_session->user);
483
484                         if (inc_dialog->mainwin && inc_dialog->show_dialog)
485                                 manage_window_focus_in
486                                         (inc_dialog->mainwin->window,
487                                          NULL, NULL);
488                         if (pass) {
489                                 pop3_session->ac_prefs->tmp_pass =
490                                         g_strdup(pass);
491                                 pop3_session->pass = pass;
492                         } else {
493                                 inc_session_destroy(session);
494                                 inc_dialog->queue_list = g_list_remove
495                                         (inc_dialog->queue_list, session);
496                                 continue;
497                         }
498                 }
499
500                 gtk_clist_set_pixmap(clist, num, 0, currentxpm, currentxpmmask);
501                 gtk_clist_set_text(clist, num, 2, _("Retrieving"));
502
503                 session_set_recv_message_notify(session->session,
504                                                 inc_recv_message, session);
505                 session_set_recv_data_progressive_notify
506                         (session->session, inc_recv_data_progressive, session);
507                 session_set_recv_data_notify(session->session,
508                                              inc_recv_data_finished, session);
509
510                 /* begin POP3 session */
511                 inc_state = inc_pop3_session_do(session);
512
513                 switch (inc_state) {
514                 case INC_SUCCESS:
515                         if (pop3_session->cur_total_num > 0)
516                                 msg = g_strdup_printf
517                                         (_("Done (%d message(s) (%s) received)"),
518                                          pop3_session->cur_total_num,
519                                          to_human_readable(pop3_session->cur_total_recv_bytes));
520                         else
521                                 msg = g_strdup_printf(_("Done (no new messages)"));
522                         gtk_clist_set_pixmap(clist, num, 0, okxpm, okxpmmask);
523                         gtk_clist_set_text(clist, num, 2, msg);
524                         g_free(msg);
525                         break;
526                 case INC_CONNECT_ERROR:
527                         gtk_clist_set_pixmap(clist, num, 0, errorxpm, errorxpmmask);
528                         gtk_clist_set_text(clist, num, 2, _("Connection failed"));
529                         break;
530                 case INC_AUTH_FAILED:
531                         gtk_clist_set_pixmap(clist, num, 0, errorxpm, errorxpmmask);
532                         gtk_clist_set_text(clist, num, 2, _("Auth failed"));
533                         break;
534                 case INC_LOCKED:
535                         gtk_clist_set_pixmap(clist, num, 0, errorxpm, errorxpmmask);
536                         gtk_clist_set_text(clist, num, 2, _("Locked"));
537                         break;
538                 case INC_ERROR:
539                 case INC_NO_SPACE:
540                 case INC_IO_ERROR:
541                 case INC_SOCKET_ERROR:
542                         gtk_clist_set_pixmap(clist, num, 0, errorxpm, errorxpmmask);
543                         gtk_clist_set_text(clist, num, 2, _("Error"));
544                         break;
545                 case INC_CANCEL:
546                         gtk_clist_set_pixmap(clist, num, 0, okxpm, okxpmmask);
547                         gtk_clist_set_text(clist, num, 2, _("Cancelled"));
548                         break;
549                 default:
550                         break;
551                 }
552                 
553                 if (pop3_session->error_val == PS_AUTHFAIL) {
554                         if(!prefs_common.no_recv_err_panel) {
555                                 if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
556                                     ((prefs_common.recv_dialog_mode == RECV_DIALOG_ACTIVE) && focus_window)) {
557                                         manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
558                                 }
559                                 alertpanel_error
560                                         (_("Authorization for %s on %s failed"),
561                                          pop3_session->user,
562                                          pop3_session->ac_prefs->recv_server);
563                         }
564                 }
565
566                 /* CLAWS: perform filtering actions on dropped message */
567                 /* CLAWS: get default inbox (perhaps per account) */
568                 if (pop3_session->ac_prefs->inbox) {
569                         /* CLAWS: get destination folder / mailbox */
570                         inbox = folder_find_item_from_identifier(pop3_session->ac_prefs->inbox);
571                         if (!inbox)
572                                 inbox = folder_get_default_inbox();
573                 } else
574                         inbox = folder_get_default_inbox();
575
576                 /* get list of messages in processing */
577                 processing = folder_get_default_processing();
578                 folder_item_scan(processing);
579                 msglist = folder_item_get_msg_list(processing);
580
581                 folder_item_update_freeze();
582
583                 /* process messages */
584                 for(msglist_element = msglist; msglist_element != NULL; msglist_element = msglist_element->next) {
585                         msginfo = (MsgInfo *) msglist_element->data;
586                         if (!pop3_session->ac_prefs->filter_on_recv || !procmsg_msginfo_filter(msginfo))
587                                 folder_item_move_msg(inbox, msginfo);
588                         procmsg_msginfo_free(msginfo);
589                 }
590                 g_slist_free(msglist);
591
592                 folder_item_update_thaw();
593
594
595                 new_msgs += pop3_session->cur_total_num;
596
597                 if (pop3_session->error_val == PS_AUTHFAIL &&
598                     pop3_session->ac_prefs->tmp_pass) {
599                         g_free(pop3_session->ac_prefs->tmp_pass);
600                         pop3_session->ac_prefs->tmp_pass = NULL;
601                 }
602
603                 pop3_write_uidl_list(pop3_session);
604
605                 if (inc_state != INC_SUCCESS && inc_state != INC_CANCEL) {
606                         error_num++;
607                         if (inc_dialog->show_dialog)
608                                 manage_window_focus_in
609                                         (inc_dialog->dialog->window,
610                                          NULL, NULL);
611                         inc_put_error(inc_state, pop3_session->error_msg);
612                         if (inc_dialog->show_dialog)
613                                 manage_window_focus_out
614                                         (inc_dialog->dialog->window,
615                                          NULL, NULL);
616                         if (inc_state == INC_NO_SPACE ||
617                             inc_state == INC_IO_ERROR)
618                                 break;
619                 }
620
621                 inc_session_destroy(session);
622                 inc_dialog->queue_list =
623                         g_list_remove(inc_dialog->queue_list, session);
624
625                 num++;
626         }
627
628         if (new_msgs > 0)
629                 fin_msg = g_strdup_printf(_("Finished (%d new message(s))"),
630                                           new_msgs);
631         else
632                 fin_msg = g_strdup_printf(_("Finished (no new messages)"));
633
634         progress_dialog_set_label(inc_dialog->dialog, fin_msg);
635
636         if (error_num && !prefs_common.no_recv_err_panel) {
637                 if (inc_dialog->show_dialog)
638                         manage_window_focus_in(inc_dialog->dialog->window,
639                                                NULL, NULL);
640                 alertpanel_error_log(_("Some errors occurred while getting mail."));
641                 if (inc_dialog->show_dialog)
642                         manage_window_focus_out(inc_dialog->dialog->window,
643                                                 NULL, NULL);
644         }
645
646         while (inc_dialog->queue_list != NULL) {
647                 session = inc_dialog->queue_list->data;
648                 inc_session_destroy(session);
649                 inc_dialog->queue_list =
650                         g_list_remove(inc_dialog->queue_list, session);
651         }
652
653         if (prefs_common.close_recv_dialog)
654                 inc_progress_dialog_destroy(inc_dialog);
655         else {
656                 gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window),
657                                      fin_msg);
658                 gtk_label_set_text(GTK_LABEL(GTK_BIN(inc_dialog->dialog->cancel_btn)->child),
659                                    _("Close"));
660         }
661
662         g_free(fin_msg);
663
664         return new_msgs;
665 }
666
667 static IncState inc_pop3_session_do(IncSession *session)
668 {
669         Pop3Session *pop3_session = POP3_SESSION(session->session);
670         IncProgressDialog *inc_dialog = (IncProgressDialog *)session->data;
671         gchar *server;
672         gushort port;
673         gchar *buf;
674
675         debug_print("getting new messages of account %s...\n",
676                     pop3_session->ac_prefs->account_name);
677
678         buf = g_strdup_printf(_("%s: Retrieving new messages"),
679                               pop3_session->ac_prefs->recv_server);
680         gtk_window_set_title(GTK_WINDOW(inc_dialog->dialog->window), buf);
681         g_free(buf);
682
683         server = pop3_session->ac_prefs->recv_server;
684 #if USE_OPENSSL
685         port = pop3_session->ac_prefs->set_popport ?
686                 pop3_session->ac_prefs->popport :
687                 pop3_session->ac_prefs->ssl_pop == SSL_TUNNEL ? 995 : 110;
688         SESSION(pop3_session)->ssl_type = pop3_session->ac_prefs->ssl_pop;
689 #else
690         port = pop3_session->ac_prefs->set_popport ?
691                 pop3_session->ac_prefs->popport : 110;
692 #endif
693
694         statusbar_verbosity_set(TRUE);
695         buf = g_strdup_printf(_("Connecting to POP3 server: %s ..."), server);
696         log_message("%s\n", buf);
697
698         progress_dialog_set_label(inc_dialog->dialog, buf);
699         g_free(buf);
700         GTK_EVENTS_FLUSH();
701
702         statusbar_verbosity_set(FALSE);
703
704         if (session_connect(SESSION(pop3_session), server, port) < 0) {
705                 log_warning(_("Can't connect to POP3 server: %s:%d\n"),
706                             server, port);
707                 if(!prefs_common.no_recv_err_panel) {
708                         if((prefs_common.recv_dialog_mode == RECV_DIALOG_ALWAYS) ||
709                             ((prefs_common.recv_dialog_mode == RECV_DIALOG_ACTIVE) && focus_window)) {
710                                 manage_window_focus_in(inc_dialog->dialog->window, NULL, NULL);
711                         }
712                         alertpanel_error(_("Can't connect to POP3 server: %s:%d"),
713                                          server, port);
714                         manage_window_focus_out(inc_dialog->dialog->window, NULL, NULL);
715                 }
716                 session->inc_state = INC_CONNECT_ERROR;
717                 return INC_CONNECT_ERROR;
718         }
719
720         statusbar_verbosity_set(TRUE);
721
722         while (SESSION(pop3_session)->state != SESSION_DISCONNECTED &&
723                SESSION(pop3_session)->state != SESSION_ERROR)
724                 gtk_main_iteration();
725
726
727         statusbar_verbosity_set(FALSE);
728         if (session->inc_state == INC_SUCCESS) {
729                 switch (pop3_session->error_val) {
730                 case PS_SUCCESS:
731                         if (SESSION(pop3_session)->state == SESSION_ERROR) {
732                                 if (pop3_session->state == POP3_READY)
733                                         session->inc_state = INC_CONNECT_ERROR;
734                                 else
735                                         session->inc_state = INC_ERROR;
736                         } else
737                                 session->inc_state = INC_SUCCESS;
738                         break;
739                 case PS_AUTHFAIL:
740                         session->inc_state = INC_AUTH_FAILED;
741                         break;
742                 case PS_IOERR:
743                         session->inc_state = INC_IO_ERROR;
744                         break;
745                 case PS_SOCKET:
746                         session->inc_state = INC_SOCKET_ERROR;
747                         break;
748                 case PS_LOCKBUSY:
749                         session->inc_state = INC_LOCKED;
750                         break;
751                 default:
752                         session->inc_state = INC_ERROR;
753                         break;
754                 }
755         }
756
757         return session->inc_state;
758 }
759
760 static gint inc_recv_data_progressive(Session *session, guint cur_len,
761                                       guint total_len, gpointer data)
762 {
763         gchar buf[MSGBUFSIZE];
764         IncSession *inc_session = (IncSession *)data;
765         Pop3Session *pop3_session = POP3_SESSION(session);
766         IncProgressDialog *inc_dialog;
767         ProgressDialog *dialog;
768         gint cur_total;
769         gchar *total_size;
770
771         g_return_val_if_fail(inc_session != NULL, -1);
772
773         inc_dialog = (IncProgressDialog *)inc_session->data;
774         dialog = inc_dialog->dialog;
775
776         cur_total = pop3_session->cur_total_bytes + cur_len;
777         if (cur_total > pop3_session->total_bytes)
778                 cur_total = pop3_session->total_bytes;
779
780         Xstrdup_a(total_size, to_human_readable(pop3_session->total_bytes),
781                   return FALSE);
782         g_snprintf(buf, sizeof(buf),
783                    _("Retrieving message (%d / %d) (%s / %s)"),
784                    pop3_session->cur_msg, pop3_session->count,
785                    to_human_readable(cur_total), total_size);
786         progress_dialog_set_label(dialog, buf);
787         progress_dialog_set_percentage
788                 (dialog, (gfloat)cur_total / (gfloat)pop3_session->total_bytes);
789         if (inc_dialog->mainwin)
790                 gtk_progress_bar_update
791                         (GTK_PROGRESS_BAR(inc_dialog->mainwin->progressbar),
792                          (gfloat)cur_total / (gfloat)pop3_session->total_bytes);
793         GTK_EVENTS_FLUSH();
794
795         return 0;
796 }
797
798 static gint inc_recv_data_finished(Session *session, guint len, gpointer data)
799 {
800         IncSession *inc_session = (IncSession *)data;
801
802         g_return_val_if_fail(inc_session != NULL, -1);
803
804         inc_recv_data_progressive(session, 0, len, inc_session);
805         return 0;
806 }
807
808 static gint inc_recv_message(Session *session, const gchar *msg, gpointer data)
809 {
810         gchar buf[MSGBUFSIZE];
811         IncSession *inc_session = (IncSession *)data;
812         Pop3Session *pop3_session = POP3_SESSION(session);
813         IncProgressDialog *inc_dialog;
814         ProgressDialog *dialog;
815
816         g_return_val_if_fail(inc_session != NULL, -1);
817
818         inc_dialog = (IncProgressDialog *)inc_session->data;
819         dialog = inc_dialog->dialog;
820
821         switch (pop3_session->state) {
822         case POP3_GREETING:
823                 break;
824         case POP3_GETAUTH_USER:
825         case POP3_GETAUTH_PASS:
826         case POP3_GETAUTH_APOP:
827                 progress_dialog_set_label(dialog, _("Authenticating..."));
828                 break;
829         case POP3_GETRANGE_STAT:
830                 progress_dialog_set_label
831                         (dialog, _("Getting the number of new messages (STAT)..."));
832                 break;
833         case POP3_GETRANGE_LAST:
834                 progress_dialog_set_label
835                         (dialog, _("Getting the number of new messages (LAST)..."));
836                 break;
837         case POP3_GETRANGE_UIDL:
838                 progress_dialog_set_label
839                         (dialog, _("Getting the number of new messages (UIDL)..."));
840                 break;
841         case POP3_GETSIZE_LIST:
842                 progress_dialog_set_label
843                         (dialog, _("Getting the size of messages (LIST)..."));
844                 break;
845         case POP3_RETR:
846                 inc_recv_data_progressive
847                         (session, 0,
848                          pop3_session->msg[pop3_session->cur_msg].size, data);
849                 break;
850         case POP3_DELETE:
851                 if (pop3_session->msg[pop3_session->cur_msg].recv_time <
852                         pop3_session->current_time) {
853                         g_snprintf(buf, sizeof(buf), _("Deleting message %d"),
854                                    pop3_session->cur_msg);
855                         progress_dialog_set_label(dialog, buf);
856                 }
857                 break;
858         case POP3_LOGOUT:
859                 progress_dialog_set_label(dialog, _("Quitting"));
860                 break;
861         default:
862                 break;
863         }
864
865         return 0;
866 }
867
868 gint inc_drop_message(const gchar *file, Pop3Session *session)
869 {
870         FolderItem *inbox;
871         FolderItem *dropfolder;
872         gint msgnum;
873         IncSession *inc_session = (IncSession *)(SESSION(session)->data);
874         gint val;
875
876         if (session->ac_prefs->inbox) {
877                 inbox = folder_find_item_from_identifier
878                         (session->ac_prefs->inbox);
879                 if (!inbox)
880                         inbox = folder_get_default_inbox();
881         } else
882                 inbox = folder_get_default_inbox();
883         if (!inbox) {
884                 unlink(file);
885                 return -1;
886         }
887
888         /* CLAWS: claws uses a global .processing folder for the filtering. */
889         dropfolder = folder_get_default_processing();
890
891         /* add msg file to drop folder */
892         if ((msgnum = folder_item_add_msg(dropfolder, file, TRUE)) < 0) {
893                 unlink(file);
894                 return -1;
895         }
896
897         return 0;
898 }
899
900 static void inc_put_error(IncState istate, const gchar *msg)
901 {
902         switch (istate) {
903         case INC_ERROR:
904                 if (prefs_common.no_recv_err_panel)
905                         break;
906                 if (msg)
907                         alertpanel_error(_("Error occurred while processing mail:\n%s"), msg);
908                 else
909                         alertpanel_error(_("Error occurred while processing mail."));
910                 break;
911         case INC_NO_SPACE:
912                 alertpanel_error(_("No disk space left."));
913                 break;
914         case INC_IO_ERROR:
915                 alertpanel_error(_("Can't write file."));
916                 break;
917         case INC_SOCKET_ERROR:
918                 if (prefs_common.no_recv_err_panel)
919                         break;
920                 alertpanel_error(_("Socket error."));
921                 break;
922         case INC_LOCKED:
923                 if (prefs_common.no_recv_err_panel)
924                         break;
925                 if (msg)
926                         alertpanel_error(_("Mailbox is locked:\n%s"), msg);
927                 else
928                         alertpanel_error(_("Mailbox is locked."));
929                 break;
930         case INC_AUTH_FAILED:
931                 if (prefs_common.no_recv_err_panel)
932                         break;
933                 if (msg)
934                         alertpanel_error(_("Authentication failed:\n%s"),
935                                          msg);
936                 else
937                         alertpanel_error(_("Authentication failed."));
938                 break;
939         default:
940                 break;
941         }
942 }
943
944 static void inc_cancel(IncProgressDialog *dialog)
945 {
946         IncSession *session;
947
948         g_return_if_fail(dialog != NULL);
949
950         if (dialog->queue_list == NULL) {
951                 inc_progress_dialog_destroy(dialog);
952                 return;
953         }
954
955         session = dialog->queue_list->data;
956
957         session->session->state = SESSION_DISCONNECTED;
958         session->inc_state = INC_CANCEL;
959
960         log_message(_("Incorporation cancelled\n"));
961 }
962
963 gboolean inc_is_active(void)
964 {
965         return (inc_dialog_list != NULL);
966 }
967
968 void inc_cancel_all(void)
969 {
970         GList *cur;
971
972         for (cur = inc_dialog_list; cur != NULL; cur = cur->next)
973                 inc_cancel((IncProgressDialog *)cur->data);
974 }
975
976 static void inc_cancel_cb(GtkWidget *widget, gpointer data)
977 {
978         inc_cancel((IncProgressDialog *)data);
979 }
980
981 static gint inc_dialog_delete_cb(GtkWidget *widget, GdkEventAny *event,
982                                  gpointer data)
983 {
984         IncProgressDialog *dialog = (IncProgressDialog *)data;
985
986         if (dialog->queue_list == NULL)
987                 inc_progress_dialog_destroy(dialog);
988
989         return TRUE;
990 }
991
992 static gint inc_spool(void)
993 {
994         gchar *mbox, *logname;
995         gint msgs;
996
997         logname = g_get_user_name();
998         mbox = g_strconcat(prefs_common.spool_path
999                            ? prefs_common.spool_path : DEFAULT_SPOOL_PATH,
1000                            G_DIR_SEPARATOR_S, logname, NULL);
1001         msgs = get_spool(folder_get_default_inbox(), mbox);
1002         g_free(mbox);
1003
1004         return msgs;
1005 }
1006
1007 static gint inc_spool_account(PrefsAccount *account)
1008 {
1009         FolderItem *inbox;
1010
1011         if (account->inbox) {
1012                 inbox = folder_find_item_from_path(account->inbox);
1013                 if (!inbox)
1014                         inbox = folder_get_default_inbox();
1015         } else
1016                 inbox = folder_get_default_inbox();
1017
1018         return get_spool(inbox, account->local_mbox);
1019 }
1020
1021 static gint inc_all_spool(void)
1022 {
1023         GList *list = NULL;
1024         gint new_msgs = 0;
1025         gint account_new_msgs = 0;
1026
1027         list = account_get_list();
1028         if (!list) return 0;
1029
1030         for (; list != NULL; list = list->next) {
1031                 PrefsAccount *account = list->data;
1032
1033                 if ((account->protocol == A_LOCAL) &&
1034                     (account->recv_at_getall)) {
1035                         account_new_msgs = inc_spool_account(account);
1036                         if (account_new_msgs > 0)
1037                                 new_msgs += account_new_msgs;
1038                 }
1039         }
1040
1041         return new_msgs;
1042 }
1043
1044 static gint get_spool(FolderItem *dest, const gchar *mbox)
1045 {
1046         gint msgs, size;
1047         gint lockfd;
1048         gchar tmp_mbox[MAXPATHLEN + 1];
1049
1050         g_return_val_if_fail(dest != NULL, -1);
1051         g_return_val_if_fail(mbox != NULL, -1);
1052
1053         if (!is_file_exist(mbox) || (size = get_file_size(mbox)) == 0) {
1054                 debug_print("no messages in local mailbox.\n");
1055                 return 0;
1056         } else if (size < 0)
1057                 return -1;
1058
1059         if ((lockfd = lock_mbox(mbox, LOCK_FLOCK)) < 0)
1060                 return -1;
1061
1062         g_snprintf(tmp_mbox, sizeof(tmp_mbox), "%s%ctmpmbox.%08x",
1063                    get_tmp_dir(), G_DIR_SEPARATOR, (gint)mbox);
1064
1065         if (copy_mbox(mbox, tmp_mbox) < 0) {
1066                 unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1067                 return -1;
1068         }
1069
1070         debug_print("Getting new messages from %s into %s...\n",
1071                     mbox, dest->path);
1072
1073         msgs = proc_mbox(dest, tmp_mbox);
1074
1075         unlink(tmp_mbox);
1076         if (msgs >= 0) empty_mbox(mbox);
1077         unlock_mbox(mbox, lockfd, LOCK_FLOCK);
1078
1079         return msgs;
1080 }
1081
1082 void inc_lock(void)
1083 {
1084         inc_lock_count++;
1085 }
1086
1087 void inc_unlock(void)
1088 {
1089         if (inc_lock_count > 0)
1090                 inc_lock_count--;
1091 }
1092
1093 static guint autocheck_timer = 0;
1094 static gpointer autocheck_data = NULL;
1095
1096 static void inc_notify_cmd(gint new_msgs, gboolean notify)
1097 {
1098
1099         gchar *buf;
1100
1101         if (!(new_msgs && notify && prefs_common.newmail_notify_cmd &&
1102             *prefs_common.newmail_notify_cmd))
1103                      return;
1104         if ((buf = strchr(prefs_common.newmail_notify_cmd, '%')) &&
1105                 buf[1] == 'd' && !strchr(&buf[1], '%'))
1106                 buf = g_strdup_printf(prefs_common.newmail_notify_cmd, 
1107                                       new_msgs);
1108         else
1109                 buf = g_strdup(prefs_common.newmail_notify_cmd);
1110
1111         execute_command_line(buf, TRUE);
1112
1113         g_free(buf);
1114 }
1115  
1116 void inc_autocheck_timer_init(MainWindow *mainwin)
1117 {
1118         autocheck_data = mainwin;
1119         inc_autocheck_timer_set();
1120 }
1121
1122 static void inc_autocheck_timer_set_interval(guint interval)
1123 {
1124         inc_autocheck_timer_remove();
1125         /* last test is to avoid re-enabling auto_check after modifying 
1126            the common preferences */
1127         if (prefs_common.autochk_newmail && autocheck_data
1128             && prefs_common.work_offline == FALSE) {
1129                 autocheck_timer = gtk_timeout_add
1130                         (interval, inc_autocheck_func, autocheck_data);
1131                 debug_print("added timer = %d\n", autocheck_timer);
1132         }
1133 }
1134
1135 void inc_autocheck_timer_set(void)
1136 {
1137         inc_autocheck_timer_set_interval(prefs_common.autochk_itv * 60000);
1138 }
1139
1140 void inc_autocheck_timer_remove(void)
1141 {
1142         if (autocheck_timer) {
1143                 debug_print("removed timer = %d\n", autocheck_timer);
1144                 gtk_timeout_remove(autocheck_timer);
1145                 autocheck_timer = 0;
1146         }
1147 }
1148
1149 static gint inc_autocheck_func(gpointer data)
1150 {
1151         MainWindow *mainwin = (MainWindow *)data;
1152
1153         if (inc_lock_count) {
1154                 debug_print("autocheck is locked.\n");
1155                 inc_autocheck_timer_set_interval(1000);
1156                 return FALSE;
1157         }
1158
1159         inc_all_account_mail(mainwin, prefs_common.newmail_notify_auto);
1160
1161         return FALSE;
1162 }