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