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