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