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