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