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