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