drop openssl support as well, only use GNUTLS.
[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 #ifdef HAVE_GETADDRINFO
307          "+HAVE_GETADDRINFO  "
308 #else
309          "-HAVE_GETADDRINFO  "
310 #endif
311     );
312
313   puts (_("Built-In Defaults:"));
314   printf ("  +SENDMAIL=\"%s\"\n", SENDMAIL);
315   printf ("  +MAILPATH=\"%s\"\n", MAILPATH);
316   printf ("  +PKGDATADIR=\"%s\"\n", PKGDATADIR);
317   printf ("  +PKGDOCDIR=\"%s\"\n", PKGDOCDIR);
318   printf ("  +SYSCONFDIR=\"%s\"\n", SYSCONFDIR);
319   printf ("  +MIXMASTER=\"%s\"\n\n", MIXMASTER);
320
321   puts (_("MadMutt is based on Madmutt wich was based on Mutt before\n"));
322
323   exit (0);
324 }
325
326 static void start_curses (void)
327 {
328   km_init ();                   /* must come before mutt_init */
329
330 #ifdef USE_SLANG_CURSES
331   SLtt_Ignore_Beep = 1;         /* don't do that #*$@^! annoying visual beep! */
332   SLsmg_Display_Eight_Bit = 128;        /* characters above this are printable */
333   SLtt_set_color (0, NULL, "default", "default");
334 #else
335   /* should come before initscr() so that ncurses 4.2 doesn't try to install
336      its own SIGWINCH handler */
337   mutt_signal_initialize ();
338 #endif
339   if (initscr () == NULL) {
340     puts _("Error initializing terminal.");
341
342     exit (1);
343   }
344   mutt_signal_initialize ();
345   ci_start_color ();
346   keypad (stdscr, TRUE);
347   cbreak ();
348   noecho ();
349 #ifdef HAVE_TYPEAHEAD
350   typeahead (-1);               /* simulate smooth scrolling */
351 #endif
352 #ifdef HAVE_META
353   meta (stdscr, TRUE);
354 #endif
355 }
356
357 #define M_IGNORE  (1<<0)        /* -z */
358 #define M_BUFFY   (1<<1)        /* -Z */
359 #define M_NOSYSRC (1<<2)        /* -n */
360 #define M_RO      (1<<3)        /* -R */
361 #define M_SELECT  (1<<4)        /* -y */
362 #ifdef USE_NNTP
363 #define M_NEWS    (1<<5)        /* -g and -G */
364 #endif
365
366 __attribute__((format(printf, 1, 0)))
367 static void mutt_nocurses_error (const char *fmt, ...)
368 {
369     va_list ap;
370
371     va_start(ap, fmt);
372     vfprintf(stderr, fmt, ap);
373     va_end(ap);
374     fputc('\n', stderr);
375 }
376
377 int main (int argc, char **argv)
378 {
379   char folder[_POSIX_PATH_MAX] = "";
380   char *subject = NULL;
381   char *includeFile = NULL;
382   char *draftFile = NULL;
383   char *newMagic = NULL;
384   HEADER *msg = NULL;
385   string_list_t *attach = NULL;
386   string_list_t *commands = NULL;
387   string_list_t *queries = NULL;
388   string_list_t *alias_queries = NULL;
389   int sendflags = 0;
390   int flags = 0;
391   int version = 0;
392   int i;
393   int explicit_folder = 0;
394   int dump_variables = -1;
395
396   /* initialize random number for tmp file creation */ 
397   srand48((unsigned int) time (NULL));
398   
399   /* sanity check against stupid administrators */
400   
401   if (getegid () != getgid ()) {
402     fprintf (stderr, "%s: I don't want to run with privileges!\n", argv[0]);
403     exit (1);
404   }
405
406 #ifdef ENABLE_NLS
407   /* FIXME what about init.c:1439 ? */
408   setlocale (LC_ALL, "");
409   bindtextdomain (PACKAGE, MUTTLOCALEDIR);
410   textdomain (PACKAGE);
411 #endif
412
413   setlocale (LC_CTYPE, "");
414
415   mutt_error = mutt_message = mutt_nocurses_error;
416   srand48 (time (NULL));
417   umask (077);
418
419   p_clear(Options, countof(Options));
420   p_clear(QuadOptions, countof(QuadOptions));
421
422   while ((i = getopt(argc, argv, "A:a:b:F:f:c:e:H:s:i:hm:npQ:RTtvyzZ"
423 #ifdef USE_NNTP
424                                                                     "g:G"
425 #endif
426                     )) >= 0)
427     switch (i) {
428     case 'A':
429       alias_queries = mutt_add_list (alias_queries, optarg);
430       break;
431     case 'a':
432       if (strlen(optarg)<=512)
433         attach = mutt_add_list (attach, optarg);
434       else{
435         printf("too long arguments. exiting ...\n");
436         exit(1);
437       }
438       break;
439
440     case 'F':
441       m_strreplace(&Muttrc, optarg);
442       break;
443
444     case 'f':
445       m_strcpy(folder, sizeof(folder), optarg);
446       explicit_folder = 1;
447       break;
448
449     case 'b':
450     case 'c':
451       if (!msg)
452         msg = header_new();
453       if (!msg->env)
454         msg->env = envelope_new();
455       if (i == 'b')
456         msg->env->bcc = rfc822_parse_adrlist (msg->env->bcc, optarg);
457       else
458         msg->env->cc = rfc822_parse_adrlist (msg->env->cc, optarg);
459       break;
460
461     case 't':
462       dump_variables = 2;
463       break;
464
465     case 'T':
466       dump_variables = 1;
467       break;
468
469     case 'e':
470       commands = mutt_add_list (commands, optarg);
471       break;
472
473     case 'H':
474       draftFile = optarg;
475       break;
476
477     case 'i':
478       includeFile = optarg;
479       break;
480
481     case 'm':
482       /* should take precedence over .muttrc setting, so save it for later */
483       newMagic = optarg;
484       break;
485
486     case 'n':
487       flags |= M_NOSYSRC;
488       break;
489
490     case 'p':
491       sendflags |= SENDPOSTPONED;
492       break;
493
494     case 'Q':
495       queries = mutt_add_list (queries, optarg);
496       break;
497
498     case 'R':
499       flags |= M_RO;            /* read-only mode */
500       break;
501
502     case 's':
503       subject = optarg;
504       break;
505
506     case 'v':
507       version++;
508       break;
509
510     case 'y':                  /* My special hack mode */
511       flags |= M_SELECT;
512       break;
513
514 #ifdef USE_NNTP
515     case 'g':                  /* Specify a newsserver */
516       {
517         char buf[LONG_STRING];
518
519         snprintf (buf, sizeof (buf), "set nntp_host=%s", optarg);
520         commands = mutt_add_list (commands, buf);
521       }
522
523     case 'G':                  /* List of newsgroups */
524       flags |= M_SELECT | M_NEWS;
525       break;
526 #endif
527
528     case 'z':
529       flags |= M_IGNORE;
530       break;
531
532     case 'Z':
533       flags |= M_BUFFY | M_IGNORE;
534       break;
535
536     default:
537       mutt_usage ();
538     }
539
540   if (version) {
541     show_version ();
542   }
543
544   /* Check for a batch send. */
545   if (!isatty (0) || queries || alias_queries || dump_variables > 0) {
546     set_option (OPTNOCURSES);
547     sendflags = SENDBATCH;
548   }
549
550   /* This must come before mutt_init() because curses needs to be started
551      before calling the init_pair() function to set the color scheme.  */
552   if (!option (OPTNOCURSES))
553     start_curses ();
554
555   /* set defaults and read init files */
556   mutt_init (flags & M_NOSYSRC, commands);
557   string_list_wipe(&commands);
558
559   if (queries)
560     return mutt_query_variables (queries);
561   if (dump_variables > 0)
562     return (mutt_dump_variables (dump_variables-1));
563
564   if (alias_queries) {
565     int rv = 0;
566     address_t *a;
567
568     for (; alias_queries; alias_queries = alias_queries->next) {
569       if ((a = address_list_dup(alias_lookup(alias_queries->data)))) {
570         /* output in machine-readable form */
571         mutt_addrlist_to_idna (a, NULL);
572         mutt_write_address_list (a, stdout, 0, 0);
573         address_list_wipe(&a);
574       } else {
575         rv = 1;
576         printf ("%s\n", alias_queries->data);
577       }
578     }
579     return rv;
580   }
581
582   if (newMagic)
583     mx_set_magic (newMagic);
584
585   if (!option (OPTNOCURSES)) {
586     SETCOLOR (MT_COLOR_NORMAL);
587     clear ();
588     mutt_error = mutt_curses_error;
589     mutt_message = mutt_curses_message;
590   }
591
592   /* Create the Maildir directory if it doesn't exist. */
593   if (!option (OPTNOCURSES) && Maildir) {
594     struct stat sb;
595     char fpath[_POSIX_PATH_MAX];
596     char mesg[STRING];
597
598     m_strcpy(fpath, sizeof(fpath), Maildir);
599     mutt_expand_path (fpath, sizeof (fpath));
600     /* we're not connected yet - skip mail folder creation */
601     if (mx_get_magic (fpath) != M_IMAP)
602       if (stat (fpath, &sb) == -1 && errno == ENOENT) {
603         snprintf (mesg, sizeof (mesg), _("%s does not exist. Create it?"),
604                   Maildir);
605         if (mutt_yesorno (mesg, M_YES) == M_YES) {
606           if (mkdir (fpath, 0700) == -1 && errno != EEXIST)
607             mutt_error (_("Can't create %s: %s."), Maildir, strerror (errno));
608         }
609       }
610   }
611
612   if (sendflags & SENDPOSTPONED) {
613     if (!option (OPTNOCURSES))
614       mutt_flushinp ();
615     ci_send_message (SENDPOSTPONED, NULL, NULL, NULL, NULL);
616     mutt_endwin (NULL);
617   }
618   else if (subject || msg || sendflags || draftFile || includeFile || attach
619            || optind < argc) {
620     FILE *fin = NULL;
621     char buf[LONG_STRING];
622     char *tempfile = NULL, *infile = NULL;
623     char *bodytext = NULL;
624     FILE *fout;
625
626     if (!option (OPTNOCURSES))
627       mutt_flushinp ();
628
629     if (!msg)
630       msg = header_new();
631
632     if (draftFile)
633       infile = draftFile;
634     else {
635       if (!msg->env)
636         msg->env = envelope_new();
637
638       for (i = optind; i < argc; i++) {
639         if (url_check_scheme (argv[i]) == U_MAILTO)
640           url_parse_mailto (msg->env, &bodytext, argv[i]);
641         else
642           msg->env->to = rfc822_parse_adrlist (msg->env->to, argv[i]);
643       }
644
645       if (option (OPTAUTOEDIT) && !msg->env->to && !msg->env->cc) {
646         if (!option (OPTNOCURSES))
647           mutt_endwin (NULL);
648         fputs (_("No recipients specified.\n"), stderr);
649         exit (1);
650       }
651
652       if (subject)
653         msg->env->subject = m_strdup(subject);
654
655       if (includeFile)
656         infile = includeFile;
657     }
658
659     if (infile || bodytext) {
660       if (infile) {
661         if (m_strcmp("-", infile) == 0)
662           fin = stdin;
663         else {
664           char path[_POSIX_PATH_MAX];
665
666           m_strcpy(path, sizeof(path), infile);
667           mutt_expand_path (path, sizeof (path));
668           if ((fin = fopen (path, "r")) == NULL) {
669             if (!option (OPTNOCURSES))
670               mutt_endwin (NULL);
671             perror (path);
672             exit (1);
673           }
674         }
675       }
676       else
677         fin = NULL;
678
679       if (draftFile)
680         msg->env = mutt_read_rfc822_header (fin, NULL, 1, 0);
681
682       /* is the following if still needed? */
683
684       fout = m_tempfile(buf, sizeof(buf), NONULL(MCore.tmpdir), NULL);
685       tempfile = m_strdup(buf);
686
687       if (tempfile) {
688         if (!fout) {
689           if (!option (OPTNOCURSES))
690             mutt_endwin (NULL);
691           perror (tempfile);
692           m_fclose(&fin);
693           p_delete(&tempfile);
694           exit (1);
695         }
696         if (fin)
697           mutt_copy_stream (fin, fout);
698         else if (bodytext)
699           fputs (bodytext, fout);
700         m_fclose(&fout);
701         if (fin && fin != stdin)
702           m_fclose(&fin);
703       }
704     }
705
706     p_delete(&bodytext);
707
708     if (attach) {
709       string_list_t *t = attach;
710       BODY *a = NULL;
711
712       while (t) {
713         if (a) {
714           a->next = mutt_make_file_attach (t->data);
715           a = a->next;
716         }
717         else
718           msg->content = a = mutt_make_file_attach (t->data);
719         if (!a) {
720           if (!option (OPTNOCURSES))
721             mutt_endwin (NULL);
722           fprintf (stderr, _("%s: unable to attach file.\n"), t->data);
723           string_list_wipe(&attach);
724           exit (1);
725         }
726         t = t->next;
727       }
728       string_list_wipe(&attach);
729     }
730
731     ci_send_message (sendflags, msg, tempfile, NULL, NULL);
732
733     if (!option (OPTNOCURSES))
734       mutt_endwin (NULL);
735   }
736   else {
737     if (flags & M_BUFFY) {
738       if (!buffy_check (0)) {
739         mutt_endwin _("No mailbox with new mail.");
740
741         exit (1);
742       }
743       folder[0] = 0;
744       buffy_next (folder, sizeof (folder));
745     }
746     else if (flags & M_SELECT) {
747 #ifdef USE_NNTP
748       if (flags & M_NEWS) {
749         set_option (OPTNEWS);
750         if (!(CurrentNewsSrv = mutt_select_newsserver (NewsServer))) {
751           mutt_endwin (Errorbuf);
752           exit (1);
753         }
754       } else
755 #endif
756       if (!Incoming.len) {
757         mutt_endwin _("No incoming mailboxes defined.");
758
759         exit (1);
760       }
761       folder[0] = 0;
762       mutt_select_file (folder, sizeof (folder), M_SEL_FOLDER | M_SEL_BUFFY);
763       if (!folder[0]) {
764         mutt_endwin (NULL);
765         exit (0);
766       }
767     }
768
769     if (!folder[0])
770       m_strcpy(folder, sizeof(folder), NONULL(Spoolfile));
771
772 #ifdef USE_NNTP
773     if (option (OPTNEWS)) {
774       unset_option (OPTNEWS);
775       nntp_expand_path (folder, sizeof (folder),
776                         &CurrentNewsSrv->conn->account);
777     } else
778 #endif
779       mutt_expand_path (folder, sizeof (folder));
780
781     m_strreplace(&CurrentFolder, folder);
782     m_strreplace(&LastFolder, folder);
783
784     if (flags & M_IGNORE) {
785       /* check to see if there are any messages in the folder */
786       switch (mx_check_empty (folder)) {
787       case -1:
788         mutt_endwin (strerror (errno));
789         exit (1);
790       case 1:
791         mutt_endwin _("Mailbox is empty.");
792         exit (1);
793       }
794     }
795
796     mutt_folder_hook (folder);
797
798     if ((Context = mx_open_mailbox (folder, ((flags & M_RO)
799                                              || option (OPTREADONLY)) ?
800                                     M_READONLY : 0, NULL))
801         || !explicit_folder) {
802       mutt_index_menu ();
803       if (option (OPTXTERMSETTITLES))
804         printf("\033]2;%s\007", NONULL(XtermLeave));
805       if (Context)
806         p_delete(&Context);
807     }
808     mutt_endwin (Errorbuf);
809   }
810
811   luaM_shutdown();
812   exit (0);
813 }