enable storing of GnuPG passphrase
[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 "scoring.h"
62 #include "prefs_display_header.h"
63 #include "account.h"
64 #include "procmsg.h"
65 #include "inc.h"
66 #include "import.h"
67 #include "manage_window.h"
68 #include "alertpanel.h"
69 #include "statusbar.h"
70 #include "addressbook.h"
71 #include "compose.h"
72 #include "folder.h"
73 #include "setup.h"
74 #include "utils.h"
75 #include "gtkutils.h"
76
77 #if USE_GPGME
78 #  include "rfc2015.h"
79 #endif
80 #if USE_SSL
81 #  include "ssl.h"
82 #endif
83
84 #include "version.h"
85
86 gchar *prog_version;
87 gchar *startup_dir;
88 gboolean debug_mode = FALSE;
89
90 static gint lock_socket = -1;
91 static gint lock_socket_tag = 0;
92
93 static struct Cmd {
94         gboolean receive;
95         gboolean receive_all;
96         gboolean compose;
97         const gchar *compose_mailto;
98         gboolean status;
99         gboolean send;
100 } cmd;
101
102 static void parse_cmd_opt(int argc, char *argv[]);
103
104 #if USE_GPGME
105 static void idle_function_for_gpgme(void);
106 #endif /* USE_GPGME */
107
108 static gint prohibit_duplicate_launch   (void);
109 static void lock_socket_input_cb        (gpointer          data,
110                                          gint              source,
111                                          GdkInputCondition condition);
112 static gchar *get_socket_name           (void);
113
114 static void open_compose_new_with_recipient     (const gchar    *address);
115
116 static void send_queue                  (void);
117
118 #if 0
119 /* for gettext */
120 _("File `%s' already exists.\n"
121   "Can't create folder.")
122 #endif
123
124 #define MAKE_DIR_IF_NOT_EXIST(dir) \
125 { \
126         if (!is_dir_exist(dir)) { \
127                 if (is_file_exist(dir)) { \
128                         alertpanel_warning \
129                                 (_("File `%s' already exists.\n" \
130                                    "Can't create folder."), \
131                                  dir); \
132                         return 1; \
133                 } \
134                 if (mkdir(dir, S_IRWXU) < 0) { \
135                         FILE_OP_ERROR(dir, "mkdir"); \
136                         return 1; \
137                 } \
138                 if (chmod(dir, S_IRWXU) < 0) \
139                         FILE_OP_ERROR(dir, "chmod"); \
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         parse_cmd_opt(argc, argv);
154
155         gtk_set_locale();
156         gtk_init(&argc, &argv);
157
158 #if USE_THREADS || USE_LDAP
159         g_thread_init(NULL);
160         if (!g_thread_supported())
161                 g_error(_("g_thread is not supported by glib.\n"));
162 #endif
163
164 #if HAVE_GDK_IMLIB
165         gdk_imlib_init();
166         gtk_widget_push_visual(gdk_imlib_get_visual());
167         gtk_widget_push_colormap(gdk_imlib_get_colormap());
168 #endif
169
170 #if USE_SSL
171         ssl_init();
172 #endif
173
174         srandom((gint)time(NULL));
175
176         /* parse gtkrc files */
177         userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtkrc",
178                              NULL);
179         gtk_rc_parse(userrc);
180         g_free(userrc);
181         userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtk",
182                              G_DIR_SEPARATOR_S, "gtkrc", NULL);
183         gtk_rc_parse(userrc);
184         g_free(userrc);
185         userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "gtkrc", NULL);
186         gtk_rc_parse(userrc);
187         g_free(userrc);
188
189         gtk_rc_parse("./gtkrc");
190
191         userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
192         gtk_item_factory_parse_rc(userrc);
193         g_free(userrc);
194
195         prog_version = PROG_VERSION;
196         startup_dir = g_get_current_dir();
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
219         if (is_file_exist(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log")) {
220                 if (rename(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log",
221                            RC_DIR G_DIR_SEPARATOR_S "sylpheed.log.bak") < 0)
222                         FILE_OP_ERROR("sylpheed.log", "rename");
223         }
224         set_log_file(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log");
225
226         if (is_file_exist(RC_DIR G_DIR_SEPARATOR_S "assortrc") &&
227             !is_file_exist(RC_DIR G_DIR_SEPARATOR_S "filterrc")) {
228                 if (rename(RC_DIR G_DIR_SEPARATOR_S "assortrc",
229                            RC_DIR G_DIR_SEPARATOR_S "filterrc") < 0)
230                         FILE_OP_ERROR(RC_DIR G_DIR_SEPARATOR_S "assortrc",
231                                       "rename");
232         }
233
234         prefs_common_init();
235         prefs_common_read_config();
236
237 #if USE_GPGME
238         if (gpgme_check_engine()) {  /* Also does some gpgme init */
239                 rfc2015_disable_all();
240                 debug_print("gpgme_engine_version:\n%s\n",
241                             gpgme_get_engine_info());
242
243                 if (prefs_common.gpg_warning) {
244                         AlertValue val;
245
246                         val = alertpanel_message_with_disable
247                                 (_("Warning"),
248                                  _("GnuPG is not installed properly.\n"
249                                    "OpenPGP support disabled."));
250                         if (val & G_ALERTDISABLE)
251                                 prefs_common.gpg_warning = FALSE;
252                 }
253         }
254         gpgme_register_idle(idle_function_for_gpgme);
255 #endif
256
257 #if USE_PSPELL
258         gtkpspellcheckers = gtkpspell_checkers_new();
259 #endif
260         
261
262         prefs_common_save_config();
263         prefs_filter_read_config();
264         prefs_filter_write_config();
265         prefs_actions_read_config();
266         prefs_actions_write_config();
267         prefs_display_header_read_config();
268         prefs_display_header_write_config();
269         /* prefs_filtering_read_config(); */
270         addressbook_read_file();
271         renderer_read_config();
272
273         gtkut_widget_init();
274
275         mainwin = main_window_create
276                 (prefs_common.sep_folder | prefs_common.sep_msg << 1);
277         folderview = mainwin->folderview;
278
279         /* register the callback of unix domain socket input */
280         lock_socket_tag = gdk_input_add(lock_socket,
281                                         GDK_INPUT_READ | GDK_INPUT_EXCEPTION,
282                                         lock_socket_input_cb,
283                                         mainwin);
284
285         account_read_config_all();
286         account_save_config_all();
287
288         if (folder_read_list() < 0) {
289                 setup(mainwin);
290                 folder_write_list();
291         }
292         if (!account_get_list()) {
293                 account_edit_open();
294                 account_add();
295         }
296
297         account_set_missing_folder();
298         folder_set_missing_folders();
299         folderview_set(folderview);
300
301         /* prefs_scoring_read_config(); */
302         prefs_matcher_read_config();
303         /* make one all-folder processing before using sylpheed */
304         processing_apply(mainwin->summaryview);
305
306         addressbook_read_file();
307
308         inc_autocheck_timer_init(mainwin);
309
310         /* ignore SIGPIPE signal for preventing sudden death of program */
311         signal(SIGPIPE, SIG_IGN);
312
313         if (cmd.receive_all || prefs_common.chk_on_startup)
314                 inc_all_account_mail(mainwin, prefs_common.newmail_notify_manu);
315         else if (cmd.receive)
316                 inc_mail(mainwin, prefs_common.newmail_notify_manu);
317         else
318                 gtk_widget_grab_focus(folderview->ctree);
319
320         if (cmd.compose)
321                 open_compose_new_with_recipient(cmd.compose_mailto);
322         if (cmd.send)
323                 send_queue();
324
325         gtk_main();
326
327         addressbook_destroy();
328
329 #if USE_PSPELL       
330         gtkpspell_checkers_delete();
331 #endif
332
333         return 0;
334 }
335
336 static void parse_cmd_opt(int argc, char *argv[])
337 {
338         gint i;
339
340         for (i = 1; i < argc; i++) {
341                 if (!strncmp(argv[i], "--debug", 7))
342                         debug_mode = TRUE;
343                 else if (!strncmp(argv[i], "--receive-all", 13))
344                         cmd.receive_all = TRUE;
345                 else if (!strncmp(argv[i], "--receive", 9))
346                         cmd.receive = TRUE;
347                 else if (!strncmp(argv[i], "--compose", 9)) {
348                         const gchar *p = argv[i + 1];
349
350                         cmd.compose = TRUE;
351                         cmd.compose_mailto = NULL;
352                         if (p && *p != '\0' && *p != '-') {
353                                 if (!strncmp(p, "mailto:", 7))
354                                         cmd.compose_mailto = p + 7;
355                                 else
356                                         cmd.compose_mailto = p;
357                                 i++;
358                         }
359                 } else if (!strncmp(argv[i], "--send", 6)) {
360                         cmd.send = TRUE;
361                 } else if (!strncmp(argv[i], "--version", 9)) {
362                         puts("Sylpheed version " VERSION);
363                         exit(0);
364                 } else if (!strncmp(argv[i], "--status", 8)) {
365                         cmd.status = TRUE;
366                 } else if (!strncmp(argv[i], "--help", 6)) {
367                         g_print(_("Usage: %s [OPTION]...\n"),
368                                 g_basename(argv[0]));
369
370                         puts(_("  --compose [address]    open composition window"));
371                         puts(_("  --receive              receive new messages"));
372                         puts(_("  --receive-all          receive new messages of all accounts"));
373                         puts(_("  --send                 send all queued messages"));
374                         puts(_("  --status               show the total number of messages"));
375                         puts(_("  --debug                debug mode"));
376                         puts(_("  --help                 display this help and exit"));
377                         puts(_("  --version              output version information and exit"));
378
379                         exit(1);
380                 }
381         }
382 }
383
384 static gint get_queued_message_num(void)
385 {
386         FolderItem *queue;
387
388         queue = folder_get_default_queue();
389         if (!queue) return -1;
390
391         folder_item_scan(queue);
392         return queue->total;
393 }
394
395 void app_will_exit(GtkWidget *widget, gpointer data)
396 {
397         MainWindow *mainwin = data;
398         gchar *filename;
399
400         if (compose_get_compose_list()) {
401                 if (alertpanel(_("Notice"),
402                                _("Composing message exists. Really quit?"),
403                                _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT)
404                         return;
405                 manage_window_focus_in(mainwin->window, NULL, NULL);
406         }
407
408         if (prefs_common.warn_queued_on_exit && get_queued_message_num() > 0) {
409                 if (alertpanel(_("Queued messages"),
410                                _("Some unsent messages are queued. Exit now?"),
411                                _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT)
412                         return;
413                 manage_window_focus_in(mainwin->window, NULL, NULL);
414         }
415
416         inc_autocheck_timer_remove();
417
418 #if USE_GPGME
419         gpgmegtk_free_passphrase();
420 #endif
421
422         if (prefs_common.clean_on_exit)
423                 main_window_empty_trash(mainwin, prefs_common.ask_on_clean);
424
425         /* save all state before exiting */
426         folder_write_list();
427         summary_write_cache(mainwin->summaryview);
428
429         main_window_get_size(mainwin);
430         main_window_get_position(mainwin);
431         prefs_common_save_config();
432         prefs_filter_write_config();
433         account_save_config_all();
434         addressbook_export_to_file();
435
436         filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
437         gtk_item_factory_dump_rc(filename, NULL, TRUE);
438         g_free(filename);
439
440         /* delete temporary files */
441         remove_all_files(get_mime_tmp_dir());
442
443         close_log_file();
444
445         /* delete unix domain socket */
446         gdk_input_remove(lock_socket_tag);
447         fd_close(lock_socket);
448         filename = get_socket_name();
449         unlink(filename);
450
451 #if USE_SSL
452         ssl_done();
453 #endif
454
455         gtk_main_quit();
456 }
457
458 #if USE_GPGME
459 static void idle_function_for_gpgme(void)
460 {
461         while (gtk_events_pending())
462                 gtk_main_iteration();
463 }
464 #endif /* USE_GPGME */
465
466 static gchar *get_socket_name(void)
467 {
468         static gchar *filename = NULL;
469
470         if (filename == NULL) {
471                 filename = g_strdup_printf("%s%csylpheed-%d",
472                                            g_get_tmp_dir(), G_DIR_SEPARATOR,
473                                            getuid());
474         }
475
476         return filename;
477 }
478
479 static gint prohibit_duplicate_launch(void)
480 {
481         gint uxsock;
482         gchar *path;
483
484         path = get_socket_name();
485         uxsock = fd_connect_unix(path);
486         if (uxsock < 0) {
487                 unlink(path);
488                 return fd_open_unix(path);
489         }
490
491         /* remote command mode */
492
493         debug_print(_("another Sylpheed is already running.\n"));
494
495         if (cmd.receive_all)
496                 fd_write(uxsock, "receive_all\n", 12);
497         else if (cmd.receive)
498                 fd_write(uxsock, "receive\n", 8);
499         else if (cmd.compose) {
500                 gchar *compose_str;
501
502                 if (cmd.compose_mailto)
503                         compose_str = g_strdup_printf("compose %s\n", cmd.compose_mailto);
504                 else
505                         compose_str = g_strdup("compose\n");
506
507                 fd_write(uxsock, compose_str, strlen(compose_str));
508                 g_free(compose_str);
509         } else if (cmd.send) {
510                 fd_write(uxsock, "send\n", 5);
511         } else if (cmd.status) {
512                 gchar buf[BUFFSIZE];
513
514                 fd_write(uxsock, "status\n", 7);
515                 fd_gets(uxsock, buf, sizeof(buf));
516                 fputs(buf, stdout);
517         } else
518                 fd_write(uxsock, "popup\n", 6);
519
520         fd_close(uxsock);
521         return -1;
522 }
523
524 static void lock_socket_input_cb(gpointer data,
525                                  gint source,
526                                  GdkInputCondition condition)
527 {
528         MainWindow *mainwin = (MainWindow *)data;
529         gint sock;
530         gchar buf[BUFFSIZE];
531
532         sock = fd_accept(source);
533         fd_gets(sock, buf, sizeof(buf));
534
535         if (!strncmp(buf, "popup", 5)){
536                 main_window_popup(mainwin);
537         } else if (!strncmp(buf, "receive_all", 11)){
538                 main_window_popup(mainwin);
539                 inc_all_account_mail(mainwin, prefs_common.newmail_notify_manu);
540         } else if (!strncmp(buf, "receive", 7)){
541                 main_window_popup(mainwin);
542                 inc_mail(mainwin, prefs_common.newmail_notify_manu);
543         } else if (!strncmp(buf, "compose", 7)) {
544                 open_compose_new_with_recipient(buf + strlen("compose") + 1);
545         } else if (!strncmp(buf, "send", 4)) {
546                 send_queue();
547         } else if (!strncmp(buf, "status", 6)) {
548                 guint new, unread, total;
549
550                 folder_count_total_msgs(&new, &unread, &total);
551                 g_snprintf(buf, sizeof(buf), "%d %d %d\n", new, unread, total);
552                 fd_write(sock, buf, strlen(buf));
553         }
554
555         fd_close(sock);
556 }
557
558 static void open_compose_new_with_recipient(const gchar *address)
559 {
560         gchar *addr = NULL;
561
562         if (address) {
563                 Xstrdup_a(addr, address, return);
564                 g_strstrip(addr);
565         }
566
567         if (addr && *addr != '\0')
568                 compose_new_with_recipient(NULL, addr);
569         else
570                 compose_new(NULL);
571 }
572
573 static void send_queue(void)
574 {
575         GList *list;
576         FolderItem *def_outbox;
577
578         def_outbox = folder_get_default_outbox();
579
580         for (list = folder_get_list(); list != NULL; list = list->next) {
581                 Folder *folder = list->data;
582
583                 if (folder->queue) {
584                         if (procmsg_send_queue
585                                 (folder->queue, prefs_common.savemsg) < 0)
586                                 alertpanel_error(_("Some errors occurred while sending queued messages."));
587                         statusbar_pop_all();
588                         folder_item_scan(folder->queue);
589                         folderview_update_item(folder->queue, TRUE);
590                         if (prefs_common.savemsg && folder->outbox) {
591                                 folderview_update_item(folder->outbox, TRUE);
592                                 if (folder->outbox == def_outbox)
593                                         def_outbox = NULL;
594                         }
595                 }
596         }
597
598         if (prefs_common.savemsg && def_outbox)
599                 folderview_update_item(def_outbox, TRUE);
600 }