Fix a bunch of warnings in imap code
[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 "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 #if DEBUG
165   puts _("  -d <level>\tlog debugging output to ~/.madmuttdebug0");
166 #endif
167   puts _("\
168   -e <command>\tspecify a command to be executed after initialization\n\
169   -f <file>\tspecify which mailbox to read\n\
170   -F <file>\tspecify an alternate muttrngc file\n\
171   -g <server>\tspecify a newsserver (if compiled with NNTP)\n\
172   -G\t\tselect a newsgroup (if compiled with NNTP)\n\
173   -H <file>\tspecify a draft file to read header and body from\n\
174   -i <file>\tspecify a file which Madmutt should include in the body");
175   puts _("\
176   -d <level>\t specify debugging level of Madmutt\n\
177   -m <type>\tspecify a default mailbox type\n\
178   -n\t\tcauses Madmutt not to read the system Madmuttrc\n\
179   -p\t\trecall a postponed message\n\
180   -Q <variable>\tquery a configuration variable\n\
181   -R\t\topen mailbox in read-only mode\n\
182   -s <subj>\tspecify a subject (must be in quotes if it has spaces)");
183   puts _("\
184   -t\t\tprint the value of all variables to stdout\n\
185   -T\t\tprint the value of all changed variables to stdout\n\
186   -v\t\tshow version and compile-time definitions\n\
187   -y\t\tselect a mailbox specified in your `mailboxes' list\n\
188   -z\t\texit immediately if there are no messages in the mailbox\n\
189   -Z\t\topen the first folder with new message, exit immediately if none\n\
190   -h\t\tthis help message");
191
192   exit (0);
193 }
194
195 static void show_version (void)
196 {
197   struct utsname uts;
198 #ifdef USE_LIBESMTP
199   char buf[STRING];
200 #endif
201
202   puts (mutt_make_version (1));
203   puts (_(Notice));
204
205   uname (&uts);
206
207 #ifdef _AIX
208   printf ("System:\n  %s %s.%s", uts.sysname, uts.version, uts.release);
209 #elif defined (SCO)
210   printf ("System:\n  SCO %s", uts.release);
211 #else
212   printf ("System:\n  %s %s", uts.sysname, uts.release);
213 #endif
214
215   printf (" (%s)\nExternal Libraries:\n", uts.machine);
216
217 #ifdef NCURSES_VERSION
218   printf ("  ncurses %s\n", NCURSES_VERSION);
219 #elif defined(USE_SLANG_CURSES)
220   printf ("  slang %d\n", SLANG_VERSION);
221 #endif
222
223 #ifdef _LIBICONV_VERSION
224   printf ("  libiconv %d.%d\n", _LIBICONV_VERSION >> 8,
225           _LIBICONV_VERSION & 0xff);
226 #endif
227
228 #ifdef HAVE_LIBIDN
229   printf ("  libidn %s (compiled with %s)\n",
230           stringprep_check_version (NULL), STRINGPREP_VERSION);
231 #endif
232
233 #ifdef USE_LIBESMTP
234   smtp_version (buf, sizeof (buf), 0);
235   printf ("  libESMTP %s\n", buf);
236 #endif
237
238 #ifdef USE_HCACHE
239 #if defined(HAVE_QDBM)
240   printf ("  qdbm %s\n", dpversion);
241 #elif defined(HAVE_GDBM)
242   printf ("  gdbm %s\n", gdbm_version);
243 #elif defined(HAVE_DB4)
244   printf ("  DB4 %d.%d.%d\n", DB_VERSION_MAJOR, DB_VERSION_MINOR,
245           DB_VERSION_PATCH);
246 #endif
247 #endif
248
249 #ifdef USE_GNUTLS
250   printf ("  gnutls %s\n", LIBGNUTLS_VERSION);
251 #endif
252
253 #ifdef USE_SSL
254   printf ("  %s\n",OPENSSL_VERSION_TEXT);
255 #endif
256
257 #ifdef CRYPT_BACKEND_GPGME
258   printf ("  gpgme %s\n", GPGME_VERSION);
259 #endif
260
261   puts (_("Compile Options:"));
262
263 #ifdef DEBUG
264   puts ("  +DEBUG");
265 #else
266   puts ("  -DEBUG");
267 #endif
268
269   puts (
270 #ifdef HOMESPOOL
271          "  +HOMESPOOL  "
272 #else
273          "  -HOMESPOOL  "
274 #endif
275 #ifdef USE_SETGID
276          "+USE_SETGID  "
277 #else
278          "-USE_SETGID  "
279 #endif
280 #ifdef USE_DOTLOCK
281          "+USE_DOTLOCK  "
282 #else
283          "-USE_DOTLOCK  "
284 #endif
285 #ifdef USE_FCNTL
286          "+USE_FCNTL  "
287 #else
288          "-USE_FCNTL  "
289 #endif
290 #ifdef USE_FLOCK
291          "+USE_FLOCK   "
292 #else
293          "-USE_FLOCK   "
294 #endif
295 #ifdef USE_HCACHE
296          "+USE_HCACHE  "
297 #else
298          "-USE_HCACHE  "
299 #endif
300     );
301   puts (
302 #ifdef USE_NNTP
303          "+USE_NNTP  "
304 #else
305          "-USE_NNTP  "
306 #endif
307 #ifdef USE_GSS
308          "+USE_GSS  "
309 #else
310          "-USE_GSS  "
311 #endif
312 #ifdef USE_SSL
313          "+USE_SSL  "
314 #else
315          "-USE_SSL  "
316 #endif
317 #ifdef USE_GNUTLS
318          "+USE_GNUTLS  "
319 #else
320          "-USE_GNUTLS  "
321 #endif
322 #ifdef USE_SASL
323          "+USE_SASL  "
324 #else
325          "-USE_SASL  "
326 #endif
327 #ifdef USE_LIBESMTP
328          "+USE_LIBESMTP  "
329 #else
330          "-USE_LIBESMTP  "
331 #endif
332          "\n  "
333 #ifdef HAVE_REGCOMP
334          "+HAVE_REGCOMP  "
335 #else
336          "-HAVE_REGCOMP  "
337 #endif
338          "\n  "
339 #ifdef HAVE_COLOR
340          "+HAVE_COLOR  "
341 #else
342          "-HAVE_COLOR  "
343 #endif
344 #ifdef HAVE_START_COLOR
345          "+HAVE_START_COLOR  "
346 #else
347          "-HAVE_START_COLOR  "
348 #endif
349 #ifdef HAVE_TYPEAHEAD
350          "+HAVE_TYPEAHEAD  "
351 #else
352          "-HAVE_TYPEAHEAD  "
353 #endif
354 #ifdef HAVE_BKGDSET
355          "+HAVE_BKGDSET  "
356 #else
357          "-HAVE_BKGDSET  "
358 #endif
359          "\n  "
360 #ifdef HAVE_CURS_SET
361          "+HAVE_CURS_SET  "
362 #else
363          "-HAVE_CURS_SET  "
364 #endif
365 #ifdef HAVE_META
366          "+HAVE_META  "
367 #else
368          "-HAVE_META  "
369 #endif
370 #ifdef HAVE_RESIZETERM
371          "+HAVE_RESIZETERM  "
372 #else
373          "-HAVE_RESIZETERM  "
374 #endif
375     );
376
377   puts (
378 #ifdef CRYPT_BACKEND_GPGME
379          "+CRYPT_BACKEND_GPGME  "
380 #else
381          "-CRYPT_BACKEND_GPGME  "
382 #endif
383 #ifdef BUFFY_SIZE
384          "+BUFFY_SIZE "
385 #else
386          "-BUFFY_SIZE "
387 #endif
388          "\n  "
389 #ifdef ENABLE_NLS
390          "+ENABLE_NLS  "
391 #else
392          "-ENABLE_NLS  "
393 #endif
394 #ifdef HAVE_WC_FUNCS
395          "+HAVE_WC_FUNCS  "
396 #else
397          "-HAVE_WC_FUNCS  "
398 #endif
399 #ifdef HAVE_LANGINFO_CODESET
400          "+HAVE_LANGINFO_CODESET  "
401 #else
402          "-HAVE_LANGINFO_CODESET  "
403 #endif
404 #ifdef HAVE_LANGINFO_YESEXPR
405          "+HAVE_LANGINFO_YESEXPR  "
406 #else
407          "-HAVE_LANGINFO_YESEXPR  "
408 #endif
409          "\n  "
410 #ifdef HAVE_ICONV
411          "+HAVE_ICONV  "
412 #else
413          "-HAVE_ICONV  "
414 #endif
415 #if ICONV_NONTRANS
416          "+ICONV_NONTRANS  "
417 #else
418          "-ICONV_NONTRANS  "
419 #endif
420 #ifdef HAVE_LIBIDN
421          "+HAVE_LIBIDN  "
422 #else
423          "-HAVE_LIBIDN  "
424 #endif
425 #ifdef HAVE_GETSID
426          "+HAVE_GETSID  "
427 #else
428          "-HAVE_GETSID  "
429 #endif
430 #ifdef HAVE_GETADDRINFO
431          "+HAVE_GETADDRINFO  "
432 #else
433          "-HAVE_GETADDRINFO  "
434 #endif
435     );
436
437   puts (_("Built-In Defaults:"));
438
439 #ifdef ISPELL
440   printf ("  +ISPELL=\"%s\"\n", ISPELL);
441 #else
442   puts ("  -ISPELL");
443 #endif
444
445   printf ("  +SENDMAIL=\"%s\"\n", SENDMAIL);
446   printf ("  +MAILPATH=\"%s\"\n", MAILPATH);
447   printf ("  +PKGDATADIR=\"%s\"\n", PKGDATADIR);
448   printf ("  +PKGDOCDIR=\"%s\"\n", PKGDOCDIR);
449   printf ("  +SYSCONFDIR=\"%s\"\n", SYSCONFDIR);
450 #ifdef MIXMASTER
451   printf ("  +MIXMASTER=\"%s\"\n\n", MIXMASTER);
452 #else
453   puts ("  -MIXMASTER\n");
454 #endif
455
456   puts (_("MadMutt is based on Madmutt wich was based on Mutt before\n"));
457
458   exit (0);
459 }
460
461 static void start_curses (void)
462 {
463   km_init ();                   /* must come before mutt_init */
464
465 #ifdef USE_SLANG_CURSES
466   SLtt_Ignore_Beep = 1;         /* don't do that #*$@^! annoying visual beep! */
467   SLsmg_Display_Eight_Bit = 128;        /* characters above this are printable */
468   SLtt_set_color (0, NULL, "default", "default");
469 #else
470   /* should come before initscr() so that ncurses 4.2 doesn't try to install
471      its own SIGWINCH handler */
472   mutt_signal_initialize ();
473 #endif
474   if (initscr () == NULL) {
475     puts _("Error initializing terminal.");
476
477     exit (1);
478   }
479   mutt_signal_initialize ();
480   ci_start_color ();
481   keypad (stdscr, TRUE);
482   cbreak ();
483   noecho ();
484 #ifdef HAVE_TYPEAHEAD
485   typeahead (-1);               /* simulate smooth scrolling */
486 #endif
487 #ifdef HAVE_META
488   meta (stdscr, TRUE);
489 #endif
490 }
491
492 #define M_IGNORE  (1<<0)        /* -z */
493 #define M_BUFFY   (1<<1)        /* -Z */
494 #define M_NOSYSRC (1<<2)        /* -n */
495 #define M_RO      (1<<3)        /* -R */
496 #define M_SELECT  (1<<4)        /* -y */
497 #ifdef USE_NNTP
498 #define M_NEWS    (1<<5)        /* -g and -G */
499 #endif
500
501 int main (int argc, char **argv)
502 {
503   char folder[_POSIX_PATH_MAX] = "";
504   char *subject = NULL;
505   char *includeFile = NULL;
506   char *draftFile = NULL;
507   char *newMagic = NULL;
508   HEADER *msg = NULL;
509   string_list_t *attach = NULL;
510   string_list_t *commands = NULL;
511   string_list_t *queries = NULL;
512   string_list_t *alias_queries = NULL;
513   int sendflags = 0;
514   int flags = 0;
515   int version = 0;
516   int i;
517   int explicit_folder = 0;
518   int dump_variables = -1;
519
520   /* initialize random number for tmp file creation */ 
521   srand48((unsigned int) time (NULL));
522   
523   /* sanity check against stupid administrators */
524   
525   if (getegid () != getgid ()) {
526     fprintf (stderr, "%s: I don't want to run with privileges!\n", argv[0]);
527     exit (1);
528   }
529
530 #ifdef ENABLE_NLS
531   /* FIXME what about init.c:1439 ? */
532   setlocale (LC_ALL, "");
533   bindtextdomain (PACKAGE, MUTTLOCALEDIR);
534   textdomain (PACKAGE);
535 #endif
536
537   setlocale (LC_CTYPE, "");
538
539   mutt_error = mutt_nocurses_error;
540   mutt_message = mutt_nocurses_error;
541   srand48 (time (NULL));
542   umask (077);
543
544   p_clear(Options, countof(Options));
545   p_clear(QuadOptions, countof(QuadOptions));
546
547 #ifdef USE_NNTP
548   while ((i =
549           getopt (argc, argv,
550                   "A:a:b:F:f:c:d:e:g:GH:s:i:hm:npQ:RTtvyzZ")) != EOF)
551 #else
552   while ((i =
553           getopt (argc, argv, "A:a:b:F:f:c:d:e:H:s:i:hm:npQ:RTtvyzZ")) != EOF)
554 #endif
555     switch (i) {
556     case 'A':
557       alias_queries = mutt_add_list (alias_queries, optarg);
558       break;
559     case 'a':
560       if (strlen(optarg)<=512)
561         attach = mutt_add_list (attach, optarg);
562       else{
563         printf("too long arguments. exiting ...\n");
564         exit(1);
565       }
566       break;
567
568     case 'F':
569       m_strreplace(&Muttrc, optarg);
570       break;
571
572     case 'f':
573       m_strcpy(folder, sizeof(folder), optarg);
574       explicit_folder = 1;
575       break;
576
577     case 'b':
578     case 'c':
579       if (!msg)
580         msg = header_new();
581       if (!msg->env)
582         msg->env = envelope_new();
583       if (i == 'b')
584         msg->env->bcc = rfc822_parse_adrlist (msg->env->bcc, optarg);
585       else
586         msg->env->cc = rfc822_parse_adrlist (msg->env->cc, optarg);
587       break;
588
589     case 'd':
590       {
591         char buf[LONG_STRING];
592         int level = atoi (optarg);
593
594         snprintf (buf, sizeof (buf), "set debug_level=%d", level);
595         commands = mutt_add_list (commands, buf);
596         debug_setlevel (level);
597       }
598       break;
599
600     case 't':
601       dump_variables = 2;
602       break;
603
604     case 'T':
605       dump_variables = 1;
606       break;
607
608     case 'e':
609       commands = mutt_add_list (commands, optarg);
610       break;
611
612     case 'H':
613       draftFile = optarg;
614       break;
615
616     case 'i':
617       includeFile = optarg;
618       break;
619
620     case 'm':
621       /* should take precedence over .muttrc setting, so save it for later */
622       newMagic = optarg;
623       break;
624
625     case 'n':
626       flags |= M_NOSYSRC;
627       break;
628
629     case 'p':
630       sendflags |= SENDPOSTPONED;
631       break;
632
633     case 'Q':
634       queries = mutt_add_list (queries, optarg);
635       break;
636
637     case 'R':
638       flags |= M_RO;            /* read-only mode */
639       break;
640
641     case 's':
642       subject = optarg;
643       break;
644
645     case 'v':
646       version++;
647       break;
648
649     case 'y':                  /* My special hack mode */
650       flags |= M_SELECT;
651       break;
652
653 #ifdef USE_NNTP
654     case 'g':                  /* Specify a newsserver */
655       {
656         char buf[LONG_STRING];
657
658         snprintf (buf, sizeof (buf), "set nntp_host=%s", optarg);
659         commands = mutt_add_list (commands, buf);
660       }
661
662     case 'G':                  /* List of newsgroups */
663       flags |= M_SELECT | M_NEWS;
664       break;
665 #endif
666
667     case 'z':
668       flags |= M_IGNORE;
669       break;
670
671     case 'Z':
672       flags |= M_BUFFY | M_IGNORE;
673       break;
674
675     default:
676       mutt_usage ();
677     }
678
679   switch (version) {
680   case 0:
681     break;
682   case 1:
683     show_version ();
684     break;
685   default:
686     puts (mutt_make_version (1));
687     puts (_(Copyright_Mutt));
688     puts (_(Copyright_Mutt_NG));
689     puts (_(Copyright_GPL_1));
690     puts (_(Copyright_GPL_2));
691     puts (_(Copyright_GPL_3));
692     exit (0);
693   }
694
695   /* Check for a batch send. */
696   if (!isatty (0) || queries || alias_queries || dump_variables > 0) {
697     set_option (OPTNOCURSES);
698     sendflags = SENDBATCH;
699   }
700
701   /* This must come before mutt_init() because curses needs to be started
702      before calling the init_pair() function to set the color scheme.  */
703   if (!option (OPTNOCURSES))
704     start_curses ();
705
706   /* set defaults and read init files */
707   mx_init ();
708   mutt_init (flags & M_NOSYSRC, commands);
709   string_list_wipe(&commands);
710
711   /* Initialize crypto */
712   crypt_init ();
713
714   if (queries)
715     return mutt_query_variables (queries);
716   if (dump_variables > 0)
717     return (mutt_dump_variables (dump_variables-1));
718
719   if (alias_queries) {
720     int rv = 0;
721     address_t *a;
722
723     for (; alias_queries; alias_queries = alias_queries->next) {
724       if ((a = address_list_dup(alias_lookup(alias_queries->data)))) {
725         /* output in machine-readable form */
726         mutt_addrlist_to_idna (a, NULL);
727         mutt_write_address_list (a, stdout, 0, 0);
728         address_list_wipe(&a);
729       } else {
730         rv = 1;
731         printf ("%s\n", alias_queries->data);
732       }
733     }
734     return rv;
735   }
736
737   if (newMagic)
738     mx_set_magic (newMagic);
739
740   if (!option (OPTNOCURSES)) {
741     SETCOLOR (MT_COLOR_NORMAL);
742     clear ();
743     mutt_error = mutt_curses_error;
744     mutt_message = mutt_curses_message;
745   }
746
747   /* Create the Maildir directory if it doesn't exist. */
748   if (!option (OPTNOCURSES) && Maildir) {
749     struct stat sb;
750     char fpath[_POSIX_PATH_MAX];
751     char mesg[STRING];
752
753     m_strcpy(fpath, sizeof(fpath), Maildir);
754     mutt_expand_path (fpath, sizeof (fpath));
755     /* we're not connected yet - skip mail folder creation */
756     if (mx_get_magic (fpath) != M_IMAP)
757       if (stat (fpath, &sb) == -1 && errno == ENOENT) {
758         snprintf (mesg, sizeof (mesg), _("%s does not exist. Create it?"),
759                   Maildir);
760         if (mutt_yesorno (mesg, M_YES) == M_YES) {
761           if (mkdir (fpath, 0700) == -1 && errno != EEXIST)
762             mutt_error (_("Can't create %s: %s."), Maildir, strerror (errno));
763         }
764       }
765   }
766
767   if (sendflags & SENDPOSTPONED) {
768     if (!option (OPTNOCURSES))
769       mutt_flushinp ();
770     ci_send_message (SENDPOSTPONED, NULL, NULL, NULL, NULL);
771     mutt_endwin (NULL);
772   }
773   else if (subject || msg || sendflags || draftFile || includeFile || attach
774            || optind < argc) {
775     FILE *fin = NULL;
776     char buf[LONG_STRING];
777     char *tempfile = NULL, *infile = NULL;
778     char *bodytext = NULL;
779
780     if (!option (OPTNOCURSES))
781       mutt_flushinp ();
782
783     if (!msg)
784       msg = header_new();
785
786     if (draftFile)
787       infile = draftFile;
788     else {
789       if (!msg->env)
790         msg->env = envelope_new();
791
792       for (i = optind; i < argc; i++) {
793         if (url_check_scheme (argv[i]) == U_MAILTO)
794           url_parse_mailto (msg->env, &bodytext, argv[i]);
795         else
796           msg->env->to = rfc822_parse_adrlist (msg->env->to, argv[i]);
797       }
798
799       if (option (OPTAUTOEDIT) && !msg->env->to && !msg->env->cc) {
800         if (!option (OPTNOCURSES))
801           mutt_endwin (NULL);
802         fputs (_("No recipients specified.\n"), stderr);
803         exit (1);
804       }
805
806       if (subject)
807         msg->env->subject = m_strdup(subject);
808
809       if (includeFile)
810         infile = includeFile;
811     }
812
813     if (infile || bodytext) {
814       if (infile) {
815         if (m_strcmp("-", infile) == 0)
816           fin = stdin;
817         else {
818           char path[_POSIX_PATH_MAX];
819
820           m_strcpy(path, sizeof(path), infile);
821           mutt_expand_path (path, sizeof (path));
822           if ((fin = fopen (path, "r")) == NULL) {
823             if (!option (OPTNOCURSES))
824               mutt_endwin (NULL);
825             perror (path);
826             exit (1);
827           }
828         }
829       }
830       else
831         fin = NULL;
832
833       mutt_mktemp (buf);
834       tempfile = m_strdup(buf);
835
836       if (draftFile)
837         msg->env = mutt_read_rfc822_header (fin, NULL, 1, 0);
838
839       /* is the following if still needed? */
840
841       if (tempfile) {
842         FILE *fout;
843
844         if ((fout = safe_fopen (tempfile, "w")) == NULL) {
845           if (!option (OPTNOCURSES))
846             mutt_endwin (NULL);
847           perror (tempfile);
848           fclose (fin);
849           p_delete(&tempfile);
850           exit (1);
851         }
852         if (fin)
853           mutt_copy_stream (fin, fout);
854         else if (bodytext)
855           fputs (bodytext, fout);
856         fclose (fout);
857         if (fin && fin != stdin)
858           fclose (fin);
859       }
860     }
861
862     p_delete(&bodytext);
863
864     if (attach) {
865       string_list_t *t = attach;
866       BODY *a = NULL;
867
868       while (t) {
869         if (a) {
870           a->next = mutt_make_file_attach (t->data);
871           a = a->next;
872         }
873         else
874           msg->content = a = mutt_make_file_attach (t->data);
875         if (!a) {
876           if (!option (OPTNOCURSES))
877             mutt_endwin (NULL);
878           fprintf (stderr, _("%s: unable to attach file.\n"), t->data);
879           string_list_wipe(&attach);
880           exit (1);
881         }
882         t = t->next;
883       }
884       string_list_wipe(&attach);
885     }
886
887     ci_send_message (sendflags, msg, tempfile, NULL, NULL);
888
889     if (!option (OPTNOCURSES))
890       mutt_endwin (NULL);
891   }
892   else {
893     if (flags & M_BUFFY) {
894       if (!buffy_check (0)) {
895         mutt_endwin _("No mailbox with new mail.");
896
897         exit (1);
898       }
899       folder[0] = 0;
900       buffy_next (folder, sizeof (folder));
901     }
902     else if (flags & M_SELECT) {
903 #ifdef USE_NNTP
904       if (flags & M_NEWS) {
905         set_option (OPTNEWS);
906         if (!(CurrentNewsSrv = mutt_select_newsserver (NewsServer))) {
907           mutt_endwin (Errorbuf);
908           exit (1);
909         }
910       }
911       else
912 #endif
913       if (!Incoming) {
914         mutt_endwin _("No incoming mailboxes defined.");
915
916         exit (1);
917       }
918       folder[0] = 0;
919       mutt_select_file (folder, sizeof (folder), M_SEL_FOLDER | M_SEL_BUFFY);
920       if (!folder[0]) {
921         mutt_endwin (NULL);
922         exit (0);
923       }
924     }
925
926     if (!folder[0])
927       m_strcpy(folder, sizeof(folder), NONULL(Spoolfile));
928
929 #ifdef USE_NNTP
930     if (option (OPTNEWS)) {
931       unset_option (OPTNEWS);
932       nntp_expand_path (folder, sizeof (folder),
933                         &CurrentNewsSrv->conn->account);
934     }
935     else
936 #endif
937       mutt_expand_path (folder, sizeof (folder));
938
939     m_strreplace(&CurrentFolder, folder);
940     m_strreplace(&LastFolder, folder);
941
942     if (flags & M_IGNORE) {
943       /* check to see if there are any messages in the folder */
944       switch (mx_check_empty (folder)) {
945       case -1:
946         mutt_endwin (strerror (errno));
947         exit (1);
948       case 1:
949         mutt_endwin _("Mailbox is empty.");
950         exit (1);
951       }
952     }
953
954     mutt_folder_hook (folder);
955
956     if ((Context = mx_open_mailbox (folder, ((flags & M_RO)
957                                              || option (OPTREADONLY)) ?
958                                     M_READONLY : 0, NULL))
959         || !explicit_folder) {
960       mutt_index_menu ();
961       if (option (OPTXTERMSETTITLES))
962         mutt_xterm_set_title (NONULL (XtermLeave));
963       if (Context)
964         p_delete(&Context);
965     }
966     mutt_endwin (Errorbuf);
967   }
968
969   exit (0);
970 }