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>
23 #include <lib-mime/mime.h>
24 #include <lib-ui/curses.h>
25 #include <lib-mx/mx.h>
31 #include <lib-crypt/crypt.h>
33 #include "mutt_idna.h"
44 #include <stringprep.h>
48 #include "nntp/nntp.h"
56 #if defined(HAVE_QDBM)
58 #elif defined(HAVE_GDBM)
60 #elif defined(HAVE_DB4)
66 #include <gnutls/gnutls.h>
69 #ifdef CRYPT_BACKEND_GPGME
74 #include <openssl/opensslv.h>
77 static const char *Notice = N_("\
78 Copyright (C) 1996-2002 Michael R. Elkins and others.\n\
79 Copyright (C) 2005 The Mutt-ng Team\n\
80 Copyright (C) 2006 Pierre Habouzit undation, Inc.\n\
81 This is free software. You may redistribute copies of it under the terms of\n\
82 the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.\n\
83 There is NO WARRANTY, to the extent permitted by law.\n");
85 void mutt_exit (int code)
91 static void mutt_usage (void)
93 puts (mutt_make_version (1));
96 _("usage: madmutt [ -nRyzZ ] [ -e <cmd> ] [ -F <file> ] [ -m <type> ] [ -f <file> ]\n\
97 madmutt [ -nR ] [ -e <cmd> ] [ -F <file> ] -Q <query> [ -Q <query> ] [...]\n\
98 madmutt [ -nR ] [ -e <cmd> ] [ -F <file> ] -A <alias> [ -A <alias> ] [...]\n\
99 madmutt [ -nR ] [ -e <cmd> ] [ -F <file> ] -t");
102 madmutt [ -nR ] [ -e <cmd> ] [ -F <file> ] -T\n\
103 madmutt [ -nx ] [ -e <cmd> ] [ -a <file> ] [ -F <file> ] [ -H <file> ] [ -i <file> ] [ -s <subj> ] [ -b <addr> ] [ -c <addr> ] <addr> [ ... ]\n\
104 madmutt [ -n ] [ -e <cmd> ] [ -F <file> ] -p\n\
108 -A <alias>\texpand the given alias\n\
109 -a <file>\tattach a file to the message\n\
110 -b <address>\tspecify a blind carbon-copy (BCC) address\n\
111 -c <address>\tspecify a carbon-copy (CC) address");
113 -e <command>\tspecify a command to be executed after initialization\n\
114 -f <file>\tspecify which mailbox to read\n\
115 -F <file>\tspecify an alternate muttrngc file\n\
116 -g <server>\tspecify a newsserver (if compiled with NNTP)\n\
117 -G\t\tselect a newsgroup (if compiled with NNTP)\n\
118 -H <file>\tspecify a draft file to read header and body from\n\
119 -i <file>\tspecify a file which Madmutt should include in the body");
121 -d <level>\t specify debugging level of Madmutt\n\
122 -m <type>\tspecify a default mailbox type\n\
123 -n\t\tcauses Madmutt not to read the system Madmuttrc\n\
124 -p\t\trecall a postponed message\n\
125 -Q <variable>\tquery a configuration variable\n\
126 -R\t\topen mailbox in read-only mode\n\
127 -s <subj>\tspecify a subject (must be in quotes if it has spaces)");
129 -t\t\tprint the value of all variables to stdout\n\
130 -T\t\tprint the value of all changed variables to stdout\n\
131 -v\t\tshow version and compile-time definitions\n\
132 -y\t\tselect a mailbox specified in your `mailboxes' list\n\
133 -z\t\texit immediately if there are no messages in the mailbox\n\
134 -Z\t\topen the first folder with new message, exit immediately if none\n\
135 -h\t\tthis help message");
140 static void show_version (void)
147 puts (mutt_make_version (1));
153 printf ("System:\n %s %s.%s", uts.sysname, uts.version, uts.release);
155 printf ("System:\n SCO %s", uts.release);
157 printf ("System:\n %s %s", uts.sysname, uts.release);
160 printf (" (%s)\nExternal Libraries:\n", uts.machine);
162 #ifdef NCURSES_VERSION
163 printf (" ncurses %s\n", NCURSES_VERSION);
164 #elif defined(USE_SLANG_CURSES)
165 printf (" slang %d\n", SLANG_VERSION);
168 #ifdef _LIBICONV_VERSION
169 printf (" libiconv %d.%d\n", _LIBICONV_VERSION >> 8,
170 _LIBICONV_VERSION & 0xff);
174 printf (" libidn %s (compiled with %s)\n",
175 stringprep_check_version (NULL), STRINGPREP_VERSION);
179 smtp_version (buf, sizeof (buf), 0);
180 printf (" libESMTP %s\n", buf);
184 #if defined(HAVE_QDBM)
185 printf (" qdbm %s\n", dpversion);
186 #elif defined(HAVE_GDBM)
187 printf (" gdbm %s\n", gdbm_version);
188 #elif defined(HAVE_DB4)
189 printf (" DB4 %d.%d.%d\n", DB_VERSION_MAJOR, DB_VERSION_MINOR,
195 printf (" gnutls %s\n", LIBGNUTLS_VERSION);
199 printf (" %s\n",OPENSSL_VERSION_TEXT);
202 #ifdef CRYPT_BACKEND_GPGME
203 printf (" gpgme %s\n", GPGME_VERSION);
206 puts (_("Compile Options:"));
278 #ifdef HAVE_START_COLOR
283 #ifdef HAVE_TYPEAHEAD
304 #ifdef HAVE_RESIZETERM
312 #ifdef CRYPT_BACKEND_GPGME
313 "+CRYPT_BACKEND_GPGME "
315 "-CRYPT_BACKEND_GPGME "
328 #ifdef HAVE_LANGINFO_CODESET
329 "+HAVE_LANGINFO_CODESET "
331 "-HAVE_LANGINFO_CODESET "
333 #ifdef HAVE_LANGINFO_YESEXPR
334 "+HAVE_LANGINFO_YESEXPR "
336 "-HAVE_LANGINFO_YESEXPR "
354 #ifdef HAVE_GETADDRINFO
361 puts (_("Built-In Defaults:"));
364 printf (" +ISPELL=\"%s\"\n", ISPELL);
369 printf (" +SENDMAIL=\"%s\"\n", SENDMAIL);
370 printf (" +MAILPATH=\"%s\"\n", MAILPATH);
371 printf (" +PKGDATADIR=\"%s\"\n", PKGDATADIR);
372 printf (" +PKGDOCDIR=\"%s\"\n", PKGDOCDIR);
373 printf (" +SYSCONFDIR=\"%s\"\n", SYSCONFDIR);
375 printf (" +MIXMASTER=\"%s\"\n\n", MIXMASTER);
377 puts (" -MIXMASTER\n");
380 puts (_("MadMutt is based on Madmutt wich was based on Mutt before\n"));
385 static void start_curses (void)
387 km_init (); /* must come before mutt_init */
389 #ifdef USE_SLANG_CURSES
390 SLtt_Ignore_Beep = 1; /* don't do that #*$@^! annoying visual beep! */
391 SLsmg_Display_Eight_Bit = 128; /* characters above this are printable */
392 SLtt_set_color (0, NULL, "default", "default");
394 /* should come before initscr() so that ncurses 4.2 doesn't try to install
395 its own SIGWINCH handler */
396 mutt_signal_initialize ();
398 if (initscr () == NULL) {
399 puts _("Error initializing terminal.");
403 mutt_signal_initialize ();
405 keypad (stdscr, TRUE);
408 #ifdef HAVE_TYPEAHEAD
409 typeahead (-1); /* simulate smooth scrolling */
416 #define M_IGNORE (1<<0) /* -z */
417 #define M_BUFFY (1<<1) /* -Z */
418 #define M_NOSYSRC (1<<2) /* -n */
419 #define M_RO (1<<3) /* -R */
420 #define M_SELECT (1<<4) /* -y */
422 #define M_NEWS (1<<5) /* -g and -G */
425 int main (int argc, char **argv)
427 char folder[_POSIX_PATH_MAX] = "";
428 char *subject = NULL;
429 char *includeFile = NULL;
430 char *draftFile = NULL;
431 char *newMagic = NULL;
433 string_list_t *attach = NULL;
434 string_list_t *commands = NULL;
435 string_list_t *queries = NULL;
436 string_list_t *alias_queries = NULL;
441 int explicit_folder = 0;
442 int dump_variables = -1;
444 /* initialize random number for tmp file creation */
445 srand48((unsigned int) time (NULL));
447 /* sanity check against stupid administrators */
449 if (getegid () != getgid ()) {
450 fprintf (stderr, "%s: I don't want to run with privileges!\n", argv[0]);
455 /* FIXME what about init.c:1439 ? */
456 setlocale (LC_ALL, "");
457 bindtextdomain (PACKAGE, MUTTLOCALEDIR);
458 textdomain (PACKAGE);
461 setlocale (LC_CTYPE, "");
463 mutt_error = mutt_nocurses_error;
464 mutt_message = mutt_nocurses_error;
465 srand48 (time (NULL));
468 p_clear(Options, countof(Options));
469 p_clear(QuadOptions, countof(QuadOptions));
474 "A:a:b:F:f:c:e:g:GH:s:i:hm:npQ:RTtvyzZ")) != EOF)
477 getopt (argc, argv, "A:a:b:F:f:c:e:H:s:i:hm:npQ:RTtvyzZ")) != EOF)
481 alias_queries = mutt_add_list (alias_queries, optarg);
484 if (strlen(optarg)<=512)
485 attach = mutt_add_list (attach, optarg);
487 printf("too long arguments. exiting ...\n");
493 m_strreplace(&Muttrc, optarg);
497 m_strcpy(folder, sizeof(folder), optarg);
506 msg->env = envelope_new();
508 msg->env->bcc = rfc822_parse_adrlist (msg->env->bcc, optarg);
510 msg->env->cc = rfc822_parse_adrlist (msg->env->cc, optarg);
522 commands = mutt_add_list (commands, optarg);
530 includeFile = optarg;
534 /* should take precedence over .muttrc setting, so save it for later */
543 sendflags |= SENDPOSTPONED;
547 queries = mutt_add_list (queries, optarg);
551 flags |= M_RO; /* read-only mode */
562 case 'y': /* My special hack mode */
567 case 'g': /* Specify a newsserver */
569 char buf[LONG_STRING];
571 snprintf (buf, sizeof (buf), "set nntp_host=%s", optarg);
572 commands = mutt_add_list (commands, buf);
575 case 'G': /* List of newsgroups */
576 flags |= M_SELECT | M_NEWS;
585 flags |= M_BUFFY | M_IGNORE;
596 /* Check for a batch send. */
597 if (!isatty (0) || queries || alias_queries || dump_variables > 0) {
598 set_option (OPTNOCURSES);
599 sendflags = SENDBATCH;
602 /* This must come before mutt_init() because curses needs to be started
603 before calling the init_pair() function to set the color scheme. */
604 if (!option (OPTNOCURSES))
607 /* set defaults and read init files */
608 mutt_init (flags & M_NOSYSRC, commands);
609 string_list_wipe(&commands);
611 /* Initialize crypto */
615 return mutt_query_variables (queries);
616 if (dump_variables > 0)
617 return (mutt_dump_variables (dump_variables-1));
623 for (; alias_queries; alias_queries = alias_queries->next) {
624 if ((a = address_list_dup(alias_lookup(alias_queries->data)))) {
625 /* output in machine-readable form */
626 mutt_addrlist_to_idna (a, NULL);
627 mutt_write_address_list (a, stdout, 0, 0);
628 address_list_wipe(&a);
631 printf ("%s\n", alias_queries->data);
638 mx_set_magic (newMagic);
640 if (!option (OPTNOCURSES)) {
641 SETCOLOR (MT_COLOR_NORMAL);
643 mutt_error = mutt_curses_error;
644 mutt_message = mutt_curses_message;
647 /* Create the Maildir directory if it doesn't exist. */
648 if (!option (OPTNOCURSES) && Maildir) {
650 char fpath[_POSIX_PATH_MAX];
653 m_strcpy(fpath, sizeof(fpath), Maildir);
654 mutt_expand_path (fpath, sizeof (fpath));
655 /* we're not connected yet - skip mail folder creation */
656 if (mx_get_magic (fpath) != M_IMAP)
657 if (stat (fpath, &sb) == -1 && errno == ENOENT) {
658 snprintf (mesg, sizeof (mesg), _("%s does not exist. Create it?"),
660 if (mutt_yesorno (mesg, M_YES) == M_YES) {
661 if (mkdir (fpath, 0700) == -1 && errno != EEXIST)
662 mutt_error (_("Can't create %s: %s."), Maildir, strerror (errno));
667 if (sendflags & SENDPOSTPONED) {
668 if (!option (OPTNOCURSES))
670 ci_send_message (SENDPOSTPONED, NULL, NULL, NULL, NULL);
673 else if (subject || msg || sendflags || draftFile || includeFile || attach
676 char buf[LONG_STRING];
677 char *tempfile = NULL, *infile = NULL;
678 char *bodytext = NULL;
680 if (!option (OPTNOCURSES))
690 msg->env = envelope_new();
692 for (i = optind; i < argc; i++) {
693 if (url_check_scheme (argv[i]) == U_MAILTO)
694 url_parse_mailto (msg->env, &bodytext, argv[i]);
696 msg->env->to = rfc822_parse_adrlist (msg->env->to, argv[i]);
699 if (option (OPTAUTOEDIT) && !msg->env->to && !msg->env->cc) {
700 if (!option (OPTNOCURSES))
702 fputs (_("No recipients specified.\n"), stderr);
707 msg->env->subject = m_strdup(subject);
710 infile = includeFile;
713 if (infile || bodytext) {
715 if (m_strcmp("-", infile) == 0)
718 char path[_POSIX_PATH_MAX];
720 m_strcpy(path, sizeof(path), infile);
721 mutt_expand_path (path, sizeof (path));
722 if ((fin = fopen (path, "r")) == NULL) {
723 if (!option (OPTNOCURSES))
734 tempfile = m_strdup(buf);
737 msg->env = mutt_read_rfc822_header (fin, NULL, 1, 0);
739 /* is the following if still needed? */
744 if ((fout = safe_fopen (tempfile, "w")) == NULL) {
745 if (!option (OPTNOCURSES))
753 mutt_copy_stream (fin, fout);
755 fputs (bodytext, fout);
757 if (fin && fin != stdin)
765 string_list_t *t = attach;
770 a->next = mutt_make_file_attach (t->data);
774 msg->content = a = mutt_make_file_attach (t->data);
776 if (!option (OPTNOCURSES))
778 fprintf (stderr, _("%s: unable to attach file.\n"), t->data);
779 string_list_wipe(&attach);
784 string_list_wipe(&attach);
787 ci_send_message (sendflags, msg, tempfile, NULL, NULL);
789 if (!option (OPTNOCURSES))
793 if (flags & M_BUFFY) {
794 if (!buffy_check (0)) {
795 mutt_endwin _("No mailbox with new mail.");
800 buffy_next (folder, sizeof (folder));
802 else if (flags & M_SELECT) {
804 if (flags & M_NEWS) {
805 set_option (OPTNEWS);
806 if (!(CurrentNewsSrv = mutt_select_newsserver (NewsServer))) {
807 mutt_endwin (Errorbuf);
814 mutt_endwin _("No incoming mailboxes defined.");
819 mutt_select_file (folder, sizeof (folder), M_SEL_FOLDER | M_SEL_BUFFY);
827 m_strcpy(folder, sizeof(folder), NONULL(Spoolfile));
830 if (option (OPTNEWS)) {
831 unset_option (OPTNEWS);
832 nntp_expand_path (folder, sizeof (folder),
833 &CurrentNewsSrv->conn->account);
837 mutt_expand_path (folder, sizeof (folder));
839 m_strreplace(&CurrentFolder, folder);
840 m_strreplace(&LastFolder, folder);
842 if (flags & M_IGNORE) {
843 /* check to see if there are any messages in the folder */
844 switch (mx_check_empty (folder)) {
846 mutt_endwin (strerror (errno));
849 mutt_endwin _("Mailbox is empty.");
854 mutt_folder_hook (folder);
856 if ((Context = mx_open_mailbox (folder, ((flags & M_RO)
857 || option (OPTREADONLY)) ?
858 M_READONLY : 0, NULL))
859 || !explicit_folder) {
861 if (option (OPTXTERMSETTITLES))
862 mutt_xterm_set_title (NONULL (XtermLeave));
866 mutt_endwin (Errorbuf);