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.
18 #include <lib-lib/lib-lib.h>
20 #include <sys/utsname.h>
22 #include <lib-sys/mutt_signal.h>
24 #include <lib-mime/mime.h>
26 #include <lib-ui/curses.h>
33 #include <lib-crypt/crypt.h>
35 #include "mutt_idna.h"
46 #include <stringprep.h>
50 #include "nntp/nntp.h"
58 #if defined(HAVE_QDBM)
60 #elif defined(HAVE_GDBM)
62 #elif defined(HAVE_DB4)
68 #include <gnutls/gnutls.h>
71 #ifdef CRYPT_BACKEND_GPGME
76 #include <openssl/opensslv.h>
79 static const char *Notice = N_("\
80 Copyright (C) 1996-2002 Michael R. Elkins and others.\n\
81 Madmutt comes with ABSOLUTELY NO WARRANTY; for details type `madmutt -vv'.\n\
82 Mutt is free software, and you are welcome to redistribute it\n\
83 under certain conditions; type `madmutt -vv' for details.\n");
85 static const char *Copyright_Mutt = N_("\
86 Copyright (C) 1996-2004 Michael R. Elkins <me@mutt.org>\n\
87 Copyright (C) 1996-2002 Brandon Long <blong@fiction.net>\n\
88 Copyright (C) 1997-2005 Thomas Roessler <roessler@does-not-exist.org>\n\
89 Copyright (C) 1998-2005 Werner Koch <wk@isil.d.shuttle.de>\n\
90 Copyright (C) 1999-2005 Brendan Cully <brendan@kublai.com>\n\
91 Copyright (C) 1999-2002 Tommi Komulainen <Tommi.Komulainen@iki.fi>\n\
92 Copyright (C) 2000-2002 Edmund Grimley Evans <edmundo@rano.org>\n");
94 static const char *Copyright_Mutt_NG = N_("\
95 Copyright (C) 2005:\n\
96 Parts were written/modified by:\n\
97 Andreas Krennmair <ak@synflood.at>\n\
98 Nico Golde <nico@ngolde.de>\n\
99 Rocco Rutte <pdmef@cs.tu-berlin.de>\n\
101 Lots of others not mentioned here contributed lots of code,\n\
102 fixes, and suggestions.");
104 static const char* Copyright_GPL_1 = N_("\
106 This program is free software; you can redistribute it and/or modify\n\
107 it under the terms of the GNU General Public License as published by\n\
108 the Free Software Foundation; either version 2 of the License, or\n\
109 (at your option) any later version.\n");
111 static const char* Copyright_GPL_2 = N_("\
112 This program is distributed in the hope that it will be useful,\n\
113 but WITHOUT ANY WARRANTY; without even the implied warranty of\n\
114 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n\
115 GNU General Public License for more details.\n");
117 static const char* Copyright_GPL_3 = N_("\
118 You should have received a copy of the GNU General Public License\n\
119 along with this program; if not, write to the Free Software\n\
120 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.\n\
123 void mutt_exit (int code)
129 static void mutt_usage (void)
131 puts (mutt_make_version (1));
134 _("usage: madmutt [ -nRyzZ ] [ -e <cmd> ] [ -F <file> ] [ -m <type> ] [ -f <file> ]\n\
135 madmutt [ -nR ] [ -e <cmd> ] [ -F <file> ] -Q <query> [ -Q <query> ] [...]\n\
136 madmutt [ -nR ] [ -e <cmd> ] [ -F <file> ] -A <alias> [ -A <alias> ] [...]\n\
137 madmutt [ -nR ] [ -e <cmd> ] [ -F <file> ] -t");
140 madmutt [ -nR ] [ -e <cmd> ] [ -F <file> ] -T\n\
141 madmutt [ -nx ] [ -e <cmd> ] [ -a <file> ] [ -F <file> ] [ -H <file> ] [ -i <file> ] [ -s <subj> ] [ -b <addr> ] [ -c <addr> ] <addr> [ ... ]\n\
142 madmutt [ -n ] [ -e <cmd> ] [ -F <file> ] -p\n\
146 -A <alias>\texpand the given alias\n\
147 -a <file>\tattach a file to the message\n\
148 -b <address>\tspecify a blind carbon-copy (BCC) address\n\
149 -c <address>\tspecify a carbon-copy (CC) address");
151 -e <command>\tspecify a command to be executed after initialization\n\
152 -f <file>\tspecify which mailbox to read\n\
153 -F <file>\tspecify an alternate muttrngc file\n\
154 -g <server>\tspecify a newsserver (if compiled with NNTP)\n\
155 -G\t\tselect a newsgroup (if compiled with NNTP)\n\
156 -H <file>\tspecify a draft file to read header and body from\n\
157 -i <file>\tspecify a file which Madmutt should include in the body");
159 -d <level>\t specify debugging level of Madmutt\n\
160 -m <type>\tspecify a default mailbox type\n\
161 -n\t\tcauses Madmutt not to read the system Madmuttrc\n\
162 -p\t\trecall a postponed message\n\
163 -Q <variable>\tquery a configuration variable\n\
164 -R\t\topen mailbox in read-only mode\n\
165 -s <subj>\tspecify a subject (must be in quotes if it has spaces)");
167 -t\t\tprint the value of all variables to stdout\n\
168 -T\t\tprint the value of all changed variables to stdout\n\
169 -v\t\tshow version and compile-time definitions\n\
170 -y\t\tselect a mailbox specified in your `mailboxes' list\n\
171 -z\t\texit immediately if there are no messages in the mailbox\n\
172 -Z\t\topen the first folder with new message, exit immediately if none\n\
173 -h\t\tthis help message");
178 static void show_version (void)
185 puts (mutt_make_version (1));
191 printf ("System:\n %s %s.%s", uts.sysname, uts.version, uts.release);
193 printf ("System:\n SCO %s", uts.release);
195 printf ("System:\n %s %s", uts.sysname, uts.release);
198 printf (" (%s)\nExternal Libraries:\n", uts.machine);
200 #ifdef NCURSES_VERSION
201 printf (" ncurses %s\n", NCURSES_VERSION);
202 #elif defined(USE_SLANG_CURSES)
203 printf (" slang %d\n", SLANG_VERSION);
206 #ifdef _LIBICONV_VERSION
207 printf (" libiconv %d.%d\n", _LIBICONV_VERSION >> 8,
208 _LIBICONV_VERSION & 0xff);
212 printf (" libidn %s (compiled with %s)\n",
213 stringprep_check_version (NULL), STRINGPREP_VERSION);
217 smtp_version (buf, sizeof (buf), 0);
218 printf (" libESMTP %s\n", buf);
222 #if defined(HAVE_QDBM)
223 printf (" qdbm %s\n", dpversion);
224 #elif defined(HAVE_GDBM)
225 printf (" gdbm %s\n", gdbm_version);
226 #elif defined(HAVE_DB4)
227 printf (" DB4 %d.%d.%d\n", DB_VERSION_MAJOR, DB_VERSION_MINOR,
233 printf (" gnutls %s\n", LIBGNUTLS_VERSION);
237 printf (" %s\n",OPENSSL_VERSION_TEXT);
240 #ifdef CRYPT_BACKEND_GPGME
241 printf (" gpgme %s\n", GPGME_VERSION);
244 puts (_("Compile Options:"));
321 #ifdef HAVE_START_COLOR
326 #ifdef HAVE_TYPEAHEAD
347 #ifdef HAVE_RESIZETERM
355 #ifdef CRYPT_BACKEND_GPGME
356 "+CRYPT_BACKEND_GPGME "
358 "-CRYPT_BACKEND_GPGME "
371 #ifdef HAVE_LANGINFO_CODESET
372 "+HAVE_LANGINFO_CODESET "
374 "-HAVE_LANGINFO_CODESET "
376 #ifdef HAVE_LANGINFO_YESEXPR
377 "+HAVE_LANGINFO_YESEXPR "
379 "-HAVE_LANGINFO_YESEXPR "
397 #ifdef HAVE_GETADDRINFO
404 puts (_("Built-In Defaults:"));
407 printf (" +ISPELL=\"%s\"\n", ISPELL);
412 printf (" +SENDMAIL=\"%s\"\n", SENDMAIL);
413 printf (" +MAILPATH=\"%s\"\n", MAILPATH);
414 printf (" +PKGDATADIR=\"%s\"\n", PKGDATADIR);
415 printf (" +PKGDOCDIR=\"%s\"\n", PKGDOCDIR);
416 printf (" +SYSCONFDIR=\"%s\"\n", SYSCONFDIR);
418 printf (" +MIXMASTER=\"%s\"\n\n", MIXMASTER);
420 puts (" -MIXMASTER\n");
423 puts (_("MadMutt is based on Madmutt wich was based on Mutt before\n"));
428 static void start_curses (void)
430 km_init (); /* must come before mutt_init */
432 #ifdef USE_SLANG_CURSES
433 SLtt_Ignore_Beep = 1; /* don't do that #*$@^! annoying visual beep! */
434 SLsmg_Display_Eight_Bit = 128; /* characters above this are printable */
435 SLtt_set_color (0, NULL, "default", "default");
437 /* should come before initscr() so that ncurses 4.2 doesn't try to install
438 its own SIGWINCH handler */
439 mutt_signal_initialize ();
441 if (initscr () == NULL) {
442 puts _("Error initializing terminal.");
446 mutt_signal_initialize ();
448 keypad (stdscr, TRUE);
451 #ifdef HAVE_TYPEAHEAD
452 typeahead (-1); /* simulate smooth scrolling */
459 #define M_IGNORE (1<<0) /* -z */
460 #define M_BUFFY (1<<1) /* -Z */
461 #define M_NOSYSRC (1<<2) /* -n */
462 #define M_RO (1<<3) /* -R */
463 #define M_SELECT (1<<4) /* -y */
465 #define M_NEWS (1<<5) /* -g and -G */
468 int main (int argc, char **argv)
470 char folder[_POSIX_PATH_MAX] = "";
471 char *subject = NULL;
472 char *includeFile = NULL;
473 char *draftFile = NULL;
474 char *newMagic = NULL;
476 string_list_t *attach = NULL;
477 string_list_t *commands = NULL;
478 string_list_t *queries = NULL;
479 string_list_t *alias_queries = NULL;
484 int explicit_folder = 0;
485 int dump_variables = -1;
487 /* initialize random number for tmp file creation */
488 srand48((unsigned int) time (NULL));
490 /* sanity check against stupid administrators */
492 if (getegid () != getgid ()) {
493 fprintf (stderr, "%s: I don't want to run with privileges!\n", argv[0]);
498 /* FIXME what about init.c:1439 ? */
499 setlocale (LC_ALL, "");
500 bindtextdomain (PACKAGE, MUTTLOCALEDIR);
501 textdomain (PACKAGE);
504 setlocale (LC_CTYPE, "");
506 mutt_error = mutt_nocurses_error;
507 mutt_message = mutt_nocurses_error;
508 srand48 (time (NULL));
511 p_clear(Options, countof(Options));
512 p_clear(QuadOptions, countof(QuadOptions));
517 "A:a:b:F:f:c:e:g:GH:s:i:hm:npQ:RTtvyzZ")) != EOF)
520 getopt (argc, argv, "A:a:b:F:f:c:e:H:s:i:hm:npQ:RTtvyzZ")) != EOF)
524 alias_queries = mutt_add_list (alias_queries, optarg);
527 if (strlen(optarg)<=512)
528 attach = mutt_add_list (attach, optarg);
530 printf("too long arguments. exiting ...\n");
536 m_strreplace(&Muttrc, optarg);
540 m_strcpy(folder, sizeof(folder), optarg);
549 msg->env = envelope_new();
551 msg->env->bcc = rfc822_parse_adrlist (msg->env->bcc, optarg);
553 msg->env->cc = rfc822_parse_adrlist (msg->env->cc, optarg);
565 commands = mutt_add_list (commands, optarg);
573 includeFile = optarg;
577 /* should take precedence over .muttrc setting, so save it for later */
586 sendflags |= SENDPOSTPONED;
590 queries = mutt_add_list (queries, optarg);
594 flags |= M_RO; /* read-only mode */
605 case 'y': /* My special hack mode */
610 case 'g': /* Specify a newsserver */
612 char buf[LONG_STRING];
614 snprintf (buf, sizeof (buf), "set nntp_host=%s", optarg);
615 commands = mutt_add_list (commands, buf);
618 case 'G': /* List of newsgroups */
619 flags |= M_SELECT | M_NEWS;
628 flags |= M_BUFFY | M_IGNORE;
642 puts (mutt_make_version (1));
643 puts (_(Copyright_Mutt));
644 puts (_(Copyright_Mutt_NG));
645 puts (_(Copyright_GPL_1));
646 puts (_(Copyright_GPL_2));
647 puts (_(Copyright_GPL_3));
651 /* Check for a batch send. */
652 if (!isatty (0) || queries || alias_queries || dump_variables > 0) {
653 set_option (OPTNOCURSES);
654 sendflags = SENDBATCH;
657 /* This must come before mutt_init() because curses needs to be started
658 before calling the init_pair() function to set the color scheme. */
659 if (!option (OPTNOCURSES))
662 /* set defaults and read init files */
663 mutt_init (flags & M_NOSYSRC, commands);
664 string_list_wipe(&commands);
666 /* Initialize crypto */
670 return mutt_query_variables (queries);
671 if (dump_variables > 0)
672 return (mutt_dump_variables (dump_variables-1));
678 for (; alias_queries; alias_queries = alias_queries->next) {
679 if ((a = address_list_dup(alias_lookup(alias_queries->data)))) {
680 /* output in machine-readable form */
681 mutt_addrlist_to_idna (a, NULL);
682 mutt_write_address_list (a, stdout, 0, 0);
683 address_list_wipe(&a);
686 printf ("%s\n", alias_queries->data);
693 mx_set_magic (newMagic);
695 if (!option (OPTNOCURSES)) {
696 SETCOLOR (MT_COLOR_NORMAL);
698 mutt_error = mutt_curses_error;
699 mutt_message = mutt_curses_message;
702 /* Create the Maildir directory if it doesn't exist. */
703 if (!option (OPTNOCURSES) && Maildir) {
705 char fpath[_POSIX_PATH_MAX];
708 m_strcpy(fpath, sizeof(fpath), Maildir);
709 mutt_expand_path (fpath, sizeof (fpath));
710 /* we're not connected yet - skip mail folder creation */
711 if (mx_get_magic (fpath) != M_IMAP)
712 if (stat (fpath, &sb) == -1 && errno == ENOENT) {
713 snprintf (mesg, sizeof (mesg), _("%s does not exist. Create it?"),
715 if (mutt_yesorno (mesg, M_YES) == M_YES) {
716 if (mkdir (fpath, 0700) == -1 && errno != EEXIST)
717 mutt_error (_("Can't create %s: %s."), Maildir, strerror (errno));
722 if (sendflags & SENDPOSTPONED) {
723 if (!option (OPTNOCURSES))
725 ci_send_message (SENDPOSTPONED, NULL, NULL, NULL, NULL);
728 else if (subject || msg || sendflags || draftFile || includeFile || attach
731 char buf[LONG_STRING];
732 char *tempfile = NULL, *infile = NULL;
733 char *bodytext = NULL;
735 if (!option (OPTNOCURSES))
745 msg->env = envelope_new();
747 for (i = optind; i < argc; i++) {
748 if (url_check_scheme (argv[i]) == U_MAILTO)
749 url_parse_mailto (msg->env, &bodytext, argv[i]);
751 msg->env->to = rfc822_parse_adrlist (msg->env->to, argv[i]);
754 if (option (OPTAUTOEDIT) && !msg->env->to && !msg->env->cc) {
755 if (!option (OPTNOCURSES))
757 fputs (_("No recipients specified.\n"), stderr);
762 msg->env->subject = m_strdup(subject);
765 infile = includeFile;
768 if (infile || bodytext) {
770 if (m_strcmp("-", infile) == 0)
773 char path[_POSIX_PATH_MAX];
775 m_strcpy(path, sizeof(path), infile);
776 mutt_expand_path (path, sizeof (path));
777 if ((fin = fopen (path, "r")) == NULL) {
778 if (!option (OPTNOCURSES))
789 tempfile = m_strdup(buf);
792 msg->env = mutt_read_rfc822_header (fin, NULL, 1, 0);
794 /* is the following if still needed? */
799 if ((fout = safe_fopen (tempfile, "w")) == NULL) {
800 if (!option (OPTNOCURSES))
808 mutt_copy_stream (fin, fout);
810 fputs (bodytext, fout);
812 if (fin && fin != stdin)
820 string_list_t *t = attach;
825 a->next = mutt_make_file_attach (t->data);
829 msg->content = a = mutt_make_file_attach (t->data);
831 if (!option (OPTNOCURSES))
833 fprintf (stderr, _("%s: unable to attach file.\n"), t->data);
834 string_list_wipe(&attach);
839 string_list_wipe(&attach);
842 ci_send_message (sendflags, msg, tempfile, NULL, NULL);
844 if (!option (OPTNOCURSES))
848 if (flags & M_BUFFY) {
849 if (!buffy_check (0)) {
850 mutt_endwin _("No mailbox with new mail.");
855 buffy_next (folder, sizeof (folder));
857 else if (flags & M_SELECT) {
859 if (flags & M_NEWS) {
860 set_option (OPTNEWS);
861 if (!(CurrentNewsSrv = mutt_select_newsserver (NewsServer))) {
862 mutt_endwin (Errorbuf);
869 mutt_endwin _("No incoming mailboxes defined.");
874 mutt_select_file (folder, sizeof (folder), M_SEL_FOLDER | M_SEL_BUFFY);
882 m_strcpy(folder, sizeof(folder), NONULL(Spoolfile));
885 if (option (OPTNEWS)) {
886 unset_option (OPTNEWS);
887 nntp_expand_path (folder, sizeof (folder),
888 &CurrentNewsSrv->conn->account);
892 mutt_expand_path (folder, sizeof (folder));
894 m_strreplace(&CurrentFolder, folder);
895 m_strreplace(&LastFolder, folder);
897 if (flags & M_IGNORE) {
898 /* check to see if there are any messages in the folder */
899 switch (mx_check_empty (folder)) {
901 mutt_endwin (strerror (errno));
904 mutt_endwin _("Mailbox is empty.");
909 mutt_folder_hook (folder);
911 if ((Context = mx_open_mailbox (folder, ((flags & M_RO)
912 || option (OPTREADONLY)) ?
913 M_READONLY : 0, NULL))
914 || !explicit_folder) {
916 if (option (OPTXTERMSETTITLES))
917 mutt_xterm_set_title (NONULL (XtermLeave));
921 mutt_endwin (Errorbuf);