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/lib-ui.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 void mutt_exit(int code)
69 static void mutt_usage (void)
71 puts(mutt_make_version());
74 usage: madmutt [ -nRyzZ ] [ -e <cmd> ] [ -F <file> ] [ -f <file> ]\n\
75 madmutt [ -n ] [ -e <cmd> ] [ -a <file> ] [ -F <file> ] [ -H <file> ] [ -i <file> ] [ -s <subj> ] [ -b <addr> ] [ -c <addr> ] <addr> [ ... ]\n\
76 madmutt [ -n ] [ -e <cmd> ] [ -F <file> ] -p\n\
80 puts(_(" -a <file> attach a file to the message"));
81 puts(_(" -b <address> specify a blind carbon-copy (BCC) address"));
82 puts(_(" -c <address> specify a carbon-copy (CC) address"));
83 puts(_(" -e <command> specify a command to be executed after initialization"));
84 puts(_(" -f <file> specify which mailbox to read"));
85 puts(_(" -F <file> specify an alternate Madmuttrc file"));
87 puts(_(" -g <server> specify a newsserver"));
88 puts(_(" -G select a newsgroup"));
90 puts(_(" -H <file> specify a draft file to read header and body from"));
91 puts(_(" -i <file> specify a file which Madmutt should include in the body"));
92 puts(_(" -n causes Madmutt not to read the system Madmuttrc"));
93 puts(_(" -p recall a postponed message"));
94 puts(_(" -R open mailbox in read-only mode"));
95 puts(_(" -s <subj> specify a subject (must be in quotes if it has spaces)"));
96 puts(_(" -v show version and compile-time definitions"));
97 puts(_(" -y select a mailbox specified in your `mailboxes' list"));
98 puts(_(" -z exit immediately if there are no messages in the mailbox"));
99 puts(_(" -Z open the first folder with new message, exit immediately if none"));
100 puts(_(" -h this help message"));
105 static void show_version (void)
110 puts(mutt_make_version());
111 puts(_(" Copyright (C) 1996-2002 Michael R. Elkins and others."));
112 puts(_(" Copyright (C) 2005 The Mutt-ng Team"));
113 puts(_(" Copyright (C) 2006-2007 Pierre Habouzit"));
114 puts(_(" MadMutt is based on Mutt-ng wich was based on Mutt before"));
117 printf("System:\n %s %s (%s)\n", uts.sysname, uts.release, uts.machine);
118 puts("External Libraries:");
119 #ifdef NCURSES_VERSION
120 printf(" ncurses %s\n", NCURSES_VERSION);
122 #ifdef _LIBICONV_VERSION
123 printf(" libiconv %d.%d\n", _LIBICONV_VERSION >> 8,
124 _LIBICONV_VERSION & 0xff);
126 #ifdef STRINGPREP_VERSION
127 printf(" libidn %s\n", STRINGPREP_VERSION);
130 #if defined(HAVE_QDBM)
131 printf(" qdbm %s\n", dpversion);
132 #elif defined(HAVE_GDBM)
133 printf(" gdbm %s\n", gdbm_version);
136 printf(" gnutls %s\n", LIBGNUTLS_VERSION);
137 printf(" gpgme %s\n", GPGME_VERSION);
138 puts (_("Compile Options:"));
173 puts(_("Built-In Defaults:"));
174 printf(" SENDMAIL \"%s\"\n", SENDMAIL);
175 printf(" MAILPATH \"%s\"\n", MAILPATH);
176 printf(" PKGDATADIR \"%s\"\n", PKGDATADIR);
177 printf(" PKGDOCDIR \"%s\"\n", PKGDOCDIR);
178 printf(" SYSCONFDIR \"%s\"\n", SYSCONFDIR);
181 puts(_("This is free software. You may redistribute copies of it under the terms of"));
182 puts(_("the GNU General Public License <http://www.gnu.org/licenses/gpl.html>."));
183 puts(_("There is NO WARRANTY, to the extent permitted by law."));
188 static void start_curses (void)
190 km_init (); /* must come before mutt_init */
191 mutt_signal_initialize ();
192 if (initscr () == NULL) {
193 puts _("Error initializing terminal.");
197 mutt_signal_initialize ();
201 #define M_IGNORE (1<<0) /* -z */
202 #define M_BUFFY (1<<1) /* -Z */
203 #define M_NOSYSRC (1<<2) /* -n */
204 #define M_RO (1<<3) /* -R */
205 #define M_SELECT (1<<4) /* -y */
207 #define M_NEWS (1<<5) /* -g and -G */
210 __attribute__((format(printf, 1, 0)))
211 static void mutt_nocurses_error (const char *fmt, ...)
216 vfprintf(stderr, fmt, ap);
221 int main (int argc, char **argv)
223 char folder[_POSIX_PATH_MAX] = "";
224 char *subject = NULL;
225 char *includeFile = NULL;
226 char *draftFile = NULL;
228 string_list_t *attach = NULL;
229 string_list_t *commands = NULL;
234 int explicit_folder = 0;
236 /* initialize random number for tmp file creation */
237 srand48((unsigned int) time (NULL));
239 /* sanity check against stupid administrators */
241 if (getegid () != getgid ()) {
242 fprintf (stderr, "%s: I don't want to run with privileges!\n", argv[0]);
246 setlocale (LC_ALL, "");
247 bindtextdomain (PACKAGE, MUTTLOCALEDIR);
248 textdomain (PACKAGE);
249 setlocale (LC_CTYPE, "");
251 mutt_error = mutt_message = mutt_nocurses_error;
252 srand48 (time (NULL));
255 p_clear(Options, countof(Options));
256 p_clear(QuadOptions, countof(QuadOptions));
258 while ((i = getopt(argc, argv, "a:b:F:f:c:e:H:s:i:hnpRTtvyzZ"
265 if (strlen(optarg)<=512)
266 attach = mutt_add_list (attach, optarg);
268 printf("too long arguments. exiting ...\n");
274 m_strreplace(&Muttrc, optarg);
278 m_strcpy(folder, sizeof(folder), optarg);
287 msg->env = envelope_new();
289 msg->env->bcc = rfc822_parse_adrlist (msg->env->bcc, optarg);
291 msg->env->cc = rfc822_parse_adrlist (msg->env->cc, optarg);
295 commands = mutt_add_list (commands, optarg);
303 includeFile = optarg;
311 sendflags |= SENDPOSTPONED;
315 flags |= M_RO; /* read-only mode */
326 case 'y': /* My special hack mode */
331 case 'g': /* Specify a newsserver */
333 char buf[LONG_STRING];
335 snprintf (buf, sizeof (buf), "set nntp_host=%s", optarg);
336 commands = mutt_add_list (commands, buf);
339 case 'G': /* List of newsgroups */
340 flags |= M_SELECT | M_NEWS;
349 flags |= M_BUFFY | M_IGNORE;
360 /* Check for a batch send. */
362 set_option(OPTNOCURSES);
363 sendflags = SENDBATCH;
366 /* This must come before mutt_init() because curses needs to be started
367 before calling the init_pair() function to set the color scheme. */
368 if (!option (OPTNOCURSES))
371 /* set defaults and read init files */
372 mutt_init (flags & M_NOSYSRC, commands);
373 string_list_wipe(&commands);
375 if (!option(OPTNOCURSES)) {
379 /* Create the Maildir directory if it doesn't exist. */
380 if (!option(OPTNOCURSES) && Maildir) {
382 char fpath[_POSIX_PATH_MAX];
385 m_strcpy(fpath, sizeof(fpath), Maildir);
386 mutt_expand_path (fpath, sizeof (fpath));
387 /* we're not connected yet - skip mail folder creation */
388 if (mx_get_magic (fpath) != M_IMAP)
389 if (stat (fpath, &sb) == -1 && errno == ENOENT) {
390 snprintf (mesg, sizeof (mesg), _("%s does not exist. Create it?"),
392 if (mutt_yesorno (mesg, M_YES) == M_YES) {
393 if (mkdir (fpath, 0700) == -1 && errno != EEXIST)
394 mutt_error (_("Can't create %s: %s."), Maildir, strerror (errno));
399 if (sendflags & SENDPOSTPONED) {
400 if (!option (OPTNOCURSES))
402 ci_send_message (SENDPOSTPONED, NULL, NULL, NULL, NULL);
405 else if (subject || msg || sendflags || draftFile || includeFile || attach
408 char buf[LONG_STRING];
409 char *tempfile = NULL, *infile = NULL;
410 char *bodytext = NULL;
413 if (!option (OPTNOCURSES))
423 msg->env = envelope_new();
425 for (i = optind; i < argc; i++) {
426 if (url_check_scheme (argv[i]) == U_MAILTO)
427 url_parse_mailto (msg->env, &bodytext, argv[i]);
429 msg->env->to = rfc822_parse_adrlist (msg->env->to, argv[i]);
432 if (option (OPTAUTOEDIT) && !msg->env->to && !msg->env->cc) {
433 if (!option (OPTNOCURSES))
435 fputs (_("No recipients specified.\n"), stderr);
440 msg->env->subject = m_strdup(subject);
443 infile = includeFile;
446 if (infile || bodytext) {
448 if (m_strcmp("-", infile) == 0)
451 char path[_POSIX_PATH_MAX];
453 m_strcpy(path, sizeof(path), infile);
454 mutt_expand_path (path, sizeof (path));
455 if ((fin = fopen (path, "r")) == NULL) {
456 if (!option (OPTNOCURSES))
467 msg->env = mutt_read_rfc822_header (fin, NULL, 1, 0);
469 /* is the following if still needed? */
471 fout = m_tempfile(buf, sizeof(buf), NONULL(mod_core.tmpdir), NULL);
472 tempfile = m_strdup(buf);
476 if (!option (OPTNOCURSES))
484 mutt_copy_stream (fin, fout);
486 fputs (bodytext, fout);
488 if (fin && fin != stdin)
496 string_list_t *t = attach;
501 a->next = mutt_make_file_attach (t->data);
505 msg->content = a = mutt_make_file_attach (t->data);
507 if (!option (OPTNOCURSES))
509 fprintf (stderr, _("%s: unable to attach file.\n"), t->data);
510 string_list_wipe(&attach);
515 string_list_wipe(&attach);
518 ci_send_message (sendflags, msg, tempfile, NULL, NULL);
520 if (!option (OPTNOCURSES))
524 if (flags & M_BUFFY) {
525 if (!buffy_check (0)) {
526 mutt_endwin _("No mailbox with new mail.");
531 buffy_next (folder, sizeof (folder));
533 else if (flags & M_SELECT) {
535 if (flags & M_NEWS) {
536 set_option (OPTNEWS);
537 if (!(CurrentNewsSrv = mutt_select_newsserver (NewsServer))) {
538 mutt_endwin (Errorbuf);
544 mutt_endwin _("No incoming mailboxes defined.");
549 mutt_select_file(folder, sizeof(folder), M_SEL_FOLDER | M_SEL_BUFFY,
558 m_strcpy(folder, sizeof(folder), NONULL(Spoolfile));
561 if (option (OPTNEWS)) {
562 unset_option (OPTNEWS);
563 nntp_expand_path (folder, sizeof (folder),
564 &CurrentNewsSrv->conn->account);
567 mutt_expand_path (folder, sizeof (folder));
569 m_strreplace(&CurrentFolder, folder);
570 m_strreplace(&LastFolder, folder);
572 if (flags & M_IGNORE) {
573 /* check to see if there are any messages in the folder */
574 switch (mx_check_empty (folder)) {
576 mutt_endwin (strerror (errno));
579 mutt_endwin _("Mailbox is empty.");
584 mutt_folder_hook (folder);
586 if ((Context = mx_open_mailbox(folder, (flags & M_RO) ? M_READONLY : 0,
587 NULL)) || !explicit_folder)
590 if (option (OPTXTERMSETTITLES))
591 printf("\033]2;%s\007", NONULL(XtermLeave));
595 mutt_endwin (Errorbuf);
599 mutt_sasl_shutdown();