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