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>
23 #include <lib-lua/lib-lua.h>
24 #include <lib-sys/mutt_signal.h>
25 #include <lib-sys/evtloop.h>
26 #include <lib-mime/mime.h>
27 #include <lib-ui/lib-ui.h>
28 #include <lib-mx/mx.h>
36 #include "mutt_idna.h"
37 #include "mutt_sasl.h"
47 #include <stringprep.h>
51 #if defined(HAVE_TOKYOCABINET)
53 #elif defined(HAVE_GDBM)
58 #include <gnutls/gnutls.h>
63 void mutt_exit(int code)
69 static void mutt_usage (void)
71 puts(madmutt_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"));
86 puts(_(" -H <file> specify a draft file to read header and body from"));
87 puts(_(" -i <file> specify a file which Madmutt should include in the body"));
88 puts(_(" -n causes Madmutt not to read the system Madmuttrc"));
89 puts(_(" -p recall a postponed message"));
90 puts(_(" -R open mailbox in read-only mode"));
91 puts(_(" -s <subj> specify a subject (must be in quotes if it has spaces)"));
92 puts(_(" -v show version and compile-time definitions"));
93 puts(_(" -z exit immediately if there are no messages in the mailbox"));
94 puts(_(" -Z open the first folder with new message, exit immediately if none"));
95 puts(_(" -h this help message"));
100 static void show_version (void)
105 puts(madmutt_version);
106 puts(_(" Copyright (C) 1996-2002 Michael R. Elkins and others."));
107 puts(_(" Copyright (C) 2005 The Mutt-ng Team"));
108 puts(_(" Copyright (C) 2006-2007 Pierre Habouzit"));
109 puts(_(" MadMutt is based on Mutt-ng wich was based on Mutt before"));
112 printf("System:\n %s %s (%s)\n", uts.sysname, uts.release, uts.machine);
113 puts("External Libraries:");
114 #ifdef NCURSES_VERSION
115 printf(" ncurses %s\n", NCURSES_VERSION);
117 #ifdef _LIBICONV_VERSION
118 printf(" libiconv %d.%d\n", _LIBICONV_VERSION >> 8,
119 _LIBICONV_VERSION & 0xff);
121 #ifdef STRINGPREP_VERSION
122 printf(" libidn %s\n", STRINGPREP_VERSION);
125 #if defined(HAVE_TOKYOCABINET)
126 printf(" tokyocabinet %s\n", tcversion);
127 #elif defined(HAVE_GDBM)
128 printf(" gdbm %s\n", gdbm_version);
131 printf(" gnutls %s\n", LIBGNUTLS_VERSION);
132 printf(" gpgme %s\n", GPGME_VERSION);
133 puts (_("Compile Options:"));
148 puts(_("Built-In Defaults:"));
149 printf(" SENDMAIL \"%s\"\n", SENDMAIL);
150 printf(" MAILPATH \"%s\"\n", MAILPATH);
151 printf(" PKGDATADIR \"%s\"\n", PKGDATADIR);
152 printf(" PKGDOCDIR \"%s\"\n", PKGDOCDIR);
153 printf(" SYSCONFDIR \"%s\"\n", SYSCONFDIR);
156 puts(_("This is free software. You may redistribute copies of it under the terms of"));
157 puts(_("the GNU General Public License <http://www.gnu.org/licenses/gpl.html>."));
158 puts(_("There is NO WARRANTY, to the extent permitted by law."));
163 #define M_IGNORE (1<<0) /* -z */
164 #define M_BUFFY (1<<1) /* -Z */
165 #define M_NOSYSRC (1<<2) /* -n */
166 #define M_RO (1<<3) /* -R */
168 __attribute__((format(printf, 1, 0)))
169 static void mutt_nocurses_error (const char *fmt, ...)
174 vfprintf(stderr, fmt, ap);
179 int main (int argc, char **argv)
181 char folder[_POSIX_PATH_MAX] = "";
182 char *subject = NULL;
183 char *includeFile = NULL;
184 char *draftFile = NULL;
186 string_list_t *attach = NULL;
187 string_list_t *commands = NULL;
192 int explicit_folder = 0;
195 /* initialize random number for tmp file creation */
196 srand48((unsigned int) time (NULL));
198 /* sanity check against stupid administrators */
200 if (getegid () != getgid ()) {
201 fprintf (stderr, "%s: I don't want to run with privileges!\n", argv[0]);
205 setlocale (LC_ALL, "");
206 bindtextdomain (PACKAGE, MUTTLOCALEDIR);
207 textdomain (PACKAGE);
208 setlocale (LC_CTYPE, "");
210 mutt_error = mutt_message = mutt_nocurses_error;
211 srand48 (time (NULL));
214 while ((i = getopt(argc, argv, "a:b:F:f:c:e:H:s:i:hnpRTtvzZ")) >= 0)
217 if (strlen(optarg)<=512)
218 attach = mutt_add_list (attach, optarg);
220 printf("too long arguments. exiting ...\n");
226 m_strreplace(&Muttrc, optarg);
230 m_strcpy(folder, sizeof(folder), optarg);
239 msg->env = envelope_new();
241 msg->env->bcc = rfc822_parse_adrlist (msg->env->bcc, optarg);
243 msg->env->cc = rfc822_parse_adrlist (msg->env->cc, optarg);
247 commands = mutt_add_list (commands, optarg);
255 includeFile = optarg;
263 sendflags |= SENDPOSTPONED;
267 flags |= M_RO; /* read-only mode */
283 flags |= M_BUFFY | M_IGNORE;
294 /* Check for a batch send. */
296 set_option(OPTNOCURSES);
297 sendflags = SENDBATCH;
300 /* This must come before mutt_init() because curses needs to be started
301 before calling the init_pair() function to set the color scheme. */
302 if (!option (OPTNOCURSES)) {
305 mutt_signal_initialize();
308 /* set defaults and read init files */
310 mutt_init (flags & M_NOSYSRC, commands);
311 string_list_wipe(&commands);
312 pthread_create(&pt, NULL, &el_loop, NULL);
314 if (!option(OPTNOCURSES)) {
318 /* Create the Maildir directory if it doesn't exist. */
319 if (!option(OPTNOCURSES) && Maildir) {
321 char fpath[_POSIX_PATH_MAX];
324 m_strcpy(fpath, sizeof(fpath), Maildir);
325 mutt_expand_path (fpath, sizeof (fpath));
326 /* we're not connected yet - skip mail folder creation */
327 if (mx_get_magic (fpath) != M_IMAP)
328 if (stat (fpath, &sb) == -1 && errno == ENOENT) {
329 snprintf (mesg, sizeof (mesg), _("%s does not exist. Create it?"),
331 if (mutt_yesorno (mesg, M_YES) == M_YES) {
332 if (mkdir (fpath, 0700) == -1 && errno != EEXIST)
333 mutt_error (_("Can't create %s: %s."), Maildir, strerror (errno));
338 if (sendflags & SENDPOSTPONED) {
339 if (!option (OPTNOCURSES))
341 ci_send_message (SENDPOSTPONED, NULL, NULL, NULL, NULL);
344 else if (subject || msg || sendflags || draftFile || includeFile || attach
347 char buf[LONG_STRING];
348 char *tempfile = NULL, *infile = NULL;
349 char *bodytext = NULL;
352 if (!option (OPTNOCURSES))
362 msg->env = envelope_new();
364 for (i = optind; i < argc; i++) {
365 if (url_check_scheme (argv[i]) == U_MAILTO)
366 url_parse_mailto (msg->env, &bodytext, argv[i]);
368 msg->env->to = rfc822_parse_adrlist (msg->env->to, argv[i]);
371 if (option (OPTAUTOEDIT) && !msg->env->to && !msg->env->cc) {
372 if (!option (OPTNOCURSES))
374 fputs (_("No recipients specified.\n"), stderr);
379 msg->env->subject = m_strdup(subject);
382 infile = includeFile;
385 if (infile || bodytext) {
387 if (m_strcmp("-", infile) == 0)
390 char path[_POSIX_PATH_MAX];
392 m_strcpy(path, sizeof(path), infile);
393 mutt_expand_path (path, sizeof (path));
394 if ((fin = fopen (path, "r")) == NULL) {
395 if (!option (OPTNOCURSES))
406 msg->env = mutt_read_rfc822_header (fin, NULL, 1, 0);
408 /* is the following if still needed? */
410 fout = m_tempfile(buf, sizeof(buf), NONULL(mod_core.tmpdir), NULL);
411 tempfile = m_strdup(buf);
415 if (!option (OPTNOCURSES))
423 mutt_copy_stream (fin, fout);
425 fputs (bodytext, fout);
427 if (fin && fin != stdin)
435 string_list_t *t = attach;
440 a->next = mutt_make_file_attach (t->data);
444 msg->content = a = mutt_make_file_attach (t->data);
446 if (!option (OPTNOCURSES))
448 fprintf (stderr, _("%s: unable to attach file.\n"), t->data);
449 string_list_wipe(&attach);
454 string_list_wipe(&attach);
457 ci_send_message (sendflags, msg, tempfile, NULL, NULL);
459 if (!option (OPTNOCURSES))
463 if (flags & M_BUFFY) {
464 if (!buffy_check (0)) {
465 mutt_endwin _("No mailbox with new mail.");
470 buffy_next (folder, sizeof (folder));
474 m_strcpy(folder, sizeof(folder), NONULL(Spoolfile));
476 mutt_expand_path (folder, sizeof (folder));
477 m_strreplace(&CurrentFolder, folder);
478 m_strreplace(&LastFolder, folder);
480 if (flags & M_IGNORE) {
481 /* check to see if there are any messages in the folder */
482 switch (mx_check_empty (folder)) {
484 mutt_endwin (strerror (errno));
487 mutt_endwin _("Mailbox is empty.");
492 mutt_folder_hook (folder);
494 if ((Context = mx_open_mailbox(folder, (flags & M_RO) ? M_READONLY : 0,
495 NULL)) || !explicit_folder)
499 if (option (OPTXTERMSETTITLES))
500 printf("\033]2;%s\007", NONULL(XtermLeave));
504 mutt_endwin (Errorbuf);
508 pthread_join(pt, NULL);
510 mutt_sasl_shutdown();