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