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