terminate cleanly on SIGHUP
[claws.git] / src / main.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2003 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 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <ctype.h>
34 #include <unistd.h>
35 #include <time.h>
36 #include <sys/stat.h>
37 #include <sys/types.h>
38 #include <signal.h>
39
40 #if USE_GPGME
41 #  include <gpgme.h>
42 #  include "passphrase.h"
43 #endif
44
45 #include "sylpheed.h"
46 #include "intl.h"
47 #include "main.h"
48 #include "mainwindow.h"
49 #include "folderview.h"
50 #include "summaryview.h"
51 #include "prefs_common.h"
52 #include "prefs_account.h"
53 #include "prefs_actions.h"
54 #include "prefs_ext_prog.h"
55 #include "prefs_fonts.h"
56 #include "prefs_msg_colors.h"
57 #include "prefs_spelling.h"
58 #include "prefs_themes.h"
59 #include "prefs_wrapping.h"
60 #include "prefs_display_header.h"
61 #include "account.h"
62 #include "procmsg.h"
63 #include "inc.h"
64 #include "import.h"
65 #include "manage_window.h"
66 #include "alertpanel.h"
67 #include "statusbar.h"
68 #include "addressbook.h"
69 #include "compose.h"
70 #include "folder.h"
71 #include "setup.h"
72 #include "utils.h"
73 #include "gtkutils.h"
74 #include "socket.h"
75 #include "log.h"
76 #include "prefs_toolbar.h"
77 #include "plugin.h"
78 #include "mh_gtk.h"
79 #include "imap_gtk.h"
80 #include "news_gtk.h"
81 #include "matcher.h"
82
83 #if USE_GPGME
84 #  include "sgpgme.h"
85 #  include "pgpmime.h"
86 #endif
87 #if USE_OPENSSL
88 #  include "ssl.h"
89 #endif
90
91 #include "version.h"
92
93 #include "crash.h"
94
95 gchar *prog_version;
96 #ifdef CRASH_DIALOG
97 gchar *argv0;
98 #endif
99
100 static gint lock_socket = -1;
101 static gint lock_socket_tag = 0;
102
103 typedef enum 
104 {
105         ONLINE_MODE_DONT_CHANGE,
106         ONLINE_MODE_ONLINE,
107         ONLINE_MODE_OFFLINE
108 } OnlineMode;
109
110 static struct RemoteCmd {
111         gboolean receive;
112         gboolean receive_all;
113         gboolean compose;
114         const gchar *compose_mailto;
115         GPtrArray *attach_files;
116         gboolean status;
117         gboolean status_full;
118         GPtrArray *status_folders;
119         GPtrArray *status_full_folders;
120         gboolean send;
121         gboolean crash;
122         int online_mode;
123         gchar   *crash_params;
124 } cmd;
125
126 static void parse_cmd_opt(int argc, char *argv[]);
127
128 static gint prohibit_duplicate_launch   (void);
129 static gchar * get_crashfile_name       (void);
130 static gint lock_socket_remove          (void);
131 static void lock_socket_input_cb        (gpointer          data,
132                                          gint              source,
133                                          GdkInputCondition condition);
134 #ifndef CLAWS                                    
135 static 
136 #endif
137 gchar *get_socket_name          (void);
138
139
140 static void open_compose_new            (const gchar    *address,
141                                          GPtrArray      *attach_files);
142
143 static void send_queue                  (void);
144 static void initial_processing          (FolderItem *item, gpointer data);
145 static void quit_signal_handler         (int sig);
146 static void install_basic_sighandlers   (void);
147 static void exit_sylpheed               (MainWindow *mainwin);
148
149 #if 0
150 /* for gettext */
151 _("File `%s' already exists.\n"
152   "Can't create folder.")
153 #endif
154
155 #define MAKE_DIR_IF_NOT_EXIST(dir) \
156 { \
157         if (!is_dir_exist(dir)) { \
158                 if (is_file_exist(dir)) { \
159                         alertpanel_warning \
160                                 (_("File `%s' already exists.\n" \
161                                    "Can't create folder."), \
162                                  dir); \
163                         return 1; \
164                 } \
165                 if (make_dir(dir) < 0) \
166                         return 1; \
167         } \
168 }
169
170 static MainWindow *static_mainwindow;
171
172 int main(int argc, char *argv[])
173 {
174         gchar *userrc;
175         MainWindow *mainwin;
176         FolderView *folderview;
177
178         if (!sylpheed_init(&argc, &argv)) {
179                 return 0;
180         }
181
182         prog_version = PROG_VERSION;
183 #ifdef CRASH_DIALOG
184         argv0 = g_strdup(argv[0]);
185 #endif
186
187         parse_cmd_opt(argc, argv);
188
189 #ifdef CRASH_DIALOG
190         if (cmd.crash) {
191                 gtk_set_locale();
192                 gtk_init(&argc, &argv);
193                 crash_main(cmd.crash_params);
194                 return 0;
195         }
196         crash_install_handlers();
197 #endif
198         install_basic_sighandlers();
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 || cmd.status_full) {
205                 puts("0 Sylpheed not running.");
206                 lock_socket_remove();
207                 return 0;
208         }
209
210         gtk_set_locale();
211         gtk_init(&argc, &argv);
212
213         gdk_rgb_init();
214         gtk_widget_set_default_colormap(gdk_rgb_get_cmap());
215         gtk_widget_set_default_visual(gdk_rgb_get_visual());
216
217 #if USE_THREADS || USE_LDAP
218         g_thread_init(NULL);
219         if (!g_thread_supported())
220                 g_error(_("g_thread is not supported by glib.\n"));
221 #endif
222
223         /* parse gtkrc files */
224         userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtkrc",
225                              NULL);
226         gtk_rc_parse(userrc);
227         g_free(userrc);
228         userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtk",
229                              G_DIR_SEPARATOR_S, "gtkrc", NULL);
230         gtk_rc_parse(userrc);
231         g_free(userrc);
232         userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "gtkrc", NULL);
233         gtk_rc_parse(userrc);
234         g_free(userrc);
235
236         gtk_rc_parse("./gtkrc");
237
238         userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
239         gtk_item_factory_parse_rc(userrc);
240         g_free(userrc);
241
242         CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
243
244         MAKE_DIR_IF_NOT_EXIST(RC_DIR);
245         MAKE_DIR_IF_NOT_EXIST(get_imap_cache_dir());
246         MAKE_DIR_IF_NOT_EXIST(get_news_cache_dir());
247         MAKE_DIR_IF_NOT_EXIST(get_mime_tmp_dir());
248         MAKE_DIR_IF_NOT_EXIST(get_tmp_dir());
249         MAKE_DIR_IF_NOT_EXIST(RC_DIR G_DIR_SEPARATOR_S "uidl");
250
251         set_log_file(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log");
252
253         folder_system_init();
254         prefs_common_init();
255         prefs_common_read_config();
256
257 #if USE_GPGME
258         sgpgme_init();
259         pgpmime_init();
260 #endif
261         prefs_themes_init();
262         prefs_fonts_init();
263         prefs_ext_prog_init();
264         prefs_wrapping_init();
265         prefs_msg_colors_init();
266 #ifdef USE_ASPELL
267         gtkaspell_checkers_init();
268         prefs_spelling_init();
269 #endif
270         
271         sock_set_io_timeout(prefs_common.io_timeout_secs);
272
273         prefs_actions_read_config();
274         prefs_display_header_read_config();
275         /* prefs_filtering_read_config(); */
276         addressbook_read_file();
277         renderer_read_config();
278
279         gtkut_widget_init();
280
281         folderview_initialize();
282         mh_gtk_init();
283         imap_gtk_init();
284         news_gtk_init();
285         mainwin = main_window_create
286                 (prefs_common.sep_folder | prefs_common.sep_msg << 1);
287         folderview = mainwin->folderview;
288
289         /* register the callback of unix domain socket input */
290         lock_socket_tag = gdk_input_add(lock_socket,
291                                         GDK_INPUT_READ | GDK_INPUT_EXCEPTION,
292                                         lock_socket_input_cb,
293                                         mainwin);
294
295         account_read_config_all();
296
297         if (folder_read_list() < 0) {
298                 setup(mainwin);
299                 folder_write_list();
300         }
301         if (!account_get_list()) {
302                 account_edit_open();
303                 account_add();
304         }
305
306         account_set_missing_folder();
307         folder_set_missing_folders();
308         folderview_set(folderview);
309
310         prefs_matcher_read_config();
311
312         /* make one all-folder processing before using sylpheed */
313         folder_func_to_all_folders(initial_processing, (gpointer *)mainwin);
314
315         /* if Sylpheed crashed, rebuild caches */
316         if (!cmd.crash && is_file_exist(get_crashfile_name())) {
317                 debug_print("Sylpheed crashed, checking for new messages in local folders\n");
318                 folderview_check_new(NULL);
319         }
320         /* make the crash-indicator file */
321         str_write_to_file("foo", get_crashfile_name());
322
323         addressbook_read_file();
324
325         inc_autocheck_timer_init(mainwin);
326
327         /* ignore SIGPIPE signal for preventing sudden death of program */
328         signal(SIGPIPE, SIG_IGN);
329
330         if (cmd.online_mode == ONLINE_MODE_OFFLINE)
331                 main_window_toggle_work_offline(mainwin, TRUE);
332         if (cmd.online_mode == ONLINE_MODE_ONLINE)
333                 main_window_toggle_work_offline(mainwin, FALSE);
334
335         if (cmd.receive_all)
336                 inc_all_account_mail(mainwin, FALSE, 
337                                      prefs_common.newmail_notify_manu);
338         else if (prefs_common.chk_on_startup)
339                 inc_all_account_mail(mainwin, TRUE, 
340                                      prefs_common.newmail_notify_manu);
341         else if (cmd.receive)
342                 inc_mail(mainwin, prefs_common.newmail_notify_manu);
343         else
344                 gtk_widget_grab_focus(folderview->ctree);
345
346         if (cmd.compose)
347                 open_compose_new(cmd.compose_mailto, cmd.attach_files);
348         if (cmd.attach_files) {
349                 ptr_array_free_strings(cmd.attach_files);
350                 g_ptr_array_free(cmd.attach_files, TRUE);
351                 cmd.attach_files = NULL;
352         }
353         if (cmd.send)
354                 send_queue();
355         if (cmd.status_folders) {
356                 g_ptr_array_free(cmd.status_folders, TRUE);
357                 cmd.status_folders = NULL;
358         }
359         if (cmd.status_full_folders) {
360                 g_ptr_array_free(cmd.status_full_folders, TRUE);
361                 cmd.status_full_folders = NULL;
362         }
363
364         prefs_toolbar_init();
365
366         plugin_load_all("GTK");
367         
368         static_mainwindow = mainwin;
369         gtk_main();
370
371         exit_sylpheed(mainwin);
372
373         return 0;
374 }
375
376 static void save_all_caches(FolderItem *item, gpointer data)
377 {
378         if (!item->cache)
379                 return;
380         folder_item_write_cache(item);
381 }
382
383 static void exit_sylpheed(MainWindow *mainwin)
384 {
385         gchar *filename;
386
387         debug_print("shutting down\n");
388
389         inc_autocheck_timer_remove();
390
391         if (prefs_common.clean_on_exit)
392                 main_window_empty_trash(mainwin, prefs_common.ask_on_clean);
393
394         /* save prefs for opened folder */
395         if(mainwin->folderview->opened)
396         {
397                 FolderItem *item;
398
399                 item = gtk_ctree_node_get_row_data(GTK_CTREE(mainwin->folderview->ctree), mainwin->folderview->opened);
400                 summary_save_prefs_to_folderitem(mainwin->folderview->summaryview, item);
401         }
402
403         /* save all state before exiting */
404         folder_write_list();
405         folder_func_to_all_folders(save_all_caches, NULL);
406
407         main_window_get_size(mainwin);
408         main_window_get_position(mainwin);
409         prefs_common_save_config();
410         account_save_config_all();
411         addressbook_export_to_file();
412
413         filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
414         gtk_item_factory_dump_rc(filename, NULL, TRUE);
415         g_free(filename);
416
417         /* delete temporary files */
418         remove_all_files(get_mime_tmp_dir());
419
420         close_log_file();
421
422         /* delete crashfile */
423         if (!cmd.crash)
424                 unlink(get_crashfile_name());
425
426         lock_socket_remove();
427
428         main_window_destroy(mainwin);
429         
430         plugin_unload_all("GTK");
431
432         prefs_toolbar_done();
433
434         addressbook_destroy();
435
436 #ifdef USE_GPGME
437         pgpmime_done();
438         sgpgme_done();
439 #endif
440         prefs_themes_done();
441         prefs_fonts_done();
442         prefs_ext_prog_done();
443         prefs_wrapping_done();
444         prefs_msg_colors_done();
445 #ifdef USE_ASPELL       
446         prefs_spelling_done();
447         gtkaspell_checkers_quit();
448 #endif
449         sylpheed_done();
450
451 }
452
453 static void parse_cmd_opt(int argc, char *argv[])
454 {
455         gint i;
456
457         for (i = 1; i < argc; i++) {
458                 if (!strncmp(argv[i], "--receive-all", 13))
459                         cmd.receive_all = TRUE;
460                 else if (!strncmp(argv[i], "--receive", 9))
461                         cmd.receive = TRUE;
462                 else if (!strncmp(argv[i], "--compose", 9)) {
463                         const gchar *p = argv[i + 1];
464
465                         cmd.compose = TRUE;
466                         cmd.compose_mailto = NULL;
467                         if (p && *p != '\0' && *p != '-') {
468                                 if (!strncmp(p, "mailto:", 7))
469                                         cmd.compose_mailto = p + 7;
470                                 else
471                                         cmd.compose_mailto = p;
472                                 i++;
473                         }
474                 } else if (!strncmp(argv[i], "--attach", 8)) {
475                         const gchar *p = argv[i + 1];
476                         gchar *file;
477
478                         while (p && *p != '\0' && *p != '-') {
479                                 if (!cmd.attach_files)
480                                         cmd.attach_files = g_ptr_array_new();
481                                 if (*p != G_DIR_SEPARATOR)
482                                         file = g_strconcat(sylpheed_get_startup_dir(),
483                                                            G_DIR_SEPARATOR_S,
484                                                            p, NULL);
485                                 else
486                                         file = g_strdup(p);
487                                 g_ptr_array_add(cmd.attach_files, file);
488                                 i++;
489                                 p = argv[i + 1];
490                         }
491                 } else if (!strncmp(argv[i], "--send", 6)) {
492                         cmd.send = TRUE;
493                 } else if (!strncmp(argv[i], "--version", 9)) {
494                         puts("Sylpheed version " VERSION);
495                         exit(0);
496                 } else if (!strncmp(argv[i], "--status-full", 13)) {
497                         const gchar *p = argv[i + 1];
498  
499                         cmd.status_full = TRUE;
500                         while (p && *p != '\0' && *p != '-') {
501                                 if (!cmd.status_full_folders)
502                                         cmd.status_full_folders =
503                                                 g_ptr_array_new();
504                                 g_ptr_array_add(cmd.status_full_folders,
505                                                 g_strdup(p));
506                                 i++;
507                                 p = argv[i + 1];
508                         }
509                 } else if (!strncmp(argv[i], "--status", 8)) {
510                         const gchar *p = argv[i + 1];
511  
512                         cmd.status = TRUE;
513                         while (p && *p != '\0' && *p != '-') {
514                                 if (!cmd.status_folders)
515                                         cmd.status_folders = g_ptr_array_new();
516                                 g_ptr_array_add(cmd.status_folders,
517                                                 g_strdup(p));
518                                 i++;
519                                 p = argv[i + 1];
520                         }
521                 } else if (!strncmp(argv[i], "--online", 8)) {
522                         cmd.online_mode = ONLINE_MODE_ONLINE;
523                 } else if (!strncmp(argv[i], "--offline", 9)) {
524                         cmd.online_mode = ONLINE_MODE_OFFLINE;
525                 } else if (!strncmp(argv[i], "--help", 6)) {
526                         g_print(_("Usage: %s [OPTION]...\n"),
527                                 g_basename(argv[0]));
528
529                         puts(_("  --compose [address]    open composition window"));
530                         puts(_("  --attach file1 [file2]...\n"
531                                "                         open composition window with specified files\n"
532                                "                         attached"));
533                         puts(_("  --receive              receive new messages"));
534                         puts(_("  --receive-all          receive new messages of all accounts"));
535                         puts(_("  --send                 send all queued messages"));
536                         puts(_("  --status [folder]...   show the total number of messages"));
537                         puts(_("  --status-full [folder]...\n"
538                                "                         show the status of each folder"));
539                         puts(_("  --online               switch to online mode"));
540                         puts(_("  --offline              switch to offline mode"));
541                         puts(_("  --debug                debug mode"));
542                         puts(_("  --help                 display this help and exit"));
543                         puts(_("  --version              output version information and exit"));
544                         puts(_("  --config-dir           output configuration directory"));
545
546                         exit(1);
547                 } else if (!strncmp(argv[i], "--crash", 7)) {
548                         cmd.crash = TRUE;
549                         cmd.crash_params = g_strdup(argv[i + 1]);
550                         i++;
551                 } else if (!strncmp(argv[i], "--config-dir", sizeof "--config-dir" - 1)) {
552                         puts(RC_DIR);
553                         exit(0);
554                 }
555                 
556         }
557
558         if (cmd.attach_files && cmd.compose == FALSE) {
559                 cmd.compose = TRUE;
560                 cmd.compose_mailto = NULL;
561         }
562 }
563
564 static gint get_queued_message_num(void)
565 {
566         FolderItem *queue;
567
568         queue = folder_get_default_queue();
569         if (!queue) return -1;
570
571         folder_item_scan(queue);
572         return queue->total_msgs;
573 }
574
575 static void initial_processing(FolderItem *item, gpointer data)
576 {
577         MainWindow *mainwin = (MainWindow *)data;
578         gchar *buf;
579
580         g_return_if_fail(item);
581         buf = g_strdup_printf(_("Processing (%s)..."), 
582                               item->path 
583                               ? item->path 
584                               : _("top level folder"));
585         debug_print("%s\n", buf);
586         g_free(buf);
587
588         main_window_cursor_wait(mainwin);
589         
590         if (item->prefs->enable_processing)
591                 folder_item_apply_processing(item);
592
593         debug_print("done.\n");
594         STATUSBAR_POP(mainwin);
595         main_window_cursor_normal(mainwin);
596 }
597
598 static void draft_all_messages(void)
599 {
600         GList *compose_list = compose_get_compose_list();
601         GList *elem = NULL;
602         
603         if (compose_list) {
604                 for (elem = compose_list; elem != NULL && elem->data != NULL; 
605                      elem = elem->next) {
606                         Compose *c = (Compose*)elem->data;
607                         compose_draft(c);
608                 }
609         }       
610 }
611
612 gboolean clean_quit(gpointer data)
613 {
614         static gboolean firstrun = TRUE;
615
616         if (!firstrun)
617                 return FALSE;
618         firstrun = FALSE;
619
620         /*!< Good idea to have the main window stored in a 
621          *   static variable so we can check that variable
622          *   to see if we're really allowed to do things
623          *   that actually the spawner is supposed to 
624          *   do (like: sending mail, composing messages).
625          *   Because, really, if we're the spawnee, and
626          *   we touch GTK stuff, we're hosed. See the 
627          *   next fixme. */
628
629         /* FIXME: Use something else to signal that we're
630          * in the original spawner, and not in a spawned
631          * child. */
632         if (!static_mainwindow) 
633                 return FALSE;
634                 
635         draft_all_messages();
636
637         exit_sylpheed(static_mainwindow);
638         exit(0);
639
640         return FALSE;
641 }
642
643 void app_will_exit(GtkWidget *widget, gpointer data)
644 {
645         MainWindow *mainwin = data;
646         
647         if (compose_get_compose_list()) {
648                 gint val = alertpanel(_("Notice"),
649                                _("Composing message exists."),
650                                _("Draft them"), _("Discard them"), _("Don't quit"));
651                 switch (val) {
652                         case G_ALERTOTHER:
653                                 return;
654                         case G_ALERTALTERNATE:
655                                 break;
656                         default:
657                                 draft_all_messages();
658                 }
659                 
660                 manage_window_focus_in(mainwin->window, NULL, NULL);
661         }
662
663         if (prefs_common.warn_queued_on_exit && get_queued_message_num() > 0) {
664                 if (alertpanel(_("Queued messages"),
665                                _("Some unsent messages are queued. Exit now?"),
666                                _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT)
667                         return;
668                 manage_window_focus_in(mainwin->window, NULL, NULL);
669         }
670         gtk_main_quit();
671 }
672
673 /*
674  * CLAWS: want this public so crash dialog can delete the
675  * lock file too
676  */
677 #ifndef CLAWS
678 static
679 #endif
680 gchar *get_socket_name(void)
681 {
682         static gchar *filename = NULL;
683
684         if (filename == NULL) {
685                 filename = g_strdup_printf("%s%csylpheed-%d",
686                                            g_get_tmp_dir(), G_DIR_SEPARATOR,
687                                            getuid());
688         }
689
690         return filename;
691 }
692
693 static gchar *get_crashfile_name(void)
694 {
695         static gchar *filename = NULL;
696
697         if (filename == NULL) {
698                 filename = g_strdup_printf("%s%csylpheed-crashed",
699                                            get_tmp_dir(), G_DIR_SEPARATOR);
700         }
701
702         return filename;
703 }
704
705 static gint prohibit_duplicate_launch(void)
706 {
707         gint uxsock;
708         gchar *path;
709
710         path = get_socket_name();
711         uxsock = fd_connect_unix(path);
712         if (uxsock < 0) {
713                 unlink(path);
714                 return fd_open_unix(path);
715         }
716
717         /* remote command mode */
718
719         debug_print("another Sylpheed is already running.\n");
720
721         if (cmd.receive_all)
722                 fd_write_all(uxsock, "receive_all\n", 12);
723         else if (cmd.receive)
724                 fd_write_all(uxsock, "receive\n", 8);
725         else if (cmd.compose && cmd.attach_files) {
726                 gchar *str, *compose_str;
727                 gint i;
728
729                 if (cmd.compose_mailto)
730                         compose_str = g_strdup_printf("compose_attach %s\n",
731                                                       cmd.compose_mailto);
732                 else
733                         compose_str = g_strdup("compose_attach\n");
734
735                 fd_write_all(uxsock, compose_str, strlen(compose_str));
736                 g_free(compose_str);
737
738                 for (i = 0; i < cmd.attach_files->len; i++) {
739                         str = g_ptr_array_index(cmd.attach_files, i);
740                         fd_write_all(uxsock, str, strlen(str));
741                         fd_write_all(uxsock, "\n", 1);
742                 }
743
744                 fd_write_all(uxsock, ".\n", 2);
745         } else if (cmd.compose) {
746                 gchar *compose_str;
747
748                 if (cmd.compose_mailto)
749                         compose_str = g_strdup_printf
750                                 ("compose %s\n", cmd.compose_mailto);
751                 else
752                         compose_str = g_strdup("compose\n");
753
754                 fd_write_all(uxsock, compose_str, strlen(compose_str));
755                 g_free(compose_str);
756         } else if (cmd.send) {
757                 fd_write_all(uxsock, "send\n", 5);
758         } else if (cmd.online_mode == ONLINE_MODE_ONLINE) {
759                 fd_write(uxsock, "online\n", 6);
760         } else if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
761                 fd_write(uxsock, "offline\n", 7);
762         } else if (cmd.status || cmd.status_full) {
763                 gchar buf[BUFFSIZE];
764                 gint i;
765                 const gchar *command;
766                 GPtrArray *folders;
767                 gchar *folder;
768  
769                 command = cmd.status_full ? "status-full\n" : "status\n";
770                 folders = cmd.status_full ? cmd.status_full_folders :
771                         cmd.status_folders;
772  
773                 fd_write_all(uxsock, command, strlen(command));
774                 for (i = 0; folders && i < folders->len; ++i) {
775                         folder = g_ptr_array_index(folders, i);
776                         fd_write_all(uxsock, folder, strlen(folder));
777                         fd_write_all(uxsock, "\n", 1);
778                 }
779                 fd_write_all(uxsock, ".\n", 2);
780                 for (;;) {
781                         fd_gets(uxsock, buf, sizeof(buf));
782                         if (!strncmp(buf, ".\n", 2)) break;
783                         fputs(buf, stdout);
784                 }
785         } else
786                 fd_write_all(uxsock, "popup\n", 6);
787
788         fd_close(uxsock);
789         return -1;
790 }
791
792 static gint lock_socket_remove(void)
793 {
794         gchar *filename;
795
796         if (lock_socket < 0) return -1;
797
798         if (lock_socket_tag > 0)
799                 gdk_input_remove(lock_socket_tag);
800         fd_close(lock_socket);
801         filename = get_socket_name();
802         unlink(filename);
803
804         return 0;
805 }
806
807 static GPtrArray *get_folder_item_list(gint sock)
808 {
809         gchar buf[BUFFSIZE];
810         FolderItem *item;
811         GPtrArray *folders = NULL;
812
813         for (;;) {
814                 fd_gets(sock, buf, sizeof(buf));
815                 if (!strncmp(buf, ".\n", 2)) break;
816                 strretchomp(buf);
817                 if (!folders) folders = g_ptr_array_new();
818                 item = folder_find_item_from_identifier(buf);
819                 if (item)
820                         g_ptr_array_add(folders, item);
821                 else
822                         g_warning("no such folder: %s\n", buf);
823         }
824
825         return folders;
826 }
827
828 static void lock_socket_input_cb(gpointer data,
829                                  gint source,
830                                  GdkInputCondition condition)
831 {
832         MainWindow *mainwin = (MainWindow *)data;
833         gint sock;
834         gchar buf[BUFFSIZE];
835
836         sock = fd_accept(source);
837         fd_gets(sock, buf, sizeof(buf));
838
839         if (!strncmp(buf, "popup", 5)) {
840                 main_window_popup(mainwin);
841         } else if (!strncmp(buf, "receive_all", 11)) {
842                 inc_all_account_mail(mainwin, FALSE,
843                                      prefs_common.newmail_notify_manu);
844         } else if (!strncmp(buf, "receive", 7)) {
845                 inc_mail(mainwin, prefs_common.newmail_notify_manu);
846         } else if (!strncmp(buf, "compose_attach", 14)) {
847                 GPtrArray *files;
848                 gchar *mailto;
849
850                 mailto = g_strdup(buf + strlen("compose_attach") + 1);
851                 files = g_ptr_array_new();
852                 while (fd_gets(sock, buf, sizeof(buf)) > 0) {
853                         if (buf[0] == '.' && buf[1] == '\n') break;
854                         strretchomp(buf);
855                         g_ptr_array_add(files, g_strdup(buf));
856                 }
857                 open_compose_new(mailto, files);
858                 ptr_array_free_strings(files);
859                 g_ptr_array_free(files, TRUE);
860                 g_free(mailto);
861         } else if (!strncmp(buf, "compose", 7)) {
862                 open_compose_new(buf + strlen("compose") + 1, NULL);
863         } else if (!strncmp(buf, "send", 4)) {
864                 send_queue();
865         } else if (!strncmp(buf, "online", 6)) {
866                 main_window_toggle_work_offline(mainwin, FALSE);
867         } else if (!strncmp(buf, "offline", 7)) {
868                 main_window_toggle_work_offline(mainwin, TRUE);
869         } else if (!strncmp(buf, "status-full", 11) ||
870                    !strncmp(buf, "status", 6)) {
871                 gchar *status;
872                 GPtrArray *folders;
873  
874                 folders = get_folder_item_list(sock);
875                 status = folder_get_status
876                         (folders, !strncmp(buf, "status-full", 11));
877                 fd_write_all(sock, status, strlen(status));
878                 fd_write_all(sock, ".\n", 2);
879                 g_free(status);
880                 if (folders) g_ptr_array_free(folders, TRUE);
881         }
882
883         fd_close(sock);
884 }
885
886 static void open_compose_new(const gchar *address, GPtrArray *attach_files)
887 {
888         gchar *addr = NULL;
889
890         if (address) {
891                 Xstrdup_a(addr, address, return);
892                 g_strstrip(addr);
893         }
894
895         compose_new(NULL, addr, attach_files);
896 }
897
898 static void send_queue(void)
899 {
900         GList *list;
901
902         for (list = folder_get_list(); list != NULL; list = list->next) {
903                 Folder *folder = list->data;
904
905                 if (folder->queue) {
906                         gint res = procmsg_send_queue
907                                 (folder->queue, prefs_common.savemsg);
908
909                         if (res < 0)    
910                                 alertpanel_error(_("Some errors occurred while sending queued messages."));
911                         if (res)        
912                                 folder_item_scan(folder->queue);
913                 }
914         }
915 }
916
917 static void quit_signal_handler(int sig)
918 {
919         debug_print("Quitting on signal %d\n", sig);
920
921         g_timeout_add(0, clean_quit, NULL);
922 }
923
924 static void install_basic_sighandlers()
925 {
926         sigset_t    mask;
927         struct sigaction act;
928
929         sigemptyset(&mask);
930
931 #ifdef SIGTERM
932         sigaddset(&mask, SIGTERM);
933 #endif
934 #ifdef SIGINT
935         sigaddset(&mask, SIGINT);
936 #endif
937 #ifdef SIGHUP
938         sigaddset(&mask, SIGHUP);
939 #endif
940
941         act.sa_handler = quit_signal_handler;
942         act.sa_mask    = mask;
943         act.sa_flags   = 0;
944
945 #ifdef SIGTERM
946         sigaction(SIGTERM, &act, 0);
947 #endif
948 #ifdef SIGINT
949         sigaction(SIGINT, &act, 0);
950 #endif  
951 #ifdef SIGHUP
952         sigaction(SIGHUP, &act, 0);
953 #endif  
954
955         sigprocmask(SIG_UNBLOCK, &mask, 0);
956 }