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-crypt/crypt.h>
26 #include <lib-mx/mx.h>
33 #include "mutt_idna.h"
43 #include <stringprep.h>
47 #include <nntp/nntp.h>
55 #if defined(HAVE_QDBM)
57 #elif defined(HAVE_GDBM)
59 #elif defined(HAVE_DB4)
65 #include <gnutls/gnutls.h>
68 #ifdef CRYPT_BACKEND_GPGME
73 #include <openssl/opensslv.h>
76 static const char *Notice = N_("\
77 Copyright (C) 1996-2002 Michael R. Elkins and others.\n\
78 Copyright (C) 2005 The Mutt-ng Team\n\
79 Copyright (C) 2006 Pierre Habouzit undation, Inc.\n\
80 This is free software. You may redistribute copies of it under the terms of\n\
81 the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.\n\
82 There is NO WARRANTY, to the extent permitted by law.\n");
84 void mutt_exit (int code)
90 static void mutt_usage (void)
92 puts (mutt_make_version (1));
95 _("usage: madmutt [ -nRyzZ ] [ -e <cmd> ] [ -F <file> ] [ -m <type> ] [ -f <file> ]\n\
96 madmutt [ -nR ] [ -e <cmd> ] [ -F <file> ] -Q <query> [ -Q <query> ] [...]\n\
97 madmutt [ -nR ] [ -e <cmd> ] [ -F <file> ] -A <alias> [ -A <alias> ] [...]\n\
98 madmutt [ -nR ] [ -e <cmd> ] [ -F <file> ] -t");
101 madmutt [ -nR ] [ -e <cmd> ] [ -F <file> ] -T\n\
102 madmutt [ -nx ] [ -e <cmd> ] [ -a <file> ] [ -F <file> ] [ -H <file> ] [ -i <file> ] [ -s <subj> ] [ -b <addr> ] [ -c <addr> ] <addr> [ ... ]\n\
103 madmutt [ -n ] [ -e <cmd> ] [ -F <file> ] -p\n\
107 -A <alias>\texpand the given alias\n\
108 -a <file>\tattach a file to the message\n\
109 -b <address>\tspecify a blind carbon-copy (BCC) address\n\
110 -c <address>\tspecify a carbon-copy (CC) address");
112 -e <command>\tspecify a command to be executed after initialization\n\
113 -f <file>\tspecify which mailbox to read\n\
114 -F <file>\tspecify an alternate muttrngc file\n\
115 -g <server>\tspecify a newsserver (if compiled with NNTP)\n\
116 -G\t\tselect a newsgroup (if compiled with NNTP)\n\
117 -H <file>\tspecify a draft file to read header and body from\n\
118 -i <file>\tspecify a file which Madmutt should include in the body");
120 -d <level>\t specify debugging level of Madmutt\n\
121 -m <type>\tspecify a default mailbox type\n\
122 -n\t\tcauses Madmutt not to read the system Madmuttrc\n\
123 -p\t\trecall a postponed message\n\
124 -Q <variable>\tquery a configuration variable\n\
125 -R\t\topen mailbox in read-only mode\n\
126 -s <subj>\tspecify a subject (must be in quotes if it has spaces)");
128 -t\t\tprint the value of all variables to stdout\n\
129 -T\t\tprint the value of all changed variables to stdout\n\
130 -v\t\tshow version and compile-time definitions\n\
131 -y\t\tselect a mailbox specified in your `mailboxes' list\n\
132 -z\t\texit immediately if there are no messages in the mailbox\n\
133 -Z\t\topen the first folder with new message, exit immediately if none\n\
134 -h\t\tthis help message");
139 static void show_version (void)
146 puts (mutt_make_version (1));
152 printf ("System:\n %s %s.%s", uts.sysname, uts.version, uts.release);
154 printf ("System:\n SCO %s", uts.release);
156 printf ("System:\n %s %s", uts.sysname, uts.release);
159 printf (" (%s)\nExternal Libraries:\n", uts.machine);
161 #ifdef NCURSES_VERSION
162 printf (" ncurses %s\n", NCURSES_VERSION);
163 #elif defined(USE_SLANG_CURSES)
164 printf (" slang %d\n", SLANG_VERSION);
167 #ifdef _LIBICONV_VERSION
168 printf (" libiconv %d.%d\n", _LIBICONV_VERSION >> 8,
169 _LIBICONV_VERSION & 0xff);
173 printf (" libidn %s (compiled with %s)\n",
174 stringprep_check_version (NULL), STRINGPREP_VERSION);
178 smtp_version (buf, sizeof (buf), 0);
179 printf (" libESMTP %s\n", buf);
183 #if defined(HAVE_QDBM)
184 printf (" qdbm %s\n", dpversion);
185 #elif defined(HAVE_GDBM)
186 printf (" gdbm %s\n", gdbm_version);
187 #elif defined(HAVE_DB4)
188 printf (" DB4 %d.%d.%d\n", DB_VERSION_MAJOR, DB_VERSION_MINOR,
194 printf (" gnutls %s\n", LIBGNUTLS_VERSION);
198 printf (" %s\n",OPENSSL_VERSION_TEXT);
201 #ifdef CRYPT_BACKEND_GPGME
202 printf (" gpgme %s\n", GPGME_VERSION);
205 puts (_("Compile Options:"));
277 #ifdef HAVE_START_COLOR
282 #ifdef HAVE_TYPEAHEAD
303 #ifdef HAVE_RESIZETERM
311 #ifdef CRYPT_BACKEND_GPGME
312 "+CRYPT_BACKEND_GPGME "
314 "-CRYPT_BACKEND_GPGME "
327 #ifdef HAVE_LANGINFO_CODESET
328 "+HAVE_LANGINFO_CODESET "
330 "-HAVE_LANGINFO_CODESET "
332 #ifdef HAVE_LANGINFO_YESEXPR
333 "+HAVE_LANGINFO_YESEXPR "
335 "-HAVE_LANGINFO_YESEXPR "
353 #ifdef HAVE_GETADDRINFO
360 puts (_("Built-In Defaults:"));
363 printf (" +ISPELL=\"%s\"\n", ISPELL);
368 printf (" +SENDMAIL=\"%s\"\n", SENDMAIL);
369 printf (" +MAILPATH=\"%s\"\n", MAILPATH);
370 printf (" +PKGDATADIR=\"%s\"\n", PKGDATADIR);
371 printf (" +PKGDOCDIR=\"%s\"\n", PKGDOCDIR);
372 printf (" +SYSCONFDIR=\"%s\"\n", SYSCONFDIR);
374 printf (" +MIXMASTER=\"%s\"\n\n", MIXMASTER);
376 puts (" -MIXMASTER\n");
379 puts (_("MadMutt is based on Madmutt wich was based on Mutt before\n"));
384 static void start_curses (void)
386 km_init (); /* must come before mutt_init */
388 #ifdef USE_SLANG_CURSES
389 SLtt_Ignore_Beep = 1; /* don't do that #*$@^! annoying visual beep! */
390 SLsmg_Display_Eight_Bit = 128; /* characters above this are printable */
391 SLtt_set_color (0, NULL, "default", "default");
393 /* should come before initscr() so that ncurses 4.2 doesn't try to install
394 its own SIGWINCH handler */
395 mutt_signal_initialize ();
397 if (initscr () == NULL) {
398 puts _("Error initializing terminal.");
402 mutt_signal_initialize ();
404 keypad (stdscr, TRUE);
407 #ifdef HAVE_TYPEAHEAD
408 typeahead (-1); /* simulate smooth scrolling */
415 #define M_IGNORE (1<<0) /* -z */
416 #define M_BUFFY (1<<1) /* -Z */
417 #define M_NOSYSRC (1<<2) /* -n */
418 #define M_RO (1<<3) /* -R */
419 #define M_SELECT (1<<4) /* -y */
421 #define M_NEWS (1<<5) /* -g and -G */
424 int main (int argc, char **argv)
426 char folder[_POSIX_PATH_MAX] = "";
427 char *subject = NULL;
428 char *includeFile = NULL;
429 char *draftFile = NULL;
430 char *newMagic = NULL;
432 string_list_t *attach = NULL;
433 string_list_t *commands = NULL;
434 string_list_t *queries = NULL;
435 string_list_t *alias_queries = NULL;
440 int explicit_folder = 0;
441 int dump_variables = -1;
443 /* initialize random number for tmp file creation */
444 srand48((unsigned int) time (NULL));
446 /* sanity check against stupid administrators */
448 if (getegid () != getgid ()) {
449 fprintf (stderr, "%s: I don't want to run with privileges!\n", argv[0]);
454 /* FIXME what about init.c:1439 ? */
455 setlocale (LC_ALL, "");
456 bindtextdomain (PACKAGE, MUTTLOCALEDIR);
457 textdomain (PACKAGE);
460 setlocale (LC_CTYPE, "");
462 mutt_error = mutt_nocurses_error;
463 mutt_message = mutt_nocurses_error;
464 srand48 (time (NULL));
467 p_clear(Options, countof(Options));
468 p_clear(QuadOptions, countof(QuadOptions));
473 "A:a:b:F:f:c:e:g:GH:s:i:hm:npQ:RTtvyzZ")) != EOF)
476 getopt (argc, argv, "A:a:b:F:f:c:e:H:s:i:hm:npQ:RTtvyzZ")) != EOF)
480 alias_queries = mutt_add_list (alias_queries, optarg);
483 if (strlen(optarg)<=512)
484 attach = mutt_add_list (attach, optarg);
486 printf("too long arguments. exiting ...\n");
492 m_strreplace(&Muttrc, optarg);
496 m_strcpy(folder, sizeof(folder), optarg);
505 msg->env = envelope_new();
507 msg->env->bcc = rfc822_parse_adrlist (msg->env->bcc, optarg);
509 msg->env->cc = rfc822_parse_adrlist (msg->env->cc, optarg);
521 commands = mutt_add_list (commands, optarg);
529 includeFile = optarg;
533 /* should take precedence over .muttrc setting, so save it for later */
542 sendflags |= SENDPOSTPONED;
546 queries = mutt_add_list (queries, optarg);
550 flags |= M_RO; /* read-only mode */
561 case 'y': /* My special hack mode */
566 case 'g': /* Specify a newsserver */
568 char buf[LONG_STRING];
570 snprintf (buf, sizeof (buf), "set nntp_host=%s", optarg);
571 commands = mutt_add_list (commands, buf);
574 case 'G': /* List of newsgroups */
575 flags |= M_SELECT | M_NEWS;
584 flags |= M_BUFFY | M_IGNORE;
595 /* Check for a batch send. */
596 if (!isatty (0) || queries || alias_queries || dump_variables > 0) {
597 set_option (OPTNOCURSES);
598 sendflags = SENDBATCH;
601 /* This must come before mutt_init() because curses needs to be started
602 before calling the init_pair() function to set the color scheme. */
603 if (!option (OPTNOCURSES))
606 /* set defaults and read init files */
607 mutt_init (flags & M_NOSYSRC, commands);
608 string_list_wipe(&commands);
610 /* Initialize crypto */
614 return mutt_query_variables (queries);
615 if (dump_variables > 0)
616 return (mutt_dump_variables (dump_variables-1));
622 for (; alias_queries; alias_queries = alias_queries->next) {
623 if ((a = address_list_dup(alias_lookup(alias_queries->data)))) {
624 /* output in machine-readable form */
625 mutt_addrlist_to_idna (a, NULL);
626 mutt_write_address_list (a, stdout, 0, 0);
627 address_list_wipe(&a);
630 printf ("%s\n", alias_queries->data);
637 mx_set_magic (newMagic);
639 if (!option (OPTNOCURSES)) {
640 SETCOLOR (MT_COLOR_NORMAL);
642 mutt_error = mutt_curses_error;
643 mutt_message = mutt_curses_message;
646 /* Create the Maildir directory if it doesn't exist. */
647 if (!option (OPTNOCURSES) && Maildir) {
649 char fpath[_POSIX_PATH_MAX];
652 m_strcpy(fpath, sizeof(fpath), Maildir);
653 mutt_expand_path (fpath, sizeof (fpath));
654 /* we're not connected yet - skip mail folder creation */
655 if (mx_get_magic (fpath) != M_IMAP)
656 if (stat (fpath, &sb) == -1 && errno == ENOENT) {
657 snprintf (mesg, sizeof (mesg), _("%s does not exist. Create it?"),
659 if (mutt_yesorno (mesg, M_YES) == M_YES) {
660 if (mkdir (fpath, 0700) == -1 && errno != EEXIST)
661 mutt_error (_("Can't create %s: %s."), Maildir, strerror (errno));
666 if (sendflags & SENDPOSTPONED) {
667 if (!option (OPTNOCURSES))
669 ci_send_message (SENDPOSTPONED, NULL, NULL, NULL, NULL);
672 else if (subject || msg || sendflags || draftFile || includeFile || attach
675 char buf[LONG_STRING];
676 char *tempfile = NULL, *infile = NULL;
677 char *bodytext = NULL;
679 if (!option (OPTNOCURSES))
689 msg->env = envelope_new();
691 for (i = optind; i < argc; i++) {
692 if (url_check_scheme (argv[i]) == U_MAILTO)
693 url_parse_mailto (msg->env, &bodytext, argv[i]);
695 msg->env->to = rfc822_parse_adrlist (msg->env->to, argv[i]);
698 if (option (OPTAUTOEDIT) && !msg->env->to && !msg->env->cc) {
699 if (!option (OPTNOCURSES))
701 fputs (_("No recipients specified.\n"), stderr);
706 msg->env->subject = m_strdup(subject);
709 infile = includeFile;
712 if (infile || bodytext) {
714 if (m_strcmp("-", infile) == 0)
717 char path[_POSIX_PATH_MAX];
719 m_strcpy(path, sizeof(path), infile);
720 mutt_expand_path (path, sizeof (path));
721 if ((fin = fopen (path, "r")) == NULL) {
722 if (!option (OPTNOCURSES))
733 tempfile = m_strdup(buf);
736 msg->env = mutt_read_rfc822_header (fin, NULL, 1, 0);
738 /* is the following if still needed? */
743 if ((fout = safe_fopen (tempfile, "w")) == NULL) {
744 if (!option (OPTNOCURSES))
752 mutt_copy_stream (fin, fout);
754 fputs (bodytext, fout);
756 if (fin && fin != stdin)
764 string_list_t *t = attach;
769 a->next = mutt_make_file_attach (t->data);
773 msg->content = a = mutt_make_file_attach (t->data);
775 if (!option (OPTNOCURSES))
777 fprintf (stderr, _("%s: unable to attach file.\n"), t->data);
778 string_list_wipe(&attach);
783 string_list_wipe(&attach);
786 ci_send_message (sendflags, msg, tempfile, NULL, NULL);
788 if (!option (OPTNOCURSES))
792 if (flags & M_BUFFY) {
793 if (!buffy_check (0)) {
794 mutt_endwin _("No mailbox with new mail.");
799 buffy_next (folder, sizeof (folder));
801 else if (flags & M_SELECT) {
803 if (flags & M_NEWS) {
804 set_option (OPTNEWS);
805 if (!(CurrentNewsSrv = mutt_select_newsserver (NewsServer))) {
806 mutt_endwin (Errorbuf);
813 mutt_endwin _("No incoming mailboxes defined.");
818 mutt_select_file (folder, sizeof (folder), M_SEL_FOLDER | M_SEL_BUFFY);
826 m_strcpy(folder, sizeof(folder), NONULL(Spoolfile));
829 if (option (OPTNEWS)) {
830 unset_option (OPTNEWS);
831 nntp_expand_path (folder, sizeof (folder),
832 &CurrentNewsSrv->conn->account);
836 mutt_expand_path (folder, sizeof (folder));
838 m_strreplace(&CurrentFolder, folder);
839 m_strreplace(&LastFolder, folder);
841 if (flags & M_IGNORE) {
842 /* check to see if there are any messages in the folder */
843 switch (mx_check_empty (folder)) {
845 mutt_endwin (strerror (errno));
848 mutt_endwin _("Mailbox is empty.");
853 mutt_folder_hook (folder);
855 if ((Context = mx_open_mailbox (folder, ((flags & M_RO)
856 || option (OPTREADONLY)) ?
857 M_READONLY : 0, NULL))
858 || !explicit_folder) {
860 if (option (OPTXTERMSETTITLES))
861 printf("\033]2;%s\007", NONULL(XtermLeave));
865 mutt_endwin (Errorbuf);