6969836b0cddf04a480339a4401cfc2e61d11e4d
[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 || prefs_common.chk_on_startup)
337                 inc_all_account_mail(mainwin, prefs_common.newmail_notify_manu);
338         else if (cmd.receive)
339                 inc_mail(mainwin, prefs_common.newmail_notify_manu);
340         else
341                 gtk_widget_grab_focus(folderview->ctree);
342
343         if (cmd.compose)
344                 open_compose_new(cmd.compose_mailto, cmd.attach_files);
345         if (cmd.attach_files) {
346                 ptr_array_free_strings(cmd.attach_files);
347                 g_ptr_array_free(cmd.attach_files, TRUE);
348                 cmd.attach_files = NULL;
349         }
350         if (cmd.send)
351                 send_queue();
352         if (cmd.status_folders) {
353                 g_ptr_array_free(cmd.status_folders, TRUE);
354                 cmd.status_folders = NULL;
355         }
356         if (cmd.status_full_folders) {
357                 g_ptr_array_free(cmd.status_full_folders, TRUE);
358                 cmd.status_full_folders = NULL;
359         }
360
361         if (cmd.online_mode == ONLINE_MODE_OFFLINE)
362                 main_window_toggle_work_offline(mainwin, TRUE);
363         if (cmd.online_mode == ONLINE_MODE_ONLINE)
364                 main_window_toggle_work_offline(mainwin, FALSE);
365
366         prefs_toolbar_init();
367
368         plugin_load_all("GTK");
369         
370         static_mainwindow = mainwin;
371         gtk_main();
372
373         plugin_unload_all("GTK");
374
375         prefs_toolbar_done();
376
377         addressbook_destroy();
378
379 #ifdef USE_ASPELL       
380         prefs_spelling_done();
381         gtkaspell_checkers_quit();
382 #endif
383         sylpheed_done();
384
385         return 0;
386 }
387
388 static void parse_cmd_opt(int argc, char *argv[])
389 {
390         gint i;
391
392         for (i = 1; i < argc; i++) {
393                 if (!strncmp(argv[i], "--receive-all", 13))
394                         cmd.receive_all = TRUE;
395                 else if (!strncmp(argv[i], "--receive", 9))
396                         cmd.receive = TRUE;
397                 else if (!strncmp(argv[i], "--compose", 9)) {
398                         const gchar *p = argv[i + 1];
399
400                         cmd.compose = TRUE;
401                         cmd.compose_mailto = NULL;
402                         if (p && *p != '\0' && *p != '-') {
403                                 if (!strncmp(p, "mailto:", 7))
404                                         cmd.compose_mailto = p + 7;
405                                 else
406                                         cmd.compose_mailto = p;
407                                 i++;
408                         }
409                 } else if (!strncmp(argv[i], "--attach", 8)) {
410                         const gchar *p = argv[i + 1];
411                         gchar *file;
412
413                         while (p && *p != '\0' && *p != '-') {
414                                 if (!cmd.attach_files)
415                                         cmd.attach_files = g_ptr_array_new();
416                                 if (*p != G_DIR_SEPARATOR)
417                                         file = g_strconcat(sylpheed_get_startup_dir(),
418                                                            G_DIR_SEPARATOR_S,
419                                                            p, NULL);
420                                 else
421                                         file = g_strdup(p);
422                                 g_ptr_array_add(cmd.attach_files, file);
423                                 i++;
424                                 p = argv[i + 1];
425                         }
426                 } else if (!strncmp(argv[i], "--send", 6)) {
427                         cmd.send = TRUE;
428                 } else if (!strncmp(argv[i], "--version", 9)) {
429                         puts("Sylpheed version " VERSION);
430                         exit(0);
431                 } else if (!strncmp(argv[i], "--status-full", 13)) {
432                         const gchar *p = argv[i + 1];
433  
434                         cmd.status_full = TRUE;
435                         while (p && *p != '\0' && *p != '-') {
436                                 if (!cmd.status_full_folders)
437                                         cmd.status_full_folders =
438                                                 g_ptr_array_new();
439                                 g_ptr_array_add(cmd.status_full_folders,
440                                                 g_strdup(p));
441                                 i++;
442                                 p = argv[i + 1];
443                         }
444                 } else if (!strncmp(argv[i], "--status", 8)) {
445                         const gchar *p = argv[i + 1];
446  
447                         cmd.status = TRUE;
448                         while (p && *p != '\0' && *p != '-') {
449                                 if (!cmd.status_folders)
450                                         cmd.status_folders = g_ptr_array_new();
451                                 g_ptr_array_add(cmd.status_folders,
452                                                 g_strdup(p));
453                                 i++;
454                                 p = argv[i + 1];
455                         }
456                 } else if (!strncmp(argv[i], "--online", 8)) {
457                         cmd.online_mode = ONLINE_MODE_ONLINE;
458                 } else if (!strncmp(argv[i], "--offline", 9)) {
459                         cmd.online_mode = ONLINE_MODE_OFFLINE;
460                 } else if (!strncmp(argv[i], "--help", 6)) {
461                         g_print(_("Usage: %s [OPTION]...\n"),
462                                 g_basename(argv[0]));
463
464                         puts(_("  --compose [address]    open composition window"));
465                         puts(_("  --attach file1 [file2]...\n"
466                                "                         open composition window with specified files\n"
467                                "                         attached"));
468                         puts(_("  --receive              receive new messages"));
469                         puts(_("  --receive-all          receive new messages of all accounts"));
470                         puts(_("  --send                 send all queued messages"));
471                         puts(_("  --status [folder]...   show the total number of messages"));
472                         puts(_("  --status-full [folder]...\n"
473                                "                         show the status of each folder"));
474                         puts(_("  --online               switch to online mode"));
475                         puts(_("  --offline              switch to offline mode"));
476                         puts(_("  --debug                debug mode"));
477                         puts(_("  --help                 display this help and exit"));
478                         puts(_("  --version              output version information and exit"));
479                         puts(_("  --config-dir           output configuration directory"));
480
481                         exit(1);
482                 } else if (!strncmp(argv[i], "--crash", 7)) {
483                         cmd.crash = TRUE;
484                         cmd.crash_params = g_strdup(argv[i + 1]);
485                         i++;
486                 } else if (!strncmp(argv[i], "--config-dir", sizeof "--config-dir" - 1)) {
487                         puts(RC_DIR);
488                         exit(0);
489                 }
490                 
491         }
492
493         if (cmd.attach_files && cmd.compose == FALSE) {
494                 cmd.compose = TRUE;
495                 cmd.compose_mailto = NULL;
496         }
497 }
498
499 static gint get_queued_message_num(void)
500 {
501         FolderItem *queue;
502
503         queue = folder_get_default_queue();
504         if (!queue) return -1;
505
506         folder_item_scan(queue);
507         return queue->total_msgs;
508 }
509
510 static void save_all_caches(FolderItem *item, gpointer data)
511 {
512         if (!item->cache)
513                 return;
514         folder_item_write_cache(item);
515 }
516
517 static void initial_processing(FolderItem *item, gpointer data)
518 {
519         MainWindow *mainwin = (MainWindow *)data;
520         gchar *buf;
521
522         g_return_if_fail(item);
523         buf = g_strdup_printf(_("Processing (%s)..."), 
524                               item->path 
525                               ? item->path 
526                               : _("top level folder"));
527         debug_print("%s\n", buf);
528         g_free(buf);
529
530         main_window_cursor_wait(mainwin);
531         
532         folder_item_apply_processing(item);
533
534         debug_print("done.\n");
535         STATUSBAR_POP(mainwin);
536         main_window_cursor_normal(mainwin);
537 }
538
539 static void draft_all_messages(void)
540 {
541         GList *compose_list = compose_get_compose_list();
542         GList *elem = NULL;
543         
544         if (compose_list) {
545                 for (elem = compose_list; elem != NULL && elem->data != NULL; 
546                      elem = elem->next) {
547                         Compose *c = (Compose*)elem->data;
548                         compose_draft(c);
549                 }
550         }       
551 }
552
553 void clean_quit(void)   
554 {
555         /*!< Good idea to have the main window stored in a 
556          *   static variable so we can check that variable
557          *   to see if we're really allowed to do things
558          *   that actually the spawner is supposed to 
559          *   do (like: sending mail, composing messages).
560          *   Because, really, if we're the spawnee, and
561          *   we touch GTK stuff, we're hosed. See the 
562          *   next fixme. */
563
564         /* FIXME: Use something else to signal that we're
565          * in the original spawner, and not in a spawned
566          * child. */
567         if (!static_mainwindow) 
568                 return;
569                 
570         draft_all_messages();
571
572         if (prefs_common.warn_queued_on_exit) { 
573                 /* disable the popup */ 
574                 prefs_common.warn_queued_on_exit = FALSE;       
575                 app_will_exit(NULL, static_mainwindow);
576                 prefs_common.warn_queued_on_exit = TRUE;
577                 prefs_common_save_config();
578         } else {
579                 app_will_exit(NULL, static_mainwindow);
580         }
581         exit(0);
582 }
583
584 void app_will_exit(GtkWidget *widget, gpointer data)
585 {
586         MainWindow *mainwin = data;
587         gchar *filename;
588         GList *list;
589         
590         if (compose_get_compose_list()) {
591                 gint val = alertpanel(_("Notice"),
592                                _("Composing message exists."),
593                                _("Draft them"), _("Discard them"), _("Don't quit"));
594                 switch (val) {
595                         case G_ALERTOTHER:
596                                 return;
597                         case G_ALERTALTERNATE:
598                                 break;
599                         default:
600                                 draft_all_messages();
601                 }
602                 
603                 manage_window_focus_in(mainwin->window, NULL, NULL);
604         }
605
606         if (prefs_common.warn_queued_on_exit && get_queued_message_num() > 0) {
607                 if (alertpanel(_("Queued messages"),
608                                _("Some unsent messages are queued. Exit now?"),
609                                _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT)
610                         return;
611                 manage_window_focus_in(mainwin->window, NULL, NULL);
612         }
613
614         inc_autocheck_timer_remove();
615
616 #if USE_GPGME
617         gpgmegtk_free_passphrase();
618 #endif
619
620         if (prefs_common.clean_on_exit)
621                 main_window_empty_trash(mainwin, prefs_common.ask_on_clean);
622
623         /* save prefs for opened folder */
624         if(mainwin->folderview->opened)
625         {
626                 FolderItem *item;
627
628                 item = gtk_ctree_node_get_row_data(GTK_CTREE(mainwin->folderview->ctree), mainwin->folderview->opened);
629                 summary_save_prefs_to_folderitem(mainwin->folderview->summaryview, item);
630         }
631
632         /* save all state before exiting */
633         folder_write_list();
634         folder_func_to_all_folders(save_all_caches, NULL);
635         for (list = folder_get_list(); list != NULL; list = g_list_next(list)) {
636                 Folder *folder = FOLDER(list->data);
637
638                 folder_tree_destroy(folder);
639         }
640
641         main_window_get_size(mainwin);
642         main_window_get_position(mainwin);
643         prefs_common_save_config();
644         account_save_config_all();
645         addressbook_export_to_file();
646
647         filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
648         gtk_item_factory_dump_rc(filename, NULL, TRUE);
649         g_free(filename);
650
651         /* delete temporary files */
652         remove_all_files(get_mime_tmp_dir());
653
654         close_log_file();
655
656         /* delete crashfile */
657         if (!cmd.crash)
658                 unlink(get_crashfile_name());
659
660         lock_socket_remove();
661
662         gtk_main_quit();
663 }
664
665 #if USE_GPGME
666 static void idle_function_for_gpgme(void)
667 {
668         while (gtk_events_pending())
669                 gtk_main_iteration();
670 }
671 #endif /* USE_GPGME */
672
673 /*
674  * CLAWS: want this public so crash dialog can delete the
675  * lock file too
676  */
677 #ifndef CLAWS
678 static
679 #endif
680 gchar *get_socket_name(void)
681 {
682         static gchar *filename = NULL;
683
684         if (filename == NULL) {
685                 filename = g_strdup_printf("%s%csylpheed-%d",
686                                            g_get_tmp_dir(), G_DIR_SEPARATOR,
687                                            getuid());
688         }
689
690         return filename;
691 }
692
693 static gchar *get_crashfile_name(void)
694 {
695         static gchar *filename = NULL;
696
697         if (filename == NULL) {
698                 filename = g_strdup_printf("%s%csylpheed-crashed",
699                                            get_tmp_dir(), G_DIR_SEPARATOR);
700         }
701
702         return filename;
703 }
704
705 static gint prohibit_duplicate_launch(void)
706 {
707         gint uxsock;
708         gchar *path;
709
710         path = get_socket_name();
711         uxsock = fd_connect_unix(path);
712         if (uxsock < 0) {
713                 unlink(path);
714                 return fd_open_unix(path);
715         }
716
717         /* remote command mode */
718
719         debug_print("another Sylpheed is already running.\n");
720
721         if (cmd.receive_all)
722                 fd_write_all(uxsock, "receive_all\n", 12);
723         else if (cmd.receive)
724                 fd_write_all(uxsock, "receive\n", 8);
725         else if (cmd.compose && cmd.attach_files) {
726                 gchar *str, *compose_str;
727                 gint i;
728
729                 if (cmd.compose_mailto)
730                         compose_str = g_strdup_printf("compose_attach %s\n",
731                                                       cmd.compose_mailto);
732                 else
733                         compose_str = g_strdup("compose_attach\n");
734
735                 fd_write_all(uxsock, compose_str, strlen(compose_str));
736                 g_free(compose_str);
737
738                 for (i = 0; i < cmd.attach_files->len; i++) {
739                         str = g_ptr_array_index(cmd.attach_files, i);
740                         fd_write_all(uxsock, str, strlen(str));
741                         fd_write_all(uxsock, "\n", 1);
742                 }
743
744                 fd_write_all(uxsock, ".\n", 2);
745         } else if (cmd.compose) {
746                 gchar *compose_str;
747
748                 if (cmd.compose_mailto)
749                         compose_str = g_strdup_printf
750                                 ("compose %s\n", cmd.compose_mailto);
751                 else
752                         compose_str = g_strdup("compose\n");
753
754                 fd_write_all(uxsock, compose_str, strlen(compose_str));
755                 g_free(compose_str);
756         } else if (cmd.send) {
757                 fd_write_all(uxsock, "send\n", 5);
758         } else if (cmd.online_mode == ONLINE_MODE_ONLINE) {
759                 fd_write(uxsock, "online\n", 6);
760         } else if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
761                 fd_write(uxsock, "offline\n", 7);
762         } else if (cmd.status || cmd.status_full) {
763                 gchar buf[BUFFSIZE];
764                 gint i;
765                 const gchar *command;
766                 GPtrArray *folders;
767                 gchar *folder;
768  
769                 command = cmd.status_full ? "status-full\n" : "status\n";
770                 folders = cmd.status_full ? cmd.status_full_folders :
771                         cmd.status_folders;
772  
773                 fd_write_all(uxsock, command, strlen(command));
774                 for (i = 0; folders && i < folders->len; ++i) {
775                         folder = g_ptr_array_index(folders, i);
776                         fd_write_all(uxsock, folder, strlen(folder));
777                         fd_write_all(uxsock, "\n", 1);
778                 }
779                 fd_write_all(uxsock, ".\n", 2);
780                 for (;;) {
781                         fd_gets(uxsock, buf, sizeof(buf));
782                         if (!strncmp(buf, ".\n", 2)) break;
783                         fputs(buf, stdout);
784                 }
785         } else
786                 fd_write_all(uxsock, "popup\n", 6);
787
788         fd_close(uxsock);
789         return -1;
790 }
791
792 static gint lock_socket_remove(void)
793 {
794         gchar *filename;
795
796         if (lock_socket < 0) return -1;
797
798         if (lock_socket_tag > 0)
799                 gdk_input_remove(lock_socket_tag);
800         fd_close(lock_socket);
801         filename = get_socket_name();
802         unlink(filename);
803
804         return 0;
805 }
806
807 static GPtrArray *get_folder_item_list(gint sock)
808 {
809         gchar buf[BUFFSIZE];
810         FolderItem *item;
811         GPtrArray *folders = NULL;
812
813         for (;;) {
814                 fd_gets(sock, buf, sizeof(buf));
815                 if (!strncmp(buf, ".\n", 2)) break;
816                 strretchomp(buf);
817                 if (!folders) folders = g_ptr_array_new();
818                 item = folder_find_item_from_identifier(buf);
819                 if (item)
820                         g_ptr_array_add(folders, item);
821                 else
822                         g_warning("no such folder: %s\n", buf);
823         }
824
825         return folders;
826 }
827
828 static void lock_socket_input_cb(gpointer data,
829                                  gint source,
830                                  GdkInputCondition condition)
831 {
832         MainWindow *mainwin = (MainWindow *)data;
833         gint sock;
834         gchar buf[BUFFSIZE];
835
836         sock = fd_accept(source);
837         fd_gets(sock, buf, sizeof(buf));
838
839         if (!strncmp(buf, "popup", 5)) {
840                 main_window_popup(mainwin);
841         } else if (!strncmp(buf, "receive_all", 11)) {
842                 inc_all_account_mail(mainwin, prefs_common.newmail_notify_manu);
843         } else if (!strncmp(buf, "receive", 7)) {
844                 inc_mail(mainwin, prefs_common.newmail_notify_manu);
845         } else if (!strncmp(buf, "compose_attach", 14)) {
846                 GPtrArray *files;
847                 gchar *mailto;
848
849                 mailto = g_strdup(buf + strlen("compose_attach") + 1);
850                 files = g_ptr_array_new();
851                 while (fd_gets(sock, buf, sizeof(buf)) > 0) {
852                         if (buf[0] == '.' && buf[1] == '\n') break;
853                         strretchomp(buf);
854                         g_ptr_array_add(files, g_strdup(buf));
855                 }
856                 open_compose_new(mailto, files);
857                 ptr_array_free_strings(files);
858                 g_ptr_array_free(files, TRUE);
859                 g_free(mailto);
860         } else if (!strncmp(buf, "compose", 7)) {
861                 open_compose_new(buf + strlen("compose") + 1, NULL);
862         } else if (!strncmp(buf, "send", 4)) {
863                 send_queue();
864         } else if (!strncmp(buf, "online", 6)) {
865                 main_window_toggle_work_offline(mainwin, FALSE);
866         } else if (!strncmp(buf, "offline", 7)) {
867                 main_window_toggle_work_offline(mainwin, TRUE);
868         } else if (!strncmp(buf, "status-full", 11) ||
869                    !strncmp(buf, "status", 6)) {
870                 gchar *status;
871                 GPtrArray *folders;
872  
873                 folders = get_folder_item_list(sock);
874                 status = folder_get_status
875                         (folders, !strncmp(buf, "status-full", 11));
876                 fd_write_all(sock, status, strlen(status));
877                 fd_write_all(sock, ".\n", 2);
878                 g_free(status);
879                 if (folders) g_ptr_array_free(folders, TRUE);
880         }
881
882         fd_close(sock);
883 }
884
885 static void open_compose_new(const gchar *address, GPtrArray *attach_files)
886 {
887         gchar *addr = NULL;
888
889         if (address) {
890                 Xstrdup_a(addr, address, return);
891                 g_strstrip(addr);
892         }
893
894         compose_new(NULL, addr, attach_files);
895 }
896
897 static void send_queue(void)
898 {
899         GList *list;
900         FolderItem *def_outbox;
901
902         def_outbox = folder_get_default_outbox();
903
904         for (list = folder_get_list(); list != NULL; list = list->next) {
905                 Folder *folder = list->data;
906
907                 if (folder->queue) {
908                         gint res = procmsg_send_queue
909                                 (folder->queue, prefs_common.savemsg);
910
911                         if (res < 0)    
912                                 alertpanel_error(_("Some errors occurred while sending queued messages."));
913                         if (res)        
914                                 folder_item_scan(folder->queue);
915                         if (prefs_common.savemsg && folder->outbox) {
916                                 if (folder->outbox == def_outbox)
917                                         def_outbox = NULL;
918                         }
919                 }
920         }
921 }
922
923 static void quit_signal_handler(int sig)
924 {
925         debug_print("Quitting on signal %d\n", sig);
926         clean_quit();
927 }
928
929 static void install_basic_sighandlers()
930 {
931         sigset_t    mask;
932         struct sigaction act;
933
934         sigemptyset(&mask);
935
936 #ifdef SIGTERM
937         sigaddset(&mask, SIGTERM);
938 #endif
939 #ifdef SIGINT
940         sigaddset(&mask, SIGINT);
941 #endif
942
943         act.sa_handler = quit_signal_handler;
944         act.sa_mask    = mask;
945         act.sa_flags   = 0;
946
947 #ifdef SIGTERM
948         sigaction(SIGTERM, &act, 0);
949 #endif
950 #ifdef SIGINT
951         sigaction(SIGINT, &act, 0);
952 #endif  
953
954         sigprocmask(SIG_UNBLOCK, &mask, 0);
955 }