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