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