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