Fix prefs_filtering renaming for MH folders (we
[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
77 #if USE_GPGME
78 #  include "rfc2015.h"
79 #endif
80 #if USE_SSL
81 #  include "ssl.h"
82 #endif
83
84 #include "version.h"
85
86 #include "crash.h"
87
88 gchar *prog_version;
89 gchar *startup_dir;
90 gchar *argv0;
91 gboolean debug_mode = FALSE;
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         gtk_set_locale();
179         gtk_init(&argc, &argv);
180
181         if (cmd.crash) {
182                 crash_main(cmd.crash_params);
183                 return 0;
184         }
185
186         crash_install_handlers();
187
188 #if USE_THREADS || USE_LDAP
189         g_thread_init(NULL);
190         if (!g_thread_supported())
191                 g_error(_("g_thread is not supported by glib.\n"));
192 #endif
193
194 #if HAVE_GDK_IMLIB
195         gdk_imlib_init();
196         gtk_widget_push_visual(gdk_imlib_get_visual());
197         gtk_widget_push_colormap(gdk_imlib_get_colormap());
198 #endif
199
200 #if USE_SSL
201         ssl_init();
202 #endif
203
204         srandom((gint)time(NULL));
205
206         /* parse gtkrc files */
207         userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtkrc",
208                              NULL);
209         gtk_rc_parse(userrc);
210         g_free(userrc);
211         userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtk",
212                              G_DIR_SEPARATOR_S, "gtkrc", NULL);
213         gtk_rc_parse(userrc);
214         g_free(userrc);
215         userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "gtkrc", NULL);
216         gtk_rc_parse(userrc);
217         g_free(userrc);
218
219         gtk_rc_parse("./gtkrc");
220
221         userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
222         gtk_item_factory_parse_rc(userrc);
223         g_free(userrc);
224
225         CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
226
227         /* check and create unix domain socket */
228         lock_socket = prohibit_duplicate_launch();
229         if (lock_socket < 0) return 0;
230
231         if (cmd.status) {
232                 puts("0 Sylpheed not running.");
233                 return 0;
234         }
235
236         /* backup if old rc file exists */
237         if (is_file_exist(RC_DIR)) {
238                 if (rename(RC_DIR, RC_DIR ".bak") < 0)
239                         FILE_OP_ERROR(RC_DIR, "rename");
240         }
241         MAKE_DIR_IF_NOT_EXIST(RC_DIR);
242         MAKE_DIR_IF_NOT_EXIST(get_imap_cache_dir());
243         MAKE_DIR_IF_NOT_EXIST(get_news_cache_dir());
244         MAKE_DIR_IF_NOT_EXIST(get_mime_tmp_dir());
245         MAKE_DIR_IF_NOT_EXIST(get_tmp_dir());
246         MAKE_DIR_IF_NOT_EXIST(RC_DIR G_DIR_SEPARATOR_S "uidl");
247
248         if (is_file_exist(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log")) {
249                 if (rename(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log",
250                            RC_DIR G_DIR_SEPARATOR_S "sylpheed.log.bak") < 0)
251                         FILE_OP_ERROR("sylpheed.log", "rename");
252         }
253         set_log_file(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log");
254
255         if (is_file_exist(RC_DIR G_DIR_SEPARATOR_S "assortrc") &&
256             !is_file_exist(RC_DIR G_DIR_SEPARATOR_S "filterrc")) {
257                 if (rename(RC_DIR G_DIR_SEPARATOR_S "assortrc",
258                            RC_DIR G_DIR_SEPARATOR_S "filterrc") < 0)
259                         FILE_OP_ERROR(RC_DIR G_DIR_SEPARATOR_S "assortrc",
260                                       "rename");
261         }
262
263         prefs_common_init();
264         prefs_common_read_config();
265
266 #if USE_GPGME
267         gpg_started = FALSE;
268         if (gpgme_check_engine()) {  /* Also does some gpgme init */
269                 rfc2015_disable_all();
270                 debug_print("gpgme_engine_version:\n%s\n",
271                             gpgme_get_engine_info());
272
273                 if (prefs_common.gpg_warning) {
274                         AlertValue val;
275
276                         val = alertpanel_message_with_disable
277                                 (_("Warning"),
278                                  _("GnuPG is not installed properly, or needs to be upgraded.\n"
279                                    "OpenPGP support disabled."));
280                         if (val & G_ALERTDISABLE)
281                                 prefs_common.gpg_warning = FALSE;
282                 }
283         } else
284                 gpg_started = TRUE;
285
286         gpgme_register_idle(idle_function_for_gpgme);
287 #endif
288
289 #if USE_ASPELL
290         gtkaspellcheckers = gtkaspell_checkers_new();
291 #endif
292         
293
294         prefs_common_save_config();
295         prefs_actions_read_config();
296         prefs_actions_write_config();
297         prefs_display_header_read_config();
298         prefs_display_header_write_config();
299         /* prefs_filtering_read_config(); */
300         addressbook_read_file();
301         renderer_read_config();
302
303         gtkut_widget_init();
304
305         mainwin = main_window_create
306                 (prefs_common.sep_folder | prefs_common.sep_msg << 1);
307         folderview = mainwin->folderview;
308
309         /* register the callback of unix domain socket input */
310         lock_socket_tag = gdk_input_add(lock_socket,
311                                         GDK_INPUT_READ | GDK_INPUT_EXCEPTION,
312                                         lock_socket_input_cb,
313                                         mainwin);
314
315         account_read_config_all();
316         account_save_config_all();
317
318         if (folder_read_list() < 0) {
319                 setup(mainwin);
320                 folder_write_list();
321         }
322         if (!account_get_list()) {
323                 account_edit_open();
324                 account_add();
325         }
326
327         account_set_missing_folder();
328         folder_set_missing_folders();
329         folderview_set(folderview);
330
331         /* prefs_scoring_read_config(); */
332         prefs_matcher_read_config();
333
334         /* make one all-folder processing before using sylpheed */
335         folder_func_to_all_folders(initial_processing, (gpointer *)mainwin);
336
337         /* if Sylpheed crashed, rebuild caches */
338         if (!cmd.crash && is_file_exist(get_crashfile_name())) {
339                 debug_print("Sylpheed crashed, checking for new messages\n");
340                 folderview_check_new_all();
341         }
342         /* make the crash-indicator file */
343         str_write_to_file("foo", get_crashfile_name());
344
345         addressbook_read_file();
346
347         inc_autocheck_timer_init(mainwin);
348
349         /* ignore SIGPIPE signal for preventing sudden death of program */
350         signal(SIGPIPE, SIG_IGN);
351
352         if (cmd.receive_all || prefs_common.chk_on_startup)
353                 inc_all_account_mail(mainwin, prefs_common.newmail_notify_manu);
354         else if (cmd.receive)
355                 inc_mail(mainwin, prefs_common.newmail_notify_manu);
356         else
357                 gtk_widget_grab_focus(folderview->ctree);
358
359         if (cmd.compose)
360                 open_compose_new(cmd.compose_mailto, cmd.attach_files);
361         if (cmd.attach_files) {
362                 ptr_array_free_strings(cmd.attach_files);
363                 g_ptr_array_free(cmd.attach_files, TRUE);
364                 cmd.attach_files = NULL;
365         }
366         if (cmd.send)
367                 send_queue();
368
369         if (cmd.online_mode == ONLINE_MODE_OFFLINE)
370                 main_window_toggle_work_offline(mainwin, TRUE);
371         if (cmd.online_mode == ONLINE_MODE_ONLINE)
372                 main_window_toggle_work_offline(mainwin, FALSE);
373         
374         static_mainwindow = mainwin;
375         gtk_main();
376
377         addressbook_destroy();
378
379 #if USE_ASPELL       
380         gtkaspell_checkers_delete();
381 #endif
382
383         return 0;
384 }
385
386 static void parse_cmd_opt(int argc, char *argv[])
387 {
388         gint i;
389
390         for (i = 1; i < argc; i++) {
391                 if (!strncmp(argv[i], "--debug", 7))
392                         debug_mode = TRUE;
393                 else if (!strncmp(argv[i], "--receive-all", 13))
394                         cmd.receive_all = TRUE;
395                 else if (!strncmp(argv[i], "--receive", 9))
396                         cmd.receive = TRUE;
397                 else if (!strncmp(argv[i], "--compose", 9)) {
398                         const gchar *p = argv[i + 1];
399
400                         cmd.compose = TRUE;
401                         cmd.compose_mailto = NULL;
402                         if (p && *p != '\0' && *p != '-') {
403                                 if (!strncmp(p, "mailto:", 7))
404                                         cmd.compose_mailto = p + 7;
405                                 else
406                                         cmd.compose_mailto = p;
407                                 i++;
408                         }
409                 } else if (!strncmp(argv[i], "--attach", 8)) {
410                         const gchar *p = argv[i + 1];
411                         gchar *file;
412
413                         while (p && *p != '\0' && *p != '-') {
414                                 if (!cmd.attach_files)
415                                         cmd.attach_files = g_ptr_array_new();
416                                 if (*p != G_DIR_SEPARATOR)
417                                         file = g_strconcat(startup_dir,
418                                                            G_DIR_SEPARATOR_S,
419                                                            p, NULL);
420                                 else
421                                         file = g_strdup(p);
422                                 g_ptr_array_add(cmd.attach_files, file);
423                                 i++;
424                                 p = argv[i + 1];
425                         }
426                 } else if (!strncmp(argv[i], "--send", 6)) {
427                         cmd.send = TRUE;
428                 } else if (!strncmp(argv[i], "--version", 9)) {
429                         puts("Sylpheed version " VERSION);
430                         exit(0);
431                 } else if (!strncmp(argv[i], "--status", 8)) {
432                         cmd.status = TRUE;
433                 } else if (!strncmp(argv[i], "--online", 8)) {
434                         cmd.online_mode = ONLINE_MODE_ONLINE;
435                 } else if (!strncmp(argv[i], "--offline", 9)) {
436                         cmd.online_mode = ONLINE_MODE_OFFLINE;
437                 } else if (!strncmp(argv[i], "--help", 6)) {
438                         g_print(_("Usage: %s [OPTION]...\n"),
439                                 g_basename(argv[0]));
440
441                         puts(_("  --compose [address]    open composition window"));
442                         puts(_("  --attach file1 [file2]...\n"
443                                "                         open composition window with specified files\n"
444                                "                         attached"));
445                         puts(_("  --receive              receive new messages"));
446                         puts(_("  --receive-all          receive new messages of all accounts"));
447                         puts(_("  --send                 send all queued messages"));
448                         puts(_("  --status               show the total number of messages"));
449                         puts(_("  --online               switch to online mode"));
450                         puts(_("  --offline              switch to offline mode"));
451                         puts(_("  --debug                debug mode"));
452                         puts(_("  --help                 display this help and exit"));
453                         puts(_("  --version              output version information and exit"));
454
455                         exit(1);
456                 } else if (!strncmp(argv[i], "--crash", 7)) {
457                         cmd.crash = TRUE;
458                         cmd.crash_params = g_strdup(argv[i + 1]);
459                         i++;
460                 }
461                 
462         }
463
464         if (cmd.attach_files && cmd.compose == FALSE) {
465                 cmd.compose = TRUE;
466                 cmd.compose_mailto = NULL;
467         }
468 }
469
470 static gint get_queued_message_num(void)
471 {
472         FolderItem *queue;
473
474         queue = folder_get_default_queue();
475         if (!queue) return -1;
476
477         folder_item_scan(queue);
478         return queue->total;
479 }
480
481 static void save_all_caches(FolderItem *item, gpointer data)
482 {
483         if (!item->cache)
484                 return;
485         folder_item_write_cache(item);
486 }
487
488 static void initial_processing(FolderItem *item, gpointer data)
489 {
490         MainWindow *mainwin = (MainWindow *)data;
491         gchar *buf;
492
493         g_return_if_fail(item);
494         buf = g_strdup_printf(_("Processing (%s)..."), 
495                               item->path 
496                               ? item->path 
497                               : _("top level folder"));
498         debug_print("%s\n", buf);
499         g_free(buf);
500
501         main_window_cursor_wait(mainwin);
502         
503         folder_item_apply_processing(item);
504
505         debug_print("done.\n");
506         STATUSBAR_POP(mainwin);
507         main_window_cursor_normal(mainwin);
508 }
509
510 static void draft_all_messages(void)
511 {
512         GList * compose_list = compose_get_compose_list();
513         GList * elem = NULL;
514         if(compose_list) {
515                 for (elem = compose_list; elem != NULL && elem->data != NULL; elem = elem->next) {
516                         Compose *c = (Compose*)elem->data;
517                         compose_draft(c);
518                 }
519         }       
520 }
521
522 void clean_quit(void)   
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 unix domain socket */
605         gdk_input_remove(lock_socket_tag);
606         fd_close(lock_socket);
607         filename = get_socket_name();
608         unlink(filename);
609         
610         /* delete crashfile */
611         if (!cmd.crash)
612                 unlink(get_crashfile_name());
613
614 #if USE_SSL
615         ssl_done();
616 #endif
617
618         gtk_main_quit();
619 }
620
621 #if USE_GPGME
622 static void idle_function_for_gpgme(void)
623 {
624         while (gtk_events_pending())
625                 gtk_main_iteration();
626 }
627 #endif /* USE_GPGME */
628
629 /*
630  * CLAWS: want this public so crash dialog can delete the
631  * lock file too
632  */
633 #ifndef CLAWS
634 static
635 #endif
636 gchar *get_socket_name(void)
637 {
638         static gchar *filename = NULL;
639
640         if (filename == NULL) {
641                 filename = g_strdup_printf("%s%csylpheed-%d",
642                                            g_get_tmp_dir(), G_DIR_SEPARATOR,
643                                            getuid());
644         }
645
646         return filename;
647 }
648
649 static gchar *get_crashfile_name(void)
650 {
651         static gchar *filename = NULL;
652
653         if (filename == NULL) {
654                 filename = g_strdup_printf("%s%csylpheed-crashed",
655                                            get_tmp_dir(), G_DIR_SEPARATOR);
656         }
657
658         return filename;
659 }
660
661 static gint prohibit_duplicate_launch(void)
662 {
663         gint uxsock;
664         gchar *path;
665
666         path = get_socket_name();
667         uxsock = fd_connect_unix(path);
668         if (uxsock < 0) {
669                 unlink(path);
670                 return fd_open_unix(path);
671         }
672
673         /* remote command mode */
674
675         debug_print("another Sylpheed is already running.\n");
676
677         if (cmd.receive_all)
678                 fd_write(uxsock, "receive_all\n", 12);
679         else if (cmd.receive)
680                 fd_write(uxsock, "receive\n", 8);
681         else if (cmd.compose && cmd.attach_files) {
682                 gchar *str, *compose_str;
683                 gint i;
684
685                 if (cmd.compose_mailto)
686                         compose_str = g_strdup_printf("compose_attach %s\n",
687                                                       cmd.compose_mailto);
688                 else
689                         compose_str = g_strdup("compose_attach\n");
690
691                 fd_write(uxsock, compose_str, strlen(compose_str));
692                 g_free(compose_str);
693
694                 for (i = 0; i < cmd.attach_files->len; i++) {
695                         str = g_ptr_array_index(cmd.attach_files, i);
696                         fd_write(uxsock, str, strlen(str));
697                         fd_write(uxsock, "\n", 1);
698                 }
699
700                 fd_write(uxsock, ".\n", 2);
701         } else if (cmd.compose) {
702                 gchar *compose_str;
703
704                 if (cmd.compose_mailto)
705                         compose_str = g_strdup_printf("compose %s\n", cmd.compose_mailto);
706                 else
707                         compose_str = g_strdup("compose\n");
708
709                 fd_write(uxsock, compose_str, strlen(compose_str));
710                 g_free(compose_str);
711         } else if (cmd.send) {
712                 fd_write(uxsock, "send\n", 5);
713         } else if (cmd.online_mode == ONLINE_MODE_ONLINE) {
714                 fd_write(uxsock, "online\n", 6);
715         } else if (cmd.online_mode == ONLINE_MODE_OFFLINE) {
716                 fd_write(uxsock, "offline\n", 7);
717         } else if (cmd.status) {
718                 gchar buf[BUFFSIZE];
719
720                 fd_write(uxsock, "status\n", 7);
721                 fd_gets(uxsock, buf, sizeof(buf));
722                 fputs(buf, stdout);
723         } else
724                 fd_write(uxsock, "popup\n", 6);
725
726         fd_close(uxsock);
727         return -1;
728 }
729
730 static void lock_socket_input_cb(gpointer data,
731                                  gint source,
732                                  GdkInputCondition condition)
733 {
734         MainWindow *mainwin = (MainWindow *)data;
735         gint sock;
736         gchar buf[BUFFSIZE];
737
738         sock = fd_accept(source);
739         fd_gets(sock, buf, sizeof(buf));
740
741         if (!strncmp(buf, "popup", 5)) {
742                 main_window_popup(mainwin);
743         } else if (!strncmp(buf, "receive_all", 11)) {
744                 main_window_popup(mainwin);
745                 inc_all_account_mail(mainwin, prefs_common.newmail_notify_manu);
746         } else if (!strncmp(buf, "receive", 7)) {
747                 main_window_popup(mainwin);
748                 inc_mail(mainwin, prefs_common.newmail_notify_manu);
749         } else if (!strncmp(buf, "compose_attach", 14)) {
750                 GPtrArray *files;
751                 gchar *mailto;
752
753                 mailto = g_strdup(buf + strlen("compose_attach") + 1);
754                 files = g_ptr_array_new();
755                 while (fd_gets(sock, buf, sizeof(buf)) > 0) {
756                         if (buf[0] == '.' && buf[1] == '\n') break;
757                         strretchomp(buf);
758                         g_ptr_array_add(files, g_strdup(buf));
759                 }
760                 open_compose_new(mailto, files);
761                 ptr_array_free_strings(files);
762                 g_ptr_array_free(files, TRUE);
763                 g_free(mailto);
764         } else if (!strncmp(buf, "compose", 7)) {
765                 open_compose_new(buf + strlen("compose") + 1, NULL);
766         } else if (!strncmp(buf, "send", 4)) {
767                 send_queue();
768         } else if (!strncmp(buf, "online", 6)) {
769                 main_window_toggle_work_offline(mainwin, FALSE);
770         } else if (!strncmp(buf, "offline", 7)) {
771                 main_window_toggle_work_offline(mainwin, TRUE);
772         } else if (!strncmp(buf, "status", 6)) {
773                 guint new, unread, total;
774
775                 folder_count_total_msgs(&new, &unread, &total);
776                 g_snprintf(buf, sizeof(buf), "%d %d %d\n", new, unread, total);
777                 fd_write(sock, buf, strlen(buf));
778         }
779
780         fd_close(sock);
781 }
782
783 static void open_compose_new(const gchar *address, GPtrArray *attach_files)
784 {
785         gchar *addr = NULL;
786
787         if (address) {
788                 Xstrdup_a(addr, address, return);
789                 g_strstrip(addr);
790         }
791
792         compose_new(NULL, addr, attach_files);
793 }
794
795 static void send_queue(void)
796 {
797         GList *list;
798         FolderItem *def_outbox;
799
800         def_outbox = folder_get_default_outbox();
801
802         for (list = folder_get_list(); list != NULL; list = list->next) {
803                 Folder *folder = list->data;
804
805                 if (folder->queue) {
806                         if (procmsg_send_queue
807                                 (folder->queue, prefs_common.savemsg) < 0)
808                                 alertpanel_error(_("Some errors occurred while sending queued messages."));
809                         statusbar_pop_all();
810                         folder_item_scan(folder->queue);
811                         folderview_update_item(folder->queue, TRUE);
812                         if (prefs_common.savemsg && folder->outbox) {
813                                 folderview_update_item(folder->outbox, TRUE);
814                                 if (folder->outbox == def_outbox)
815                                         def_outbox = NULL;
816                         }
817                 }
818         }
819
820         if (prefs_common.savemsg && def_outbox)
821                 folderview_update_item(def_outbox, TRUE);
822 }