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