add --send cli option
[claws.git] / src / main.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2001 Hiroyuki Yamamoto
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include "defs.h"
25
26 #include <glib.h>
27 #include <gtk/gtkmain.h>
28 #include <gtk/gtkrc.h>
29
30 #if HAVE_GDK_IMLIB
31 #  include <gdk_imlib.h>
32 #endif
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <ctype.h>
38 #include <unistd.h>
39 #include <time.h>
40 #include <sys/stat.h>
41 #include <sys/types.h>
42 #include <signal.h>
43
44 #if HAVE_LOCALE_H
45 #  include <locale.h>
46 #endif
47
48 #if USE_GPGME
49 #  include <gpgme.h>
50 #endif
51
52 #include "intl.h"
53 #include "main.h"
54 #include "mainwindow.h"
55 #include "folderview.h"
56 #include "summaryview.h"
57 #include "prefs_common.h"
58 #include "prefs_filter.h"
59 #include "prefs_account.h"
60 #include "scoring.h"
61 #include "prefs_display_header.h"
62 #include "account.h"
63 #include "procmsg.h"
64 #include "inc.h"
65 #include "import.h"
66 #include "manage_window.h"
67 #include "alertpanel.h"
68 #include "addressbook.h"
69 #include "compose.h"
70 #include "folder.h"
71 #include "setup.h"
72 #include "utils.h"
73 #include "gtkutils.h"
74
75 #if USE_GPGME
76 #  include "rfc2015.h"
77 #endif
78 #if USE_SSL
79 #  include "ssl.h"
80 #endif
81
82 #include "version.h"
83
84 gchar *prog_version;
85 gchar *startup_dir;
86 gboolean debug_mode = FALSE;
87
88 static gint lock_socket = -1;
89 static gint lock_socket_tag = 0;
90
91 static struct Cmd {
92         gboolean receive;
93         gboolean receive_all;
94         gboolean compose;
95         const gchar *compose_mailto;
96         gboolean status;
97         gboolean send;
98 } cmd;
99
100 static void parse_cmd_opt(int argc, char *argv[]);
101
102 #if USE_GPGME
103 static void idle_function_for_gpgme(void);
104 #endif /* USE_GPGME */
105
106 static gint prohibit_duplicate_launch   (void);
107 static void lock_socket_input_cb        (gpointer          data,
108                                          gint              source,
109                                          GdkInputCondition condition);
110 static gchar *get_socket_name           (void);
111
112 static void open_compose_new_with_recipient     (const gchar    *address);
113
114 #if 0
115 /* for gettext */
116 _("File `%s' already exists.\n"
117   "Can't create folder.")
118 #endif
119
120 #define MAKE_DIR_IF_NOT_EXIST(dir) \
121 { \
122         if (!is_dir_exist(dir)) { \
123                 if (is_file_exist(dir)) { \
124                         alertpanel_warning \
125                                 (_("File `%s' already exists.\n" \
126                                    "Can't create folder."), \
127                                  dir); \
128                         return 1; \
129                 } \
130                 if (mkdir(dir, S_IRWXU) < 0) { \
131                         FILE_OP_ERROR(dir, "mkdir"); \
132                         return 1; \
133                 } \
134                 if (chmod(dir, S_IRWXU) < 0) \
135                         FILE_OP_ERROR(dir, "chmod"); \
136         } \
137 }
138
139 int main(int argc, char *argv[])
140 {
141         gchar *userrc;
142         MainWindow *mainwin;
143         FolderView *folderview;
144
145         setlocale(LC_ALL, "");
146         bindtextdomain(PACKAGE, LOCALEDIR);
147         textdomain(PACKAGE);
148
149         parse_cmd_opt(argc, argv);
150
151         gtk_set_locale();
152         gtk_init(&argc, &argv);
153
154 #if USE_THREADS || USE_LDAP
155         g_thread_init(NULL);
156         if (!g_thread_supported())
157                 g_error(_("g_thread is not supported by glib.\n"));
158 #endif
159
160 #if HAVE_GDK_IMLIB
161         gdk_imlib_init();
162         gtk_widget_push_visual(gdk_imlib_get_visual());
163         gtk_widget_push_colormap(gdk_imlib_get_colormap());
164 #endif
165
166 #if USE_SSL
167         ssl_init();
168 #endif
169
170         srandom((gint)time(NULL));
171
172         /* parse gtkrc files */
173         userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtkrc",
174                              NULL);
175         gtk_rc_parse(userrc);
176         g_free(userrc);
177         userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtk",
178                              G_DIR_SEPARATOR_S, "gtkrc", NULL);
179         gtk_rc_parse(userrc);
180         g_free(userrc);
181         userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "gtkrc", NULL);
182         gtk_rc_parse(userrc);
183         g_free(userrc);
184
185         gtk_rc_parse("./gtkrc");
186
187         userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
188         gtk_item_factory_parse_rc(userrc);
189         g_free(userrc);
190
191         prog_version = PROG_VERSION;
192         startup_dir = g_get_current_dir();
193
194         CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
195
196         /* check and create unix domain socket */
197         lock_socket = prohibit_duplicate_launch();
198         if (lock_socket < 0) return 0;
199
200         if (cmd.status) {
201                 puts("0 Sylpheed not running.");
202                 return 0;
203         }
204
205         /* backup if old rc file exists */
206         if (is_file_exist(RC_DIR)) {
207                 if (rename(RC_DIR, RC_DIR ".bak") < 0)
208                         FILE_OP_ERROR(RC_DIR, "rename");
209         }
210         MAKE_DIR_IF_NOT_EXIST(RC_DIR);
211         MAKE_DIR_IF_NOT_EXIST(get_imap_cache_dir());
212         MAKE_DIR_IF_NOT_EXIST(get_news_cache_dir());
213         MAKE_DIR_IF_NOT_EXIST(get_mime_tmp_dir());
214
215         if (is_file_exist(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log")) {
216                 if (rename(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log",
217                            RC_DIR G_DIR_SEPARATOR_S "sylpheed.log.bak") < 0)
218                         FILE_OP_ERROR("sylpheed.log", "rename");
219         }
220         set_log_file(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log");
221
222         if (is_file_exist(RC_DIR G_DIR_SEPARATOR_S "assortrc") &&
223             !is_file_exist(RC_DIR G_DIR_SEPARATOR_S "filterrc")) {
224                 if (rename(RC_DIR G_DIR_SEPARATOR_S "assortrc",
225                            RC_DIR G_DIR_SEPARATOR_S "filterrc") < 0)
226                         FILE_OP_ERROR(RC_DIR G_DIR_SEPARATOR_S "assortrc",
227                                       "rename");
228         }
229
230         prefs_common_init();
231         prefs_common_read_config();
232
233 #if USE_GPGME
234         if (gpgme_check_engine()) {  /* Also does some gpgme init */
235                 rfc2015_disable_all();
236                 debug_print("gpgme_engine_version:\n%s\n",
237                             gpgme_get_engine_info());
238
239                 if (prefs_common.gpg_warning) {
240                         AlertValue val;
241
242                         val = alertpanel_message_with_disable
243                                 (_("Warning"),
244                                  _("GnuPG is not installed properly.\n"
245                                    "OpenPGP support disabled."));
246                         if (val & G_ALERTDISABLE)
247                                 prefs_common.gpg_warning = FALSE;
248                 }
249         }
250         gpgme_register_idle(idle_function_for_gpgme);
251 #endif
252
253 #if USE_PSPELL
254         gtkpspellcheckers = gtkpspell_checkers_new();
255 #endif
256         
257
258         prefs_common_save_config();
259         prefs_filter_read_config();
260         prefs_filter_write_config();
261         prefs_display_header_read_config();
262         prefs_display_header_write_config();
263         /* prefs_filtering_read_config(); */
264         addressbook_read_file();
265         renderer_read_config();
266
267         gtkut_widget_init();
268
269         mainwin = main_window_create
270                 (prefs_common.sep_folder | prefs_common.sep_msg << 1);
271         folderview = mainwin->folderview;
272
273         /* register the callback of unix domain socket input */
274         lock_socket_tag = gdk_input_add(lock_socket,
275                                         GDK_INPUT_READ | GDK_INPUT_EXCEPTION,
276                                         lock_socket_input_cb,
277                                         mainwin);
278
279         account_read_config_all();
280         account_save_config_all();
281
282         if (folder_read_list() < 0) {
283                 setup(mainwin);
284                 folder_write_list();
285         }
286         if (!account_get_list()) {
287                 account_edit_open();
288                 account_add();
289         }
290
291         account_set_missing_folder();
292         folderview_set(folderview);
293
294         /* prefs_scoring_read_config(); */
295         prefs_matcher_read_config();
296         /* make one all-folder processing before using sylpheed */
297         processing_apply(mainwin->summaryview);
298
299         addressbook_read_file();
300
301         inc_autocheck_timer_init(mainwin);
302
303         if (cmd.receive_all || prefs_common.chk_on_startup)
304                 inc_all_account_mail(mainwin);
305         else if (cmd.receive)
306                 inc_mail(mainwin);
307         else
308                 gtk_widget_grab_focus(folderview->ctree);
309
310         if (cmd.compose)
311                 open_compose_new_with_recipient(cmd.compose_mailto);
312
313         if (cmd.send) {
314                 if (procmsg_send_queue() < 0)
315                         alertpanel_error(_("Some errors occurred while sending queued messages."));
316         }
317         
318         /* ignore SIGPIPE signal for preventing sudden death of program */
319         signal(SIGPIPE, SIG_IGN);
320
321         gtk_main();
322
323 #if USE_PSPELL       
324         gtkpspell_checkers_delete();
325 #endif
326
327         return 0;
328 }
329
330 static void parse_cmd_opt(int argc, char *argv[])
331 {
332         gint i;
333
334         for (i = 1; i < argc; i++) {
335                 if (!strncmp(argv[i], "--debug", 7))
336                         debug_mode = TRUE;
337                 else if (!strncmp(argv[i], "--receive-all", 13))
338                         cmd.receive_all = TRUE;
339                 else if (!strncmp(argv[i], "--receive", 9))
340                         cmd.receive = TRUE;
341                 else if (!strncmp(argv[i], "--compose", 9)) {
342                         const gchar *p = argv[i + 1];
343
344                         cmd.compose = TRUE;
345                         cmd.compose_mailto = NULL;
346                         if (p && *p != '\0' && *p != '-') {
347                                 if (!strncmp(p, "mailto:", 7))
348                                         cmd.compose_mailto = p + 7;
349                                 else
350                                         cmd.compose_mailto = p;
351                                 i++;
352                         }
353                 } else if (!strncmp(argv[i], "--version", 9)) {
354                         puts("Sylpheed version " VERSION);
355                         exit(0);
356                 } else if (!strncmp(argv[i], "--status", 8)) {
357                         cmd.status = TRUE;
358                 } else if (!strncmp(argv[i], "--send", 6)) {
359                         cmd.send = TRUE;
360                 } else if (!strncmp(argv[i], "--help", 6)) {
361                         g_print(_("Usage: %s [OPTION]...\n"),
362                                 g_basename(argv[0]));
363
364                         puts(_("  --compose [address]    open composition window"));
365                         puts(_("  --receive              receive new messages"));
366                         puts(_("  --receive-all          receive new messages of all accounts"));
367                         puts(_("  --send                 send all queued messages"));
368                         puts(_("  --status               show the total number of messages"));
369                         puts(_("  --debug                debug mode"));
370                         puts(_("  --help                 display this help and exit"));
371                         puts(_("  --version              output version information and exit"));
372
373                         exit(1);
374                 }
375         }
376 }
377
378 static gint get_queued_message_num(void)
379 {
380         FolderItem *queue;
381
382         queue = folder_get_default_queue();
383         if (!queue) return -1;
384
385         folder_item_scan(queue);
386         return queue->total;
387 }
388
389 void app_will_exit(GtkWidget *widget, gpointer data)
390 {
391         MainWindow *mainwin = data;
392         gchar *filename;
393
394         if (compose_get_compose_list()) {
395                 if (alertpanel(_("Notice"),
396                                _("Composing message exists. Really quit?"),
397                                _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT)
398                         return;
399                 manage_window_focus_in(mainwin->window, NULL, NULL);
400         }
401
402         if (prefs_common.warn_queued_on_exit && get_queued_message_num() > 0) {
403                 if (alertpanel(_("Queued messages"),
404                                _("Some unsent messages are queued. Exit now?"),
405                                _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT)
406                         return;
407                 manage_window_focus_in(mainwin->window, NULL, NULL);
408         }
409
410         inc_autocheck_timer_remove();
411
412         if (prefs_common.clean_on_exit)
413                 main_window_empty_trash(mainwin, prefs_common.ask_on_clean);
414
415         /* save all state before exiting */
416         folder_write_list();
417         summary_write_cache(mainwin->summaryview);
418
419         main_window_get_size(mainwin);
420         main_window_get_position(mainwin);
421         prefs_common_save_config();
422         prefs_filter_write_config();
423         account_save_config_all();
424         addressbook_export_to_file();
425
426         filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
427         gtk_item_factory_dump_rc(filename, NULL, TRUE);
428         g_free(filename);
429
430         /* delete temporary files */
431         remove_all_files(get_mime_tmp_dir());
432
433         close_log_file();
434
435         /* delete unix domain socket */
436         gdk_input_remove(lock_socket_tag);
437         fd_close(lock_socket);
438         filename = get_socket_name();
439         unlink(filename);
440
441 #if USE_SSL
442         ssl_done();
443 #endif
444
445         gtk_main_quit();
446 }
447
448 #if USE_GPGME
449 static void idle_function_for_gpgme(void)
450 {
451         while (gtk_events_pending())
452                 gtk_main_iteration();
453 }
454 #endif /* USE_GPGME */
455
456 static gchar *get_socket_name(void)
457 {
458         static gchar *filename = NULL;
459
460         if (filename == NULL) {
461                 filename = g_strdup_printf("%s%csylpheed-%d",
462                                            g_get_tmp_dir(), G_DIR_SEPARATOR,
463                                            getuid());
464         }
465
466         return filename;
467 }
468
469 static gint prohibit_duplicate_launch(void)
470 {
471         gint uxsock;
472         gchar *path;
473
474         path = get_socket_name();
475         uxsock = fd_connect_unix(path);
476         if (uxsock < 0) {
477                 unlink(path);
478                 return fd_open_unix(path);
479         }
480
481         /* remote command mode */
482
483         debug_print(_("another Sylpheed is already running.\n"));
484
485         if (cmd.receive_all)
486                 fd_write(uxsock, "receive_all\n", 12);
487         else if (cmd.receive)
488                 fd_write(uxsock, "receive\n", 8);
489         else if (cmd.send) {
490                 gchar buf[BUFFSIZE];
491
492                 fd_write(uxsock, "send\n", 5);
493                 fd_gets(uxsock, buf, sizeof(buf));
494                 fputs(buf, stdout);
495         }
496         else if (cmd.compose) {
497                 gchar *compose_str;
498
499                 if (cmd.compose_mailto)
500                         compose_str = g_strdup_printf("compose %s\n", cmd.compose_mailto);
501                 else
502                         compose_str = g_strdup("compose\n");
503
504                 fd_write(uxsock, compose_str, strlen(compose_str));
505                 g_free(compose_str);
506         } else if (cmd.status) {
507                 gchar buf[BUFFSIZE];
508
509                 fd_write(uxsock, "status\n", 7);
510                 fd_gets(uxsock, buf, sizeof(buf));
511                 fputs(buf, stdout);
512         } else
513                 fd_write(uxsock, "popup\n", 6);
514
515         fd_close(uxsock);
516         return -1;
517 }
518
519 static void lock_socket_input_cb(gpointer data,
520                                  gint source,
521                                  GdkInputCondition condition)
522 {
523         MainWindow *mainwin = (MainWindow *)data;
524         gint sock;
525         gchar buf[BUFFSIZE];
526
527         sock = fd_accept(source);
528         fd_gets(sock, buf, sizeof(buf));
529
530         if (!strncmp(buf, "popup", 5)){
531                 main_window_popup(mainwin);
532         } else if (!strncmp(buf, "receive_all", 11)){
533                 main_window_popup(mainwin);
534                 inc_all_account_mail(mainwin);
535         } else if (!strncmp(buf, "receive", 7)){
536                 main_window_popup(mainwin);
537                 inc_mail(mainwin);
538         } else if (!strncmp(buf, "send", 4)) {
539                 gint queued = get_queued_message_num();
540
541                 if (queued > 0) {
542                         if (procmsg_send_queue() < 0)
543                                 g_snprintf(buf, sizeof(buf),
544                                         "%s\n", /* avoids adding another translatable */
545                                         _("Some errors occurred while sending queued messages."));
546                         else    /* queue sent ok */
547                                 g_snprintf(buf, sizeof(buf), _("\%d queued message(s) sent\n"), queued);
548                 } 
549                 else {
550                         g_snprintf(buf, sizeof(buf), "%s\n", _("No queued messages found."));
551                 }
552                 fd_write(sock, buf, strlen(buf));
553         } else if (!strncmp(buf, "compose", 7)) {
554                 open_compose_new_with_recipient(buf + strlen("compose") + 1);
555         } else if (!strncmp(buf, "status", 6)) {
556                 guint new, unread, total;
557
558                 folder_count_total_msgs(&new, &unread, &total);
559                 g_snprintf(buf, sizeof(buf), "%d %d %d\n", new, unread, total);
560                 fd_write(sock, buf, strlen(buf));
561         }
562
563         fd_close(sock);
564 }
565
566 static void open_compose_new_with_recipient(const gchar *address)
567 {
568         gchar *addr = NULL;
569                         
570         if (address) {
571                 Xstrdup_a(addr, address, return);
572                 g_strstrip(addr);
573         }
574
575         if (addr && *addr != '\0')
576                 compose_new_with_recipient(NULL, addr);
577         else
578                 compose_new(NULL);
579 }