8b3bcb7108b57d089b9ab6d13d094a32fb0c55dc
[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 #if HAVE_CONFIG_H
19 # include "config.h"
20 #endif
21
22 #include <lib-lib/mem.h>
23 #include <lib-lib/str.h>
24 #include <lib-lib/macros.h>
25 #include <lib-lib/file.h>
26
27 #include "mutt.h"
28 #include "mx.h"
29 #include "buffy.h"
30 #include "sort.h"
31 #include "mutt_crypt.h"
32 #include "mutt_curses.h"
33 #include "keymap.h"
34 #include "url.h"
35 #include "mutt_idna.h"
36 #include "xterm.h"
37
38 #include "lib/debug.h"
39
40 #include <string.h>
41 #include <stdlib.h>
42 #include <locale.h>
43 #include <unistd.h>
44 #include <errno.h>
45 #include <sys/stat.h>
46 #include <sys/utsname.h>
47
48 #ifdef HAVE_GETOPT_H
49 #include <getopt.h>
50 #else
51 extern char *optarg;
52 extern int optind;
53 #endif
54
55 #ifdef HAVE_LIBIDN
56 #include <stringprep.h>
57 #endif
58
59 #ifdef USE_NNTP
60 #include "nntp/nntp.h"
61 #endif
62
63 #ifdef USE_LIBESMTP
64 #include <libesmtp.h>
65 #endif
66
67 #ifdef USE_HCACHE
68 #if HAVE_QDBM
69 #include <depot.h>
70 #elif HAVE_GDBM
71 #include <gdbm.h>
72 #elif HAVE_DB4
73 #include <db.h>
74 #endif
75 #endif
76
77 #ifdef USE_GNUTLS
78 #include <gnutls/gnutls.h>
79 #endif
80
81 #ifdef CRYPT_BACKEND_GPGME
82 #include <gpgme.h>
83 #endif
84
85 #ifdef USE_SSL
86 #include <openssl/opensslv.h>
87 #endif
88
89 static const char *ReachingUs = N_("\
90 To contact the developers, please mail to <mutt-ng-devel@lists.berlios.de>.\n\
91 To visit the Mutt-ng homepage go to http://www.muttng.org.\n\
92 To report a bug, please use the fleang(1) utility.\n");
93
94 static const char *Notice = N_("\
95 Copyright (C) 1996-2002 Michael R. Elkins and others.\n\
96 Mutt-ng comes with ABSOLUTELY NO WARRANTY; for details type `muttng -vv'.\n\
97 Mutt is free software, and you are welcome to redistribute it\n\
98 under certain conditions; type `muttng -vv' for details.\n");
99
100 static const char *Copyright_Mutt = N_("\
101 Copyright (C) 1996-2004 Michael R. Elkins <me@mutt.org>\n\
102 Copyright (C) 1996-2002 Brandon Long <blong@fiction.net>\n\
103 Copyright (C) 1997-2005 Thomas Roessler <roessler@does-not-exist.org>\n\
104 Copyright (C) 1998-2005 Werner Koch <wk@isil.d.shuttle.de>\n\
105 Copyright (C) 1999-2005 Brendan Cully <brendan@kublai.com>\n\
106 Copyright (C) 1999-2002 Tommi Komulainen <Tommi.Komulainen@iki.fi>\n\
107 Copyright (C) 2000-2002 Edmund Grimley Evans <edmundo@rano.org>\n");
108
109 static const char *Copyright_Mutt_NG = N_("\
110 Copyright (C) 2005:\n\
111   Parts were written/modified by:\n\
112      Andreas Krennmair <ak@synflood.at>\n\
113      Nico Golde <nico@ngolde.de>\n\
114      Rocco Rutte <pdmef@cs.tu-berlin.de>\n\
115 \n\
116 Lots of others not mentioned here contributed lots of code,\n\
117 fixes, and suggestions.");
118
119 static const char* Copyright_GPL_1 = N_("\
120 \n\
121     This program is free software; you can redistribute it and/or modify\n\
122     it under the terms of the GNU General Public License as published by\n\
123     the Free Software Foundation; either version 2 of the License, or\n\
124     (at your option) any later version.\n");
125
126 static const char* Copyright_GPL_2 = N_("\
127     This program is distributed in the hope that it will be useful,\n\
128     but WITHOUT ANY WARRANTY; without even the implied warranty of\n\
129     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n\
130     GNU General Public License for more details.\n");
131
132 static const char* Copyright_GPL_3 = N_("\
133     You should have received a copy of the GNU General Public License\n\
134     along with this program; if not, write to the Free Software\n\
135     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.\n\
136 ");
137
138 void mutt_exit (int code)
139 {
140   mutt_endwin (NULL);
141   exit (code);
142 }
143
144 static void mutt_usage (void)
145 {
146   puts (mutt_make_version (1));
147
148   puts
149     _("usage: muttng [ -nRyzZ ] [ -e <cmd> ] [ -F <file> ] [ -m <type> ] [ -f <file> ]\n\
150        muttng [ -nR ] [ -e <cmd> ] [ -F <file> ] -Q <query> [ -Q <query> ] [...]\n\
151        muttng [ -nR ] [ -e <cmd> ] [ -F <file> ] -A <alias> [ -A <alias> ] [...]\n\
152        muttng [ -nR ] [ -e <cmd> ] [ -F <file> ] -t");
153   puts
154     _("\
155        muttng [ -nR ] [ -e <cmd> ] [ -F <file> ] -T\n\
156        muttng [ -nx ] [ -e <cmd> ] [ -a <file> ] [ -F <file> ] [ -H <file> ] [ -i <file> ] [ -s <subj> ] [ -b <addr> ] [ -c <addr> ] <addr> [ ... ]\n\
157        muttng [ -n ] [ -e <cmd> ] [ -F <file> ] -p\n\
158        muttng -v[v]\n\
159 \n\
160 options:\n\
161   -A <alias>\texpand the given alias\n\
162   -a <file>\tattach a file to the message\n\
163   -b <address>\tspecify a blind carbon-copy (BCC) address\n\
164   -c <address>\tspecify a carbon-copy (CC) address");
165 #if DEBUG
166   puts _("  -d <level>\tlog debugging output to ~/.muttngdebug0");
167 #endif
168   puts _("\
169   -e <command>\tspecify a command to be executed after initialization\n\
170   -f <file>\tspecify which mailbox to read\n\
171   -F <file>\tspecify an alternate muttrngc file\n\
172   -g <server>\tspecify a newsserver (if compiled with NNTP)\n\
173   -G\t\tselect a newsgroup (if compiled with NNTP)\n\
174   -H <file>\tspecify a draft file to read header and body from\n\
175   -i <file>\tspecify a file which Mutt-ng should include in the body");
176   puts _("\
177   -d <level>\t specify debugging level of Mutt-ng\n\
178   -m <type>\tspecify a default mailbox type\n\
179   -n\t\tcauses Mutt-ng not to read the system Muttngrc\n\
180   -p\t\trecall a postponed message\n\
181   -Q <variable>\tquery a configuration variable\n\
182   -R\t\topen mailbox in read-only mode\n\
183   -s <subj>\tspecify a subject (must be in quotes if it has spaces)");
184   puts _("\
185   -t\t\tprint the value of all variables to stdout\n\
186   -T\t\tprint the value of all changed variables to stdout\n\
187   -v\t\tshow version and compile-time definitions\n\
188   -x\t\tsimulate the mailx send mode\n\
189   -y\t\tselect a mailbox specified in your `mailboxes' list\n\
190   -z\t\texit immediately if there are no messages in the mailbox\n\
191   -Z\t\topen the first folder with new message, exit immediately if none\n\
192   -h\t\tthis help message");
193
194   exit (0);
195 }
196
197 static void show_version (void)
198 {
199   struct utsname uts;
200 #ifdef USE_LIBESMTP
201   char buf[STRING];
202 #endif
203
204   puts (mutt_make_version (1));
205   puts (_(Notice));
206
207   uname (&uts);
208
209 #ifdef _AIX
210   printf ("System:\n  %s %s.%s", uts.sysname, uts.version, uts.release);
211 #elif defined (SCO)
212   printf ("System:\n  SCO %s", uts.release);
213 #else
214   printf ("System:\n  %s %s", uts.sysname, uts.release);
215 #endif
216
217   printf (" (%s)\nExternal Libraries:\n", uts.machine);
218
219 #ifdef NCURSES_VERSION
220   printf ("  ncurses %s\n", NCURSES_VERSION);
221 #elif defined(USE_SLANG_CURSES)
222   printf ("  slang %d\n", SLANG_VERSION);
223 #endif
224
225 #ifdef _LIBICONV_VERSION
226   printf ("  libiconv %d.%d\n", _LIBICONV_VERSION >> 8,
227           _LIBICONV_VERSION & 0xff);
228 #endif
229
230 #ifdef HAVE_LIBIDN
231   printf ("  libidn %s (compiled with %s)\n",
232           stringprep_check_version (NULL), STRINGPREP_VERSION);
233 #endif
234
235 #ifdef USE_LIBESMTP
236   smtp_version (buf, sizeof (buf), 0);
237   printf ("  libESMTP %s\n", buf);
238 #endif
239
240 #ifdef USE_HCACHE
241 #if HAVE_QDBM
242   printf ("  qdbm %s\n", dpversion);
243 #elif HAVE_GDBM
244   printf ("  gdbm %s\n", gdbm_version);
245 #elif HAVE_DB4
246   printf ("  DB4 %d.%d.%d\n", DB_VERSION_MAJOR, DB_VERSION_MINOR,
247           DB_VERSION_PATCH);
248 #endif
249 #endif
250
251 #ifdef USE_GNUTLS
252   printf ("  gnutls %s\n", LIBGNUTLS_VERSION);
253 #endif
254
255 #ifdef USE_SSL
256   printf ("  %s\n",OPENSSL_VERSION_TEXT);
257 #endif
258
259 #ifdef CRYPT_BACKEND_GPGME
260   printf ("  gpgme %s\n", GPGME_VERSION);
261 #endif
262
263   puts (_("Compile Options:"));
264
265 #ifdef DEBUG
266   puts ("  +DEBUG");
267 #else
268   puts ("  -DEBUG");
269 #endif
270
271   puts (
272 #ifdef HOMESPOOL
273          "  +HOMESPOOL  "
274 #else
275          "  -HOMESPOOL  "
276 #endif
277 #ifdef USE_SETGID
278          "+USE_SETGID  "
279 #else
280          "-USE_SETGID  "
281 #endif
282 #ifdef USE_DOTLOCK
283          "+USE_DOTLOCK  "
284 #else
285          "-USE_DOTLOCK  "
286 #endif
287 #ifdef USE_FCNTL
288          "+USE_FCNTL  "
289 #else
290          "-USE_FCNTL  "
291 #endif
292 #ifdef USE_FLOCK
293          "+USE_FLOCK   "
294 #else
295          "-USE_FLOCK   "
296 #endif
297 #ifdef USE_HCACHE
298          "+USE_HCACHE  "
299 #else
300          "-USE_HCACHE  "
301 #endif
302     );
303   puts (
304 #ifdef USE_POP
305          "  +USE_POP  "
306 #else
307          "  -USE_POP  "
308 #endif
309 #ifdef USE_NNTP
310          "+USE_NNTP  "
311 #else
312          "-USE_NNTP  "
313 #endif
314 #ifdef USE_IMAP
315          "+USE_IMAP  "
316 #else
317          "-USE_IMAP  "
318 #endif
319 #ifdef USE_GSS
320          "+USE_GSS  "
321 #else
322          "-USE_GSS  "
323 #endif
324 #ifdef USE_SSL
325          "+USE_SSL  "
326 #else
327          "-USE_SSL  "
328 #endif
329 #ifdef USE_GNUTLS
330          "+USE_GNUTLS  "
331 #else
332          "-USE_GNUTLS  "
333 #endif
334 #ifdef USE_SASL
335          "+USE_SASL  "
336 #else
337          "-USE_SASL  "
338 #endif
339 #ifdef USE_LIBESMTP
340          "+USE_LIBESMTP  "
341 #else
342          "-USE_LIBESMTP  "
343 #endif
344          "\n  "
345 #ifdef HAVE_REGCOMP
346          "+HAVE_REGCOMP  "
347 #else
348          "-HAVE_REGCOMP  "
349 #endif
350 #ifdef USE_GNU_REGEX
351          "+USE_GNU_REGEX  "
352 #else
353          "-USE_GNU_REGEX  "
354 #endif
355          "\n  "
356 #ifdef HAVE_COLOR
357          "+HAVE_COLOR  "
358 #else
359          "-HAVE_COLOR  "
360 #endif
361 #ifdef HAVE_START_COLOR
362          "+HAVE_START_COLOR  "
363 #else
364          "-HAVE_START_COLOR  "
365 #endif
366 #ifdef HAVE_TYPEAHEAD
367          "+HAVE_TYPEAHEAD  "
368 #else
369          "-HAVE_TYPEAHEAD  "
370 #endif
371 #ifdef HAVE_BKGDSET
372          "+HAVE_BKGDSET  "
373 #else
374          "-HAVE_BKGDSET  "
375 #endif
376          "\n  "
377 #ifdef HAVE_CURS_SET
378          "+HAVE_CURS_SET  "
379 #else
380          "-HAVE_CURS_SET  "
381 #endif
382 #ifdef HAVE_META
383          "+HAVE_META  "
384 #else
385          "-HAVE_META  "
386 #endif
387 #ifdef HAVE_RESIZETERM
388          "+HAVE_RESIZETERM  "
389 #else
390          "-HAVE_RESIZETERM  "
391 #endif
392     );
393
394   puts (
395 #ifdef CRYPT_BACKEND_GPGME
396          "+CRYPT_BACKEND_GPGME  "
397 #else
398          "-CRYPT_BACKEND_GPGME  "
399 #endif
400 #ifdef BUFFY_SIZE
401          "+BUFFY_SIZE "
402 #else
403          "-BUFFY_SIZE "
404 #endif
405          "\n  "
406 #ifdef ENABLE_NLS
407          "+ENABLE_NLS  "
408 #else
409          "-ENABLE_NLS  "
410 #endif
411 #ifdef LOCALES_HACK
412          "+LOCALES_HACK  "
413 #else
414          "-LOCALES_HACK  "
415 #endif
416 #ifdef HAVE_WC_FUNCS
417          "+HAVE_WC_FUNCS  "
418 #else
419          "-HAVE_WC_FUNCS  "
420 #endif
421 #ifdef HAVE_LANGINFO_CODESET
422          "+HAVE_LANGINFO_CODESET  "
423 #else
424          "-HAVE_LANGINFO_CODESET  "
425 #endif
426 #ifdef HAVE_LANGINFO_YESEXPR
427          "+HAVE_LANGINFO_YESEXPR  "
428 #else
429          "-HAVE_LANGINFO_YESEXPR  "
430 #endif
431          "\n  "
432 #if HAVE_ICONV
433          "+HAVE_ICONV  "
434 #else
435          "-HAVE_ICONV  "
436 #endif
437 #if ICONV_NONTRANS
438          "+ICONV_NONTRANS  "
439 #else
440          "-ICONV_NONTRANS  "
441 #endif
442 #if HAVE_LIBIDN
443          "+HAVE_LIBIDN  "
444 #else
445          "-HAVE_LIBIDN  "
446 #endif
447 #if HAVE_GETSID
448          "+HAVE_GETSID  "
449 #else
450          "-HAVE_GETSID  "
451 #endif
452 #if HAVE_GETADDRINFO
453          "+HAVE_GETADDRINFO  "
454 #else
455          "-HAVE_GETADDRINFO  "
456 #endif
457     );
458
459   puts (_("Built-In Defaults:"));
460
461 #ifdef ISPELL
462   printf ("  +ISPELL=\"%s\"\n", ISPELL);
463 #else
464   puts ("  -ISPELL");
465 #endif
466
467   printf ("  +SENDMAIL=\"%s\"\n", SENDMAIL);
468   printf ("  +MAILPATH=\"%s\"\n", MAILPATH);
469   printf ("  +PKGDATADIR=\"%s\"\n", PKGDATADIR);
470   printf ("  +PKGDOCDIR=\"%s\"\n", PKGDOCDIR);
471   printf ("  +SYSCONFDIR=\"%s\"\n", SYSCONFDIR);
472   printf ("  +EXECSHELL=\"%s\"\n", EXECSHELL);
473 #ifdef MIXMASTER
474   printf ("  +MIXMASTER=\"%s\"\n\n", MIXMASTER);
475 #else
476   puts ("  -MIXMASTER\n");
477 #endif
478
479   puts (_(ReachingUs));
480
481   puts (_("MadMutt is based on Mutt-ng wich was based on Mutt before\n"));
482
483   exit (0);
484 }
485
486 static void start_curses (void)
487 {
488   km_init ();                   /* must come before mutt_init */
489
490 #ifdef USE_SLANG_CURSES
491   SLtt_Ignore_Beep = 1;         /* don't do that #*$@^! annoying visual beep! */
492   SLsmg_Display_Eight_Bit = 128;        /* characters above this are printable */
493   SLtt_set_color (0, NULL, "default", "default");
494 #else
495   /* should come before initscr() so that ncurses 4.2 doesn't try to install
496      its own SIGWINCH handler */
497   mutt_signal_init ();
498 #endif
499   if (initscr () == NULL) {
500     puts _("Error initializing terminal.");
501
502     exit (1);
503   }
504 #if 1                           /* USE_SLANG_CURSES  - commenting out suggested in #455. */
505   /* slang requires the signal handlers to be set after initializing */
506   mutt_signal_init ();
507 #endif
508   ci_start_color ();
509   keypad (stdscr, TRUE);
510   cbreak ();
511   noecho ();
512 #if HAVE_TYPEAHEAD
513   typeahead (-1);               /* simulate smooth scrolling */
514 #endif
515 #if HAVE_META
516   meta (stdscr, TRUE);
517 #endif
518 }
519
520 #define M_IGNORE  (1<<0)        /* -z */
521 #define M_BUFFY   (1<<1)        /* -Z */
522 #define M_NOSYSRC (1<<2)        /* -n */
523 #define M_RO      (1<<3)        /* -R */
524 #define M_SELECT  (1<<4)        /* -y */
525 #ifdef USE_NNTP
526 #define M_NEWS    (1<<5)        /* -g and -G */
527 #endif
528
529 int main (int argc, char **argv)
530 {
531   char folder[_POSIX_PATH_MAX] = "";
532   char *subject = NULL;
533   char *includeFile = NULL;
534   char *draftFile = NULL;
535   char *newMagic = NULL;
536   HEADER *msg = NULL;
537   LIST *attach = NULL;
538   LIST *commands = NULL;
539   LIST *queries = NULL;
540   LIST *alias_queries = NULL;
541   int sendflags = 0;
542   int flags = 0;
543   int version = 0;
544   int i;
545   int explicit_folder = 0;
546   int dump_variables = -1;
547
548   /* initialize random number for tmp file creation */ 
549   srand ((unsigned int) time (NULL));
550   
551   /* sanity check against stupid administrators */
552   
553   if (getegid () != getgid ()) {
554     fprintf (stderr, "%s: I don't want to run with privileges!\n", argv[0]);
555     exit (1);
556   }
557
558 #ifdef ENABLE_NLS
559   /* FIXME what about init.c:1439 ? */
560   setlocale (LC_ALL, "");
561   bindtextdomain (PACKAGE, MUTTLOCALEDIR);
562   textdomain (PACKAGE);
563 #endif
564
565   setlocale (LC_CTYPE, "");
566
567   mutt_error = mutt_nocurses_error;
568   mutt_message = mutt_nocurses_error;
569   SRAND (time (NULL));
570   umask (077);
571
572   p_clear(Options, sizeof(Options));
573   p_clear(QuadOptions, sizeof(QuadOptions));
574
575 #ifdef USE_NNTP
576   while ((i =
577           getopt (argc, argv,
578                   "A:a:b:F:f:c:d:e:g:GH:s:i:hm:npQ:RTtvxyzZ")) != EOF)
579 #else
580   while ((i =
581           getopt (argc, argv, "A:a:b:F:f:c:d:e:H:s:i:hm:npQ:RTtvxyzZ")) != EOF)
582 #endif
583     switch (i) {
584     case 'A':
585       alias_queries = mutt_add_list (alias_queries, optarg);
586       break;
587     case 'a':
588       if (strlen(optarg)<=512)
589         attach = mutt_add_list (attach, optarg);
590       else{
591         printf("too long arguments. exiting ...\n");
592         exit(1);
593       }
594       break;
595
596     case 'F':
597       m_strreplace(&Muttrc, optarg);
598       break;
599
600     case 'f':
601       m_strcpy(folder, sizeof(folder), optarg);
602       explicit_folder = 1;
603       break;
604
605     case 'b':
606     case 'c':
607       if (!msg)
608         msg = mutt_new_header ();
609       if (!msg->env)
610         msg->env = mutt_new_envelope ();
611       if (i == 'b')
612         msg->env->bcc = rfc822_parse_adrlist (msg->env->bcc, optarg);
613       else
614         msg->env->cc = rfc822_parse_adrlist (msg->env->cc, optarg);
615       break;
616
617     case 'd':
618       {
619         char buf[LONG_STRING];
620         int level = atoi (optarg);
621
622         snprintf (buf, sizeof (buf), "set debug_level=%d", level);
623         commands = mutt_add_list (commands, buf);
624         debug_setlevel (level);
625       }
626       break;
627
628     case 't':
629       dump_variables = 2;
630       break;
631
632     case 'T':
633       dump_variables = 1;
634       break;
635
636     case 'e':
637       commands = mutt_add_list (commands, optarg);
638       break;
639
640     case 'H':
641       draftFile = optarg;
642       break;
643
644     case 'i':
645       includeFile = optarg;
646       break;
647
648     case 'm':
649       /* should take precedence over .muttrc setting, so save it for later */
650       newMagic = optarg;
651       break;
652
653     case 'n':
654       flags |= M_NOSYSRC;
655       break;
656
657     case 'p':
658       sendflags |= SENDPOSTPONED;
659       break;
660
661     case 'Q':
662       queries = mutt_add_list (queries, optarg);
663       break;
664
665     case 'R':
666       flags |= M_RO;            /* read-only mode */
667       break;
668
669     case 's':
670       subject = optarg;
671       break;
672
673     case 'v':
674       version++;
675       break;
676
677     case 'x':                  /* mailx compatible send mode */
678       sendflags |= SENDMAILX;
679       break;
680
681     case 'y':                  /* My special hack mode */
682       flags |= M_SELECT;
683       break;
684
685 #ifdef USE_NNTP
686     case 'g':                  /* Specify a newsserver */
687       {
688         char buf[LONG_STRING];
689
690         snprintf (buf, sizeof (buf), "set nntp_host=%s", optarg);
691         commands = mutt_add_list (commands, buf);
692       }
693
694     case 'G':                  /* List of newsgroups */
695       flags |= M_SELECT | M_NEWS;
696       break;
697 #endif
698
699     case 'z':
700       flags |= M_IGNORE;
701       break;
702
703     case 'Z':
704       flags |= M_BUFFY | M_IGNORE;
705       break;
706
707     default:
708       mutt_usage ();
709     }
710
711   switch (version) {
712   case 0:
713     break;
714   case 1:
715     show_version ();
716     break;
717   default:
718     puts (mutt_make_version (1));
719     puts (_(Copyright_Mutt));
720     puts (_(Copyright_Mutt_NG));
721     puts (_(Copyright_GPL_1));
722     puts (_(Copyright_GPL_2));
723     puts (_(Copyright_GPL_3));
724     puts (_(ReachingUs));
725     exit (0);
726   }
727
728   /* Check for a batch send. */
729   if (!isatty (0) || queries || alias_queries || dump_variables > 0) {
730     set_option (OPTNOCURSES);
731     sendflags = SENDBATCH;
732   }
733
734   /* This must come before mutt_init() because curses needs to be started
735      before calling the init_pair() function to set the color scheme.  */
736   if (!option (OPTNOCURSES))
737     start_curses ();
738
739   /* set defaults and read init files */
740   mx_init ();
741   mutt_init (flags & M_NOSYSRC, commands);
742   mutt_free_list (&commands);
743
744   /* Initialize crypto */
745   crypt_init ();
746
747   if (queries)
748     return mutt_query_variables (queries);
749   if (dump_variables > 0)
750     return (mutt_dump_variables (dump_variables-1));
751
752   if (alias_queries) {
753     int rv = 0;
754     address_t *a;
755
756     for (; alias_queries; alias_queries = alias_queries->next) {
757       if ((a = mutt_lookup_alias (alias_queries->data))) {
758         /* output in machine-readable form */
759         mutt_addrlist_to_idna (a, NULL);
760         mutt_write_address_list (a, stdout, 0, 0);
761       }
762       else {
763         rv = 1;
764         printf ("%s\n", alias_queries->data);
765       }
766     }
767     return rv;
768   }
769
770   if (newMagic)
771     mx_set_magic (newMagic);
772
773   if (!option (OPTNOCURSES)) {
774     SETCOLOR (MT_COLOR_NORMAL);
775     clear ();
776     mutt_error = mutt_curses_error;
777     mutt_message = mutt_curses_message;
778   }
779
780   /* Create the Maildir directory if it doesn't exist. */
781   if (!option (OPTNOCURSES) && Maildir) {
782     struct stat sb;
783     char fpath[_POSIX_PATH_MAX];
784     char msg[STRING];
785
786     m_strcpy(fpath, sizeof(fpath), Maildir);
787     mutt_expand_path (fpath, sizeof (fpath));
788 #ifdef USE_IMAP
789     /* we're not connected yet - skip mail folder creation */
790     if (mx_get_magic (fpath) != M_IMAP)
791 #endif
792       if (stat (fpath, &sb) == -1 && errno == ENOENT) {
793         snprintf (msg, sizeof (msg), _("%s does not exist. Create it?"),
794                   Maildir);
795         if (mutt_yesorno (msg, M_YES) == M_YES) {
796           if (mkdir (fpath, 0700) == -1 && errno != EEXIST)
797             mutt_error (_("Can't create %s: %s."), Maildir, strerror (errno));
798         }
799       }
800   }
801
802   if (sendflags & SENDPOSTPONED) {
803     if (!option (OPTNOCURSES))
804       mutt_flushinp ();
805     ci_send_message (SENDPOSTPONED, NULL, NULL, NULL, NULL);
806     mutt_endwin (NULL);
807   }
808   else if (subject || msg || sendflags || draftFile || includeFile || attach
809            || optind < argc) {
810     FILE *fin = NULL;
811     char buf[LONG_STRING];
812     char *tempfile = NULL, *infile = NULL;
813     char *bodytext = NULL;
814
815     if (!option (OPTNOCURSES))
816       mutt_flushinp ();
817
818     if (!msg)
819       msg = mutt_new_header ();
820
821     if (draftFile)
822       infile = draftFile;
823     else {
824       if (!msg->env)
825         msg->env = mutt_new_envelope ();
826
827       for (i = optind; i < argc; i++) {
828         if (url_check_scheme (argv[i]) == U_MAILTO)
829           url_parse_mailto (msg->env, &bodytext, argv[i]);
830         else
831           msg->env->to = rfc822_parse_adrlist (msg->env->to, argv[i]);
832       }
833
834       if (option (OPTAUTOEDIT) && !msg->env->to && !msg->env->cc) {
835         if (!option (OPTNOCURSES))
836           mutt_endwin (NULL);
837         fputs (_("No recipients specified.\n"), stderr);
838         exit (1);
839       }
840
841       if (subject)
842         msg->env->subject = m_strdup(subject);
843
844       if (includeFile)
845         infile = includeFile;
846     }
847
848     if (infile || bodytext) {
849       if (infile) {
850         if (m_strcmp("-", infile) == 0)
851           fin = stdin;
852         else {
853           char path[_POSIX_PATH_MAX];
854
855           m_strcpy(path, sizeof(path), infile);
856           mutt_expand_path (path, sizeof (path));
857           if ((fin = fopen (path, "r")) == NULL) {
858             if (!option (OPTNOCURSES))
859               mutt_endwin (NULL);
860             perror (path);
861             exit (1);
862           }
863         }
864       }
865       else
866         fin = NULL;
867
868       mutt_mktemp (buf);
869       tempfile = m_strdup(buf);
870
871       if (draftFile)
872         msg->env = mutt_read_rfc822_header (fin, NULL, 1, 0);
873
874       /* is the following if still needed? */
875
876       if (tempfile) {
877         FILE *fout;
878
879         if ((fout = safe_fopen (tempfile, "w")) == NULL) {
880           if (!option (OPTNOCURSES))
881             mutt_endwin (NULL);
882           perror (tempfile);
883           fclose (fin);
884           p_delete(&tempfile);
885           exit (1);
886         }
887         if (fin)
888           mutt_copy_stream (fin, fout);
889         else if (bodytext)
890           fputs (bodytext, fout);
891         fclose (fout);
892         if (fin && fin != stdin)
893           fclose (fin);
894       }
895     }
896
897     p_delete(&bodytext);
898
899     if (attach) {
900       LIST *t = attach;
901       BODY *a = NULL;
902
903       while (t) {
904         if (a) {
905           a->next = mutt_make_file_attach (t->data);
906           a = a->next;
907         }
908         else
909           msg->content = a = mutt_make_file_attach (t->data);
910         if (!a) {
911           if (!option (OPTNOCURSES))
912             mutt_endwin (NULL);
913           fprintf (stderr, _("%s: unable to attach file.\n"), t->data);
914           mutt_free_list (&attach);
915           exit (1);
916         }
917         t = t->next;
918       }
919       mutt_free_list (&attach);
920     }
921
922     ci_send_message (sendflags, msg, tempfile, NULL, NULL);
923
924     if (!option (OPTNOCURSES))
925       mutt_endwin (NULL);
926   }
927   else {
928     if (flags & M_BUFFY) {
929       if (!buffy_check (0)) {
930         mutt_endwin _("No mailbox with new mail.");
931
932         exit (1);
933       }
934       folder[0] = 0;
935       buffy_next (folder, sizeof (folder));
936     }
937     else if (flags & M_SELECT) {
938 #ifdef USE_NNTP
939       if (flags & M_NEWS) {
940         set_option (OPTNEWS);
941         if (!(CurrentNewsSrv = mutt_select_newsserver (NewsServer))) {
942           mutt_endwin (Errorbuf);
943           exit (1);
944         }
945       }
946       else
947 #endif
948       if (!Incoming) {
949         mutt_endwin _("No incoming mailboxes defined.");
950
951         exit (1);
952       }
953       folder[0] = 0;
954       mutt_select_file (folder, sizeof (folder), M_SEL_FOLDER | M_SEL_BUFFY);
955       if (!folder[0]) {
956         mutt_endwin (NULL);
957         exit (0);
958       }
959     }
960
961     if (!folder[0])
962       m_strcpy(folder, sizeof(folder), NONULL(Spoolfile));
963
964 #ifdef USE_NNTP
965     if (option (OPTNEWS)) {
966       unset_option (OPTNEWS);
967       nntp_expand_path (folder, sizeof (folder),
968                         &CurrentNewsSrv->conn->account);
969     }
970     else
971 #endif
972       mutt_expand_path (folder, sizeof (folder));
973
974     m_strreplace(&CurrentFolder, folder);
975     m_strreplace(&LastFolder, folder);
976
977     if (flags & M_IGNORE) {
978       /* check to see if there are any messages in the folder */
979       switch (mx_check_empty (folder)) {
980       case -1:
981         mutt_endwin (strerror (errno));
982         exit (1);
983       case 1:
984         mutt_endwin _("Mailbox is empty.");
985         exit (1);
986       }
987     }
988
989     mutt_folder_hook (folder);
990
991     if ((Context = mx_open_mailbox (folder, ((flags & M_RO)
992                                              || option (OPTREADONLY)) ?
993                                     M_READONLY : 0, NULL))
994         || !explicit_folder) {
995       mutt_index_menu ();
996       if (option (OPTXTERMSETTITLES))
997         mutt_xterm_set_title (NONULL (XtermLeave));
998       if (Context)
999         p_delete(&Context);
1000     }
1001     mutt_endwin (Errorbuf);
1002   }
1003
1004   exit (0);
1005 }