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