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