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 "
376 #ifdef HAVE_LANGINFO_CODESET
377 "+HAVE_LANGINFO_CODESET "
379 "-HAVE_LANGINFO_CODESET "
381 #ifdef HAVE_LANGINFO_YESEXPR
382 "+HAVE_LANGINFO_YESEXPR "
384 "-HAVE_LANGINFO_YESEXPR "
402 #ifdef HAVE_GETADDRINFO
409 puts (_("Built-In Defaults:"));
412 printf (" +ISPELL=\"%s\"\n", ISPELL);
417 printf (" +SENDMAIL=\"%s\"\n", SENDMAIL);
418 printf (" +MAILPATH=\"%s\"\n", MAILPATH);
419 printf (" +PKGDATADIR=\"%s\"\n", PKGDATADIR);
420 printf (" +PKGDOCDIR=\"%s\"\n", PKGDOCDIR);
421 printf (" +SYSCONFDIR=\"%s\"\n", SYSCONFDIR);
423 printf (" +MIXMASTER=\"%s\"\n\n", MIXMASTER);
425 puts (" -MIXMASTER\n");
428 puts (_("MadMutt is based on Madmutt wich was based on Mutt before\n"));
433 static void start_curses (void)
435 km_init (); /* must come before mutt_init */
437 #ifdef USE_SLANG_CURSES
438 SLtt_Ignore_Beep = 1; /* don't do that #*$@^! annoying visual beep! */
439 SLsmg_Display_Eight_Bit = 128; /* characters above this are printable */
440 SLtt_set_color (0, NULL, "default", "default");
442 /* should come before initscr() so that ncurses 4.2 doesn't try to install
443 its own SIGWINCH handler */
444 mutt_signal_initialize ();
446 if (initscr () == NULL) {
447 puts _("Error initializing terminal.");
451 mutt_signal_initialize ();
453 keypad (stdscr, TRUE);
456 #ifdef HAVE_TYPEAHEAD
457 typeahead (-1); /* simulate smooth scrolling */
464 #define M_IGNORE (1<<0) /* -z */
465 #define M_BUFFY (1<<1) /* -Z */
466 #define M_NOSYSRC (1<<2) /* -n */
467 #define M_RO (1<<3) /* -R */
468 #define M_SELECT (1<<4) /* -y */
470 #define M_NEWS (1<<5) /* -g and -G */
473 int main (int argc, char **argv)
475 char folder[_POSIX_PATH_MAX] = "";
476 char *subject = NULL;
477 char *includeFile = NULL;
478 char *draftFile = NULL;
479 char *newMagic = NULL;
481 string_list_t *attach = NULL;
482 string_list_t *commands = NULL;
483 string_list_t *queries = NULL;
484 string_list_t *alias_queries = NULL;
489 int explicit_folder = 0;
490 int dump_variables = -1;
492 /* initialize random number for tmp file creation */
493 srand48((unsigned int) time (NULL));
495 /* sanity check against stupid administrators */
497 if (getegid () != getgid ()) {
498 fprintf (stderr, "%s: I don't want to run with privileges!\n", argv[0]);
503 /* FIXME what about init.c:1439 ? */
504 setlocale (LC_ALL, "");
505 bindtextdomain (PACKAGE, MUTTLOCALEDIR);
506 textdomain (PACKAGE);
509 setlocale (LC_CTYPE, "");
511 mutt_error = mutt_nocurses_error;
512 mutt_message = mutt_nocurses_error;
513 srand48 (time (NULL));
516 p_clear(Options, countof(Options));
517 p_clear(QuadOptions, countof(QuadOptions));
522 "A:a:b:F:f:c:e:g:GH:s:i:hm:npQ:RTtvyzZ")) != EOF)
525 getopt (argc, argv, "A:a:b:F:f:c:e:H:s:i:hm:npQ:RTtvyzZ")) != EOF)
529 alias_queries = mutt_add_list (alias_queries, optarg);
532 if (strlen(optarg)<=512)
533 attach = mutt_add_list (attach, optarg);
535 printf("too long arguments. exiting ...\n");
541 m_strreplace(&Muttrc, optarg);
545 m_strcpy(folder, sizeof(folder), optarg);
554 msg->env = envelope_new();
556 msg->env->bcc = rfc822_parse_adrlist (msg->env->bcc, optarg);
558 msg->env->cc = rfc822_parse_adrlist (msg->env->cc, optarg);
570 commands = mutt_add_list (commands, optarg);
578 includeFile = optarg;
582 /* should take precedence over .muttrc setting, so save it for later */
591 sendflags |= SENDPOSTPONED;
595 queries = mutt_add_list (queries, optarg);
599 flags |= M_RO; /* read-only mode */
610 case 'y': /* My special hack mode */
615 case 'g': /* Specify a newsserver */
617 char buf[LONG_STRING];
619 snprintf (buf, sizeof (buf), "set nntp_host=%s", optarg);
620 commands = mutt_add_list (commands, buf);
623 case 'G': /* List of newsgroups */
624 flags |= M_SELECT | M_NEWS;
633 flags |= M_BUFFY | M_IGNORE;
647 puts (mutt_make_version (1));
648 puts (_(Copyright_Mutt));
649 puts (_(Copyright_Mutt_NG));
650 puts (_(Copyright_GPL_1));
651 puts (_(Copyright_GPL_2));
652 puts (_(Copyright_GPL_3));
656 /* Check for a batch send. */
657 if (!isatty (0) || queries || alias_queries || dump_variables > 0) {
658 set_option (OPTNOCURSES);
659 sendflags = SENDBATCH;
662 /* This must come before mutt_init() because curses needs to be started
663 before calling the init_pair() function to set the color scheme. */
664 if (!option (OPTNOCURSES))
667 /* set defaults and read init files */
669 mutt_init (flags & M_NOSYSRC, commands);
670 string_list_wipe(&commands);
672 /* Initialize crypto */
676 return mutt_query_variables (queries);
677 if (dump_variables > 0)
678 return (mutt_dump_variables (dump_variables-1));
684 for (; alias_queries; alias_queries = alias_queries->next) {
685 if ((a = address_list_dup(alias_lookup(alias_queries->data)))) {
686 /* output in machine-readable form */
687 mutt_addrlist_to_idna (a, NULL);
688 mutt_write_address_list (a, stdout, 0, 0);
689 address_list_wipe(&a);
692 printf ("%s\n", alias_queries->data);
699 mx_set_magic (newMagic);
701 if (!option (OPTNOCURSES)) {
702 SETCOLOR (MT_COLOR_NORMAL);
704 mutt_error = mutt_curses_error;
705 mutt_message = mutt_curses_message;
708 /* Create the Maildir directory if it doesn't exist. */
709 if (!option (OPTNOCURSES) && Maildir) {
711 char fpath[_POSIX_PATH_MAX];
714 m_strcpy(fpath, sizeof(fpath), Maildir);
715 mutt_expand_path (fpath, sizeof (fpath));
716 /* we're not connected yet - skip mail folder creation */
717 if (mx_get_magic (fpath) != M_IMAP)
718 if (stat (fpath, &sb) == -1 && errno == ENOENT) {
719 snprintf (mesg, sizeof (mesg), _("%s does not exist. Create it?"),
721 if (mutt_yesorno (mesg, M_YES) == M_YES) {
722 if (mkdir (fpath, 0700) == -1 && errno != EEXIST)
723 mutt_error (_("Can't create %s: %s."), Maildir, strerror (errno));
728 if (sendflags & SENDPOSTPONED) {
729 if (!option (OPTNOCURSES))
731 ci_send_message (SENDPOSTPONED, NULL, NULL, NULL, NULL);
734 else if (subject || msg || sendflags || draftFile || includeFile || attach
737 char buf[LONG_STRING];
738 char *tempfile = NULL, *infile = NULL;
739 char *bodytext = NULL;
741 if (!option (OPTNOCURSES))
751 msg->env = envelope_new();
753 for (i = optind; i < argc; i++) {
754 if (url_check_scheme (argv[i]) == U_MAILTO)
755 url_parse_mailto (msg->env, &bodytext, argv[i]);
757 msg->env->to = rfc822_parse_adrlist (msg->env->to, argv[i]);
760 if (option (OPTAUTOEDIT) && !msg->env->to && !msg->env->cc) {
761 if (!option (OPTNOCURSES))
763 fputs (_("No recipients specified.\n"), stderr);
768 msg->env->subject = m_strdup(subject);
771 infile = includeFile;
774 if (infile || bodytext) {
776 if (m_strcmp("-", infile) == 0)
779 char path[_POSIX_PATH_MAX];
781 m_strcpy(path, sizeof(path), infile);
782 mutt_expand_path (path, sizeof (path));
783 if ((fin = fopen (path, "r")) == NULL) {
784 if (!option (OPTNOCURSES))
795 tempfile = m_strdup(buf);
798 msg->env = mutt_read_rfc822_header (fin, NULL, 1, 0);
800 /* is the following if still needed? */
805 if ((fout = safe_fopen (tempfile, "w")) == NULL) {
806 if (!option (OPTNOCURSES))
814 mutt_copy_stream (fin, fout);
816 fputs (bodytext, fout);
818 if (fin && fin != stdin)
826 string_list_t *t = attach;
831 a->next = mutt_make_file_attach (t->data);
835 msg->content = a = mutt_make_file_attach (t->data);
837 if (!option (OPTNOCURSES))
839 fprintf (stderr, _("%s: unable to attach file.\n"), t->data);
840 string_list_wipe(&attach);
845 string_list_wipe(&attach);
848 ci_send_message (sendflags, msg, tempfile, NULL, NULL);
850 if (!option (OPTNOCURSES))
854 if (flags & M_BUFFY) {
855 if (!buffy_check (0)) {
856 mutt_endwin _("No mailbox with new mail.");
861 buffy_next (folder, sizeof (folder));
863 else if (flags & M_SELECT) {
865 if (flags & M_NEWS) {
866 set_option (OPTNEWS);
867 if (!(CurrentNewsSrv = mutt_select_newsserver (NewsServer))) {
868 mutt_endwin (Errorbuf);
875 mutt_endwin _("No incoming mailboxes defined.");
880 mutt_select_file (folder, sizeof (folder), M_SEL_FOLDER | M_SEL_BUFFY);
888 m_strcpy(folder, sizeof(folder), NONULL(Spoolfile));
891 if (option (OPTNEWS)) {
892 unset_option (OPTNEWS);
893 nntp_expand_path (folder, sizeof (folder),
894 &CurrentNewsSrv->conn->account);
898 mutt_expand_path (folder, sizeof (folder));
900 m_strreplace(&CurrentFolder, folder);
901 m_strreplace(&LastFolder, folder);
903 if (flags & M_IGNORE) {
904 /* check to see if there are any messages in the folder */
905 switch (mx_check_empty (folder)) {
907 mutt_endwin (strerror (errno));
910 mutt_endwin _("Mailbox is empty.");
915 mutt_folder_hook (folder);
917 if ((Context = mx_open_mailbox (folder, ((flags & M_RO)
918 || option (OPTREADONLY)) ?
919 M_READONLY : 0, NULL))
920 || !explicit_folder) {
922 if (option (OPTXTERMSETTITLES))
923 mutt_xterm_set_title (NONULL (XtermLeave));
927 mutt_endwin (Errorbuf);