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