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-lua/lib-lua.h>
23 #include <lib-sys/mutt_signal.h>
24 #include <lib-mime/mime.h>
25 #include <lib-ui/curses.h>
26 #include <lib-crypt/crypt.h>
27 #include <lib-mx/mx.h>
34 #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());
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());
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:"));
273 #ifdef HAVE_START_COLOR
278 #ifdef HAVE_TYPEAHEAD
299 #ifdef HAVE_RESIZETERM
307 #ifdef CRYPT_BACKEND_GPGME
308 "+CRYPT_BACKEND_GPGME "
310 "-CRYPT_BACKEND_GPGME "
323 #ifdef HAVE_LANGINFO_CODESET
324 "+HAVE_LANGINFO_CODESET "
326 "-HAVE_LANGINFO_CODESET "
328 #ifdef HAVE_LANGINFO_YESEXPR
329 "+HAVE_LANGINFO_YESEXPR "
331 "-HAVE_LANGINFO_YESEXPR "
349 #ifdef HAVE_GETADDRINFO
356 puts (_("Built-In Defaults:"));
359 printf (" +ISPELL=\"%s\"\n", ISPELL);
364 printf (" +SENDMAIL=\"%s\"\n", SENDMAIL);
365 printf (" +MAILPATH=\"%s\"\n", MAILPATH);
366 printf (" +PKGDATADIR=\"%s\"\n", PKGDATADIR);
367 printf (" +PKGDOCDIR=\"%s\"\n", PKGDOCDIR);
368 printf (" +SYSCONFDIR=\"%s\"\n", SYSCONFDIR);
369 printf (" +MIXMASTER=\"%s\"\n\n", MIXMASTER);
371 puts (_("MadMutt is based on Madmutt wich was based on Mutt before\n"));
376 static void start_curses (void)
378 km_init (); /* must come before mutt_init */
380 #ifdef USE_SLANG_CURSES
381 SLtt_Ignore_Beep = 1; /* don't do that #*$@^! annoying visual beep! */
382 SLsmg_Display_Eight_Bit = 128; /* characters above this are printable */
383 SLtt_set_color (0, NULL, "default", "default");
385 /* should come before initscr() so that ncurses 4.2 doesn't try to install
386 its own SIGWINCH handler */
387 mutt_signal_initialize ();
389 if (initscr () == NULL) {
390 puts _("Error initializing terminal.");
394 mutt_signal_initialize ();
396 keypad (stdscr, TRUE);
399 #ifdef HAVE_TYPEAHEAD
400 typeahead (-1); /* simulate smooth scrolling */
407 #define M_IGNORE (1<<0) /* -z */
408 #define M_BUFFY (1<<1) /* -Z */
409 #define M_NOSYSRC (1<<2) /* -n */
410 #define M_RO (1<<3) /* -R */
411 #define M_SELECT (1<<4) /* -y */
413 #define M_NEWS (1<<5) /* -g and -G */
416 int main (int argc, char **argv)
418 char folder[_POSIX_PATH_MAX] = "";
419 char *subject = NULL;
420 char *includeFile = NULL;
421 char *draftFile = NULL;
422 char *newMagic = NULL;
424 string_list_t *attach = NULL;
425 string_list_t *commands = NULL;
426 string_list_t *queries = NULL;
427 string_list_t *alias_queries = NULL;
432 int explicit_folder = 0;
433 int dump_variables = -1;
435 /* initialize random number for tmp file creation */
436 srand48((unsigned int) time (NULL));
438 /* sanity check against stupid administrators */
440 if (getegid () != getgid ()) {
441 fprintf (stderr, "%s: I don't want to run with privileges!\n", argv[0]);
446 /* FIXME what about init.c:1439 ? */
447 setlocale (LC_ALL, "");
448 bindtextdomain (PACKAGE, MUTTLOCALEDIR);
449 textdomain (PACKAGE);
452 setlocale (LC_CTYPE, "");
454 mutt_error = mutt_nocurses_error;
455 mutt_message = mutt_nocurses_error;
456 srand48 (time (NULL));
459 p_clear(Options, countof(Options));
460 p_clear(QuadOptions, countof(QuadOptions));
462 while ((i = getopt(argc, argv, "A:a:b:F:f:c:e:H:s:i:hm:npQ:RTtvyzZ"
469 alias_queries = mutt_add_list (alias_queries, optarg);
472 if (strlen(optarg)<=512)
473 attach = mutt_add_list (attach, optarg);
475 printf("too long arguments. exiting ...\n");
481 m_strreplace(&Muttrc, optarg);
485 m_strcpy(folder, sizeof(folder), optarg);
494 msg->env = envelope_new();
496 msg->env->bcc = rfc822_parse_adrlist (msg->env->bcc, optarg);
498 msg->env->cc = rfc822_parse_adrlist (msg->env->cc, optarg);
510 commands = mutt_add_list (commands, optarg);
518 includeFile = optarg;
522 /* should take precedence over .muttrc setting, so save it for later */
531 sendflags |= SENDPOSTPONED;
535 queries = mutt_add_list (queries, optarg);
539 flags |= M_RO; /* read-only mode */
550 case 'y': /* My special hack mode */
555 case 'g': /* Specify a newsserver */
557 char buf[LONG_STRING];
559 snprintf (buf, sizeof (buf), "set nntp_host=%s", optarg);
560 commands = mutt_add_list (commands, buf);
563 case 'G': /* List of newsgroups */
564 flags |= M_SELECT | M_NEWS;
573 flags |= M_BUFFY | M_IGNORE;
584 /* Check for a batch send. */
585 if (!isatty (0) || queries || alias_queries || dump_variables > 0) {
586 set_option (OPTNOCURSES);
587 sendflags = SENDBATCH;
590 /* This must come before mutt_init() because curses needs to be started
591 before calling the init_pair() function to set the color scheme. */
592 if (!option (OPTNOCURSES))
595 /* set defaults and read init files */
596 mutt_init (flags & M_NOSYSRC, commands);
597 string_list_wipe(&commands);
599 /* Initialize crypto */
603 return mutt_query_variables (queries);
604 if (dump_variables > 0)
605 return (mutt_dump_variables (dump_variables-1));
611 for (; alias_queries; alias_queries = alias_queries->next) {
612 if ((a = address_list_dup(alias_lookup(alias_queries->data)))) {
613 /* output in machine-readable form */
614 mutt_addrlist_to_idna (a, NULL);
615 mutt_write_address_list (a, stdout, 0, 0);
616 address_list_wipe(&a);
619 printf ("%s\n", alias_queries->data);
626 mx_set_magic (newMagic);
628 if (!option (OPTNOCURSES)) {
629 SETCOLOR (MT_COLOR_NORMAL);
631 mutt_error = mutt_curses_error;
632 mutt_message = mutt_curses_message;
635 /* Create the Maildir directory if it doesn't exist. */
636 if (!option (OPTNOCURSES) && Maildir) {
638 char fpath[_POSIX_PATH_MAX];
641 m_strcpy(fpath, sizeof(fpath), Maildir);
642 mutt_expand_path (fpath, sizeof (fpath));
643 /* we're not connected yet - skip mail folder creation */
644 if (mx_get_magic (fpath) != M_IMAP)
645 if (stat (fpath, &sb) == -1 && errno == ENOENT) {
646 snprintf (mesg, sizeof (mesg), _("%s does not exist. Create it?"),
648 if (mutt_yesorno (mesg, M_YES) == M_YES) {
649 if (mkdir (fpath, 0700) == -1 && errno != EEXIST)
650 mutt_error (_("Can't create %s: %s."), Maildir, strerror (errno));
655 if (sendflags & SENDPOSTPONED) {
656 if (!option (OPTNOCURSES))
658 ci_send_message (SENDPOSTPONED, NULL, NULL, NULL, NULL);
661 else if (subject || msg || sendflags || draftFile || includeFile || attach
664 char buf[LONG_STRING];
665 char *tempfile = NULL, *infile = NULL;
666 char *bodytext = NULL;
669 if (!option (OPTNOCURSES))
679 msg->env = envelope_new();
681 for (i = optind; i < argc; i++) {
682 if (url_check_scheme (argv[i]) == U_MAILTO)
683 url_parse_mailto (msg->env, &bodytext, argv[i]);
685 msg->env->to = rfc822_parse_adrlist (msg->env->to, argv[i]);
688 if (option (OPTAUTOEDIT) && !msg->env->to && !msg->env->cc) {
689 if (!option (OPTNOCURSES))
691 fputs (_("No recipients specified.\n"), stderr);
696 msg->env->subject = m_strdup(subject);
699 infile = includeFile;
702 if (infile || bodytext) {
704 if (m_strcmp("-", infile) == 0)
707 char path[_POSIX_PATH_MAX];
709 m_strcpy(path, sizeof(path), infile);
710 mutt_expand_path (path, sizeof (path));
711 if ((fin = fopen (path, "r")) == NULL) {
712 if (!option (OPTNOCURSES))
723 msg->env = mutt_read_rfc822_header (fin, NULL, 1, 0);
725 /* is the following if still needed? */
727 fout = m_tempfile(buf, sizeof(buf), NONULL(Tempdir), NULL);
728 tempfile = m_strdup(buf);
732 if (!option (OPTNOCURSES))
740 mutt_copy_stream (fin, fout);
742 fputs (bodytext, fout);
744 if (fin && fin != stdin)
752 string_list_t *t = attach;
757 a->next = mutt_make_file_attach (t->data);
761 msg->content = a = mutt_make_file_attach (t->data);
763 if (!option (OPTNOCURSES))
765 fprintf (stderr, _("%s: unable to attach file.\n"), t->data);
766 string_list_wipe(&attach);
771 string_list_wipe(&attach);
774 ci_send_message (sendflags, msg, tempfile, NULL, NULL);
776 if (!option (OPTNOCURSES))
780 if (flags & M_BUFFY) {
781 if (!buffy_check (0)) {
782 mutt_endwin _("No mailbox with new mail.");
787 buffy_next (folder, sizeof (folder));
789 else if (flags & M_SELECT) {
791 if (flags & M_NEWS) {
792 set_option (OPTNEWS);
793 if (!(CurrentNewsSrv = mutt_select_newsserver (NewsServer))) {
794 mutt_endwin (Errorbuf);
800 mutt_endwin _("No incoming mailboxes defined.");
805 mutt_select_file (folder, sizeof (folder), M_SEL_FOLDER | M_SEL_BUFFY);
813 m_strcpy(folder, sizeof(folder), NONULL(Spoolfile));
816 if (option (OPTNEWS)) {
817 unset_option (OPTNEWS);
818 nntp_expand_path (folder, sizeof (folder),
819 &CurrentNewsSrv->conn->account);
822 mutt_expand_path (folder, sizeof (folder));
824 m_strreplace(&CurrentFolder, folder);
825 m_strreplace(&LastFolder, folder);
827 if (flags & M_IGNORE) {
828 /* check to see if there are any messages in the folder */
829 switch (mx_check_empty (folder)) {
831 mutt_endwin (strerror (errno));
834 mutt_endwin _("Mailbox is empty.");
839 mutt_folder_hook (folder);
841 if ((Context = mx_open_mailbox (folder, ((flags & M_RO)
842 || option (OPTREADONLY)) ?
843 M_READONLY : 0, NULL))
844 || !explicit_folder) {
846 if (option (OPTXTERMSETTITLES))
847 printf("\033]2;%s\007", NONULL(XtermLeave));
851 mutt_endwin (Errorbuf);