Have an event loop thread.
[apps/madmutt.git] / main.c
1 /*
2  * Copyright notice from original mutt:
3  * Copyright (C) 1996-2002 Michael R. Elkins <me@mutt.org>
4  * Copyright (C) 1999-2002 Thomas Roessler <roessler@does-not-exist.org>
5  * Copyright (C) 2004 g10 Code GmbH
6  *
7  * Parts written/modified by:
8  * Nico Golde <nion@muttng.org>
9  * Andreas Krennmair <ak@synflood.at>
10  *
11  * This file is part of mutt-ng, see http://www.muttng.org/.
12  * It's licensed under the GNU General Public License,
13  * please see the file GPL in the top level source directory.
14  */
15
16 #define MAIN_C 1
17
18 #include <lib-lib/lib-lib.h>
19
20 #include <sys/utsname.h>
21 #include <pthread.h>
22
23 #include <lib-lua/lib-lua.h>
24 #include <lib-sys/mutt_signal.h>
25 #include <lib-sys/evtloop.h>
26 #include <lib-mime/mime.h>
27 #include <lib-ui/lib-ui.h>
28 #include <lib-mx/mx.h>
29
30 #include "mutt.h"
31 #include "crypt.h"
32 #include "alias.h"
33 #include "buffy.h"
34 #include "sort.h"
35 #include "keymap.h"
36 #include "mutt_idna.h"
37 #include "mutt_sasl.h"
38
39 #ifdef HAVE_GETOPT_H
40 #include <getopt.h>
41 #else
42 extern char *optarg;
43 extern int optind;
44 #endif
45
46 #ifdef HAVE_LIBIDN
47 #include <stringprep.h>
48 #endif
49
50 #ifdef USE_HCACHE
51 #if defined(HAVE_TOKYOCABINET)
52 #include <tcutil.h>
53 #elif defined(HAVE_GDBM)
54 #include <gdbm.h>
55 #endif
56 #endif
57
58 #include <gnutls/gnutls.h>
59 #include <gpgme.h>
60
61 #include <libintl.h>
62
63 void mutt_exit(int code)
64 {
65     mutt_endwin(NULL);
66     exit(code);
67 }
68
69 static void mutt_usage (void)
70 {
71     puts(madmutt_version);
72
73     puts(_("\
74 usage: madmutt [ -nRyzZ ] [ -e <cmd> ] [ -F <file> ] [ -f <file> ]\n\
75        madmutt [ -n ] [ -e <cmd> ] [ -a <file> ] [ -F <file> ] [ -H <file> ] [ -i <file> ] [ -s <subj> ] [ -b <addr> ] [ -c <addr> ] <addr> [ ... ]\n\
76        madmutt [ -n ] [ -e <cmd> ] [ -F <file> ] -p\n\
77        madmutt -v\n"));
78
79     puts(_("Options:"));
80     puts(_("  -a <file>     attach a file to the message"));
81     puts(_("  -b <address>  specify a blind carbon-copy (BCC) address"));
82     puts(_("  -c <address>  specify a carbon-copy (CC) address"));
83     puts(_("  -e <command>  specify a command to be executed after initialization"));
84     puts(_("  -f <file>     specify which mailbox to read"));
85     puts(_("  -F <file>     specify an alternate Madmuttrc file"));
86     puts(_("  -H <file>     specify a draft file to read header and body from"));
87     puts(_("  -i <file>     specify a file which Madmutt should include in the body"));
88     puts(_("  -n            causes Madmutt not to read the system Madmuttrc"));
89     puts(_("  -p            recall a postponed message"));
90     puts(_("  -R            open mailbox in read-only mode"));
91     puts(_("  -s <subj>     specify a subject (must be in quotes if it has spaces)"));
92     puts(_("  -v            show version and compile-time definitions"));
93     puts(_("  -z            exit immediately if there are no messages in the mailbox"));
94     puts(_("  -Z            open the first folder with new message, exit immediately if none"));
95     puts(_("  -h            this help message"));
96
97     exit(0);
98 }
99
100 static void show_version (void)
101 {
102     struct utsname uts;
103     uname(&uts);
104
105     puts(madmutt_version);
106     puts(_("  Copyright (C) 1996-2002 Michael R. Elkins and others."));
107     puts(_("  Copyright (C) 2005      The Mutt-ng Team"));
108     puts(_("  Copyright (C) 2006-2007 Pierre Habouzit"));
109     puts(_("  MadMutt is based on Mutt-ng wich was based on Mutt before"));
110     puts("");
111
112     printf("System:\n  %s %s (%s)\n", uts.sysname, uts.release, uts.machine);
113     puts("External Libraries:");
114 #ifdef NCURSES_VERSION
115     printf("  ncurses %s\n", NCURSES_VERSION);
116 #endif
117 #ifdef _LIBICONV_VERSION
118     printf("  libiconv %d.%d\n", _LIBICONV_VERSION >> 8,
119            _LIBICONV_VERSION & 0xff);
120 #endif
121 #ifdef STRINGPREP_VERSION
122     printf("  libidn %s\n", STRINGPREP_VERSION);
123 #endif
124 #ifdef USE_HCACHE
125 #if defined(HAVE_TOKYOCABINET)
126     printf("  tokyocabinet %s\n", tcversion);
127 #elif defined(HAVE_GDBM)
128     printf("  gdbm %s\n", gdbm_version);
129 #endif
130 #endif
131     printf("  gnutls %s\n", LIBGNUTLS_VERSION);
132     printf("  gpgme %s\n",  GPGME_VERSION);
133     puts (_("Compile Options:"));
134
135     puts (
136 #ifdef USE_HCACHE
137         "  +USE_HCACHE"
138 #else
139         "  -USE_HCACHE"
140 #endif
141 #ifdef HAVE_LIBIDN
142         "  +HAVE_LIBIDN"
143 #else
144         "  -HAVE_LIBIDN"
145 #endif
146         );
147
148     puts(_("Built-In Defaults:"));
149     printf("  SENDMAIL   \"%s\"\n", SENDMAIL);
150     printf("  MAILPATH   \"%s\"\n", MAILPATH);
151     printf("  PKGDATADIR \"%s\"\n", PKGDATADIR);
152     printf("  PKGDOCDIR  \"%s\"\n", PKGDOCDIR);
153     printf("  SYSCONFDIR \"%s\"\n", SYSCONFDIR);
154
155     puts("");
156     puts(_("This is free software.  You may redistribute copies of it under the terms of"));
157     puts(_("the GNU General Public License <http://www.gnu.org/licenses/gpl.html>."));
158     puts(_("There is NO WARRANTY, to the extent permitted by law."));
159
160     exit(0);
161 }
162
163 #define M_IGNORE  (1<<0)        /* -z */
164 #define M_BUFFY   (1<<1)        /* -Z */
165 #define M_NOSYSRC (1<<2)        /* -n */
166 #define M_RO      (1<<3)        /* -R */
167
168 __attribute__((format(printf, 1, 0)))
169 static void mutt_nocurses_error (const char *fmt, ...)
170 {
171     va_list ap;
172
173     va_start(ap, fmt);
174     vfprintf(stderr, fmt, ap);
175     va_end(ap);
176     fputc('\n', stderr);
177 }
178
179 static void *evtloop(void *data)
180 {
181     for (;;) {
182         el_dispatch(100);
183         pthread_testcancel();
184     }
185 }
186
187 int main (int argc, char **argv)
188 {
189   char folder[_POSIX_PATH_MAX] = "";
190   char *subject = NULL;
191   char *includeFile = NULL;
192   char *draftFile = NULL;
193   HEADER *msg = NULL;
194   string_list_t *attach = NULL;
195   string_list_t *commands = NULL;
196   int sendflags = 0;
197   int flags = 0;
198   int version = 0;
199   int i;
200   int explicit_folder = 0;
201   pthread_t pt;
202
203   /* initialize random number for tmp file creation */ 
204   srand48((unsigned int) time (NULL));
205   
206   /* sanity check against stupid administrators */
207   
208   if (getegid () != getgid ()) {
209     fprintf (stderr, "%s: I don't want to run with privileges!\n", argv[0]);
210     exit (1);
211   }
212
213   setlocale (LC_ALL, "");
214   bindtextdomain (PACKAGE, MUTTLOCALEDIR);
215   textdomain (PACKAGE);
216   setlocale (LC_CTYPE, "");
217
218   mutt_error = mutt_message = mutt_nocurses_error;
219   srand48 (time (NULL));
220   umask (077);
221
222   while ((i = getopt(argc, argv, "a:b:F:f:c:e:H:s:i:hnpRTtvzZ")) >= 0)
223     switch (i) {
224     case 'a':
225       if (strlen(optarg)<=512)
226         attach = mutt_add_list (attach, optarg);
227       else{
228         printf("too long arguments. exiting ...\n");
229         exit(1);
230       }
231       break;
232
233     case 'F':
234       m_strreplace(&Muttrc, optarg);
235       break;
236
237     case 'f':
238       m_strcpy(folder, sizeof(folder), optarg);
239       explicit_folder = 1;
240       break;
241
242     case 'b':
243     case 'c':
244       if (!msg)
245         msg = header_new();
246       if (!msg->env)
247         msg->env = envelope_new();
248       if (i == 'b')
249         msg->env->bcc = rfc822_parse_adrlist (msg->env->bcc, optarg);
250       else
251         msg->env->cc = rfc822_parse_adrlist (msg->env->cc, optarg);
252       break;
253
254     case 'e':
255       commands = mutt_add_list (commands, optarg);
256       break;
257
258     case 'H':
259       draftFile = optarg;
260       break;
261
262     case 'i':
263       includeFile = optarg;
264       break;
265
266     case 'n':
267       flags |= M_NOSYSRC;
268       break;
269
270     case 'p':
271       sendflags |= SENDPOSTPONED;
272       break;
273
274     case 'R':
275       flags |= M_RO;            /* read-only mode */
276       break;
277
278     case 's':
279       subject = optarg;
280       break;
281
282     case 'v':
283       version++;
284       break;
285
286     case 'z':
287       flags |= M_IGNORE;
288       break;
289
290     case 'Z':
291       flags |= M_BUFFY | M_IGNORE;
292       break;
293
294     default:
295       mutt_usage ();
296     }
297
298   if (version) {
299     show_version ();
300   }
301
302   /* Check for a batch send. */
303   if (!isatty (0)) {
304     set_option(OPTNOCURSES);
305     sendflags = SENDBATCH;
306   }
307
308   /* This must come before mutt_init() because curses needs to be started
309      before calling the init_pair() function to set the color scheme.  */
310   if (!option (OPTNOCURSES)) {
311     km_init();
312     curses_initialize();
313     mutt_signal_initialize();
314   }
315
316   /* set defaults and read init files */
317   el_initialize();
318   mutt_init (flags & M_NOSYSRC, commands);
319   string_list_wipe(&commands);
320   pthread_create(&pt, NULL, &evtloop, NULL);
321
322   if (!option(OPTNOCURSES)) {
323       ui_layout_init();
324   }
325
326   /* Create the Maildir directory if it doesn't exist. */
327   if (!option(OPTNOCURSES) && Maildir) {
328     struct stat sb;
329     char fpath[_POSIX_PATH_MAX];
330     char mesg[STRING];
331
332     m_strcpy(fpath, sizeof(fpath), Maildir);
333     mutt_expand_path (fpath, sizeof (fpath));
334     /* we're not connected yet - skip mail folder creation */
335     if (mx_get_magic (fpath) != M_IMAP)
336       if (stat (fpath, &sb) == -1 && errno == ENOENT) {
337         snprintf (mesg, sizeof (mesg), _("%s does not exist. Create it?"),
338                   Maildir);
339         if (mutt_yesorno (mesg, M_YES) == M_YES) {
340           if (mkdir (fpath, 0700) == -1 && errno != EEXIST)
341             mutt_error (_("Can't create %s: %s."), Maildir, strerror (errno));
342         }
343       }
344   }
345
346   if (sendflags & SENDPOSTPONED) {
347     if (!option (OPTNOCURSES))
348       mutt_flushinp ();
349     ci_send_message (SENDPOSTPONED, NULL, NULL, NULL, NULL);
350     mutt_endwin (NULL);
351   }
352   else if (subject || msg || sendflags || draftFile || includeFile || attach
353            || optind < argc) {
354     FILE *fin = NULL;
355     char buf[LONG_STRING];
356     char *tempfile = NULL, *infile = NULL;
357     char *bodytext = NULL;
358     FILE *fout;
359
360     if (!option (OPTNOCURSES))
361       mutt_flushinp ();
362
363     if (!msg)
364       msg = header_new();
365
366     if (draftFile)
367       infile = draftFile;
368     else {
369       if (!msg->env)
370         msg->env = envelope_new();
371
372       for (i = optind; i < argc; i++) {
373         if (url_check_scheme (argv[i]) == U_MAILTO)
374           url_parse_mailto (msg->env, &bodytext, argv[i]);
375         else
376           msg->env->to = rfc822_parse_adrlist (msg->env->to, argv[i]);
377       }
378
379       if (option (OPTAUTOEDIT) && !msg->env->to && !msg->env->cc) {
380         if (!option (OPTNOCURSES))
381           mutt_endwin (NULL);
382         fputs (_("No recipients specified.\n"), stderr);
383         exit (1);
384       }
385
386       if (subject)
387         msg->env->subject = m_strdup(subject);
388
389       if (includeFile)
390         infile = includeFile;
391     }
392
393     if (infile || bodytext) {
394       if (infile) {
395         if (m_strcmp("-", infile) == 0)
396           fin = stdin;
397         else {
398           char path[_POSIX_PATH_MAX];
399
400           m_strcpy(path, sizeof(path), infile);
401           mutt_expand_path (path, sizeof (path));
402           if ((fin = fopen (path, "r")) == NULL) {
403             if (!option (OPTNOCURSES))
404               mutt_endwin (NULL);
405             perror (path);
406             exit (1);
407           }
408         }
409       }
410       else
411         fin = NULL;
412
413       if (draftFile)
414         msg->env = mutt_read_rfc822_header (fin, NULL, 1, 0);
415
416       /* is the following if still needed? */
417
418       fout = m_tempfile(buf, sizeof(buf), NONULL(mod_core.tmpdir), NULL);
419       tempfile = m_strdup(buf);
420
421       if (tempfile) {
422         if (!fout) {
423           if (!option (OPTNOCURSES))
424             mutt_endwin (NULL);
425           perror (tempfile);
426           m_fclose(&fin);
427           p_delete(&tempfile);
428           exit (1);
429         }
430         if (fin)
431           mutt_copy_stream (fin, fout);
432         else if (bodytext)
433           fputs (bodytext, fout);
434         m_fclose(&fout);
435         if (fin && fin != stdin)
436           m_fclose(&fin);
437       }
438     }
439
440     p_delete(&bodytext);
441
442     if (attach) {
443       string_list_t *t = attach;
444       BODY *a = NULL;
445
446       while (t) {
447         if (a) {
448           a->next = mutt_make_file_attach (t->data);
449           a = a->next;
450         }
451         else
452           msg->content = a = mutt_make_file_attach (t->data);
453         if (!a) {
454           if (!option (OPTNOCURSES))
455             mutt_endwin (NULL);
456           fprintf (stderr, _("%s: unable to attach file.\n"), t->data);
457           string_list_wipe(&attach);
458           exit (1);
459         }
460         t = t->next;
461       }
462       string_list_wipe(&attach);
463     }
464
465     ci_send_message (sendflags, msg, tempfile, NULL, NULL);
466
467     if (!option (OPTNOCURSES))
468       mutt_endwin (NULL);
469   }
470   else {
471     if (flags & M_BUFFY) {
472       if (!buffy_check (0)) {
473         mutt_endwin _("No mailbox with new mail.");
474
475         exit (1);
476       }
477       folder[0] = 0;
478       buffy_next (folder, sizeof (folder));
479     }
480
481     if (!folder[0])
482       m_strcpy(folder, sizeof(folder), NONULL(Spoolfile));
483
484     mutt_expand_path (folder, sizeof (folder));
485     m_strreplace(&CurrentFolder, folder);
486     m_strreplace(&LastFolder, folder);
487
488     if (flags & M_IGNORE) {
489       /* check to see if there are any messages in the folder */
490       switch (mx_check_empty (folder)) {
491       case -1:
492         mutt_endwin (strerror (errno));
493         exit (1);
494       case 1:
495         mutt_endwin _("Mailbox is empty.");
496         exit (1);
497       }
498     }
499
500     mutt_folder_hook (folder);
501
502     if ((Context = mx_open_mailbox(folder, (flags & M_RO) ?  M_READONLY : 0,
503                                    NULL)) || !explicit_folder)
504     {
505       mutt_index_menu ();
506
507       if (option (OPTXTERMSETTITLES))
508         printf("\033]2;%s\007", NONULL(XtermLeave));
509       if (Context)
510         p_delete(&Context);
511     }
512     mutt_endwin (Errorbuf);
513   }
514
515   pthread_cancel(pt);
516   pthread_join(pt, NULL);
517   luaM_shutdown();
518   mutt_sasl_shutdown();
519   el_shutdown();
520   exit (0);
521 }