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