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