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