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