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