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