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