ebd242a3d850d5ff2afab0baee35e5d93c669d8f
[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(RC_DIR G_DIR_SEPARATOR_S "uidl");
219
220         if (is_file_exist(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log")) {
221                 if (rename(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log",
222                            RC_DIR G_DIR_SEPARATOR_S "sylpheed.log.bak") < 0)
223                         FILE_OP_ERROR("sylpheed.log", "rename");
224         }
225         set_log_file(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log");
226
227         if (is_file_exist(RC_DIR G_DIR_SEPARATOR_S "assortrc") &&
228             !is_file_exist(RC_DIR G_DIR_SEPARATOR_S "filterrc")) {
229                 if (rename(RC_DIR G_DIR_SEPARATOR_S "assortrc",
230                            RC_DIR G_DIR_SEPARATOR_S "filterrc") < 0)
231                         FILE_OP_ERROR(RC_DIR G_DIR_SEPARATOR_S "assortrc",
232                                       "rename");
233         }
234
235         prefs_common_init();
236         prefs_common_read_config();
237
238 #if USE_GPGME
239         if (gpgme_check_engine()) {  /* Also does some gpgme init */
240                 rfc2015_disable_all();
241                 debug_print("gpgme_engine_version:\n%s\n",
242                             gpgme_get_engine_info());
243
244                 if (prefs_common.gpg_warning) {
245                         AlertValue val;
246
247                         val = alertpanel_message_with_disable
248                                 (_("Warning"),
249                                  _("GnuPG is not installed properly.\n"
250                                    "OpenPGP support disabled."));
251                         if (val & G_ALERTDISABLE)
252                                 prefs_common.gpg_warning = FALSE;
253                 }
254         }
255         gpgme_register_idle(idle_function_for_gpgme);
256 #endif
257
258 #if USE_PSPELL
259         gtkpspellcheckers = gtkpspell_checkers_new();
260 #endif
261         
262
263         prefs_common_save_config();
264         prefs_filter_read_config();
265         prefs_filter_write_config();
266         prefs_actions_read_config();
267         prefs_actions_write_config();
268         prefs_display_header_read_config();
269         prefs_display_header_write_config();
270         /* prefs_filtering_read_config(); */
271         addressbook_read_file();
272         renderer_read_config();
273
274         gtkut_widget_init();
275
276         mainwin = main_window_create
277                 (prefs_common.sep_folder | prefs_common.sep_msg << 1);
278         folderview = mainwin->folderview;
279
280         /* register the callback of unix domain socket input */
281         lock_socket_tag = gdk_input_add(lock_socket,
282                                         GDK_INPUT_READ | GDK_INPUT_EXCEPTION,
283                                         lock_socket_input_cb,
284                                         mainwin);
285
286         account_read_config_all();
287         account_save_config_all();
288
289         if (folder_read_list() < 0) {
290                 setup(mainwin);
291                 folder_write_list();
292         }
293         if (!account_get_list()) {
294                 account_edit_open();
295                 account_add();
296         }
297
298         account_set_missing_folder();
299         folder_set_missing_folders();
300         folderview_set(folderview);
301
302         /* prefs_scoring_read_config(); */
303         prefs_matcher_read_config();
304
305         /* make one all-folder processing before using sylpheed */
306         folder_func_to_all_folders(initial_processing, (gpointer *)mainwin);
307
308         addressbook_read_file();
309
310         inc_autocheck_timer_init(mainwin);
311
312         /* ignore SIGPIPE signal for preventing sudden death of program */
313         signal(SIGPIPE, SIG_IGN);
314
315         if (cmd.receive_all || prefs_common.chk_on_startup)
316                 inc_all_account_mail(mainwin, prefs_common.newmail_notify_manu);
317         else if (cmd.receive)
318                 inc_mail(mainwin, prefs_common.newmail_notify_manu);
319         else
320                 gtk_widget_grab_focus(folderview->ctree);
321
322         if (cmd.compose)
323                 open_compose_new(cmd.compose_mailto, cmd.attach_files);
324         if (cmd.attach_files) {
325                 ptr_array_free_strings(cmd.attach_files);
326                 g_ptr_array_free(cmd.attach_files, TRUE);
327                 cmd.attach_files = NULL;
328         }
329         if (cmd.send)
330                 send_queue();
331
332         gtk_main();
333
334         addressbook_destroy();
335
336 #if USE_PSPELL       
337         gtkpspell_checkers_delete();
338 #endif
339
340         return 0;
341 }
342
343 static void parse_cmd_opt(int argc, char *argv[])
344 {
345         gint i;
346
347         for (i = 1; i < argc; i++) {
348                 if (!strncmp(argv[i], "--debug", 7))
349                         debug_mode = TRUE;
350                 else if (!strncmp(argv[i], "--receive-all", 13))
351                         cmd.receive_all = TRUE;
352                 else if (!strncmp(argv[i], "--receive", 9))
353                         cmd.receive = TRUE;
354                 else if (!strncmp(argv[i], "--compose", 9)) {
355                         const gchar *p = argv[i + 1];
356
357                         cmd.compose = TRUE;
358                         cmd.compose_mailto = NULL;
359                         if (p && *p != '\0' && *p != '-') {
360                                 if (!strncmp(p, "mailto:", 7))
361                                         cmd.compose_mailto = p + 7;
362                                 else
363                                         cmd.compose_mailto = p;
364                                 i++;
365                         }
366                 } else if (!strncmp(argv[i], "--attach", 8)) {
367                         const gchar *p = argv[i + 1];
368                         gchar *file;
369
370                         while (p && *p != '\0' && *p != '-') {
371                                 if (!cmd.attach_files)
372                                         cmd.attach_files = g_ptr_array_new();
373                                 if (*p != G_DIR_SEPARATOR)
374                                         file = g_strconcat(startup_dir,
375                                                            G_DIR_SEPARATOR_S,
376                                                            p, NULL);
377                                 else
378                                         file = g_strdup(p);
379                                 g_ptr_array_add(cmd.attach_files, file);
380                                 i++;
381                                 p = argv[i + 1];
382                         }
383                 } else if (!strncmp(argv[i], "--send", 6)) {
384                         cmd.send = TRUE;
385                 } else if (!strncmp(argv[i], "--version", 9)) {
386                         puts("Sylpheed version " VERSION);
387                         exit(0);
388                 } else if (!strncmp(argv[i], "--status", 8)) {
389                         cmd.status = TRUE;
390                 } else if (!strncmp(argv[i], "--help", 6)) {
391                         g_print(_("Usage: %s [OPTION]...\n"),
392                                 g_basename(argv[0]));
393
394                         puts(_("  --compose [address]    open composition window"));
395                         puts(_("  --attach file1 [file2]...\n"
396                                "                         open composition window with specified files\n"
397                                "                         attached"));
398                         puts(_("  --receive              receive new messages"));
399                         puts(_("  --receive-all          receive new messages of all accounts"));
400                         puts(_("  --send                 send all queued messages"));
401                         puts(_("  --status               show the total number of messages"));
402                         puts(_("  --debug                debug mode"));
403                         puts(_("  --help                 display this help and exit"));
404                         puts(_("  --version              output version information and exit"));
405
406                         exit(1);
407                 }
408         }
409
410         if (cmd.attach_files && cmd.compose == FALSE) {
411                 cmd.compose = TRUE;
412                 cmd.compose_mailto = NULL;
413         }
414 }
415
416 static gint get_queued_message_num(void)
417 {
418         FolderItem *queue;
419
420         queue = folder_get_default_queue();
421         if (!queue) return -1;
422
423         folder_item_scan(queue);
424         return queue->total;
425 }
426
427 static void save_all_caches(FolderItem *item, gpointer data)
428 {
429         if(!item->cache)
430                 return;
431                 
432         folder_item_write_cache(item);
433 }
434
435 static void initial_processing(FolderItem *item, gpointer data)
436 {
437         MainWindow *mainwin = (MainWindow *)data;
438         gchar *buf;
439
440         g_return_if_fail(item);
441         buf = g_strdup_printf(_("Processing (%s)..."), 
442                               item->path 
443                               ? item->path 
444                               : _("top level folder"));
445         debug_print("%s\n", buf);
446         STATUSBAR_PUSH(mainwin, buf);
447         g_free(buf);
448
449         main_window_cursor_wait(mainwin);
450         
451         folder_item_apply_processing(item);
452
453         debug_print(_("done.\n"));
454         STATUSBAR_POP(mainwin);
455         main_window_cursor_normal(mainwin);
456 }
457
458 void app_will_exit(GtkWidget *widget, gpointer data)
459 {
460         MainWindow *mainwin = data;
461         gchar *filename;
462
463         if (compose_get_compose_list()) {
464                 if (alertpanel(_("Notice"),
465                                _("Composing message exists. Really quit?"),
466                                _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT)
467                         return;
468                 manage_window_focus_in(mainwin->window, NULL, NULL);
469         }
470
471         if (prefs_common.warn_queued_on_exit && get_queued_message_num() > 0) {
472                 if (alertpanel(_("Queued messages"),
473                                _("Some unsent messages are queued. Exit now?"),
474                                _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT)
475                         return;
476                 manage_window_focus_in(mainwin->window, NULL, NULL);
477         }
478
479         inc_autocheck_timer_remove();
480
481 #if USE_GPGME
482         gpgmegtk_free_passphrase();
483 #endif
484
485         if (prefs_common.clean_on_exit)
486                 main_window_empty_trash(mainwin, prefs_common.ask_on_clean);
487
488         /* save all state before exiting */
489         folder_write_list();
490         folder_func_to_all_folders(save_all_caches, NULL);
491
492         main_window_get_size(mainwin);
493         main_window_get_position(mainwin);
494         prefs_common_save_config();
495         prefs_filter_write_config();
496         account_save_config_all();
497         addressbook_export_to_file();
498
499         filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
500         gtk_item_factory_dump_rc(filename, NULL, TRUE);
501         g_free(filename);
502
503         /* delete temporary files */
504         remove_all_files(get_mime_tmp_dir());
505
506         close_log_file();
507
508         /* delete unix domain socket */
509         gdk_input_remove(lock_socket_tag);
510         fd_close(lock_socket);
511         filename = get_socket_name();
512         unlink(filename);
513
514 #if USE_SSL
515         ssl_done();
516 #endif
517
518         gtk_main_quit();
519 }
520
521 #if USE_GPGME
522 static void idle_function_for_gpgme(void)
523 {
524         while (gtk_events_pending())
525                 gtk_main_iteration();
526 }
527 #endif /* USE_GPGME */
528
529 static gchar *get_socket_name(void)
530 {
531         static gchar *filename = NULL;
532
533         if (filename == NULL) {
534                 filename = g_strdup_printf("%s%csylpheed-%d",
535                                            g_get_tmp_dir(), G_DIR_SEPARATOR,
536                                            getuid());
537         }
538
539         return filename;
540 }
541
542 static gint prohibit_duplicate_launch(void)
543 {
544         gint uxsock;
545         gchar *path;
546
547         path = get_socket_name();
548         uxsock = fd_connect_unix(path);
549         if (uxsock < 0) {
550                 unlink(path);
551                 return fd_open_unix(path);
552         }
553
554         /* remote command mode */
555
556         debug_print(_("another Sylpheed is already running.\n"));
557
558         if (cmd.receive_all)
559                 fd_write(uxsock, "receive_all\n", 12);
560         else if (cmd.receive)
561                 fd_write(uxsock, "receive\n", 8);
562         else if (cmd.compose && cmd.attach_files) {
563                 gchar *str, *compose_str;
564                 gint i;
565
566                 if (cmd.compose_mailto)
567                         compose_str = g_strdup_printf("compose_attach %s\n",
568                                                       cmd.compose_mailto);
569                 else
570                         compose_str = g_strdup("compose_attach\n");
571
572                 fd_write(uxsock, compose_str, strlen(compose_str));
573                 g_free(compose_str);
574
575                 for (i = 0; i < cmd.attach_files->len; i++) {
576                         str = g_ptr_array_index(cmd.attach_files, i);
577                         fd_write(uxsock, str, strlen(str));
578                         fd_write(uxsock, "\n", 1);
579                 }
580
581                 fd_write(uxsock, ".\n", 2);
582         } else if (cmd.compose) {
583                 gchar *compose_str;
584
585                 if (cmd.compose_mailto)
586                         compose_str = g_strdup_printf("compose %s\n", cmd.compose_mailto);
587                 else
588                         compose_str = g_strdup("compose\n");
589
590                 fd_write(uxsock, compose_str, strlen(compose_str));
591                 g_free(compose_str);
592         } else if (cmd.send) {
593                 fd_write(uxsock, "send\n", 5);
594         } else if (cmd.status) {
595                 gchar buf[BUFFSIZE];
596
597                 fd_write(uxsock, "status\n", 7);
598                 fd_gets(uxsock, buf, sizeof(buf));
599                 fputs(buf, stdout);
600         } else
601                 fd_write(uxsock, "popup\n", 6);
602
603         fd_close(uxsock);
604         return -1;
605 }
606
607 static void lock_socket_input_cb(gpointer data,
608                                  gint source,
609                                  GdkInputCondition condition)
610 {
611         MainWindow *mainwin = (MainWindow *)data;
612         gint sock;
613         gchar buf[BUFFSIZE];
614
615         sock = fd_accept(source);
616         fd_gets(sock, buf, sizeof(buf));
617
618         if (!strncmp(buf, "popup", 5)) {
619                 main_window_popup(mainwin);
620         } else if (!strncmp(buf, "receive_all", 11)) {
621                 main_window_popup(mainwin);
622                 inc_all_account_mail(mainwin, prefs_common.newmail_notify_manu);
623         } else if (!strncmp(buf, "receive", 7)) {
624                 main_window_popup(mainwin);
625                 inc_mail(mainwin, prefs_common.newmail_notify_manu);
626         } else if (!strncmp(buf, "compose_attach", 14)) {
627                 GPtrArray *files;
628                 gchar *mailto;
629
630                 mailto = g_strdup(buf + strlen("compose_attach") + 1);
631                 files = g_ptr_array_new();
632                 while (fd_gets(sock, buf, sizeof(buf)) > 0) {
633                         if (buf[0] == '.' && buf[1] == '\n') break;
634                         strretchomp(buf);
635                         g_ptr_array_add(files, g_strdup(buf));
636                 }
637                 open_compose_new(mailto, files);
638                 ptr_array_free_strings(files);
639                 g_ptr_array_free(files, TRUE);
640                 g_free(mailto);
641         } else if (!strncmp(buf, "compose", 7)) {
642                 open_compose_new(buf + strlen("compose") + 1, NULL);
643         } else if (!strncmp(buf, "send", 4)) {
644                 send_queue();
645         } else if (!strncmp(buf, "status", 6)) {
646                 guint new, unread, total;
647
648                 folder_count_total_msgs(&new, &unread, &total);
649                 g_snprintf(buf, sizeof(buf), "%d %d %d\n", new, unread, total);
650                 fd_write(sock, buf, strlen(buf));
651         }
652
653         fd_close(sock);
654 }
655
656 static void open_compose_new(const gchar *address, GPtrArray *attach_files)
657 {
658         gchar *addr = NULL;
659
660         if (address) {
661                 Xstrdup_a(addr, address, return);
662                 g_strstrip(addr);
663         }
664
665         compose_new(NULL, addr, attach_files);
666 }
667
668 static void send_queue(void)
669 {
670         GList *list;
671         FolderItem *def_outbox;
672
673         def_outbox = folder_get_default_outbox();
674
675         for (list = folder_get_list(); list != NULL; list = list->next) {
676                 Folder *folder = list->data;
677
678                 if (folder->queue) {
679                         if (procmsg_send_queue
680                                 (folder->queue, prefs_common.savemsg) < 0)
681                                 alertpanel_error(_("Some errors occurred while sending queued messages."));
682                         statusbar_pop_all();
683                         folder_item_scan(folder->queue);
684                         folderview_update_item(folder->queue, TRUE);
685                         if (prefs_common.savemsg && folder->outbox) {
686                                 folderview_update_item(folder->outbox, TRUE);
687                                 if (folder->outbox == def_outbox)
688                                         def_outbox = NULL;
689                         }
690                 }
691         }
692
693         if (prefs_common.savemsg && def_outbox)
694                 folderview_update_item(def_outbox, TRUE);
695 }