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