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>
35 #include <lib-lib/url.h>
37 #include <lib-sys/mutt_signal.h>
39 #include <lib-mime/mime.h>
41 #include <lib-ui/curses.h>
48 #include <lib-crypt/crypt.h>
50 #include "mutt_idna.h"
61 #include <stringprep.h>
65 #include "nntp/nntp.h"
73 #if defined(HAVE_QDBM)
75 #elif defined(HAVE_GDBM)
77 #elif defined(HAVE_DB4)
83 #include <gnutls/gnutls.h>
86 #ifdef CRYPT_BACKEND_GPGME
91 #include <openssl/opensslv.h>
94 static const char *Notice = N_("\
95 Copyright (C) 1996-2002 Michael R. Elkins and others.\n\
96 Madmutt comes with ABSOLUTELY NO WARRANTY; for details type `madmutt -vv'.\n\
97 Mutt is free software, and you are welcome to redistribute it\n\
98 under certain conditions; type `madmutt -vv' for details.\n");
100 static const char *Copyright_Mutt = N_("\
101 Copyright (C) 1996-2004 Michael R. Elkins <me@mutt.org>\n\
102 Copyright (C) 1996-2002 Brandon Long <blong@fiction.net>\n\
103 Copyright (C) 1997-2005 Thomas Roessler <roessler@does-not-exist.org>\n\
104 Copyright (C) 1998-2005 Werner Koch <wk@isil.d.shuttle.de>\n\
105 Copyright (C) 1999-2005 Brendan Cully <brendan@kublai.com>\n\
106 Copyright (C) 1999-2002 Tommi Komulainen <Tommi.Komulainen@iki.fi>\n\
107 Copyright (C) 2000-2002 Edmund Grimley Evans <edmundo@rano.org>\n");
109 static const char *Copyright_Mutt_NG = N_("\
110 Copyright (C) 2005:\n\
111 Parts were written/modified by:\n\
112 Andreas Krennmair <ak@synflood.at>\n\
113 Nico Golde <nico@ngolde.de>\n\
114 Rocco Rutte <pdmef@cs.tu-berlin.de>\n\
116 Lots of others not mentioned here contributed lots of code,\n\
117 fixes, and suggestions.");
119 static const char* Copyright_GPL_1 = N_("\
121 This program is free software; you can redistribute it and/or modify\n\
122 it under the terms of the GNU General Public License as published by\n\
123 the Free Software Foundation; either version 2 of the License, or\n\
124 (at your option) any later version.\n");
126 static const char* Copyright_GPL_2 = N_("\
127 This program is distributed in the hope that it will be useful,\n\
128 but WITHOUT ANY WARRANTY; without even the implied warranty of\n\
129 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n\
130 GNU General Public License for more details.\n");
132 static const char* Copyright_GPL_3 = N_("\
133 You should have received a copy of the GNU General Public License\n\
134 along with this program; if not, write to the Free Software\n\
135 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.\n\
138 void mutt_exit (int code)
144 static void mutt_usage (void)
146 puts (mutt_make_version (1));
149 _("usage: madmutt [ -nRyzZ ] [ -e <cmd> ] [ -F <file> ] [ -m <type> ] [ -f <file> ]\n\
150 madmutt [ -nR ] [ -e <cmd> ] [ -F <file> ] -Q <query> [ -Q <query> ] [...]\n\
151 madmutt [ -nR ] [ -e <cmd> ] [ -F <file> ] -A <alias> [ -A <alias> ] [...]\n\
152 madmutt [ -nR ] [ -e <cmd> ] [ -F <file> ] -t");
155 madmutt [ -nR ] [ -e <cmd> ] [ -F <file> ] -T\n\
156 madmutt [ -nx ] [ -e <cmd> ] [ -a <file> ] [ -F <file> ] [ -H <file> ] [ -i <file> ] [ -s <subj> ] [ -b <addr> ] [ -c <addr> ] <addr> [ ... ]\n\
157 madmutt [ -n ] [ -e <cmd> ] [ -F <file> ] -p\n\
161 -A <alias>\texpand the given alias\n\
162 -a <file>\tattach a file to the message\n\
163 -b <address>\tspecify a blind carbon-copy (BCC) address\n\
164 -c <address>\tspecify a carbon-copy (CC) address");
166 puts _(" -d <level>\tlog debugging output to ~/.madmuttdebug0");
169 -e <command>\tspecify a command to be executed after initialization\n\
170 -f <file>\tspecify which mailbox to read\n\
171 -F <file>\tspecify an alternate muttrngc file\n\
172 -g <server>\tspecify a newsserver (if compiled with NNTP)\n\
173 -G\t\tselect a newsgroup (if compiled with NNTP)\n\
174 -H <file>\tspecify a draft file to read header and body from\n\
175 -i <file>\tspecify a file which Madmutt should include in the body");
177 -d <level>\t specify debugging level of Madmutt\n\
178 -m <type>\tspecify a default mailbox type\n\
179 -n\t\tcauses Madmutt not to read the system Madmuttrc\n\
180 -p\t\trecall a postponed message\n\
181 -Q <variable>\tquery a configuration variable\n\
182 -R\t\topen mailbox in read-only mode\n\
183 -s <subj>\tspecify a subject (must be in quotes if it has spaces)");
185 -t\t\tprint the value of all variables to stdout\n\
186 -T\t\tprint the value of all changed variables to stdout\n\
187 -v\t\tshow version and compile-time definitions\n\
188 -y\t\tselect a mailbox specified in your `mailboxes' list\n\
189 -z\t\texit immediately if there are no messages in the mailbox\n\
190 -Z\t\topen the first folder with new message, exit immediately if none\n\
191 -h\t\tthis help message");
196 static void show_version (void)
203 puts (mutt_make_version (1));
209 printf ("System:\n %s %s.%s", uts.sysname, uts.version, uts.release);
211 printf ("System:\n SCO %s", uts.release);
213 printf ("System:\n %s %s", uts.sysname, uts.release);
216 printf (" (%s)\nExternal Libraries:\n", uts.machine);
218 #ifdef NCURSES_VERSION
219 printf (" ncurses %s\n", NCURSES_VERSION);
220 #elif defined(USE_SLANG_CURSES)
221 printf (" slang %d\n", SLANG_VERSION);
224 #ifdef _LIBICONV_VERSION
225 printf (" libiconv %d.%d\n", _LIBICONV_VERSION >> 8,
226 _LIBICONV_VERSION & 0xff);
230 printf (" libidn %s (compiled with %s)\n",
231 stringprep_check_version (NULL), STRINGPREP_VERSION);
235 smtp_version (buf, sizeof (buf), 0);
236 printf (" libESMTP %s\n", buf);
240 #if defined(HAVE_QDBM)
241 printf (" qdbm %s\n", dpversion);
242 #elif defined(HAVE_GDBM)
243 printf (" gdbm %s\n", gdbm_version);
244 #elif defined(HAVE_DB4)
245 printf (" DB4 %d.%d.%d\n", DB_VERSION_MAJOR, DB_VERSION_MINOR,
251 printf (" gnutls %s\n", LIBGNUTLS_VERSION);
255 printf (" %s\n",OPENSSL_VERSION_TEXT);
258 #ifdef CRYPT_BACKEND_GPGME
259 printf (" gpgme %s\n", GPGME_VERSION);
262 puts (_("Compile Options:"));
345 #ifdef HAVE_START_COLOR
350 #ifdef HAVE_TYPEAHEAD
371 #ifdef HAVE_RESIZETERM
379 #ifdef CRYPT_BACKEND_GPGME
380 "+CRYPT_BACKEND_GPGME "
382 "-CRYPT_BACKEND_GPGME "
400 #ifdef HAVE_LANGINFO_CODESET
401 "+HAVE_LANGINFO_CODESET "
403 "-HAVE_LANGINFO_CODESET "
405 #ifdef HAVE_LANGINFO_YESEXPR
406 "+HAVE_LANGINFO_YESEXPR "
408 "-HAVE_LANGINFO_YESEXPR "
431 #ifdef HAVE_GETADDRINFO
438 puts (_("Built-In Defaults:"));
441 printf (" +ISPELL=\"%s\"\n", ISPELL);
446 printf (" +SENDMAIL=\"%s\"\n", SENDMAIL);
447 printf (" +MAILPATH=\"%s\"\n", MAILPATH);
448 printf (" +PKGDATADIR=\"%s\"\n", PKGDATADIR);
449 printf (" +PKGDOCDIR=\"%s\"\n", PKGDOCDIR);
450 printf (" +SYSCONFDIR=\"%s\"\n", SYSCONFDIR);
452 printf (" +MIXMASTER=\"%s\"\n\n", MIXMASTER);
454 puts (" -MIXMASTER\n");
457 puts (_("MadMutt is based on Madmutt wich was based on Mutt before\n"));
462 static void start_curses (void)
464 km_init (); /* must come before mutt_init */
466 #ifdef USE_SLANG_CURSES
467 SLtt_Ignore_Beep = 1; /* don't do that #*$@^! annoying visual beep! */
468 SLsmg_Display_Eight_Bit = 128; /* characters above this are printable */
469 SLtt_set_color (0, NULL, "default", "default");
471 /* should come before initscr() so that ncurses 4.2 doesn't try to install
472 its own SIGWINCH handler */
473 mutt_signal_initialize ();
475 if (initscr () == NULL) {
476 puts _("Error initializing terminal.");
480 mutt_signal_initialize ();
482 keypad (stdscr, TRUE);
485 #ifdef HAVE_TYPEAHEAD
486 typeahead (-1); /* simulate smooth scrolling */
493 #define M_IGNORE (1<<0) /* -z */
494 #define M_BUFFY (1<<1) /* -Z */
495 #define M_NOSYSRC (1<<2) /* -n */
496 #define M_RO (1<<3) /* -R */
497 #define M_SELECT (1<<4) /* -y */
499 #define M_NEWS (1<<5) /* -g and -G */
502 int main (int argc, char **argv)
504 char folder[_POSIX_PATH_MAX] = "";
505 char *subject = NULL;
506 char *includeFile = NULL;
507 char *draftFile = NULL;
508 char *newMagic = NULL;
510 string_list_t *attach = NULL;
511 string_list_t *commands = NULL;
512 string_list_t *queries = NULL;
513 string_list_t *alias_queries = NULL;
518 int explicit_folder = 0;
519 int dump_variables = -1;
521 /* initialize random number for tmp file creation */
522 srand48((unsigned int) time (NULL));
524 /* sanity check against stupid administrators */
526 if (getegid () != getgid ()) {
527 fprintf (stderr, "%s: I don't want to run with privileges!\n", argv[0]);
532 /* FIXME what about init.c:1439 ? */
533 setlocale (LC_ALL, "");
534 bindtextdomain (PACKAGE, MUTTLOCALEDIR);
535 textdomain (PACKAGE);
538 setlocale (LC_CTYPE, "");
540 mutt_error = mutt_nocurses_error;
541 mutt_message = mutt_nocurses_error;
542 srand48 (time (NULL));
545 p_clear(Options, countof(Options));
546 p_clear(QuadOptions, countof(QuadOptions));
551 "A:a:b:F:f:c:d:e:g:GH:s:i:hm:npQ:RTtvyzZ")) != EOF)
554 getopt (argc, argv, "A:a:b:F:f:c:d:e:H:s:i:hm:npQ:RTtvyzZ")) != EOF)
558 alias_queries = mutt_add_list (alias_queries, optarg);
561 if (strlen(optarg)<=512)
562 attach = mutt_add_list (attach, optarg);
564 printf("too long arguments. exiting ...\n");
570 m_strreplace(&Muttrc, optarg);
574 m_strcpy(folder, sizeof(folder), optarg);
583 msg->env = envelope_new();
585 msg->env->bcc = rfc822_parse_adrlist (msg->env->bcc, optarg);
587 msg->env->cc = rfc822_parse_adrlist (msg->env->cc, optarg);
592 char buf[LONG_STRING];
593 int level = atoi (optarg);
595 snprintf (buf, sizeof (buf), "set debug_level=%d", level);
596 commands = mutt_add_list (commands, buf);
597 debug_setlevel (level);
610 commands = mutt_add_list (commands, optarg);
618 includeFile = optarg;
622 /* should take precedence over .muttrc setting, so save it for later */
631 sendflags |= SENDPOSTPONED;
635 queries = mutt_add_list (queries, optarg);
639 flags |= M_RO; /* read-only mode */
650 case 'y': /* My special hack mode */
655 case 'g': /* Specify a newsserver */
657 char buf[LONG_STRING];
659 snprintf (buf, sizeof (buf), "set nntp_host=%s", optarg);
660 commands = mutt_add_list (commands, buf);
663 case 'G': /* List of newsgroups */
664 flags |= M_SELECT | M_NEWS;
673 flags |= M_BUFFY | M_IGNORE;
687 puts (mutt_make_version (1));
688 puts (_(Copyright_Mutt));
689 puts (_(Copyright_Mutt_NG));
690 puts (_(Copyright_GPL_1));
691 puts (_(Copyright_GPL_2));
692 puts (_(Copyright_GPL_3));
696 /* Check for a batch send. */
697 if (!isatty (0) || queries || alias_queries || dump_variables > 0) {
698 set_option (OPTNOCURSES);
699 sendflags = SENDBATCH;
702 /* This must come before mutt_init() because curses needs to be started
703 before calling the init_pair() function to set the color scheme. */
704 if (!option (OPTNOCURSES))
707 /* set defaults and read init files */
709 mutt_init (flags & M_NOSYSRC, commands);
710 string_list_wipe(&commands);
712 /* Initialize crypto */
716 return mutt_query_variables (queries);
717 if (dump_variables > 0)
718 return (mutt_dump_variables (dump_variables-1));
724 for (; alias_queries; alias_queries = alias_queries->next) {
725 if ((a = address_list_dup(alias_lookup(alias_queries->data)))) {
726 /* output in machine-readable form */
727 mutt_addrlist_to_idna (a, NULL);
728 mutt_write_address_list (a, stdout, 0, 0);
729 address_list_wipe(&a);
732 printf ("%s\n", alias_queries->data);
739 mx_set_magic (newMagic);
741 if (!option (OPTNOCURSES)) {
742 SETCOLOR (MT_COLOR_NORMAL);
744 mutt_error = mutt_curses_error;
745 mutt_message = mutt_curses_message;
748 /* Create the Maildir directory if it doesn't exist. */
749 if (!option (OPTNOCURSES) && Maildir) {
751 char fpath[_POSIX_PATH_MAX];
754 m_strcpy(fpath, sizeof(fpath), Maildir);
755 mutt_expand_path (fpath, sizeof (fpath));
756 /* we're not connected yet - skip mail folder creation */
757 if (mx_get_magic (fpath) != M_IMAP)
758 if (stat (fpath, &sb) == -1 && errno == ENOENT) {
759 snprintf (mesg, sizeof (mesg), _("%s does not exist. Create it?"),
761 if (mutt_yesorno (mesg, M_YES) == M_YES) {
762 if (mkdir (fpath, 0700) == -1 && errno != EEXIST)
763 mutt_error (_("Can't create %s: %s."), Maildir, strerror (errno));
768 if (sendflags & SENDPOSTPONED) {
769 if (!option (OPTNOCURSES))
771 ci_send_message (SENDPOSTPONED, NULL, NULL, NULL, NULL);
774 else if (subject || msg || sendflags || draftFile || includeFile || attach
777 char buf[LONG_STRING];
778 char *tempfile = NULL, *infile = NULL;
779 char *bodytext = NULL;
781 if (!option (OPTNOCURSES))
791 msg->env = envelope_new();
793 for (i = optind; i < argc; i++) {
794 if (url_check_scheme (argv[i]) == U_MAILTO)
795 url_parse_mailto (msg->env, &bodytext, argv[i]);
797 msg->env->to = rfc822_parse_adrlist (msg->env->to, argv[i]);
800 if (option (OPTAUTOEDIT) && !msg->env->to && !msg->env->cc) {
801 if (!option (OPTNOCURSES))
803 fputs (_("No recipients specified.\n"), stderr);
808 msg->env->subject = m_strdup(subject);
811 infile = includeFile;
814 if (infile || bodytext) {
816 if (m_strcmp("-", infile) == 0)
819 char path[_POSIX_PATH_MAX];
821 m_strcpy(path, sizeof(path), infile);
822 mutt_expand_path (path, sizeof (path));
823 if ((fin = fopen (path, "r")) == NULL) {
824 if (!option (OPTNOCURSES))
835 tempfile = m_strdup(buf);
838 msg->env = mutt_read_rfc822_header (fin, NULL, 1, 0);
840 /* is the following if still needed? */
845 if ((fout = safe_fopen (tempfile, "w")) == NULL) {
846 if (!option (OPTNOCURSES))
854 mutt_copy_stream (fin, fout);
856 fputs (bodytext, fout);
858 if (fin && fin != stdin)
866 string_list_t *t = attach;
871 a->next = mutt_make_file_attach (t->data);
875 msg->content = a = mutt_make_file_attach (t->data);
877 if (!option (OPTNOCURSES))
879 fprintf (stderr, _("%s: unable to attach file.\n"), t->data);
880 string_list_wipe(&attach);
885 string_list_wipe(&attach);
888 ci_send_message (sendflags, msg, tempfile, NULL, NULL);
890 if (!option (OPTNOCURSES))
894 if (flags & M_BUFFY) {
895 if (!buffy_check (0)) {
896 mutt_endwin _("No mailbox with new mail.");
901 buffy_next (folder, sizeof (folder));
903 else if (flags & M_SELECT) {
905 if (flags & M_NEWS) {
906 set_option (OPTNEWS);
907 if (!(CurrentNewsSrv = mutt_select_newsserver (NewsServer))) {
908 mutt_endwin (Errorbuf);
915 mutt_endwin _("No incoming mailboxes defined.");
920 mutt_select_file (folder, sizeof (folder), M_SEL_FOLDER | M_SEL_BUFFY);
928 m_strcpy(folder, sizeof(folder), NONULL(Spoolfile));
931 if (option (OPTNEWS)) {
932 unset_option (OPTNEWS);
933 nntp_expand_path (folder, sizeof (folder),
934 &CurrentNewsSrv->conn->account);
938 mutt_expand_path (folder, sizeof (folder));
940 m_strreplace(&CurrentFolder, folder);
941 m_strreplace(&LastFolder, folder);
943 if (flags & M_IGNORE) {
944 /* check to see if there are any messages in the folder */
945 switch (mx_check_empty (folder)) {
947 mutt_endwin (strerror (errno));
950 mutt_endwin _("Mailbox is empty.");
955 mutt_folder_hook (folder);
957 if ((Context = mx_open_mailbox (folder, ((flags & M_RO)
958 || option (OPTREADONLY)) ?
959 M_READONLY : 0, NULL))
960 || !explicit_folder) {
962 if (option (OPTXTERMSETTITLES))
963 mutt_xterm_set_title (NONULL (XtermLeave));
967 mutt_endwin (Errorbuf);