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