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