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