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