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