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