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