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