WHAT_KEY has better equivalents in other tools.
[apps/madmutt.git] / main.c
1 /*
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
6  *
7  * Parts written/modified by:
8  * Nico Golde <nion@muttng.org>
9  * Andreas Krennmair <ak@synflood.at>
10  *
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.
14  */
15
16 #define MAIN_C 1
17
18 #include <lib-lib/lib-lib.h>
19
20 #include <sys/utsname.h>
21
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>
27
28 #include "mutt.h"
29 #include "crypt.h"
30 #include "alias.h"
31 #include "buffy.h"
32 #include "sort.h"
33 #include "keymap.h"
34 #include "mutt_idna.h"
35 #include "mutt_sasl.h"
36
37 #ifdef HAVE_GETOPT_H
38 #include <getopt.h>
39 #else
40 extern char *optarg;
41 extern int optind;
42 #endif
43
44 #ifdef HAVE_LIBIDN
45 #include <stringprep.h>
46 #endif
47
48 #ifdef USE_HCACHE
49 #if defined(HAVE_QDBM)
50 #include <depot.h>
51 #elif defined(HAVE_GDBM)
52 #include <gdbm.h>
53 #endif
54 #endif
55
56 #include <gnutls/gnutls.h>
57 #include <gpgme.h>
58
59 void mutt_exit(int code)
60 {
61     mutt_endwin(NULL);
62     exit(code);
63 }
64
65 static void mutt_usage (void)
66 {
67     puts(mutt_make_version());
68
69     puts(_("\
70 usage: madmutt [ -nRyzZ ] [ -e <cmd> ] [ -F <file> ] [ -f <file> ]\n\
71        madmutt [ -n ] [ -e <cmd> ] [ -a <file> ] [ -F <file> ] [ -H <file> ] [ -i <file> ] [ -s <subj> ] [ -b <addr> ] [ -c <addr> ] <addr> [ ... ]\n\
72        madmutt [ -n ] [ -e <cmd> ] [ -F <file> ] -p\n\
73        madmutt -v\n"));
74
75     puts(_("Options:"));
76     puts(_("  -a <file>     attach a file to the message"));
77     puts(_("  -b <address>  specify a blind carbon-copy (BCC) address"));
78     puts(_("  -c <address>  specify a carbon-copy (CC) address"));
79     puts(_("  -e <command>  specify a command to be executed after initialization"));
80     puts(_("  -f <file>     specify which mailbox to read"));
81     puts(_("  -F <file>     specify an alternate Madmuttrc file"));
82     puts(_("  -H <file>     specify a draft file to read header and body from"));
83     puts(_("  -i <file>     specify a file which Madmutt should include in the body"));
84     puts(_("  -n            causes Madmutt not to read the system Madmuttrc"));
85     puts(_("  -p            recall a postponed message"));
86     puts(_("  -R            open mailbox in read-only mode"));
87     puts(_("  -s <subj>     specify a subject (must be in quotes if it has spaces)"));
88     puts(_("  -v            show version and compile-time definitions"));
89     puts(_("  -y            select a mailbox specified in your `mailboxes' list"));
90     puts(_("  -z            exit immediately if there are no messages in the mailbox"));
91     puts(_("  -Z            open the first folder with new message, exit immediately if none"));
92     puts(_("  -h            this help message"));
93
94     exit(0);
95 }
96
97 static void show_version (void)
98 {
99     struct utsname uts;
100     uname(&uts);
101
102     puts(mutt_make_version());
103     puts(_("  Copyright (C) 1996-2002 Michael R. Elkins and others."));
104     puts(_("  Copyright (C) 2005      The Mutt-ng Team"));
105     puts(_("  Copyright (C) 2006-2007 Pierre Habouzit"));
106     puts(_("  MadMutt is based on Mutt-ng wich was based on Mutt before"));
107     puts("");
108
109     printf("System:\n  %s %s (%s)\n", uts.sysname, uts.release, uts.machine);
110     puts("External Libraries:");
111 #ifdef NCURSES_VERSION
112     printf("  ncurses %s\n", NCURSES_VERSION);
113 #endif
114 #ifdef _LIBICONV_VERSION
115     printf("  libiconv %d.%d\n", _LIBICONV_VERSION >> 8,
116            _LIBICONV_VERSION & 0xff);
117 #endif
118 #ifdef STRINGPREP_VERSION
119     printf("  libidn %s\n", STRINGPREP_VERSION);
120 #endif
121 #ifdef USE_HCACHE
122 #if defined(HAVE_QDBM)
123     printf("  qdbm %s\n", dpversion);
124 #elif defined(HAVE_GDBM)
125     printf("  gdbm %s\n", gdbm_version);
126 #endif
127 #endif
128     printf("  gnutls %s\n", LIBGNUTLS_VERSION);
129     printf("  gpgme %s\n",  GPGME_VERSION);
130     puts (_("Compile Options:"));
131
132     puts (
133 #ifdef USE_FCNTL
134         "  +USE_FCNTL"
135 #else
136         "  -USE_FCNTL"
137 #endif
138 #ifdef USE_FLOCK
139         "  +USE_FLOCK"
140 #else
141         "  -USE_FLOCK"
142 #endif
143 #ifdef USE_HCACHE
144         "  +USE_HCACHE"
145 #else
146         "  -USE_HCACHE"
147 #endif
148 #ifdef HAVE_LIBIDN
149         "  +HAVE_LIBIDN"
150 #else
151         "  -HAVE_LIBIDN"
152 #endif
153         );
154
155     puts(_("Built-In Defaults:"));
156     printf("  SENDMAIL   \"%s\"\n", SENDMAIL);
157     printf("  MAILPATH   \"%s\"\n", MAILPATH);
158     printf("  PKGDATADIR \"%s\"\n", PKGDATADIR);
159     printf("  PKGDOCDIR  \"%s\"\n", PKGDOCDIR);
160     printf("  SYSCONFDIR \"%s\"\n", SYSCONFDIR);
161
162     puts("");
163     puts(_("This is free software.  You may redistribute copies of it under the terms of"));
164     puts(_("the GNU General Public License <http://www.gnu.org/licenses/gpl.html>."));
165     puts(_("There is NO WARRANTY, to the extent permitted by law."));
166
167     exit(0);
168 }
169
170 #define M_IGNORE  (1<<0)        /* -z */
171 #define M_BUFFY   (1<<1)        /* -Z */
172 #define M_NOSYSRC (1<<2)        /* -n */
173 #define M_RO      (1<<3)        /* -R */
174 #define M_SELECT  (1<<4)        /* -y */
175 #define M_NEWS    (1<<5)        /* -g and -G */
176
177 __attribute__((format(printf, 1, 0)))
178 static void mutt_nocurses_error (const char *fmt, ...)
179 {
180     va_list ap;
181
182     va_start(ap, fmt);
183     vfprintf(stderr, fmt, ap);
184     va_end(ap);
185     fputc('\n', stderr);
186 }
187
188 int main (int argc, char **argv)
189 {
190   char folder[_POSIX_PATH_MAX] = "";
191   char *subject = NULL;
192   char *includeFile = NULL;
193   char *draftFile = NULL;
194   HEADER *msg = NULL;
195   string_list_t *attach = NULL;
196   string_list_t *commands = NULL;
197   int sendflags = 0;
198   int flags = 0;
199   int version = 0;
200   int i;
201   int explicit_folder = 0;
202
203   /* initialize random number for tmp file creation */ 
204   srand48((unsigned int) time (NULL));
205   
206   /* sanity check against stupid administrators */
207   
208   if (getegid () != getgid ()) {
209     fprintf (stderr, "%s: I don't want to run with privileges!\n", argv[0]);
210     exit (1);
211   }
212
213   setlocale (LC_ALL, "");
214   bindtextdomain (PACKAGE, MUTTLOCALEDIR);
215   textdomain (PACKAGE);
216   setlocale (LC_CTYPE, "");
217
218   mutt_error = mutt_message = mutt_nocurses_error;
219   srand48 (time (NULL));
220   umask (077);
221
222   while ((i = getopt(argc, argv, "a:b:F:f:c:e:H:s:i:hnpRTtvyzZ")) >= 0)
223     switch (i) {
224     case 'a':
225       if (strlen(optarg)<=512)
226         attach = mutt_add_list (attach, optarg);
227       else{
228         printf("too long arguments. exiting ...\n");
229         exit(1);
230       }
231       break;
232
233     case 'F':
234       m_strreplace(&Muttrc, optarg);
235       break;
236
237     case 'f':
238       m_strcpy(folder, sizeof(folder), optarg);
239       explicit_folder = 1;
240       break;
241
242     case 'b':
243     case 'c':
244       if (!msg)
245         msg = header_new();
246       if (!msg->env)
247         msg->env = envelope_new();
248       if (i == 'b')
249         msg->env->bcc = rfc822_parse_adrlist (msg->env->bcc, optarg);
250       else
251         msg->env->cc = rfc822_parse_adrlist (msg->env->cc, optarg);
252       break;
253
254     case 'e':
255       commands = mutt_add_list (commands, optarg);
256       break;
257
258     case 'H':
259       draftFile = optarg;
260       break;
261
262     case 'i':
263       includeFile = optarg;
264       break;
265
266     case 'n':
267       flags |= M_NOSYSRC;
268       break;
269
270     case 'p':
271       sendflags |= SENDPOSTPONED;
272       break;
273
274     case 'R':
275       flags |= M_RO;            /* read-only mode */
276       break;
277
278     case 's':
279       subject = optarg;
280       break;
281
282     case 'v':
283       version++;
284       break;
285
286     case 'y':                  /* My special hack mode */
287       flags |= M_SELECT;
288       break;
289
290     case 'z':
291       flags |= M_IGNORE;
292       break;
293
294     case 'Z':
295       flags |= M_BUFFY | M_IGNORE;
296       break;
297
298     default:
299       mutt_usage ();
300     }
301
302   if (version) {
303     show_version ();
304   }
305
306   /* Check for a batch send. */
307   if (!isatty (0)) {
308     set_option(OPTNOCURSES);
309     sendflags = SENDBATCH;
310   }
311
312   /* This must come before mutt_init() because curses needs to be started
313      before calling the init_pair() function to set the color scheme.  */
314   if (!option (OPTNOCURSES)) {
315     km_init();
316     curses_initialize();
317     mutt_signal_initialize();
318   }
319
320   /* set defaults and read init files */
321   mutt_init (flags & M_NOSYSRC, commands);
322   string_list_wipe(&commands);
323
324   if (!option(OPTNOCURSES)) {
325       ui_layout_init();
326   }
327
328   /* Create the Maildir directory if it doesn't exist. */
329   if (!option(OPTNOCURSES) && Maildir) {
330     struct stat sb;
331     char fpath[_POSIX_PATH_MAX];
332     char mesg[STRING];
333
334     m_strcpy(fpath, sizeof(fpath), Maildir);
335     mutt_expand_path (fpath, sizeof (fpath));
336     /* we're not connected yet - skip mail folder creation */
337     if (mx_get_magic (fpath) != M_IMAP)
338       if (stat (fpath, &sb) == -1 && errno == ENOENT) {
339         snprintf (mesg, sizeof (mesg), _("%s does not exist. Create it?"),
340                   Maildir);
341         if (mutt_yesorno (mesg, M_YES) == M_YES) {
342           if (mkdir (fpath, 0700) == -1 && errno != EEXIST)
343             mutt_error (_("Can't create %s: %s."), Maildir, strerror (errno));
344         }
345       }
346   }
347
348   if (sendflags & SENDPOSTPONED) {
349     if (!option (OPTNOCURSES))
350       mutt_flushinp ();
351     ci_send_message (SENDPOSTPONED, NULL, NULL, NULL, NULL);
352     mutt_endwin (NULL);
353   }
354   else if (subject || msg || sendflags || draftFile || includeFile || attach
355            || optind < argc) {
356     FILE *fin = NULL;
357     char buf[LONG_STRING];
358     char *tempfile = NULL, *infile = NULL;
359     char *bodytext = NULL;
360     FILE *fout;
361
362     if (!option (OPTNOCURSES))
363       mutt_flushinp ();
364
365     if (!msg)
366       msg = header_new();
367
368     if (draftFile)
369       infile = draftFile;
370     else {
371       if (!msg->env)
372         msg->env = envelope_new();
373
374       for (i = optind; i < argc; i++) {
375         if (url_check_scheme (argv[i]) == U_MAILTO)
376           url_parse_mailto (msg->env, &bodytext, argv[i]);
377         else
378           msg->env->to = rfc822_parse_adrlist (msg->env->to, argv[i]);
379       }
380
381       if (option (OPTAUTOEDIT) && !msg->env->to && !msg->env->cc) {
382         if (!option (OPTNOCURSES))
383           mutt_endwin (NULL);
384         fputs (_("No recipients specified.\n"), stderr);
385         exit (1);
386       }
387
388       if (subject)
389         msg->env->subject = m_strdup(subject);
390
391       if (includeFile)
392         infile = includeFile;
393     }
394
395     if (infile || bodytext) {
396       if (infile) {
397         if (m_strcmp("-", infile) == 0)
398           fin = stdin;
399         else {
400           char path[_POSIX_PATH_MAX];
401
402           m_strcpy(path, sizeof(path), infile);
403           mutt_expand_path (path, sizeof (path));
404           if ((fin = fopen (path, "r")) == NULL) {
405             if (!option (OPTNOCURSES))
406               mutt_endwin (NULL);
407             perror (path);
408             exit (1);
409           }
410         }
411       }
412       else
413         fin = NULL;
414
415       if (draftFile)
416         msg->env = mutt_read_rfc822_header (fin, NULL, 1, 0);
417
418       /* is the following if still needed? */
419
420       fout = m_tempfile(buf, sizeof(buf), NONULL(mod_core.tmpdir), NULL);
421       tempfile = m_strdup(buf);
422
423       if (tempfile) {
424         if (!fout) {
425           if (!option (OPTNOCURSES))
426             mutt_endwin (NULL);
427           perror (tempfile);
428           m_fclose(&fin);
429           p_delete(&tempfile);
430           exit (1);
431         }
432         if (fin)
433           mutt_copy_stream (fin, fout);
434         else if (bodytext)
435           fputs (bodytext, fout);
436         m_fclose(&fout);
437         if (fin && fin != stdin)
438           m_fclose(&fin);
439       }
440     }
441
442     p_delete(&bodytext);
443
444     if (attach) {
445       string_list_t *t = attach;
446       BODY *a = NULL;
447
448       while (t) {
449         if (a) {
450           a->next = mutt_make_file_attach (t->data);
451           a = a->next;
452         }
453         else
454           msg->content = a = mutt_make_file_attach (t->data);
455         if (!a) {
456           if (!option (OPTNOCURSES))
457             mutt_endwin (NULL);
458           fprintf (stderr, _("%s: unable to attach file.\n"), t->data);
459           string_list_wipe(&attach);
460           exit (1);
461         }
462         t = t->next;
463       }
464       string_list_wipe(&attach);
465     }
466
467     ci_send_message (sendflags, msg, tempfile, NULL, NULL);
468
469     if (!option (OPTNOCURSES))
470       mutt_endwin (NULL);
471   }
472   else {
473     if (flags & M_BUFFY) {
474       if (!buffy_check (0)) {
475         mutt_endwin _("No mailbox with new mail.");
476
477         exit (1);
478       }
479       folder[0] = 0;
480       buffy_next (folder, sizeof (folder));
481     }
482     else if (flags & M_SELECT) {
483       if (!Incoming.len) {
484         mutt_endwin _("No incoming mailboxes defined.");
485
486         exit (1);
487       }
488       folder[0] = 0;
489       mutt_select_file(folder, sizeof(folder), M_SEL_FOLDER | M_SEL_BUFFY,
490                        NULL, NULL);
491       if (!folder[0]) {
492         mutt_endwin (NULL);
493         exit (0);
494       }
495     }
496
497     if (!folder[0])
498       m_strcpy(folder, sizeof(folder), NONULL(Spoolfile));
499
500     mutt_expand_path (folder, sizeof (folder));
501     m_strreplace(&CurrentFolder, folder);
502     m_strreplace(&LastFolder, folder);
503
504     if (flags & M_IGNORE) {
505       /* check to see if there are any messages in the folder */
506       switch (mx_check_empty (folder)) {
507       case -1:
508         mutt_endwin (strerror (errno));
509         exit (1);
510       case 1:
511         mutt_endwin _("Mailbox is empty.");
512         exit (1);
513       }
514     }
515
516     mutt_folder_hook (folder);
517
518     if ((Context = mx_open_mailbox(folder, (flags & M_RO) ?  M_READONLY : 0,
519                                    NULL)) || !explicit_folder)
520     {
521       mutt_index_menu ();
522       if (option (OPTXTERMSETTITLES))
523         printf("\033]2;%s\007", NONULL(XtermLeave));
524       if (Context)
525         p_delete(&Context);
526     }
527     mutt_endwin (Errorbuf);
528   }
529
530   luaM_shutdown();
531   mutt_sasl_shutdown();
532   exit (0);
533 }