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