Nico Golde:
[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 static const char *ReachingUs = N_("\
56 To contact the developers, please mail to <mutt-ng-devel@lists.berlios.de>.\n\
57 To visit the Mutt-ng homepage go to http://www.muttng.org.\n\
58 To report a bug, please use the fleang(1) utility.\n");
59
60 static const char *Notice = N_("\
61 Copyright (C) 1996-2002 Michael R. Elkins and others.\n\
62 Mutt-ng comes with ABSOLUTELY NO WARRANTY; for details type `muttng -vv'.\n\
63 Mutt is free software, and you are welcome to redistribute it\n\
64 under certain conditions; type `muttng -vv' for details.\n");
65
66 static const char *Copyright = N_("\
67 Copyright (C) 1996-2004 Michael R. Elkins <me@mutt.org>\n\
68 Copyright (C) 1996-2002 Brandon Long <blong@fiction.net>\n\
69 Copyright (C) 1997-2005 Thomas Roessler <roessler@does-not-exist.org>\n\
70 Copyright (C) 1998-2005 Werner Koch <wk@isil.d.shuttle.de>\n\
71 Copyright (C) 1999-2005 Brendan Cully <brendan@kublai.com>\n\
72 Copyright (C) 1999-2002 Tommi Komulainen <Tommi.Komulainen@iki.fi>\n\
73 Copyright (C) 2000-2002 Edmund Grimley Evans <edmundo@rano.org>\n\n\
74 Copyright (C) 2005:\n\
75   Parts were written/modified by:\n\
76      Andreas Krennmair <ak@synflood.at>\n\
77      Nico Golde <nico@ngolde.de>\n\
78      Rocco Rutte <pdmef@cs.tu-berlin.de>\n\
79 \n\
80 Lots of others not mentioned here contributed lots of code,\n\
81 fixes, and suggestions.\n\
82 \n\
83     This program is free software; you can redistribute it and/or modify\n\
84     it under the terms of the GNU General Public License as published by\n\
85     the Free Software Foundation; either version 2 of the License, or\n\
86     (at your option) any later version.\n\
87 \n\
88     This program is distributed in the hope that it will be useful,\n\
89     but WITHOUT ANY WARRANTY; without even the implied warranty of\n\
90     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n\
91     GNU General Public License for more details.\n\
92 \n\
93     You should have received a copy of the GNU General Public License\n\
94     along with this program; if not, write to the Free Software\n\
95     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.\n\
96 ");
97
98 void mutt_exit (int code)
99 {
100   mutt_endwin (NULL);
101   exit (code);
102 }
103
104 static void mutt_usage (void)
105 {
106   puts (mutt_make_version ());
107
108   puts
109     _
110     ("usage: muttng [ -nRyzZ ] [ -e <cmd> ] [ -F <file> ] [ -m <type> ] [ -f <file> ]\n\
111        muttng [ -nR ] [ -e <cmd> ] [ -F <file> ] -Q <query> [ -Q <query> ] [...]\n\
112        muttng [ -nR ] [ -e <cmd> ] [ -F <file> ] -A <alias> [ -A <alias> ] [...]\n\
113        muttng [ -nR ] [ -e <cmd> ] [ -F <file> ] -D\n\
114        muttng [ -nx ] [ -e <cmd> ] [ -a <file> ] [ -F <file> ] [ -H <file> ] [ -i <file> ] [ -s <subj> ] [ -b <addr> ] [ -c <addr> ] <addr> [ ... ]\n\
115        muttng [ -n ] [ -e <cmd> ] [ -F <file> ] -p\n\
116        muttng -v[v]\n\
117 \n\
118 options:\n\
119   -A <alias>\texpand the given alias\n\
120   -a <file>\tattach a file to the message\n\
121   -b <address>\tspecify a blind carbon-copy (BCC) address\n\
122   -c <address>\tspecify a carbon-copy (CC) address\n\
123   -D\t\tprint the value of all variables to stdout");
124 #if DEBUG
125   puts _("  -d <level>\tlog debugging output to ~/.muttngdebug0");
126 #endif
127   puts _("\
128   -e <command>\tspecify a command to be executed after initialization\n\
129   -f <file>\tspecify which mailbox to read\n\
130   -F <file>\tspecify an alternate muttrc file\n\
131   -g <server>\tspecify a newsserver (if compiled with NNTP)\n\
132   -G\t\tselect a newsgroup (if compiled with NNTP)\n\
133   -H <file>\tspecify a draft file to read header and body from\n\
134   -i <file>\tspecify a file which Mutt-ng should include in the body\n\
135   -d <level>\t specify debugging level of Mutt-ng\n\
136   -m <type>\tspecify a default mailbox type\n\
137   -n\t\tcauses Mutt-ng not to read the system Muttngrc\n\
138   -p\t\trecall a postponed message\n\
139   -Q <variable>\tquery a configuration variable\n\
140   -R\t\topen mailbox in read-only mode\n\
141   -s <subj>\tspecify a subject (must be in quotes if it has spaces)\n\
142   -v\t\tshow version and compile-time definitions\n\
143   -x\t\tsimulate the mailx send mode\n\
144   -y\t\tselect a mailbox specified in your `mailboxes' list\n\
145   -z\t\texit immediately if there are no messages in the mailbox\n\
146   -Z\t\topen the first folder with new message, exit immediately if none\n\
147   -h\t\tthis help message");
148
149   exit (0);
150 }
151
152 static void show_version (void)
153 {
154   struct utsname uts;
155
156   puts (mutt_make_version ());
157   puts (_(Notice));
158
159   uname (&uts);
160
161 #ifdef _AIX
162   printf ("System: %s %s.%s", uts.sysname, uts.version, uts.release);
163 #elif defined (SCO)
164   printf ("System: SCO %s", uts.release);
165 #else
166   printf ("System: %s %s", uts.sysname, uts.release);
167 #endif
168
169   printf (" (%s)", uts.machine);
170
171 #ifdef NCURSES_VERSION
172   printf (" [using ncurses %s]", NCURSES_VERSION);
173 #elif defined(USE_SLANG_CURSES)
174   printf (" [using slang %d]", SLANG_VERSION);
175 #endif
176
177 #ifdef _LIBICONV_VERSION
178   printf (" [using libiconv %d.%d]", _LIBICONV_VERSION >> 8,
179           _LIBICONV_VERSION & 0xff);
180 #endif
181
182 #ifdef HAVE_LIBIDN
183   printf (" [using libidn %s (compiled with %s)]",
184           stringprep_check_version (NULL), STRINGPREP_VERSION);
185 #endif
186
187   puts (_("\nCompile options:"));
188
189 #ifdef DOMAIN
190   printf ("DOMAIN=\"%s\"\n", DOMAIN);
191 #else
192   puts ("-DOMAIN");
193 #endif
194
195 #ifdef DEBUG
196   puts ("+DEBUG");
197 #else
198   puts ("-DEBUG");
199 #endif
200
201
202
203   puts (
204 #ifdef HOMESPOOL
205          "+HOMESPOOL  "
206 #else
207          "-HOMESPOOL  "
208 #endif
209 #ifdef USE_SETGID
210          "+USE_SETGID  "
211 #else
212          "-USE_SETGID  "
213 #endif
214 #ifdef USE_DOTLOCK
215          "+USE_DOTLOCK  "
216 #else
217          "-USE_DOTLOCK  "
218 #endif
219 #ifdef DL_STANDALONE
220          "+DL_STANDALONE  "
221 #else
222          "-DL_STANDALONE  "
223 #endif
224          "\n"
225 #ifdef USE_FCNTL
226          "+USE_FCNTL  "
227 #else
228          "-USE_FCNTL  "
229 #endif
230 #ifdef USE_FLOCK
231          "+USE_FLOCK   "
232 #else
233          "-USE_FLOCK   "
234 #endif
235 #ifdef USE_INODESORT
236          "+USE_INODESORT   "
237 #else
238          "-USE_INODESORT   "
239 #endif
240 #if USE_HCACHE
241          "+USE_HCACHE  "
242 #else
243          "-USE_HCACHE  "
244 #endif
245     );
246   puts (
247 #ifdef USE_POP
248          "+USE_POP  "
249 #else
250          "-USE_POP  "
251 #endif
252 #ifdef USE_NNTP
253          "+USE_NNTP  "
254 #else
255          "-USE_NNTP  "
256 #endif
257 #ifdef USE_IMAP
258          "+USE_IMAP  "
259 #else
260          "-USE_IMAP  "
261 #endif
262 #ifdef USE_GSS
263          "+USE_GSS  "
264 #else
265          "-USE_GSS  "
266 #endif
267 #ifdef USE_SSL
268          "+USE_SSL  "
269 #else
270          "-USE_SSL  "
271 #endif
272 #ifdef USE_GNUTLS
273          "+USE_GNUTLS  "
274 #else
275          "-USE_GNUTLS  "
276 #endif
277 #ifdef USE_SASL
278          "+USE_SASL  "
279 #else
280          "-USE_SASL  "
281 #endif
282 #ifdef USE_SASL2
283          "+USE_SASL2  "
284 #else
285          "-USE_SASL2  "
286 #endif
287 #ifdef USE_LIBESMTP
288          "+USE_LIBESMTP  "
289 #else
290          "-USE_LIBESMTP  "
291 #endif
292          "\n"
293 #ifdef HAVE_REGCOMP
294          "+HAVE_REGCOMP  "
295 #else
296          "-HAVE_REGCOMP  "
297 #endif
298 #ifdef USE_GNU_REGEX
299          "+USE_GNU_REGEX  "
300 #else
301          "-USE_GNU_REGEX  "
302 #endif
303 #ifdef USE_COMPRESSED
304          "+COMPRESSED  "
305 #else
306          "-COMPRESSED  "
307 #endif
308          "\n"
309 #ifdef HAVE_COLOR
310          "+HAVE_COLOR  "
311 #else
312          "-HAVE_COLOR  "
313 #endif
314 #ifdef HAVE_START_COLOR
315          "+HAVE_START_COLOR  "
316 #else
317          "-HAVE_START_COLOR  "
318 #endif
319 #ifdef HAVE_TYPEAHEAD
320          "+HAVE_TYPEAHEAD  "
321 #else
322          "-HAVE_TYPEAHEAD  "
323 #endif
324 #ifdef HAVE_BKGDSET
325          "+HAVE_BKGDSET  "
326 #else
327          "-HAVE_BKGDSET  "
328 #endif
329          "\n"
330 #ifdef HAVE_CURS_SET
331          "+HAVE_CURS_SET  "
332 #else
333          "-HAVE_CURS_SET  "
334 #endif
335 #ifdef HAVE_META
336          "+HAVE_META  "
337 #else
338          "-HAVE_META  "
339 #endif
340 #ifdef HAVE_RESIZETERM
341          "+HAVE_RESIZETERM  "
342 #else
343          "-HAVE_RESIZETERM  "
344 #endif
345     );
346
347   puts (
348 #ifdef CRYPT_BACKEND_CLASSIC_PGP
349          "+CRYPT_BACKEND_CLASSIC_PGP  "
350 #else
351          "-CRYPT_BACKEND_CLASSIC_PGP  "
352 #endif
353 #ifdef CRYPT_BACKEND_CLASSIC_SMIME
354          "+CRYPT_BACKEND_CLASSIC_SMIME  "
355 #else
356          "-CRYPT_BACKEND_CLASSIC_SMIME  "
357 #endif
358 #ifdef CRYPT_BACKEND_GPGME
359          "+CRYPT_BACKEND_GPGME  "
360 #else
361          "-CRYPT_BACKEND_GPGME  "
362 #endif
363 #ifdef BUFFY_SIZE
364          "+BUFFY_SIZE "
365 #else
366          "-BUFFY_SIZE "
367 #endif
368 #ifdef SUN_ATTACHMENT
369          "+SUN_ATTACHMENT  "
370 #else
371          "-SUN_ATTACHMENT  "
372 #endif
373          "\n"
374 #ifdef ENABLE_NLS
375          "+ENABLE_NLS  "
376 #else
377          "-ENABLE_NLS  "
378 #endif
379 #ifdef LOCALES_HACK
380          "+LOCALES_HACK  "
381 #else
382          "-LOCALES_HACK  "
383 #endif
384 #ifdef HAVE_WC_FUNCS
385          "+HAVE_WC_FUNCS  "
386 #else
387          "-HAVE_WC_FUNCS  "
388 #endif
389 #ifdef HAVE_LANGINFO_CODESET
390          "+HAVE_LANGINFO_CODESET  "
391 #else
392          "-HAVE_LANGINFO_CODESET  "
393 #endif
394 #ifdef HAVE_LANGINFO_YESEXPR
395          "+HAVE_LANGINFO_YESEXPR  "
396 #else
397          "-HAVE_LANGINFO_YESEXPR  "
398 #endif
399          "\n"
400 #if HAVE_ICONV
401          "+HAVE_ICONV  "
402 #else
403          "-HAVE_ICONV  "
404 #endif
405 #if ICONV_NONTRANS
406          "+ICONV_NONTRANS  "
407 #else
408          "-ICONV_NONTRANS  "
409 #endif
410 #if HAVE_LIBIDN
411          "+HAVE_LIBIDN  "
412 #else
413          "-HAVE_LIBIDN  "
414 #endif
415 #if HAVE_GETSID
416          "+HAVE_GETSID  "
417 #else
418          "-HAVE_GETSID  "
419 #endif
420 #if HAVE_GETADDRINFO
421          "+HAVE_GETADDRINFO  "
422 #else
423          "-HAVE_GETADDRINFO  "
424 #endif
425     );
426
427 #ifdef ISPELL
428   printf ("ISPELL=\"%s\"\n", ISPELL);
429 #else
430   puts ("-ISPELL");
431 #endif
432
433   printf ("SENDMAIL=\"%s\"\n", SENDMAIL);
434   printf ("MAILPATH=\"%s\"\n", MAILPATH);
435   printf ("PKGDATADIR=\"%s\"\n", PKGDATADIR);
436   printf ("SYSCONFDIR=\"%s\"\n", SYSCONFDIR);
437   printf ("EXECSHELL=\"%s\"\n", EXECSHELL);
438 #ifdef MIXMASTER
439   printf ("MIXMASTER=\"%s\"\n", MIXMASTER);
440 #else
441   puts ("-MIXMASTER");
442 #endif
443
444   puts (_(ReachingUs));
445
446   puts (_("Mutt-ng is based on the following patches written for mutt:\n"));
447   mutt_print_patchlist ();
448
449   exit (0);
450 }
451
452 static void start_curses (void)
453 {
454   km_init ();                   /* must come before mutt_init */
455
456 #ifdef USE_SLANG_CURSES
457   SLtt_Ignore_Beep = 1;         /* don't do that #*$@^! annoying visual beep! */
458   SLsmg_Display_Eight_Bit = 128;        /* characters above this are printable */
459   SLtt_set_color (0, NULL, "default", "default");
460 #else
461   /* should come before initscr() so that ncurses 4.2 doesn't try to install
462      its own SIGWINCH handler */
463   mutt_signal_init ();
464 #endif
465   if (initscr () == NULL) {
466     puts _("Error initializing terminal.");
467
468     exit (1);
469   }
470 #if 1                           /* USE_SLANG_CURSES  - commenting out suggested in #455. */
471   /* slang requires the signal handlers to be set after initializing */
472   mutt_signal_init ();
473 #endif
474   ci_start_color ();
475   keypad (stdscr, TRUE);
476   cbreak ();
477   noecho ();
478 #if HAVE_TYPEAHEAD
479   typeahead (-1);               /* simulate smooth scrolling */
480 #endif
481 #if HAVE_META
482   meta (stdscr, TRUE);
483 #endif
484 }
485
486 #define M_IGNORE  (1<<0)        /* -z */
487 #define M_BUFFY   (1<<1)        /* -Z */
488 #define M_NOSYSRC (1<<2)        /* -n */
489 #define M_RO      (1<<3)        /* -R */
490 #define M_SELECT  (1<<4)        /* -y */
491 #ifdef USE_NNTP
492 #define M_NEWS    (1<<5)        /* -g and -G */
493 #endif
494
495 int main (int argc, char **argv)
496 {
497   char folder[_POSIX_PATH_MAX] = "";
498   char *subject = NULL;
499   char *includeFile = NULL;
500   char *draftFile = NULL;
501   char *newMagic = NULL;
502   HEADER *msg = NULL;
503   LIST *attach = NULL;
504   LIST *commands = NULL;
505   LIST *queries = NULL;
506   LIST *alias_queries = NULL;
507   int sendflags = 0;
508   int flags = 0;
509   int version = 0;
510   int i;
511   int explicit_folder = 0;
512   int dump_variables = 0;
513   extern char *optarg;
514   extern int optind;
515
516   /* initialize random number for tmp file creation */ 
517   srand ((unsigned int) time (NULL));
518   
519   /* sanity check against stupid administrators */
520   
521   if (getegid () != getgid ()) {
522     fprintf (stderr, "%s: I don't want to run with privileges!\n", argv[0]);
523     exit (1);
524   }
525
526 #ifdef ENABLE_NLS
527   /* FIXME what about init.c:1439 ? */
528   setlocale (LC_ALL, "");
529   bindtextdomain (PACKAGE, MUTTLOCALEDIR);
530   textdomain (PACKAGE);
531 #endif
532
533   setlocale (LC_CTYPE, "");
534
535   mutt_error = mutt_nocurses_error;
536   mutt_message = mutt_nocurses_error;
537   SRAND (time (NULL));
538   umask (077);
539
540   memset (Options, 0, sizeof (Options));
541   memset (QuadOptions, 0, sizeof (QuadOptions));
542
543 #ifdef USE_NNTP
544   while ((i =
545           getopt (argc, argv,
546                   "A:a:b:F:f:c:Dd:e:g:GH:s:i:hm:npQ:RvxyzZ")) != EOF)
547 #else
548   while ((i =
549           getopt (argc, argv, "A:a:b:F:f:c:Dd:e:H:s:i:hm:npQ:RvxyzZ")) != EOF)
550 #endif
551     switch (i) {
552     case 'A':
553       alias_queries = mutt_add_list (alias_queries, optarg);
554       break;
555     case 'a':
556       if (strlen(optarg)<=512)
557         attach = mutt_add_list (attach, optarg);
558       else{
559         printf("too long arguments. exiting ...\n");
560         exit(1);
561       }
562       break;
563
564     case 'F':
565       str_replace (&Muttrc, optarg);
566       break;
567
568     case 'f':
569       strfcpy (folder, optarg, sizeof (folder));
570       explicit_folder = 1;
571       break;
572
573     case 'b':
574     case 'c':
575       if (!msg)
576         msg = mutt_new_header ();
577       if (!msg->env)
578         msg->env = mutt_new_envelope ();
579       if (i == 'b')
580         msg->env->bcc = rfc822_parse_adrlist (msg->env->bcc, optarg);
581       else
582         msg->env->cc = rfc822_parse_adrlist (msg->env->cc, optarg);
583       break;
584
585     case 'd':
586       debug_setlevel(atoi(optarg));
587 #ifdef DEBUG
588       if (atoi(optarg) <= DEBUG_MAX_LEVEL && atoi(optarg) >= DEBUG_MIN_LEVEL)
589         printf (_("Debugging at level %d.\n"), atoi(optarg));
590       else {
591         printf(_("Please specify a valid debugging level ("
592                  DEBUG_MIN_LEVEL_S "-" DEBUG_MAX_LEVEL_S ").\n"));
593         return 1;
594       }
595 #else
596       printf _("DEBUG was not defined during compilation.  Ignored.\n");
597 #endif
598       break;
599
600     case 'D':
601       dump_variables = 1;
602       break;
603
604     case 'e':
605       commands = mutt_add_list (commands, optarg);
606       break;
607
608     case 'H':
609       draftFile = optarg;
610       break;
611
612     case 'i':
613       includeFile = optarg;
614       break;
615
616     case 'm':
617       /* should take precedence over .muttrc setting, so save it for later */
618       newMagic = optarg;
619       break;
620
621     case 'n':
622       flags |= M_NOSYSRC;
623       break;
624
625     case 'p':
626       sendflags |= SENDPOSTPONED;
627       break;
628
629     case 'Q':
630       queries = mutt_add_list (queries, optarg);
631       break;
632
633     case 'R':
634       flags |= M_RO;            /* read-only mode */
635       break;
636
637     case 's':
638       subject = optarg;
639       break;
640
641     case 'v':
642       version++;
643       break;
644
645     case 'x':                  /* mailx compatible send mode */
646       sendflags |= SENDMAILX;
647       break;
648
649     case 'y':                  /* My special hack mode */
650       flags |= M_SELECT;
651       break;
652
653 #ifdef USE_NNTP
654     case 'g':                  /* Specify a newsserver */
655       {
656         char buf[LONG_STRING];
657
658         snprintf (buf, sizeof (buf), "set news_server=%s", optarg);
659         commands = mutt_add_list (commands, buf);
660       }
661
662     case 'G':                  /* List of newsgroups */
663       flags |= M_SELECT | M_NEWS;
664       break;
665 #endif
666
667     case 'z':
668       flags |= M_IGNORE;
669       break;
670
671     case 'Z':
672       flags |= M_BUFFY | M_IGNORE;
673       break;
674
675     default:
676       mutt_usage ();
677     }
678
679   switch (version) {
680   case 0:
681     break;
682   case 1:
683     show_version ();
684     break;
685   default:
686     puts (mutt_make_version ());
687     puts (_(Copyright));
688     puts (_(ReachingUs));
689     exit (0);
690   }
691
692   /* Check for a batch send. */
693   if (!isatty (0) || queries || alias_queries || dump_variables) {
694     set_option (OPTNOCURSES);
695     sendflags = SENDBATCH;
696   }
697
698   /* This must come before mutt_init() because curses needs to be started
699      before calling the init_pair() function to set the color scheme.  */
700   if (!option (OPTNOCURSES))
701     start_curses ();
702
703   /* set defaults and read init files */
704   mx_init ();
705   mutt_init (flags & M_NOSYSRC, commands);
706   mutt_free_list (&commands);
707
708   /* Initialize crypto */
709   crypt_init ();
710
711   if (queries)
712     return mutt_query_variables (queries);
713   if (dump_variables)
714     return (mutt_dump_variables ());
715
716   if (alias_queries) {
717     int rv = 0;
718     ADDRESS *a;
719
720     for (; alias_queries; alias_queries = alias_queries->next) {
721       if ((a = mutt_lookup_alias (alias_queries->data))) {
722         /* output in machine-readable form */
723         mutt_addrlist_to_idna (a, NULL);
724         mutt_write_address_list (a, stdout, 0, 0);
725       }
726       else {
727         rv = 1;
728         printf ("%s\n", alias_queries->data);
729       }
730     }
731     return rv;
732   }
733
734   if (newMagic)
735     mx_set_magic (newMagic);
736
737   if (!option (OPTNOCURSES)) {
738     SETCOLOR (MT_COLOR_NORMAL);
739     clear ();
740     mutt_error = mutt_curses_error;
741     mutt_message = mutt_curses_message;
742   }
743
744   /* Create the Maildir directory if it doesn't exist. */
745   if (!option (OPTNOCURSES) && Maildir) {
746     struct stat sb;
747     char fpath[_POSIX_PATH_MAX];
748     char msg[STRING];
749
750     strfcpy (fpath, Maildir, sizeof (fpath));
751     mutt_expand_path (fpath, sizeof (fpath));
752 #ifdef USE_IMAP
753     /* we're not connected yet - skip mail folder creation */
754     if (mx_get_magic (fpath) != M_IMAP)
755 #endif
756       if (stat (fpath, &sb) == -1 && errno == ENOENT) {
757         snprintf (msg, sizeof (msg), _("%s does not exist. Create it?"),
758                   Maildir);
759         if (mutt_yesorno (msg, 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 = mutt_new_header ();
784
785     if (draftFile)
786       infile = draftFile;
787     else {
788       if (!msg->env)
789         msg->env = mutt_new_envelope ();
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 = str_dup (subject);
807
808       if (includeFile)
809         infile = includeFile;
810     }
811
812     if (infile || bodytext) {
813       if (infile) {
814         if (str_cmp ("-", infile) == 0)
815           fin = stdin;
816         else {
817           char path[_POSIX_PATH_MAX];
818
819           strfcpy (path, infile, sizeof (path));
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 = str_dup (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           mem_free (&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     mem_free (&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       strfcpy (folder, NONULL (Spoolfile), sizeof (folder));
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     str_replace (&CurrentFolder, folder);
939     str_replace (&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         mem_free (&Context);
964     }
965     mutt_endwin (Errorbuf);
966   }
967
968   exit (0);
969 }