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);
147 #elif defined(USE_SLANG_CURSES)
148 printf (" slang %d\n", SLANG_VERSION);
151 #ifdef _LIBICONV_VERSION
152 printf (" libiconv %d.%d\n", _LIBICONV_VERSION >> 8,
153 _LIBICONV_VERSION & 0xff);
157 printf (" libidn %s (compiled with %s)\n",
158 stringprep_check_version (NULL), STRINGPREP_VERSION);
162 #if defined(HAVE_QDBM)
163 printf (" qdbm %s\n", dpversion);
164 #elif defined(HAVE_GDBM)
165 printf (" gdbm %s\n", gdbm_version);
169 printf (" gnutls %s\n", LIBGNUTLS_VERSION);
170 printf (" gpgme %s\n", GPGME_VERSION);
171 puts (_("Compile Options:"));
208 puts (_("Built-In Defaults:"));
209 printf (" +SENDMAIL=\"%s\"\n", SENDMAIL);
210 printf (" +MAILPATH=\"%s\"\n", MAILPATH);
211 printf (" +PKGDATADIR=\"%s\"\n", PKGDATADIR);
212 printf (" +PKGDOCDIR=\"%s\"\n", PKGDOCDIR);
213 printf (" +SYSCONFDIR=\"%s\"\n", SYSCONFDIR);
214 printf (" +MIXMASTER=\"%s\"\n\n", MIXMASTER);
216 puts (_("MadMutt is based on Madmutt wich was based on Mutt before\n"));
221 static void start_curses (void)
223 km_init (); /* must come before mutt_init */
225 #ifdef USE_SLANG_CURSES
226 SLtt_Ignore_Beep = 1; /* don't do that #*$@^! annoying visual beep! */
227 SLsmg_Display_Eight_Bit = 128; /* characters above this are printable */
228 SLtt_set_color (0, NULL, "default", "default");
230 /* should come before initscr() so that ncurses 4.2 doesn't try to install
231 its own SIGWINCH handler */
232 mutt_signal_initialize ();
234 if (initscr () == NULL) {
235 puts _("Error initializing terminal.");
239 mutt_signal_initialize ();
241 keypad (stdscr, TRUE);
244 #ifdef HAVE_TYPEAHEAD
245 typeahead (-1); /* simulate smooth scrolling */
252 #define M_IGNORE (1<<0) /* -z */
253 #define M_BUFFY (1<<1) /* -Z */
254 #define M_NOSYSRC (1<<2) /* -n */
255 #define M_RO (1<<3) /* -R */
256 #define M_SELECT (1<<4) /* -y */
258 #define M_NEWS (1<<5) /* -g and -G */
261 __attribute__((format(printf, 1, 0)))
262 static void mutt_nocurses_error (const char *fmt, ...)
267 vfprintf(stderr, fmt, ap);
272 int main (int argc, char **argv)
274 char folder[_POSIX_PATH_MAX] = "";
275 char *subject = NULL;
276 char *includeFile = NULL;
277 char *draftFile = NULL;
278 char *newMagic = NULL;
280 string_list_t *attach = NULL;
281 string_list_t *commands = NULL;
282 string_list_t *queries = NULL;
283 string_list_t *alias_queries = NULL;
288 int explicit_folder = 0;
289 int dump_variables = -1;
291 /* initialize random number for tmp file creation */
292 srand48((unsigned int) time (NULL));
294 /* sanity check against stupid administrators */
296 if (getegid () != getgid ()) {
297 fprintf (stderr, "%s: I don't want to run with privileges!\n", argv[0]);
301 setlocale (LC_ALL, "");
302 bindtextdomain (PACKAGE, MUTTLOCALEDIR);
303 textdomain (PACKAGE);
304 setlocale (LC_CTYPE, "");
306 mutt_error = mutt_message = mutt_nocurses_error;
307 srand48 (time (NULL));
310 p_clear(Options, countof(Options));
311 p_clear(QuadOptions, countof(QuadOptions));
313 while ((i = getopt(argc, argv, "A:a:b:F:f:c:e:H:s:i:hm:npQ:RTtvyzZ"
320 alias_queries = mutt_add_list (alias_queries, optarg);
323 if (strlen(optarg)<=512)
324 attach = mutt_add_list (attach, optarg);
326 printf("too long arguments. exiting ...\n");
332 m_strreplace(&Muttrc, optarg);
336 m_strcpy(folder, sizeof(folder), optarg);
345 msg->env = envelope_new();
347 msg->env->bcc = rfc822_parse_adrlist (msg->env->bcc, optarg);
349 msg->env->cc = rfc822_parse_adrlist (msg->env->cc, optarg);
361 commands = mutt_add_list (commands, optarg);
369 includeFile = optarg;
373 /* should take precedence over .muttrc setting, so save it for later */
382 sendflags |= SENDPOSTPONED;
386 queries = mutt_add_list (queries, optarg);
390 flags |= M_RO; /* read-only mode */
401 case 'y': /* My special hack mode */
406 case 'g': /* Specify a newsserver */
408 char buf[LONG_STRING];
410 snprintf (buf, sizeof (buf), "set nntp_host=%s", optarg);
411 commands = mutt_add_list (commands, buf);
414 case 'G': /* List of newsgroups */
415 flags |= M_SELECT | M_NEWS;
424 flags |= M_BUFFY | M_IGNORE;
435 /* Check for a batch send. */
436 if (!isatty (0) || queries || alias_queries || dump_variables > 0) {
437 set_option (OPTNOCURSES);
438 sendflags = SENDBATCH;
441 /* This must come before mutt_init() because curses needs to be started
442 before calling the init_pair() function to set the color scheme. */
443 if (!option (OPTNOCURSES))
446 /* set defaults and read init files */
447 mutt_init (flags & M_NOSYSRC, commands);
448 string_list_wipe(&commands);
451 return mutt_query_variables (queries);
452 if (dump_variables > 0)
453 return (mutt_dump_variables (dump_variables-1));
459 for (; alias_queries; alias_queries = alias_queries->next) {
460 if ((a = address_list_dup(alias_lookup(alias_queries->data)))) {
461 /* output in machine-readable form */
462 mutt_addrlist_to_idna (a, NULL);
463 mutt_write_address_list (a, stdout, 0, 0);
464 address_list_wipe(&a);
467 printf ("%s\n", alias_queries->data);
474 mx_set_magic (newMagic);
476 if (!option (OPTNOCURSES)) {
477 SETCOLOR (MT_COLOR_NORMAL);
479 mutt_error = mutt_curses_error;
480 mutt_message = mutt_curses_message;
483 /* Create the Maildir directory if it doesn't exist. */
484 if (!option (OPTNOCURSES) && Maildir) {
486 char fpath[_POSIX_PATH_MAX];
489 m_strcpy(fpath, sizeof(fpath), Maildir);
490 mutt_expand_path (fpath, sizeof (fpath));
491 /* we're not connected yet - skip mail folder creation */
492 if (mx_get_magic (fpath) != M_IMAP)
493 if (stat (fpath, &sb) == -1 && errno == ENOENT) {
494 snprintf (mesg, sizeof (mesg), _("%s does not exist. Create it?"),
496 if (mutt_yesorno (mesg, M_YES) == M_YES) {
497 if (mkdir (fpath, 0700) == -1 && errno != EEXIST)
498 mutt_error (_("Can't create %s: %s."), Maildir, strerror (errno));
503 if (sendflags & SENDPOSTPONED) {
504 if (!option (OPTNOCURSES))
506 ci_send_message (SENDPOSTPONED, NULL, NULL, NULL, NULL);
509 else if (subject || msg || sendflags || draftFile || includeFile || attach
512 char buf[LONG_STRING];
513 char *tempfile = NULL, *infile = NULL;
514 char *bodytext = NULL;
517 if (!option (OPTNOCURSES))
527 msg->env = envelope_new();
529 for (i = optind; i < argc; i++) {
530 if (url_check_scheme (argv[i]) == U_MAILTO)
531 url_parse_mailto (msg->env, &bodytext, argv[i]);
533 msg->env->to = rfc822_parse_adrlist (msg->env->to, argv[i]);
536 if (option (OPTAUTOEDIT) && !msg->env->to && !msg->env->cc) {
537 if (!option (OPTNOCURSES))
539 fputs (_("No recipients specified.\n"), stderr);
544 msg->env->subject = m_strdup(subject);
547 infile = includeFile;
550 if (infile || bodytext) {
552 if (m_strcmp("-", infile) == 0)
555 char path[_POSIX_PATH_MAX];
557 m_strcpy(path, sizeof(path), infile);
558 mutt_expand_path (path, sizeof (path));
559 if ((fin = fopen (path, "r")) == NULL) {
560 if (!option (OPTNOCURSES))
571 msg->env = mutt_read_rfc822_header (fin, NULL, 1, 0);
573 /* is the following if still needed? */
575 fout = m_tempfile(buf, sizeof(buf), NONULL(mod_core.tmpdir), NULL);
576 tempfile = m_strdup(buf);
580 if (!option (OPTNOCURSES))
588 mutt_copy_stream (fin, fout);
590 fputs (bodytext, fout);
592 if (fin && fin != stdin)
600 string_list_t *t = attach;
605 a->next = mutt_make_file_attach (t->data);
609 msg->content = a = mutt_make_file_attach (t->data);
611 if (!option (OPTNOCURSES))
613 fprintf (stderr, _("%s: unable to attach file.\n"), t->data);
614 string_list_wipe(&attach);
619 string_list_wipe(&attach);
622 ci_send_message (sendflags, msg, tempfile, NULL, NULL);
624 if (!option (OPTNOCURSES))
628 if (flags & M_BUFFY) {
629 if (!buffy_check (0)) {
630 mutt_endwin _("No mailbox with new mail.");
635 buffy_next (folder, sizeof (folder));
637 else if (flags & M_SELECT) {
639 if (flags & M_NEWS) {
640 set_option (OPTNEWS);
641 if (!(CurrentNewsSrv = mutt_select_newsserver (NewsServer))) {
642 mutt_endwin (Errorbuf);
648 mutt_endwin _("No incoming mailboxes defined.");
653 mutt_select_file(folder, sizeof(folder), M_SEL_FOLDER | M_SEL_BUFFY,
662 m_strcpy(folder, sizeof(folder), NONULL(Spoolfile));
665 if (option (OPTNEWS)) {
666 unset_option (OPTNEWS);
667 nntp_expand_path (folder, sizeof (folder),
668 &CurrentNewsSrv->conn->account);
671 mutt_expand_path (folder, sizeof (folder));
673 m_strreplace(&CurrentFolder, folder);
674 m_strreplace(&LastFolder, folder);
676 if (flags & M_IGNORE) {
677 /* check to see if there are any messages in the folder */
678 switch (mx_check_empty (folder)) {
680 mutt_endwin (strerror (errno));
683 mutt_endwin _("Mailbox is empty.");
688 mutt_folder_hook (folder);
690 if ((Context = mx_open_mailbox (folder, ((flags & M_RO)
691 || option (OPTREADONLY)) ?
692 M_READONLY : 0, NULL))
693 || !explicit_folder) {
695 if (option (OPTXTERMSETTITLES))
696 printf("\033]2;%s\007", NONULL(XtermLeave));
700 mutt_endwin (Errorbuf);
704 mutt_sasl_shutdown();