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 Copyright (C) 2005 The Mutt-ng Team\n\
82 Copyright (C) 2006 Pierre Habouzit undation, Inc.\n\
83 This is free software. You may redistribute copies of it under the terms of\n\
84 the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.\n\
85 There is NO WARRANTY, to the extent permitted by law.\n");
87 void mutt_exit (int code)
93 static void mutt_usage (void)
95 puts (mutt_make_version (1));
98 _("usage: madmutt [ -nRyzZ ] [ -e <cmd> ] [ -F <file> ] [ -m <type> ] [ -f <file> ]\n\
99 madmutt [ -nR ] [ -e <cmd> ] [ -F <file> ] -Q <query> [ -Q <query> ] [...]\n\
100 madmutt [ -nR ] [ -e <cmd> ] [ -F <file> ] -A <alias> [ -A <alias> ] [...]\n\
101 madmutt [ -nR ] [ -e <cmd> ] [ -F <file> ] -t");
104 madmutt [ -nR ] [ -e <cmd> ] [ -F <file> ] -T\n\
105 madmutt [ -nx ] [ -e <cmd> ] [ -a <file> ] [ -F <file> ] [ -H <file> ] [ -i <file> ] [ -s <subj> ] [ -b <addr> ] [ -c <addr> ] <addr> [ ... ]\n\
106 madmutt [ -n ] [ -e <cmd> ] [ -F <file> ] -p\n\
110 -A <alias>\texpand the given alias\n\
111 -a <file>\tattach a file to the message\n\
112 -b <address>\tspecify a blind carbon-copy (BCC) address\n\
113 -c <address>\tspecify a carbon-copy (CC) address");
115 -e <command>\tspecify a command to be executed after initialization\n\
116 -f <file>\tspecify which mailbox to read\n\
117 -F <file>\tspecify an alternate muttrngc file\n\
118 -g <server>\tspecify a newsserver (if compiled with NNTP)\n\
119 -G\t\tselect a newsgroup (if compiled with NNTP)\n\
120 -H <file>\tspecify a draft file to read header and body from\n\
121 -i <file>\tspecify a file which Madmutt should include in the body");
123 -d <level>\t specify debugging level of Madmutt\n\
124 -m <type>\tspecify a default mailbox type\n\
125 -n\t\tcauses Madmutt not to read the system Madmuttrc\n\
126 -p\t\trecall a postponed message\n\
127 -Q <variable>\tquery a configuration variable\n\
128 -R\t\topen mailbox in read-only mode\n\
129 -s <subj>\tspecify a subject (must be in quotes if it has spaces)");
131 -t\t\tprint the value of all variables to stdout\n\
132 -T\t\tprint the value of all changed variables to stdout\n\
133 -v\t\tshow version and compile-time definitions\n\
134 -y\t\tselect a mailbox specified in your `mailboxes' list\n\
135 -z\t\texit immediately if there are no messages in the mailbox\n\
136 -Z\t\topen the first folder with new message, exit immediately if none\n\
137 -h\t\tthis help message");
142 static void show_version (void)
149 puts (mutt_make_version (1));
155 printf ("System:\n %s %s.%s", uts.sysname, uts.version, uts.release);
157 printf ("System:\n SCO %s", uts.release);
159 printf ("System:\n %s %s", uts.sysname, uts.release);
162 printf (" (%s)\nExternal Libraries:\n", uts.machine);
164 #ifdef NCURSES_VERSION
165 printf (" ncurses %s\n", NCURSES_VERSION);
166 #elif defined(USE_SLANG_CURSES)
167 printf (" slang %d\n", SLANG_VERSION);
170 #ifdef _LIBICONV_VERSION
171 printf (" libiconv %d.%d\n", _LIBICONV_VERSION >> 8,
172 _LIBICONV_VERSION & 0xff);
176 printf (" libidn %s (compiled with %s)\n",
177 stringprep_check_version (NULL), STRINGPREP_VERSION);
181 smtp_version (buf, sizeof (buf), 0);
182 printf (" libESMTP %s\n", buf);
186 #if defined(HAVE_QDBM)
187 printf (" qdbm %s\n", dpversion);
188 #elif defined(HAVE_GDBM)
189 printf (" gdbm %s\n", gdbm_version);
190 #elif defined(HAVE_DB4)
191 printf (" DB4 %d.%d.%d\n", DB_VERSION_MAJOR, DB_VERSION_MINOR,
197 printf (" gnutls %s\n", LIBGNUTLS_VERSION);
201 printf (" %s\n",OPENSSL_VERSION_TEXT);
204 #ifdef CRYPT_BACKEND_GPGME
205 printf (" gpgme %s\n", GPGME_VERSION);
208 puts (_("Compile Options:"));
280 #ifdef HAVE_START_COLOR
285 #ifdef HAVE_TYPEAHEAD
306 #ifdef HAVE_RESIZETERM
314 #ifdef CRYPT_BACKEND_GPGME
315 "+CRYPT_BACKEND_GPGME "
317 "-CRYPT_BACKEND_GPGME "
330 #ifdef HAVE_LANGINFO_CODESET
331 "+HAVE_LANGINFO_CODESET "
333 "-HAVE_LANGINFO_CODESET "
335 #ifdef HAVE_LANGINFO_YESEXPR
336 "+HAVE_LANGINFO_YESEXPR "
338 "-HAVE_LANGINFO_YESEXPR "
356 #ifdef HAVE_GETADDRINFO
363 puts (_("Built-In Defaults:"));
366 printf (" +ISPELL=\"%s\"\n", ISPELL);
371 printf (" +SENDMAIL=\"%s\"\n", SENDMAIL);
372 printf (" +MAILPATH=\"%s\"\n", MAILPATH);
373 printf (" +PKGDATADIR=\"%s\"\n", PKGDATADIR);
374 printf (" +PKGDOCDIR=\"%s\"\n", PKGDOCDIR);
375 printf (" +SYSCONFDIR=\"%s\"\n", SYSCONFDIR);
377 printf (" +MIXMASTER=\"%s\"\n\n", MIXMASTER);
379 puts (" -MIXMASTER\n");
382 puts (_("MadMutt is based on Madmutt wich was based on Mutt before\n"));
387 static void start_curses (void)
389 km_init (); /* must come before mutt_init */
391 #ifdef USE_SLANG_CURSES
392 SLtt_Ignore_Beep = 1; /* don't do that #*$@^! annoying visual beep! */
393 SLsmg_Display_Eight_Bit = 128; /* characters above this are printable */
394 SLtt_set_color (0, NULL, "default", "default");
396 /* should come before initscr() so that ncurses 4.2 doesn't try to install
397 its own SIGWINCH handler */
398 mutt_signal_initialize ();
400 if (initscr () == NULL) {
401 puts _("Error initializing terminal.");
405 mutt_signal_initialize ();
407 keypad (stdscr, TRUE);
410 #ifdef HAVE_TYPEAHEAD
411 typeahead (-1); /* simulate smooth scrolling */
418 #define M_IGNORE (1<<0) /* -z */
419 #define M_BUFFY (1<<1) /* -Z */
420 #define M_NOSYSRC (1<<2) /* -n */
421 #define M_RO (1<<3) /* -R */
422 #define M_SELECT (1<<4) /* -y */
424 #define M_NEWS (1<<5) /* -g and -G */
427 int main (int argc, char **argv)
429 char folder[_POSIX_PATH_MAX] = "";
430 char *subject = NULL;
431 char *includeFile = NULL;
432 char *draftFile = NULL;
433 char *newMagic = NULL;
435 string_list_t *attach = NULL;
436 string_list_t *commands = NULL;
437 string_list_t *queries = NULL;
438 string_list_t *alias_queries = NULL;
443 int explicit_folder = 0;
444 int dump_variables = -1;
446 /* initialize random number for tmp file creation */
447 srand48((unsigned int) time (NULL));
449 /* sanity check against stupid administrators */
451 if (getegid () != getgid ()) {
452 fprintf (stderr, "%s: I don't want to run with privileges!\n", argv[0]);
457 /* FIXME what about init.c:1439 ? */
458 setlocale (LC_ALL, "");
459 bindtextdomain (PACKAGE, MUTTLOCALEDIR);
460 textdomain (PACKAGE);
463 setlocale (LC_CTYPE, "");
465 mutt_error = mutt_nocurses_error;
466 mutt_message = mutt_nocurses_error;
467 srand48 (time (NULL));
470 p_clear(Options, countof(Options));
471 p_clear(QuadOptions, countof(QuadOptions));
476 "A:a:b:F:f:c:e:g:GH:s:i:hm:npQ:RTtvyzZ")) != EOF)
479 getopt (argc, argv, "A:a:b:F:f:c:e:H:s:i:hm:npQ:RTtvyzZ")) != EOF)
483 alias_queries = mutt_add_list (alias_queries, optarg);
486 if (strlen(optarg)<=512)
487 attach = mutt_add_list (attach, optarg);
489 printf("too long arguments. exiting ...\n");
495 m_strreplace(&Muttrc, optarg);
499 m_strcpy(folder, sizeof(folder), optarg);
508 msg->env = envelope_new();
510 msg->env->bcc = rfc822_parse_adrlist (msg->env->bcc, optarg);
512 msg->env->cc = rfc822_parse_adrlist (msg->env->cc, optarg);
524 commands = mutt_add_list (commands, optarg);
532 includeFile = optarg;
536 /* should take precedence over .muttrc setting, so save it for later */
545 sendflags |= SENDPOSTPONED;
549 queries = mutt_add_list (queries, optarg);
553 flags |= M_RO; /* read-only mode */
564 case 'y': /* My special hack mode */
569 case 'g': /* Specify a newsserver */
571 char buf[LONG_STRING];
573 snprintf (buf, sizeof (buf), "set nntp_host=%s", optarg);
574 commands = mutt_add_list (commands, buf);
577 case 'G': /* List of newsgroups */
578 flags |= M_SELECT | M_NEWS;
587 flags |= M_BUFFY | M_IGNORE;
598 /* Check for a batch send. */
599 if (!isatty (0) || queries || alias_queries || dump_variables > 0) {
600 set_option (OPTNOCURSES);
601 sendflags = SENDBATCH;
604 /* This must come before mutt_init() because curses needs to be started
605 before calling the init_pair() function to set the color scheme. */
606 if (!option (OPTNOCURSES))
609 /* set defaults and read init files */
610 mutt_init (flags & M_NOSYSRC, commands);
611 string_list_wipe(&commands);
613 /* Initialize crypto */
617 return mutt_query_variables (queries);
618 if (dump_variables > 0)
619 return (mutt_dump_variables (dump_variables-1));
625 for (; alias_queries; alias_queries = alias_queries->next) {
626 if ((a = address_list_dup(alias_lookup(alias_queries->data)))) {
627 /* output in machine-readable form */
628 mutt_addrlist_to_idna (a, NULL);
629 mutt_write_address_list (a, stdout, 0, 0);
630 address_list_wipe(&a);
633 printf ("%s\n", alias_queries->data);
640 mx_set_magic (newMagic);
642 if (!option (OPTNOCURSES)) {
643 SETCOLOR (MT_COLOR_NORMAL);
645 mutt_error = mutt_curses_error;
646 mutt_message = mutt_curses_message;
649 /* Create the Maildir directory if it doesn't exist. */
650 if (!option (OPTNOCURSES) && Maildir) {
652 char fpath[_POSIX_PATH_MAX];
655 m_strcpy(fpath, sizeof(fpath), Maildir);
656 mutt_expand_path (fpath, sizeof (fpath));
657 /* we're not connected yet - skip mail folder creation */
658 if (mx_get_magic (fpath) != M_IMAP)
659 if (stat (fpath, &sb) == -1 && errno == ENOENT) {
660 snprintf (mesg, sizeof (mesg), _("%s does not exist. Create it?"),
662 if (mutt_yesorno (mesg, M_YES) == M_YES) {
663 if (mkdir (fpath, 0700) == -1 && errno != EEXIST)
664 mutt_error (_("Can't create %s: %s."), Maildir, strerror (errno));
669 if (sendflags & SENDPOSTPONED) {
670 if (!option (OPTNOCURSES))
672 ci_send_message (SENDPOSTPONED, NULL, NULL, NULL, NULL);
675 else if (subject || msg || sendflags || draftFile || includeFile || attach
678 char buf[LONG_STRING];
679 char *tempfile = NULL, *infile = NULL;
680 char *bodytext = NULL;
682 if (!option (OPTNOCURSES))
692 msg->env = envelope_new();
694 for (i = optind; i < argc; i++) {
695 if (url_check_scheme (argv[i]) == U_MAILTO)
696 url_parse_mailto (msg->env, &bodytext, argv[i]);
698 msg->env->to = rfc822_parse_adrlist (msg->env->to, argv[i]);
701 if (option (OPTAUTOEDIT) && !msg->env->to && !msg->env->cc) {
702 if (!option (OPTNOCURSES))
704 fputs (_("No recipients specified.\n"), stderr);
709 msg->env->subject = m_strdup(subject);
712 infile = includeFile;
715 if (infile || bodytext) {
717 if (m_strcmp("-", infile) == 0)
720 char path[_POSIX_PATH_MAX];
722 m_strcpy(path, sizeof(path), infile);
723 mutt_expand_path (path, sizeof (path));
724 if ((fin = fopen (path, "r")) == NULL) {
725 if (!option (OPTNOCURSES))
736 tempfile = m_strdup(buf);
739 msg->env = mutt_read_rfc822_header (fin, NULL, 1, 0);
741 /* is the following if still needed? */
746 if ((fout = safe_fopen (tempfile, "w")) == NULL) {
747 if (!option (OPTNOCURSES))
755 mutt_copy_stream (fin, fout);
757 fputs (bodytext, fout);
759 if (fin && fin != stdin)
767 string_list_t *t = attach;
772 a->next = mutt_make_file_attach (t->data);
776 msg->content = a = mutt_make_file_attach (t->data);
778 if (!option (OPTNOCURSES))
780 fprintf (stderr, _("%s: unable to attach file.\n"), t->data);
781 string_list_wipe(&attach);
786 string_list_wipe(&attach);
789 ci_send_message (sendflags, msg, tempfile, NULL, NULL);
791 if (!option (OPTNOCURSES))
795 if (flags & M_BUFFY) {
796 if (!buffy_check (0)) {
797 mutt_endwin _("No mailbox with new mail.");
802 buffy_next (folder, sizeof (folder));
804 else if (flags & M_SELECT) {
806 if (flags & M_NEWS) {
807 set_option (OPTNEWS);
808 if (!(CurrentNewsSrv = mutt_select_newsserver (NewsServer))) {
809 mutt_endwin (Errorbuf);
816 mutt_endwin _("No incoming mailboxes defined.");
821 mutt_select_file (folder, sizeof (folder), M_SEL_FOLDER | M_SEL_BUFFY);
829 m_strcpy(folder, sizeof(folder), NONULL(Spoolfile));
832 if (option (OPTNEWS)) {
833 unset_option (OPTNEWS);
834 nntp_expand_path (folder, sizeof (folder),
835 &CurrentNewsSrv->conn->account);
839 mutt_expand_path (folder, sizeof (folder));
841 m_strreplace(&CurrentFolder, folder);
842 m_strreplace(&LastFolder, folder);
844 if (flags & M_IGNORE) {
845 /* check to see if there are any messages in the folder */
846 switch (mx_check_empty (folder)) {
848 mutt_endwin (strerror (errno));
851 mutt_endwin _("Mailbox is empty.");
856 mutt_folder_hook (folder);
858 if ((Context = mx_open_mailbox (folder, ((flags & M_RO)
859 || option (OPTREADONLY)) ?
860 M_READONLY : 0, NULL))
861 || !explicit_folder) {
863 if (option (OPTXTERMSETTITLES))
864 mutt_xterm_set_title (NONULL (XtermLeave));
868 mutt_endwin (Errorbuf);