3b7cb574e09b453cd60ea8d9901ec54ba1cb0f66
[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         gboolean status;
100         gboolean send;
101 } cmd;
102
103 static void parse_cmd_opt(int argc, char *argv[]);
104
105 #if USE_GPGME
106 static void idle_function_for_gpgme(void);
107 #endif /* USE_GPGME */
108
109 static gint prohibit_duplicate_launch   (void);
110 static void lock_socket_input_cb        (gpointer          data,
111                                          gint              source,
112                                          GdkInputCondition condition);
113 static gchar *get_socket_name           (void);
114
115 static void open_compose_new_with_recipient     (const gchar    *address);
116
117 static void send_queue                  (void);
118 static void initial_processing          (FolderItem *item, gpointer data);
119
120 #if 0
121 /* for gettext */
122 _("File `%s' already exists.\n"
123   "Can't create folder.")
124 #endif
125
126 #define MAKE_DIR_IF_NOT_EXIST(dir) \
127 { \
128         if (!is_dir_exist(dir)) { \
129                 if (is_file_exist(dir)) { \
130                         alertpanel_warning \
131                                 (_("File `%s' already exists.\n" \
132                                    "Can't create folder."), \
133                                  dir); \
134                         return 1; \
135                 } \
136                 if (make_dir(dir) < 0) \
137                         return 1; \
138         } \
139 }
140
141 int main(int argc, char *argv[])
142 {
143         gchar *userrc;
144         MainWindow *mainwin;
145         FolderView *folderview;
146
147         setlocale(LC_ALL, "");
148         bindtextdomain(PACKAGE, LOCALEDIR);
149         textdomain(PACKAGE);
150
151         parse_cmd_opt(argc, argv);
152
153         gtk_set_locale();
154         gtk_init(&argc, &argv);
155
156 #if USE_THREADS || USE_LDAP
157         g_thread_init(NULL);
158         if (!g_thread_supported())
159                 g_error(_("g_thread is not supported by glib.\n"));
160 #endif
161
162 #if HAVE_GDK_IMLIB
163         gdk_imlib_init();
164         gtk_widget_push_visual(gdk_imlib_get_visual());
165         gtk_widget_push_colormap(gdk_imlib_get_colormap());
166 #endif
167
168 #if USE_SSL
169         ssl_init();
170 #endif
171
172         srandom((gint)time(NULL));
173
174         /* parse gtkrc files */
175         userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtkrc",
176                              NULL);
177         gtk_rc_parse(userrc);
178         g_free(userrc);
179         userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtk",
180                              G_DIR_SEPARATOR_S, "gtkrc", NULL);
181         gtk_rc_parse(userrc);
182         g_free(userrc);
183         userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "gtkrc", NULL);
184         gtk_rc_parse(userrc);
185         g_free(userrc);
186
187         gtk_rc_parse("./gtkrc");
188
189         userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
190         gtk_item_factory_parse_rc(userrc);
191         g_free(userrc);
192
193         prog_version = PROG_VERSION;
194         startup_dir = g_get_current_dir();
195
196         CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
197
198         /* check and create unix domain socket */
199         lock_socket = prohibit_duplicate_launch();
200         if (lock_socket < 0) return 0;
201
202         if (cmd.status) {
203                 puts("0 Sylpheed not running.");
204                 return 0;
205         }
206
207         /* backup if old rc file exists */
208         if (is_file_exist(RC_DIR)) {
209                 if (rename(RC_DIR, RC_DIR ".bak") < 0)
210                         FILE_OP_ERROR(RC_DIR, "rename");
211         }
212         MAKE_DIR_IF_NOT_EXIST(RC_DIR);
213         MAKE_DIR_IF_NOT_EXIST(get_imap_cache_dir());
214         MAKE_DIR_IF_NOT_EXIST(get_news_cache_dir());
215         MAKE_DIR_IF_NOT_EXIST(get_mime_tmp_dir());
216         MAKE_DIR_IF_NOT_EXIST(RC_DIR G_DIR_SEPARATOR_S "uidl");
217
218         if (is_file_exist(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log")) {
219                 if (rename(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log",
220                            RC_DIR G_DIR_SEPARATOR_S "sylpheed.log.bak") < 0)
221                         FILE_OP_ERROR("sylpheed.log", "rename");
222         }
223         set_log_file(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log");
224
225         if (is_file_exist(RC_DIR G_DIR_SEPARATOR_S "assortrc") &&
226             !is_file_exist(RC_DIR G_DIR_SEPARATOR_S "filterrc")) {
227                 if (rename(RC_DIR G_DIR_SEPARATOR_S "assortrc",
228                            RC_DIR G_DIR_SEPARATOR_S "filterrc") < 0)
229                         FILE_OP_ERROR(RC_DIR G_DIR_SEPARATOR_S "assortrc",
230                                       "rename");
231         }
232
233         prefs_common_init();
234         prefs_common_read_config();
235
236 #if USE_GPGME
237         if (gpgme_check_engine()) {  /* Also does some gpgme init */
238                 rfc2015_disable_all();
239                 debug_print("gpgme_engine_version:\n%s\n",
240                             gpgme_get_engine_info());
241
242                 if (prefs_common.gpg_warning) {
243                         AlertValue val;
244
245                         val = alertpanel_message_with_disable
246                                 (_("Warning"),
247                                  _("GnuPG is not installed properly.\n"
248                                    "OpenPGP support disabled."));
249                         if (val & G_ALERTDISABLE)
250                                 prefs_common.gpg_warning = FALSE;
251                 }
252         }
253         gpgme_register_idle(idle_function_for_gpgme);
254 #endif
255
256 #if USE_PSPELL
257         gtkpspellcheckers = gtkpspell_checkers_new();
258 #endif
259         
260
261         prefs_common_save_config();
262         prefs_filter_read_config();
263         prefs_filter_write_config();
264         prefs_actions_read_config();
265         prefs_actions_write_config();
266         prefs_display_header_read_config();
267         prefs_display_header_write_config();
268         /* prefs_filtering_read_config(); */
269         addressbook_read_file();
270         renderer_read_config();
271
272         gtkut_widget_init();
273
274         mainwin = main_window_create
275                 (prefs_common.sep_folder | prefs_common.sep_msg << 1);
276         folderview = mainwin->folderview;
277
278         /* register the callback of unix domain socket input */
279         lock_socket_tag = gdk_input_add(lock_socket,
280                                         GDK_INPUT_READ | GDK_INPUT_EXCEPTION,
281                                         lock_socket_input_cb,
282                                         mainwin);
283
284         account_read_config_all();
285         account_save_config_all();
286
287         if (folder_read_list() < 0) {
288                 setup(mainwin);
289                 folder_write_list();
290         }
291         if (!account_get_list()) {
292                 account_edit_open();
293                 account_add();
294         }
295
296         account_set_missing_folder();
297         folder_set_missing_folders();
298         folderview_set(folderview);
299
300         /* prefs_scoring_read_config(); */
301         prefs_matcher_read_config();
302
303         /* make one all-folder processing before using sylpheed */
304         folder_func_to_all_folders(initial_processing, (gpointer *)mainwin);
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 static void save_all_caches(FolderItem *item, gpointer data)
396 {
397         if(!item->cache)
398                 return;
399                 
400         folder_item_write_cache(item);
401 }
402
403 static void initial_processing(FolderItem *item, gpointer data)
404 {
405         MainWindow *mainwin = (MainWindow *)data;
406         gchar *buf;
407                 
408         buf = g_strdup_printf(_("Processing (%s)..."), item->path);
409         debug_print("%s\n", buf);
410         STATUSBAR_PUSH(mainwin, buf);
411         g_free(buf);
412
413         main_window_cursor_wait(mainwin);
414         
415         folder_item_apply_processing(item);
416
417         debug_print(_("done.\n"));
418         STATUSBAR_POP(mainwin);
419         main_window_cursor_normal(mainwin);
420 }
421
422 void app_will_exit(GtkWidget *widget, gpointer data)
423 {
424         MainWindow *mainwin = data;
425         gchar *filename;
426
427         if (compose_get_compose_list()) {
428                 if (alertpanel(_("Notice"),
429                                _("Composing message exists. Really quit?"),
430                                _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT)
431                         return;
432                 manage_window_focus_in(mainwin->window, NULL, NULL);
433         }
434
435         if (prefs_common.warn_queued_on_exit && get_queued_message_num() > 0) {
436                 if (alertpanel(_("Queued messages"),
437                                _("Some unsent messages are queued. Exit now?"),
438                                _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT)
439                         return;
440                 manage_window_focus_in(mainwin->window, NULL, NULL);
441         }
442
443         inc_autocheck_timer_remove();
444
445 #if USE_GPGME
446         gpgmegtk_free_passphrase();
447 #endif
448
449         if (prefs_common.clean_on_exit)
450                 main_window_empty_trash(mainwin, prefs_common.ask_on_clean);
451
452         /* save all state before exiting */
453         folder_write_list();
454         folder_func_to_all_folders(save_all_caches, NULL);
455
456         main_window_get_size(mainwin);
457         main_window_get_position(mainwin);
458         prefs_common_save_config();
459         prefs_filter_write_config();
460         account_save_config_all();
461         addressbook_export_to_file();
462
463         filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
464         gtk_item_factory_dump_rc(filename, NULL, TRUE);
465         g_free(filename);
466
467         /* delete temporary files */
468         remove_all_files(get_mime_tmp_dir());
469
470         close_log_file();
471
472         /* delete unix domain socket */
473         gdk_input_remove(lock_socket_tag);
474         fd_close(lock_socket);
475         filename = get_socket_name();
476         unlink(filename);
477
478 #if USE_SSL
479         ssl_done();
480 #endif
481
482         gtk_main_quit();
483 }
484
485 #if USE_GPGME
486 static void idle_function_for_gpgme(void)
487 {
488         while (gtk_events_pending())
489                 gtk_main_iteration();
490 }
491 #endif /* USE_GPGME */
492
493 static gchar *get_socket_name(void)
494 {
495         static gchar *filename = NULL;
496
497         if (filename == NULL) {
498                 filename = g_strdup_printf("%s%csylpheed-%d",
499                                            g_get_tmp_dir(), G_DIR_SEPARATOR,
500                                            getuid());
501         }
502
503         return filename;
504 }
505
506 static gint prohibit_duplicate_launch(void)
507 {
508         gint uxsock;
509         gchar *path;
510
511         path = get_socket_name();
512         uxsock = fd_connect_unix(path);
513         if (uxsock < 0) {
514                 unlink(path);
515                 return fd_open_unix(path);
516         }
517
518         /* remote command mode */
519
520         debug_print(_("another Sylpheed is already running.\n"));
521
522         if (cmd.receive_all)
523                 fd_write(uxsock, "receive_all\n", 12);
524         else if (cmd.receive)
525                 fd_write(uxsock, "receive\n", 8);
526         else if (cmd.compose) {
527                 gchar *compose_str;
528
529                 if (cmd.compose_mailto)
530                         compose_str = g_strdup_printf("compose %s\n", cmd.compose_mailto);
531                 else
532                         compose_str = g_strdup("compose\n");
533
534                 fd_write(uxsock, compose_str, strlen(compose_str));
535                 g_free(compose_str);
536         } else if (cmd.send) {
537                 fd_write(uxsock, "send\n", 5);
538         } else if (cmd.status) {
539                 gchar buf[BUFFSIZE];
540
541                 fd_write(uxsock, "status\n", 7);
542                 fd_gets(uxsock, buf, sizeof(buf));
543                 fputs(buf, stdout);
544         } else
545                 fd_write(uxsock, "popup\n", 6);
546
547         fd_close(uxsock);
548         return -1;
549 }
550
551 static void lock_socket_input_cb(gpointer data,
552                                  gint source,
553                                  GdkInputCondition condition)
554 {
555         MainWindow *mainwin = (MainWindow *)data;
556         gint sock;
557         gchar buf[BUFFSIZE];
558
559         sock = fd_accept(source);
560         fd_gets(sock, buf, sizeof(buf));
561
562         if (!strncmp(buf, "popup", 5)){
563                 main_window_popup(mainwin);
564         } else if (!strncmp(buf, "receive_all", 11)){
565                 main_window_popup(mainwin);
566                 inc_all_account_mail(mainwin, prefs_common.newmail_notify_manu);
567         } else if (!strncmp(buf, "receive", 7)){
568                 main_window_popup(mainwin);
569                 inc_mail(mainwin, prefs_common.newmail_notify_manu);
570         } else if (!strncmp(buf, "compose", 7)) {
571                 open_compose_new_with_recipient(buf + strlen("compose") + 1);
572         } else if (!strncmp(buf, "send", 4)) {
573                 send_queue();
574         } else if (!strncmp(buf, "status", 6)) {
575                 guint new, unread, total;
576
577                 folder_count_total_msgs(&new, &unread, &total);
578                 g_snprintf(buf, sizeof(buf), "%d %d %d\n", new, unread, total);
579                 fd_write(sock, buf, strlen(buf));
580         }
581
582         fd_close(sock);
583 }
584
585 static void open_compose_new_with_recipient(const gchar *address)
586 {
587         gchar *addr = NULL;
588
589         if (address) {
590                 Xstrdup_a(addr, address, return);
591                 g_strstrip(addr);
592         }
593
594         if (addr && *addr != '\0')
595                 compose_new_with_recipient(NULL, addr);
596         else
597                 compose_new(NULL);
598 }
599
600 static void send_queue(void)
601 {
602         GList *list;
603         FolderItem *def_outbox;
604
605         def_outbox = folder_get_default_outbox();
606
607         for (list = folder_get_list(); list != NULL; list = list->next) {
608                 Folder *folder = list->data;
609
610                 if (folder->queue) {
611                         if (procmsg_send_queue
612                                 (folder->queue, prefs_common.savemsg) < 0)
613                                 alertpanel_error(_("Some errors occurred while sending queued messages."));
614                         statusbar_pop_all();
615                         folder_item_scan(folder->queue);
616                         folderview_update_item(folder->queue, TRUE);
617                         if (prefs_common.savemsg && folder->outbox) {
618                                 folderview_update_item(folder->outbox, TRUE);
619                                 if (folder->outbox == def_outbox)
620                                         def_outbox = NULL;
621                         }
622                 }
623         }
624
625         if (prefs_common.savemsg && def_outbox)
626                 folderview_update_item(def_outbox, TRUE);
627 }