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