c3abca9ca94be8c6f4bfe1b9e9d5ef6207afee39
[claws.git] / src / main.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2002 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 #if HAVE_GDK_IMLIB
31 #  include <gdk_imlib.h>
32 #endif
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <ctype.h>
38 #include <unistd.h>
39 #include <time.h>
40 #include <sys/stat.h>
41 #include <sys/types.h>
42 #include <signal.h>
43
44 #if HAVE_LOCALE_H
45 #  include <locale.h>
46 #endif
47
48 #if USE_GPGME
49 #  include <gpgme.h>
50 #  include "passphrase.h"
51 #endif
52
53 #include "intl.h"
54 #include "main.h"
55 #include "mainwindow.h"
56 #include "folderview.h"
57 #include "summaryview.h"
58 #include "prefs_common.h"
59 #include "prefs_filter.h"
60 #include "prefs_account.h"
61 #include "prefs_actions.h"
62 #include "scoring.h"
63 #include "prefs_display_header.h"
64 #include "account.h"
65 #include "procmsg.h"
66 #include "inc.h"
67 #include "import.h"
68 #include "manage_window.h"
69 #include "alertpanel.h"
70 #include "statusbar.h"
71 #include "addressbook.h"
72 #include "compose.h"
73 #include "folder.h"
74 #include "setup.h"
75 #include "utils.h"
76 #include "gtkutils.h"
77
78 #if USE_GPGME
79 #  include "rfc2015.h"
80 #endif
81 #if USE_SSL
82 #  include "ssl.h"
83 #endif
84
85 #include "version.h"
86
87 #include "crash.h"
88
89 gchar *prog_version;
90 gchar *startup_dir;
91 gchar *argv0;
92 gboolean debug_mode = FALSE;
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
105 static struct Cmd {
106         gboolean receive;
107         gboolean receive_all;
108         gboolean compose;
109         const gchar *compose_mailto;
110         GPtrArray *attach_files;
111         gboolean status;
112         gboolean send;
113         gboolean crash;
114         int online_mode;
115         gchar   *crash_params;
116 } cmd;
117
118 static void parse_cmd_opt(int argc, char *argv[]);
119
120 #if USE_GPGME
121 static void idle_function_for_gpgme(void);
122 #endif /* USE_GPGME */
123
124 static gint prohibit_duplicate_launch   (void);
125 static gchar * get_crashfile_name       (void);
126 static 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 int main(int argc, char *argv[])
163 {
164         gchar *userrc;
165         MainWindow *mainwin;
166         FolderView *folderview;
167
168         setlocale(LC_ALL, "");
169         bindtextdomain(PACKAGE, LOCALEDIR);
170         textdomain(PACKAGE);
171
172         prog_version = PROG_VERSION;
173         startup_dir = g_get_current_dir();
174         argv0 = g_strdup(argv[0]);
175
176         parse_cmd_opt(argc, argv);
177
178         gtk_set_locale();
179         gtk_init(&argc, &argv);
180
181         if (cmd.crash) {
182                 crash_main(cmd.crash_params);
183                 return 0;
184         }
185
186         crash_install_handlers();
187
188 #if USE_THREADS || USE_LDAP
189         g_thread_init(NULL);
190         if (!g_thread_supported())
191                 g_error(_("g_thread is not supported by glib.\n"));
192 #endif
193
194 #if HAVE_GDK_IMLIB
195         gdk_imlib_init();
196         gtk_widget_push_visual(gdk_imlib_get_visual());
197         gtk_widget_push_colormap(gdk_imlib_get_colormap());
198 #endif
199
200 #if USE_SSL
201         ssl_init();
202 #endif
203
204         srandom((gint)time(NULL));
205
206         /* parse gtkrc files */
207         userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtkrc",
208                              NULL);
209         gtk_rc_parse(userrc);
210         g_free(userrc);
211         userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtk",
212                              G_DIR_SEPARATOR_S, "gtkrc", NULL);
213         gtk_rc_parse(userrc);
214         g_free(userrc);
215         userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "gtkrc", NULL);
216         gtk_rc_parse(userrc);
217         g_free(userrc);
218
219         gtk_rc_parse("./gtkrc");
220
221         userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
222         gtk_item_factory_parse_rc(userrc);
223         g_free(userrc);
224
225         CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
226
227         /* check and create unix domain socket */
228         lock_socket = prohibit_duplicate_launch();
229         if (lock_socket < 0) return 0;
230
231         if (cmd.status) {
232                 puts("0 Sylpheed not running.");
233                 return 0;
234         }
235
236         /* backup if old rc file exists */
237         if (is_file_exist(RC_DIR)) {
238                 if (rename(RC_DIR, RC_DIR ".bak") < 0)
239                         FILE_OP_ERROR(RC_DIR, "rename");
240         }
241         MAKE_DIR_IF_NOT_EXIST(RC_DIR);
242         MAKE_DIR_IF_NOT_EXIST(get_imap_cache_dir());
243         MAKE_DIR_IF_NOT_EXIST(get_news_cache_dir());
244         MAKE_DIR_IF_NOT_EXIST(get_mime_tmp_dir());
245         MAKE_DIR_IF_NOT_EXIST(get_tmp_dir());
246         MAKE_DIR_IF_NOT_EXIST(RC_DIR G_DIR_SEPARATOR_S "uidl");
247
248         if (is_file_exist(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log")) {
249                 if (rename(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log",
250                            RC_DIR G_DIR_SEPARATOR_S "sylpheed.log.bak") < 0)
251                         FILE_OP_ERROR("sylpheed.log", "rename");
252         }
253         set_log_file(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log");
254
255         if (is_file_exist(RC_DIR G_DIR_SEPARATOR_S "assortrc") &&
256             !is_file_exist(RC_DIR G_DIR_SEPARATOR_S "filterrc")) {
257                 if (rename(RC_DIR G_DIR_SEPARATOR_S "assortrc",
258                            RC_DIR G_DIR_SEPARATOR_S "filterrc") < 0)
259                         FILE_OP_ERROR(RC_DIR G_DIR_SEPARATOR_S "assortrc",
260                                       "rename");
261         }
262
263         prefs_common_init();
264         prefs_common_read_config();
265
266 #if USE_GPGME
267         if (gpgme_check_engine()) {  /* Also does some gpgme init */
268                 rfc2015_disable_all();
269                 debug_print("gpgme_engine_version:\n%s\n",
270                             gpgme_get_engine_info());
271
272                 if (prefs_common.gpg_warning) {
273                         AlertValue val;
274
275                         val = alertpanel_message_with_disable
276                                 (_("Warning"),
277                                  _("GnuPG is not installed properly, or needs to be upgraded.\n"
278                                    "OpenPGP support disabled."));
279                         if (val & G_ALERTDISABLE)
280                                 prefs_common.gpg_warning = FALSE;
281                 }
282         }
283         gpgme_register_idle(idle_function_for_gpgme);
284 #endif
285
286 #if USE_ASPELL
287         gtkaspellcheckers = gtkaspell_checkers_new();
288 #endif
289         
290
291         prefs_common_save_config();
292         prefs_filter_read_config();
293         prefs_filter_write_config();
294         prefs_actions_read_config();
295         prefs_actions_write_config();
296         prefs_display_header_read_config();
297         prefs_display_header_write_config();
298         /* prefs_filtering_read_config(); */
299         addressbook_read_file();
300         renderer_read_config();
301
302         gtkut_widget_init();
303
304         mainwin = main_window_create
305                 (prefs_common.sep_folder | prefs_common.sep_msg << 1);
306         folderview = mainwin->folderview;
307
308         /* register the callback of unix domain socket input */
309         lock_socket_tag = gdk_input_add(lock_socket,
310                                         GDK_INPUT_READ | GDK_INPUT_EXCEPTION,
311                                         lock_socket_input_cb,
312                                         mainwin);
313
314         account_read_config_all();
315         account_save_config_all();
316
317         if (folder_read_list() < 0) {
318                 setup(mainwin);
319                 folder_write_list();
320         }
321         if (!account_get_list()) {
322                 account_edit_open();
323                 account_add();
324         }
325
326         account_set_missing_folder();
327         folder_set_missing_folders();
328         folderview_set(folderview);
329
330         /* prefs_scoring_read_config(); */
331         prefs_matcher_read_config();
332
333         /* make one all-folder processing before using sylpheed */
334         folder_func_to_all_folders(initial_processing, (gpointer *)mainwin);
335
336         /* if Sylpheed crashed, rebuild caches */
337         if (!cmd.crash && is_file_exist(get_crashfile_name())) {
338                 debug_print("Sylpheed crashed, checking for new messages\n");
339                 folderview_check_new_all();
340         }
341         /* make the crash-indicator file */
342         str_write_to_file("foo", get_crashfile_name());
343
344         addressbook_read_file();
345
346         inc_autocheck_timer_init(mainwin);
347
348         /* ignore SIGPIPE signal for preventing sudden death of program */
349         signal(SIGPIPE, SIG_IGN);
350
351         if (cmd.receive_all || prefs_common.chk_on_startup)
352                 inc_all_account_mail(mainwin, prefs_common.newmail_notify_manu);
353         else if (cmd.receive)
354                 inc_mail(mainwin, prefs_common.newmail_notify_manu);
355         else
356                 gtk_widget_grab_focus(folderview->ctree);
357
358         if (cmd.compose)
359                 open_compose_new(cmd.compose_mailto, cmd.attach_files);
360         if (cmd.attach_files) {
361                 ptr_array_free_strings(cmd.attach_files);
362                 g_ptr_array_free(cmd.attach_files, TRUE);
363                 cmd.attach_files = NULL;
364         }
365         if (cmd.send)
366                 send_queue();
367
368         if (cmd.online_mode == ONLINE_MODE_OFFLINE)
369                 main_window_toggle_work_offline(mainwin, TRUE);
370         if (cmd.online_mode == ONLINE_MODE_ONLINE)
371                 main_window_toggle_work_offline(mainwin, FALSE);
372         
373         gtk_main();
374
375         addressbook_destroy();
376
377 #if USE_ASPELL       
378         gtkaspell_checkers_delete();
379 #endif
380
381         return 0;
382 }
383
384 static void parse_cmd_opt(int argc, char *argv[])
385 {
386         gint i;
387
388         for (i = 1; i < argc; i++) {
389                 if (!strncmp(argv[i], "--debug", 7))
390                         debug_mode = TRUE;
391                 else 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(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", 8)) {
430                         cmd.status = TRUE;
431                 } else if (!strncmp(argv[i], "--online", 8)) {
432                         cmd.online_mode = ONLINE_MODE_ONLINE;
433                 } else if (!strncmp(argv[i], "--offline", 9)) {
434                         cmd.online_mode = ONLINE_MODE_OFFLINE;
435                 } else if (!strncmp(argv[i], "--help", 6)) {
436                         g_print(_("Usage: %s [OPTION]...\n"),
437                                 g_basename(argv[0]));
438
439                         puts(_("  --compose [address]    open composition window"));
440                         puts(_("  --attach file1 [file2]...\n"
441                                "                         open composition window with specified files\n"
442                                "                         attached"));
443                         puts(_("  --receive              receive new messages"));
444                         puts(_("  --receive-all          receive new messages of all accounts"));
445                         puts(_("  --send                 send all queued messages"));
446                         puts(_("  --status               show the total number of messages"));
447                         puts(_("  --online               switch to online mode"));
448                         puts(_("  --offline              switch to offline mode"));
449                         puts(_("  --debug                debug mode"));
450                         puts(_("  --help                 display this help and exit"));
451                         puts(_("  --version              output version information and exit"));
452
453                         exit(1);
454                 } else if (!strncmp(argv[i], "--crash", 7)) {
455                         cmd.crash = TRUE;
456                         cmd.crash_params = g_strdup(argv[i + 1]);
457                         i++;
458                 }
459                 
460         }
461
462         if (cmd.attach_files && cmd.compose == FALSE) {
463                 cmd.compose = TRUE;
464                 cmd.compose_mailto = NULL;
465         }
466 }
467
468 static gint get_queued_message_num(void)
469 {
470         FolderItem *queue;
471
472         queue = folder_get_default_queue();
473         if (!queue) return -1;
474
475         folder_item_scan(queue);
476         return queue->total;
477 }
478
479 static void save_all_caches(FolderItem *item, gpointer data)
480 {
481         if (!item->cache)
482                 return;
483         folder_item_write_cache(item);
484 }
485
486 static void initial_processing(FolderItem *item, gpointer data)
487 {
488         MainWindow *mainwin = (MainWindow *)data;
489         gchar *buf;
490
491         g_return_if_fail(item);
492         buf = g_strdup_printf(_("Processing (%s)..."), 
493                               item->path 
494                               ? item->path 
495                               : _("top level folder"));
496         debug_print("%s\n", buf);
497         g_free(buf);
498
499         main_window_cursor_wait(mainwin);
500         
501         folder_item_apply_processing(item);
502
503         debug_print("done.\n");
504         STATUSBAR_POP(mainwin);
505         main_window_cursor_normal(mainwin);
506 }
507
508 void app_will_exit(GtkWidget *widget, gpointer data)
509 {
510         MainWindow *mainwin = data;
511         gchar *filename;
512
513         if (compose_get_compose_list()) {
514                 if (alertpanel(_("Notice"),
515                                _("Composing message exists. Really quit?"),
516                                _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT)
517                         return;
518                 manage_window_focus_in(mainwin->window, NULL, NULL);
519         }
520
521         if (prefs_common.warn_queued_on_exit && get_queued_message_num() > 0) {
522                 if (alertpanel(_("Queued messages"),
523                                _("Some unsent messages are queued. Exit now?"),
524                                _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT)
525                         return;
526                 manage_window_focus_in(mainwin->window, NULL, NULL);
527         }
528
529         inc_autocheck_timer_remove();
530
531 #if USE_GPGME
532         gpgmegtk_free_passphrase();
533 #endif
534
535         if (prefs_common.clean_on_exit)
536                 main_window_empty_trash(mainwin, prefs_common.ask_on_clean);
537
538         /* save prefs for opened folder */
539         if(mainwin->folderview->opened)
540         {
541                 FolderItem *item;
542
543                 item = gtk_ctree_node_get_row_data(GTK_CTREE(mainwin->folderview->ctree), mainwin->folderview->opened);
544                 summary_save_prefs_to_folderitem(mainwin->folderview->summaryview, item);
545         }
546
547         /* save all state before exiting */
548         folder_write_list();
549         folder_func_to_all_folders(save_all_caches, NULL);
550
551         main_window_get_size(mainwin);
552         main_window_get_position(mainwin);
553         prefs_common_save_config();
554         prefs_filter_write_config();
555         account_save_config_all();
556         addressbook_export_to_file();
557
558         filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
559         gtk_item_factory_dump_rc(filename, NULL, TRUE);
560         g_free(filename);
561
562         /* delete temporary files */
563         remove_all_files(get_mime_tmp_dir());
564
565         close_log_file();
566
567         /* delete unix domain socket */
568         gdk_input_remove(lock_socket_tag);
569         fd_close(lock_socket);
570         filename = get_socket_name();
571         unlink(filename);
572         
573         /* delete crashfile */
574         if (!cmd.crash)
575                 unlink(get_crashfile_name());
576
577 #if USE_SSL
578         ssl_done();
579 #endif
580
581         gtk_main_quit();
582 }
583
584 #if USE_GPGME
585 static void idle_function_for_gpgme(void)
586 {
587         while (gtk_events_pending())
588                 gtk_main_iteration();
589 }
590 #endif /* USE_GPGME */
591
592 /*
593  * CLAWS: want this public so crash dialog can delete the
594  * lock file too
595  */
596 #ifndef CLAWS
597 static
598 #endif
599 gchar *get_socket_name(void)
600 {
601         static gchar *filename = NULL;
602
603         if (filename == NULL) {
604                 filename = g_strdup_printf("%s%csylpheed-%d",
605                                            g_get_tmp_dir(), G_DIR_SEPARATOR,
606                                            getuid());
607         }
608
609         return filename;
610 }
611
612 static gchar *get_crashfile_name(void)
613 {
614         static gchar *filename = NULL;
615
616         if (filename == NULL) {
617                 filename = g_strdup_printf("%s%csylpheed-crashed",
618                                            get_tmp_dir(), G_DIR_SEPARATOR);
619         }
620
621         return filename;
622 }
623
624 static gint prohibit_duplicate_launch(void)
625 {
626         gint uxsock;
627         gchar *path;
628
629         path = get_socket_name();
630         uxsock = fd_connect_unix(path);
631         if (uxsock < 0) {
632                 unlink(path);
633                 return fd_open_unix(path);
634         }
635
636         /* remote command mode */
637
638         debug_print("another Sylpheed is already running.\n");
639
640         if (cmd.receive_all)
641                 fd_write(uxsock, "receive_all\n", 12);
642         else if (cmd.receive)
643                 fd_write(uxsock, "receive\n", 8);
644         else if (cmd.compose && cmd.attach_files) {
645                 gchar *str, *compose_str;
646                 gint i;
647
648                 if (cmd.compose_mailto)
649                         compose_str = g_strdup_printf("compose_attach %s\n",
650                                                       cmd.compose_mailto);
651                 else
652                         compose_str = g_strdup("compose_attach\n");
653
654                 fd_write(uxsock, compose_str, strlen(compose_str));
655                 g_free(compose_str);
656
657                 for (i = 0; i < cmd.attach_files->len; i++) {
658                         str = g_ptr_array_index(cmd.attach_files, i);
659                         fd_write(uxsock, str, strlen(str));
660                         fd_write(uxsock, "\n", 1);
661                 }
662
663                 fd_write(uxsock, ".\n", 2);
664         } else if (cmd.compose) {
665                 gchar *compose_str;
666
667                 if (cmd.compose_mailto)
668                         compose_str = g_strdup_printf("compose %s\n", cmd.compose_mailto);
669                 else
670                         compose_str = g_strdup("compose\n");
671
672                 fd_write(uxsock, compose_str, strlen(compose_str));
673                 g_free(compose_str);
674         } else if (cmd.send) {
675                 fd_write(uxsock, "send\n", 5);
676         } else if (cmd.online_mode == ONLINE_MODE_ONLINE) {
677                 fd_write(uxsock, "online\n", 6);
678         } else if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
679                 fd_write(uxsock, "offline\n", 7);
680         } else if (cmd.status) {
681                 gchar buf[BUFFSIZE];
682
683                 fd_write(uxsock, "status\n", 7);
684                 fd_gets(uxsock, buf, sizeof(buf));
685                 fputs(buf, stdout);
686         } else
687                 fd_write(uxsock, "popup\n", 6);
688
689         fd_close(uxsock);
690         return -1;
691 }
692
693 static void lock_socket_input_cb(gpointer data,
694                                  gint source,
695                                  GdkInputCondition condition)
696 {
697         MainWindow *mainwin = (MainWindow *)data;
698         gint sock;
699         gchar buf[BUFFSIZE];
700
701         sock = fd_accept(source);
702         fd_gets(sock, buf, sizeof(buf));
703
704         if (!strncmp(buf, "popup", 5)) {
705                 main_window_popup(mainwin);
706         } else if (!strncmp(buf, "receive_all", 11)) {
707                 main_window_popup(mainwin);
708                 inc_all_account_mail(mainwin, prefs_common.newmail_notify_manu);
709         } else if (!strncmp(buf, "receive", 7)) {
710                 main_window_popup(mainwin);
711                 inc_mail(mainwin, prefs_common.newmail_notify_manu);
712         } else if (!strncmp(buf, "compose_attach", 14)) {
713                 GPtrArray *files;
714                 gchar *mailto;
715
716                 mailto = g_strdup(buf + strlen("compose_attach") + 1);
717                 files = g_ptr_array_new();
718                 while (fd_gets(sock, buf, sizeof(buf)) > 0) {
719                         if (buf[0] == '.' && buf[1] == '\n') break;
720                         strretchomp(buf);
721                         g_ptr_array_add(files, g_strdup(buf));
722                 }
723                 open_compose_new(mailto, files);
724                 ptr_array_free_strings(files);
725                 g_ptr_array_free(files, TRUE);
726                 g_free(mailto);
727         } else if (!strncmp(buf, "compose", 7)) {
728                 open_compose_new(buf + strlen("compose") + 1, NULL);
729         } else if (!strncmp(buf, "send", 4)) {
730                 send_queue();
731         } else if (!strncmp(buf, "online", 6)) {
732                 main_window_toggle_work_offline(mainwin, FALSE);
733         } else if (!strncmp(buf, "offline", 7)) {
734                 main_window_toggle_work_offline(mainwin, TRUE);
735         } else if (!strncmp(buf, "status", 6)) {
736                 guint new, unread, total;
737
738                 folder_count_total_msgs(&new, &unread, &total);
739                 g_snprintf(buf, sizeof(buf), "%d %d %d\n", new, unread, total);
740                 fd_write(sock, buf, strlen(buf));
741         }
742
743         fd_close(sock);
744 }
745
746 static void open_compose_new(const gchar *address, GPtrArray *attach_files)
747 {
748         gchar *addr = NULL;
749
750         if (address) {
751                 Xstrdup_a(addr, address, return);
752                 g_strstrip(addr);
753         }
754
755         compose_new(NULL, addr, attach_files);
756 }
757
758 static void send_queue(void)
759 {
760         GList *list;
761         FolderItem *def_outbox;
762
763         def_outbox = folder_get_default_outbox();
764
765         for (list = folder_get_list(); list != NULL; list = list->next) {
766                 Folder *folder = list->data;
767
768                 if (folder->queue) {
769                         if (procmsg_send_queue
770                                 (folder->queue, prefs_common.savemsg) < 0)
771                                 alertpanel_error(_("Some errors occurred while sending queued messages."));
772                         statusbar_pop_all();
773                         folder_item_scan(folder->queue);
774                         folderview_update_item(folder->queue, TRUE);
775                         if (prefs_common.savemsg && folder->outbox) {
776                                 folderview_update_item(folder->outbox, TRUE);
777                                 if (folder->outbox == def_outbox)
778                                         def_outbox = NULL;
779                         }
780                 }
781         }
782
783         if (prefs_common.savemsg && def_outbox)
784                 folderview_update_item(def_outbox, TRUE);
785 }