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