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/url.h>
36 #include <lib-sys/mutt_signal.h>
38 #include <lib-mime/mime.h>
40 #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 -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);
236 #if defined(HAVE_QDBM)
237 printf (" qdbm %s\n", dpversion);
238 #elif defined(HAVE_GDBM)
239 printf (" gdbm %s\n", gdbm_version);
240 #elif defined(HAVE_DB4)
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:"));
335 #ifdef HAVE_START_COLOR
340 #ifdef HAVE_TYPEAHEAD
361 #ifdef HAVE_RESIZETERM
369 #ifdef CRYPT_BACKEND_GPGME
370 "+CRYPT_BACKEND_GPGME "
372 "-CRYPT_BACKEND_GPGME "
390 #ifdef HAVE_LANGINFO_CODESET
391 "+HAVE_LANGINFO_CODESET "
393 "-HAVE_LANGINFO_CODESET "
395 #ifdef HAVE_LANGINFO_YESEXPR
396 "+HAVE_LANGINFO_YESEXPR "
398 "-HAVE_LANGINFO_YESEXPR "
421 #ifdef HAVE_GETADDRINFO
428 puts (_("Built-In Defaults:"));
431 printf (" +ISPELL=\"%s\"\n", ISPELL);
436 printf (" +SENDMAIL=\"%s\"\n", SENDMAIL);
437 printf (" +MAILPATH=\"%s\"\n", MAILPATH);
438 printf (" +PKGDATADIR=\"%s\"\n", PKGDATADIR);
439 printf (" +PKGDOCDIR=\"%s\"\n", PKGDOCDIR);
440 printf (" +SYSCONFDIR=\"%s\"\n", SYSCONFDIR);
442 printf (" +MIXMASTER=\"%s\"\n\n", MIXMASTER);
444 puts (" -MIXMASTER\n");
447 puts (_("MadMutt is based on Madmutt wich was based on Mutt before\n"));
452 static void start_curses (void)
454 km_init (); /* must come before mutt_init */
456 #ifdef USE_SLANG_CURSES
457 SLtt_Ignore_Beep = 1; /* don't do that #*$@^! annoying visual beep! */
458 SLsmg_Display_Eight_Bit = 128; /* characters above this are printable */
459 SLtt_set_color (0, NULL, "default", "default");
461 /* should come before initscr() so that ncurses 4.2 doesn't try to install
462 its own SIGWINCH handler */
463 mutt_signal_initialize ();
465 if (initscr () == NULL) {
466 puts _("Error initializing terminal.");
470 mutt_signal_initialize ();
472 keypad (stdscr, TRUE);
475 #ifdef HAVE_TYPEAHEAD
476 typeahead (-1); /* simulate smooth scrolling */
483 #define M_IGNORE (1<<0) /* -z */
484 #define M_BUFFY (1<<1) /* -Z */
485 #define M_NOSYSRC (1<<2) /* -n */
486 #define M_RO (1<<3) /* -R */
487 #define M_SELECT (1<<4) /* -y */
489 #define M_NEWS (1<<5) /* -g and -G */
492 int main (int argc, char **argv)
494 char folder[_POSIX_PATH_MAX] = "";
495 char *subject = NULL;
496 char *includeFile = NULL;
497 char *draftFile = NULL;
498 char *newMagic = NULL;
500 string_list_t *attach = NULL;
501 string_list_t *commands = NULL;
502 string_list_t *queries = NULL;
503 string_list_t *alias_queries = NULL;
508 int explicit_folder = 0;
509 int dump_variables = -1;
511 /* initialize random number for tmp file creation */
512 srand48((unsigned int) time (NULL));
514 /* sanity check against stupid administrators */
516 if (getegid () != getgid ()) {
517 fprintf (stderr, "%s: I don't want to run with privileges!\n", argv[0]);
522 /* FIXME what about init.c:1439 ? */
523 setlocale (LC_ALL, "");
524 bindtextdomain (PACKAGE, MUTTLOCALEDIR);
525 textdomain (PACKAGE);
528 setlocale (LC_CTYPE, "");
530 mutt_error = mutt_nocurses_error;
531 mutt_message = mutt_nocurses_error;
532 srand48 (time (NULL));
535 p_clear(Options, countof(Options));
536 p_clear(QuadOptions, countof(QuadOptions));
541 "A:a:b:F:f:c:e:g:GH:s:i:hm:npQ:RTtvyzZ")) != EOF)
544 getopt (argc, argv, "A:a:b:F:f:c:e:H:s:i:hm:npQ:RTtvyzZ")) != EOF)
548 alias_queries = mutt_add_list (alias_queries, optarg);
551 if (strlen(optarg)<=512)
552 attach = mutt_add_list (attach, optarg);
554 printf("too long arguments. exiting ...\n");
560 m_strreplace(&Muttrc, optarg);
564 m_strcpy(folder, sizeof(folder), optarg);
573 msg->env = envelope_new();
575 msg->env->bcc = rfc822_parse_adrlist (msg->env->bcc, optarg);
577 msg->env->cc = rfc822_parse_adrlist (msg->env->cc, optarg);
589 commands = mutt_add_list (commands, optarg);
597 includeFile = optarg;
601 /* should take precedence over .muttrc setting, so save it for later */
610 sendflags |= SENDPOSTPONED;
614 queries = mutt_add_list (queries, optarg);
618 flags |= M_RO; /* read-only mode */
629 case 'y': /* My special hack mode */
634 case 'g': /* Specify a newsserver */
636 char buf[LONG_STRING];
638 snprintf (buf, sizeof (buf), "set nntp_host=%s", optarg);
639 commands = mutt_add_list (commands, buf);
642 case 'G': /* List of newsgroups */
643 flags |= M_SELECT | M_NEWS;
652 flags |= M_BUFFY | M_IGNORE;
666 puts (mutt_make_version (1));
667 puts (_(Copyright_Mutt));
668 puts (_(Copyright_Mutt_NG));
669 puts (_(Copyright_GPL_1));
670 puts (_(Copyright_GPL_2));
671 puts (_(Copyright_GPL_3));
675 /* Check for a batch send. */
676 if (!isatty (0) || queries || alias_queries || dump_variables > 0) {
677 set_option (OPTNOCURSES);
678 sendflags = SENDBATCH;
681 /* This must come before mutt_init() because curses needs to be started
682 before calling the init_pair() function to set the color scheme. */
683 if (!option (OPTNOCURSES))
686 /* set defaults and read init files */
688 mutt_init (flags & M_NOSYSRC, commands);
689 string_list_wipe(&commands);
691 /* Initialize crypto */
695 return mutt_query_variables (queries);
696 if (dump_variables > 0)
697 return (mutt_dump_variables (dump_variables-1));
703 for (; alias_queries; alias_queries = alias_queries->next) {
704 if ((a = address_list_dup(alias_lookup(alias_queries->data)))) {
705 /* output in machine-readable form */
706 mutt_addrlist_to_idna (a, NULL);
707 mutt_write_address_list (a, stdout, 0, 0);
708 address_list_wipe(&a);
711 printf ("%s\n", alias_queries->data);
718 mx_set_magic (newMagic);
720 if (!option (OPTNOCURSES)) {
721 SETCOLOR (MT_COLOR_NORMAL);
723 mutt_error = mutt_curses_error;
724 mutt_message = mutt_curses_message;
727 /* Create the Maildir directory if it doesn't exist. */
728 if (!option (OPTNOCURSES) && Maildir) {
730 char fpath[_POSIX_PATH_MAX];
733 m_strcpy(fpath, sizeof(fpath), Maildir);
734 mutt_expand_path (fpath, sizeof (fpath));
735 /* we're not connected yet - skip mail folder creation */
736 if (mx_get_magic (fpath) != M_IMAP)
737 if (stat (fpath, &sb) == -1 && errno == ENOENT) {
738 snprintf (mesg, sizeof (mesg), _("%s does not exist. Create it?"),
740 if (mutt_yesorno (mesg, M_YES) == M_YES) {
741 if (mkdir (fpath, 0700) == -1 && errno != EEXIST)
742 mutt_error (_("Can't create %s: %s."), Maildir, strerror (errno));
747 if (sendflags & SENDPOSTPONED) {
748 if (!option (OPTNOCURSES))
750 ci_send_message (SENDPOSTPONED, NULL, NULL, NULL, NULL);
753 else if (subject || msg || sendflags || draftFile || includeFile || attach
756 char buf[LONG_STRING];
757 char *tempfile = NULL, *infile = NULL;
758 char *bodytext = NULL;
760 if (!option (OPTNOCURSES))
770 msg->env = envelope_new();
772 for (i = optind; i < argc; i++) {
773 if (url_check_scheme (argv[i]) == U_MAILTO)
774 url_parse_mailto (msg->env, &bodytext, argv[i]);
776 msg->env->to = rfc822_parse_adrlist (msg->env->to, argv[i]);
779 if (option (OPTAUTOEDIT) && !msg->env->to && !msg->env->cc) {
780 if (!option (OPTNOCURSES))
782 fputs (_("No recipients specified.\n"), stderr);
787 msg->env->subject = m_strdup(subject);
790 infile = includeFile;
793 if (infile || bodytext) {
795 if (m_strcmp("-", infile) == 0)
798 char path[_POSIX_PATH_MAX];
800 m_strcpy(path, sizeof(path), infile);
801 mutt_expand_path (path, sizeof (path));
802 if ((fin = fopen (path, "r")) == NULL) {
803 if (!option (OPTNOCURSES))
814 tempfile = m_strdup(buf);
817 msg->env = mutt_read_rfc822_header (fin, NULL, 1, 0);
819 /* is the following if still needed? */
824 if ((fout = safe_fopen (tempfile, "w")) == NULL) {
825 if (!option (OPTNOCURSES))
833 mutt_copy_stream (fin, fout);
835 fputs (bodytext, fout);
837 if (fin && fin != stdin)
845 string_list_t *t = attach;
850 a->next = mutt_make_file_attach (t->data);
854 msg->content = a = mutt_make_file_attach (t->data);
856 if (!option (OPTNOCURSES))
858 fprintf (stderr, _("%s: unable to attach file.\n"), t->data);
859 string_list_wipe(&attach);
864 string_list_wipe(&attach);
867 ci_send_message (sendflags, msg, tempfile, NULL, NULL);
869 if (!option (OPTNOCURSES))
873 if (flags & M_BUFFY) {
874 if (!buffy_check (0)) {
875 mutt_endwin _("No mailbox with new mail.");
880 buffy_next (folder, sizeof (folder));
882 else if (flags & M_SELECT) {
884 if (flags & M_NEWS) {
885 set_option (OPTNEWS);
886 if (!(CurrentNewsSrv = mutt_select_newsserver (NewsServer))) {
887 mutt_endwin (Errorbuf);
894 mutt_endwin _("No incoming mailboxes defined.");
899 mutt_select_file (folder, sizeof (folder), M_SEL_FOLDER | M_SEL_BUFFY);
907 m_strcpy(folder, sizeof(folder), NONULL(Spoolfile));
910 if (option (OPTNEWS)) {
911 unset_option (OPTNEWS);
912 nntp_expand_path (folder, sizeof (folder),
913 &CurrentNewsSrv->conn->account);
917 mutt_expand_path (folder, sizeof (folder));
919 m_strreplace(&CurrentFolder, folder);
920 m_strreplace(&LastFolder, folder);
922 if (flags & M_IGNORE) {
923 /* check to see if there are any messages in the folder */
924 switch (mx_check_empty (folder)) {
926 mutt_endwin (strerror (errno));
929 mutt_endwin _("Mailbox is empty.");
934 mutt_folder_hook (folder);
936 if ((Context = mx_open_mailbox (folder, ((flags & M_RO)
937 || option (OPTREADONLY)) ?
938 M_READONLY : 0, NULL))
939 || !explicit_folder) {
941 if (option (OPTXTERMSETTITLES))
942 mutt_xterm_set_title (NONULL (XtermLeave));
946 mutt_endwin (Errorbuf);