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