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