Rocco Rutte:
[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 ("  +PKGDOCDIR=\"%s\"\n", PKGDOCDIR);
488   printf ("  +SYSCONFDIR=\"%s\"\n", SYSCONFDIR);
489   printf ("  +EXECSHELL=\"%s\"\n", EXECSHELL);
490 #ifdef MIXMASTER
491   printf ("  +MIXMASTER=\"%s\"\n\n", MIXMASTER);
492 #else
493   puts ("  -MIXMASTER\n");
494 #endif
495
496   puts (_(ReachingUs));
497
498   puts (_("Mutt-ng is based on the following patches written for mutt:\n"));
499   mutt_print_patchlist ();
500
501   exit (0);
502 }
503
504 static void start_curses (void)
505 {
506   km_init ();                   /* must come before mutt_init */
507
508 #ifdef USE_SLANG_CURSES
509   SLtt_Ignore_Beep = 1;         /* don't do that #*$@^! annoying visual beep! */
510   SLsmg_Display_Eight_Bit = 128;        /* characters above this are printable */
511   SLtt_set_color (0, NULL, "default", "default");
512 #else
513   /* should come before initscr() so that ncurses 4.2 doesn't try to install
514      its own SIGWINCH handler */
515   mutt_signal_init ();
516 #endif
517   if (initscr () == NULL) {
518     puts _("Error initializing terminal.");
519
520     exit (1);
521   }
522 #if 1                           /* USE_SLANG_CURSES  - commenting out suggested in #455. */
523   /* slang requires the signal handlers to be set after initializing */
524   mutt_signal_init ();
525 #endif
526   ci_start_color ();
527   keypad (stdscr, TRUE);
528   cbreak ();
529   noecho ();
530 #if HAVE_TYPEAHEAD
531   typeahead (-1);               /* simulate smooth scrolling */
532 #endif
533 #if HAVE_META
534   meta (stdscr, TRUE);
535 #endif
536 }
537
538 #define M_IGNORE  (1<<0)        /* -z */
539 #define M_BUFFY   (1<<1)        /* -Z */
540 #define M_NOSYSRC (1<<2)        /* -n */
541 #define M_RO      (1<<3)        /* -R */
542 #define M_SELECT  (1<<4)        /* -y */
543 #ifdef USE_NNTP
544 #define M_NEWS    (1<<5)        /* -g and -G */
545 #endif
546
547 int main (int argc, char **argv)
548 {
549   char folder[_POSIX_PATH_MAX] = "";
550   char *subject = NULL;
551   char *includeFile = NULL;
552   char *draftFile = NULL;
553   char *newMagic = NULL;
554   HEADER *msg = NULL;
555   LIST *attach = NULL;
556   LIST *commands = NULL;
557   LIST *queries = NULL;
558   LIST *alias_queries = NULL;
559   int sendflags = 0;
560   int flags = 0;
561   int version = 0;
562   int i;
563   int explicit_folder = 0;
564   int dump_variables = -1;
565   extern char *optarg;
566   extern int optind;
567
568   /* initialize random number for tmp file creation */ 
569   srand ((unsigned int) time (NULL));
570   
571   /* sanity check against stupid administrators */
572   
573   if (getegid () != getgid ()) {
574     fprintf (stderr, "%s: I don't want to run with privileges!\n", argv[0]);
575     exit (1);
576   }
577
578 #ifdef ENABLE_NLS
579   /* FIXME what about init.c:1439 ? */
580   setlocale (LC_ALL, "");
581   bindtextdomain (PACKAGE, MUTTLOCALEDIR);
582   textdomain (PACKAGE);
583 #endif
584
585   setlocale (LC_CTYPE, "");
586
587   mutt_error = mutt_nocurses_error;
588   mutt_message = mutt_nocurses_error;
589   SRAND (time (NULL));
590   umask (077);
591
592   memset (Options, 0, sizeof (Options));
593   memset (QuadOptions, 0, sizeof (QuadOptions));
594
595 #ifdef USE_NNTP
596   while ((i =
597           getopt (argc, argv,
598                   "A:a:b:F:f:c:d:e:g:GH:s:i:hm:npQ:RTtvxyzZ")) != EOF)
599 #else
600   while ((i =
601           getopt (argc, argv, "A:a:b:F:f:c:d:e:H:s:i:hm:npQ:RTtvxyzZ")) != EOF)
602 #endif
603     switch (i) {
604     case 'A':
605       alias_queries = mutt_add_list (alias_queries, optarg);
606       break;
607     case 'a':
608       if (strlen(optarg)<=512)
609         attach = mutt_add_list (attach, optarg);
610       else{
611         printf("too long arguments. exiting ...\n");
612         exit(1);
613       }
614       break;
615
616     case 'F':
617       str_replace (&Muttrc, optarg);
618       break;
619
620     case 'f':
621       strfcpy (folder, optarg, sizeof (folder));
622       explicit_folder = 1;
623       break;
624
625     case 'b':
626     case 'c':
627       if (!msg)
628         msg = mutt_new_header ();
629       if (!msg->env)
630         msg->env = mutt_new_envelope ();
631       if (i == 'b')
632         msg->env->bcc = rfc822_parse_adrlist (msg->env->bcc, optarg);
633       else
634         msg->env->cc = rfc822_parse_adrlist (msg->env->cc, optarg);
635       break;
636
637     case 'd':
638       debug_setlevel(atoi(optarg));
639 #ifndef DEBUG
640       printf _("DEBUG was not defined during compilation.  Ignored.\n");
641 #endif
642       break;
643
644     case 't':
645       dump_variables = 2;
646       break;
647
648     case 'T':
649       dump_variables = 1;
650       break;
651
652     case 'e':
653       commands = mutt_add_list (commands, optarg);
654       break;
655
656     case 'H':
657       draftFile = optarg;
658       break;
659
660     case 'i':
661       includeFile = optarg;
662       break;
663
664     case 'm':
665       /* should take precedence over .muttrc setting, so save it for later */
666       newMagic = optarg;
667       break;
668
669     case 'n':
670       flags |= M_NOSYSRC;
671       break;
672
673     case 'p':
674       sendflags |= SENDPOSTPONED;
675       break;
676
677     case 'Q':
678       queries = mutt_add_list (queries, optarg);
679       break;
680
681     case 'R':
682       flags |= M_RO;            /* read-only mode */
683       break;
684
685     case 's':
686       subject = optarg;
687       break;
688
689     case 'v':
690       version++;
691       break;
692
693     case 'x':                  /* mailx compatible send mode */
694       sendflags |= SENDMAILX;
695       break;
696
697     case 'y':                  /* My special hack mode */
698       flags |= M_SELECT;
699       break;
700
701 #ifdef USE_NNTP
702     case 'g':                  /* Specify a newsserver */
703       {
704         char buf[LONG_STRING];
705
706         snprintf (buf, sizeof (buf), "set news_server=%s", optarg);
707         commands = mutt_add_list (commands, buf);
708       }
709
710     case 'G':                  /* List of newsgroups */
711       flags |= M_SELECT | M_NEWS;
712       break;
713 #endif
714
715     case 'z':
716       flags |= M_IGNORE;
717       break;
718
719     case 'Z':
720       flags |= M_BUFFY | M_IGNORE;
721       break;
722
723     default:
724       mutt_usage ();
725     }
726
727   switch (version) {
728   case 0:
729     break;
730   case 1:
731     show_version ();
732     break;
733   default:
734     puts (mutt_make_version (1));
735     puts (_(Copyright));
736     puts (_(ReachingUs));
737     exit (0);
738   }
739
740   /* Check for a batch send. */
741   if (!isatty (0) || queries || alias_queries || dump_variables > 0) {
742     set_option (OPTNOCURSES);
743     sendflags = SENDBATCH;
744   }
745
746   /* This must come before mutt_init() because curses needs to be started
747      before calling the init_pair() function to set the color scheme.  */
748   if (!option (OPTNOCURSES))
749     start_curses ();
750
751   /* set defaults and read init files */
752   mx_init ();
753   mutt_init (flags & M_NOSYSRC, commands);
754   mutt_free_list (&commands);
755
756   /* Initialize crypto */
757   crypt_init ();
758
759   if (queries)
760     return mutt_query_variables (queries);
761   if (dump_variables > 0)
762     return (mutt_dump_variables (dump_variables-1));
763
764   if (alias_queries) {
765     int rv = 0;
766     ADDRESS *a;
767
768     for (; alias_queries; alias_queries = alias_queries->next) {
769       if ((a = mutt_lookup_alias (alias_queries->data))) {
770         /* output in machine-readable form */
771         mutt_addrlist_to_idna (a, NULL);
772         mutt_write_address_list (a, stdout, 0, 0);
773       }
774       else {
775         rv = 1;
776         printf ("%s\n", alias_queries->data);
777       }
778     }
779     return rv;
780   }
781
782   if (newMagic)
783     mx_set_magic (newMagic);
784
785   if (!option (OPTNOCURSES)) {
786     SETCOLOR (MT_COLOR_NORMAL);
787     clear ();
788     mutt_error = mutt_curses_error;
789     mutt_message = mutt_curses_message;
790   }
791
792   /* Create the Maildir directory if it doesn't exist. */
793   if (!option (OPTNOCURSES) && Maildir) {
794     struct stat sb;
795     char fpath[_POSIX_PATH_MAX];
796     char msg[STRING];
797
798     strfcpy (fpath, Maildir, sizeof (fpath));
799     mutt_expand_path (fpath, sizeof (fpath));
800 #ifdef USE_IMAP
801     /* we're not connected yet - skip mail folder creation */
802     if (mx_get_magic (fpath) != M_IMAP)
803 #endif
804       if (stat (fpath, &sb) == -1 && errno == ENOENT) {
805         snprintf (msg, sizeof (msg), _("%s does not exist. Create it?"),
806                   Maildir);
807         if (mutt_yesorno (msg, M_YES) == M_YES) {
808           if (mkdir (fpath, 0700) == -1 && errno != EEXIST)
809             mutt_error (_("Can't create %s: %s."), Maildir, strerror (errno));
810         }
811       }
812   }
813
814   if (sendflags & SENDPOSTPONED) {
815     if (!option (OPTNOCURSES))
816       mutt_flushinp ();
817     ci_send_message (SENDPOSTPONED, NULL, NULL, NULL, NULL);
818     mutt_endwin (NULL);
819   }
820   else if (subject || msg || sendflags || draftFile || includeFile || attach
821            || optind < argc) {
822     FILE *fin = NULL;
823     char buf[LONG_STRING];
824     char *tempfile = NULL, *infile = NULL;
825     char *bodytext = NULL;
826
827     if (!option (OPTNOCURSES))
828       mutt_flushinp ();
829
830     if (!msg)
831       msg = mutt_new_header ();
832
833     if (draftFile)
834       infile = draftFile;
835     else {
836       if (!msg->env)
837         msg->env = mutt_new_envelope ();
838
839       for (i = optind; i < argc; i++) {
840         if (url_check_scheme (argv[i]) == U_MAILTO)
841           url_parse_mailto (msg->env, &bodytext, argv[i]);
842         else
843           msg->env->to = rfc822_parse_adrlist (msg->env->to, argv[i]);
844       }
845
846       if (option (OPTAUTOEDIT) && !msg->env->to && !msg->env->cc) {
847         if (!option (OPTNOCURSES))
848           mutt_endwin (NULL);
849         fputs (_("No recipients specified.\n"), stderr);
850         exit (1);
851       }
852
853       if (subject)
854         msg->env->subject = str_dup (subject);
855
856       if (includeFile)
857         infile = includeFile;
858     }
859
860     if (infile || bodytext) {
861       if (infile) {
862         if (str_cmp ("-", infile) == 0)
863           fin = stdin;
864         else {
865           char path[_POSIX_PATH_MAX];
866
867           strfcpy (path, infile, sizeof (path));
868           mutt_expand_path (path, sizeof (path));
869           if ((fin = fopen (path, "r")) == NULL) {
870             if (!option (OPTNOCURSES))
871               mutt_endwin (NULL);
872             perror (path);
873             exit (1);
874           }
875         }
876       }
877       else
878         fin = NULL;
879
880       mutt_mktemp (buf);
881       tempfile = str_dup (buf);
882
883       if (draftFile)
884         msg->env = mutt_read_rfc822_header (fin, NULL, 1, 0);
885
886       /* is the following if still needed? */
887
888       if (tempfile) {
889         FILE *fout;
890
891         if ((fout = safe_fopen (tempfile, "w")) == NULL) {
892           if (!option (OPTNOCURSES))
893             mutt_endwin (NULL);
894           perror (tempfile);
895           fclose (fin);
896           mem_free (&tempfile);
897           exit (1);
898         }
899         if (fin)
900           mutt_copy_stream (fin, fout);
901         else if (bodytext)
902           fputs (bodytext, fout);
903         fclose (fout);
904         if (fin && fin != stdin)
905           fclose (fin);
906       }
907     }
908
909     mem_free (&bodytext);
910
911     if (attach) {
912       LIST *t = attach;
913       BODY *a = NULL;
914
915       while (t) {
916         if (a) {
917           a->next = mutt_make_file_attach (t->data);
918           a = a->next;
919         }
920         else
921           msg->content = a = mutt_make_file_attach (t->data);
922         if (!a) {
923           if (!option (OPTNOCURSES))
924             mutt_endwin (NULL);
925           fprintf (stderr, _("%s: unable to attach file.\n"), t->data);
926           mutt_free_list (&attach);
927           exit (1);
928         }
929         t = t->next;
930       }
931       mutt_free_list (&attach);
932     }
933
934     ci_send_message (sendflags, msg, tempfile, NULL, NULL);
935
936     if (!option (OPTNOCURSES))
937       mutt_endwin (NULL);
938   }
939   else {
940     if (flags & M_BUFFY) {
941       if (!buffy_check (0)) {
942         mutt_endwin _("No mailbox with new mail.");
943
944         exit (1);
945       }
946       folder[0] = 0;
947       buffy_next (folder, sizeof (folder));
948     }
949     else if (flags & M_SELECT) {
950 #ifdef USE_NNTP
951       if (flags & M_NEWS) {
952         set_option (OPTNEWS);
953         if (!(CurrentNewsSrv = mutt_select_newsserver (NewsServer))) {
954           mutt_endwin (Errorbuf);
955           exit (1);
956         }
957       }
958       else
959 #endif
960       if (!Incoming) {
961         mutt_endwin _("No incoming mailboxes defined.");
962
963         exit (1);
964       }
965       folder[0] = 0;
966       mutt_select_file (folder, sizeof (folder), M_SEL_FOLDER | M_SEL_BUFFY);
967       if (!folder[0]) {
968         mutt_endwin (NULL);
969         exit (0);
970       }
971     }
972
973     if (!folder[0])
974       strfcpy (folder, NONULL (Spoolfile), sizeof (folder));
975
976 #ifdef USE_NNTP
977     if (option (OPTNEWS)) {
978       unset_option (OPTNEWS);
979       nntp_expand_path (folder, sizeof (folder),
980                         &CurrentNewsSrv->conn->account);
981     }
982     else
983 #endif
984       mutt_expand_path (folder, sizeof (folder));
985
986     str_replace (&CurrentFolder, folder);
987     str_replace (&LastFolder, folder);
988
989     if (flags & M_IGNORE) {
990       /* check to see if there are any messages in the folder */
991       switch (mx_check_empty (folder)) {
992       case -1:
993         mutt_endwin (strerror (errno));
994         exit (1);
995       case 1:
996         mutt_endwin _("Mailbox is empty.");
997         exit (1);
998       }
999     }
1000
1001     mutt_folder_hook (folder);
1002
1003     if ((Context = mx_open_mailbox (folder, ((flags & M_RO)
1004                                              || option (OPTREADONLY)) ?
1005                                     M_READONLY : 0, NULL))
1006         || !explicit_folder) {
1007       mutt_index_menu ();
1008       if (option (OPTXTERMSETTITLES))
1009         mutt_xterm_set_title (NONULL (XtermLeave));
1010       if (Context)
1011         mem_free (&Context);
1012     }
1013     mutt_endwin (Errorbuf);
1014   }
1015
1016   exit (0);
1017 }