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"
35 #include "mutt_sasl.h"
45 #include <stringprep.h>
53 #if defined(HAVE_QDBM)
55 #elif defined(HAVE_GDBM)
60 #include <gnutls/gnutls.h>
63 static const char *Notice = N_("\
64 Copyright (C) 1996-2002 Michael R. Elkins and others.\n\
65 Copyright (C) 2005 The Mutt-ng Team\n\
66 Copyright (C) 2006 Pierre Habouzit undation, Inc.\n\
67 This is free software. You may redistribute copies of it under the terms of\n\
68 the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.\n\
69 There is NO WARRANTY, to the extent permitted by law.\n");
71 void mutt_exit (int code)
77 static void mutt_usage (void)
79 puts (mutt_make_version());
82 _("usage: madmutt [ -nRyzZ ] [ -e <cmd> ] [ -F <file> ] [ -m <type> ] [ -f <file> ]\n\
83 madmutt [ -nR ] [ -e <cmd> ] [ -F <file> ] -Q <query> [ -Q <query> ] [...]\n\
84 madmutt [ -nR ] [ -e <cmd> ] [ -F <file> ] -A <alias> [ -A <alias> ] [...]\n\
85 madmutt [ -nR ] [ -e <cmd> ] [ -F <file> ] -t");
88 madmutt [ -nR ] [ -e <cmd> ] [ -F <file> ] -T\n\
89 madmutt [ -nx ] [ -e <cmd> ] [ -a <file> ] [ -F <file> ] [ -H <file> ] [ -i <file> ] [ -s <subj> ] [ -b <addr> ] [ -c <addr> ] <addr> [ ... ]\n\
90 madmutt [ -n ] [ -e <cmd> ] [ -F <file> ] -p\n\
94 -A <alias>\texpand the given alias\n\
95 -a <file>\tattach a file to the message\n\
96 -b <address>\tspecify a blind carbon-copy (BCC) address\n\
97 -c <address>\tspecify a carbon-copy (CC) address");
99 -e <command>\tspecify a command to be executed after initialization\n\
100 -f <file>\tspecify which mailbox to read\n\
101 -F <file>\tspecify an alternate muttrngc file\n\
102 -g <server>\tspecify a newsserver (if compiled with NNTP)\n\
103 -G\t\tselect a newsgroup (if compiled with NNTP)\n\
104 -H <file>\tspecify a draft file to read header and body from\n\
105 -i <file>\tspecify a file which Madmutt should include in the body");
107 -d <level>\t specify debugging level of Madmutt\n\
108 -m <type>\tspecify a default mailbox type\n\
109 -n\t\tcauses Madmutt not to read the system Madmuttrc\n\
110 -p\t\trecall a postponed message\n\
111 -Q <variable>\tquery a configuration variable\n\
112 -R\t\topen mailbox in read-only mode\n\
113 -s <subj>\tspecify a subject (must be in quotes if it has spaces)");
115 -t\t\tprint the value of all variables to stdout\n\
116 -T\t\tprint the value of all changed variables to stdout\n\
117 -v\t\tshow version and compile-time definitions\n\
118 -y\t\tselect a mailbox specified in your `mailboxes' list\n\
119 -z\t\texit immediately if there are no messages in the mailbox\n\
120 -Z\t\topen the first folder with new message, exit immediately if none\n\
121 -h\t\tthis help message");
126 static void show_version (void)
130 puts (mutt_make_version());
136 printf ("System:\n %s %s.%s", uts.sysname, uts.version, uts.release);
138 printf ("System:\n SCO %s", uts.release);
140 printf ("System:\n %s %s", uts.sysname, uts.release);
143 printf (" (%s)\nExternal Libraries:\n", uts.machine);
145 #ifdef NCURSES_VERSION
146 printf (" ncurses %s\n", NCURSES_VERSION);
149 #ifdef _LIBICONV_VERSION
150 printf (" libiconv %d.%d\n", _LIBICONV_VERSION >> 8,
151 _LIBICONV_VERSION & 0xff);
155 printf (" libidn %s (compiled with %s)\n",
156 stringprep_check_version (NULL), STRINGPREP_VERSION);
160 #if defined(HAVE_QDBM)
161 printf (" qdbm %s\n", dpversion);
162 #elif defined(HAVE_GDBM)
163 printf (" gdbm %s\n", gdbm_version);
167 printf (" gnutls %s\n", LIBGNUTLS_VERSION);
168 printf (" gpgme %s\n", GPGME_VERSION);
169 puts (_("Compile Options:"));
206 puts (_("Built-In Defaults:"));
207 printf (" +SENDMAIL=\"%s\"\n", SENDMAIL);
208 printf (" +MAILPATH=\"%s\"\n", MAILPATH);
209 printf (" +PKGDATADIR=\"%s\"\n", PKGDATADIR);
210 printf (" +PKGDOCDIR=\"%s\"\n", PKGDOCDIR);
211 printf (" +SYSCONFDIR=\"%s\"\n", SYSCONFDIR);
213 puts (_("MadMutt is based on Madmutt wich was based on Mutt before\n"));
218 static void start_curses (void)
220 km_init (); /* must come before mutt_init */
221 mutt_signal_initialize ();
222 if (initscr () == NULL) {
223 puts _("Error initializing terminal.");
227 mutt_signal_initialize ();
229 keypad (stdscr, TRUE);
232 typeahead (-1); /* simulate smooth scrolling */
236 #define M_IGNORE (1<<0) /* -z */
237 #define M_BUFFY (1<<1) /* -Z */
238 #define M_NOSYSRC (1<<2) /* -n */
239 #define M_RO (1<<3) /* -R */
240 #define M_SELECT (1<<4) /* -y */
242 #define M_NEWS (1<<5) /* -g and -G */
245 __attribute__((format(printf, 1, 0)))
246 static void mutt_nocurses_error (const char *fmt, ...)
251 vfprintf(stderr, fmt, ap);
256 int main (int argc, char **argv)
258 char folder[_POSIX_PATH_MAX] = "";
259 char *subject = NULL;
260 char *includeFile = NULL;
261 char *draftFile = NULL;
262 char *newMagic = NULL;
264 string_list_t *attach = NULL;
265 string_list_t *commands = NULL;
266 string_list_t *queries = NULL;
267 string_list_t *alias_queries = NULL;
272 int explicit_folder = 0;
273 int dump_variables = -1;
275 /* initialize random number for tmp file creation */
276 srand48((unsigned int) time (NULL));
278 /* sanity check against stupid administrators */
280 if (getegid () != getgid ()) {
281 fprintf (stderr, "%s: I don't want to run with privileges!\n", argv[0]);
285 setlocale (LC_ALL, "");
286 bindtextdomain (PACKAGE, MUTTLOCALEDIR);
287 textdomain (PACKAGE);
288 setlocale (LC_CTYPE, "");
290 mutt_error = mutt_message = mutt_nocurses_error;
291 srand48 (time (NULL));
294 p_clear(Options, countof(Options));
295 p_clear(QuadOptions, countof(QuadOptions));
297 while ((i = getopt(argc, argv, "A:a:b:F:f:c:e:H:s:i:hm:npQ:RTtvyzZ"
304 alias_queries = mutt_add_list (alias_queries, optarg);
307 if (strlen(optarg)<=512)
308 attach = mutt_add_list (attach, optarg);
310 printf("too long arguments. exiting ...\n");
316 m_strreplace(&Muttrc, optarg);
320 m_strcpy(folder, sizeof(folder), optarg);
329 msg->env = envelope_new();
331 msg->env->bcc = rfc822_parse_adrlist (msg->env->bcc, optarg);
333 msg->env->cc = rfc822_parse_adrlist (msg->env->cc, optarg);
345 commands = mutt_add_list (commands, optarg);
353 includeFile = optarg;
357 /* should take precedence over .muttrc setting, so save it for later */
366 sendflags |= SENDPOSTPONED;
370 queries = mutt_add_list (queries, optarg);
374 flags |= M_RO; /* read-only mode */
385 case 'y': /* My special hack mode */
390 case 'g': /* Specify a newsserver */
392 char buf[LONG_STRING];
394 snprintf (buf, sizeof (buf), "set nntp_host=%s", optarg);
395 commands = mutt_add_list (commands, buf);
398 case 'G': /* List of newsgroups */
399 flags |= M_SELECT | M_NEWS;
408 flags |= M_BUFFY | M_IGNORE;
419 /* Check for a batch send. */
420 if (!isatty (0) || queries || alias_queries || dump_variables > 0) {
421 set_option (OPTNOCURSES);
422 sendflags = SENDBATCH;
425 /* This must come before mutt_init() because curses needs to be started
426 before calling the init_pair() function to set the color scheme. */
427 if (!option (OPTNOCURSES))
430 /* set defaults and read init files */
431 mutt_init (flags & M_NOSYSRC, commands);
432 string_list_wipe(&commands);
435 return mutt_query_variables (queries);
436 if (dump_variables > 0)
437 return (mutt_dump_variables (dump_variables-1));
443 for (; alias_queries; alias_queries = alias_queries->next) {
444 if ((a = address_list_dup(alias_lookup(alias_queries->data)))) {
445 /* output in machine-readable form */
446 mutt_addrlist_to_idna (a, NULL);
447 mutt_write_address_list (a, stdout, 0, 0);
448 address_list_wipe(&a);
451 printf ("%s\n", alias_queries->data);
458 mx_set_magic (newMagic);
460 if (!option (OPTNOCURSES)) {
461 SETCOLOR (MT_COLOR_NORMAL);
463 mutt_error = mutt_curses_error;
464 mutt_message = mutt_curses_message;
467 /* Create the Maildir directory if it doesn't exist. */
468 if (!option (OPTNOCURSES) && Maildir) {
470 char fpath[_POSIX_PATH_MAX];
473 m_strcpy(fpath, sizeof(fpath), Maildir);
474 mutt_expand_path (fpath, sizeof (fpath));
475 /* we're not connected yet - skip mail folder creation */
476 if (mx_get_magic (fpath) != M_IMAP)
477 if (stat (fpath, &sb) == -1 && errno == ENOENT) {
478 snprintf (mesg, sizeof (mesg), _("%s does not exist. Create it?"),
480 if (mutt_yesorno (mesg, M_YES) == M_YES) {
481 if (mkdir (fpath, 0700) == -1 && errno != EEXIST)
482 mutt_error (_("Can't create %s: %s."), Maildir, strerror (errno));
487 if (sendflags & SENDPOSTPONED) {
488 if (!option (OPTNOCURSES))
490 ci_send_message (SENDPOSTPONED, NULL, NULL, NULL, NULL);
493 else if (subject || msg || sendflags || draftFile || includeFile || attach
496 char buf[LONG_STRING];
497 char *tempfile = NULL, *infile = NULL;
498 char *bodytext = NULL;
501 if (!option (OPTNOCURSES))
511 msg->env = envelope_new();
513 for (i = optind; i < argc; i++) {
514 if (url_check_scheme (argv[i]) == U_MAILTO)
515 url_parse_mailto (msg->env, &bodytext, argv[i]);
517 msg->env->to = rfc822_parse_adrlist (msg->env->to, argv[i]);
520 if (option (OPTAUTOEDIT) && !msg->env->to && !msg->env->cc) {
521 if (!option (OPTNOCURSES))
523 fputs (_("No recipients specified.\n"), stderr);
528 msg->env->subject = m_strdup(subject);
531 infile = includeFile;
534 if (infile || bodytext) {
536 if (m_strcmp("-", infile) == 0)
539 char path[_POSIX_PATH_MAX];
541 m_strcpy(path, sizeof(path), infile);
542 mutt_expand_path (path, sizeof (path));
543 if ((fin = fopen (path, "r")) == NULL) {
544 if (!option (OPTNOCURSES))
555 msg->env = mutt_read_rfc822_header (fin, NULL, 1, 0);
557 /* is the following if still needed? */
559 fout = m_tempfile(buf, sizeof(buf), NONULL(mod_core.tmpdir), NULL);
560 tempfile = m_strdup(buf);
564 if (!option (OPTNOCURSES))
572 mutt_copy_stream (fin, fout);
574 fputs (bodytext, fout);
576 if (fin && fin != stdin)
584 string_list_t *t = attach;
589 a->next = mutt_make_file_attach (t->data);
593 msg->content = a = mutt_make_file_attach (t->data);
595 if (!option (OPTNOCURSES))
597 fprintf (stderr, _("%s: unable to attach file.\n"), t->data);
598 string_list_wipe(&attach);
603 string_list_wipe(&attach);
606 ci_send_message (sendflags, msg, tempfile, NULL, NULL);
608 if (!option (OPTNOCURSES))
612 if (flags & M_BUFFY) {
613 if (!buffy_check (0)) {
614 mutt_endwin _("No mailbox with new mail.");
619 buffy_next (folder, sizeof (folder));
621 else if (flags & M_SELECT) {
623 if (flags & M_NEWS) {
624 set_option (OPTNEWS);
625 if (!(CurrentNewsSrv = mutt_select_newsserver (NewsServer))) {
626 mutt_endwin (Errorbuf);
632 mutt_endwin _("No incoming mailboxes defined.");
637 mutt_select_file(folder, sizeof(folder), M_SEL_FOLDER | M_SEL_BUFFY,
646 m_strcpy(folder, sizeof(folder), NONULL(Spoolfile));
649 if (option (OPTNEWS)) {
650 unset_option (OPTNEWS);
651 nntp_expand_path (folder, sizeof (folder),
652 &CurrentNewsSrv->conn->account);
655 mutt_expand_path (folder, sizeof (folder));
657 m_strreplace(&CurrentFolder, folder);
658 m_strreplace(&LastFolder, folder);
660 if (flags & M_IGNORE) {
661 /* check to see if there are any messages in the folder */
662 switch (mx_check_empty (folder)) {
664 mutt_endwin (strerror (errno));
667 mutt_endwin _("Mailbox is empty.");
672 mutt_folder_hook (folder);
674 if ((Context = mx_open_mailbox (folder, ((flags & M_RO)
675 || option (OPTREADONLY)) ?
676 M_READONLY : 0, NULL))
677 || !explicit_folder) {
679 if (option (OPTXTERMSETTITLES))
680 printf("\033]2;%s\007", NONULL(XtermLeave));
684 mutt_endwin (Errorbuf);
688 mutt_sasl_shutdown();