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