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