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