sync with 0.9.4cvs13
[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_spelling.h"
55 #include "scoring.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 "rfc2015.h"
77 #endif
78 #if USE_OPENSSL
79 #  include "ssl.h"
80 #endif
81
82 #include "version.h"
83
84 #include "crash.h"
85
86 gchar *prog_version;
87 #ifdef CRASH_DIALOG
88 gchar *argv0;
89 #endif
90
91 static gint lock_socket = -1;
92 static gint lock_socket_tag = 0;
93
94 typedef enum 
95 {
96         ONLINE_MODE_DONT_CHANGE,
97         ONLINE_MODE_ONLINE,
98         ONLINE_MODE_OFFLINE
99 } OnlineMode;
100
101 static struct RemoteCmd {
102         gboolean receive;
103         gboolean receive_all;
104         gboolean compose;
105         const gchar *compose_mailto;
106         GPtrArray *attach_files;
107         gboolean status;
108         gboolean status_full;
109         GPtrArray *status_folders;
110         GPtrArray *status_full_folders;
111         gboolean send;
112         gboolean crash;
113         int online_mode;
114         gchar   *crash_params;
115 } cmd;
116
117 static void parse_cmd_opt(int argc, char *argv[]);
118
119 #if USE_GPGME
120 static void idle_function_for_gpgme(void);
121 #endif /* USE_GPGME */
122
123 static gint prohibit_duplicate_launch   (void);
124 static gchar * get_crashfile_name       (void);
125 static gint lock_socket_remove          (void);
126 static void lock_socket_input_cb        (gpointer          data,
127                                          gint              source,
128                                          GdkInputCondition condition);
129 #ifndef CLAWS                                    
130 static 
131 #endif
132 gchar *get_socket_name          (void);
133
134
135 static void open_compose_new            (const gchar    *address,
136                                          GPtrArray      *attach_files);
137
138 static void send_queue                  (void);
139 static void initial_processing          (FolderItem *item, gpointer data);
140 static void quit_signal_handler         (int sig);
141 static void install_basic_sighandlers   (void);
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         gpg_started = FALSE;
253         if (gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP) != 
254                         GPGME_No_Error) {  /* Also does some gpgme init */
255                 rfc2015_disable_all();
256                 debug_print("gpgme_engine_version:\n%s\n",
257                             gpgme_get_engine_info());
258
259                 if (prefs_common.gpg_warning) {
260                         AlertValue val;
261
262                         val = alertpanel_message_with_disable
263                                 (_("Warning"),
264                                  _("GnuPG is not installed properly, or needs to be upgraded.\n"
265                                    "OpenPGP support disabled."));
266                         if (val & G_ALERTDISABLE)
267                                 prefs_common.gpg_warning = FALSE;
268                 }
269         } else
270                 gpg_started = TRUE;
271
272         gpgme_register_idle(idle_function_for_gpgme);
273 #endif
274
275 #ifdef USE_ASPELL
276         gtkaspell_checkers_init();
277         prefs_spelling_init();
278 #endif
279         
280         sock_set_io_timeout(prefs_common.io_timeout_secs);
281
282         prefs_actions_read_config();
283         prefs_display_header_read_config();
284         /* prefs_filtering_read_config(); */
285         addressbook_read_file();
286         renderer_read_config();
287
288         gtkut_widget_init();
289
290         mainwin = main_window_create
291                 (prefs_common.sep_folder | prefs_common.sep_msg << 1);
292         folderview = mainwin->folderview;
293
294         /* register the callback of unix domain socket input */
295         lock_socket_tag = gdk_input_add(lock_socket,
296                                         GDK_INPUT_READ | GDK_INPUT_EXCEPTION,
297                                         lock_socket_input_cb,
298                                         mainwin);
299
300         account_read_config_all();
301
302         if (folder_read_list() < 0) {
303                 setup(mainwin);
304                 folder_write_list();
305         }
306         if (!account_get_list()) {
307                 account_edit_open();
308                 account_add();
309         }
310
311         account_set_missing_folder();
312         folder_set_missing_folders();
313         folderview_set(folderview);
314
315         /* prefs_scoring_read_config(); */
316         prefs_matcher_read_config();
317
318         /* make one all-folder processing before using sylpheed */
319         folder_func_to_all_folders(initial_processing, (gpointer *)mainwin);
320
321         /* if Sylpheed crashed, rebuild caches */
322         if (!cmd.crash && is_file_exist(get_crashfile_name())) {
323                 debug_print("Sylpheed crashed, checking for new messages in local folders\n");
324                 folderview_check_new(NULL);
325         }
326         /* make the crash-indicator file */
327         str_write_to_file("foo", get_crashfile_name());
328
329         addressbook_read_file();
330
331         inc_autocheck_timer_init(mainwin);
332
333         /* ignore SIGPIPE signal for preventing sudden death of program */
334         signal(SIGPIPE, SIG_IGN);
335
336         if (cmd.receive_all)
337                 inc_all_account_mail(mainwin, FALSE, 
338                                      prefs_common.newmail_notify_manu);
339         else if (prefs_common.chk_on_startup)
340                 inc_all_account_mail(mainwin, TRUE, 
341                                      prefs_common.newmail_notify_manu);
342         else if (cmd.receive)
343                 inc_mail(mainwin, prefs_common.newmail_notify_manu);
344         else
345                 gtk_widget_grab_focus(folderview->ctree);
346
347         if (cmd.compose)
348                 open_compose_new(cmd.compose_mailto, cmd.attach_files);
349         if (cmd.attach_files) {
350                 ptr_array_free_strings(cmd.attach_files);
351                 g_ptr_array_free(cmd.attach_files, TRUE);
352                 cmd.attach_files = NULL;
353         }
354         if (cmd.send)
355                 send_queue();
356         if (cmd.status_folders) {
357                 g_ptr_array_free(cmd.status_folders, TRUE);
358                 cmd.status_folders = NULL;
359         }
360         if (cmd.status_full_folders) {
361                 g_ptr_array_free(cmd.status_full_folders, TRUE);
362                 cmd.status_full_folders = NULL;
363         }
364
365         if (cmd.online_mode == ONLINE_MODE_OFFLINE)
366                 main_window_toggle_work_offline(mainwin, TRUE);
367         if (cmd.online_mode == ONLINE_MODE_ONLINE)
368                 main_window_toggle_work_offline(mainwin, FALSE);
369
370         prefs_toolbar_init();
371
372         plugin_load_all("GTK");
373         
374         static_mainwindow = mainwin;
375         gtk_main();
376
377         plugin_unload_all("GTK");
378
379         prefs_toolbar_done();
380
381         addressbook_destroy();
382
383 #ifdef USE_ASPELL       
384         prefs_spelling_done();
385         gtkaspell_checkers_quit();
386 #endif
387         sylpheed_done();
388
389         return 0;
390 }
391
392 static void parse_cmd_opt(int argc, char *argv[])
393 {
394         gint i;
395
396         for (i = 1; i < argc; i++) {
397                 if (!strncmp(argv[i], "--receive-all", 13))
398                         cmd.receive_all = TRUE;
399                 else if (!strncmp(argv[i], "--receive", 9))
400                         cmd.receive = TRUE;
401                 else if (!strncmp(argv[i], "--compose", 9)) {
402                         const gchar *p = argv[i + 1];
403
404                         cmd.compose = TRUE;
405                         cmd.compose_mailto = NULL;
406                         if (p && *p != '\0' && *p != '-') {
407                                 if (!strncmp(p, "mailto:", 7))
408                                         cmd.compose_mailto = p + 7;
409                                 else
410                                         cmd.compose_mailto = p;
411                                 i++;
412                         }
413                 } else if (!strncmp(argv[i], "--attach", 8)) {
414                         const gchar *p = argv[i + 1];
415                         gchar *file;
416
417                         while (p && *p != '\0' && *p != '-') {
418                                 if (!cmd.attach_files)
419                                         cmd.attach_files = g_ptr_array_new();
420                                 if (*p != G_DIR_SEPARATOR)
421                                         file = g_strconcat(sylpheed_get_startup_dir(),
422                                                            G_DIR_SEPARATOR_S,
423                                                            p, NULL);
424                                 else
425                                         file = g_strdup(p);
426                                 g_ptr_array_add(cmd.attach_files, file);
427                                 i++;
428                                 p = argv[i + 1];
429                         }
430                 } else if (!strncmp(argv[i], "--send", 6)) {
431                         cmd.send = TRUE;
432                 } else if (!strncmp(argv[i], "--version", 9)) {
433                         puts("Sylpheed version " VERSION);
434                         exit(0);
435                 } else if (!strncmp(argv[i], "--status-full", 13)) {
436                         const gchar *p = argv[i + 1];
437  
438                         cmd.status_full = TRUE;
439                         while (p && *p != '\0' && *p != '-') {
440                                 if (!cmd.status_full_folders)
441                                         cmd.status_full_folders =
442                                                 g_ptr_array_new();
443                                 g_ptr_array_add(cmd.status_full_folders,
444                                                 g_strdup(p));
445                                 i++;
446                                 p = argv[i + 1];
447                         }
448                 } else if (!strncmp(argv[i], "--status", 8)) {
449                         const gchar *p = argv[i + 1];
450  
451                         cmd.status = TRUE;
452                         while (p && *p != '\0' && *p != '-') {
453                                 if (!cmd.status_folders)
454                                         cmd.status_folders = g_ptr_array_new();
455                                 g_ptr_array_add(cmd.status_folders,
456                                                 g_strdup(p));
457                                 i++;
458                                 p = argv[i + 1];
459                         }
460                 } else if (!strncmp(argv[i], "--online", 8)) {
461                         cmd.online_mode = ONLINE_MODE_ONLINE;
462                 } else if (!strncmp(argv[i], "--offline", 9)) {
463                         cmd.online_mode = ONLINE_MODE_OFFLINE;
464                 } else if (!strncmp(argv[i], "--help", 6)) {
465                         g_print(_("Usage: %s [OPTION]...\n"),
466                                 g_basename(argv[0]));
467
468                         puts(_("  --compose [address]    open composition window"));
469                         puts(_("  --attach file1 [file2]...\n"
470                                "                         open composition window with specified files\n"
471                                "                         attached"));
472                         puts(_("  --receive              receive new messages"));
473                         puts(_("  --receive-all          receive new messages of all accounts"));
474                         puts(_("  --send                 send all queued messages"));
475                         puts(_("  --status [folder]...   show the total number of messages"));
476                         puts(_("  --status-full [folder]...\n"
477                                "                         show the status of each folder"));
478                         puts(_("  --online               switch to online mode"));
479                         puts(_("  --offline              switch to offline mode"));
480                         puts(_("  --debug                debug mode"));
481                         puts(_("  --help                 display this help and exit"));
482                         puts(_("  --version              output version information and exit"));
483                         puts(_("  --config-dir           output configuration directory"));
484
485                         exit(1);
486                 } else if (!strncmp(argv[i], "--crash", 7)) {
487                         cmd.crash = TRUE;
488                         cmd.crash_params = g_strdup(argv[i + 1]);
489                         i++;
490                 } else if (!strncmp(argv[i], "--config-dir", sizeof "--config-dir" - 1)) {
491                         puts(RC_DIR);
492                         exit(0);
493                 }
494                 
495         }
496
497         if (cmd.attach_files && cmd.compose == FALSE) {
498                 cmd.compose = TRUE;
499                 cmd.compose_mailto = NULL;
500         }
501 }
502
503 static gint get_queued_message_num(void)
504 {
505         FolderItem *queue;
506
507         queue = folder_get_default_queue();
508         if (!queue) return -1;
509
510         folder_item_scan(queue);
511         return queue->total_msgs;
512 }
513
514 static void save_all_caches(FolderItem *item, gpointer data)
515 {
516         if (!item->cache)
517                 return;
518         folder_item_write_cache(item);
519 }
520
521 static void initial_processing(FolderItem *item, gpointer data)
522 {
523         MainWindow *mainwin = (MainWindow *)data;
524         gchar *buf;
525
526         g_return_if_fail(item);
527         buf = g_strdup_printf(_("Processing (%s)..."), 
528                               item->path 
529                               ? item->path 
530                               : _("top level folder"));
531         debug_print("%s\n", buf);
532         g_free(buf);
533
534         main_window_cursor_wait(mainwin);
535         
536         folder_item_apply_processing(item);
537
538         debug_print("done.\n");
539         STATUSBAR_POP(mainwin);
540         main_window_cursor_normal(mainwin);
541 }
542
543 static void draft_all_messages(void)
544 {
545         GList *compose_list = compose_get_compose_list();
546         GList *elem = NULL;
547         
548         if (compose_list) {
549                 for (elem = compose_list; elem != NULL && elem->data != NULL; 
550                      elem = elem->next) {
551                         Compose *c = (Compose*)elem->data;
552                         compose_draft(c);
553                 }
554         }       
555 }
556
557 void clean_quit(void)   
558 {
559         /*!< Good idea to have the main window stored in a 
560          *   static variable so we can check that variable
561          *   to see if we're really allowed to do things
562          *   that actually the spawner is supposed to 
563          *   do (like: sending mail, composing messages).
564          *   Because, really, if we're the spawnee, and
565          *   we touch GTK stuff, we're hosed. See the 
566          *   next fixme. */
567
568         /* FIXME: Use something else to signal that we're
569          * in the original spawner, and not in a spawned
570          * child. */
571         if (!static_mainwindow) 
572                 return;
573                 
574         draft_all_messages();
575
576         if (prefs_common.warn_queued_on_exit) { 
577                 /* disable the popup */ 
578                 prefs_common.warn_queued_on_exit = FALSE;       
579                 app_will_exit(NULL, static_mainwindow);
580                 prefs_common.warn_queued_on_exit = TRUE;
581                 prefs_common_save_config();
582         } else {
583                 app_will_exit(NULL, static_mainwindow);
584         }
585         exit(0);
586 }
587
588 void app_will_exit(GtkWidget *widget, gpointer data)
589 {
590         MainWindow *mainwin = data;
591         gchar *filename;
592         GList *list;
593         
594         if (compose_get_compose_list()) {
595                 gint val = alertpanel(_("Notice"),
596                                _("Composing message exists."),
597                                _("Draft them"), _("Discard them"), _("Don't quit"));
598                 switch (val) {
599                         case G_ALERTOTHER:
600                                 return;
601                         case G_ALERTALTERNATE:
602                                 break;
603                         default:
604                                 draft_all_messages();
605                 }
606                 
607                 manage_window_focus_in(mainwin->window, NULL, NULL);
608         }
609
610         if (prefs_common.warn_queued_on_exit && get_queued_message_num() > 0) {
611                 if (alertpanel(_("Queued messages"),
612                                _("Some unsent messages are queued. Exit now?"),
613                                _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT)
614                         return;
615                 manage_window_focus_in(mainwin->window, NULL, NULL);
616         }
617
618         inc_autocheck_timer_remove();
619
620 #if USE_GPGME
621         gpgmegtk_free_passphrase();
622 #endif
623
624         if (prefs_common.clean_on_exit)
625                 main_window_empty_trash(mainwin, prefs_common.ask_on_clean);
626
627         /* save prefs for opened folder */
628         if(mainwin->folderview->opened)
629         {
630                 FolderItem *item;
631
632                 item = gtk_ctree_node_get_row_data(GTK_CTREE(mainwin->folderview->ctree), mainwin->folderview->opened);
633                 summary_save_prefs_to_folderitem(mainwin->folderview->summaryview, item);
634         }
635
636         /* save all state before exiting */
637         folder_write_list();
638         folder_func_to_all_folders(save_all_caches, NULL);
639         for (list = folder_get_list(); list != NULL; list = g_list_next(list)) {
640                 Folder *folder = FOLDER(list->data);
641
642                 folder_tree_destroy(folder);
643         }
644
645         main_window_get_size(mainwin);
646         main_window_get_position(mainwin);
647         prefs_common_save_config();
648         account_save_config_all();
649         addressbook_export_to_file();
650
651         filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
652         gtk_item_factory_dump_rc(filename, NULL, TRUE);
653         g_free(filename);
654
655         /* delete temporary files */
656         remove_all_files(get_mime_tmp_dir());
657
658         close_log_file();
659
660         /* delete crashfile */
661         if (!cmd.crash)
662                 unlink(get_crashfile_name());
663
664         lock_socket_remove();
665
666         gtk_main_quit();
667 }
668
669 #if USE_GPGME
670 static void idle_function_for_gpgme(void)
671 {
672         while (gtk_events_pending())
673                 gtk_main_iteration();
674 }
675 #endif /* USE_GPGME */
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         FolderItem *def_outbox;
906
907         def_outbox = folder_get_default_outbox();
908
909         for (list = folder_get_list(); list != NULL; list = list->next) {
910                 Folder *folder = list->data;
911
912                 if (folder->queue) {
913                         gint res = procmsg_send_queue
914                                 (folder->queue, prefs_common.savemsg);
915
916                         if (res < 0)    
917                                 alertpanel_error(_("Some errors occurred while sending queued messages."));
918                         if (res)        
919                                 folder_item_scan(folder->queue);
920                         if (prefs_common.savemsg && folder->outbox) {
921                                 if (folder->outbox == def_outbox)
922                                         def_outbox = NULL;
923                         }
924                 }
925         }
926 }
927
928 static void quit_signal_handler(int sig)
929 {
930         debug_print("Quitting on signal %d\n", sig);
931         clean_quit();
932 }
933
934 static void install_basic_sighandlers()
935 {
936         sigset_t    mask;
937         struct sigaction act;
938
939         sigemptyset(&mask);
940
941 #ifdef SIGTERM
942         sigaddset(&mask, SIGTERM);
943 #endif
944 #ifdef SIGINT
945         sigaddset(&mask, SIGINT);
946 #endif
947
948         act.sa_handler = quit_signal_handler;
949         act.sa_mask    = mask;
950         act.sa_flags   = 0;
951
952 #ifdef SIGTERM
953         sigaction(SIGTERM, &act, 0);
954 #endif
955 #ifdef SIGINT
956         sigaction(SIGINT, &act, 0);
957 #endif  
958
959         sigprocmask(SIG_UNBLOCK, &mask, 0);
960 }