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