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