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
7 * Parts written/modified by:
8 * Nico Golde <nion@muttng.org>
9 * Andreas Krennmair <ak@synflood.at>
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.
22 #include <lib-lib/mem.h>
23 #include <lib-lib/str.h>
24 #include <lib-lib/macros.h>
25 #include <lib-lib/file.h>
27 #include <lib-ui/curses.h>
33 #include <lib-crypt/crypt.h>
36 #include "mutt_idna.h"
39 #include "lib/debug.h"
47 #include <sys/utsname.h>
57 #include <stringprep.h>
61 #include "nntp/nntp.h"
79 #include <gnutls/gnutls.h>
82 #ifdef CRYPT_BACKEND_GPGME
87 #include <openssl/opensslv.h>
90 static const char *Notice = N_("\
91 Copyright (C) 1996-2002 Michael R. Elkins and others.\n\
92 Madmutt comes with ABSOLUTELY NO WARRANTY; for details type `madmutt -vv'.\n\
93 Mutt is free software, and you are welcome to redistribute it\n\
94 under certain conditions; type `madmutt -vv' for details.\n");
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");
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\
112 Lots of others not mentioned here contributed lots of code,\n\
113 fixes, and suggestions.");
115 static const char* Copyright_GPL_1 = 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");
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");
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\
134 void mutt_exit (int code)
140 static void mutt_usage (void)
142 puts (mutt_make_version (1));
145 _("usage: madmutt [ -nRyzZ ] [ -e <cmd> ] [ -F <file> ] [ -m <type> ] [ -f <file> ]\n\
146 madmutt [ -nR ] [ -e <cmd> ] [ -F <file> ] -Q <query> [ -Q <query> ] [...]\n\
147 madmutt [ -nR ] [ -e <cmd> ] [ -F <file> ] -A <alias> [ -A <alias> ] [...]\n\
148 madmutt [ -nR ] [ -e <cmd> ] [ -F <file> ] -t");
151 madmutt [ -nR ] [ -e <cmd> ] [ -F <file> ] -T\n\
152 madmutt [ -nx ] [ -e <cmd> ] [ -a <file> ] [ -F <file> ] [ -H <file> ] [ -i <file> ] [ -s <subj> ] [ -b <addr> ] [ -c <addr> ] <addr> [ ... ]\n\
153 madmutt [ -n ] [ -e <cmd> ] [ -F <file> ] -p\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");
162 puts _(" -d <level>\tlog debugging output to ~/.madmuttdebug0");
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 Madmutt should include in the body");
173 -d <level>\t specify debugging level of Madmutt\n\
174 -m <type>\tspecify a default mailbox type\n\
175 -n\t\tcauses Madmutt not to read the system Madmuttrc\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)");
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 -y\t\tselect a mailbox specified in your `mailboxes' list\n\
185 -z\t\texit immediately if there are no messages in the mailbox\n\
186 -Z\t\topen the first folder with new message, exit immediately if none\n\
187 -h\t\tthis help message");
192 static void show_version (void)
199 puts (mutt_make_version (1));
205 printf ("System:\n %s %s.%s", uts.sysname, uts.version, uts.release);
207 printf ("System:\n SCO %s", uts.release);
209 printf ("System:\n %s %s", uts.sysname, uts.release);
212 printf (" (%s)\nExternal Libraries:\n", uts.machine);
214 #ifdef NCURSES_VERSION
215 printf (" ncurses %s\n", NCURSES_VERSION);
216 #elif defined(USE_SLANG_CURSES)
217 printf (" slang %d\n", SLANG_VERSION);
220 #ifdef _LIBICONV_VERSION
221 printf (" libiconv %d.%d\n", _LIBICONV_VERSION >> 8,
222 _LIBICONV_VERSION & 0xff);
226 printf (" libidn %s (compiled with %s)\n",
227 stringprep_check_version (NULL), STRINGPREP_VERSION);
231 smtp_version (buf, sizeof (buf), 0);
232 printf (" libESMTP %s\n", buf);
237 printf (" qdbm %s\n", dpversion);
239 printf (" gdbm %s\n", gdbm_version);
241 printf (" DB4 %d.%d.%d\n", DB_VERSION_MAJOR, DB_VERSION_MINOR,
247 printf (" gnutls %s\n", LIBGNUTLS_VERSION);
251 printf (" %s\n",OPENSSL_VERSION_TEXT);
254 #ifdef CRYPT_BACKEND_GPGME
255 printf (" gpgme %s\n", GPGME_VERSION);
258 puts (_("Compile Options:"));
346 #ifdef HAVE_START_COLOR
351 #ifdef HAVE_TYPEAHEAD
372 #ifdef HAVE_RESIZETERM
380 #ifdef CRYPT_BACKEND_GPGME
381 "+CRYPT_BACKEND_GPGME "
383 "-CRYPT_BACKEND_GPGME "
401 #ifdef HAVE_LANGINFO_CODESET
402 "+HAVE_LANGINFO_CODESET "
404 "-HAVE_LANGINFO_CODESET "
406 #ifdef HAVE_LANGINFO_YESEXPR
407 "+HAVE_LANGINFO_YESEXPR "
409 "-HAVE_LANGINFO_YESEXPR "
439 puts (_("Built-In Defaults:"));
442 printf (" +ISPELL=\"%s\"\n", ISPELL);
447 printf (" +SENDMAIL=\"%s\"\n", SENDMAIL);
448 printf (" +MAILPATH=\"%s\"\n", MAILPATH);
449 printf (" +PKGDATADIR=\"%s\"\n", PKGDATADIR);
450 printf (" +PKGDOCDIR=\"%s\"\n", PKGDOCDIR);
451 printf (" +SYSCONFDIR=\"%s\"\n", SYSCONFDIR);
453 printf (" +MIXMASTER=\"%s\"\n\n", MIXMASTER);
455 puts (" -MIXMASTER\n");
458 puts (_("MadMutt is based on Madmutt wich was based on Mutt before\n"));
463 static void start_curses (void)
465 km_init (); /* must come before mutt_init */
467 #ifdef USE_SLANG_CURSES
468 SLtt_Ignore_Beep = 1; /* don't do that #*$@^! annoying visual beep! */
469 SLsmg_Display_Eight_Bit = 128; /* characters above this are printable */
470 SLtt_set_color (0, NULL, "default", "default");
472 /* should come before initscr() so that ncurses 4.2 doesn't try to install
473 its own SIGWINCH handler */
476 if (initscr () == NULL) {
477 puts _("Error initializing terminal.");
481 #if 1 /* USE_SLANG_CURSES - commenting out suggested in #455. */
482 /* slang requires the signal handlers to be set after initializing */
486 keypad (stdscr, TRUE);
490 typeahead (-1); /* simulate smooth scrolling */
497 #define M_IGNORE (1<<0) /* -z */
498 #define M_BUFFY (1<<1) /* -Z */
499 #define M_NOSYSRC (1<<2) /* -n */
500 #define M_RO (1<<3) /* -R */
501 #define M_SELECT (1<<4) /* -y */
503 #define M_NEWS (1<<5) /* -g and -G */
506 int main (int argc, char **argv)
508 char folder[_POSIX_PATH_MAX] = "";
509 char *subject = NULL;
510 char *includeFile = NULL;
511 char *draftFile = NULL;
512 char *newMagic = NULL;
515 LIST *commands = NULL;
516 LIST *queries = NULL;
517 LIST *alias_queries = NULL;
522 int explicit_folder = 0;
523 int dump_variables = -1;
525 /* initialize random number for tmp file creation */
526 srand ((unsigned int) time (NULL));
528 /* sanity check against stupid administrators */
530 if (getegid () != getgid ()) {
531 fprintf (stderr, "%s: I don't want to run with privileges!\n", argv[0]);
536 /* FIXME what about init.c:1439 ? */
537 setlocale (LC_ALL, "");
538 bindtextdomain (PACKAGE, MUTTLOCALEDIR);
539 textdomain (PACKAGE);
542 setlocale (LC_CTYPE, "");
544 mutt_error = mutt_nocurses_error;
545 mutt_message = mutt_nocurses_error;
549 p_clear(Options, countof(Options));
550 p_clear(QuadOptions, countof(QuadOptions));
555 "A:a:b:F:f:c:d:e:g:GH:s:i:hm:npQ:RTtvyzZ")) != EOF)
558 getopt (argc, argv, "A:a:b:F:f:c:d:e:H:s:i:hm:npQ:RTtvyzZ")) != EOF)
562 alias_queries = mutt_add_list (alias_queries, optarg);
565 if (strlen(optarg)<=512)
566 attach = mutt_add_list (attach, optarg);
568 printf("too long arguments. exiting ...\n");
574 m_strreplace(&Muttrc, optarg);
578 m_strcpy(folder, sizeof(folder), optarg);
587 msg->env = envelope_new();
589 msg->env->bcc = rfc822_parse_adrlist (msg->env->bcc, optarg);
591 msg->env->cc = rfc822_parse_adrlist (msg->env->cc, optarg);
596 char buf[LONG_STRING];
597 int level = atoi (optarg);
599 snprintf (buf, sizeof (buf), "set debug_level=%d", level);
600 commands = mutt_add_list (commands, buf);
601 debug_setlevel (level);
614 commands = mutt_add_list (commands, optarg);
622 includeFile = optarg;
626 /* should take precedence over .muttrc setting, so save it for later */
635 sendflags |= SENDPOSTPONED;
639 queries = mutt_add_list (queries, optarg);
643 flags |= M_RO; /* read-only mode */
654 case 'y': /* My special hack mode */
659 case 'g': /* Specify a newsserver */
661 char buf[LONG_STRING];
663 snprintf (buf, sizeof (buf), "set nntp_host=%s", optarg);
664 commands = mutt_add_list (commands, buf);
667 case 'G': /* List of newsgroups */
668 flags |= M_SELECT | M_NEWS;
677 flags |= M_BUFFY | M_IGNORE;
691 puts (mutt_make_version (1));
692 puts (_(Copyright_Mutt));
693 puts (_(Copyright_Mutt_NG));
694 puts (_(Copyright_GPL_1));
695 puts (_(Copyright_GPL_2));
696 puts (_(Copyright_GPL_3));
700 /* Check for a batch send. */
701 if (!isatty (0) || queries || alias_queries || dump_variables > 0) {
702 set_option (OPTNOCURSES);
703 sendflags = SENDBATCH;
706 /* This must come before mutt_init() because curses needs to be started
707 before calling the init_pair() function to set the color scheme. */
708 if (!option (OPTNOCURSES))
711 /* set defaults and read init files */
713 mutt_init (flags & M_NOSYSRC, commands);
714 mutt_free_list (&commands);
716 /* Initialize crypto */
720 return mutt_query_variables (queries);
721 if (dump_variables > 0)
722 return (mutt_dump_variables (dump_variables-1));
728 for (; alias_queries; alias_queries = alias_queries->next) {
729 if ((a = mutt_lookup_alias (alias_queries->data))) {
730 /* output in machine-readable form */
731 mutt_addrlist_to_idna (a, NULL);
732 mutt_write_address_list (a, stdout, 0, 0);
736 printf ("%s\n", alias_queries->data);
743 mx_set_magic (newMagic);
745 if (!option (OPTNOCURSES)) {
746 SETCOLOR (MT_COLOR_NORMAL);
748 mutt_error = mutt_curses_error;
749 mutt_message = mutt_curses_message;
752 /* Create the Maildir directory if it doesn't exist. */
753 if (!option (OPTNOCURSES) && Maildir) {
755 char fpath[_POSIX_PATH_MAX];
758 m_strcpy(fpath, sizeof(fpath), Maildir);
759 mutt_expand_path (fpath, sizeof (fpath));
760 /* we're not connected yet - skip mail folder creation */
761 if (mx_get_magic (fpath) != M_IMAP)
762 if (stat (fpath, &sb) == -1 && errno == ENOENT) {
763 snprintf (mesg, sizeof (mesg), _("%s does not exist. Create it?"),
765 if (mutt_yesorno (mesg, M_YES) == M_YES) {
766 if (mkdir (fpath, 0700) == -1 && errno != EEXIST)
767 mutt_error (_("Can't create %s: %s."), Maildir, strerror (errno));
772 if (sendflags & SENDPOSTPONED) {
773 if (!option (OPTNOCURSES))
775 ci_send_message (SENDPOSTPONED, NULL, NULL, NULL, NULL);
778 else if (subject || msg || sendflags || draftFile || includeFile || attach
781 char buf[LONG_STRING];
782 char *tempfile = NULL, *infile = NULL;
783 char *bodytext = NULL;
785 if (!option (OPTNOCURSES))
795 msg->env = envelope_new();
797 for (i = optind; i < argc; i++) {
798 if (url_check_scheme (argv[i]) == U_MAILTO)
799 url_parse_mailto (msg->env, &bodytext, argv[i]);
801 msg->env->to = rfc822_parse_adrlist (msg->env->to, argv[i]);
804 if (option (OPTAUTOEDIT) && !msg->env->to && !msg->env->cc) {
805 if (!option (OPTNOCURSES))
807 fputs (_("No recipients specified.\n"), stderr);
812 msg->env->subject = m_strdup(subject);
815 infile = includeFile;
818 if (infile || bodytext) {
820 if (m_strcmp("-", infile) == 0)
823 char path[_POSIX_PATH_MAX];
825 m_strcpy(path, sizeof(path), infile);
826 mutt_expand_path (path, sizeof (path));
827 if ((fin = fopen (path, "r")) == NULL) {
828 if (!option (OPTNOCURSES))
839 tempfile = m_strdup(buf);
842 msg->env = mutt_read_rfc822_header (fin, NULL, 1, 0);
844 /* is the following if still needed? */
849 if ((fout = safe_fopen (tempfile, "w")) == NULL) {
850 if (!option (OPTNOCURSES))
858 mutt_copy_stream (fin, fout);
860 fputs (bodytext, fout);
862 if (fin && fin != stdin)
875 a->next = mutt_make_file_attach (t->data);
879 msg->content = a = mutt_make_file_attach (t->data);
881 if (!option (OPTNOCURSES))
883 fprintf (stderr, _("%s: unable to attach file.\n"), t->data);
884 mutt_free_list (&attach);
889 mutt_free_list (&attach);
892 ci_send_message (sendflags, msg, tempfile, NULL, NULL);
894 if (!option (OPTNOCURSES))
898 if (flags & M_BUFFY) {
899 if (!buffy_check (0)) {
900 mutt_endwin _("No mailbox with new mail.");
905 buffy_next (folder, sizeof (folder));
907 else if (flags & M_SELECT) {
909 if (flags & M_NEWS) {
910 set_option (OPTNEWS);
911 if (!(CurrentNewsSrv = mutt_select_newsserver (NewsServer))) {
912 mutt_endwin (Errorbuf);
919 mutt_endwin _("No incoming mailboxes defined.");
924 mutt_select_file (folder, sizeof (folder), M_SEL_FOLDER | M_SEL_BUFFY);
932 m_strcpy(folder, sizeof(folder), NONULL(Spoolfile));
935 if (option (OPTNEWS)) {
936 unset_option (OPTNEWS);
937 nntp_expand_path (folder, sizeof (folder),
938 &CurrentNewsSrv->conn->account);
942 mutt_expand_path (folder, sizeof (folder));
944 m_strreplace(&CurrentFolder, folder);
945 m_strreplace(&LastFolder, folder);
947 if (flags & M_IGNORE) {
948 /* check to see if there are any messages in the folder */
949 switch (mx_check_empty (folder)) {
951 mutt_endwin (strerror (errno));
954 mutt_endwin _("Mailbox is empty.");
959 mutt_folder_hook (folder);
961 if ((Context = mx_open_mailbox (folder, ((flags & M_RO)
962 || option (OPTREADONLY)) ?
963 M_READONLY : 0, NULL))
964 || !explicit_folder) {
966 if (option (OPTXTERMSETTITLES))
967 mutt_xterm_set_title (NONULL (XtermLeave));
971 mutt_endwin (Errorbuf);