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