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