sync with 0.8.1cvs21
[claws.git] / src / main.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/gtkrc.h>
29
30 #if HAVE_GDK_IMLIB
31 #  include <gdk_imlib.h>
32 #endif
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <ctype.h>
38 #include <unistd.h>
39 #include <time.h>
40 #include <sys/stat.h>
41 #include <sys/types.h>
42 #include <signal.h>
43
44 #if HAVE_LOCALE_H
45 #  include <locale.h>
46 #endif
47
48 #if USE_GPGME
49 #  include <gpgme.h>
50 #  include "passphrase.h"
51 #endif
52
53 #include "intl.h"
54 #include "main.h"
55 #include "mainwindow.h"
56 #include "folderview.h"
57 #include "summaryview.h"
58 #include "prefs_common.h"
59 #include "prefs_filter.h"
60 #include "prefs_account.h"
61 #include "prefs_actions.h"
62 #include "scoring.h"
63 #include "prefs_display_header.h"
64 #include "account.h"
65 #include "procmsg.h"
66 #include "inc.h"
67 #include "import.h"
68 #include "manage_window.h"
69 #include "alertpanel.h"
70 #include "statusbar.h"
71 #include "addressbook.h"
72 #include "compose.h"
73 #include "folder.h"
74 #include "setup.h"
75 #include "utils.h"
76 #include "gtkutils.h"
77
78 #if USE_GPGME
79 #  include "rfc2015.h"
80 #endif
81 #if USE_SSL
82 #  include "ssl.h"
83 #endif
84
85 #include "version.h"
86
87 gchar *prog_version;
88 gchar *startup_dir;
89 gboolean debug_mode = FALSE;
90
91 static gint lock_socket = -1;
92 static gint lock_socket_tag = 0;
93
94 static struct Cmd {
95         gboolean receive;
96         gboolean receive_all;
97         gboolean compose;
98         const gchar *compose_mailto;
99         GPtrArray *attach_files;
100         gboolean status;
101         gboolean send;
102 } cmd;
103
104 static void parse_cmd_opt(int argc, char *argv[]);
105
106 #if USE_GPGME
107 static void idle_function_for_gpgme(void);
108 #endif /* USE_GPGME */
109
110 static gint prohibit_duplicate_launch   (void);
111 static void lock_socket_input_cb        (gpointer          data,
112                                          gint              source,
113                                          GdkInputCondition condition);
114 static gchar *get_socket_name           (void);
115
116 static void open_compose_new            (const gchar    *address,
117                                          GPtrArray      *attach_files);
118
119 static void send_queue                  (void);
120 static void initial_processing          (FolderItem *item, gpointer data);
121
122 #if 0
123 /* for gettext */
124 _("File `%s' already exists.\n"
125   "Can't create folder.")
126 #endif
127
128 #define MAKE_DIR_IF_NOT_EXIST(dir) \
129 { \
130         if (!is_dir_exist(dir)) { \
131                 if (is_file_exist(dir)) { \
132                         alertpanel_warning \
133                                 (_("File `%s' already exists.\n" \
134                                    "Can't create folder."), \
135                                  dir); \
136                         return 1; \
137                 } \
138                 if (make_dir(dir) < 0) \
139                         return 1; \
140         } \
141 }
142
143 int main(int argc, char *argv[])
144 {
145         gchar *userrc;
146         MainWindow *mainwin;
147         FolderView *folderview;
148
149         setlocale(LC_ALL, "");
150         bindtextdomain(PACKAGE, LOCALEDIR);
151         textdomain(PACKAGE);
152
153         prog_version = PROG_VERSION;
154         startup_dir = g_get_current_dir();
155
156         parse_cmd_opt(argc, argv);
157
158         gtk_set_locale();
159         gtk_init(&argc, &argv);
160
161 #if USE_THREADS || USE_LDAP
162         g_thread_init(NULL);
163         if (!g_thread_supported())
164                 g_error(_("g_thread is not supported by glib.\n"));
165 #endif
166
167 #if HAVE_GDK_IMLIB
168         gdk_imlib_init();
169         gtk_widget_push_visual(gdk_imlib_get_visual());
170         gtk_widget_push_colormap(gdk_imlib_get_colormap());
171 #endif
172
173 #if USE_SSL
174         ssl_init();
175 #endif
176
177         srandom((gint)time(NULL));
178
179         /* parse gtkrc files */
180         userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtkrc",
181                              NULL);
182         gtk_rc_parse(userrc);
183         g_free(userrc);
184         userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtk",
185                              G_DIR_SEPARATOR_S, "gtkrc", NULL);
186         gtk_rc_parse(userrc);
187         g_free(userrc);
188         userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "gtkrc", NULL);
189         gtk_rc_parse(userrc);
190         g_free(userrc);
191
192         gtk_rc_parse("./gtkrc");
193
194         userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
195         gtk_item_factory_parse_rc(userrc);
196         g_free(userrc);
197
198         CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
199
200         /* check and create unix domain socket */
201         lock_socket = prohibit_duplicate_launch();
202         if (lock_socket < 0) return 0;
203
204         if (cmd.status) {
205                 puts("0 Sylpheed not running.");
206                 return 0;
207         }
208
209         /* backup if old rc file exists */
210         if (is_file_exist(RC_DIR)) {
211                 if (rename(RC_DIR, RC_DIR ".bak") < 0)
212                         FILE_OP_ERROR(RC_DIR, "rename");
213         }
214         MAKE_DIR_IF_NOT_EXIST(RC_DIR);
215         MAKE_DIR_IF_NOT_EXIST(get_imap_cache_dir());
216         MAKE_DIR_IF_NOT_EXIST(get_news_cache_dir());
217         MAKE_DIR_IF_NOT_EXIST(get_mime_tmp_dir());
218         MAKE_DIR_IF_NOT_EXIST(get_tmp_dir());
219         MAKE_DIR_IF_NOT_EXIST(RC_DIR G_DIR_SEPARATOR_S "uidl");
220
221         if (is_file_exist(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log")) {
222                 if (rename(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log",
223                            RC_DIR G_DIR_SEPARATOR_S "sylpheed.log.bak") < 0)
224                         FILE_OP_ERROR("sylpheed.log", "rename");
225         }
226         set_log_file(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log");
227
228         if (is_file_exist(RC_DIR G_DIR_SEPARATOR_S "assortrc") &&
229             !is_file_exist(RC_DIR G_DIR_SEPARATOR_S "filterrc")) {
230                 if (rename(RC_DIR G_DIR_SEPARATOR_S "assortrc",
231                            RC_DIR G_DIR_SEPARATOR_S "filterrc") < 0)
232                         FILE_OP_ERROR(RC_DIR G_DIR_SEPARATOR_S "assortrc",
233                                       "rename");
234         }
235
236         prefs_common_init();
237         prefs_common_read_config();
238
239 #if USE_GPGME
240         if (gpgme_check_engine()) {  /* Also does some gpgme init */
241                 rfc2015_disable_all();
242                 debug_print("gpgme_engine_version:\n%s\n",
243                             gpgme_get_engine_info());
244
245                 if (prefs_common.gpg_warning) {
246                         AlertValue val;
247
248                         val = alertpanel_message_with_disable
249                                 (_("Warning"),
250                                  _("GnuPG is not installed properly.\n"
251                                    "OpenPGP support disabled."));
252                         if (val & G_ALERTDISABLE)
253                                 prefs_common.gpg_warning = FALSE;
254                 }
255         }
256         gpgme_register_idle(idle_function_for_gpgme);
257 #endif
258
259 #if USE_PSPELL
260         gtkpspellcheckers = gtkpspell_checkers_new();
261 #endif
262         
263
264         prefs_common_save_config();
265         prefs_filter_read_config();
266         prefs_filter_write_config();
267         prefs_actions_read_config();
268         prefs_actions_write_config();
269         prefs_display_header_read_config();
270         prefs_display_header_write_config();
271         /* prefs_filtering_read_config(); */
272         addressbook_read_file();
273         renderer_read_config();
274
275         gtkut_widget_init();
276
277         mainwin = main_window_create
278                 (prefs_common.sep_folder | prefs_common.sep_msg << 1);
279         folderview = mainwin->folderview;
280
281         /* register the callback of unix domain socket input */
282         lock_socket_tag = gdk_input_add(lock_socket,
283                                         GDK_INPUT_READ | GDK_INPUT_EXCEPTION,
284                                         lock_socket_input_cb,
285                                         mainwin);
286
287         account_read_config_all();
288         account_save_config_all();
289
290         if (folder_read_list() < 0) {
291                 setup(mainwin);
292                 folder_write_list();
293         }
294         if (!account_get_list()) {
295                 account_edit_open();
296                 account_add();
297         }
298
299         account_set_missing_folder();
300         folder_set_missing_folders();
301         folderview_set(folderview);
302
303         /* prefs_scoring_read_config(); */
304         prefs_matcher_read_config();
305
306         /* make one all-folder processing before using sylpheed */
307         folder_func_to_all_folders(initial_processing, (gpointer *)mainwin);
308
309         addressbook_read_file();
310
311         inc_autocheck_timer_init(mainwin);
312
313         /* ignore SIGPIPE signal for preventing sudden death of program */
314         signal(SIGPIPE, SIG_IGN);
315
316         if (cmd.receive_all || prefs_common.chk_on_startup)
317                 inc_all_account_mail(mainwin, prefs_common.newmail_notify_manu);
318         else if (cmd.receive)
319                 inc_mail(mainwin, prefs_common.newmail_notify_manu);
320         else
321                 gtk_widget_grab_focus(folderview->ctree);
322
323         if (cmd.compose)
324                 open_compose_new(cmd.compose_mailto, cmd.attach_files);
325         if (cmd.attach_files) {
326                 ptr_array_free_strings(cmd.attach_files);
327                 g_ptr_array_free(cmd.attach_files, TRUE);
328                 cmd.attach_files = NULL;
329         }
330         if (cmd.send)
331                 send_queue();
332
333         gtk_main();
334
335         addressbook_destroy();
336
337 #if USE_PSPELL       
338         gtkpspell_checkers_delete();
339 #endif
340
341         return 0;
342 }
343
344 static void parse_cmd_opt(int argc, char *argv[])
345 {
346         gint i;
347
348         for (i = 1; i < argc; i++) {
349                 if (!strncmp(argv[i], "--debug", 7))
350                         debug_mode = TRUE;
351                 else if (!strncmp(argv[i], "--receive-all", 13))
352                         cmd.receive_all = TRUE;
353                 else if (!strncmp(argv[i], "--receive", 9))
354                         cmd.receive = TRUE;
355                 else if (!strncmp(argv[i], "--compose", 9)) {
356                         const gchar *p = argv[i + 1];
357
358                         cmd.compose = TRUE;
359                         cmd.compose_mailto = NULL;
360                         if (p && *p != '\0' && *p != '-') {
361                                 if (!strncmp(p, "mailto:", 7))
362                                         cmd.compose_mailto = p + 7;
363                                 else
364                                         cmd.compose_mailto = p;
365                                 i++;
366                         }
367                 } else if (!strncmp(argv[i], "--attach", 8)) {
368                         const gchar *p = argv[i + 1];
369                         gchar *file;
370
371                         while (p && *p != '\0' && *p != '-') {
372                                 if (!cmd.attach_files)
373                                         cmd.attach_files = g_ptr_array_new();
374                                 if (*p != G_DIR_SEPARATOR)
375                                         file = g_strconcat(startup_dir,
376                                                            G_DIR_SEPARATOR_S,
377                                                            p, NULL);
378                                 else
379                                         file = g_strdup(p);
380                                 g_ptr_array_add(cmd.attach_files, file);
381                                 i++;
382                                 p = argv[i + 1];
383                         }
384                 } else if (!strncmp(argv[i], "--send", 6)) {
385                         cmd.send = TRUE;
386                 } else if (!strncmp(argv[i], "--version", 9)) {
387                         puts("Sylpheed version " VERSION);
388                         exit(0);
389                 } else if (!strncmp(argv[i], "--status", 8)) {
390                         cmd.status = TRUE;
391                 } else if (!strncmp(argv[i], "--help", 6)) {
392                         g_print(_("Usage: %s [OPTION]...\n"),
393                                 g_basename(argv[0]));
394
395                         puts(_("  --compose [address]    open composition window"));
396                         puts(_("  --attach file1 [file2]...\n"
397                                "                         open composition window with specified files\n"
398                                "                         attached"));
399                         puts(_("  --receive              receive new messages"));
400                         puts(_("  --receive-all          receive new messages of all accounts"));
401                         puts(_("  --send                 send all queued messages"));
402                         puts(_("  --status               show the total number of messages"));
403                         puts(_("  --debug                debug mode"));
404                         puts(_("  --help                 display this help and exit"));
405                         puts(_("  --version              output version information and exit"));
406
407                         exit(1);
408                 }
409         }
410
411         if (cmd.attach_files && cmd.compose == FALSE) {
412                 cmd.compose = TRUE;
413                 cmd.compose_mailto = NULL;
414         }
415 }
416
417 static gint get_queued_message_num(void)
418 {
419         FolderItem *queue;
420
421         queue = folder_get_default_queue();
422         if (!queue) return -1;
423
424         folder_item_scan(queue);
425         return queue->total;
426 }
427
428 static void save_all_caches(FolderItem *item, gpointer data)
429 {
430         if(!item->cache)
431                 return;
432                 
433         folder_item_write_cache(item);
434 }
435
436 static void initial_processing(FolderItem *item, gpointer data)
437 {
438         MainWindow *mainwin = (MainWindow *)data;
439         gchar *buf;
440
441         g_return_if_fail(item);
442         buf = g_strdup_printf(_("Processing (%s)..."), 
443                               item->path 
444                               ? item->path 
445                               : _("top level folder"));
446         debug_print("%s\n", buf);
447         STATUSBAR_PUSH(mainwin, buf);
448         g_free(buf);
449
450         main_window_cursor_wait(mainwin);
451         
452         folder_item_apply_processing(item);
453
454         debug_print(_("done.\n"));
455         STATUSBAR_POP(mainwin);
456         main_window_cursor_normal(mainwin);
457 }
458
459 void app_will_exit(GtkWidget *widget, gpointer data)
460 {
461         MainWindow *mainwin = data;
462         gchar *filename;
463
464         if (compose_get_compose_list()) {
465                 if (alertpanel(_("Notice"),
466                                _("Composing message exists. Really quit?"),
467                                _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT)
468                         return;
469                 manage_window_focus_in(mainwin->window, NULL, NULL);
470         }
471
472         if (prefs_common.warn_queued_on_exit && get_queued_message_num() > 0) {
473                 if (alertpanel(_("Queued messages"),
474                                _("Some unsent messages are queued. Exit now?"),
475                                _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT)
476                         return;
477                 manage_window_focus_in(mainwin->window, NULL, NULL);
478         }
479
480         inc_autocheck_timer_remove();
481
482 #if USE_GPGME
483         gpgmegtk_free_passphrase();
484 #endif
485
486         if (prefs_common.clean_on_exit)
487                 main_window_empty_trash(mainwin, prefs_common.ask_on_clean);
488
489         /* save all state before exiting */
490         folder_write_list();
491         folder_func_to_all_folders(save_all_caches, NULL);
492
493         main_window_get_size(mainwin);
494         main_window_get_position(mainwin);
495         prefs_common_save_config();
496         prefs_filter_write_config();
497         account_save_config_all();
498         addressbook_export_to_file();
499
500         filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
501         gtk_item_factory_dump_rc(filename, NULL, TRUE);
502         g_free(filename);
503
504         /* delete temporary files */
505         remove_all_files(get_mime_tmp_dir());
506
507         close_log_file();
508
509         /* delete unix domain socket */
510         gdk_input_remove(lock_socket_tag);
511         fd_close(lock_socket);
512         filename = get_socket_name();
513         unlink(filename);
514
515 #if USE_SSL
516         ssl_done();
517 #endif
518
519         gtk_main_quit();
520 }
521
522 #if USE_GPGME
523 static void idle_function_for_gpgme(void)
524 {
525         while (gtk_events_pending())
526                 gtk_main_iteration();
527 }
528 #endif /* USE_GPGME */
529
530 static gchar *get_socket_name(void)
531 {
532         static gchar *filename = NULL;
533
534         if (filename == NULL) {
535                 filename = g_strdup_printf("%s%csylpheed-%d",
536                                            g_get_tmp_dir(), G_DIR_SEPARATOR,
537                                            getuid());
538         }
539
540         return filename;
541 }
542
543 static gint prohibit_duplicate_launch(void)
544 {
545         gint uxsock;
546         gchar *path;
547
548         path = get_socket_name();
549         uxsock = fd_connect_unix(path);
550         if (uxsock < 0) {
551                 unlink(path);
552                 return fd_open_unix(path);
553         }
554
555         /* remote command mode */
556
557         debug_print(_("another Sylpheed is already running.\n"));
558
559         if (cmd.receive_all)
560                 fd_write(uxsock, "receive_all\n", 12);
561         else if (cmd.receive)
562                 fd_write(uxsock, "receive\n", 8);
563         else if (cmd.compose && cmd.attach_files) {
564                 gchar *str, *compose_str;
565                 gint i;
566
567                 if (cmd.compose_mailto)
568                         compose_str = g_strdup_printf("compose_attach %s\n",
569                                                       cmd.compose_mailto);
570                 else
571                         compose_str = g_strdup("compose_attach\n");
572
573                 fd_write(uxsock, compose_str, strlen(compose_str));
574                 g_free(compose_str);
575
576                 for (i = 0; i < cmd.attach_files->len; i++) {
577                         str = g_ptr_array_index(cmd.attach_files, i);
578                         fd_write(uxsock, str, strlen(str));
579                         fd_write(uxsock, "\n", 1);
580                 }
581
582                 fd_write(uxsock, ".\n", 2);
583         } else if (cmd.compose) {
584                 gchar *compose_str;
585
586                 if (cmd.compose_mailto)
587                         compose_str = g_strdup_printf("compose %s\n", cmd.compose_mailto);
588                 else
589                         compose_str = g_strdup("compose\n");
590
591                 fd_write(uxsock, compose_str, strlen(compose_str));
592                 g_free(compose_str);
593         } else if (cmd.send) {
594                 fd_write(uxsock, "send\n", 5);
595         } else if (cmd.status) {
596                 gchar buf[BUFFSIZE];
597
598                 fd_write(uxsock, "status\n", 7);
599                 fd_gets(uxsock, buf, sizeof(buf));
600                 fputs(buf, stdout);
601         } else
602                 fd_write(uxsock, "popup\n", 6);
603
604         fd_close(uxsock);
605         return -1;
606 }
607
608 static void lock_socket_input_cb(gpointer data,
609                                  gint source,
610                                  GdkInputCondition condition)
611 {
612         MainWindow *mainwin = (MainWindow *)data;
613         gint sock;
614         gchar buf[BUFFSIZE];
615
616         sock = fd_accept(source);
617         fd_gets(sock, buf, sizeof(buf));
618
619         if (!strncmp(buf, "popup", 5)) {
620                 main_window_popup(mainwin);
621         } else if (!strncmp(buf, "receive_all", 11)) {
622                 main_window_popup(mainwin);
623                 inc_all_account_mail(mainwin, prefs_common.newmail_notify_manu);
624         } else if (!strncmp(buf, "receive", 7)) {
625                 main_window_popup(mainwin);
626                 inc_mail(mainwin, prefs_common.newmail_notify_manu);
627         } else if (!strncmp(buf, "compose_attach", 14)) {
628                 GPtrArray *files;
629                 gchar *mailto;
630
631                 mailto = g_strdup(buf + strlen("compose_attach") + 1);
632                 files = g_ptr_array_new();
633                 while (fd_gets(sock, buf, sizeof(buf)) > 0) {
634                         if (buf[0] == '.' && buf[1] == '\n') break;
635                         strretchomp(buf);
636                         g_ptr_array_add(files, g_strdup(buf));
637                 }
638                 open_compose_new(mailto, files);
639                 ptr_array_free_strings(files);
640                 g_ptr_array_free(files, TRUE);
641                 g_free(mailto);
642         } else if (!strncmp(buf, "compose", 7)) {
643                 open_compose_new(buf + strlen("compose") + 1, NULL);
644         } else if (!strncmp(buf, "send", 4)) {
645                 send_queue();
646         } else if (!strncmp(buf, "status", 6)) {
647                 guint new, unread, total;
648
649                 folder_count_total_msgs(&new, &unread, &total);
650                 g_snprintf(buf, sizeof(buf), "%d %d %d\n", new, unread, total);
651                 fd_write(sock, buf, strlen(buf));
652         }
653
654         fd_close(sock);
655 }
656
657 static void open_compose_new(const gchar *address, GPtrArray *attach_files)
658 {
659         gchar *addr = NULL;
660
661         if (address) {
662                 Xstrdup_a(addr, address, return);
663                 g_strstrip(addr);
664         }
665
666         compose_new(NULL, addr, attach_files);
667 }
668
669 static void send_queue(void)
670 {
671         GList *list;
672         FolderItem *def_outbox;
673
674         def_outbox = folder_get_default_outbox();
675
676         for (list = folder_get_list(); list != NULL; list = list->next) {
677                 Folder *folder = list->data;
678
679                 if (folder->queue) {
680                         if (procmsg_send_queue
681                                 (folder->queue, prefs_common.savemsg) < 0)
682                                 alertpanel_error(_("Some errors occurred while sending queued messages."));
683                         statusbar_pop_all();
684                         folder_item_scan(folder->queue);
685                         folderview_update_item(folder->queue, TRUE);
686                         if (prefs_common.savemsg && folder->outbox) {
687                                 folderview_update_item(folder->outbox, TRUE);
688                                 if (folder->outbox == def_outbox)
689                                         def_outbox = NULL;
690                         }
691                 }
692         }
693
694         if (prefs_common.savemsg && def_outbox)
695                 folderview_update_item(def_outbox, TRUE);
696 }