5aaddc1b8527bbc1b71560fb8135975bbaf8b53b
[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         if (gpgme_check_engine()) {  /* Also does some gpgme init */
267                 rfc2015_disable_all();
268                 debug_print("gpgme_engine_version:\n%s\n",
269                             gpgme_get_engine_info());
270
271                 if (prefs_common.gpg_warning) {
272                         AlertValue val;
273
274                         val = alertpanel_message_with_disable
275                                 (_("Warning"),
276                                  _("GnuPG is not installed properly, or needs to be upgraded.\n"
277                                    "OpenPGP support disabled."));
278                         if (val & G_ALERTDISABLE)
279                                 prefs_common.gpg_warning = FALSE;
280                 }
281         }
282         gpgme_register_idle(idle_function_for_gpgme);
283 #endif
284
285 #if USE_ASPELL
286         gtkaspellcheckers = gtkaspell_checkers_new();
287 #endif
288         
289
290         prefs_common_save_config();
291         prefs_actions_read_config();
292         prefs_actions_write_config();
293         prefs_display_header_read_config();
294         prefs_display_header_write_config();
295         /* prefs_filtering_read_config(); */
296         addressbook_read_file();
297         renderer_read_config();
298
299         gtkut_widget_init();
300
301         mainwin = main_window_create
302                 (prefs_common.sep_folder | prefs_common.sep_msg << 1);
303         folderview = mainwin->folderview;
304
305         /* register the callback of unix domain socket input */
306         lock_socket_tag = gdk_input_add(lock_socket,
307                                         GDK_INPUT_READ | GDK_INPUT_EXCEPTION,
308                                         lock_socket_input_cb,
309                                         mainwin);
310
311         account_read_config_all();
312         account_save_config_all();
313
314         if (folder_read_list() < 0) {
315                 setup(mainwin);
316                 folder_write_list();
317         }
318         if (!account_get_list()) {
319                 account_edit_open();
320                 account_add();
321         }
322
323         account_set_missing_folder();
324         folder_set_missing_folders();
325         folderview_set(folderview);
326
327         /* prefs_scoring_read_config(); */
328         prefs_matcher_read_config();
329
330         /* make one all-folder processing before using sylpheed */
331         folder_func_to_all_folders(initial_processing, (gpointer *)mainwin);
332
333         /* if Sylpheed crashed, rebuild caches */
334         if (!cmd.crash && is_file_exist(get_crashfile_name())) {
335                 debug_print("Sylpheed crashed, checking for new messages\n");
336                 folderview_check_new_all();
337         }
338         /* make the crash-indicator file */
339         str_write_to_file("foo", get_crashfile_name());
340
341         addressbook_read_file();
342
343         inc_autocheck_timer_init(mainwin);
344
345         /* ignore SIGPIPE signal for preventing sudden death of program */
346         signal(SIGPIPE, SIG_IGN);
347
348         if (cmd.receive_all || prefs_common.chk_on_startup)
349                 inc_all_account_mail(mainwin, prefs_common.newmail_notify_manu);
350         else if (cmd.receive)
351                 inc_mail(mainwin, prefs_common.newmail_notify_manu);
352         else
353                 gtk_widget_grab_focus(folderview->ctree);
354
355         if (cmd.compose)
356                 open_compose_new(cmd.compose_mailto, cmd.attach_files);
357         if (cmd.attach_files) {
358                 ptr_array_free_strings(cmd.attach_files);
359                 g_ptr_array_free(cmd.attach_files, TRUE);
360                 cmd.attach_files = NULL;
361         }
362         if (cmd.send)
363                 send_queue();
364
365         if (cmd.online_mode == ONLINE_MODE_OFFLINE)
366                 main_window_toggle_work_offline(mainwin, TRUE);
367         if (cmd.online_mode == ONLINE_MODE_ONLINE)
368                 main_window_toggle_work_offline(mainwin, FALSE);
369         
370         gtk_main();
371
372         addressbook_destroy();
373
374 #if USE_ASPELL       
375         gtkaspell_checkers_delete();
376 #endif
377
378         return 0;
379 }
380
381 static void parse_cmd_opt(int argc, char *argv[])
382 {
383         gint i;
384
385         for (i = 1; i < argc; i++) {
386                 if (!strncmp(argv[i], "--debug", 7))
387                         debug_mode = TRUE;
388                 else if (!strncmp(argv[i], "--receive-all", 13))
389                         cmd.receive_all = TRUE;
390                 else if (!strncmp(argv[i], "--receive", 9))
391                         cmd.receive = TRUE;
392                 else if (!strncmp(argv[i], "--compose", 9)) {
393                         const gchar *p = argv[i + 1];
394
395                         cmd.compose = TRUE;
396                         cmd.compose_mailto = NULL;
397                         if (p && *p != '\0' && *p != '-') {
398                                 if (!strncmp(p, "mailto:", 7))
399                                         cmd.compose_mailto = p + 7;
400                                 else
401                                         cmd.compose_mailto = p;
402                                 i++;
403                         }
404                 } else if (!strncmp(argv[i], "--attach", 8)) {
405                         const gchar *p = argv[i + 1];
406                         gchar *file;
407
408                         while (p && *p != '\0' && *p != '-') {
409                                 if (!cmd.attach_files)
410                                         cmd.attach_files = g_ptr_array_new();
411                                 if (*p != G_DIR_SEPARATOR)
412                                         file = g_strconcat(startup_dir,
413                                                            G_DIR_SEPARATOR_S,
414                                                            p, NULL);
415                                 else
416                                         file = g_strdup(p);
417                                 g_ptr_array_add(cmd.attach_files, file);
418                                 i++;
419                                 p = argv[i + 1];
420                         }
421                 } else if (!strncmp(argv[i], "--send", 6)) {
422                         cmd.send = TRUE;
423                 } else if (!strncmp(argv[i], "--version", 9)) {
424                         puts("Sylpheed version " VERSION);
425                         exit(0);
426                 } else if (!strncmp(argv[i], "--status", 8)) {
427                         cmd.status = TRUE;
428                 } else if (!strncmp(argv[i], "--online", 8)) {
429                         cmd.online_mode = ONLINE_MODE_ONLINE;
430                 } else if (!strncmp(argv[i], "--offline", 9)) {
431                         cmd.online_mode = ONLINE_MODE_OFFLINE;
432                 } else if (!strncmp(argv[i], "--help", 6)) {
433                         g_print(_("Usage: %s [OPTION]...\n"),
434                                 g_basename(argv[0]));
435
436                         puts(_("  --compose [address]    open composition window"));
437                         puts(_("  --attach file1 [file2]...\n"
438                                "                         open composition window with specified files\n"
439                                "                         attached"));
440                         puts(_("  --receive              receive new messages"));
441                         puts(_("  --receive-all          receive new messages of all accounts"));
442                         puts(_("  --send                 send all queued messages"));
443                         puts(_("  --status               show the total number of messages"));
444                         puts(_("  --online               switch to online mode"));
445                         puts(_("  --offline              switch to offline mode"));
446                         puts(_("  --debug                debug mode"));
447                         puts(_("  --help                 display this help and exit"));
448                         puts(_("  --version              output version information and exit"));
449
450                         exit(1);
451                 } else if (!strncmp(argv[i], "--crash", 7)) {
452                         cmd.crash = TRUE;
453                         cmd.crash_params = g_strdup(argv[i + 1]);
454                         i++;
455                 }
456                 
457         }
458
459         if (cmd.attach_files && cmd.compose == FALSE) {
460                 cmd.compose = TRUE;
461                 cmd.compose_mailto = NULL;
462         }
463 }
464
465 static gint get_queued_message_num(void)
466 {
467         FolderItem *queue;
468
469         queue = folder_get_default_queue();
470         if (!queue) return -1;
471
472         folder_item_scan(queue);
473         return queue->total;
474 }
475
476 static void save_all_caches(FolderItem *item, gpointer data)
477 {
478         if (!item->cache)
479                 return;
480         folder_item_write_cache(item);
481 }
482
483 static void initial_processing(FolderItem *item, gpointer data)
484 {
485         MainWindow *mainwin = (MainWindow *)data;
486         gchar *buf;
487
488         g_return_if_fail(item);
489         buf = g_strdup_printf(_("Processing (%s)..."), 
490                               item->path 
491                               ? item->path 
492                               : _("top level folder"));
493         debug_print("%s\n", buf);
494         g_free(buf);
495
496         main_window_cursor_wait(mainwin);
497         
498         folder_item_apply_processing(item);
499
500         debug_print("done.\n");
501         STATUSBAR_POP(mainwin);
502         main_window_cursor_normal(mainwin);
503 }
504
505 void app_will_exit(GtkWidget *widget, gpointer data)
506 {
507         MainWindow *mainwin = data;
508         gchar *filename;
509
510         if (compose_get_compose_list()) {
511                 if (alertpanel(_("Notice"),
512                                _("Composing message exists. Really quit?"),
513                                _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT)
514                         return;
515                 manage_window_focus_in(mainwin->window, NULL, NULL);
516         }
517
518         if (prefs_common.warn_queued_on_exit && get_queued_message_num() > 0) {
519                 if (alertpanel(_("Queued messages"),
520                                _("Some unsent messages are queued. Exit now?"),
521                                _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT)
522                         return;
523                 manage_window_focus_in(mainwin->window, NULL, NULL);
524         }
525
526         inc_autocheck_timer_remove();
527
528 #if USE_GPGME
529         gpgmegtk_free_passphrase();
530 #endif
531
532         if (prefs_common.clean_on_exit)
533                 main_window_empty_trash(mainwin, prefs_common.ask_on_clean);
534
535         /* save prefs for opened folder */
536         if(mainwin->folderview->opened)
537         {
538                 FolderItem *item;
539
540                 item = gtk_ctree_node_get_row_data(GTK_CTREE(mainwin->folderview->ctree), mainwin->folderview->opened);
541                 summary_save_prefs_to_folderitem(mainwin->folderview->summaryview, item);
542         }
543
544         /* save all state before exiting */
545         folder_write_list();
546         folder_func_to_all_folders(save_all_caches, NULL);
547
548         main_window_get_size(mainwin);
549         main_window_get_position(mainwin);
550         prefs_common_save_config();
551         account_save_config_all();
552         addressbook_export_to_file();
553
554         filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
555         gtk_item_factory_dump_rc(filename, NULL, TRUE);
556         g_free(filename);
557
558         /* delete temporary files */
559         remove_all_files(get_mime_tmp_dir());
560
561         close_log_file();
562
563         /* delete unix domain socket */
564         gdk_input_remove(lock_socket_tag);
565         fd_close(lock_socket);
566         filename = get_socket_name();
567         unlink(filename);
568         
569         /* delete crashfile */
570         if (!cmd.crash)
571                 unlink(get_crashfile_name());
572
573 #if USE_SSL
574         ssl_done();
575 #endif
576
577         gtk_main_quit();
578 }
579
580 #if USE_GPGME
581 static void idle_function_for_gpgme(void)
582 {
583         while (gtk_events_pending())
584                 gtk_main_iteration();
585 }
586 #endif /* USE_GPGME */
587
588 /*
589  * CLAWS: want this public so crash dialog can delete the
590  * lock file too
591  */
592 #ifndef CLAWS
593 static
594 #endif
595 gchar *get_socket_name(void)
596 {
597         static gchar *filename = NULL;
598
599         if (filename == NULL) {
600                 filename = g_strdup_printf("%s%csylpheed-%d",
601                                            g_get_tmp_dir(), G_DIR_SEPARATOR,
602                                            getuid());
603         }
604
605         return filename;
606 }
607
608 static gchar *get_crashfile_name(void)
609 {
610         static gchar *filename = NULL;
611
612         if (filename == NULL) {
613                 filename = g_strdup_printf("%s%csylpheed-crashed",
614                                            get_tmp_dir(), G_DIR_SEPARATOR);
615         }
616
617         return filename;
618 }
619
620 static gint prohibit_duplicate_launch(void)
621 {
622         gint uxsock;
623         gchar *path;
624
625         path = get_socket_name();
626         uxsock = fd_connect_unix(path);
627         if (uxsock < 0) {
628                 unlink(path);
629                 return fd_open_unix(path);
630         }
631
632         /* remote command mode */
633
634         debug_print("another Sylpheed is already running.\n");
635
636         if (cmd.receive_all)
637                 fd_write(uxsock, "receive_all\n", 12);
638         else if (cmd.receive)
639                 fd_write(uxsock, "receive\n", 8);
640         else if (cmd.compose && cmd.attach_files) {
641                 gchar *str, *compose_str;
642                 gint i;
643
644                 if (cmd.compose_mailto)
645                         compose_str = g_strdup_printf("compose_attach %s\n",
646                                                       cmd.compose_mailto);
647                 else
648                         compose_str = g_strdup("compose_attach\n");
649
650                 fd_write(uxsock, compose_str, strlen(compose_str));
651                 g_free(compose_str);
652
653                 for (i = 0; i < cmd.attach_files->len; i++) {
654                         str = g_ptr_array_index(cmd.attach_files, i);
655                         fd_write(uxsock, str, strlen(str));
656                         fd_write(uxsock, "\n", 1);
657                 }
658
659                 fd_write(uxsock, ".\n", 2);
660         } else if (cmd.compose) {
661                 gchar *compose_str;
662
663                 if (cmd.compose_mailto)
664                         compose_str = g_strdup_printf("compose %s\n", cmd.compose_mailto);
665                 else
666                         compose_str = g_strdup("compose\n");
667
668                 fd_write(uxsock, compose_str, strlen(compose_str));
669                 g_free(compose_str);
670         } else if (cmd.send) {
671                 fd_write(uxsock, "send\n", 5);
672         } else if (cmd.online_mode == ONLINE_MODE_ONLINE) {
673                 fd_write(uxsock, "online\n", 6);
674         } else if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
675                 fd_write(uxsock, "offline\n", 7);
676         } else if (cmd.status) {
677                 gchar buf[BUFFSIZE];
678
679                 fd_write(uxsock, "status\n", 7);
680                 fd_gets(uxsock, buf, sizeof(buf));
681                 fputs(buf, stdout);
682         } else
683                 fd_write(uxsock, "popup\n", 6);
684
685         fd_close(uxsock);
686         return -1;
687 }
688
689 static void lock_socket_input_cb(gpointer data,
690                                  gint source,
691                                  GdkInputCondition condition)
692 {
693         MainWindow *mainwin = (MainWindow *)data;
694         gint sock;
695         gchar buf[BUFFSIZE];
696
697         sock = fd_accept(source);
698         fd_gets(sock, buf, sizeof(buf));
699
700         if (!strncmp(buf, "popup", 5)) {
701                 main_window_popup(mainwin);
702         } else if (!strncmp(buf, "receive_all", 11)) {
703                 main_window_popup(mainwin);
704                 inc_all_account_mail(mainwin, prefs_common.newmail_notify_manu);
705         } else if (!strncmp(buf, "receive", 7)) {
706                 main_window_popup(mainwin);
707                 inc_mail(mainwin, prefs_common.newmail_notify_manu);
708         } else if (!strncmp(buf, "compose_attach", 14)) {
709                 GPtrArray *files;
710                 gchar *mailto;
711
712                 mailto = g_strdup(buf + strlen("compose_attach") + 1);
713                 files = g_ptr_array_new();
714                 while (fd_gets(sock, buf, sizeof(buf)) > 0) {
715                         if (buf[0] == '.' && buf[1] == '\n') break;
716                         strretchomp(buf);
717                         g_ptr_array_add(files, g_strdup(buf));
718                 }
719                 open_compose_new(mailto, files);
720                 ptr_array_free_strings(files);
721                 g_ptr_array_free(files, TRUE);
722                 g_free(mailto);
723         } else if (!strncmp(buf, "compose", 7)) {
724                 open_compose_new(buf + strlen("compose") + 1, NULL);
725         } else if (!strncmp(buf, "send", 4)) {
726                 send_queue();
727         } else if (!strncmp(buf, "online", 6)) {
728                 main_window_toggle_work_offline(mainwin, FALSE);
729         } else if (!strncmp(buf, "offline", 7)) {
730                 main_window_toggle_work_offline(mainwin, TRUE);
731         } else if (!strncmp(buf, "status", 6)) {
732                 guint new, unread, total;
733
734                 folder_count_total_msgs(&new, &unread, &total);
735                 g_snprintf(buf, sizeof(buf), "%d %d %d\n", new, unread, total);
736                 fd_write(sock, buf, strlen(buf));
737         }
738
739         fd_close(sock);
740 }
741
742 static void open_compose_new(const gchar *address, GPtrArray *attach_files)
743 {
744         gchar *addr = NULL;
745
746         if (address) {
747                 Xstrdup_a(addr, address, return);
748                 g_strstrip(addr);
749         }
750
751         compose_new(NULL, addr, attach_files);
752 }
753
754 static void send_queue(void)
755 {
756         GList *list;
757         FolderItem *def_outbox;
758
759         def_outbox = folder_get_default_outbox();
760
761         for (list = folder_get_list(); list != NULL; list = list->next) {
762                 Folder *folder = list->data;
763
764                 if (folder->queue) {
765                         if (procmsg_send_queue
766                                 (folder->queue, prefs_common.savemsg) < 0)
767                                 alertpanel_error(_("Some errors occurred while sending queued messages."));
768                         statusbar_pop_all();
769                         folder_item_scan(folder->queue);
770                         folderview_update_item(folder->queue, TRUE);
771                         if (prefs_common.savemsg && folder->outbox) {
772                                 folderview_update_item(folder->outbox, TRUE);
773                                 if (folder->outbox == def_outbox)
774                                         def_outbox = NULL;
775                         }
776                 }
777         }
778
779         if (prefs_common.savemsg && def_outbox)
780                 folderview_update_item(def_outbox, TRUE);
781 }