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