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