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:"));
263 #ifdef HAVE_START_COLOR
268 #ifdef HAVE_TYPEAHEAD
289 #ifdef HAVE_RESIZETERM
297 "+CRYPT_BACKEND_GPGME "
309 #ifdef HAVE_LANGINFO_CODESET
310 "+HAVE_LANGINFO_CODESET "
312 "-HAVE_LANGINFO_CODESET "
314 #ifdef HAVE_LANGINFO_YESEXPR
315 "+HAVE_LANGINFO_YESEXPR "
317 "-HAVE_LANGINFO_YESEXPR "
335 #ifdef HAVE_GETADDRINFO
342 puts (_("Built-In Defaults:"));
343 printf (" +SENDMAIL=\"%s\"\n", SENDMAIL);
344 printf (" +MAILPATH=\"%s\"\n", MAILPATH);
345 printf (" +PKGDATADIR=\"%s\"\n", PKGDATADIR);
346 printf (" +PKGDOCDIR=\"%s\"\n", PKGDOCDIR);
347 printf (" +SYSCONFDIR=\"%s\"\n", SYSCONFDIR);
348 printf (" +MIXMASTER=\"%s\"\n\n", MIXMASTER);
350 puts (_("MadMutt is based on Madmutt wich was based on Mutt before\n"));
355 static void start_curses (void)
357 km_init (); /* must come before mutt_init */
359 #ifdef USE_SLANG_CURSES
360 SLtt_Ignore_Beep = 1; /* don't do that #*$@^! annoying visual beep! */
361 SLsmg_Display_Eight_Bit = 128; /* characters above this are printable */
362 SLtt_set_color (0, NULL, "default", "default");
364 /* should come before initscr() so that ncurses 4.2 doesn't try to install
365 its own SIGWINCH handler */
366 mutt_signal_initialize ();
368 if (initscr () == NULL) {
369 puts _("Error initializing terminal.");
373 mutt_signal_initialize ();
375 keypad (stdscr, TRUE);
378 #ifdef HAVE_TYPEAHEAD
379 typeahead (-1); /* simulate smooth scrolling */
386 #define M_IGNORE (1<<0) /* -z */
387 #define M_BUFFY (1<<1) /* -Z */
388 #define M_NOSYSRC (1<<2) /* -n */
389 #define M_RO (1<<3) /* -R */
390 #define M_SELECT (1<<4) /* -y */
392 #define M_NEWS (1<<5) /* -g and -G */
395 __attribute__((format(printf, 1, 0)))
396 static void mutt_nocurses_error (const char *fmt, ...)
401 vfprintf(stderr, fmt, ap);
406 int main (int argc, char **argv)
408 char folder[_POSIX_PATH_MAX] = "";
409 char *subject = NULL;
410 char *includeFile = NULL;
411 char *draftFile = NULL;
412 char *newMagic = NULL;
414 string_list_t *attach = NULL;
415 string_list_t *commands = NULL;
416 string_list_t *queries = NULL;
417 string_list_t *alias_queries = NULL;
422 int explicit_folder = 0;
423 int dump_variables = -1;
425 /* initialize random number for tmp file creation */
426 srand48((unsigned int) time (NULL));
428 /* sanity check against stupid administrators */
430 if (getegid () != getgid ()) {
431 fprintf (stderr, "%s: I don't want to run with privileges!\n", argv[0]);
436 /* FIXME what about init.c:1439 ? */
437 setlocale (LC_ALL, "");
438 bindtextdomain (PACKAGE, MUTTLOCALEDIR);
439 textdomain (PACKAGE);
442 setlocale (LC_CTYPE, "");
444 mutt_error = mutt_message = mutt_nocurses_error;
445 srand48 (time (NULL));
448 p_clear(Options, countof(Options));
449 p_clear(QuadOptions, countof(QuadOptions));
451 while ((i = getopt(argc, argv, "A:a:b:F:f:c:e:H:s:i:hm:npQ:RTtvyzZ"
458 alias_queries = mutt_add_list (alias_queries, optarg);
461 if (strlen(optarg)<=512)
462 attach = mutt_add_list (attach, optarg);
464 printf("too long arguments. exiting ...\n");
470 m_strreplace(&Muttrc, optarg);
474 m_strcpy(folder, sizeof(folder), optarg);
483 msg->env = envelope_new();
485 msg->env->bcc = rfc822_parse_adrlist (msg->env->bcc, optarg);
487 msg->env->cc = rfc822_parse_adrlist (msg->env->cc, optarg);
499 commands = mutt_add_list (commands, optarg);
507 includeFile = optarg;
511 /* should take precedence over .muttrc setting, so save it for later */
520 sendflags |= SENDPOSTPONED;
524 queries = mutt_add_list (queries, optarg);
528 flags |= M_RO; /* read-only mode */
539 case 'y': /* My special hack mode */
544 case 'g': /* Specify a newsserver */
546 char buf[LONG_STRING];
548 snprintf (buf, sizeof (buf), "set nntp_host=%s", optarg);
549 commands = mutt_add_list (commands, buf);
552 case 'G': /* List of newsgroups */
553 flags |= M_SELECT | M_NEWS;
562 flags |= M_BUFFY | M_IGNORE;
573 /* Check for a batch send. */
574 if (!isatty (0) || queries || alias_queries || dump_variables > 0) {
575 set_option (OPTNOCURSES);
576 sendflags = SENDBATCH;
579 /* This must come before mutt_init() because curses needs to be started
580 before calling the init_pair() function to set the color scheme. */
581 if (!option (OPTNOCURSES))
584 /* set defaults and read init files */
585 mutt_init (flags & M_NOSYSRC, commands);
586 string_list_wipe(&commands);
589 return mutt_query_variables (queries);
590 if (dump_variables > 0)
591 return (mutt_dump_variables (dump_variables-1));
597 for (; alias_queries; alias_queries = alias_queries->next) {
598 if ((a = address_list_dup(alias_lookup(alias_queries->data)))) {
599 /* output in machine-readable form */
600 mutt_addrlist_to_idna (a, NULL);
601 mutt_write_address_list (a, stdout, 0, 0);
602 address_list_wipe(&a);
605 printf ("%s\n", alias_queries->data);
612 mx_set_magic (newMagic);
614 if (!option (OPTNOCURSES)) {
615 SETCOLOR (MT_COLOR_NORMAL);
617 mutt_error = mutt_curses_error;
618 mutt_message = mutt_curses_message;
621 /* Create the Maildir directory if it doesn't exist. */
622 if (!option (OPTNOCURSES) && Maildir) {
624 char fpath[_POSIX_PATH_MAX];
627 m_strcpy(fpath, sizeof(fpath), Maildir);
628 mutt_expand_path (fpath, sizeof (fpath));
629 /* we're not connected yet - skip mail folder creation */
630 if (mx_get_magic (fpath) != M_IMAP)
631 if (stat (fpath, &sb) == -1 && errno == ENOENT) {
632 snprintf (mesg, sizeof (mesg), _("%s does not exist. Create it?"),
634 if (mutt_yesorno (mesg, M_YES) == M_YES) {
635 if (mkdir (fpath, 0700) == -1 && errno != EEXIST)
636 mutt_error (_("Can't create %s: %s."), Maildir, strerror (errno));
641 if (sendflags & SENDPOSTPONED) {
642 if (!option (OPTNOCURSES))
644 ci_send_message (SENDPOSTPONED, NULL, NULL, NULL, NULL);
647 else if (subject || msg || sendflags || draftFile || includeFile || attach
650 char buf[LONG_STRING];
651 char *tempfile = NULL, *infile = NULL;
652 char *bodytext = NULL;
655 if (!option (OPTNOCURSES))
665 msg->env = envelope_new();
667 for (i = optind; i < argc; i++) {
668 if (url_check_scheme (argv[i]) == U_MAILTO)
669 url_parse_mailto (msg->env, &bodytext, argv[i]);
671 msg->env->to = rfc822_parse_adrlist (msg->env->to, argv[i]);
674 if (option (OPTAUTOEDIT) && !msg->env->to && !msg->env->cc) {
675 if (!option (OPTNOCURSES))
677 fputs (_("No recipients specified.\n"), stderr);
682 msg->env->subject = m_strdup(subject);
685 infile = includeFile;
688 if (infile || bodytext) {
690 if (m_strcmp("-", infile) == 0)
693 char path[_POSIX_PATH_MAX];
695 m_strcpy(path, sizeof(path), infile);
696 mutt_expand_path (path, sizeof (path));
697 if ((fin = fopen (path, "r")) == NULL) {
698 if (!option (OPTNOCURSES))
709 msg->env = mutt_read_rfc822_header (fin, NULL, 1, 0);
711 /* is the following if still needed? */
713 fout = m_tempfile(buf, sizeof(buf), NONULL(MCore.tmpdir), NULL);
714 tempfile = m_strdup(buf);
718 if (!option (OPTNOCURSES))
726 mutt_copy_stream (fin, fout);
728 fputs (bodytext, fout);
730 if (fin && fin != stdin)
738 string_list_t *t = attach;
743 a->next = mutt_make_file_attach (t->data);
747 msg->content = a = mutt_make_file_attach (t->data);
749 if (!option (OPTNOCURSES))
751 fprintf (stderr, _("%s: unable to attach file.\n"), t->data);
752 string_list_wipe(&attach);
757 string_list_wipe(&attach);
760 ci_send_message (sendflags, msg, tempfile, NULL, NULL);
762 if (!option (OPTNOCURSES))
766 if (flags & M_BUFFY) {
767 if (!buffy_check (0)) {
768 mutt_endwin _("No mailbox with new mail.");
773 buffy_next (folder, sizeof (folder));
775 else if (flags & M_SELECT) {
777 if (flags & M_NEWS) {
778 set_option (OPTNEWS);
779 if (!(CurrentNewsSrv = mutt_select_newsserver (NewsServer))) {
780 mutt_endwin (Errorbuf);
786 mutt_endwin _("No incoming mailboxes defined.");
791 mutt_select_file (folder, sizeof (folder), M_SEL_FOLDER | M_SEL_BUFFY);
799 m_strcpy(folder, sizeof(folder), NONULL(Spoolfile));
802 if (option (OPTNEWS)) {
803 unset_option (OPTNEWS);
804 nntp_expand_path (folder, sizeof (folder),
805 &CurrentNewsSrv->conn->account);
808 mutt_expand_path (folder, sizeof (folder));
810 m_strreplace(&CurrentFolder, folder);
811 m_strreplace(&LastFolder, folder);
813 if (flags & M_IGNORE) {
814 /* check to see if there are any messages in the folder */
815 switch (mx_check_empty (folder)) {
817 mutt_endwin (strerror (errno));
820 mutt_endwin _("Mailbox is empty.");
825 mutt_folder_hook (folder);
827 if ((Context = mx_open_mailbox (folder, ((flags & M_RO)
828 || option (OPTREADONLY)) ?
829 M_READONLY : 0, NULL))
830 || !explicit_folder) {
832 if (option (OPTXTERMSETTITLES))
833 printf("\033]2;%s\007", NONULL(XtermLeave));
837 mutt_endwin (Errorbuf);