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