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