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