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-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>
72 #include <openssl/opensslv.h>
75 static const char *Notice = N_("\
76 Copyright (C) 1996-2002 Michael R. Elkins and others.\n\
77 Copyright (C) 2005 The Mutt-ng Team\n\
78 Copyright (C) 2006 Pierre Habouzit undation, Inc.\n\
79 This is free software. You may redistribute copies of it under the terms of\n\
80 the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.\n\
81 There is NO WARRANTY, to the extent permitted by law.\n");
83 void mutt_exit (int code)
89 static void mutt_usage (void)
91 puts (mutt_make_version());
94 _("usage: madmutt [ -nRyzZ ] [ -e <cmd> ] [ -F <file> ] [ -m <type> ] [ -f <file> ]\n\
95 madmutt [ -nR ] [ -e <cmd> ] [ -F <file> ] -Q <query> [ -Q <query> ] [...]\n\
96 madmutt [ -nR ] [ -e <cmd> ] [ -F <file> ] -A <alias> [ -A <alias> ] [...]\n\
97 madmutt [ -nR ] [ -e <cmd> ] [ -F <file> ] -t");
100 madmutt [ -nR ] [ -e <cmd> ] [ -F <file> ] -T\n\
101 madmutt [ -nx ] [ -e <cmd> ] [ -a <file> ] [ -F <file> ] [ -H <file> ] [ -i <file> ] [ -s <subj> ] [ -b <addr> ] [ -c <addr> ] <addr> [ ... ]\n\
102 madmutt [ -n ] [ -e <cmd> ] [ -F <file> ] -p\n\
106 -A <alias>\texpand the given alias\n\
107 -a <file>\tattach a file to the message\n\
108 -b <address>\tspecify a blind carbon-copy (BCC) address\n\
109 -c <address>\tspecify a carbon-copy (CC) address");
111 -e <command>\tspecify a command to be executed after initialization\n\
112 -f <file>\tspecify which mailbox to read\n\
113 -F <file>\tspecify an alternate muttrngc file\n\
114 -g <server>\tspecify a newsserver (if compiled with NNTP)\n\
115 -G\t\tselect a newsgroup (if compiled with NNTP)\n\
116 -H <file>\tspecify a draft file to read header and body from\n\
117 -i <file>\tspecify a file which Madmutt should include in the body");
119 -d <level>\t specify debugging level of Madmutt\n\
120 -m <type>\tspecify a default mailbox type\n\
121 -n\t\tcauses Madmutt not to read the system Madmuttrc\n\
122 -p\t\trecall a postponed message\n\
123 -Q <variable>\tquery a configuration variable\n\
124 -R\t\topen mailbox in read-only mode\n\
125 -s <subj>\tspecify a subject (must be in quotes if it has spaces)");
127 -t\t\tprint the value of all variables to stdout\n\
128 -T\t\tprint the value of all changed variables to stdout\n\
129 -v\t\tshow version and compile-time definitions\n\
130 -y\t\tselect a mailbox specified in your `mailboxes' list\n\
131 -z\t\texit immediately if there are no messages in the mailbox\n\
132 -Z\t\topen the first folder with new message, exit immediately if none\n\
133 -h\t\tthis help message");
138 static void show_version (void)
145 puts (mutt_make_version());
151 printf ("System:\n %s %s.%s", uts.sysname, uts.version, uts.release);
153 printf ("System:\n SCO %s", uts.release);
155 printf ("System:\n %s %s", uts.sysname, uts.release);
158 printf (" (%s)\nExternal Libraries:\n", uts.machine);
160 #ifdef NCURSES_VERSION
161 printf (" ncurses %s\n", NCURSES_VERSION);
162 #elif defined(USE_SLANG_CURSES)
163 printf (" slang %d\n", SLANG_VERSION);
166 #ifdef _LIBICONV_VERSION
167 printf (" libiconv %d.%d\n", _LIBICONV_VERSION >> 8,
168 _LIBICONV_VERSION & 0xff);
172 printf (" libidn %s (compiled with %s)\n",
173 stringprep_check_version (NULL), STRINGPREP_VERSION);
177 smtp_version (buf, sizeof (buf), 0);
178 printf (" libESMTP %s\n", buf);
182 #if defined(HAVE_QDBM)
183 printf (" qdbm %s\n", dpversion);
184 #elif defined(HAVE_GDBM)
185 printf (" gdbm %s\n", gdbm_version);
186 #elif defined(HAVE_DB4)
187 printf (" DB4 %d.%d.%d\n", DB_VERSION_MAJOR, DB_VERSION_MINOR,
193 printf (" gnutls %s\n", LIBGNUTLS_VERSION);
197 printf (" %s\n",OPENSSL_VERSION_TEXT);
200 printf (" gpgme %s\n", GPGME_VERSION);
201 puts (_("Compile Options:"));
268 #ifdef HAVE_START_COLOR
273 #ifdef HAVE_TYPEAHEAD
294 #ifdef HAVE_RESIZETERM
302 "+CRYPT_BACKEND_GPGME "
314 #ifdef HAVE_LANGINFO_CODESET
315 "+HAVE_LANGINFO_CODESET "
317 "-HAVE_LANGINFO_CODESET "
319 #ifdef HAVE_LANGINFO_YESEXPR
320 "+HAVE_LANGINFO_YESEXPR "
322 "-HAVE_LANGINFO_YESEXPR "
340 #ifdef HAVE_GETADDRINFO
347 puts (_("Built-In Defaults:"));
348 printf (" +SENDMAIL=\"%s\"\n", SENDMAIL);
349 printf (" +MAILPATH=\"%s\"\n", MAILPATH);
350 printf (" +PKGDATADIR=\"%s\"\n", PKGDATADIR);
351 printf (" +PKGDOCDIR=\"%s\"\n", PKGDOCDIR);
352 printf (" +SYSCONFDIR=\"%s\"\n", SYSCONFDIR);
353 printf (" +MIXMASTER=\"%s\"\n\n", MIXMASTER);
355 puts (_("MadMutt is based on Madmutt wich was based on Mutt before\n"));
360 static void start_curses (void)
362 km_init (); /* must come before mutt_init */
364 #ifdef USE_SLANG_CURSES
365 SLtt_Ignore_Beep = 1; /* don't do that #*$@^! annoying visual beep! */
366 SLsmg_Display_Eight_Bit = 128; /* characters above this are printable */
367 SLtt_set_color (0, NULL, "default", "default");
369 /* should come before initscr() so that ncurses 4.2 doesn't try to install
370 its own SIGWINCH handler */
371 mutt_signal_initialize ();
373 if (initscr () == NULL) {
374 puts _("Error initializing terminal.");
378 mutt_signal_initialize ();
380 keypad (stdscr, TRUE);
383 #ifdef HAVE_TYPEAHEAD
384 typeahead (-1); /* simulate smooth scrolling */
391 #define M_IGNORE (1<<0) /* -z */
392 #define M_BUFFY (1<<1) /* -Z */
393 #define M_NOSYSRC (1<<2) /* -n */
394 #define M_RO (1<<3) /* -R */
395 #define M_SELECT (1<<4) /* -y */
397 #define M_NEWS (1<<5) /* -g and -G */
400 __attribute__((format(printf, 1, 0)))
401 static void mutt_nocurses_error (const char *fmt, ...)
406 vfprintf(stderr, fmt, ap);
411 int main (int argc, char **argv)
413 char folder[_POSIX_PATH_MAX] = "";
414 char *subject = NULL;
415 char *includeFile = NULL;
416 char *draftFile = NULL;
417 char *newMagic = NULL;
419 string_list_t *attach = NULL;
420 string_list_t *commands = NULL;
421 string_list_t *queries = NULL;
422 string_list_t *alias_queries = NULL;
427 int explicit_folder = 0;
428 int dump_variables = -1;
430 /* initialize random number for tmp file creation */
431 srand48((unsigned int) time (NULL));
433 /* sanity check against stupid administrators */
435 if (getegid () != getgid ()) {
436 fprintf (stderr, "%s: I don't want to run with privileges!\n", argv[0]);
441 /* FIXME what about init.c:1439 ? */
442 setlocale (LC_ALL, "");
443 bindtextdomain (PACKAGE, MUTTLOCALEDIR);
444 textdomain (PACKAGE);
447 setlocale (LC_CTYPE, "");
449 mutt_error = mutt_message = mutt_nocurses_error;
450 srand48 (time (NULL));
453 p_clear(Options, countof(Options));
454 p_clear(QuadOptions, countof(QuadOptions));
456 while ((i = getopt(argc, argv, "A:a:b:F:f:c:e:H:s:i:hm:npQ:RTtvyzZ"
463 alias_queries = mutt_add_list (alias_queries, optarg);
466 if (strlen(optarg)<=512)
467 attach = mutt_add_list (attach, optarg);
469 printf("too long arguments. exiting ...\n");
475 m_strreplace(&Muttrc, optarg);
479 m_strcpy(folder, sizeof(folder), optarg);
488 msg->env = envelope_new();
490 msg->env->bcc = rfc822_parse_adrlist (msg->env->bcc, optarg);
492 msg->env->cc = rfc822_parse_adrlist (msg->env->cc, optarg);
504 commands = mutt_add_list (commands, optarg);
512 includeFile = optarg;
516 /* should take precedence over .muttrc setting, so save it for later */
525 sendflags |= SENDPOSTPONED;
529 queries = mutt_add_list (queries, optarg);
533 flags |= M_RO; /* read-only mode */
544 case 'y': /* My special hack mode */
549 case 'g': /* Specify a newsserver */
551 char buf[LONG_STRING];
553 snprintf (buf, sizeof (buf), "set nntp_host=%s", optarg);
554 commands = mutt_add_list (commands, buf);
557 case 'G': /* List of newsgroups */
558 flags |= M_SELECT | M_NEWS;
567 flags |= M_BUFFY | M_IGNORE;
578 /* Check for a batch send. */
579 if (!isatty (0) || queries || alias_queries || dump_variables > 0) {
580 set_option (OPTNOCURSES);
581 sendflags = SENDBATCH;
584 /* This must come before mutt_init() because curses needs to be started
585 before calling the init_pair() function to set the color scheme. */
586 if (!option (OPTNOCURSES))
589 /* set defaults and read init files */
590 mutt_init (flags & M_NOSYSRC, commands);
591 string_list_wipe(&commands);
594 return mutt_query_variables (queries);
595 if (dump_variables > 0)
596 return (mutt_dump_variables (dump_variables-1));
602 for (; alias_queries; alias_queries = alias_queries->next) {
603 if ((a = address_list_dup(alias_lookup(alias_queries->data)))) {
604 /* output in machine-readable form */
605 mutt_addrlist_to_idna (a, NULL);
606 mutt_write_address_list (a, stdout, 0, 0);
607 address_list_wipe(&a);
610 printf ("%s\n", alias_queries->data);
617 mx_set_magic (newMagic);
619 if (!option (OPTNOCURSES)) {
620 SETCOLOR (MT_COLOR_NORMAL);
622 mutt_error = mutt_curses_error;
623 mutt_message = mutt_curses_message;
626 /* Create the Maildir directory if it doesn't exist. */
627 if (!option (OPTNOCURSES) && Maildir) {
629 char fpath[_POSIX_PATH_MAX];
632 m_strcpy(fpath, sizeof(fpath), Maildir);
633 mutt_expand_path (fpath, sizeof (fpath));
634 /* we're not connected yet - skip mail folder creation */
635 if (mx_get_magic (fpath) != M_IMAP)
636 if (stat (fpath, &sb) == -1 && errno == ENOENT) {
637 snprintf (mesg, sizeof (mesg), _("%s does not exist. Create it?"),
639 if (mutt_yesorno (mesg, M_YES) == M_YES) {
640 if (mkdir (fpath, 0700) == -1 && errno != EEXIST)
641 mutt_error (_("Can't create %s: %s."), Maildir, strerror (errno));
646 if (sendflags & SENDPOSTPONED) {
647 if (!option (OPTNOCURSES))
649 ci_send_message (SENDPOSTPONED, NULL, NULL, NULL, NULL);
652 else if (subject || msg || sendflags || draftFile || includeFile || attach
655 char buf[LONG_STRING];
656 char *tempfile = NULL, *infile = NULL;
657 char *bodytext = NULL;
660 if (!option (OPTNOCURSES))
670 msg->env = envelope_new();
672 for (i = optind; i < argc; i++) {
673 if (url_check_scheme (argv[i]) == U_MAILTO)
674 url_parse_mailto (msg->env, &bodytext, argv[i]);
676 msg->env->to = rfc822_parse_adrlist (msg->env->to, argv[i]);
679 if (option (OPTAUTOEDIT) && !msg->env->to && !msg->env->cc) {
680 if (!option (OPTNOCURSES))
682 fputs (_("No recipients specified.\n"), stderr);
687 msg->env->subject = m_strdup(subject);
690 infile = includeFile;
693 if (infile || bodytext) {
695 if (m_strcmp("-", infile) == 0)
698 char path[_POSIX_PATH_MAX];
700 m_strcpy(path, sizeof(path), infile);
701 mutt_expand_path (path, sizeof (path));
702 if ((fin = fopen (path, "r")) == NULL) {
703 if (!option (OPTNOCURSES))
714 msg->env = mutt_read_rfc822_header (fin, NULL, 1, 0);
716 /* is the following if still needed? */
718 fout = m_tempfile(buf, sizeof(buf), NONULL(MCore.tmpdir), NULL);
719 tempfile = m_strdup(buf);
723 if (!option (OPTNOCURSES))
731 mutt_copy_stream (fin, fout);
733 fputs (bodytext, fout);
735 if (fin && fin != stdin)
743 string_list_t *t = attach;
748 a->next = mutt_make_file_attach (t->data);
752 msg->content = a = mutt_make_file_attach (t->data);
754 if (!option (OPTNOCURSES))
756 fprintf (stderr, _("%s: unable to attach file.\n"), t->data);
757 string_list_wipe(&attach);
762 string_list_wipe(&attach);
765 ci_send_message (sendflags, msg, tempfile, NULL, NULL);
767 if (!option (OPTNOCURSES))
771 if (flags & M_BUFFY) {
772 if (!buffy_check (0)) {
773 mutt_endwin _("No mailbox with new mail.");
778 buffy_next (folder, sizeof (folder));
780 else if (flags & M_SELECT) {
782 if (flags & M_NEWS) {
783 set_option (OPTNEWS);
784 if (!(CurrentNewsSrv = mutt_select_newsserver (NewsServer))) {
785 mutt_endwin (Errorbuf);
791 mutt_endwin _("No incoming mailboxes defined.");
796 mutt_select_file (folder, sizeof (folder), M_SEL_FOLDER | M_SEL_BUFFY);
804 m_strcpy(folder, sizeof(folder), NONULL(Spoolfile));
807 if (option (OPTNEWS)) {
808 unset_option (OPTNEWS);
809 nntp_expand_path (folder, sizeof (folder),
810 &CurrentNewsSrv->conn->account);
813 mutt_expand_path (folder, sizeof (folder));
815 m_strreplace(&CurrentFolder, folder);
816 m_strreplace(&LastFolder, folder);
818 if (flags & M_IGNORE) {
819 /* check to see if there are any messages in the folder */
820 switch (mx_check_empty (folder)) {
822 mutt_endwin (strerror (errno));
825 mutt_endwin _("Mailbox is empty.");
830 mutt_folder_hook (folder);
832 if ((Context = mx_open_mailbox (folder, ((flags & M_RO)
833 || option (OPTREADONLY)) ?
834 M_READONLY : 0, NULL))
835 || !explicit_folder) {
837 if (option (OPTXTERMSETTITLES))
838 printf("\033]2;%s\007", NONULL(XtermLeave));
842 mutt_endwin (Errorbuf);