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