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