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