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)
63 #include <gnutls/gnutls.h>
66 static const char *Notice = N_("\
67 Copyright (C) 1996-2002 Michael R. Elkins and others.\n\
68 Copyright (C) 2005 The Mutt-ng Team\n\
69 Copyright (C) 2006 Pierre Habouzit undation, Inc.\n\
70 This is free software. You may redistribute copies of it under the terms of\n\
71 the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.\n\
72 There is NO WARRANTY, to the extent permitted by law.\n");
74 void mutt_exit (int code)
80 static void mutt_usage (void)
82 puts (mutt_make_version());
85 _("usage: madmutt [ -nRyzZ ] [ -e <cmd> ] [ -F <file> ] [ -m <type> ] [ -f <file> ]\n\
86 madmutt [ -nR ] [ -e <cmd> ] [ -F <file> ] -Q <query> [ -Q <query> ] [...]\n\
87 madmutt [ -nR ] [ -e <cmd> ] [ -F <file> ] -A <alias> [ -A <alias> ] [...]\n\
88 madmutt [ -nR ] [ -e <cmd> ] [ -F <file> ] -t");
91 madmutt [ -nR ] [ -e <cmd> ] [ -F <file> ] -T\n\
92 madmutt [ -nx ] [ -e <cmd> ] [ -a <file> ] [ -F <file> ] [ -H <file> ] [ -i <file> ] [ -s <subj> ] [ -b <addr> ] [ -c <addr> ] <addr> [ ... ]\n\
93 madmutt [ -n ] [ -e <cmd> ] [ -F <file> ] -p\n\
97 -A <alias>\texpand the given alias\n\
98 -a <file>\tattach a file to the message\n\
99 -b <address>\tspecify a blind carbon-copy (BCC) address\n\
100 -c <address>\tspecify a carbon-copy (CC) address");
102 -e <command>\tspecify a command to be executed after initialization\n\
103 -f <file>\tspecify which mailbox to read\n\
104 -F <file>\tspecify an alternate muttrngc file\n\
105 -g <server>\tspecify a newsserver (if compiled with NNTP)\n\
106 -G\t\tselect a newsgroup (if compiled with NNTP)\n\
107 -H <file>\tspecify a draft file to read header and body from\n\
108 -i <file>\tspecify a file which Madmutt should include in the body");
110 -d <level>\t specify debugging level of Madmutt\n\
111 -m <type>\tspecify a default mailbox type\n\
112 -n\t\tcauses Madmutt not to read the system Madmuttrc\n\
113 -p\t\trecall a postponed message\n\
114 -Q <variable>\tquery a configuration variable\n\
115 -R\t\topen mailbox in read-only mode\n\
116 -s <subj>\tspecify a subject (must be in quotes if it has spaces)");
118 -t\t\tprint the value of all variables to stdout\n\
119 -T\t\tprint the value of all changed variables to stdout\n\
120 -v\t\tshow version and compile-time definitions\n\
121 -y\t\tselect a mailbox specified in your `mailboxes' list\n\
122 -z\t\texit immediately if there are no messages in the mailbox\n\
123 -Z\t\topen the first folder with new message, exit immediately if none\n\
124 -h\t\tthis help message");
129 static void show_version (void)
136 puts (mutt_make_version());
142 printf ("System:\n %s %s.%s", uts.sysname, uts.version, uts.release);
144 printf ("System:\n SCO %s", uts.release);
146 printf ("System:\n %s %s", uts.sysname, uts.release);
149 printf (" (%s)\nExternal Libraries:\n", uts.machine);
151 #ifdef NCURSES_VERSION
152 printf (" ncurses %s\n", NCURSES_VERSION);
153 #elif defined(USE_SLANG_CURSES)
154 printf (" slang %d\n", SLANG_VERSION);
157 #ifdef _LIBICONV_VERSION
158 printf (" libiconv %d.%d\n", _LIBICONV_VERSION >> 8,
159 _LIBICONV_VERSION & 0xff);
163 printf (" libidn %s (compiled with %s)\n",
164 stringprep_check_version (NULL), STRINGPREP_VERSION);
168 smtp_version (buf, sizeof (buf), 0);
169 printf (" libESMTP %s\n", buf);
173 #if defined(HAVE_QDBM)
174 printf (" qdbm %s\n", dpversion);
175 #elif defined(HAVE_GDBM)
176 printf (" gdbm %s\n", gdbm_version);
180 printf (" gnutls %s\n", LIBGNUTLS_VERSION);
181 printf (" gpgme %s\n", GPGME_VERSION);
182 puts (_("Compile Options:"));
229 #ifdef HAVE_START_COLOR
234 #ifdef HAVE_TYPEAHEAD
255 #ifdef HAVE_RESIZETERM
263 "+CRYPT_BACKEND_GPGME "
275 #ifdef HAVE_LANGINFO_CODESET
276 "+HAVE_LANGINFO_CODESET "
278 "-HAVE_LANGINFO_CODESET "
280 #ifdef HAVE_LANGINFO_YESEXPR
281 "+HAVE_LANGINFO_YESEXPR "
283 "-HAVE_LANGINFO_YESEXPR "
303 puts (_("Built-In Defaults:"));
304 printf (" +SENDMAIL=\"%s\"\n", SENDMAIL);
305 printf (" +MAILPATH=\"%s\"\n", MAILPATH);
306 printf (" +PKGDATADIR=\"%s\"\n", PKGDATADIR);
307 printf (" +PKGDOCDIR=\"%s\"\n", PKGDOCDIR);
308 printf (" +SYSCONFDIR=\"%s\"\n", SYSCONFDIR);
309 printf (" +MIXMASTER=\"%s\"\n\n", MIXMASTER);
311 puts (_("MadMutt is based on Madmutt wich was based on Mutt before\n"));
316 static void start_curses (void)
318 km_init (); /* must come before mutt_init */
320 #ifdef USE_SLANG_CURSES
321 SLtt_Ignore_Beep = 1; /* don't do that #*$@^! annoying visual beep! */
322 SLsmg_Display_Eight_Bit = 128; /* characters above this are printable */
323 SLtt_set_color (0, NULL, "default", "default");
325 /* should come before initscr() so that ncurses 4.2 doesn't try to install
326 its own SIGWINCH handler */
327 mutt_signal_initialize ();
329 if (initscr () == NULL) {
330 puts _("Error initializing terminal.");
334 mutt_signal_initialize ();
336 keypad (stdscr, TRUE);
339 #ifdef HAVE_TYPEAHEAD
340 typeahead (-1); /* simulate smooth scrolling */
347 #define M_IGNORE (1<<0) /* -z */
348 #define M_BUFFY (1<<1) /* -Z */
349 #define M_NOSYSRC (1<<2) /* -n */
350 #define M_RO (1<<3) /* -R */
351 #define M_SELECT (1<<4) /* -y */
353 #define M_NEWS (1<<5) /* -g and -G */
356 __attribute__((format(printf, 1, 0)))
357 static void mutt_nocurses_error (const char *fmt, ...)
362 vfprintf(stderr, fmt, ap);
367 int main (int argc, char **argv)
369 char folder[_POSIX_PATH_MAX] = "";
370 char *subject = NULL;
371 char *includeFile = NULL;
372 char *draftFile = NULL;
373 char *newMagic = NULL;
375 string_list_t *attach = NULL;
376 string_list_t *commands = NULL;
377 string_list_t *queries = NULL;
378 string_list_t *alias_queries = NULL;
383 int explicit_folder = 0;
384 int dump_variables = -1;
386 /* initialize random number for tmp file creation */
387 srand48((unsigned int) time (NULL));
389 /* sanity check against stupid administrators */
391 if (getegid () != getgid ()) {
392 fprintf (stderr, "%s: I don't want to run with privileges!\n", argv[0]);
397 /* FIXME what about init.c:1439 ? */
398 setlocale (LC_ALL, "");
399 bindtextdomain (PACKAGE, MUTTLOCALEDIR);
400 textdomain (PACKAGE);
403 setlocale (LC_CTYPE, "");
405 mutt_error = mutt_message = mutt_nocurses_error;
406 srand48 (time (NULL));
409 p_clear(Options, countof(Options));
410 p_clear(QuadOptions, countof(QuadOptions));
412 while ((i = getopt(argc, argv, "A:a:b:F:f:c:e:H:s:i:hm:npQ:RTtvyzZ"
419 alias_queries = mutt_add_list (alias_queries, optarg);
422 if (strlen(optarg)<=512)
423 attach = mutt_add_list (attach, optarg);
425 printf("too long arguments. exiting ...\n");
431 m_strreplace(&Muttrc, optarg);
435 m_strcpy(folder, sizeof(folder), optarg);
444 msg->env = envelope_new();
446 msg->env->bcc = rfc822_parse_adrlist (msg->env->bcc, optarg);
448 msg->env->cc = rfc822_parse_adrlist (msg->env->cc, optarg);
460 commands = mutt_add_list (commands, optarg);
468 includeFile = optarg;
472 /* should take precedence over .muttrc setting, so save it for later */
481 sendflags |= SENDPOSTPONED;
485 queries = mutt_add_list (queries, optarg);
489 flags |= M_RO; /* read-only mode */
500 case 'y': /* My special hack mode */
505 case 'g': /* Specify a newsserver */
507 char buf[LONG_STRING];
509 snprintf (buf, sizeof (buf), "set nntp_host=%s", optarg);
510 commands = mutt_add_list (commands, buf);
513 case 'G': /* List of newsgroups */
514 flags |= M_SELECT | M_NEWS;
523 flags |= M_BUFFY | M_IGNORE;
534 /* Check for a batch send. */
535 if (!isatty (0) || queries || alias_queries || dump_variables > 0) {
536 set_option (OPTNOCURSES);
537 sendflags = SENDBATCH;
540 /* This must come before mutt_init() because curses needs to be started
541 before calling the init_pair() function to set the color scheme. */
542 if (!option (OPTNOCURSES))
545 /* set defaults and read init files */
546 mutt_init (flags & M_NOSYSRC, commands);
547 string_list_wipe(&commands);
550 return mutt_query_variables (queries);
551 if (dump_variables > 0)
552 return (mutt_dump_variables (dump_variables-1));
558 for (; alias_queries; alias_queries = alias_queries->next) {
559 if ((a = address_list_dup(alias_lookup(alias_queries->data)))) {
560 /* output in machine-readable form */
561 mutt_addrlist_to_idna (a, NULL);
562 mutt_write_address_list (a, stdout, 0, 0);
563 address_list_wipe(&a);
566 printf ("%s\n", alias_queries->data);
573 mx_set_magic (newMagic);
575 if (!option (OPTNOCURSES)) {
576 SETCOLOR (MT_COLOR_NORMAL);
578 mutt_error = mutt_curses_error;
579 mutt_message = mutt_curses_message;
582 /* Create the Maildir directory if it doesn't exist. */
583 if (!option (OPTNOCURSES) && Maildir) {
585 char fpath[_POSIX_PATH_MAX];
588 m_strcpy(fpath, sizeof(fpath), Maildir);
589 mutt_expand_path (fpath, sizeof (fpath));
590 /* we're not connected yet - skip mail folder creation */
591 if (mx_get_magic (fpath) != M_IMAP)
592 if (stat (fpath, &sb) == -1 && errno == ENOENT) {
593 snprintf (mesg, sizeof (mesg), _("%s does not exist. Create it?"),
595 if (mutt_yesorno (mesg, M_YES) == M_YES) {
596 if (mkdir (fpath, 0700) == -1 && errno != EEXIST)
597 mutt_error (_("Can't create %s: %s."), Maildir, strerror (errno));
602 if (sendflags & SENDPOSTPONED) {
603 if (!option (OPTNOCURSES))
605 ci_send_message (SENDPOSTPONED, NULL, NULL, NULL, NULL);
608 else if (subject || msg || sendflags || draftFile || includeFile || attach
611 char buf[LONG_STRING];
612 char *tempfile = NULL, *infile = NULL;
613 char *bodytext = NULL;
616 if (!option (OPTNOCURSES))
626 msg->env = envelope_new();
628 for (i = optind; i < argc; i++) {
629 if (url_check_scheme (argv[i]) == U_MAILTO)
630 url_parse_mailto (msg->env, &bodytext, argv[i]);
632 msg->env->to = rfc822_parse_adrlist (msg->env->to, argv[i]);
635 if (option (OPTAUTOEDIT) && !msg->env->to && !msg->env->cc) {
636 if (!option (OPTNOCURSES))
638 fputs (_("No recipients specified.\n"), stderr);
643 msg->env->subject = m_strdup(subject);
646 infile = includeFile;
649 if (infile || bodytext) {
651 if (m_strcmp("-", infile) == 0)
654 char path[_POSIX_PATH_MAX];
656 m_strcpy(path, sizeof(path), infile);
657 mutt_expand_path (path, sizeof (path));
658 if ((fin = fopen (path, "r")) == NULL) {
659 if (!option (OPTNOCURSES))
670 msg->env = mutt_read_rfc822_header (fin, NULL, 1, 0);
672 /* is the following if still needed? */
674 fout = m_tempfile(buf, sizeof(buf), NONULL(MCore.tmpdir), NULL);
675 tempfile = m_strdup(buf);
679 if (!option (OPTNOCURSES))
687 mutt_copy_stream (fin, fout);
689 fputs (bodytext, fout);
691 if (fin && fin != stdin)
699 string_list_t *t = attach;
704 a->next = mutt_make_file_attach (t->data);
708 msg->content = a = mutt_make_file_attach (t->data);
710 if (!option (OPTNOCURSES))
712 fprintf (stderr, _("%s: unable to attach file.\n"), t->data);
713 string_list_wipe(&attach);
718 string_list_wipe(&attach);
721 ci_send_message (sendflags, msg, tempfile, NULL, NULL);
723 if (!option (OPTNOCURSES))
727 if (flags & M_BUFFY) {
728 if (!buffy_check (0)) {
729 mutt_endwin _("No mailbox with new mail.");
734 buffy_next (folder, sizeof (folder));
736 else if (flags & M_SELECT) {
738 if (flags & M_NEWS) {
739 set_option (OPTNEWS);
740 if (!(CurrentNewsSrv = mutt_select_newsserver (NewsServer))) {
741 mutt_endwin (Errorbuf);
747 mutt_endwin _("No incoming mailboxes defined.");
752 mutt_select_file (folder, sizeof (folder), M_SEL_FOLDER | M_SEL_BUFFY);
760 m_strcpy(folder, sizeof(folder), NONULL(Spoolfile));
763 if (option (OPTNEWS)) {
764 unset_option (OPTNEWS);
765 nntp_expand_path (folder, sizeof (folder),
766 &CurrentNewsSrv->conn->account);
769 mutt_expand_path (folder, sizeof (folder));
771 m_strreplace(&CurrentFolder, folder);
772 m_strreplace(&LastFolder, folder);
774 if (flags & M_IGNORE) {
775 /* check to see if there are any messages in the folder */
776 switch (mx_check_empty (folder)) {
778 mutt_endwin (strerror (errno));
781 mutt_endwin _("Mailbox is empty.");
786 mutt_folder_hook (folder);
788 if ((Context = mx_open_mailbox (folder, ((flags & M_RO)
789 || option (OPTREADONLY)) ?
790 M_READONLY : 0, NULL))
791 || !explicit_folder) {
793 if (option (OPTXTERMSETTITLES))
794 printf("\033]2;%s\007", NONULL(XtermLeave));
798 mutt_endwin (Errorbuf);
802 mutt_sasl_shutdown();