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