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-ui/curses.h>
42 #include <lib-crypt/crypt.h>
45 #include "mutt_idna.h"
56 #include <stringprep.h>
60 #include "nntp/nntp.h"
78 #include <gnutls/gnutls.h>
81 #ifdef CRYPT_BACKEND_GPGME
86 #include <openssl/opensslv.h>
89 static const char *Notice = N_("\
90 Copyright (C) 1996-2002 Michael R. Elkins and others.\n\
91 Madmutt comes with ABSOLUTELY NO WARRANTY; for details type `madmutt -vv'.\n\
92 Mutt is free software, and you are welcome to redistribute it\n\
93 under certain conditions; type `madmutt -vv' for details.\n");
95 static const char *Copyright_Mutt = N_("\
96 Copyright (C) 1996-2004 Michael R. Elkins <me@mutt.org>\n\
97 Copyright (C) 1996-2002 Brandon Long <blong@fiction.net>\n\
98 Copyright (C) 1997-2005 Thomas Roessler <roessler@does-not-exist.org>\n\
99 Copyright (C) 1998-2005 Werner Koch <wk@isil.d.shuttle.de>\n\
100 Copyright (C) 1999-2005 Brendan Cully <brendan@kublai.com>\n\
101 Copyright (C) 1999-2002 Tommi Komulainen <Tommi.Komulainen@iki.fi>\n\
102 Copyright (C) 2000-2002 Edmund Grimley Evans <edmundo@rano.org>\n");
104 static const char *Copyright_Mutt_NG = N_("\
105 Copyright (C) 2005:\n\
106 Parts were written/modified by:\n\
107 Andreas Krennmair <ak@synflood.at>\n\
108 Nico Golde <nico@ngolde.de>\n\
109 Rocco Rutte <pdmef@cs.tu-berlin.de>\n\
111 Lots of others not mentioned here contributed lots of code,\n\
112 fixes, and suggestions.");
114 static const char* Copyright_GPL_1 = N_("\
116 This program is free software; you can redistribute it and/or modify\n\
117 it under the terms of the GNU General Public License as published by\n\
118 the Free Software Foundation; either version 2 of the License, or\n\
119 (at your option) any later version.\n");
121 static const char* Copyright_GPL_2 = N_("\
122 This program is distributed in the hope that it will be useful,\n\
123 but WITHOUT ANY WARRANTY; without even the implied warranty of\n\
124 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n\
125 GNU General Public License for more details.\n");
127 static const char* Copyright_GPL_3 = N_("\
128 You should have received a copy of the GNU General Public License\n\
129 along with this program; if not, write to the Free Software\n\
130 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.\n\
133 void mutt_exit (int code)
139 static void mutt_usage (void)
141 puts (mutt_make_version (1));
144 _("usage: madmutt [ -nRyzZ ] [ -e <cmd> ] [ -F <file> ] [ -m <type> ] [ -f <file> ]\n\
145 madmutt [ -nR ] [ -e <cmd> ] [ -F <file> ] -Q <query> [ -Q <query> ] [...]\n\
146 madmutt [ -nR ] [ -e <cmd> ] [ -F <file> ] -A <alias> [ -A <alias> ] [...]\n\
147 madmutt [ -nR ] [ -e <cmd> ] [ -F <file> ] -t");
150 madmutt [ -nR ] [ -e <cmd> ] [ -F <file> ] -T\n\
151 madmutt [ -nx ] [ -e <cmd> ] [ -a <file> ] [ -F <file> ] [ -H <file> ] [ -i <file> ] [ -s <subj> ] [ -b <addr> ] [ -c <addr> ] <addr> [ ... ]\n\
152 madmutt [ -n ] [ -e <cmd> ] [ -F <file> ] -p\n\
156 -A <alias>\texpand the given alias\n\
157 -a <file>\tattach a file to the message\n\
158 -b <address>\tspecify a blind carbon-copy (BCC) address\n\
159 -c <address>\tspecify a carbon-copy (CC) address");
161 puts _(" -d <level>\tlog debugging output to ~/.madmuttdebug0");
164 -e <command>\tspecify a command to be executed after initialization\n\
165 -f <file>\tspecify which mailbox to read\n\
166 -F <file>\tspecify an alternate muttrngc file\n\
167 -g <server>\tspecify a newsserver (if compiled with NNTP)\n\
168 -G\t\tselect a newsgroup (if compiled with NNTP)\n\
169 -H <file>\tspecify a draft file to read header and body from\n\
170 -i <file>\tspecify a file which Madmutt should include in the body");
172 -d <level>\t specify debugging level of Madmutt\n\
173 -m <type>\tspecify a default mailbox type\n\
174 -n\t\tcauses Madmutt not to read the system Madmuttrc\n\
175 -p\t\trecall a postponed message\n\
176 -Q <variable>\tquery a configuration variable\n\
177 -R\t\topen mailbox in read-only mode\n\
178 -s <subj>\tspecify a subject (must be in quotes if it has spaces)");
180 -t\t\tprint the value of all variables to stdout\n\
181 -T\t\tprint the value of all changed variables to stdout\n\
182 -v\t\tshow version and compile-time definitions\n\
183 -y\t\tselect a mailbox specified in your `mailboxes' list\n\
184 -z\t\texit immediately if there are no messages in the mailbox\n\
185 -Z\t\topen the first folder with new message, exit immediately if none\n\
186 -h\t\tthis help message");
191 static void show_version (void)
198 puts (mutt_make_version (1));
204 printf ("System:\n %s %s.%s", uts.sysname, uts.version, uts.release);
206 printf ("System:\n SCO %s", uts.release);
208 printf ("System:\n %s %s", uts.sysname, uts.release);
211 printf (" (%s)\nExternal Libraries:\n", uts.machine);
213 #ifdef NCURSES_VERSION
214 printf (" ncurses %s\n", NCURSES_VERSION);
215 #elif defined(USE_SLANG_CURSES)
216 printf (" slang %d\n", SLANG_VERSION);
219 #ifdef _LIBICONV_VERSION
220 printf (" libiconv %d.%d\n", _LIBICONV_VERSION >> 8,
221 _LIBICONV_VERSION & 0xff);
225 printf (" libidn %s (compiled with %s)\n",
226 stringprep_check_version (NULL), STRINGPREP_VERSION);
230 smtp_version (buf, sizeof (buf), 0);
231 printf (" libESMTP %s\n", buf);
236 printf (" qdbm %s\n", dpversion);
238 printf (" gdbm %s\n", gdbm_version);
240 printf (" DB4 %d.%d.%d\n", DB_VERSION_MAJOR, DB_VERSION_MINOR,
246 printf (" gnutls %s\n", LIBGNUTLS_VERSION);
250 printf (" %s\n",OPENSSL_VERSION_TEXT);
253 #ifdef CRYPT_BACKEND_GPGME
254 printf (" gpgme %s\n", GPGME_VERSION);
257 puts (_("Compile Options:"));
340 #ifdef HAVE_START_COLOR
345 #ifdef HAVE_TYPEAHEAD
366 #ifdef HAVE_RESIZETERM
374 #ifdef CRYPT_BACKEND_GPGME
375 "+CRYPT_BACKEND_GPGME "
377 "-CRYPT_BACKEND_GPGME "
395 #ifdef HAVE_LANGINFO_CODESET
396 "+HAVE_LANGINFO_CODESET "
398 "-HAVE_LANGINFO_CODESET "
400 #ifdef HAVE_LANGINFO_YESEXPR
401 "+HAVE_LANGINFO_YESEXPR "
403 "-HAVE_LANGINFO_YESEXPR "
433 puts (_("Built-In Defaults:"));
436 printf (" +ISPELL=\"%s\"\n", ISPELL);
441 printf (" +SENDMAIL=\"%s\"\n", SENDMAIL);
442 printf (" +MAILPATH=\"%s\"\n", MAILPATH);
443 printf (" +PKGDATADIR=\"%s\"\n", PKGDATADIR);
444 printf (" +PKGDOCDIR=\"%s\"\n", PKGDOCDIR);
445 printf (" +SYSCONFDIR=\"%s\"\n", SYSCONFDIR);
447 printf (" +MIXMASTER=\"%s\"\n\n", MIXMASTER);
449 puts (" -MIXMASTER\n");
452 puts (_("MadMutt is based on Madmutt wich was based on Mutt before\n"));
457 static void start_curses (void)
459 km_init (); /* must come before mutt_init */
461 #ifdef USE_SLANG_CURSES
462 SLtt_Ignore_Beep = 1; /* don't do that #*$@^! annoying visual beep! */
463 SLsmg_Display_Eight_Bit = 128; /* characters above this are printable */
464 SLtt_set_color (0, NULL, "default", "default");
466 /* should come before initscr() so that ncurses 4.2 doesn't try to install
467 its own SIGWINCH handler */
470 if (initscr () == NULL) {
471 puts _("Error initializing terminal.");
475 #if 1 /* USE_SLANG_CURSES - commenting out suggested in #455. */
476 /* slang requires the signal handlers to be set after initializing */
480 keypad (stdscr, TRUE);
484 typeahead (-1); /* simulate smooth scrolling */
491 #define M_IGNORE (1<<0) /* -z */
492 #define M_BUFFY (1<<1) /* -Z */
493 #define M_NOSYSRC (1<<2) /* -n */
494 #define M_RO (1<<3) /* -R */
495 #define M_SELECT (1<<4) /* -y */
497 #define M_NEWS (1<<5) /* -g and -G */
500 int main (int argc, char **argv)
502 char folder[_POSIX_PATH_MAX] = "";
503 char *subject = NULL;
504 char *includeFile = NULL;
505 char *draftFile = NULL;
506 char *newMagic = NULL;
509 LIST *commands = NULL;
510 LIST *queries = NULL;
511 LIST *alias_queries = NULL;
516 int explicit_folder = 0;
517 int dump_variables = -1;
519 /* initialize random number for tmp file creation */
520 srand ((unsigned int) time (NULL));
522 /* sanity check against stupid administrators */
524 if (getegid () != getgid ()) {
525 fprintf (stderr, "%s: I don't want to run with privileges!\n", argv[0]);
530 /* FIXME what about init.c:1439 ? */
531 setlocale (LC_ALL, "");
532 bindtextdomain (PACKAGE, MUTTLOCALEDIR);
533 textdomain (PACKAGE);
536 setlocale (LC_CTYPE, "");
538 mutt_error = mutt_nocurses_error;
539 mutt_message = mutt_nocurses_error;
543 p_clear(Options, countof(Options));
544 p_clear(QuadOptions, countof(QuadOptions));
549 "A:a:b:F:f:c:d:e:g:GH:s:i:hm:npQ:RTtvyzZ")) != EOF)
552 getopt (argc, argv, "A:a:b:F:f:c:d:e:H:s:i:hm:npQ:RTtvyzZ")) != EOF)
556 alias_queries = mutt_add_list (alias_queries, optarg);
559 if (strlen(optarg)<=512)
560 attach = mutt_add_list (attach, optarg);
562 printf("too long arguments. exiting ...\n");
568 m_strreplace(&Muttrc, optarg);
572 m_strcpy(folder, sizeof(folder), optarg);
581 msg->env = envelope_new();
583 msg->env->bcc = rfc822_parse_adrlist (msg->env->bcc, optarg);
585 msg->env->cc = rfc822_parse_adrlist (msg->env->cc, optarg);
590 char buf[LONG_STRING];
591 int level = atoi (optarg);
593 snprintf (buf, sizeof (buf), "set debug_level=%d", level);
594 commands = mutt_add_list (commands, buf);
595 debug_setlevel (level);
608 commands = mutt_add_list (commands, optarg);
616 includeFile = optarg;
620 /* should take precedence over .muttrc setting, so save it for later */
629 sendflags |= SENDPOSTPONED;
633 queries = mutt_add_list (queries, optarg);
637 flags |= M_RO; /* read-only mode */
648 case 'y': /* My special hack mode */
653 case 'g': /* Specify a newsserver */
655 char buf[LONG_STRING];
657 snprintf (buf, sizeof (buf), "set nntp_host=%s", optarg);
658 commands = mutt_add_list (commands, buf);
661 case 'G': /* List of newsgroups */
662 flags |= M_SELECT | M_NEWS;
671 flags |= M_BUFFY | M_IGNORE;
685 puts (mutt_make_version (1));
686 puts (_(Copyright_Mutt));
687 puts (_(Copyright_Mutt_NG));
688 puts (_(Copyright_GPL_1));
689 puts (_(Copyright_GPL_2));
690 puts (_(Copyright_GPL_3));
694 /* Check for a batch send. */
695 if (!isatty (0) || queries || alias_queries || dump_variables > 0) {
696 set_option (OPTNOCURSES);
697 sendflags = SENDBATCH;
700 /* This must come before mutt_init() because curses needs to be started
701 before calling the init_pair() function to set the color scheme. */
702 if (!option (OPTNOCURSES))
705 /* set defaults and read init files */
707 mutt_init (flags & M_NOSYSRC, commands);
708 mutt_free_list (&commands);
710 /* Initialize crypto */
714 return mutt_query_variables (queries);
715 if (dump_variables > 0)
716 return (mutt_dump_variables (dump_variables-1));
722 for (; alias_queries; alias_queries = alias_queries->next) {
723 if ((a = mutt_lookup_alias (alias_queries->data))) {
724 /* output in machine-readable form */
725 mutt_addrlist_to_idna (a, NULL);
726 mutt_write_address_list (a, stdout, 0, 0);
730 printf ("%s\n", alias_queries->data);
737 mx_set_magic (newMagic);
739 if (!option (OPTNOCURSES)) {
740 SETCOLOR (MT_COLOR_NORMAL);
742 mutt_error = mutt_curses_error;
743 mutt_message = mutt_curses_message;
746 /* Create the Maildir directory if it doesn't exist. */
747 if (!option (OPTNOCURSES) && Maildir) {
749 char fpath[_POSIX_PATH_MAX];
752 m_strcpy(fpath, sizeof(fpath), Maildir);
753 mutt_expand_path (fpath, sizeof (fpath));
754 /* we're not connected yet - skip mail folder creation */
755 if (mx_get_magic (fpath) != M_IMAP)
756 if (stat (fpath, &sb) == -1 && errno == ENOENT) {
757 snprintf (mesg, sizeof (mesg), _("%s does not exist. Create it?"),
759 if (mutt_yesorno (mesg, M_YES) == M_YES) {
760 if (mkdir (fpath, 0700) == -1 && errno != EEXIST)
761 mutt_error (_("Can't create %s: %s."), Maildir, strerror (errno));
766 if (sendflags & SENDPOSTPONED) {
767 if (!option (OPTNOCURSES))
769 ci_send_message (SENDPOSTPONED, NULL, NULL, NULL, NULL);
772 else if (subject || msg || sendflags || draftFile || includeFile || attach
775 char buf[LONG_STRING];
776 char *tempfile = NULL, *infile = NULL;
777 char *bodytext = NULL;
779 if (!option (OPTNOCURSES))
789 msg->env = envelope_new();
791 for (i = optind; i < argc; i++) {
792 if (url_check_scheme (argv[i]) == U_MAILTO)
793 url_parse_mailto (msg->env, &bodytext, argv[i]);
795 msg->env->to = rfc822_parse_adrlist (msg->env->to, argv[i]);
798 if (option (OPTAUTOEDIT) && !msg->env->to && !msg->env->cc) {
799 if (!option (OPTNOCURSES))
801 fputs (_("No recipients specified.\n"), stderr);
806 msg->env->subject = m_strdup(subject);
809 infile = includeFile;
812 if (infile || bodytext) {
814 if (m_strcmp("-", infile) == 0)
817 char path[_POSIX_PATH_MAX];
819 m_strcpy(path, sizeof(path), infile);
820 mutt_expand_path (path, sizeof (path));
821 if ((fin = fopen (path, "r")) == NULL) {
822 if (!option (OPTNOCURSES))
833 tempfile = m_strdup(buf);
836 msg->env = mutt_read_rfc822_header (fin, NULL, 1, 0);
838 /* is the following if still needed? */
843 if ((fout = safe_fopen (tempfile, "w")) == NULL) {
844 if (!option (OPTNOCURSES))
852 mutt_copy_stream (fin, fout);
854 fputs (bodytext, fout);
856 if (fin && fin != stdin)
869 a->next = mutt_make_file_attach (t->data);
873 msg->content = a = mutt_make_file_attach (t->data);
875 if (!option (OPTNOCURSES))
877 fprintf (stderr, _("%s: unable to attach file.\n"), t->data);
878 mutt_free_list (&attach);
883 mutt_free_list (&attach);
886 ci_send_message (sendflags, msg, tempfile, NULL, NULL);
888 if (!option (OPTNOCURSES))
892 if (flags & M_BUFFY) {
893 if (!buffy_check (0)) {
894 mutt_endwin _("No mailbox with new mail.");
899 buffy_next (folder, sizeof (folder));
901 else if (flags & M_SELECT) {
903 if (flags & M_NEWS) {
904 set_option (OPTNEWS);
905 if (!(CurrentNewsSrv = mutt_select_newsserver (NewsServer))) {
906 mutt_endwin (Errorbuf);
913 mutt_endwin _("No incoming mailboxes defined.");
918 mutt_select_file (folder, sizeof (folder), M_SEL_FOLDER | M_SEL_BUFFY);
926 m_strcpy(folder, sizeof(folder), NONULL(Spoolfile));
929 if (option (OPTNEWS)) {
930 unset_option (OPTNEWS);
931 nntp_expand_path (folder, sizeof (folder),
932 &CurrentNewsSrv->conn->account);
936 mutt_expand_path (folder, sizeof (folder));
938 m_strreplace(&CurrentFolder, folder);
939 m_strreplace(&LastFolder, folder);
941 if (flags & M_IGNORE) {
942 /* check to see if there are any messages in the folder */
943 switch (mx_check_empty (folder)) {
945 mutt_endwin (strerror (errno));
948 mutt_endwin _("Mailbox is empty.");
953 mutt_folder_hook (folder);
955 if ((Context = mx_open_mailbox (folder, ((flags & M_RO)
956 || option (OPTREADONLY)) ?
957 M_READONLY : 0, NULL))
958 || !explicit_folder) {
960 if (option (OPTXTERMSETTITLES))
961 mutt_xterm_set_title (NONULL (XtermLeave));
965 mutt_endwin (Errorbuf);