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