try to authenticate with CRAM-MD5
[claws.git] / src / main.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2002 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 "statusbar.h"
69 #include "addressbook.h"
70 #include "compose.h"
71 #include "folder.h"
72 #include "setup.h"
73 #include "utils.h"
74 #include "gtkutils.h"
75
76 #if USE_GPGME
77 #  include "rfc2015.h"
78 #endif
79 #if USE_SSL
80 #  include "ssl.h"
81 #endif
82
83 #include "version.h"
84
85 gchar *prog_version;
86 gchar *startup_dir;
87 gboolean debug_mode = FALSE;
88
89 static gint lock_socket = -1;
90 static gint lock_socket_tag = 0;
91
92 static struct Cmd {
93         gboolean receive;
94         gboolean receive_all;
95         gboolean compose;
96         const gchar *compose_mailto;
97         gboolean status;
98         gboolean send;
99 } cmd;
100
101 static void parse_cmd_opt(int argc, char *argv[]);
102
103 #if USE_GPGME
104 static void idle_function_for_gpgme(void);
105 #endif /* USE_GPGME */
106
107 static gint prohibit_duplicate_launch   (void);
108 static void lock_socket_input_cb        (gpointer          data,
109                                          gint              source,
110                                          GdkInputCondition condition);
111 static gchar *get_socket_name           (void);
112
113 static void open_compose_new_with_recipient     (const gchar    *address);
114
115 static void send_queue                  (void);
116
117 #if 0
118 /* for gettext */
119 _("File `%s' already exists.\n"
120   "Can't create folder.")
121 #endif
122
123 #define MAKE_DIR_IF_NOT_EXIST(dir) \
124 { \
125         if (!is_dir_exist(dir)) { \
126                 if (is_file_exist(dir)) { \
127                         alertpanel_warning \
128                                 (_("File `%s' already exists.\n" \
129                                    "Can't create folder."), \
130                                  dir); \
131                         return 1; \
132                 } \
133                 if (mkdir(dir, S_IRWXU) < 0) { \
134                         FILE_OP_ERROR(dir, "mkdir"); \
135                         return 1; \
136                 } \
137                 if (chmod(dir, S_IRWXU) < 0) \
138                         FILE_OP_ERROR(dir, "chmod"); \
139         } \
140 }
141
142 int main(int argc, char *argv[])
143 {
144         gchar *userrc;
145         MainWindow *mainwin;
146         FolderView *folderview;
147
148         setlocale(LC_ALL, "");
149         bindtextdomain(PACKAGE, LOCALEDIR);
150         textdomain(PACKAGE);
151
152         parse_cmd_opt(argc, argv);
153
154         gtk_set_locale();
155         gtk_init(&argc, &argv);
156
157 #if USE_THREADS || USE_LDAP
158         g_thread_init(NULL);
159         if (!g_thread_supported())
160                 g_error(_("g_thread is not supported by glib.\n"));
161 #endif
162
163 #if HAVE_GDK_IMLIB
164         gdk_imlib_init();
165         gtk_widget_push_visual(gdk_imlib_get_visual());
166         gtk_widget_push_colormap(gdk_imlib_get_colormap());
167 #endif
168
169 #if USE_SSL
170         ssl_init();
171 #endif
172
173         srandom((gint)time(NULL));
174
175         /* parse gtkrc files */
176         userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtkrc",
177                              NULL);
178         gtk_rc_parse(userrc);
179         g_free(userrc);
180         userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtk",
181                              G_DIR_SEPARATOR_S, "gtkrc", NULL);
182         gtk_rc_parse(userrc);
183         g_free(userrc);
184         userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "gtkrc", NULL);
185         gtk_rc_parse(userrc);
186         g_free(userrc);
187
188         gtk_rc_parse("./gtkrc");
189
190         userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
191         gtk_item_factory_parse_rc(userrc);
192         g_free(userrc);
193
194         prog_version = PROG_VERSION;
195         startup_dir = g_get_current_dir();
196
197         CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
198
199         /* check and create unix domain socket */
200         lock_socket = prohibit_duplicate_launch();
201         if (lock_socket < 0) return 0;
202
203         if (cmd.status) {
204                 puts("0 Sylpheed not running.");
205                 return 0;
206         }
207
208         /* backup if old rc file exists */
209         if (is_file_exist(RC_DIR)) {
210                 if (rename(RC_DIR, RC_DIR ".bak") < 0)
211                         FILE_OP_ERROR(RC_DIR, "rename");
212         }
213         MAKE_DIR_IF_NOT_EXIST(RC_DIR);
214         MAKE_DIR_IF_NOT_EXIST(get_imap_cache_dir());
215         MAKE_DIR_IF_NOT_EXIST(get_news_cache_dir());
216         MAKE_DIR_IF_NOT_EXIST(get_mime_tmp_dir());
217
218         if (is_file_exist(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log")) {
219                 if (rename(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log",
220                            RC_DIR G_DIR_SEPARATOR_S "sylpheed.log.bak") < 0)
221                         FILE_OP_ERROR("sylpheed.log", "rename");
222         }
223         set_log_file(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log");
224
225         if (is_file_exist(RC_DIR G_DIR_SEPARATOR_S "assortrc") &&
226             !is_file_exist(RC_DIR G_DIR_SEPARATOR_S "filterrc")) {
227                 if (rename(RC_DIR G_DIR_SEPARATOR_S "assortrc",
228                            RC_DIR G_DIR_SEPARATOR_S "filterrc") < 0)
229                         FILE_OP_ERROR(RC_DIR G_DIR_SEPARATOR_S "assortrc",
230                                       "rename");
231         }
232
233         prefs_common_init();
234         prefs_common_read_config();
235
236 #if USE_GPGME
237         if (gpgme_check_engine()) {  /* Also does some gpgme init */
238                 rfc2015_disable_all();
239                 debug_print("gpgme_engine_version:\n%s\n",
240                             gpgme_get_engine_info());
241
242                 if (prefs_common.gpg_warning) {
243                         AlertValue val;
244
245                         val = alertpanel_message_with_disable
246                                 (_("Warning"),
247                                  _("GnuPG is not installed properly.\n"
248                                    "OpenPGP support disabled."));
249                         if (val & G_ALERTDISABLE)
250                                 prefs_common.gpg_warning = FALSE;
251                 }
252         }
253         gpgme_register_idle(idle_function_for_gpgme);
254 #endif
255
256 #if USE_PSPELL
257         gtkpspellcheckers = gtkpspell_checkers_new();
258 #endif
259         
260
261         prefs_common_save_config();
262         prefs_filter_read_config();
263         prefs_filter_write_config();
264         prefs_actions_read_config();
265         prefs_actions_write_config();
266         prefs_display_header_read_config();
267         prefs_display_header_write_config();
268         /* prefs_filtering_read_config(); */
269         addressbook_read_file();
270         renderer_read_config();
271
272         gtkut_widget_init();
273
274         mainwin = main_window_create
275                 (prefs_common.sep_folder | prefs_common.sep_msg << 1);
276         folderview = mainwin->folderview;
277
278         /* register the callback of unix domain socket input */
279         lock_socket_tag = gdk_input_add(lock_socket,
280                                         GDK_INPUT_READ | GDK_INPUT_EXCEPTION,
281                                         lock_socket_input_cb,
282                                         mainwin);
283
284         account_read_config_all();
285         account_save_config_all();
286
287         if (folder_read_list() < 0) {
288                 setup(mainwin);
289                 folder_write_list();
290         }
291         if (!account_get_list()) {
292                 account_edit_open();
293                 account_add();
294         }
295
296         account_set_missing_folder();
297         folder_set_missing_folders();
298         folderview_set(folderview);
299
300         /* prefs_scoring_read_config(); */
301         prefs_matcher_read_config();
302         /* make one all-folder processing before using sylpheed */
303         processing_apply(mainwin->summaryview);
304
305         addressbook_read_file();
306
307         inc_autocheck_timer_init(mainwin);
308
309         /* ignore SIGPIPE signal for preventing sudden death of program */
310         signal(SIGPIPE, SIG_IGN);
311
312         if (cmd.receive_all || prefs_common.chk_on_startup)
313                 inc_all_account_mail(mainwin, prefs_common.newmail_notify_manu);
314         else if (cmd.receive)
315                 inc_mail(mainwin, prefs_common.newmail_notify_manu);
316         else
317                 gtk_widget_grab_focus(folderview->ctree);
318
319         if (cmd.compose)
320                 open_compose_new_with_recipient(cmd.compose_mailto);
321         if (cmd.send)
322                 send_queue();
323
324         gtk_main();
325
326         addressbook_destroy();
327
328 #if USE_PSPELL       
329         gtkpspell_checkers_delete();
330 #endif
331
332         return 0;
333 }
334
335 static void parse_cmd_opt(int argc, char *argv[])
336 {
337         gint i;
338
339         for (i = 1; i < argc; i++) {
340                 if (!strncmp(argv[i], "--debug", 7))
341                         debug_mode = TRUE;
342                 else if (!strncmp(argv[i], "--receive-all", 13))
343                         cmd.receive_all = TRUE;
344                 else if (!strncmp(argv[i], "--receive", 9))
345                         cmd.receive = TRUE;
346                 else if (!strncmp(argv[i], "--compose", 9)) {
347                         const gchar *p = argv[i + 1];
348
349                         cmd.compose = TRUE;
350                         cmd.compose_mailto = NULL;
351                         if (p && *p != '\0' && *p != '-') {
352                                 if (!strncmp(p, "mailto:", 7))
353                                         cmd.compose_mailto = p + 7;
354                                 else
355                                         cmd.compose_mailto = p;
356                                 i++;
357                         }
358                 } else if (!strncmp(argv[i], "--send", 6)) {
359                         cmd.send = TRUE;
360                 } else if (!strncmp(argv[i], "--version", 9)) {
361                         puts("Sylpheed version " VERSION);
362                         exit(0);
363                 } else if (!strncmp(argv[i], "--status", 8)) {
364                         cmd.status = TRUE;
365                 } else if (!strncmp(argv[i], "--help", 6)) {
366                         g_print(_("Usage: %s [OPTION]...\n"),
367                                 g_basename(argv[0]));
368
369                         puts(_("  --compose [address]    open composition window"));
370                         puts(_("  --receive              receive new messages"));
371                         puts(_("  --receive-all          receive new messages of all accounts"));
372                         puts(_("  --send                 send all queued messages"));
373                         puts(_("  --status               show the total number of messages"));
374                         puts(_("  --debug                debug mode"));
375                         puts(_("  --help                 display this help and exit"));
376                         puts(_("  --version              output version information and exit"));
377
378                         exit(1);
379                 }
380         }
381 }
382
383 static gint get_queued_message_num(void)
384 {
385         FolderItem *queue;
386
387         queue = folder_get_default_queue();
388         if (!queue) return -1;
389
390         folder_item_scan(queue);
391         return queue->total;
392 }
393
394 void app_will_exit(GtkWidget *widget, gpointer data)
395 {
396         MainWindow *mainwin = data;
397         gchar *filename;
398
399         if (compose_get_compose_list()) {
400                 if (alertpanel(_("Notice"),
401                                _("Composing message exists. Really quit?"),
402                                _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT)
403                         return;
404                 manage_window_focus_in(mainwin->window, NULL, NULL);
405         }
406
407         if (prefs_common.warn_queued_on_exit && get_queued_message_num() > 0) {
408                 if (alertpanel(_("Queued messages"),
409                                _("Some unsent messages are queued. Exit now?"),
410                                _("OK"), _("Cancel"), NULL) != G_ALERTDEFAULT)
411                         return;
412                 manage_window_focus_in(mainwin->window, NULL, NULL);
413         }
414
415         inc_autocheck_timer_remove();
416
417         if (prefs_common.clean_on_exit)
418                 main_window_empty_trash(mainwin, prefs_common.ask_on_clean);
419
420         /* save all state before exiting */
421         folder_write_list();
422         summary_write_cache(mainwin->summaryview);
423
424         main_window_get_size(mainwin);
425         main_window_get_position(mainwin);
426         prefs_common_save_config();
427         prefs_filter_write_config();
428         account_save_config_all();
429         addressbook_export_to_file();
430
431         filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
432         gtk_item_factory_dump_rc(filename, NULL, TRUE);
433         g_free(filename);
434
435         /* delete temporary files */
436         remove_all_files(get_mime_tmp_dir());
437
438         close_log_file();
439
440         /* delete unix domain socket */
441         gdk_input_remove(lock_socket_tag);
442         fd_close(lock_socket);
443         filename = get_socket_name();
444         unlink(filename);
445
446 #if USE_SSL
447         ssl_done();
448 #endif
449
450         gtk_main_quit();
451 }
452
453 #if USE_GPGME
454 static void idle_function_for_gpgme(void)
455 {
456         while (gtk_events_pending())
457                 gtk_main_iteration();
458 }
459 #endif /* USE_GPGME */
460
461 static gchar *get_socket_name(void)
462 {
463         static gchar *filename = NULL;
464
465         if (filename == NULL) {
466                 filename = g_strdup_printf("%s%csylpheed-%d",
467                                            g_get_tmp_dir(), G_DIR_SEPARATOR,
468                                            getuid());
469         }
470
471         return filename;
472 }
473
474 static gint prohibit_duplicate_launch(void)
475 {
476         gint uxsock;
477         gchar *path;
478
479         path = get_socket_name();
480         uxsock = fd_connect_unix(path);
481         if (uxsock < 0) {
482                 unlink(path);
483                 return fd_open_unix(path);
484         }
485
486         /* remote command mode */
487
488         debug_print(_("another Sylpheed is already running.\n"));
489
490         if (cmd.receive_all)
491                 fd_write(uxsock, "receive_all\n", 12);
492         else if (cmd.receive)
493                 fd_write(uxsock, "receive\n", 8);
494         else if (cmd.compose) {
495                 gchar *compose_str;
496
497                 if (cmd.compose_mailto)
498                         compose_str = g_strdup_printf("compose %s\n", cmd.compose_mailto);
499                 else
500                         compose_str = g_strdup("compose\n");
501
502                 fd_write(uxsock, compose_str, strlen(compose_str));
503                 g_free(compose_str);
504         } else if (cmd.send) {
505                 fd_write(uxsock, "send\n", 5);
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, prefs_common.newmail_notify_manu);
535         } else if (!strncmp(buf, "receive", 7)){
536                 main_window_popup(mainwin);
537                 inc_mail(mainwin, prefs_common.newmail_notify_manu);
538         } else if (!strncmp(buf, "compose", 7)) {
539                 open_compose_new_with_recipient(buf + strlen("compose") + 1);
540         } else if (!strncmp(buf, "send", 4)) {
541                 send_queue();
542         } else if (!strncmp(buf, "status", 6)) {
543                 guint new, unread, total;
544
545                 folder_count_total_msgs(&new, &unread, &total);
546                 g_snprintf(buf, sizeof(buf), "%d %d %d\n", new, unread, total);
547                 fd_write(sock, buf, strlen(buf));
548         }
549
550         fd_close(sock);
551 }
552
553 static void open_compose_new_with_recipient(const gchar *address)
554 {
555         gchar *addr = NULL;
556
557         if (address) {
558                 Xstrdup_a(addr, address, return);
559                 g_strstrip(addr);
560         }
561
562         if (addr && *addr != '\0')
563                 compose_new_with_recipient(NULL, addr);
564         else
565                 compose_new(NULL);
566 }
567
568 static void send_queue(void)
569 {
570         GList *list;
571         FolderItem *def_outbox;
572
573         def_outbox = folder_get_default_outbox();
574
575         for (list = folder_get_list(); list != NULL; list = list->next) {
576                 Folder *folder = list->data;
577
578                 if (folder->queue) {
579                         if (procmsg_send_queue
580                                 (folder->queue, prefs_common.savemsg) < 0)
581                                 alertpanel_error(_("Some errors occurred while sending queued messages."));
582                         statusbar_pop_all();
583                         folder_item_scan(folder->queue);
584                         folderview_update_item(folder->queue, TRUE);
585                         if (prefs_common.savemsg && folder->outbox) {
586                                 folderview_update_item(folder->outbox, TRUE);
587                                 if (folder->outbox == def_outbox)
588                                         def_outbox = NULL;
589                         }
590                 }
591         }
592
593         if (prefs_common.savemsg && def_outbox)
594                 folderview_update_item(def_outbox, TRUE);
595 }