0.8.11claws22
[claws.git] / src / main.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2003 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 #include "prefs_toolbar.h"
75
76 #if USE_GPGME
77 #  include "rfc2015.h"
78 #endif
79 #if USE_OPENSSL
80 #  include "ssl.h"
81 #endif
82
83 #include "version.h"
84
85 #include "crash.h"
86
87 gchar *prog_version;
88 gchar *startup_dir;
89 #ifdef CRASH_DIALOG
90 gchar *argv0;
91 #endif
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 static struct RemoteCmd {
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 gint lock_socket_remove          (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 static MainWindow *static_mainwindow;
162 int main(int argc, char *argv[])
163 {
164         gchar *userrc;
165         MainWindow *mainwin;
166         FolderView *folderview;
167
168         if(!sylpheed_init(&argc, &argv)) {
169                 return 0;
170         }
171
172         prog_version = PROG_VERSION;
173         startup_dir = g_get_current_dir();
174 #ifdef CRASH_DIALOG
175         argv0 = g_strdup(argv[0]);
176 #endif
177
178         parse_cmd_opt(argc, argv);
179
180 #ifdef CRASH_DIALOG
181         if (cmd.crash) {
182                 gtk_set_locale();
183                 gtk_init(&argc, &argv);
184                 crash_main(cmd.crash_params);
185                 return 0;
186         }
187         crash_install_handlers();
188 #endif
189
190         /* check and create unix domain socket */
191         lock_socket = prohibit_duplicate_launch();
192         if (lock_socket < 0) return 0;
193
194         if (cmd.status) {
195                 puts("0 Sylpheed not running.");
196                 lock_socket_remove();
197                 return 0;
198         }
199
200         gtk_set_locale();
201         gtk_init(&argc, &argv);
202
203 #if USE_THREADS || USE_LDAP
204         g_thread_init(NULL);
205         if (!g_thread_supported())
206                 g_error(_("g_thread is not supported by glib.\n"));
207 #endif
208
209 #if HAVE_GDK_IMLIB
210         gdk_imlib_init();
211         gtk_widget_push_visual(gdk_imlib_get_visual());
212         gtk_widget_push_colormap(gdk_imlib_get_colormap());
213 #endif
214
215         /* parse gtkrc files */
216         userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtkrc",
217                              NULL);
218         gtk_rc_parse(userrc);
219         g_free(userrc);
220         userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtk",
221                              G_DIR_SEPARATOR_S, "gtkrc", NULL);
222         gtk_rc_parse(userrc);
223         g_free(userrc);
224         userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "gtkrc", NULL);
225         gtk_rc_parse(userrc);
226         g_free(userrc);
227
228         gtk_rc_parse("./gtkrc");
229
230         userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
231         gtk_item_factory_parse_rc(userrc);
232         g_free(userrc);
233
234         CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
235
236         MAKE_DIR_IF_NOT_EXIST(RC_DIR);
237         MAKE_DIR_IF_NOT_EXIST(get_imap_cache_dir());
238         MAKE_DIR_IF_NOT_EXIST(get_news_cache_dir());
239         MAKE_DIR_IF_NOT_EXIST(get_mime_tmp_dir());
240         MAKE_DIR_IF_NOT_EXIST(get_tmp_dir());
241         MAKE_DIR_IF_NOT_EXIST(RC_DIR G_DIR_SEPARATOR_S "uidl");
242
243         set_log_file(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log");
244
245         folder_system_init();
246         prefs_common_init();
247         prefs_common_read_config();
248
249 #if USE_GPGME
250         gpg_started = FALSE;
251         if (gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP) != 
252                         GPGME_No_Error) {  /* Also does some gpgme init */
253                 rfc2015_disable_all();
254                 debug_print("gpgme_engine_version:\n%s\n",
255                             gpgme_get_engine_info());
256
257                 if (prefs_common.gpg_warning) {
258                         AlertValue val;
259
260                         val = alertpanel_message_with_disable
261                                 (_("Warning"),
262                                  _("GnuPG is not installed properly, or needs to be upgraded.\n"
263                                    "OpenPGP support disabled."));
264                         if (val & G_ALERTDISABLE)
265                                 prefs_common.gpg_warning = FALSE;
266                 }
267         } else
268                 gpg_started = TRUE;
269
270         gpgme_register_idle(idle_function_for_gpgme);
271 #endif
272
273 #if USE_ASPELL
274         gtkaspellcheckers = gtkaspell_checkers_new();
275 #endif
276         
277
278         prefs_common_save_config();
279         prefs_actions_read_config();
280         prefs_actions_write_config();
281         prefs_display_header_read_config();
282         prefs_display_header_write_config();
283         /* prefs_filtering_read_config(); */
284         addressbook_read_file();
285         renderer_read_config();
286
287         gtkut_widget_init();
288
289         mainwin = main_window_create
290                 (prefs_common.sep_folder | prefs_common.sep_msg << 1);
291         folderview = mainwin->folderview;
292
293         /* register the callback of unix domain socket input */
294         lock_socket_tag = gdk_input_add(lock_socket,
295                                         GDK_INPUT_READ | GDK_INPUT_EXCEPTION,
296                                         lock_socket_input_cb,
297                                         mainwin);
298
299         account_read_config_all();
300         account_save_config_all();
301
302         if (folder_read_list() < 0) {
303                 setup(mainwin);
304                 folder_write_list();
305         }
306         if (!account_get_list()) {
307                 account_edit_open();
308                 account_add();
309         }
310
311         account_set_missing_folder();
312         folder_set_missing_folders();
313         folderview_set(folderview);
314
315         /* prefs_scoring_read_config(); */
316         prefs_matcher_read_config();
317
318         /* make one all-folder processing before using sylpheed */
319         folder_func_to_all_folders(initial_processing, (gpointer *)mainwin);
320
321         /* if Sylpheed crashed, rebuild caches */
322         if (!cmd.crash && is_file_exist(get_crashfile_name())) {
323                 debug_print("Sylpheed crashed, checking for new messages in local folders\n");
324                 folderview_check_new(NULL);
325         }
326         /* make the crash-indicator file */
327         str_write_to_file("foo", get_crashfile_name());
328
329         addressbook_read_file();
330
331         inc_autocheck_timer_init(mainwin);
332
333         /* ignore SIGPIPE signal for preventing sudden death of program */
334         signal(SIGPIPE, SIG_IGN);
335
336         if (cmd.receive_all || prefs_common.chk_on_startup)
337                 inc_all_account_mail(mainwin, prefs_common.newmail_notify_manu);
338         else if (cmd.receive)
339                 inc_mail(mainwin, prefs_common.newmail_notify_manu);
340         else
341                 gtk_widget_grab_focus(folderview->ctree);
342
343         if (cmd.compose)
344                 open_compose_new(cmd.compose_mailto, cmd.attach_files);
345         if (cmd.attach_files) {
346                 ptr_array_free_strings(cmd.attach_files);
347                 g_ptr_array_free(cmd.attach_files, TRUE);
348                 cmd.attach_files = NULL;
349         }
350         if (cmd.send)
351                 send_queue();
352
353         if (cmd.online_mode == ONLINE_MODE_OFFLINE)
354                 main_window_toggle_work_offline(mainwin, TRUE);
355         if (cmd.online_mode == ONLINE_MODE_ONLINE)
356                 main_window_toggle_work_offline(mainwin, FALSE);
357
358         prefs_toolbar_init();
359         
360         static_mainwindow = mainwin;
361         gtk_main();
362
363         prefs_toolbar_done();
364
365         addressbook_destroy();
366
367 #if USE_ASPELL       
368         gtkaspell_checkers_delete();
369 #endif
370         sylpheed_done();
371
372         return 0;
373 }
374
375 static void parse_cmd_opt(int argc, char *argv[])
376 {
377         gint i;
378
379         for (i = 1; i < argc; i++) {
380                 if (!strncmp(argv[i], "--receive-all", 13))
381                         cmd.receive_all = TRUE;
382                 else if (!strncmp(argv[i], "--receive", 9))
383                         cmd.receive = TRUE;
384                 else if (!strncmp(argv[i], "--compose", 9)) {
385                         const gchar *p = argv[i + 1];
386
387                         cmd.compose = TRUE;
388                         cmd.compose_mailto = NULL;
389                         if (p && *p != '\0' && *p != '-') {
390                                 if (!strncmp(p, "mailto:", 7))
391                                         cmd.compose_mailto = p + 7;
392                                 else
393                                         cmd.compose_mailto = p;
394                                 i++;
395                         }
396                 } else if (!strncmp(argv[i], "--attach", 8)) {
397                         const gchar *p = argv[i + 1];
398                         gchar *file;
399
400                         while (p && *p != '\0' && *p != '-') {
401                                 if (!cmd.attach_files)
402                                         cmd.attach_files = g_ptr_array_new();
403                                 if (*p != G_DIR_SEPARATOR)
404                                         file = g_strconcat(startup_dir,
405                                                            G_DIR_SEPARATOR_S,
406                                                            p, NULL);
407                                 else
408                                         file = g_strdup(p);
409                                 g_ptr_array_add(cmd.attach_files, file);
410                                 i++;
411                                 p = argv[i + 1];
412                         }
413                 } else if (!strncmp(argv[i], "--send", 6)) {
414                         cmd.send = TRUE;
415                 } else if (!strncmp(argv[i], "--version", 9)) {
416                         puts("Sylpheed version " VERSION);
417                         exit(0);
418                 } else if (!strncmp(argv[i], "--status", 8)) {
419                         cmd.status = TRUE;
420                 } else if (!strncmp(argv[i], "--online", 8)) {
421                         cmd.online_mode = ONLINE_MODE_ONLINE;
422                 } else if (!strncmp(argv[i], "--offline", 9)) {
423                         cmd.online_mode = ONLINE_MODE_OFFLINE;
424                 } else if (!strncmp(argv[i], "--help", 6)) {
425                         g_print(_("Usage: %s [OPTION]...\n"),
426                                 g_basename(argv[0]));
427
428                         puts(_("  --compose [address]    open composition window"));
429                         puts(_("  --attach file1 [file2]...\n"
430                                "                         open composition window with specified files\n"
431                                "                         attached"));
432                         puts(_("  --receive              receive new messages"));
433                         puts(_("  --receive-all          receive new messages of all accounts"));
434                         puts(_("  --send                 send all queued messages"));
435                         puts(_("  --status               show the total number of messages"));
436                         puts(_("  --online               switch to online mode"));
437                         puts(_("  --offline              switch to offline mode"));
438                         puts(_("  --debug                debug mode"));
439                         puts(_("  --help                 display this help and exit"));
440                         puts(_("  --version              output version information and exit"));
441
442                         exit(1);
443                 } else if (!strncmp(argv[i], "--crash", 7)) {
444                         cmd.crash = TRUE;
445                         cmd.crash_params = g_strdup(argv[i + 1]);
446                         i++;
447                 }
448                 
449         }
450
451         if (cmd.attach_files && cmd.compose == FALSE) {
452                 cmd.compose = TRUE;
453                 cmd.compose_mailto = NULL;
454         }
455 }
456
457 static gint get_queued_message_num(void)
458 {
459         FolderItem *queue;
460
461         queue = folder_get_default_queue();
462         if (!queue) return -1;
463
464         folder_item_scan(queue);
465         return queue->total;
466 }
467
468 static void save_all_caches(FolderItem *item, gpointer data)
469 {
470         if (!item->cache)
471                 return;
472         folder_item_write_cache(item);
473 }
474
475 static void initial_processing(FolderItem *item, gpointer data)
476 {
477         MainWindow *mainwin = (MainWindow *)data;
478         gchar *buf;
479
480         g_return_if_fail(item);
481         buf = g_strdup_printf(_("Processing (%s)..."), 
482                               item->path 
483                               ? item->path 
484                               : _("top level folder"));
485         debug_print("%s\n", buf);
486         g_free(buf);
487
488         main_window_cursor_wait(mainwin);
489         
490         folder_item_apply_processing(item);
491
492         debug_print("done.\n");
493         STATUSBAR_POP(mainwin);
494         main_window_cursor_normal(mainwin);
495 }
496
497 static void draft_all_messages(void)
498 {
499         GList * compose_list = compose_get_compose_list();
500         GList * elem = NULL;
501         if(compose_list) {
502                 for (elem = compose_list; elem != NULL && elem->data != NULL; elem = elem->next) {
503                         Compose *c = (Compose*)elem->data;
504                         compose_draft(c);
505                 }
506         }       
507 }
508
509 void clean_quit(void)   
510 {
511         draft_all_messages();
512
513         if (prefs_common.warn_queued_on_exit)
514         {       /* disable the popup */ 
515                 prefs_common.warn_queued_on_exit = FALSE;       
516                 app_will_exit(NULL, static_mainwindow);
517                 prefs_common.warn_queued_on_exit = TRUE;
518                 prefs_common_save_config();
519         } else {
520                 app_will_exit(NULL, static_mainwindow);
521         }
522         exit(0);
523 }
524
525 void app_will_exit(GtkWidget *widget, gpointer data)
526 {
527         MainWindow *mainwin = data;
528         gchar *filename;
529         
530         if (compose_get_compose_list()) {
531                 gint val = alertpanel(_("Notice"),
532                                _("Composing message exists."),
533                                _("Draft them"), _("Discard them"), _("Don't quit"));
534                 switch (val) {
535                         case G_ALERTOTHER:
536                                 return;
537                         case G_ALERTALTERNATE:
538                                 break;
539                         default:
540                                 draft_all_messages();
541                 }
542                 
543                 manage_window_focus_in(mainwin->window, NULL, NULL);
544         }
545
546         if (prefs_common.warn_queued_on_exit && get_queued_message_num() > 0) {
547                 if (alertpanel(_("Queued messages"),
548                                _("Some unsent messages are queued. Exit now?"),
549                                _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT)
550                         return;
551                 manage_window_focus_in(mainwin->window, NULL, NULL);
552         }
553
554         inc_autocheck_timer_remove();
555
556 #if USE_GPGME
557         gpgmegtk_free_passphrase();
558 #endif
559
560         if (prefs_common.clean_on_exit)
561                 main_window_empty_trash(mainwin, prefs_common.ask_on_clean);
562
563         /* save prefs for opened folder */
564         if(mainwin->folderview->opened)
565         {
566                 FolderItem *item;
567
568                 item = gtk_ctree_node_get_row_data(GTK_CTREE(mainwin->folderview->ctree), mainwin->folderview->opened);
569                 summary_save_prefs_to_folderitem(mainwin->folderview->summaryview, item);
570         }
571
572         /* save all state before exiting */
573         folder_write_list();
574         folder_func_to_all_folders(save_all_caches, NULL);
575
576         main_window_get_size(mainwin);
577         main_window_get_position(mainwin);
578         prefs_common_save_config();
579         account_save_config_all();
580         addressbook_export_to_file();
581
582         filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
583         gtk_item_factory_dump_rc(filename, NULL, TRUE);
584         g_free(filename);
585
586         /* delete temporary files */
587         remove_all_files(get_mime_tmp_dir());
588
589         close_log_file();
590
591         /* delete crashfile */
592         if (!cmd.crash)
593                 unlink(get_crashfile_name());
594
595         lock_socket_remove();
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 gint lock_socket_remove(void)
710 {
711         gchar *filename;
712
713         if (lock_socket < 0) return -1;
714
715         if (lock_socket_tag > 0)
716                 gdk_input_remove(lock_socket_tag);
717         fd_close(lock_socket);
718         filename = get_socket_name();
719         unlink(filename);
720
721         return 0;
722 }
723
724 static void lock_socket_input_cb(gpointer data,
725                                  gint source,
726                                  GdkInputCondition condition)
727 {
728         MainWindow *mainwin = (MainWindow *)data;
729         gint sock;
730         gchar buf[BUFFSIZE];
731
732         sock = fd_accept(source);
733         fd_gets(sock, buf, sizeof(buf));
734
735         if (!strncmp(buf, "popup", 5)) {
736                 main_window_popup(mainwin);
737         } else if (!strncmp(buf, "receive_all", 11)) {
738                 main_window_popup(mainwin);
739                 inc_all_account_mail(mainwin, prefs_common.newmail_notify_manu);
740         } else if (!strncmp(buf, "receive", 7)) {
741                 main_window_popup(mainwin);
742                 inc_mail(mainwin, prefs_common.newmail_notify_manu);
743         } else if (!strncmp(buf, "compose_attach", 14)) {
744                 GPtrArray *files;
745                 gchar *mailto;
746
747                 mailto = g_strdup(buf + strlen("compose_attach") + 1);
748                 files = g_ptr_array_new();
749                 while (fd_gets(sock, buf, sizeof(buf)) > 0) {
750                         if (buf[0] == '.' && buf[1] == '\n') break;
751                         strretchomp(buf);
752                         g_ptr_array_add(files, g_strdup(buf));
753                 }
754                 open_compose_new(mailto, files);
755                 ptr_array_free_strings(files);
756                 g_ptr_array_free(files, TRUE);
757                 g_free(mailto);
758         } else if (!strncmp(buf, "compose", 7)) {
759                 open_compose_new(buf + strlen("compose") + 1, NULL);
760         } else if (!strncmp(buf, "send", 4)) {
761                 send_queue();
762         } else if (!strncmp(buf, "online", 6)) {
763                 main_window_toggle_work_offline(mainwin, FALSE);
764         } else if (!strncmp(buf, "offline", 7)) {
765                 main_window_toggle_work_offline(mainwin, TRUE);
766         } else if (!strncmp(buf, "status", 6)) {
767                 guint new, unread, unreadmarked, total;
768
769                 folder_count_total_msgs(&new, &unread, &unreadmarked, &total);
770                 g_snprintf(buf, sizeof(buf), "%d %d %d %d\n", new, unread, unreadmarked, total);
771                 fd_write(sock, buf, strlen(buf));
772         }
773
774         fd_close(sock);
775 }
776
777 static void open_compose_new(const gchar *address, GPtrArray *attach_files)
778 {
779         gchar *addr = NULL;
780
781         if (address) {
782                 Xstrdup_a(addr, address, return);
783                 g_strstrip(addr);
784         }
785
786         compose_new(NULL, addr, attach_files);
787 }
788
789 static void send_queue(void)
790 {
791         GList *list;
792         FolderItem *def_outbox;
793
794         def_outbox = folder_get_default_outbox();
795
796         for (list = folder_get_list(); list != NULL; list = list->next) {
797                 Folder *folder = list->data;
798
799                 if (folder->queue) {
800                         if (procmsg_send_queue
801                                 (folder->queue, prefs_common.savemsg) < 0)
802                                 alertpanel_error(_("Some errors occurred while sending queued messages."));
803                         folder_item_scan(folder->queue);
804                         if (prefs_common.savemsg && folder->outbox) {
805                                 if (folder->outbox == def_outbox)
806                                         def_outbox = NULL;
807                         }
808                 }
809         }
810 }