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