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