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