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-sys/evtloop.h>
25 #include <lib-mime/mime.h>
26 #include <lib-ui/lib-ui.h>
27 #include <lib-mx/mx.h>
35 #include "mutt_idna.h"
36 #include "mutt_sasl.h"
46 #include <stringprep.h>
50 #if defined(HAVE_TOKYOCABINET)
52 #elif defined(HAVE_GDBM)
57 #include <gnutls/gnutls.h>
62 void mutt_exit(int code)
68 static void mutt_usage (void)
70 puts(madmutt_version);
73 usage: madmutt [ -nRyzZ ] [ -e <cmd> ] [ -F <file> ] [ -f <file> ]\n\
74 madmutt [ -n ] [ -e <cmd> ] [ -a <file> ] [ -F <file> ] [ -H <file> ] [ -i <file> ] [ -s <subj> ] [ -b <addr> ] [ -c <addr> ] <addr> [ ... ]\n\
75 madmutt [ -n ] [ -e <cmd> ] [ -F <file> ] -p\n\
79 puts(_(" -a <file> attach a file to the message"));
80 puts(_(" -b <address> specify a blind carbon-copy (BCC) address"));
81 puts(_(" -c <address> specify a carbon-copy (CC) address"));
82 puts(_(" -e <command> specify a command to be executed after initialization"));
83 puts(_(" -f <file> specify which mailbox to read"));
84 puts(_(" -F <file> specify an alternate Madmuttrc file"));
85 puts(_(" -H <file> specify a draft file to read header and body from"));
86 puts(_(" -i <file> specify a file which Madmutt should include in the body"));
87 puts(_(" -n causes Madmutt not to read the system Madmuttrc"));
88 puts(_(" -p recall a postponed message"));
89 puts(_(" -R open mailbox in read-only mode"));
90 puts(_(" -s <subj> specify a subject (must be in quotes if it has spaces)"));
91 puts(_(" -v show version and compile-time definitions"));
92 puts(_(" -z exit immediately if there are no messages in the mailbox"));
93 puts(_(" -Z open the first folder with new message, exit immediately if none"));
94 puts(_(" -h this help message"));
99 static void show_version (void)
104 puts(madmutt_version);
105 puts(_(" Copyright (C) 1996-2002 Michael R. Elkins and others."));
106 puts(_(" Copyright (C) 2005 The Mutt-ng Team"));
107 puts(_(" Copyright (C) 2006-2007 Pierre Habouzit"));
108 puts(_(" MadMutt is based on Mutt-ng wich was based on Mutt before"));
111 printf("System:\n %s %s (%s)\n", uts.sysname, uts.release, uts.machine);
112 puts("External Libraries:");
113 #ifdef NCURSES_VERSION
114 printf(" ncurses %s\n", NCURSES_VERSION);
116 #ifdef _LIBICONV_VERSION
117 printf(" libiconv %d.%d\n", _LIBICONV_VERSION >> 8,
118 _LIBICONV_VERSION & 0xff);
120 #ifdef STRINGPREP_VERSION
121 printf(" libidn %s\n", STRINGPREP_VERSION);
124 #if defined(HAVE_TOKYOCABINET)
125 printf(" tokyocabinet %s\n", tcversion);
126 #elif defined(HAVE_GDBM)
127 printf(" gdbm %s\n", gdbm_version);
130 printf(" gnutls %s\n", LIBGNUTLS_VERSION);
131 printf(" gpgme %s\n", GPGME_VERSION);
132 puts (_("Compile Options:"));
147 puts(_("Built-In Defaults:"));
148 printf(" SENDMAIL \"%s\"\n", SENDMAIL);
149 printf(" MAILPATH \"%s\"\n", MAILPATH);
150 printf(" PKGDATADIR \"%s\"\n", PKGDATADIR);
151 printf(" PKGDOCDIR \"%s\"\n", PKGDOCDIR);
152 printf(" SYSCONFDIR \"%s\"\n", SYSCONFDIR);
155 puts(_("This is free software. You may redistribute copies of it under the terms of"));
156 puts(_("the GNU General Public License <http://www.gnu.org/licenses/gpl.html>."));
157 puts(_("There is NO WARRANTY, to the extent permitted by law."));
162 #define M_IGNORE (1<<0) /* -z */
163 #define M_BUFFY (1<<1) /* -Z */
164 #define M_NOSYSRC (1<<2) /* -n */
165 #define M_RO (1<<3) /* -R */
167 __attribute__((format(printf, 1, 0)))
168 static void mutt_nocurses_error (const char *fmt, ...)
173 vfprintf(stderr, fmt, ap);
178 int main (int argc, char **argv)
180 char folder[_POSIX_PATH_MAX] = "";
181 char *subject = NULL;
182 char *includeFile = NULL;
183 char *draftFile = NULL;
185 string_list_t *attach = NULL;
186 string_list_t *commands = NULL;
191 int explicit_folder = 0;
193 /* initialize random number for tmp file creation */
194 srand48((unsigned int) time (NULL));
196 /* sanity check against stupid administrators */
198 if (getegid () != getgid ()) {
199 fprintf (stderr, "%s: I don't want to run with privileges!\n", argv[0]);
203 setlocale (LC_ALL, "");
204 bindtextdomain (PACKAGE, MUTTLOCALEDIR);
205 textdomain (PACKAGE);
206 setlocale (LC_CTYPE, "");
208 mutt_error = mutt_message = mutt_nocurses_error;
209 srand48 (time (NULL));
212 while ((i = getopt(argc, argv, "a:b:F:f:c:e:H:s:i:hnpRTtvzZ")) >= 0)
215 if (strlen(optarg)<=512)
216 attach = mutt_add_list (attach, optarg);
218 printf("too long arguments. exiting ...\n");
224 m_strreplace(&Muttrc, optarg);
228 m_strcpy(folder, sizeof(folder), optarg);
237 msg->env = envelope_new();
239 msg->env->bcc = rfc822_parse_adrlist (msg->env->bcc, optarg);
241 msg->env->cc = rfc822_parse_adrlist (msg->env->cc, optarg);
245 commands = mutt_add_list (commands, optarg);
253 includeFile = optarg;
261 sendflags |= SENDPOSTPONED;
265 flags |= M_RO; /* read-only mode */
281 flags |= M_BUFFY | M_IGNORE;
292 /* Check for a batch send. */
294 set_option(OPTNOCURSES);
295 sendflags = SENDBATCH;
298 /* This must come before mutt_init() because curses needs to be started
299 before calling the init_pair() function to set the color scheme. */
300 if (!option (OPTNOCURSES)) {
303 mutt_signal_initialize();
306 /* set defaults and read init files */
308 mutt_init (flags & M_NOSYSRC, commands);
309 string_list_wipe(&commands);
311 if (!option(OPTNOCURSES)) {
315 /* Create the Maildir directory if it doesn't exist. */
316 if (!option(OPTNOCURSES) && Maildir) {
318 char fpath[_POSIX_PATH_MAX];
321 m_strcpy(fpath, sizeof(fpath), Maildir);
322 mutt_expand_path (fpath, sizeof (fpath));
323 /* we're not connected yet - skip mail folder creation */
324 if (mx_get_magic (fpath) != M_IMAP)
325 if (stat (fpath, &sb) == -1 && errno == ENOENT) {
326 snprintf (mesg, sizeof (mesg), _("%s does not exist. Create it?"),
328 if (mutt_yesorno (mesg, M_YES) == M_YES) {
329 if (mkdir (fpath, 0700) == -1 && errno != EEXIST)
330 mutt_error (_("Can't create %s: %s."), Maildir, strerror (errno));
335 if (sendflags & SENDPOSTPONED) {
336 if (!option (OPTNOCURSES))
338 ci_send_message (SENDPOSTPONED, NULL, NULL, NULL, NULL);
341 else if (subject || msg || sendflags || draftFile || includeFile || attach
344 char buf[LONG_STRING];
345 char *tempfile = NULL, *infile = NULL;
346 char *bodytext = NULL;
349 if (!option (OPTNOCURSES))
359 msg->env = envelope_new();
361 for (i = optind; i < argc; i++) {
362 if (url_check_scheme (argv[i]) == U_MAILTO)
363 url_parse_mailto (msg->env, &bodytext, argv[i]);
365 msg->env->to = rfc822_parse_adrlist (msg->env->to, argv[i]);
368 if (option (OPTAUTOEDIT) && !msg->env->to && !msg->env->cc) {
369 if (!option (OPTNOCURSES))
371 fputs (_("No recipients specified.\n"), stderr);
376 msg->env->subject = m_strdup(subject);
379 infile = includeFile;
382 if (infile || bodytext) {
384 if (m_strcmp("-", infile) == 0)
387 char path[_POSIX_PATH_MAX];
389 m_strcpy(path, sizeof(path), infile);
390 mutt_expand_path (path, sizeof (path));
391 if ((fin = fopen (path, "r")) == NULL) {
392 if (!option (OPTNOCURSES))
403 msg->env = mutt_read_rfc822_header (fin, NULL, 1, 0);
405 /* is the following if still needed? */
407 fout = m_tempfile(buf, sizeof(buf), NONULL(mod_core.tmpdir), NULL);
408 tempfile = m_strdup(buf);
412 if (!option (OPTNOCURSES))
420 mutt_copy_stream (fin, fout);
422 fputs (bodytext, fout);
424 if (fin && fin != stdin)
432 string_list_t *t = attach;
437 a->next = mutt_make_file_attach (t->data);
441 msg->content = a = mutt_make_file_attach (t->data);
443 if (!option (OPTNOCURSES))
445 fprintf (stderr, _("%s: unable to attach file.\n"), t->data);
446 string_list_wipe(&attach);
451 string_list_wipe(&attach);
454 ci_send_message (sendflags, msg, tempfile, NULL, NULL);
456 if (!option (OPTNOCURSES))
460 if (flags & M_BUFFY) {
461 if (!buffy_check (0)) {
462 mutt_endwin _("No mailbox with new mail.");
467 buffy_next (folder, sizeof (folder));
471 m_strcpy(folder, sizeof(folder), NONULL(Spoolfile));
473 mutt_expand_path (folder, sizeof (folder));
474 m_strreplace(&CurrentFolder, folder);
475 m_strreplace(&LastFolder, folder);
477 if (flags & M_IGNORE) {
478 /* check to see if there are any messages in the folder */
479 switch (mx_check_empty (folder)) {
481 mutt_endwin (strerror (errno));
484 mutt_endwin _("Mailbox is empty.");
489 mutt_folder_hook (folder);
491 if ((Context = mx_open_mailbox(folder, (flags & M_RO) ? M_READONLY : 0,
492 NULL)) || !explicit_folder)
496 if (option (OPTXTERMSETTITLES))
497 printf("\033]2;%s\007", NONULL(XtermLeave));
501 mutt_endwin (Errorbuf);
505 mutt_sasl_shutdown();