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)
65 #include <gnutls/gnutls.h>
68 static const char *Notice = N_("\
69 Copyright (C) 1996-2002 Michael R. Elkins and others.\n\
70 Copyright (C) 2005 The Mutt-ng Team\n\
71 Copyright (C) 2006 Pierre Habouzit undation, Inc.\n\
72 This is free software. You may redistribute copies of it under the terms of\n\
73 the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.\n\
74 There is NO WARRANTY, to the extent permitted by law.\n");
76 void mutt_exit (int code)
82 static void mutt_usage (void)
84 puts (mutt_make_version());
87 _("usage: madmutt [ -nRyzZ ] [ -e <cmd> ] [ -F <file> ] [ -m <type> ] [ -f <file> ]\n\
88 madmutt [ -nR ] [ -e <cmd> ] [ -F <file> ] -Q <query> [ -Q <query> ] [...]\n\
89 madmutt [ -nR ] [ -e <cmd> ] [ -F <file> ] -A <alias> [ -A <alias> ] [...]\n\
90 madmutt [ -nR ] [ -e <cmd> ] [ -F <file> ] -t");
93 madmutt [ -nR ] [ -e <cmd> ] [ -F <file> ] -T\n\
94 madmutt [ -nx ] [ -e <cmd> ] [ -a <file> ] [ -F <file> ] [ -H <file> ] [ -i <file> ] [ -s <subj> ] [ -b <addr> ] [ -c <addr> ] <addr> [ ... ]\n\
95 madmutt [ -n ] [ -e <cmd> ] [ -F <file> ] -p\n\
99 -A <alias>\texpand the given alias\n\
100 -a <file>\tattach a file to the message\n\
101 -b <address>\tspecify a blind carbon-copy (BCC) address\n\
102 -c <address>\tspecify a carbon-copy (CC) address");
104 -e <command>\tspecify a command to be executed after initialization\n\
105 -f <file>\tspecify which mailbox to read\n\
106 -F <file>\tspecify an alternate muttrngc file\n\
107 -g <server>\tspecify a newsserver (if compiled with NNTP)\n\
108 -G\t\tselect a newsgroup (if compiled with NNTP)\n\
109 -H <file>\tspecify a draft file to read header and body from\n\
110 -i <file>\tspecify a file which Madmutt should include in the body");
112 -d <level>\t specify debugging level of Madmutt\n\
113 -m <type>\tspecify a default mailbox type\n\
114 -n\t\tcauses Madmutt not to read the system Madmuttrc\n\
115 -p\t\trecall a postponed message\n\
116 -Q <variable>\tquery a configuration variable\n\
117 -R\t\topen mailbox in read-only mode\n\
118 -s <subj>\tspecify a subject (must be in quotes if it has spaces)");
120 -t\t\tprint the value of all variables to stdout\n\
121 -T\t\tprint the value of all changed variables to stdout\n\
122 -v\t\tshow version and compile-time definitions\n\
123 -y\t\tselect a mailbox specified in your `mailboxes' list\n\
124 -z\t\texit immediately if there are no messages in the mailbox\n\
125 -Z\t\topen the first folder with new message, exit immediately if none\n\
126 -h\t\tthis help message");
131 static void show_version (void)
138 puts (mutt_make_version());
144 printf ("System:\n %s %s.%s", uts.sysname, uts.version, uts.release);
146 printf ("System:\n SCO %s", uts.release);
148 printf ("System:\n %s %s", uts.sysname, uts.release);
151 printf (" (%s)\nExternal Libraries:\n", uts.machine);
153 #ifdef NCURSES_VERSION
154 printf (" ncurses %s\n", NCURSES_VERSION);
155 #elif defined(USE_SLANG_CURSES)
156 printf (" slang %d\n", SLANG_VERSION);
159 #ifdef _LIBICONV_VERSION
160 printf (" libiconv %d.%d\n", _LIBICONV_VERSION >> 8,
161 _LIBICONV_VERSION & 0xff);
165 printf (" libidn %s (compiled with %s)\n",
166 stringprep_check_version (NULL), STRINGPREP_VERSION);
170 smtp_version (buf, sizeof (buf), 0);
171 printf (" libESMTP %s\n", buf);
175 #if defined(HAVE_QDBM)
176 printf (" qdbm %s\n", dpversion);
177 #elif defined(HAVE_GDBM)
178 printf (" gdbm %s\n", gdbm_version);
179 #elif defined(HAVE_DB4)
180 printf (" DB4 %d.%d.%d\n", DB_VERSION_MAJOR, DB_VERSION_MINOR,
185 printf (" gnutls %s\n", LIBGNUTLS_VERSION);
186 printf (" gpgme %s\n", GPGME_VERSION);
187 puts (_("Compile Options:"));
234 #ifdef HAVE_START_COLOR
239 #ifdef HAVE_TYPEAHEAD
260 #ifdef HAVE_RESIZETERM
268 "+CRYPT_BACKEND_GPGME "
280 #ifdef HAVE_LANGINFO_CODESET
281 "+HAVE_LANGINFO_CODESET "
283 "-HAVE_LANGINFO_CODESET "
285 #ifdef HAVE_LANGINFO_YESEXPR
286 "+HAVE_LANGINFO_YESEXPR "
288 "-HAVE_LANGINFO_YESEXPR "
308 puts (_("Built-In Defaults:"));
309 printf (" +SENDMAIL=\"%s\"\n", SENDMAIL);
310 printf (" +MAILPATH=\"%s\"\n", MAILPATH);
311 printf (" +PKGDATADIR=\"%s\"\n", PKGDATADIR);
312 printf (" +PKGDOCDIR=\"%s\"\n", PKGDOCDIR);
313 printf (" +SYSCONFDIR=\"%s\"\n", SYSCONFDIR);
314 printf (" +MIXMASTER=\"%s\"\n\n", MIXMASTER);
316 puts (_("MadMutt is based on Madmutt wich was based on Mutt before\n"));
321 static void start_curses (void)
323 km_init (); /* must come before mutt_init */
325 #ifdef USE_SLANG_CURSES
326 SLtt_Ignore_Beep = 1; /* don't do that #*$@^! annoying visual beep! */
327 SLsmg_Display_Eight_Bit = 128; /* characters above this are printable */
328 SLtt_set_color (0, NULL, "default", "default");
330 /* should come before initscr() so that ncurses 4.2 doesn't try to install
331 its own SIGWINCH handler */
332 mutt_signal_initialize ();
334 if (initscr () == NULL) {
335 puts _("Error initializing terminal.");
339 mutt_signal_initialize ();
341 keypad (stdscr, TRUE);
344 #ifdef HAVE_TYPEAHEAD
345 typeahead (-1); /* simulate smooth scrolling */
352 #define M_IGNORE (1<<0) /* -z */
353 #define M_BUFFY (1<<1) /* -Z */
354 #define M_NOSYSRC (1<<2) /* -n */
355 #define M_RO (1<<3) /* -R */
356 #define M_SELECT (1<<4) /* -y */
358 #define M_NEWS (1<<5) /* -g and -G */
361 __attribute__((format(printf, 1, 0)))
362 static void mutt_nocurses_error (const char *fmt, ...)
367 vfprintf(stderr, fmt, ap);
372 int main (int argc, char **argv)
374 char folder[_POSIX_PATH_MAX] = "";
375 char *subject = NULL;
376 char *includeFile = NULL;
377 char *draftFile = NULL;
378 char *newMagic = NULL;
380 string_list_t *attach = NULL;
381 string_list_t *commands = NULL;
382 string_list_t *queries = NULL;
383 string_list_t *alias_queries = NULL;
388 int explicit_folder = 0;
389 int dump_variables = -1;
391 /* initialize random number for tmp file creation */
392 srand48((unsigned int) time (NULL));
394 /* sanity check against stupid administrators */
396 if (getegid () != getgid ()) {
397 fprintf (stderr, "%s: I don't want to run with privileges!\n", argv[0]);
402 /* FIXME what about init.c:1439 ? */
403 setlocale (LC_ALL, "");
404 bindtextdomain (PACKAGE, MUTTLOCALEDIR);
405 textdomain (PACKAGE);
408 setlocale (LC_CTYPE, "");
410 mutt_error = mutt_message = mutt_nocurses_error;
411 srand48 (time (NULL));
414 p_clear(Options, countof(Options));
415 p_clear(QuadOptions, countof(QuadOptions));
417 while ((i = getopt(argc, argv, "A:a:b:F:f:c:e:H:s:i:hm:npQ:RTtvyzZ"
424 alias_queries = mutt_add_list (alias_queries, optarg);
427 if (strlen(optarg)<=512)
428 attach = mutt_add_list (attach, optarg);
430 printf("too long arguments. exiting ...\n");
436 m_strreplace(&Muttrc, optarg);
440 m_strcpy(folder, sizeof(folder), optarg);
449 msg->env = envelope_new();
451 msg->env->bcc = rfc822_parse_adrlist (msg->env->bcc, optarg);
453 msg->env->cc = rfc822_parse_adrlist (msg->env->cc, optarg);
465 commands = mutt_add_list (commands, optarg);
473 includeFile = optarg;
477 /* should take precedence over .muttrc setting, so save it for later */
486 sendflags |= SENDPOSTPONED;
490 queries = mutt_add_list (queries, optarg);
494 flags |= M_RO; /* read-only mode */
505 case 'y': /* My special hack mode */
510 case 'g': /* Specify a newsserver */
512 char buf[LONG_STRING];
514 snprintf (buf, sizeof (buf), "set nntp_host=%s", optarg);
515 commands = mutt_add_list (commands, buf);
518 case 'G': /* List of newsgroups */
519 flags |= M_SELECT | M_NEWS;
528 flags |= M_BUFFY | M_IGNORE;
539 /* Check for a batch send. */
540 if (!isatty (0) || queries || alias_queries || dump_variables > 0) {
541 set_option (OPTNOCURSES);
542 sendflags = SENDBATCH;
545 /* This must come before mutt_init() because curses needs to be started
546 before calling the init_pair() function to set the color scheme. */
547 if (!option (OPTNOCURSES))
550 /* set defaults and read init files */
551 mutt_init (flags & M_NOSYSRC, commands);
552 string_list_wipe(&commands);
555 return mutt_query_variables (queries);
556 if (dump_variables > 0)
557 return (mutt_dump_variables (dump_variables-1));
563 for (; alias_queries; alias_queries = alias_queries->next) {
564 if ((a = address_list_dup(alias_lookup(alias_queries->data)))) {
565 /* output in machine-readable form */
566 mutt_addrlist_to_idna (a, NULL);
567 mutt_write_address_list (a, stdout, 0, 0);
568 address_list_wipe(&a);
571 printf ("%s\n", alias_queries->data);
578 mx_set_magic (newMagic);
580 if (!option (OPTNOCURSES)) {
581 SETCOLOR (MT_COLOR_NORMAL);
583 mutt_error = mutt_curses_error;
584 mutt_message = mutt_curses_message;
587 /* Create the Maildir directory if it doesn't exist. */
588 if (!option (OPTNOCURSES) && Maildir) {
590 char fpath[_POSIX_PATH_MAX];
593 m_strcpy(fpath, sizeof(fpath), Maildir);
594 mutt_expand_path (fpath, sizeof (fpath));
595 /* we're not connected yet - skip mail folder creation */
596 if (mx_get_magic (fpath) != M_IMAP)
597 if (stat (fpath, &sb) == -1 && errno == ENOENT) {
598 snprintf (mesg, sizeof (mesg), _("%s does not exist. Create it?"),
600 if (mutt_yesorno (mesg, M_YES) == M_YES) {
601 if (mkdir (fpath, 0700) == -1 && errno != EEXIST)
602 mutt_error (_("Can't create %s: %s."), Maildir, strerror (errno));
607 if (sendflags & SENDPOSTPONED) {
608 if (!option (OPTNOCURSES))
610 ci_send_message (SENDPOSTPONED, NULL, NULL, NULL, NULL);
613 else if (subject || msg || sendflags || draftFile || includeFile || attach
616 char buf[LONG_STRING];
617 char *tempfile = NULL, *infile = NULL;
618 char *bodytext = NULL;
621 if (!option (OPTNOCURSES))
631 msg->env = envelope_new();
633 for (i = optind; i < argc; i++) {
634 if (url_check_scheme (argv[i]) == U_MAILTO)
635 url_parse_mailto (msg->env, &bodytext, argv[i]);
637 msg->env->to = rfc822_parse_adrlist (msg->env->to, argv[i]);
640 if (option (OPTAUTOEDIT) && !msg->env->to && !msg->env->cc) {
641 if (!option (OPTNOCURSES))
643 fputs (_("No recipients specified.\n"), stderr);
648 msg->env->subject = m_strdup(subject);
651 infile = includeFile;
654 if (infile || bodytext) {
656 if (m_strcmp("-", infile) == 0)
659 char path[_POSIX_PATH_MAX];
661 m_strcpy(path, sizeof(path), infile);
662 mutt_expand_path (path, sizeof (path));
663 if ((fin = fopen (path, "r")) == NULL) {
664 if (!option (OPTNOCURSES))
675 msg->env = mutt_read_rfc822_header (fin, NULL, 1, 0);
677 /* is the following if still needed? */
679 fout = m_tempfile(buf, sizeof(buf), NONULL(MCore.tmpdir), NULL);
680 tempfile = m_strdup(buf);
684 if (!option (OPTNOCURSES))
692 mutt_copy_stream (fin, fout);
694 fputs (bodytext, fout);
696 if (fin && fin != stdin)
704 string_list_t *t = attach;
709 a->next = mutt_make_file_attach (t->data);
713 msg->content = a = mutt_make_file_attach (t->data);
715 if (!option (OPTNOCURSES))
717 fprintf (stderr, _("%s: unable to attach file.\n"), t->data);
718 string_list_wipe(&attach);
723 string_list_wipe(&attach);
726 ci_send_message (sendflags, msg, tempfile, NULL, NULL);
728 if (!option (OPTNOCURSES))
732 if (flags & M_BUFFY) {
733 if (!buffy_check (0)) {
734 mutt_endwin _("No mailbox with new mail.");
739 buffy_next (folder, sizeof (folder));
741 else if (flags & M_SELECT) {
743 if (flags & M_NEWS) {
744 set_option (OPTNEWS);
745 if (!(CurrentNewsSrv = mutt_select_newsserver (NewsServer))) {
746 mutt_endwin (Errorbuf);
752 mutt_endwin _("No incoming mailboxes defined.");
757 mutt_select_file (folder, sizeof (folder), M_SEL_FOLDER | M_SEL_BUFFY);
765 m_strcpy(folder, sizeof(folder), NONULL(Spoolfile));
768 if (option (OPTNEWS)) {
769 unset_option (OPTNEWS);
770 nntp_expand_path (folder, sizeof (folder),
771 &CurrentNewsSrv->conn->account);
774 mutt_expand_path (folder, sizeof (folder));
776 m_strreplace(&CurrentFolder, folder);
777 m_strreplace(&LastFolder, folder);
779 if (flags & M_IGNORE) {
780 /* check to see if there are any messages in the folder */
781 switch (mx_check_empty (folder)) {
783 mutt_endwin (strerror (errno));
786 mutt_endwin _("Mailbox is empty.");
791 mutt_folder_hook (folder);
793 if ((Context = mx_open_mailbox (folder, ((flags & M_RO)
794 || option (OPTREADONLY)) ?
795 M_READONLY : 0, NULL))
796 || !explicit_folder) {
798 if (option (OPTXTERMSETTITLES))
799 printf("\033]2;%s\007", NONULL(XtermLeave));
803 mutt_endwin (Errorbuf);