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