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