add check if gpg is active
[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_account.h"
60 #include "prefs_actions.h"
61 #include "scoring.h"
62 #include "prefs_display_header.h"
63 #include "account.h"
64 #include "procmsg.h"
65 #include "inc.h"
66 #include "import.h"
67 #include "manage_window.h"
68 #include "alertpanel.h"
69 #include "statusbar.h"
70 #include "addressbook.h"
71 #include "compose.h"
72 #include "folder.h"
73 #include "setup.h"
74 #include "utils.h"
75 #include "gtkutils.h"
76
77 #if USE_GPGME
78 #  include "rfc2015.h"
79 #endif
80 #if USE_SSL
81 #  include "ssl.h"
82 #endif
83
84 #include "version.h"
85
86 #include "crash.h"
87
88 gchar *prog_version;
89 gchar *startup_dir;
90 gchar *argv0;
91 gboolean debug_mode = FALSE;
92
93 static gint lock_socket = -1;
94 static gint lock_socket_tag = 0;
95
96 typedef enum 
97 {
98         ONLINE_MODE_DONT_CHANGE,
99         ONLINE_MODE_ONLINE,
100         ONLINE_MODE_OFFLINE
101 } OnlineMode;
102
103
104 static struct Cmd {
105         gboolean receive;
106         gboolean receive_all;
107         gboolean compose;
108         const gchar *compose_mailto;
109         GPtrArray *attach_files;
110         gboolean status;
111         gboolean send;
112         gboolean crash;
113         int online_mode;
114         gchar   *crash_params;
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 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 int main(int argc, char *argv[])
162 {
163         gchar *userrc;
164         MainWindow *mainwin;
165         FolderView *folderview;
166
167         setlocale(LC_ALL, "");
168         bindtextdomain(PACKAGE, LOCALEDIR);
169         textdomain(PACKAGE);
170
171         prog_version = PROG_VERSION;
172         startup_dir = g_get_current_dir();
173         argv0 = g_strdup(argv[0]);
174
175         parse_cmd_opt(argc, argv);
176
177         gtk_set_locale();
178         gtk_init(&argc, &argv);
179
180         if (cmd.crash) {
181                 crash_main(cmd.crash_params);
182                 return 0;
183         }
184
185         crash_install_handlers();
186
187 #if USE_THREADS || USE_LDAP
188         g_thread_init(NULL);
189         if (!g_thread_supported())
190                 g_error(_("g_thread is not supported by glib.\n"));
191 #endif
192
193 #if HAVE_GDK_IMLIB
194         gdk_imlib_init();
195         gtk_widget_push_visual(gdk_imlib_get_visual());
196         gtk_widget_push_colormap(gdk_imlib_get_colormap());
197 #endif
198
199 #if USE_SSL
200         ssl_init();
201 #endif
202
203         srandom((gint)time(NULL));
204
205         /* parse gtkrc files */
206         userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtkrc",
207                              NULL);
208         gtk_rc_parse(userrc);
209         g_free(userrc);
210         userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtk",
211                              G_DIR_SEPARATOR_S, "gtkrc", NULL);
212         gtk_rc_parse(userrc);
213         g_free(userrc);
214         userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "gtkrc", NULL);
215         gtk_rc_parse(userrc);
216         g_free(userrc);
217
218         gtk_rc_parse("./gtkrc");
219
220         userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
221         gtk_item_factory_parse_rc(userrc);
222         g_free(userrc);
223
224         CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
225
226         /* check and create unix domain socket */
227         lock_socket = prohibit_duplicate_launch();
228         if (lock_socket < 0) return 0;
229
230         if (cmd.status) {
231                 puts("0 Sylpheed not running.");
232                 return 0;
233         }
234
235         /* backup if old rc file exists */
236         if (is_file_exist(RC_DIR)) {
237                 if (rename(RC_DIR, RC_DIR ".bak") < 0)
238                         FILE_OP_ERROR(RC_DIR, "rename");
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         if (is_file_exist(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log")) {
248                 if (rename(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log",
249                            RC_DIR G_DIR_SEPARATOR_S "sylpheed.log.bak") < 0)
250                         FILE_OP_ERROR("sylpheed.log", "rename");
251         }
252         set_log_file(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log");
253
254         if (is_file_exist(RC_DIR G_DIR_SEPARATOR_S "assortrc") &&
255             !is_file_exist(RC_DIR G_DIR_SEPARATOR_S "filterrc")) {
256                 if (rename(RC_DIR G_DIR_SEPARATOR_S "assortrc",
257                            RC_DIR G_DIR_SEPARATOR_S "filterrc") < 0)
258                         FILE_OP_ERROR(RC_DIR G_DIR_SEPARATOR_S "assortrc",
259                                       "rename");
260         }
261
262         prefs_common_init();
263         prefs_common_read_config();
264
265 #if USE_GPGME
266         gpg_started = FALSE;
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         } else
283                 gpg_started = TRUE;
284
285         gpgme_register_idle(idle_function_for_gpgme);
286 #endif
287
288 #if USE_ASPELL
289         gtkaspellcheckers = gtkaspell_checkers_new();
290 #endif
291         
292
293         prefs_common_save_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         account_save_config_all();
555         addressbook_export_to_file();
556
557         filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
558         gtk_item_factory_dump_rc(filename, NULL, TRUE);
559         g_free(filename);
560
561         /* delete temporary files */
562         remove_all_files(get_mime_tmp_dir());
563
564         close_log_file();
565
566         /* delete unix domain socket */
567         gdk_input_remove(lock_socket_tag);
568         fd_close(lock_socket);
569         filename = get_socket_name();
570         unlink(filename);
571         
572         /* delete crashfile */
573         if (!cmd.crash)
574                 unlink(get_crashfile_name());
575
576 #if USE_SSL
577         ssl_done();
578 #endif
579
580         gtk_main_quit();
581 }
582
583 #if USE_GPGME
584 static void idle_function_for_gpgme(void)
585 {
586         while (gtk_events_pending())
587                 gtk_main_iteration();
588 }
589 #endif /* USE_GPGME */
590
591 /*
592  * CLAWS: want this public so crash dialog can delete the
593  * lock file too
594  */
595 #ifndef CLAWS
596 static
597 #endif
598 gchar *get_socket_name(void)
599 {
600         static gchar *filename = NULL;
601
602         if (filename == NULL) {
603                 filename = g_strdup_printf("%s%csylpheed-%d",
604                                            g_get_tmp_dir(), G_DIR_SEPARATOR,
605                                            getuid());
606         }
607
608         return filename;
609 }
610
611 static gchar *get_crashfile_name(void)
612 {
613         static gchar *filename = NULL;
614
615         if (filename == NULL) {
616                 filename = g_strdup_printf("%s%csylpheed-crashed",
617                                            get_tmp_dir(), G_DIR_SEPARATOR);
618         }
619
620         return filename;
621 }
622
623 static gint prohibit_duplicate_launch(void)
624 {
625         gint uxsock;
626         gchar *path;
627
628         path = get_socket_name();
629         uxsock = fd_connect_unix(path);
630         if (uxsock < 0) {
631                 unlink(path);
632                 return fd_open_unix(path);
633         }
634
635         /* remote command mode */
636
637         debug_print("another Sylpheed is already running.\n");
638
639         if (cmd.receive_all)
640                 fd_write(uxsock, "receive_all\n", 12);
641         else if (cmd.receive)
642                 fd_write(uxsock, "receive\n", 8);
643         else if (cmd.compose && cmd.attach_files) {
644                 gchar *str, *compose_str;
645                 gint i;
646
647                 if (cmd.compose_mailto)
648                         compose_str = g_strdup_printf("compose_attach %s\n",
649                                                       cmd.compose_mailto);
650                 else
651                         compose_str = g_strdup("compose_attach\n");
652
653                 fd_write(uxsock, compose_str, strlen(compose_str));
654                 g_free(compose_str);
655
656                 for (i = 0; i < cmd.attach_files->len; i++) {
657                         str = g_ptr_array_index(cmd.attach_files, i);
658                         fd_write(uxsock, str, strlen(str));
659                         fd_write(uxsock, "\n", 1);
660                 }
661
662                 fd_write(uxsock, ".\n", 2);
663         } else if (cmd.compose) {
664                 gchar *compose_str;
665
666                 if (cmd.compose_mailto)
667                         compose_str = g_strdup_printf("compose %s\n", cmd.compose_mailto);
668                 else
669                         compose_str = g_strdup("compose\n");
670
671                 fd_write(uxsock, compose_str, strlen(compose_str));
672                 g_free(compose_str);
673         } else if (cmd.send) {
674                 fd_write(uxsock, "send\n", 5);
675         } else if (cmd.online_mode == ONLINE_MODE_ONLINE) {
676                 fd_write(uxsock, "online\n", 6);
677         } else if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
678                 fd_write(uxsock, "offline\n", 7);
679         } else if (cmd.status) {
680                 gchar buf[BUFFSIZE];
681
682                 fd_write(uxsock, "status\n", 7);
683                 fd_gets(uxsock, buf, sizeof(buf));
684                 fputs(buf, stdout);
685         } else
686                 fd_write(uxsock, "popup\n", 6);
687
688         fd_close(uxsock);
689         return -1;
690 }
691
692 static void lock_socket_input_cb(gpointer data,
693                                  gint source,
694                                  GdkInputCondition condition)
695 {
696         MainWindow *mainwin = (MainWindow *)data;
697         gint sock;
698         gchar buf[BUFFSIZE];
699
700         sock = fd_accept(source);
701         fd_gets(sock, buf, sizeof(buf));
702
703         if (!strncmp(buf, "popup", 5)) {
704                 main_window_popup(mainwin);
705         } else if (!strncmp(buf, "receive_all", 11)) {
706                 main_window_popup(mainwin);
707                 inc_all_account_mail(mainwin, prefs_common.newmail_notify_manu);
708         } else if (!strncmp(buf, "receive", 7)) {
709                 main_window_popup(mainwin);
710                 inc_mail(mainwin, prefs_common.newmail_notify_manu);
711         } else if (!strncmp(buf, "compose_attach", 14)) {
712                 GPtrArray *files;
713                 gchar *mailto;
714
715                 mailto = g_strdup(buf + strlen("compose_attach") + 1);
716                 files = g_ptr_array_new();
717                 while (fd_gets(sock, buf, sizeof(buf)) > 0) {
718                         if (buf[0] == '.' && buf[1] == '\n') break;
719                         strretchomp(buf);
720                         g_ptr_array_add(files, g_strdup(buf));
721                 }
722                 open_compose_new(mailto, files);
723                 ptr_array_free_strings(files);
724                 g_ptr_array_free(files, TRUE);
725                 g_free(mailto);
726         } else if (!strncmp(buf, "compose", 7)) {
727                 open_compose_new(buf + strlen("compose") + 1, NULL);
728         } else if (!strncmp(buf, "send", 4)) {
729                 send_queue();
730         } else if (!strncmp(buf, "online", 6)) {
731                 main_window_toggle_work_offline(mainwin, FALSE);
732         } else if (!strncmp(buf, "offline", 7)) {
733                 main_window_toggle_work_offline(mainwin, TRUE);
734         } else if (!strncmp(buf, "status", 6)) {
735                 guint new, unread, total;
736
737                 folder_count_total_msgs(&new, &unread, &total);
738                 g_snprintf(buf, sizeof(buf), "%d %d %d\n", new, unread, total);
739                 fd_write(sock, buf, strlen(buf));
740         }
741
742         fd_close(sock);
743 }
744
745 static void open_compose_new(const gchar *address, GPtrArray *attach_files)
746 {
747         gchar *addr = NULL;
748
749         if (address) {
750                 Xstrdup_a(addr, address, return);
751                 g_strstrip(addr);
752         }
753
754         compose_new(NULL, addr, attach_files);
755 }
756
757 static void send_queue(void)
758 {
759         GList *list;
760         FolderItem *def_outbox;
761
762         def_outbox = folder_get_default_outbox();
763
764         for (list = folder_get_list(); list != NULL; list = list->next) {
765                 Folder *folder = list->data;
766
767                 if (folder->queue) {
768                         if (procmsg_send_queue
769                                 (folder->queue, prefs_common.savemsg) < 0)
770                                 alertpanel_error(_("Some errors occurred while sending queued messages."));
771                         statusbar_pop_all();
772                         folder_item_scan(folder->queue);
773                         folderview_update_item(folder->queue, TRUE);
774                         if (prefs_common.savemsg && folder->outbox) {
775                                 folderview_update_item(folder->outbox, TRUE);
776                                 if (folder->outbox == def_outbox)
777                                         def_outbox = NULL;
778                         }
779                 }
780         }
781
782         if (prefs_common.savemsg && def_outbox)
783                 folderview_update_item(def_outbox, TRUE);
784 }