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>
49 #include <nntp/nntp.h>
57 #if defined(HAVE_QDBM)
59 #elif defined(HAVE_GDBM)
64 #include <gnutls/gnutls.h>
67 static const char *Notice = N_("\
68 Copyright (C) 1996-2002 Michael R. Elkins and others.\n\
69 Copyright (C) 2005 The Mutt-ng Team\n\
70 Copyright (C) 2006 Pierre Habouzit undation, Inc.\n\
71 This is free software. You may redistribute copies of it under the terms of\n\
72 the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.\n\
73 There is NO WARRANTY, to the extent permitted by law.\n");
75 void mutt_exit (int code)
81 static void mutt_usage (void)
83 puts (mutt_make_version());
86 _("usage: madmutt [ -nRyzZ ] [ -e <cmd> ] [ -F <file> ] [ -m <type> ] [ -f <file> ]\n\
87 madmutt [ -nR ] [ -e <cmd> ] [ -F <file> ] -Q <query> [ -Q <query> ] [...]\n\
88 madmutt [ -nR ] [ -e <cmd> ] [ -F <file> ] -A <alias> [ -A <alias> ] [...]\n\
89 madmutt [ -nR ] [ -e <cmd> ] [ -F <file> ] -t");
92 madmutt [ -nR ] [ -e <cmd> ] [ -F <file> ] -T\n\
93 madmutt [ -nx ] [ -e <cmd> ] [ -a <file> ] [ -F <file> ] [ -H <file> ] [ -i <file> ] [ -s <subj> ] [ -b <addr> ] [ -c <addr> ] <addr> [ ... ]\n\
94 madmutt [ -n ] [ -e <cmd> ] [ -F <file> ] -p\n\
98 -A <alias>\texpand the given alias\n\
99 -a <file>\tattach a file to the message\n\
100 -b <address>\tspecify a blind carbon-copy (BCC) address\n\
101 -c <address>\tspecify a carbon-copy (CC) address");
103 -e <command>\tspecify a command to be executed after initialization\n\
104 -f <file>\tspecify which mailbox to read\n\
105 -F <file>\tspecify an alternate muttrngc file\n\
106 -g <server>\tspecify a newsserver (if compiled with NNTP)\n\
107 -G\t\tselect a newsgroup (if compiled with NNTP)\n\
108 -H <file>\tspecify a draft file to read header and body from\n\
109 -i <file>\tspecify a file which Madmutt should include in the body");
111 -d <level>\t specify debugging level of Madmutt\n\
112 -m <type>\tspecify a default mailbox type\n\
113 -n\t\tcauses Madmutt not to read the system Madmuttrc\n\
114 -p\t\trecall a postponed message\n\
115 -Q <variable>\tquery a configuration variable\n\
116 -R\t\topen mailbox in read-only mode\n\
117 -s <subj>\tspecify a subject (must be in quotes if it has spaces)");
119 -t\t\tprint the value of all variables to stdout\n\
120 -T\t\tprint the value of all changed variables to stdout\n\
121 -v\t\tshow version and compile-time definitions\n\
122 -y\t\tselect a mailbox specified in your `mailboxes' list\n\
123 -z\t\texit immediately if there are no messages in the mailbox\n\
124 -Z\t\topen the first folder with new message, exit immediately if none\n\
125 -h\t\tthis help message");
130 static void show_version (void)
137 puts (mutt_make_version());
143 printf ("System:\n %s %s.%s", uts.sysname, uts.version, uts.release);
145 printf ("System:\n SCO %s", uts.release);
147 printf ("System:\n %s %s", uts.sysname, uts.release);
150 printf (" (%s)\nExternal Libraries:\n", uts.machine);
152 #ifdef NCURSES_VERSION
153 printf (" ncurses %s\n", NCURSES_VERSION);
154 #elif defined(USE_SLANG_CURSES)
155 printf (" slang %d\n", SLANG_VERSION);
158 #ifdef _LIBICONV_VERSION
159 printf (" libiconv %d.%d\n", _LIBICONV_VERSION >> 8,
160 _LIBICONV_VERSION & 0xff);
164 printf (" libidn %s (compiled with %s)\n",
165 stringprep_check_version (NULL), STRINGPREP_VERSION);
169 smtp_version (buf, sizeof (buf), 0);
170 printf (" libESMTP %s\n", buf);
174 #if defined(HAVE_QDBM)
175 printf (" qdbm %s\n", dpversion);
176 #elif defined(HAVE_GDBM)
177 printf (" gdbm %s\n", gdbm_version);
181 printf (" gnutls %s\n", LIBGNUTLS_VERSION);
182 printf (" gpgme %s\n", GPGME_VERSION);
183 puts (_("Compile Options:"));
225 puts (_("Built-In Defaults:"));
226 printf (" +SENDMAIL=\"%s\"\n", SENDMAIL);
227 printf (" +MAILPATH=\"%s\"\n", MAILPATH);
228 printf (" +PKGDATADIR=\"%s\"\n", PKGDATADIR);
229 printf (" +PKGDOCDIR=\"%s\"\n", PKGDOCDIR);
230 printf (" +SYSCONFDIR=\"%s\"\n", SYSCONFDIR);
231 printf (" +MIXMASTER=\"%s\"\n\n", MIXMASTER);
233 puts (_("MadMutt is based on Madmutt wich was based on Mutt before\n"));
238 static void start_curses (void)
240 km_init (); /* must come before mutt_init */
242 #ifdef USE_SLANG_CURSES
243 SLtt_Ignore_Beep = 1; /* don't do that #*$@^! annoying visual beep! */
244 SLsmg_Display_Eight_Bit = 128; /* characters above this are printable */
245 SLtt_set_color (0, NULL, "default", "default");
247 /* should come before initscr() so that ncurses 4.2 doesn't try to install
248 its own SIGWINCH handler */
249 mutt_signal_initialize ();
251 if (initscr () == NULL) {
252 puts _("Error initializing terminal.");
256 mutt_signal_initialize ();
258 keypad (stdscr, TRUE);
261 #ifdef HAVE_TYPEAHEAD
262 typeahead (-1); /* simulate smooth scrolling */
269 #define M_IGNORE (1<<0) /* -z */
270 #define M_BUFFY (1<<1) /* -Z */
271 #define M_NOSYSRC (1<<2) /* -n */
272 #define M_RO (1<<3) /* -R */
273 #define M_SELECT (1<<4) /* -y */
275 #define M_NEWS (1<<5) /* -g and -G */
278 __attribute__((format(printf, 1, 0)))
279 static void mutt_nocurses_error (const char *fmt, ...)
284 vfprintf(stderr, fmt, ap);
289 int main (int argc, char **argv)
291 char folder[_POSIX_PATH_MAX] = "";
292 char *subject = NULL;
293 char *includeFile = NULL;
294 char *draftFile = NULL;
295 char *newMagic = NULL;
297 string_list_t *attach = NULL;
298 string_list_t *commands = NULL;
299 string_list_t *queries = NULL;
300 string_list_t *alias_queries = NULL;
305 int explicit_folder = 0;
306 int dump_variables = -1;
308 /* initialize random number for tmp file creation */
309 srand48((unsigned int) time (NULL));
311 /* sanity check against stupid administrators */
313 if (getegid () != getgid ()) {
314 fprintf (stderr, "%s: I don't want to run with privileges!\n", argv[0]);
318 setlocale (LC_ALL, "");
319 bindtextdomain (PACKAGE, MUTTLOCALEDIR);
320 textdomain (PACKAGE);
321 setlocale (LC_CTYPE, "");
323 mutt_error = mutt_message = mutt_nocurses_error;
324 srand48 (time (NULL));
327 p_clear(Options, countof(Options));
328 p_clear(QuadOptions, countof(QuadOptions));
330 while ((i = getopt(argc, argv, "A:a:b:F:f:c:e:H:s:i:hm:npQ:RTtvyzZ"
337 alias_queries = mutt_add_list (alias_queries, optarg);
340 if (strlen(optarg)<=512)
341 attach = mutt_add_list (attach, optarg);
343 printf("too long arguments. exiting ...\n");
349 m_strreplace(&Muttrc, optarg);
353 m_strcpy(folder, sizeof(folder), optarg);
362 msg->env = envelope_new();
364 msg->env->bcc = rfc822_parse_adrlist (msg->env->bcc, optarg);
366 msg->env->cc = rfc822_parse_adrlist (msg->env->cc, optarg);
378 commands = mutt_add_list (commands, optarg);
386 includeFile = optarg;
390 /* should take precedence over .muttrc setting, so save it for later */
399 sendflags |= SENDPOSTPONED;
403 queries = mutt_add_list (queries, optarg);
407 flags |= M_RO; /* read-only mode */
418 case 'y': /* My special hack mode */
423 case 'g': /* Specify a newsserver */
425 char buf[LONG_STRING];
427 snprintf (buf, sizeof (buf), "set nntp_host=%s", optarg);
428 commands = mutt_add_list (commands, buf);
431 case 'G': /* List of newsgroups */
432 flags |= M_SELECT | M_NEWS;
441 flags |= M_BUFFY | M_IGNORE;
452 /* Check for a batch send. */
453 if (!isatty (0) || queries || alias_queries || dump_variables > 0) {
454 set_option (OPTNOCURSES);
455 sendflags = SENDBATCH;
458 /* This must come before mutt_init() because curses needs to be started
459 before calling the init_pair() function to set the color scheme. */
460 if (!option (OPTNOCURSES))
463 /* set defaults and read init files */
464 mutt_init (flags & M_NOSYSRC, commands);
465 string_list_wipe(&commands);
468 return mutt_query_variables (queries);
469 if (dump_variables > 0)
470 return (mutt_dump_variables (dump_variables-1));
476 for (; alias_queries; alias_queries = alias_queries->next) {
477 if ((a = address_list_dup(alias_lookup(alias_queries->data)))) {
478 /* output in machine-readable form */
479 mutt_addrlist_to_idna (a, NULL);
480 mutt_write_address_list (a, stdout, 0, 0);
481 address_list_wipe(&a);
484 printf ("%s\n", alias_queries->data);
491 mx_set_magic (newMagic);
493 if (!option (OPTNOCURSES)) {
494 SETCOLOR (MT_COLOR_NORMAL);
496 mutt_error = mutt_curses_error;
497 mutt_message = mutt_curses_message;
500 /* Create the Maildir directory if it doesn't exist. */
501 if (!option (OPTNOCURSES) && Maildir) {
503 char fpath[_POSIX_PATH_MAX];
506 m_strcpy(fpath, sizeof(fpath), Maildir);
507 mutt_expand_path (fpath, sizeof (fpath));
508 /* we're not connected yet - skip mail folder creation */
509 if (mx_get_magic (fpath) != M_IMAP)
510 if (stat (fpath, &sb) == -1 && errno == ENOENT) {
511 snprintf (mesg, sizeof (mesg), _("%s does not exist. Create it?"),
513 if (mutt_yesorno (mesg, M_YES) == M_YES) {
514 if (mkdir (fpath, 0700) == -1 && errno != EEXIST)
515 mutt_error (_("Can't create %s: %s."), Maildir, strerror (errno));
520 if (sendflags & SENDPOSTPONED) {
521 if (!option (OPTNOCURSES))
523 ci_send_message (SENDPOSTPONED, NULL, NULL, NULL, NULL);
526 else if (subject || msg || sendflags || draftFile || includeFile || attach
529 char buf[LONG_STRING];
530 char *tempfile = NULL, *infile = NULL;
531 char *bodytext = NULL;
534 if (!option (OPTNOCURSES))
544 msg->env = envelope_new();
546 for (i = optind; i < argc; i++) {
547 if (url_check_scheme (argv[i]) == U_MAILTO)
548 url_parse_mailto (msg->env, &bodytext, argv[i]);
550 msg->env->to = rfc822_parse_adrlist (msg->env->to, argv[i]);
553 if (option (OPTAUTOEDIT) && !msg->env->to && !msg->env->cc) {
554 if (!option (OPTNOCURSES))
556 fputs (_("No recipients specified.\n"), stderr);
561 msg->env->subject = m_strdup(subject);
564 infile = includeFile;
567 if (infile || bodytext) {
569 if (m_strcmp("-", infile) == 0)
572 char path[_POSIX_PATH_MAX];
574 m_strcpy(path, sizeof(path), infile);
575 mutt_expand_path (path, sizeof (path));
576 if ((fin = fopen (path, "r")) == NULL) {
577 if (!option (OPTNOCURSES))
588 msg->env = mutt_read_rfc822_header (fin, NULL, 1, 0);
590 /* is the following if still needed? */
592 fout = m_tempfile(buf, sizeof(buf), NONULL(mod_core.tmpdir), NULL);
593 tempfile = m_strdup(buf);
597 if (!option (OPTNOCURSES))
605 mutt_copy_stream (fin, fout);
607 fputs (bodytext, fout);
609 if (fin && fin != stdin)
617 string_list_t *t = attach;
622 a->next = mutt_make_file_attach (t->data);
626 msg->content = a = mutt_make_file_attach (t->data);
628 if (!option (OPTNOCURSES))
630 fprintf (stderr, _("%s: unable to attach file.\n"), t->data);
631 string_list_wipe(&attach);
636 string_list_wipe(&attach);
639 ci_send_message (sendflags, msg, tempfile, NULL, NULL);
641 if (!option (OPTNOCURSES))
645 if (flags & M_BUFFY) {
646 if (!buffy_check (0)) {
647 mutt_endwin _("No mailbox with new mail.");
652 buffy_next (folder, sizeof (folder));
654 else if (flags & M_SELECT) {
656 if (flags & M_NEWS) {
657 set_option (OPTNEWS);
658 if (!(CurrentNewsSrv = mutt_select_newsserver (NewsServer))) {
659 mutt_endwin (Errorbuf);
665 mutt_endwin _("No incoming mailboxes defined.");
670 mutt_select_file (folder, sizeof (folder), M_SEL_FOLDER | M_SEL_BUFFY);
678 m_strcpy(folder, sizeof(folder), NONULL(Spoolfile));
681 if (option (OPTNEWS)) {
682 unset_option (OPTNEWS);
683 nntp_expand_path (folder, sizeof (folder),
684 &CurrentNewsSrv->conn->account);
687 mutt_expand_path (folder, sizeof (folder));
689 m_strreplace(&CurrentFolder, folder);
690 m_strreplace(&LastFolder, folder);
692 if (flags & M_IGNORE) {
693 /* check to see if there are any messages in the folder */
694 switch (mx_check_empty (folder)) {
696 mutt_endwin (strerror (errno));
699 mutt_endwin _("Mailbox is empty.");
704 mutt_folder_hook (folder);
706 if ((Context = mx_open_mailbox (folder, ((flags & M_RO)
707 || option (OPTREADONLY)) ?
708 M_READONLY : 0, NULL))
709 || !explicit_folder) {
711 if (option (OPTXTERMSETTITLES))
712 printf("\033]2;%s\007", NONULL(XtermLeave));
716 mutt_endwin (Errorbuf);
720 mutt_sasl_shutdown();