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