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