Andreas Krennmair:
[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_SASL
263         "+USE_SASL  "
264 #else
265         "-USE_SASL  "
266 #endif
267 #ifdef USE_SASL2
268         "+USE_SASL2  "
269 #else
270         "-USE_SASL2  "
271 #endif
272
273 #ifdef USE_LIBESMTP
274     "+USE_LIBESMTP  "
275 #else
276     "-USE_LIBESMTP  "
277 #endif
278         "\n"
279         
280 #ifdef HAVE_REGCOMP
281         "+HAVE_REGCOMP  "
282 #else
283         "-HAVE_REGCOMP  "
284 #endif
285
286 #ifdef USE_GNU_REGEX
287         "+USE_GNU_REGEX  "
288 #else
289         "-USE_GNU_REGEX  "
290 #endif
291
292 #ifdef USE_COMPRESSED
293         "+COMPRESSED  "
294 #else
295         "-COMPRESSED  "
296 #endif
297
298         "\n"
299         
300 #ifdef HAVE_COLOR
301         "+HAVE_COLOR  "
302 #else
303         "-HAVE_COLOR  "
304 #endif
305         
306 #ifdef HAVE_START_COLOR
307         "+HAVE_START_COLOR  "
308 #else
309         "-HAVE_START_COLOR  "
310 #endif
311         
312 #ifdef HAVE_TYPEAHEAD
313         "+HAVE_TYPEAHEAD  "
314 #else
315         "-HAVE_TYPEAHEAD  "
316 #endif
317         
318 #ifdef HAVE_BKGDSET
319         "+HAVE_BKGDSET  "
320 #else
321         "-HAVE_BKGDSET  "
322 #endif
323
324         "\n"
325         
326 #ifdef HAVE_CURS_SET
327         "+HAVE_CURS_SET  "
328 #else
329         "-HAVE_CURS_SET  "
330 #endif
331         
332 #ifdef HAVE_META
333         "+HAVE_META  "
334 #else
335         "-HAVE_META  "
336 #endif
337         
338 #ifdef HAVE_RESIZETERM
339         "+HAVE_RESIZETERM  "
340 #else
341         "-HAVE_RESIZETERM  "
342 #endif
343         );      
344   
345   puts (
346 #ifdef CRYPT_BACKEND_CLASSIC_PGP
347         "+CRYPT_BACKEND_CLASSIC_PGP  "
348 #else
349         "-CRYPT_BACKEND_CLASSIC_PGP  "
350 #endif
351 #ifdef CRYPT_BACKEND_CLASSIC_SMIME
352         "+CRYPT_BACKEND_CLASSIC_SMIME  "
353 #else
354         "-CRYPT_BACKEND_CLASSIC_SMIME  "
355 #endif
356 #ifdef CRYPT_BACKEND_GPGME
357         "+CRYPT_BACKEND_GPGME  "
358 #else
359         "-CRYPT_BACKEND_GPGME  "
360 #endif
361
362 #ifdef BUFFY_SIZE
363         "+BUFFY_SIZE "
364 #else
365         "-BUFFY_SIZE "
366 #endif
367 #ifdef EXACT_ADDRESS
368         "+EXACT_ADDRESS  "
369 #else
370         "-EXACT_ADDRESS  "
371 #endif
372
373 #ifdef SUN_ATTACHMENT
374         "+SUN_ATTACHMENT  "
375 #else
376         "-SUN_ATTACHMENT  "
377 #endif
378
379         "\n"
380         
381 #ifdef ENABLE_NLS
382         "+ENABLE_NLS  "
383 #else
384         "-ENABLE_NLS  "
385 #endif
386
387 #ifdef LOCALES_HACK
388         "+LOCALES_HACK  "
389 #else
390         "-LOCALES_HACK  "
391 #endif
392               
393 #ifdef HAVE_WC_FUNCS
394         "+HAVE_WC_FUNCS  "
395 #else
396         "-HAVE_WC_FUNCS  "
397 #endif
398         
399 #ifdef HAVE_LANGINFO_CODESET
400         "+HAVE_LANGINFO_CODESET  "
401 #else
402         "-HAVE_LANGINFO_CODESET  "
403 #endif
404
405         
406 #ifdef HAVE_LANGINFO_YESEXPR
407         "+HAVE_LANGINFO_YESEXPR  "
408 #else
409         "-HAVE_LANGINFO_YESEXPR  "
410 #endif
411         
412         "\n"
413
414 #if HAVE_ICONV
415         "+HAVE_ICONV  "
416 #else
417         "-HAVE_ICONV  "
418 #endif
419
420 #if ICONV_NONTRANS
421         "+ICONV_NONTRANS  "
422 #else
423         "-ICONV_NONTRANS  "
424 #endif
425
426 #if HAVE_LIBIDN
427         "+HAVE_LIBIDN  "
428 #else
429         "-HAVE_LIBIDN  "
430 #endif
431         
432 #if HAVE_GETSID
433         "+HAVE_GETSID  "
434 #else
435         "-HAVE_GETSID  "
436 #endif
437
438 #if HAVE_GETADDRINFO
439         "+HAVE_GETADDRINFO  "
440 #else
441         "-HAVE_GETADDRINFO  "
442 #endif
443
444         );
445
446 #ifdef ISPELL
447   printf ("ISPELL=\"%s\"\n", ISPELL);
448 #else
449   puts ("-ISPELL");
450 #endif
451
452   printf ("SENDMAIL=\"%s\"\n", SENDMAIL);
453   printf ("MAILPATH=\"%s\"\n", MAILPATH);
454   printf ("PKGDATADIR=\"%s\"\n", PKGDATADIR);
455   printf ("SYSCONFDIR=\"%s\"\n", SYSCONFDIR);
456   printf ("EXECSHELL=\"%s\"\n", EXECSHELL);
457 #ifdef MIXMASTER
458   printf ("MIXMASTER=\"%s\"\n", MIXMASTER);
459 #else
460   puts ("-MIXMASTER");
461 #endif
462
463   puts(_(ReachingUs));
464
465   mutt_print_patchlist();
466   
467   exit (0);
468 }
469
470 static void start_curses (void)
471 {
472   km_init (); /* must come before mutt_init */
473
474 #ifdef USE_SLANG_CURSES
475   SLtt_Ignore_Beep = 1; /* don't do that #*$@^! annoying visual beep! */
476   SLsmg_Display_Eight_Bit = 128; /* characters above this are printable */
477 #else
478   /* should come before initscr() so that ncurses 4.2 doesn't try to install
479      its own SIGWINCH handler */
480   mutt_signal_init ();
481 #endif
482   if (initscr () == NULL)
483   {
484     puts _("Error initializing terminal.");
485     exit (1);
486   }
487 #if 1 /* USE_SLANG_CURSES  - commenting out suggested in #455. */
488   /* slang requires the signal handlers to be set after initializing */
489   mutt_signal_init ();
490 #endif
491   ci_start_color ();
492   keypad (stdscr, TRUE);
493   cbreak ();
494   noecho ();
495 #if HAVE_TYPEAHEAD
496   typeahead (-1);       /* simulate smooth scrolling */
497 #endif
498 #if HAVE_META
499   meta (stdscr, TRUE);
500 #endif
501 }
502
503 #define M_IGNORE  (1<<0)        /* -z */
504 #define M_BUFFY   (1<<1)        /* -Z */
505 #define M_NOSYSRC (1<<2)        /* -n */
506 #define M_RO      (1<<3)        /* -R */
507 #define M_SELECT  (1<<4)        /* -y */
508 #ifdef USE_NNTP
509 #define M_NEWS    (1<<5)        /* -g and -G */
510 #endif
511
512 int main (int argc, char **argv)
513 {
514   char folder[_POSIX_PATH_MAX] = "";
515   char *subject = NULL;
516   char *includeFile = NULL;
517   char *draftFile = NULL;
518   char *newMagic = NULL;
519   HEADER *msg = NULL;
520   LIST *attach = NULL;
521   LIST *commands = NULL;
522   LIST *queries = NULL;
523   LIST *alias_queries = NULL;
524   int sendflags = 0;
525   int flags = 0;
526   int version = 0;
527   int i;
528   int explicit_folder = 0;
529   extern char *optarg;
530   extern int optind;
531
532   /* sanity check against stupid administrators */
533   
534   if(getegid() != getgid())
535   {
536     fprintf(stderr, "%s: I don't want to run with privileges!\n",
537             argv[0]);
538     exit(1);
539   }
540
541 #ifdef ENABLE_NLS
542   /* FIXME what about init.c:1439 ? */
543   setlocale (LC_ALL, "");
544   bindtextdomain (PACKAGE, MUTTLOCALEDIR);
545   textdomain (PACKAGE);
546 #endif
547
548   setlocale (LC_CTYPE, "");
549
550   mutt_error = mutt_nocurses_error;
551   mutt_message = mutt_nocurses_error;
552   SRAND (time (NULL));
553   umask (077);
554
555   memset (Options, 0, sizeof (Options));
556   memset (QuadOptions, 0, sizeof (QuadOptions));
557   
558 #ifdef USE_NNTP
559   while ((i = getopt (argc, argv, "A:a:b:F:f:c:d:e:g:GH:s:i:hm:npQ:RvxyzZ")) != EOF)
560 #else
561   while ((i = getopt (argc, argv, "A:a:b:F:f:c:d:e:H:s:i:hm:npQ:RvxyzZ")) != EOF)
562 #endif
563     switch (i)
564     {
565       case 'A':
566         alias_queries = mutt_add_list (alias_queries, optarg);
567         break;
568       case 'a':
569         attach = mutt_add_list (attach, optarg);
570         break;
571
572       case 'F':
573         mutt_str_replace (&Muttrc, optarg);
574         break;
575
576       case 'f':
577         strfcpy (folder, optarg, sizeof (folder));
578         explicit_folder = 1;
579         break;
580
581       case 'b':
582       case 'c':
583         if (!msg)
584           msg = mutt_new_header ();
585         if (!msg->env)
586           msg->env = mutt_new_envelope ();
587         if (i == 'b')
588           msg->env->bcc = rfc822_parse_adrlist (msg->env->bcc, optarg);
589         else
590           msg->env->cc = rfc822_parse_adrlist (msg->env->cc, optarg);
591         break;
592
593       case 'd':
594 #ifdef DEBUG
595         debuglevel = atoi (optarg);
596         printf (_("Debugging at level %d.\n"), debuglevel);
597 #else
598         printf _("DEBUG was not defined during compilation.  Ignored.\n");
599 #endif
600         break;
601
602       case 'e':
603         commands = mutt_add_list (commands, optarg);
604         break;
605
606       case 'H':
607         draftFile = optarg;
608         break;
609
610       case 'i':
611         includeFile = optarg;
612         break;
613
614       case 'm':
615         /* should take precedence over .muttrc setting, so save it for later */
616         newMagic = optarg; 
617         break;
618
619       case 'n':
620         flags |= M_NOSYSRC;
621         break;
622
623       case 'p':
624         sendflags |= SENDPOSTPONED;
625         break;
626
627       case 'Q':
628         queries = mutt_add_list (queries, optarg);
629         break;
630       
631       case 'R':
632         flags |= M_RO; /* read-only mode */
633         break;
634
635       case 's':
636         subject = optarg;
637         break;
638
639       case 'v':
640         version++;
641         break;
642
643       case 'x': /* mailx compatible send mode */
644         sendflags |= SENDMAILX;
645         break;
646
647       case 'y': /* My special hack mode */
648         flags |= M_SELECT;
649         break;
650
651 #ifdef USE_NNTP
652       case 'g': /* Specify a newsserver */
653         {
654           char buf[LONG_STRING];
655
656           snprintf (buf, sizeof (buf), "set news_server=%s", optarg);
657           commands = mutt_add_list (commands, buf);
658         }
659
660       case 'G': /* List of newsgroups */
661         flags |= M_SELECT | M_NEWS;
662         break;
663 #endif
664
665       case 'z':
666         flags |= M_IGNORE;
667         break;
668
669       case 'Z':
670         flags |= M_BUFFY | M_IGNORE;
671         break;
672
673       default:
674         mutt_usage ();
675     }
676
677   switch (version)
678   {
679     case 0:
680       break;
681     case 1:
682       show_version ();
683       break;
684     default:
685       puts (mutt_make_version ());
686       puts (_(Copyright));
687       puts (_(ReachingUs));
688       exit (0);
689   }
690
691   /* Check for a batch send. */
692   if (!isatty (0) || queries || alias_queries)
693   {
694     set_option (OPTNOCURSES);
695     sendflags = SENDBATCH;
696   }
697
698   /* This must come before mutt_init() because curses needs to be started
699      before calling the init_pair() function to set the color scheme.  */
700   if (!option (OPTNOCURSES))
701     start_curses ();
702
703   /* set defaults and read init files */
704   mutt_init (flags & M_NOSYSRC, commands);
705   mutt_free_list (&commands);
706
707   if (queries)
708     return mutt_query_variables (queries);
709
710   if (alias_queries)
711   {
712     int rv = 0;
713     ADDRESS *a;
714     for (; alias_queries; alias_queries = alias_queries->next)
715     {
716       if ((a = mutt_lookup_alias (alias_queries->data)))
717       { 
718         /* output in machine-readable form */
719         mutt_addrlist_to_idna (a, NULL);
720         mutt_write_address_list (a, stdout, 0, 0);
721       }
722       else
723       {
724         rv = 1;
725         printf ("%s\n", alias_queries->data);
726       }
727     }
728     return rv;
729   }
730   
731   if (newMagic)
732     mx_set_magic (newMagic);
733
734   if (!option (OPTNOCURSES))
735   {
736     SETCOLOR (MT_COLOR_NORMAL);
737     clear ();
738     mutt_error = mutt_curses_error;
739     mutt_message = mutt_curses_message;
740   }
741
742   /* Create the Maildir directory if it doesn't exist. */
743   if (!option (OPTNOCURSES) && Maildir)
744   {
745     struct stat sb;
746     char fpath[_POSIX_PATH_MAX];
747     char msg[STRING];
748
749     strfcpy (fpath, Maildir, sizeof (fpath));
750     mutt_expand_path (fpath, sizeof (fpath));
751 #ifdef USE_IMAP
752     /* we're not connected yet - skip mail folder creation */
753     if (!mx_is_imap (fpath))
754 #endif
755     if (stat (fpath, &sb) == -1 && errno == ENOENT)
756     {
757       snprintf (msg, sizeof (msg), _("%s does not exist. Create it?"), Maildir);
758       if (mutt_yesorno (msg, M_YES) == M_YES)
759       {
760         if (mkdir (fpath, 0700) == -1 && errno != EEXIST)
761           mutt_error ( _("Can't create %s: %s."), Maildir, strerror (errno));
762       }
763     }
764   }
765
766   if (sendflags & SENDPOSTPONED)
767   {
768     if (!option (OPTNOCURSES))
769       mutt_flushinp ();
770     ci_send_message (SENDPOSTPONED, NULL, NULL, NULL, NULL);
771     mutt_endwin (NULL);
772   }
773   else if (subject || msg || sendflags || draftFile || includeFile || attach ||
774            optind < argc)
775   {
776     FILE *fin = NULL;
777     char buf[LONG_STRING];
778     char *tempfile = NULL, *infile = NULL;
779     char *bodytext = NULL;
780     
781     if (!option (OPTNOCURSES))
782       mutt_flushinp ();
783
784     if (!msg)
785       msg = mutt_new_header ();
786
787     if (draftFile)
788       infile = draftFile;
789     else
790     {
791       if (!msg->env)
792         msg->env = mutt_new_envelope ();
793
794       for (i = optind; i < argc; i++)
795       {
796         if (url_check_scheme (argv[i]) == U_MAILTO)
797           url_parse_mailto (msg->env, &bodytext, argv[i]);
798         else
799           msg->env->to = rfc822_parse_adrlist (msg->env->to, argv[i]);
800       }
801
802       if (option (OPTAUTOEDIT) && !msg->env->to && !msg->env->cc)
803       {
804         if (!option (OPTNOCURSES))
805           mutt_endwin (NULL);
806         fputs (_("No recipients specified.\n"), stderr);
807         exit (1);
808       }
809
810       if (subject)
811         msg->env->subject = safe_strdup (subject);
812
813       if (includeFile)
814         infile = includeFile;
815     }
816
817     if (infile || bodytext)
818     {
819       if (infile)
820       {
821         if (mutt_strcmp ("-", infile) == 0)
822           fin = stdin;
823         else 
824         {
825           char path[_POSIX_PATH_MAX];
826           
827           strfcpy (path, infile, sizeof (path));
828           mutt_expand_path (path, sizeof (path));
829           if ((fin = fopen (path, "r")) == NULL)
830           {
831             if (!option (OPTNOCURSES))
832               mutt_endwin (NULL);
833             perror (path);
834             exit (1);
835           }
836         }
837       }
838       else
839         fin = NULL;
840
841       mutt_mktemp (buf);
842       tempfile = safe_strdup (buf);
843
844       if (draftFile)
845         msg->env = mutt_read_rfc822_header (fin, NULL, 1, 0);
846
847       /* is the following if still needed? */
848       
849       if (tempfile)
850       {
851         FILE *fout;
852
853         if ((fout = safe_fopen (tempfile, "w")) == NULL)
854         {
855           if (!option (OPTNOCURSES))
856             mutt_endwin (NULL);
857           perror (tempfile);
858           fclose (fin);
859           FREE (&tempfile);
860           exit (1);
861         }
862         if (fin)
863           mutt_copy_stream (fin, fout);
864         else if (bodytext)
865           fputs (bodytext, fout);
866         fclose (fout);
867         if (fin && fin != stdin)
868           fclose (fin);
869       }
870     }
871
872     FREE (&bodytext);
873     
874     if (attach)
875     {
876       LIST *t = attach;
877       BODY *a = NULL;
878
879       while (t)
880       {
881         if (a)
882         {
883           a->next = mutt_make_file_attach (t->data);
884           a = a->next;
885         }
886         else
887           msg->content = a = mutt_make_file_attach (t->data);
888         if (!a)
889         {
890           if (!option (OPTNOCURSES))
891             mutt_endwin (NULL);
892           fprintf (stderr, _("%s: unable to attach file.\n"), t->data);
893           mutt_free_list (&attach);
894           exit (1);
895         }
896         t = t->next;
897       }
898       mutt_free_list (&attach);
899     }
900
901     ci_send_message (sendflags, msg, tempfile, NULL, NULL);
902
903     if (!option (OPTNOCURSES))
904       mutt_endwin (NULL);
905   }
906   else
907   {
908     if (flags & M_BUFFY)
909     {
910       if (!mutt_buffy_check (0))
911       {
912         mutt_endwin _("No mailbox with new mail.");
913         exit (1);
914       }
915       folder[0] = 0;
916       mutt_buffy (folder, sizeof (folder));
917     }
918     else if (flags & M_SELECT)
919     {
920 #ifdef USE_NNTP
921       if (flags & M_NEWS)
922       {
923         set_option (OPTNEWS);
924         if(!(CurrentNewsSrv = mutt_select_newsserver (NewsServer)))
925         {
926           mutt_endwin (Errorbuf);
927           exit (1);
928         }
929       }
930       else
931 #endif
932       if (!Incoming) {
933         mutt_endwin _("No incoming mailboxes defined.");
934         exit (1);
935       }
936       folder[0] = 0;
937       mutt_select_file (folder, sizeof (folder), M_SEL_FOLDER | M_SEL_BUFFY);
938       if (!folder[0])
939       {
940         mutt_endwin (NULL);
941         exit (0);
942       }
943     }
944
945     if (!folder[0])
946       strfcpy (folder, NONULL(Spoolfile), sizeof (folder));
947
948 #ifdef USE_NNTP
949     if (option (OPTNEWS))
950     {
951       unset_option (OPTNEWS);
952       nntp_expand_path (folder, sizeof (folder), &CurrentNewsSrv->conn->account);
953     }
954     else
955 #endif
956     mutt_expand_path (folder, sizeof (folder));
957
958     mutt_str_replace (&LastFolder, folder);
959
960     if (flags & M_IGNORE)
961     {
962       /* check to see if there are any messages in the folder */
963       switch (mx_check_empty (folder))
964       {
965         case -1:
966           mutt_endwin (strerror (errno));
967           exit (1);
968         case 1:
969           mutt_endwin _("Mailbox is empty.");
970           exit (1);
971       }
972     }
973
974     mutt_folder_hook (folder);
975
976     if((Context = mx_open_mailbox (folder, ((flags & M_RO) || option (OPTREADONLY)) ? M_READONLY : 0, NULL))
977        || !explicit_folder)
978     {
979       mutt_index_menu ();
980       if (Context)
981         FREE (&Context);
982     }
983     mutt_endwin (Errorbuf);
984   }
985
986   exit (0);
987 }