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