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>
47 #include <lib-crypt/crypt.h>
49 #include "mutt_idna.h"
60 #include <stringprep.h>
64 #include "nntp/nntp.h"
72 #if defined(HAVE_QDBM)
74 #elif defined(HAVE_GDBM)
76 #elif defined(HAVE_DB4)
82 #include <gnutls/gnutls.h>
85 #ifdef CRYPT_BACKEND_GPGME
90 #include <openssl/opensslv.h>
93 static const char *Notice = N_("\
94 Copyright (C) 1996-2002 Michael R. Elkins and others.\n\
95 Madmutt comes with ABSOLUTELY NO WARRANTY; for details type `madmutt -vv'.\n\
96 Mutt is free software, and you are welcome to redistribute it\n\
97 under certain conditions; type `madmutt -vv' for details.\n");
99 static const char *Copyright_Mutt = N_("\
100 Copyright (C) 1996-2004 Michael R. Elkins <me@mutt.org>\n\
101 Copyright (C) 1996-2002 Brandon Long <blong@fiction.net>\n\
102 Copyright (C) 1997-2005 Thomas Roessler <roessler@does-not-exist.org>\n\
103 Copyright (C) 1998-2005 Werner Koch <wk@isil.d.shuttle.de>\n\
104 Copyright (C) 1999-2005 Brendan Cully <brendan@kublai.com>\n\
105 Copyright (C) 1999-2002 Tommi Komulainen <Tommi.Komulainen@iki.fi>\n\
106 Copyright (C) 2000-2002 Edmund Grimley Evans <edmundo@rano.org>\n");
108 static const char *Copyright_Mutt_NG = N_("\
109 Copyright (C) 2005:\n\
110 Parts were written/modified by:\n\
111 Andreas Krennmair <ak@synflood.at>\n\
112 Nico Golde <nico@ngolde.de>\n\
113 Rocco Rutte <pdmef@cs.tu-berlin.de>\n\
115 Lots of others not mentioned here contributed lots of code,\n\
116 fixes, and suggestions.");
118 static const char* Copyright_GPL_1 = N_("\
120 This program is free software; you can redistribute it and/or modify\n\
121 it under the terms of the GNU General Public License as published by\n\
122 the Free Software Foundation; either version 2 of the License, or\n\
123 (at your option) any later version.\n");
125 static const char* Copyright_GPL_2 = N_("\
126 This program is distributed in the hope that it will be useful,\n\
127 but WITHOUT ANY WARRANTY; without even the implied warranty of\n\
128 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n\
129 GNU General Public License for more details.\n");
131 static const char* Copyright_GPL_3 = N_("\
132 You should have received a copy of the GNU General Public License\n\
133 along with this program; if not, write to the Free Software\n\
134 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.\n\
137 void mutt_exit (int code)
143 static void mutt_usage (void)
145 puts (mutt_make_version (1));
148 _("usage: madmutt [ -nRyzZ ] [ -e <cmd> ] [ -F <file> ] [ -m <type> ] [ -f <file> ]\n\
149 madmutt [ -nR ] [ -e <cmd> ] [ -F <file> ] -Q <query> [ -Q <query> ] [...]\n\
150 madmutt [ -nR ] [ -e <cmd> ] [ -F <file> ] -A <alias> [ -A <alias> ] [...]\n\
151 madmutt [ -nR ] [ -e <cmd> ] [ -F <file> ] -t");
154 madmutt [ -nR ] [ -e <cmd> ] [ -F <file> ] -T\n\
155 madmutt [ -nx ] [ -e <cmd> ] [ -a <file> ] [ -F <file> ] [ -H <file> ] [ -i <file> ] [ -s <subj> ] [ -b <addr> ] [ -c <addr> ] <addr> [ ... ]\n\
156 madmutt [ -n ] [ -e <cmd> ] [ -F <file> ] -p\n\
160 -A <alias>\texpand the given alias\n\
161 -a <file>\tattach a file to the message\n\
162 -b <address>\tspecify a blind carbon-copy (BCC) address\n\
163 -c <address>\tspecify a carbon-copy (CC) address");
165 puts _(" -d <level>\tlog debugging output to ~/.madmuttdebug0");
168 -e <command>\tspecify a command to be executed after initialization\n\
169 -f <file>\tspecify which mailbox to read\n\
170 -F <file>\tspecify an alternate muttrngc file\n\
171 -g <server>\tspecify a newsserver (if compiled with NNTP)\n\
172 -G\t\tselect a newsgroup (if compiled with NNTP)\n\
173 -H <file>\tspecify a draft file to read header and body from\n\
174 -i <file>\tspecify a file which Madmutt should include in the body");
176 -d <level>\t specify debugging level of Madmutt\n\
177 -m <type>\tspecify a default mailbox type\n\
178 -n\t\tcauses Madmutt not to read the system Madmuttrc\n\
179 -p\t\trecall a postponed message\n\
180 -Q <variable>\tquery a configuration variable\n\
181 -R\t\topen mailbox in read-only mode\n\
182 -s <subj>\tspecify a subject (must be in quotes if it has spaces)");
184 -t\t\tprint the value of all variables to stdout\n\
185 -T\t\tprint the value of all changed variables to stdout\n\
186 -v\t\tshow version and compile-time definitions\n\
187 -y\t\tselect a mailbox specified in your `mailboxes' list\n\
188 -z\t\texit immediately if there are no messages in the mailbox\n\
189 -Z\t\topen the first folder with new message, exit immediately if none\n\
190 -h\t\tthis help message");
195 static void show_version (void)
202 puts (mutt_make_version (1));
208 printf ("System:\n %s %s.%s", uts.sysname, uts.version, uts.release);
210 printf ("System:\n SCO %s", uts.release);
212 printf ("System:\n %s %s", uts.sysname, uts.release);
215 printf (" (%s)\nExternal Libraries:\n", uts.machine);
217 #ifdef NCURSES_VERSION
218 printf (" ncurses %s\n", NCURSES_VERSION);
219 #elif defined(USE_SLANG_CURSES)
220 printf (" slang %d\n", SLANG_VERSION);
223 #ifdef _LIBICONV_VERSION
224 printf (" libiconv %d.%d\n", _LIBICONV_VERSION >> 8,
225 _LIBICONV_VERSION & 0xff);
229 printf (" libidn %s (compiled with %s)\n",
230 stringprep_check_version (NULL), STRINGPREP_VERSION);
234 smtp_version (buf, sizeof (buf), 0);
235 printf (" libESMTP %s\n", buf);
239 #if defined(HAVE_QDBM)
240 printf (" qdbm %s\n", dpversion);
241 #elif defined(HAVE_GDBM)
242 printf (" gdbm %s\n", gdbm_version);
243 #elif defined(HAVE_DB4)
244 printf (" DB4 %d.%d.%d\n", DB_VERSION_MAJOR, DB_VERSION_MINOR,
250 printf (" gnutls %s\n", LIBGNUTLS_VERSION);
254 printf (" %s\n",OPENSSL_VERSION_TEXT);
257 #ifdef CRYPT_BACKEND_GPGME
258 printf (" gpgme %s\n", GPGME_VERSION);
261 puts (_("Compile Options:"));
344 #ifdef HAVE_START_COLOR
349 #ifdef HAVE_TYPEAHEAD
370 #ifdef HAVE_RESIZETERM
378 #ifdef CRYPT_BACKEND_GPGME
379 "+CRYPT_BACKEND_GPGME "
381 "-CRYPT_BACKEND_GPGME "
399 #ifdef HAVE_LANGINFO_CODESET
400 "+HAVE_LANGINFO_CODESET "
402 "-HAVE_LANGINFO_CODESET "
404 #ifdef HAVE_LANGINFO_YESEXPR
405 "+HAVE_LANGINFO_YESEXPR "
407 "-HAVE_LANGINFO_YESEXPR "
430 #ifdef HAVE_GETADDRINFO
437 puts (_("Built-In Defaults:"));
440 printf (" +ISPELL=\"%s\"\n", ISPELL);
445 printf (" +SENDMAIL=\"%s\"\n", SENDMAIL);
446 printf (" +MAILPATH=\"%s\"\n", MAILPATH);
447 printf (" +PKGDATADIR=\"%s\"\n", PKGDATADIR);
448 printf (" +PKGDOCDIR=\"%s\"\n", PKGDOCDIR);
449 printf (" +SYSCONFDIR=\"%s\"\n", SYSCONFDIR);
451 printf (" +MIXMASTER=\"%s\"\n\n", MIXMASTER);
453 puts (" -MIXMASTER\n");
456 puts (_("MadMutt is based on Madmutt wich was based on Mutt before\n"));
461 static void start_curses (void)
463 km_init (); /* must come before mutt_init */
465 #ifdef USE_SLANG_CURSES
466 SLtt_Ignore_Beep = 1; /* don't do that #*$@^! annoying visual beep! */
467 SLsmg_Display_Eight_Bit = 128; /* characters above this are printable */
468 SLtt_set_color (0, NULL, "default", "default");
470 /* should come before initscr() so that ncurses 4.2 doesn't try to install
471 its own SIGWINCH handler */
472 mutt_signal_initialize ();
474 if (initscr () == NULL) {
475 puts _("Error initializing terminal.");
479 mutt_signal_initialize ();
481 keypad (stdscr, TRUE);
484 #ifdef HAVE_TYPEAHEAD
485 typeahead (-1); /* simulate smooth scrolling */
492 #define M_IGNORE (1<<0) /* -z */
493 #define M_BUFFY (1<<1) /* -Z */
494 #define M_NOSYSRC (1<<2) /* -n */
495 #define M_RO (1<<3) /* -R */
496 #define M_SELECT (1<<4) /* -y */
498 #define M_NEWS (1<<5) /* -g and -G */
501 int main (int argc, char **argv)
503 char folder[_POSIX_PATH_MAX] = "";
504 char *subject = NULL;
505 char *includeFile = NULL;
506 char *draftFile = NULL;
507 char *newMagic = NULL;
509 string_list_t *attach = NULL;
510 string_list_t *commands = NULL;
511 string_list_t *queries = NULL;
512 string_list_t *alias_queries = NULL;
517 int explicit_folder = 0;
518 int dump_variables = -1;
520 /* initialize random number for tmp file creation */
521 srand48((unsigned int) time (NULL));
523 /* sanity check against stupid administrators */
525 if (getegid () != getgid ()) {
526 fprintf (stderr, "%s: I don't want to run with privileges!\n", argv[0]);
531 /* FIXME what about init.c:1439 ? */
532 setlocale (LC_ALL, "");
533 bindtextdomain (PACKAGE, MUTTLOCALEDIR);
534 textdomain (PACKAGE);
537 setlocale (LC_CTYPE, "");
539 mutt_error = mutt_nocurses_error;
540 mutt_message = mutt_nocurses_error;
541 srand48 (time (NULL));
544 p_clear(Options, countof(Options));
545 p_clear(QuadOptions, countof(QuadOptions));
550 "A:a:b:F:f:c:d:e:g:GH:s:i:hm:npQ:RTtvyzZ")) != EOF)
553 getopt (argc, argv, "A:a:b:F:f:c:d:e:H:s:i:hm:npQ:RTtvyzZ")) != EOF)
557 alias_queries = mutt_add_list (alias_queries, optarg);
560 if (strlen(optarg)<=512)
561 attach = mutt_add_list (attach, optarg);
563 printf("too long arguments. exiting ...\n");
569 m_strreplace(&Muttrc, optarg);
573 m_strcpy(folder, sizeof(folder), optarg);
582 msg->env = envelope_new();
584 msg->env->bcc = rfc822_parse_adrlist (msg->env->bcc, optarg);
586 msg->env->cc = rfc822_parse_adrlist (msg->env->cc, optarg);
591 char buf[LONG_STRING];
592 int level = atoi (optarg);
594 snprintf (buf, sizeof (buf), "set debug_level=%d", level);
595 commands = mutt_add_list (commands, buf);
596 debug_setlevel (level);
609 commands = mutt_add_list (commands, optarg);
617 includeFile = optarg;
621 /* should take precedence over .muttrc setting, so save it for later */
630 sendflags |= SENDPOSTPONED;
634 queries = mutt_add_list (queries, optarg);
638 flags |= M_RO; /* read-only mode */
649 case 'y': /* My special hack mode */
654 case 'g': /* Specify a newsserver */
656 char buf[LONG_STRING];
658 snprintf (buf, sizeof (buf), "set nntp_host=%s", optarg);
659 commands = mutt_add_list (commands, buf);
662 case 'G': /* List of newsgroups */
663 flags |= M_SELECT | M_NEWS;
672 flags |= M_BUFFY | M_IGNORE;
686 puts (mutt_make_version (1));
687 puts (_(Copyright_Mutt));
688 puts (_(Copyright_Mutt_NG));
689 puts (_(Copyright_GPL_1));
690 puts (_(Copyright_GPL_2));
691 puts (_(Copyright_GPL_3));
695 /* Check for a batch send. */
696 if (!isatty (0) || queries || alias_queries || dump_variables > 0) {
697 set_option (OPTNOCURSES);
698 sendflags = SENDBATCH;
701 /* This must come before mutt_init() because curses needs to be started
702 before calling the init_pair() function to set the color scheme. */
703 if (!option (OPTNOCURSES))
706 /* set defaults and read init files */
708 mutt_init (flags & M_NOSYSRC, commands);
709 string_list_wipe(&commands);
711 /* Initialize crypto */
715 return mutt_query_variables (queries);
716 if (dump_variables > 0)
717 return (mutt_dump_variables (dump_variables-1));
723 for (; alias_queries; alias_queries = alias_queries->next) {
724 if ((a = address_list_dup(alias_lookup(alias_queries->data)))) {
725 /* output in machine-readable form */
726 mutt_addrlist_to_idna (a, NULL);
727 mutt_write_address_list (a, stdout, 0, 0);
728 address_list_wipe(&a);
731 printf ("%s\n", alias_queries->data);
738 mx_set_magic (newMagic);
740 if (!option (OPTNOCURSES)) {
741 SETCOLOR (MT_COLOR_NORMAL);
743 mutt_error = mutt_curses_error;
744 mutt_message = mutt_curses_message;
747 /* Create the Maildir directory if it doesn't exist. */
748 if (!option (OPTNOCURSES) && Maildir) {
750 char fpath[_POSIX_PATH_MAX];
753 m_strcpy(fpath, sizeof(fpath), Maildir);
754 mutt_expand_path (fpath, sizeof (fpath));
755 /* we're not connected yet - skip mail folder creation */
756 if (mx_get_magic (fpath) != M_IMAP)
757 if (stat (fpath, &sb) == -1 && errno == ENOENT) {
758 snprintf (mesg, sizeof (mesg), _("%s does not exist. Create it?"),
760 if (mutt_yesorno (mesg, M_YES) == M_YES) {
761 if (mkdir (fpath, 0700) == -1 && errno != EEXIST)
762 mutt_error (_("Can't create %s: %s."), Maildir, strerror (errno));
767 if (sendflags & SENDPOSTPONED) {
768 if (!option (OPTNOCURSES))
770 ci_send_message (SENDPOSTPONED, NULL, NULL, NULL, NULL);
773 else if (subject || msg || sendflags || draftFile || includeFile || attach
776 char buf[LONG_STRING];
777 char *tempfile = NULL, *infile = NULL;
778 char *bodytext = NULL;
780 if (!option (OPTNOCURSES))
790 msg->env = envelope_new();
792 for (i = optind; i < argc; i++) {
793 if (url_check_scheme (argv[i]) == U_MAILTO)
794 url_parse_mailto (msg->env, &bodytext, argv[i]);
796 msg->env->to = rfc822_parse_adrlist (msg->env->to, argv[i]);
799 if (option (OPTAUTOEDIT) && !msg->env->to && !msg->env->cc) {
800 if (!option (OPTNOCURSES))
802 fputs (_("No recipients specified.\n"), stderr);
807 msg->env->subject = m_strdup(subject);
810 infile = includeFile;
813 if (infile || bodytext) {
815 if (m_strcmp("-", infile) == 0)
818 char path[_POSIX_PATH_MAX];
820 m_strcpy(path, sizeof(path), infile);
821 mutt_expand_path (path, sizeof (path));
822 if ((fin = fopen (path, "r")) == NULL) {
823 if (!option (OPTNOCURSES))
834 tempfile = m_strdup(buf);
837 msg->env = mutt_read_rfc822_header (fin, NULL, 1, 0);
839 /* is the following if still needed? */
844 if ((fout = safe_fopen (tempfile, "w")) == NULL) {
845 if (!option (OPTNOCURSES))
853 mutt_copy_stream (fin, fout);
855 fputs (bodytext, fout);
857 if (fin && fin != stdin)
865 string_list_t *t = attach;
870 a->next = mutt_make_file_attach (t->data);
874 msg->content = a = mutt_make_file_attach (t->data);
876 if (!option (OPTNOCURSES))
878 fprintf (stderr, _("%s: unable to attach file.\n"), t->data);
879 string_list_wipe(&attach);
884 string_list_wipe(&attach);
887 ci_send_message (sendflags, msg, tempfile, NULL, NULL);
889 if (!option (OPTNOCURSES))
893 if (flags & M_BUFFY) {
894 if (!buffy_check (0)) {
895 mutt_endwin _("No mailbox with new mail.");
900 buffy_next (folder, sizeof (folder));
902 else if (flags & M_SELECT) {
904 if (flags & M_NEWS) {
905 set_option (OPTNEWS);
906 if (!(CurrentNewsSrv = mutt_select_newsserver (NewsServer))) {
907 mutt_endwin (Errorbuf);
914 mutt_endwin _("No incoming mailboxes defined.");
919 mutt_select_file (folder, sizeof (folder), M_SEL_FOLDER | M_SEL_BUFFY);
927 m_strcpy(folder, sizeof(folder), NONULL(Spoolfile));
930 if (option (OPTNEWS)) {
931 unset_option (OPTNEWS);
932 nntp_expand_path (folder, sizeof (folder),
933 &CurrentNewsSrv->conn->account);
937 mutt_expand_path (folder, sizeof (folder));
939 m_strreplace(&CurrentFolder, folder);
940 m_strreplace(&LastFolder, folder);
942 if (flags & M_IGNORE) {
943 /* check to see if there are any messages in the folder */
944 switch (mx_check_empty (folder)) {
946 mutt_endwin (strerror (errno));
949 mutt_endwin _("Mailbox is empty.");
954 mutt_folder_hook (folder);
956 if ((Context = mx_open_mailbox (folder, ((flags & M_RO)
957 || option (OPTREADONLY)) ?
958 M_READONLY : 0, NULL))
959 || !explicit_folder) {
961 if (option (OPTXTERMSETTITLES))
962 mutt_xterm_set_title (NONULL (XtermLeave));
966 mutt_endwin (Errorbuf);