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.
28 #include <sys/utsname.h>
30 #include <lib-lib/mem.h>
31 #include <lib-lib/str.h>
32 #include <lib-lib/macros.h>
33 #include <lib-lib/file.h>
34 #include <lib-lib/debug.h>
36 #include <lib-sys/mutt_signal.h>
38 #include <lib-ui/curses.h>
44 #include <lib-crypt/crypt.h>
47 #include "mutt_idna.h"
58 #include <stringprep.h>
62 #include "nntp/nntp.h"
80 #include <gnutls/gnutls.h>
83 #ifdef CRYPT_BACKEND_GPGME
88 #include <openssl/opensslv.h>
91 static const char *Notice = N_("\
92 Copyright (C) 1996-2002 Michael R. Elkins and others.\n\
93 Madmutt comes with ABSOLUTELY NO WARRANTY; for details type `madmutt -vv'.\n\
94 Mutt is free software, and you are welcome to redistribute it\n\
95 under certain conditions; type `madmutt -vv' for details.\n");
97 static const char *Copyright_Mutt = N_("\
98 Copyright (C) 1996-2004 Michael R. Elkins <me@mutt.org>\n\
99 Copyright (C) 1996-2002 Brandon Long <blong@fiction.net>\n\
100 Copyright (C) 1997-2005 Thomas Roessler <roessler@does-not-exist.org>\n\
101 Copyright (C) 1998-2005 Werner Koch <wk@isil.d.shuttle.de>\n\
102 Copyright (C) 1999-2005 Brendan Cully <brendan@kublai.com>\n\
103 Copyright (C) 1999-2002 Tommi Komulainen <Tommi.Komulainen@iki.fi>\n\
104 Copyright (C) 2000-2002 Edmund Grimley Evans <edmundo@rano.org>\n");
106 static const char *Copyright_Mutt_NG = N_("\
107 Copyright (C) 2005:\n\
108 Parts were written/modified by:\n\
109 Andreas Krennmair <ak@synflood.at>\n\
110 Nico Golde <nico@ngolde.de>\n\
111 Rocco Rutte <pdmef@cs.tu-berlin.de>\n\
113 Lots of others not mentioned here contributed lots of code,\n\
114 fixes, and suggestions.");
116 static const char* Copyright_GPL_1 = N_("\
118 This program is free software; you can redistribute it and/or modify\n\
119 it under the terms of the GNU General Public License as published by\n\
120 the Free Software Foundation; either version 2 of the License, or\n\
121 (at your option) any later version.\n");
123 static const char* Copyright_GPL_2 = N_("\
124 This program is distributed in the hope that it will be useful,\n\
125 but WITHOUT ANY WARRANTY; without even the implied warranty of\n\
126 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n\
127 GNU General Public License for more details.\n");
129 static const char* Copyright_GPL_3 = N_("\
130 You should have received a copy of the GNU General Public License\n\
131 along with this program; if not, write to the Free Software\n\
132 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.\n\
135 void mutt_exit (int code)
141 static void mutt_usage (void)
143 puts (mutt_make_version (1));
146 _("usage: madmutt [ -nRyzZ ] [ -e <cmd> ] [ -F <file> ] [ -m <type> ] [ -f <file> ]\n\
147 madmutt [ -nR ] [ -e <cmd> ] [ -F <file> ] -Q <query> [ -Q <query> ] [...]\n\
148 madmutt [ -nR ] [ -e <cmd> ] [ -F <file> ] -A <alias> [ -A <alias> ] [...]\n\
149 madmutt [ -nR ] [ -e <cmd> ] [ -F <file> ] -t");
152 madmutt [ -nR ] [ -e <cmd> ] [ -F <file> ] -T\n\
153 madmutt [ -nx ] [ -e <cmd> ] [ -a <file> ] [ -F <file> ] [ -H <file> ] [ -i <file> ] [ -s <subj> ] [ -b <addr> ] [ -c <addr> ] <addr> [ ... ]\n\
154 madmutt [ -n ] [ -e <cmd> ] [ -F <file> ] -p\n\
158 -A <alias>\texpand the given alias\n\
159 -a <file>\tattach a file to the message\n\
160 -b <address>\tspecify a blind carbon-copy (BCC) address\n\
161 -c <address>\tspecify a carbon-copy (CC) address");
163 puts _(" -d <level>\tlog debugging output to ~/.madmuttdebug0");
166 -e <command>\tspecify a command to be executed after initialization\n\
167 -f <file>\tspecify which mailbox to read\n\
168 -F <file>\tspecify an alternate muttrngc file\n\
169 -g <server>\tspecify a newsserver (if compiled with NNTP)\n\
170 -G\t\tselect a newsgroup (if compiled with NNTP)\n\
171 -H <file>\tspecify a draft file to read header and body from\n\
172 -i <file>\tspecify a file which Madmutt should include in the body");
174 -d <level>\t specify debugging level of Madmutt\n\
175 -m <type>\tspecify a default mailbox type\n\
176 -n\t\tcauses Madmutt not to read the system Madmuttrc\n\
177 -p\t\trecall a postponed message\n\
178 -Q <variable>\tquery a configuration variable\n\
179 -R\t\topen mailbox in read-only mode\n\
180 -s <subj>\tspecify a subject (must be in quotes if it has spaces)");
182 -t\t\tprint the value of all variables to stdout\n\
183 -T\t\tprint the value of all changed variables to stdout\n\
184 -v\t\tshow version and compile-time definitions\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");
193 static void show_version (void)
200 puts (mutt_make_version (1));
206 printf ("System:\n %s %s.%s", uts.sysname, uts.version, uts.release);
208 printf ("System:\n SCO %s", uts.release);
210 printf ("System:\n %s %s", uts.sysname, uts.release);
213 printf (" (%s)\nExternal Libraries:\n", uts.machine);
215 #ifdef NCURSES_VERSION
216 printf (" ncurses %s\n", NCURSES_VERSION);
217 #elif defined(USE_SLANG_CURSES)
218 printf (" slang %d\n", SLANG_VERSION);
221 #ifdef _LIBICONV_VERSION
222 printf (" libiconv %d.%d\n", _LIBICONV_VERSION >> 8,
223 _LIBICONV_VERSION & 0xff);
227 printf (" libidn %s (compiled with %s)\n",
228 stringprep_check_version (NULL), STRINGPREP_VERSION);
232 smtp_version (buf, sizeof (buf), 0);
233 printf (" libESMTP %s\n", buf);
238 printf (" qdbm %s\n", dpversion);
240 printf (" gdbm %s\n", gdbm_version);
242 printf (" DB4 %d.%d.%d\n", DB_VERSION_MAJOR, DB_VERSION_MINOR,
248 printf (" gnutls %s\n", LIBGNUTLS_VERSION);
252 printf (" %s\n",OPENSSL_VERSION_TEXT);
255 #ifdef CRYPT_BACKEND_GPGME
256 printf (" gpgme %s\n", GPGME_VERSION);
259 puts (_("Compile Options:"));
342 #ifdef HAVE_START_COLOR
347 #ifdef HAVE_TYPEAHEAD
368 #ifdef HAVE_RESIZETERM
376 #ifdef CRYPT_BACKEND_GPGME
377 "+CRYPT_BACKEND_GPGME "
379 "-CRYPT_BACKEND_GPGME "
397 #ifdef HAVE_LANGINFO_CODESET
398 "+HAVE_LANGINFO_CODESET "
400 "-HAVE_LANGINFO_CODESET "
402 #ifdef HAVE_LANGINFO_YESEXPR
403 "+HAVE_LANGINFO_YESEXPR "
405 "-HAVE_LANGINFO_YESEXPR "
435 puts (_("Built-In Defaults:"));
438 printf (" +ISPELL=\"%s\"\n", ISPELL);
443 printf (" +SENDMAIL=\"%s\"\n", SENDMAIL);
444 printf (" +MAILPATH=\"%s\"\n", MAILPATH);
445 printf (" +PKGDATADIR=\"%s\"\n", PKGDATADIR);
446 printf (" +PKGDOCDIR=\"%s\"\n", PKGDOCDIR);
447 printf (" +SYSCONFDIR=\"%s\"\n", SYSCONFDIR);
449 printf (" +MIXMASTER=\"%s\"\n\n", MIXMASTER);
451 puts (" -MIXMASTER\n");
454 puts (_("MadMutt is based on Madmutt wich was based on Mutt before\n"));
459 static void start_curses (void)
461 km_init (); /* must come before mutt_init */
463 #ifdef USE_SLANG_CURSES
464 SLtt_Ignore_Beep = 1; /* don't do that #*$@^! annoying visual beep! */
465 SLsmg_Display_Eight_Bit = 128; /* characters above this are printable */
466 SLtt_set_color (0, NULL, "default", "default");
468 /* should come before initscr() so that ncurses 4.2 doesn't try to install
469 its own SIGWINCH handler */
470 mutt_signal_initialize ();
472 if (initscr () == NULL) {
473 puts _("Error initializing terminal.");
477 mutt_signal_initialize ();
479 keypad (stdscr, TRUE);
483 typeahead (-1); /* simulate smooth scrolling */
490 #define M_IGNORE (1<<0) /* -z */
491 #define M_BUFFY (1<<1) /* -Z */
492 #define M_NOSYSRC (1<<2) /* -n */
493 #define M_RO (1<<3) /* -R */
494 #define M_SELECT (1<<4) /* -y */
496 #define M_NEWS (1<<5) /* -g and -G */
499 int main (int argc, char **argv)
501 char folder[_POSIX_PATH_MAX] = "";
502 char *subject = NULL;
503 char *includeFile = NULL;
504 char *draftFile = NULL;
505 char *newMagic = NULL;
508 LIST *commands = NULL;
509 LIST *queries = NULL;
510 LIST *alias_queries = NULL;
515 int explicit_folder = 0;
516 int dump_variables = -1;
518 /* initialize random number for tmp file creation */
519 srand ((unsigned int) time (NULL));
521 /* sanity check against stupid administrators */
523 if (getegid () != getgid ()) {
524 fprintf (stderr, "%s: I don't want to run with privileges!\n", argv[0]);
529 /* FIXME what about init.c:1439 ? */
530 setlocale (LC_ALL, "");
531 bindtextdomain (PACKAGE, MUTTLOCALEDIR);
532 textdomain (PACKAGE);
535 setlocale (LC_CTYPE, "");
537 mutt_error = mutt_nocurses_error;
538 mutt_message = mutt_nocurses_error;
542 p_clear(Options, countof(Options));
543 p_clear(QuadOptions, countof(QuadOptions));
548 "A:a:b:F:f:c:d:e:g:GH:s:i:hm:npQ:RTtvyzZ")) != EOF)
551 getopt (argc, argv, "A:a:b:F:f:c:d:e:H:s:i:hm:npQ:RTtvyzZ")) != EOF)
555 alias_queries = mutt_add_list (alias_queries, optarg);
558 if (strlen(optarg)<=512)
559 attach = mutt_add_list (attach, optarg);
561 printf("too long arguments. exiting ...\n");
567 m_strreplace(&Muttrc, optarg);
571 m_strcpy(folder, sizeof(folder), optarg);
580 msg->env = envelope_new();
582 msg->env->bcc = rfc822_parse_adrlist (msg->env->bcc, optarg);
584 msg->env->cc = rfc822_parse_adrlist (msg->env->cc, optarg);
589 char buf[LONG_STRING];
590 int level = atoi (optarg);
592 snprintf (buf, sizeof (buf), "set debug_level=%d", level);
593 commands = mutt_add_list (commands, buf);
594 debug_setlevel (level);
607 commands = mutt_add_list (commands, optarg);
615 includeFile = optarg;
619 /* should take precedence over .muttrc setting, so save it for later */
628 sendflags |= SENDPOSTPONED;
632 queries = mutt_add_list (queries, optarg);
636 flags |= M_RO; /* read-only mode */
647 case 'y': /* My special hack mode */
652 case 'g': /* Specify a newsserver */
654 char buf[LONG_STRING];
656 snprintf (buf, sizeof (buf), "set nntp_host=%s", optarg);
657 commands = mutt_add_list (commands, buf);
660 case 'G': /* List of newsgroups */
661 flags |= M_SELECT | M_NEWS;
670 flags |= M_BUFFY | M_IGNORE;
684 puts (mutt_make_version (1));
685 puts (_(Copyright_Mutt));
686 puts (_(Copyright_Mutt_NG));
687 puts (_(Copyright_GPL_1));
688 puts (_(Copyright_GPL_2));
689 puts (_(Copyright_GPL_3));
693 /* Check for a batch send. */
694 if (!isatty (0) || queries || alias_queries || dump_variables > 0) {
695 set_option (OPTNOCURSES);
696 sendflags = SENDBATCH;
699 /* This must come before mutt_init() because curses needs to be started
700 before calling the init_pair() function to set the color scheme. */
701 if (!option (OPTNOCURSES))
704 /* set defaults and read init files */
706 mutt_init (flags & M_NOSYSRC, commands);
707 mutt_free_list (&commands);
709 /* Initialize crypto */
713 return mutt_query_variables (queries);
714 if (dump_variables > 0)
715 return (mutt_dump_variables (dump_variables-1));
721 for (; alias_queries; alias_queries = alias_queries->next) {
722 if ((a = mutt_lookup_alias (alias_queries->data))) {
723 /* output in machine-readable form */
724 mutt_addrlist_to_idna (a, NULL);
725 mutt_write_address_list (a, stdout, 0, 0);
729 printf ("%s\n", alias_queries->data);
736 mx_set_magic (newMagic);
738 if (!option (OPTNOCURSES)) {
739 SETCOLOR (MT_COLOR_NORMAL);
741 mutt_error = mutt_curses_error;
742 mutt_message = mutt_curses_message;
745 /* Create the Maildir directory if it doesn't exist. */
746 if (!option (OPTNOCURSES) && Maildir) {
748 char fpath[_POSIX_PATH_MAX];
751 m_strcpy(fpath, sizeof(fpath), Maildir);
752 mutt_expand_path (fpath, sizeof (fpath));
753 /* we're not connected yet - skip mail folder creation */
754 if (mx_get_magic (fpath) != M_IMAP)
755 if (stat (fpath, &sb) == -1 && errno == ENOENT) {
756 snprintf (mesg, sizeof (mesg), _("%s does not exist. Create it?"),
758 if (mutt_yesorno (mesg, M_YES) == M_YES) {
759 if (mkdir (fpath, 0700) == -1 && errno != EEXIST)
760 mutt_error (_("Can't create %s: %s."), Maildir, strerror (errno));
765 if (sendflags & SENDPOSTPONED) {
766 if (!option (OPTNOCURSES))
768 ci_send_message (SENDPOSTPONED, NULL, NULL, NULL, NULL);
771 else if (subject || msg || sendflags || draftFile || includeFile || attach
774 char buf[LONG_STRING];
775 char *tempfile = NULL, *infile = NULL;
776 char *bodytext = NULL;
778 if (!option (OPTNOCURSES))
788 msg->env = envelope_new();
790 for (i = optind; i < argc; i++) {
791 if (url_check_scheme (argv[i]) == U_MAILTO)
792 url_parse_mailto (msg->env, &bodytext, argv[i]);
794 msg->env->to = rfc822_parse_adrlist (msg->env->to, argv[i]);
797 if (option (OPTAUTOEDIT) && !msg->env->to && !msg->env->cc) {
798 if (!option (OPTNOCURSES))
800 fputs (_("No recipients specified.\n"), stderr);
805 msg->env->subject = m_strdup(subject);
808 infile = includeFile;
811 if (infile || bodytext) {
813 if (m_strcmp("-", infile) == 0)
816 char path[_POSIX_PATH_MAX];
818 m_strcpy(path, sizeof(path), infile);
819 mutt_expand_path (path, sizeof (path));
820 if ((fin = fopen (path, "r")) == NULL) {
821 if (!option (OPTNOCURSES))
832 tempfile = m_strdup(buf);
835 msg->env = mutt_read_rfc822_header (fin, NULL, 1, 0);
837 /* is the following if still needed? */
842 if ((fout = safe_fopen (tempfile, "w")) == NULL) {
843 if (!option (OPTNOCURSES))
851 mutt_copy_stream (fin, fout);
853 fputs (bodytext, fout);
855 if (fin && fin != stdin)
868 a->next = mutt_make_file_attach (t->data);
872 msg->content = a = mutt_make_file_attach (t->data);
874 if (!option (OPTNOCURSES))
876 fprintf (stderr, _("%s: unable to attach file.\n"), t->data);
877 mutt_free_list (&attach);
882 mutt_free_list (&attach);
885 ci_send_message (sendflags, msg, tempfile, NULL, NULL);
887 if (!option (OPTNOCURSES))
891 if (flags & M_BUFFY) {
892 if (!buffy_check (0)) {
893 mutt_endwin _("No mailbox with new mail.");
898 buffy_next (folder, sizeof (folder));
900 else if (flags & M_SELECT) {
902 if (flags & M_NEWS) {
903 set_option (OPTNEWS);
904 if (!(CurrentNewsSrv = mutt_select_newsserver (NewsServer))) {
905 mutt_endwin (Errorbuf);
912 mutt_endwin _("No incoming mailboxes defined.");
917 mutt_select_file (folder, sizeof (folder), M_SEL_FOLDER | M_SEL_BUFFY);
925 m_strcpy(folder, sizeof(folder), NONULL(Spoolfile));
928 if (option (OPTNEWS)) {
929 unset_option (OPTNEWS);
930 nntp_expand_path (folder, sizeof (folder),
931 &CurrentNewsSrv->conn->account);
935 mutt_expand_path (folder, sizeof (folder));
937 m_strreplace(&CurrentFolder, folder);
938 m_strreplace(&LastFolder, folder);
940 if (flags & M_IGNORE) {
941 /* check to see if there are any messages in the folder */
942 switch (mx_check_empty (folder)) {
944 mutt_endwin (strerror (errno));
947 mutt_endwin _("Mailbox is empty.");
952 mutt_folder_hook (folder);
954 if ((Context = mx_open_mailbox (folder, ((flags & M_RO)
955 || option (OPTREADONLY)) ?
956 M_READONLY : 0, NULL))
957 || !explicit_folder) {
959 if (option (OPTXTERMSETTITLES))
960 mutt_xterm_set_title (NONULL (XtermLeave));
964 mutt_endwin (Errorbuf);