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