2 * Copyright (C) 1996-2007 Michael R. Elkins <me@mutt.org>
3 * Copyright (C) 1999-2007 Thomas Roessler <roessler@does-not-exist.org>
4 * Copyright (C) 2004 g10 Code GmbH
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
28 #include "mutt_curses.h"
32 #include "mutt_crypt.h"
33 #include "mutt_idna.h"
36 #include "mutt_sasl.h"
40 #include "imap/imap.h"
53 #include <sys/utsname.h>
60 #include <stringprep.h>
63 static const char *ReachingUs = N_("\
64 To contact the developers, please mail to <mutt-dev@mutt.org>.\n\
65 To report a bug, please visit http://bugs.mutt.org/.\n");
67 static const char *Notice = N_("\
68 Copyright (C) 1996-2009 Michael R. Elkins and others.\n\
69 Mutt comes with ABSOLUTELY NO WARRANTY; for details type `mutt -vv'.\n\
70 Mutt is free software, and you are welcome to redistribute it\n\
71 under certain conditions; type `mutt -vv' for details.\n");
73 static const char *Copyright = N_("\
74 Copyright (C) 1996-2007 Michael R. Elkins <me@mutt.org>\n\
75 Copyright (C) 1996-2002 Brandon Long <blong@fiction.net>\n\
76 Copyright (C) 1997-2008 Thomas Roessler <roessler@does-not-exist.org>\n\
77 Copyright (C) 1998-2005 Werner Koch <wk@isil.d.shuttle.de>\n\
78 Copyright (C) 1999-2009 Brendan Cully <brendan@kublai.com>\n\
79 Copyright (C) 1999-2002 Tommi Komulainen <Tommi.Komulainen@iki.fi>\n\
80 Copyright (C) 2000-2002 Edmund Grimley Evans <edmundo@rano.org>\n\
81 Copyright (C) 2006-2008 Rocco Rutte <pdmef@gmx.net>\n\
83 Many others not mentioned here contributed code, fixes,\n\
86 static const char *Licence = N_("\
87 This program is free software; you can redistribute it and/or modify\n\
88 it under the terms of the GNU General Public License as published by\n\
89 the Free Software Foundation; either version 2 of the License, or\n\
90 (at your option) any later version.\n\
92 This program is distributed in the hope that it will be useful,\n\
93 but WITHOUT ANY WARRANTY; without even the implied warranty of\n\
94 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n\
95 GNU General Public License for more details.\n");
96 static const char *Obtaining = N_("\
97 You should have received a copy of the GNU General Public License\n\
98 along with this program; if not, write to the Free Software\n\
99 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.\n\
102 void mutt_exit (int code)
108 static void mutt_usage (void)
110 puts (mutt_make_version ());
113 "usage: mutt [<options>] [-z] [-f <file> | -yZ]\n\
114 mutt [<options>] [-x] [-Hi <file>] [-s <subj>] [-bc <addr>] [-a <file> [...]] [--] <addr> [...]\n\
115 mutt [<options>] [-x] [-s <subj>] [-bc <addr>] [-a <file> [...]] [--] <addr> [...] < message\n\
116 mutt [<options>] -p\n\
117 mutt [<options>] -A <alias> [...]\n\
118 mutt [<options>] -Q <query> [...]\n\
119 mutt [<options>] -D\n\
124 -A <alias>\texpand the given alias\n\
125 -a <file>\tattach a file to the message\n\
126 -b <address>\tspecify a blind carbon-copy (BCC) address\n\
127 -c <address>\tspecify a carbon-copy (CC) address\n\
128 -D\t\tprint the value of all variables to stdout");
130 puts _(" -d <level>\tlog debugging output to ~/.muttdebug0");
133 " -e <command>\tspecify a command to be executed after initialization\n\
134 -f <file>\tspecify which mailbox to read\n\
135 -F <file>\tspecify an alternate muttrc file\n\
136 -H <file>\tspecify a draft file to read header and body from\n\
137 -i <file>\tspecify a file which Mutt should include in the body\n\
138 -m <type>\tspecify a default mailbox type\n\
139 -n\t\tcauses Mutt not to read the system Muttrc\n\
140 -p\t\trecall a postponed message");
143 -Q <variable>\tquery a configuration variable\n\
144 -R\t\topen mailbox in read-only mode\n\
145 -s <subj>\tspecify a subject (must be in quotes if it has spaces)\n\
146 -v\t\tshow version and compile-time definitions\n\
147 -x\t\tsimulate the mailx send mode\n\
148 -y\t\tselect a mailbox specified in your `mailboxes' list\n\
149 -z\t\texit immediately if there are no messages in the mailbox\n\
150 -Z\t\topen the first folder with new message, exit immediately if none\n\
151 -h\t\tthis help message");
152 puts _(" --\t\ttreat remaining arguments as addr even if starting with a dash\n\
153 \t\twhen using -a with multiple filenames using -- is mandatory");
158 static void show_version (void)
162 puts (mutt_make_version());
168 printf ("System: %s %s.%s", uts.sysname, uts.version, uts.release);
170 printf ("System: SCO %s", uts.release);
172 printf ("System: %s %s", uts.sysname, uts.release);
175 printf (" (%s)", uts.machine);
177 #ifdef NCURSES_VERSION
178 printf ("\nncurses: %s (compiled with %s)", curses_version(), NCURSES_VERSION);
179 #elif defined(USE_SLANG_CURSES)
180 printf ("\nslang: %d", SLANG_VERSION);
183 #ifdef _LIBICONV_VERSION
184 printf ("\nlibiconv: %d.%d", _LIBICONV_VERSION >> 8,
185 _LIBICONV_VERSION & 0xff);
189 printf ("\nlibidn: %s (compiled with %s)", stringprep_check_version (NULL),
194 printf ("\nhcache backend: %s", mutt_hcache_backend ());
197 puts (_("\nCompile options:"));
200 printf ("DOMAIN=\"%s\"\n", DOMAIN);
271 #ifdef USE_SSL_OPENSSL
277 #ifdef USE_SSL_GNUTLS
322 #ifdef HAVE_START_COLOR
328 #ifdef HAVE_TYPEAHEAD
354 #ifdef HAVE_RESIZETERM
362 #ifdef CRYPT_BACKEND_CLASSIC_PGP
363 "+CRYPT_BACKEND_CLASSIC_PGP "
365 "-CRYPT_BACKEND_CLASSIC_PGP "
367 #ifdef CRYPT_BACKEND_CLASSIC_SMIME
368 "+CRYPT_BACKEND_CLASSIC_SMIME "
370 "-CRYPT_BACKEND_CLASSIC_SMIME "
372 #ifdef CRYPT_BACKEND_GPGME
373 "+CRYPT_BACKEND_GPGME "
375 "-CRYPT_BACKEND_GPGME "
386 #ifdef SUN_ATTACHMENT
412 #ifdef HAVE_LANGINFO_CODESET
413 "+HAVE_LANGINFO_CODESET "
415 "-HAVE_LANGINFO_CODESET "
419 #ifdef HAVE_LANGINFO_YESEXPR
420 "+HAVE_LANGINFO_YESEXPR "
422 "-HAVE_LANGINFO_YESEXPR "
460 printf ("ISPELL=\"%s\"\n", ISPELL);
465 printf ("SENDMAIL=\"%s\"\n", SENDMAIL);
466 printf ("MAILPATH=\"%s\"\n", MAILPATH);
467 printf ("PKGDATADIR=\"%s\"\n", PKGDATADIR);
468 printf ("SYSCONFDIR=\"%s\"\n", SYSCONFDIR);
469 printf ("EXECSHELL=\"%s\"\n", EXECSHELL);
471 printf ("MIXMASTER=\"%s\"\n", MIXMASTER);
478 mutt_print_patchlist();
483 static void start_curses (void)
485 km_init (); /* must come before mutt_init */
487 #ifdef USE_SLANG_CURSES
488 SLtt_Ignore_Beep = 1; /* don't do that #*$@^! annoying visual beep! */
489 SLsmg_Display_Eight_Bit = 128; /* characters above this are printable */
490 SLtt_set_color(0, NULL, "default", "default");
491 #if SLANG_VERSION >= 20000
495 /* should come before initscr() so that ncurses 4.2 doesn't try to install
496 its own SIGWINCH handler */
499 if (initscr () == NULL)
501 puts _("Error initializing terminal.");
504 #if 1 /* USE_SLANG_CURSES - commenting out suggested in #455. */
505 /* slang requires the signal handlers to be set after initializing */
509 keypad (stdscr, TRUE);
513 typeahead (-1); /* simulate smooth scrolling */
520 #define M_IGNORE (1<<0) /* -z */
521 #define M_BUFFY (1<<1) /* -Z */
522 #define M_NOSYSRC (1<<2) /* -n */
523 #define M_RO (1<<3) /* -R */
524 #define M_SELECT (1<<4) /* -y */
526 int main (int argc, char **argv)
528 char folder[_POSIX_PATH_MAX] = "";
529 char *subject = NULL;
530 char *includeFile = NULL;
531 char *draftFile = NULL;
532 char *newMagic = NULL;
535 LIST *commands = NULL;
536 LIST *queries = NULL;
537 LIST *alias_queries = NULL;
542 int explicit_folder = 0;
543 int dump_variables = 0;
546 int double_dash = argc, nargc = 1;
548 /* sanity check against stupid administrators */
550 if(getegid() != getgid())
552 fprintf(stderr, "%s: I don't want to run with privileges!\n",
558 /* FIXME what about init.c:1439 ? */
559 setlocale (LC_ALL, "");
560 bindtextdomain (PACKAGE, MUTTLOCALEDIR);
561 textdomain (PACKAGE);
564 setlocale (LC_CTYPE, "");
566 mutt_error = mutt_nocurses_error;
567 mutt_message = mutt_nocurses_error;
571 memset (Options, 0, sizeof (Options));
572 memset (QuadOptions, 0, sizeof (QuadOptions));
574 for (optind = 1; optind < double_dash; )
576 /* We're getopt'ing POSIXLY, so we'll be here every time getopt()
577 * encounters a non-option. That could be a file to attach
578 * (all non-options between -a and --) or it could be an address
579 * (which gets collapsed to the front of argv).
581 for (; optind < argc; optind++)
583 if (argv[optind][0] == '-' && argv[optind][1] != '\0')
585 if (argv[optind][1] == '-' && argv[optind][2] == '\0')
586 double_dash = optind; /* quit outer loop after getopt */
587 break; /* drop through to getopt */
590 /* non-option, either an attachment or address */
592 attach = mutt_add_list (attach, argv[optind]);
594 argv[nargc++] = argv[optind];
597 if ((i = getopt (argc, argv, "+A:a:b:F:f:c:Dd:e:H:s:i:hm:npQ:RvxyzZ")) != EOF)
601 alias_queries = mutt_add_list (alias_queries, optarg);
604 attach = mutt_add_list (attach, optarg);
608 mutt_str_replace (&Muttrc, optarg);
612 strfcpy (folder, optarg, sizeof (folder));
619 msg = mutt_new_header ();
621 msg->env = mutt_new_envelope ();
623 msg->env->bcc = rfc822_parse_adrlist (msg->env->bcc, optarg);
625 msg->env->cc = rfc822_parse_adrlist (msg->env->cc, optarg);
634 debuglevel = atoi (optarg);
635 printf (_("Debugging at level %d.\n"), debuglevel);
637 printf _("DEBUG was not defined during compilation. Ignored.\n");
642 commands = mutt_add_list (commands, optarg);
650 includeFile = optarg;
654 /* should take precedence over .muttrc setting, so save it for later */
663 sendflags |= SENDPOSTPONED;
667 queries = mutt_add_list (queries, optarg);
671 flags |= M_RO; /* read-only mode */
682 case 'x': /* mailx compatible send mode */
683 sendflags |= SENDMAILX;
686 case 'y': /* My special hack mode */
695 flags |= M_BUFFY | M_IGNORE;
703 /* collapse remaining argv */
704 while (optind < argc)
705 argv[nargc++] = argv[optind++];
717 puts (mutt_make_version ());
721 puts (_(ReachingUs));
725 /* Check for a batch send. */
726 if (!isatty (0) || queries || alias_queries || dump_variables)
728 set_option (OPTNOCURSES);
729 sendflags = SENDBATCH;
732 /* This must come before mutt_init() because curses needs to be started
733 before calling the init_pair() function to set the color scheme. */
734 if (!option (OPTNOCURSES))
737 /* set defaults and read init files */
738 mutt_init (flags & M_NOSYSRC, commands);
739 mutt_free_list (&commands);
741 /* Initialize crypto backends. */
746 for (; optind < argc; optind++)
747 queries = mutt_add_list (queries, argv[optind]);
748 return mutt_query_variables (queries);
751 return mutt_dump_variables();
757 for (; optind < argc; optind++)
758 alias_queries = mutt_add_list (alias_queries, argv[optind]);
759 for (; alias_queries; alias_queries = alias_queries->next)
761 if ((a = mutt_lookup_alias (alias_queries->data)))
763 /* output in machine-readable form */
764 mutt_addrlist_to_idna (a, NULL);
765 mutt_write_address_list (a, stdout, 0, 0);
770 printf ("%s\n", alias_queries->data);
777 mx_set_magic (newMagic);
779 if (!option (OPTNOCURSES))
781 SETCOLOR (MT_COLOR_NORMAL);
783 mutt_error = mutt_curses_error;
784 mutt_message = mutt_curses_message;
787 /* Create the Maildir directory if it doesn't exist. */
788 if (!option (OPTNOCURSES) && Maildir)
791 char fpath[_POSIX_PATH_MAX];
794 strfcpy (fpath, Maildir, sizeof (fpath));
795 mutt_expand_path (fpath, sizeof (fpath));
797 /* we're not connected yet - skip mail folder creation */
798 if (!mx_is_imap (fpath))
800 if (stat (fpath, &sb) == -1 && errno == ENOENT)
802 snprintf (msg, sizeof (msg), _("%s does not exist. Create it?"), Maildir);
803 if (mutt_yesorno (msg, M_YES) == M_YES)
805 if (mkdir (fpath, 0700) == -1 && errno != EEXIST)
806 mutt_error ( _("Can't create %s: %s."), Maildir, strerror (errno));
811 if (sendflags & SENDPOSTPONED)
813 if (!option (OPTNOCURSES))
815 ci_send_message (SENDPOSTPONED, NULL, NULL, NULL, NULL);
818 else if (subject || msg || sendflags || draftFile || includeFile || attach ||
822 char buf[LONG_STRING];
823 char *tempfile = NULL, *infile = NULL;
824 char *bodytext = NULL;
827 if (!option (OPTNOCURSES))
831 msg = mutt_new_header ();
838 msg->env = mutt_new_envelope ();
840 for (i = optind; i < argc; i++)
842 if (url_check_scheme (argv[i]) == U_MAILTO)
844 if (url_parse_mailto (msg->env, &bodytext, argv[i]) < 0)
846 if (!option (OPTNOCURSES))
848 fputs (_("Failed to parse mailto: link\n"), stderr);
853 msg->env->to = rfc822_parse_adrlist (msg->env->to, argv[i]);
856 if (option (OPTAUTOEDIT) && !msg->env->to && !msg->env->cc)
858 if (!option (OPTNOCURSES))
860 fputs (_("No recipients specified.\n"), stderr);
865 msg->env->subject = safe_strdup (subject);
868 infile = includeFile;
871 if (infile || bodytext)
875 if (mutt_strcmp ("-", infile) == 0)
879 char path[_POSIX_PATH_MAX];
881 strfcpy (path, infile, sizeof (path));
882 mutt_expand_path (path, sizeof (path));
883 if ((fin = fopen (path, "r")) == NULL)
885 if (!option (OPTNOCURSES))
896 tempfile = safe_strdup (buf);
899 msg->env = mutt_read_rfc822_header (fin, NULL, 1, 0);
901 /* is the following if still needed? */
907 if ((fout = safe_fopen (tempfile, "w")) == NULL)
909 if (!option (OPTNOCURSES))
917 mutt_copy_stream (fin, fout);
919 fputs (bodytext, fout);
921 if (fin && fin != stdin)
937 a->next = mutt_make_file_attach (t->data);
941 msg->content = a = mutt_make_file_attach (t->data);
944 if (!option (OPTNOCURSES))
946 fprintf (stderr, _("%s: unable to attach file.\n"), t->data);
947 mutt_free_list (&attach);
952 mutt_free_list (&attach);
955 rv = ci_send_message (sendflags, msg, tempfile, NULL, NULL);
957 if (!option (OPTNOCURSES))
967 if (!mutt_buffy_check (0))
969 mutt_endwin _("No mailbox with new mail.");
973 mutt_buffy (folder, sizeof (folder));
975 else if (flags & M_SELECT)
978 mutt_endwin _("No incoming mailboxes defined.");
982 mutt_select_file (folder, sizeof (folder), M_SEL_FOLDER | M_SEL_BUFFY);
991 strfcpy (folder, NONULL(Spoolfile), sizeof (folder));
992 mutt_expand_path (folder, sizeof (folder));
994 mutt_str_replace (&CurrentFolder, folder);
995 mutt_str_replace (&LastFolder, folder);
997 if (flags & M_IGNORE)
999 /* check to see if there are any messages in the folder */
1000 switch (mx_check_empty (folder))
1003 mutt_endwin (strerror (errno));
1006 mutt_endwin _("Mailbox is empty.");
1011 mutt_folder_hook (folder);
1013 if((Context = mx_open_mailbox (folder, ((flags & M_RO) || option (OPTREADONLY)) ? M_READONLY : 0, NULL))
1014 || !explicit_folder)
1027 mutt_endwin (Errorbuf);