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