drop mod_ssl.starttls setting.
[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-sys/evtloop.h>
25 #include <lib-mime/mime.h>
26 #include <lib-ui/lib-ui.h>
27 #include <lib-mx/mx.h>
28
29 #include "mutt.h"
30 #include "crypt.h"
31 #include "alias.h"
32 #include "buffy.h"
33 #include "sort.h"
34 #include "keymap.h"
35 #include "mutt_idna.h"
36 #include "mutt_sasl.h"
37
38 #ifdef HAVE_GETOPT_H
39 #include <getopt.h>
40 #else
41 extern char *optarg;
42 extern int optind;
43 #endif
44
45 #ifdef HAVE_LIBIDN
46 #include <stringprep.h>
47 #endif
48
49 #ifdef USE_HCACHE
50 #if defined(HAVE_TOKYOCABINET)
51 #include <tcutil.h>
52 #elif defined(HAVE_GDBM)
53 #include <gdbm.h>
54 #endif
55 #endif
56
57 #include <gnutls/gnutls.h>
58 #include <gpgme.h>
59
60 #include <libintl.h>
61
62 void mutt_exit(int code)
63 {
64     mutt_endwin(NULL);
65     exit(code);
66 }
67
68 static void mutt_usage (void)
69 {
70     puts(madmutt_version);
71
72     puts(_("\
73 usage: madmutt [ -nRyzZ ] [ -e <cmd> ] [ -F <file> ] [ -f <file> ]\n\
74        madmutt [ -n ] [ -e <cmd> ] [ -a <file> ] [ -F <file> ] [ -H <file> ] [ -i <file> ] [ -s <subj> ] [ -b <addr> ] [ -c <addr> ] <addr> [ ... ]\n\
75        madmutt [ -n ] [ -e <cmd> ] [ -F <file> ] -p\n\
76        madmutt -v\n"));
77
78     puts(_("Options:"));
79     puts(_("  -a <file>     attach a file to the message"));
80     puts(_("  -b <address>  specify a blind carbon-copy (BCC) address"));
81     puts(_("  -c <address>  specify a carbon-copy (CC) address"));
82     puts(_("  -e <command>  specify a command to be executed after initialization"));
83     puts(_("  -f <file>     specify which mailbox to read"));
84     puts(_("  -F <file>     specify an alternate Madmuttrc file"));
85     puts(_("  -H <file>     specify a draft file to read header and body from"));
86     puts(_("  -i <file>     specify a file which Madmutt should include in the body"));
87     puts(_("  -n            causes Madmutt not to read the system Madmuttrc"));
88     puts(_("  -p            recall a postponed message"));
89     puts(_("  -R            open mailbox in read-only mode"));
90     puts(_("  -s <subj>     specify a subject (must be in quotes if it has spaces)"));
91     puts(_("  -v            show version and compile-time definitions"));
92     puts(_("  -z            exit immediately if there are no messages in the mailbox"));
93     puts(_("  -Z            open the first folder with new message, exit immediately if none"));
94     puts(_("  -h            this help message"));
95
96     exit(0);
97 }
98
99 static void show_version (void)
100 {
101     struct utsname uts;
102     uname(&uts);
103
104     puts(madmutt_version);
105     puts(_("  Copyright (C) 1996-2002 Michael R. Elkins and others."));
106     puts(_("  Copyright (C) 2005      The Mutt-ng Team"));
107     puts(_("  Copyright (C) 2006-2007 Pierre Habouzit"));
108     puts(_("  MadMutt is based on Mutt-ng wich was based on Mutt before"));
109     puts("");
110
111     printf("System:\n  %s %s (%s)\n", uts.sysname, uts.release, uts.machine);
112     puts("External Libraries:");
113 #ifdef NCURSES_VERSION
114     printf("  ncurses %s\n", NCURSES_VERSION);
115 #endif
116 #ifdef _LIBICONV_VERSION
117     printf("  libiconv %d.%d\n", _LIBICONV_VERSION >> 8,
118            _LIBICONV_VERSION & 0xff);
119 #endif
120 #ifdef STRINGPREP_VERSION
121     printf("  libidn %s\n", STRINGPREP_VERSION);
122 #endif
123 #ifdef USE_HCACHE
124 #if defined(HAVE_TOKYOCABINET)
125     printf("  tokyocabinet %s\n", tcversion);
126 #elif defined(HAVE_GDBM)
127     printf("  gdbm %s\n", gdbm_version);
128 #endif
129 #endif
130     printf("  gnutls %s\n", LIBGNUTLS_VERSION);
131     printf("  gpgme %s\n",  GPGME_VERSION);
132     puts (_("Compile Options:"));
133
134     puts (
135 #ifdef USE_HCACHE
136         "  +USE_HCACHE"
137 #else
138         "  -USE_HCACHE"
139 #endif
140 #ifdef HAVE_LIBIDN
141         "  +HAVE_LIBIDN"
142 #else
143         "  -HAVE_LIBIDN"
144 #endif
145         );
146
147     puts(_("Built-In Defaults:"));
148     printf("  SENDMAIL   \"%s\"\n", SENDMAIL);
149     printf("  MAILPATH   \"%s\"\n", MAILPATH);
150     printf("  PKGDATADIR \"%s\"\n", PKGDATADIR);
151     printf("  PKGDOCDIR  \"%s\"\n", PKGDOCDIR);
152     printf("  SYSCONFDIR \"%s\"\n", SYSCONFDIR);
153
154     puts("");
155     puts(_("This is free software.  You may redistribute copies of it under the terms of"));
156     puts(_("the GNU General Public License <http://www.gnu.org/licenses/gpl.html>."));
157     puts(_("There is NO WARRANTY, to the extent permitted by law."));
158
159     exit(0);
160 }
161
162 #define M_IGNORE  (1<<0)        /* -z */
163 #define M_BUFFY   (1<<1)        /* -Z */
164 #define M_NOSYSRC (1<<2)        /* -n */
165 #define M_RO      (1<<3)        /* -R */
166
167 __attribute__((format(printf, 1, 0)))
168 static void mutt_nocurses_error (const char *fmt, ...)
169 {
170     va_list ap;
171
172     va_start(ap, fmt);
173     vfprintf(stderr, fmt, ap);
174     va_end(ap);
175     fputc('\n', stderr);
176 }
177
178 int main (int argc, char **argv)
179 {
180   char folder[_POSIX_PATH_MAX] = "";
181   char *subject = NULL;
182   char *includeFile = NULL;
183   char *draftFile = NULL;
184   HEADER *msg = NULL;
185   string_list_t *attach = NULL;
186   string_list_t *commands = NULL;
187   int sendflags = 0;
188   int flags = 0;
189   int version = 0;
190   int i;
191   int explicit_folder = 0;
192
193   /* initialize random number for tmp file creation */ 
194   srand48((unsigned int) time (NULL));
195   
196   /* sanity check against stupid administrators */
197   
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   el_initialize();
308   mutt_init (flags & M_NOSYSRC, commands);
309   string_list_wipe(&commands);
310
311   if (!option(OPTNOCURSES)) {
312       ui_layout_init();
313   }
314
315   /* Create the Maildir directory if it doesn't exist. */
316   if (!option(OPTNOCURSES) && Maildir) {
317     struct stat sb;
318     char fpath[_POSIX_PATH_MAX];
319     char mesg[STRING];
320
321     m_strcpy(fpath, sizeof(fpath), Maildir);
322     mutt_expand_path (fpath, sizeof (fpath));
323     /* we're not connected yet - skip mail folder creation */
324     if (mx_get_magic (fpath) != M_IMAP)
325       if (stat (fpath, &sb) == -1 && errno == ENOENT) {
326         snprintf (mesg, sizeof (mesg), _("%s does not exist. Create it?"),
327                   Maildir);
328         if (mutt_yesorno (mesg, M_YES) == M_YES) {
329           if (mkdir (fpath, 0700) == -1 && errno != EEXIST)
330             mutt_error (_("Can't create %s: %s."), Maildir, strerror (errno));
331         }
332       }
333   }
334
335   if (sendflags & SENDPOSTPONED) {
336     if (!option (OPTNOCURSES))
337       mutt_flushinp ();
338     ci_send_message (SENDPOSTPONED, NULL, NULL, NULL, NULL);
339     mutt_endwin (NULL);
340   }
341   else if (subject || msg || sendflags || draftFile || includeFile || attach
342            || optind < argc) {
343     FILE *fin = NULL;
344     char buf[LONG_STRING];
345     char *tempfile = NULL, *infile = NULL;
346     char *bodytext = NULL;
347     FILE *fout;
348
349     if (!option (OPTNOCURSES))
350       mutt_flushinp ();
351
352     if (!msg)
353       msg = header_new();
354
355     if (draftFile)
356       infile = draftFile;
357     else {
358       if (!msg->env)
359         msg->env = envelope_new();
360
361       for (i = optind; i < argc; i++) {
362         if (url_check_scheme (argv[i]) == U_MAILTO)
363           url_parse_mailto (msg->env, &bodytext, argv[i]);
364         else
365           msg->env->to = rfc822_parse_adrlist (msg->env->to, argv[i]);
366       }
367
368       if (option (OPTAUTOEDIT) && !msg->env->to && !msg->env->cc) {
369         if (!option (OPTNOCURSES))
370           mutt_endwin (NULL);
371         fputs (_("No recipients specified.\n"), stderr);
372         exit (1);
373       }
374
375       if (subject)
376         msg->env->subject = m_strdup(subject);
377
378       if (includeFile)
379         infile = includeFile;
380     }
381
382     if (infile || bodytext) {
383       if (infile) {
384         if (m_strcmp("-", infile) == 0)
385           fin = stdin;
386         else {
387           char path[_POSIX_PATH_MAX];
388
389           m_strcpy(path, sizeof(path), infile);
390           mutt_expand_path (path, sizeof (path));
391           if ((fin = fopen (path, "r")) == NULL) {
392             if (!option (OPTNOCURSES))
393               mutt_endwin (NULL);
394             perror (path);
395             exit (1);
396           }
397         }
398       }
399       else
400         fin = NULL;
401
402       if (draftFile)
403         msg->env = mutt_read_rfc822_header (fin, NULL, 1, 0);
404
405       /* is the following if still needed? */
406
407       fout = m_tempfile(buf, sizeof(buf), NONULL(mod_core.tmpdir), NULL);
408       tempfile = m_strdup(buf);
409
410       if (tempfile) {
411         if (!fout) {
412           if (!option (OPTNOCURSES))
413             mutt_endwin (NULL);
414           perror (tempfile);
415           m_fclose(&fin);
416           p_delete(&tempfile);
417           exit (1);
418         }
419         if (fin)
420           mutt_copy_stream (fin, fout);
421         else if (bodytext)
422           fputs (bodytext, fout);
423         m_fclose(&fout);
424         if (fin && fin != stdin)
425           m_fclose(&fin);
426       }
427     }
428
429     p_delete(&bodytext);
430
431     if (attach) {
432       string_list_t *t = attach;
433       BODY *a = NULL;
434
435       while (t) {
436         if (a) {
437           a->next = mutt_make_file_attach (t->data);
438           a = a->next;
439         }
440         else
441           msg->content = a = mutt_make_file_attach (t->data);
442         if (!a) {
443           if (!option (OPTNOCURSES))
444             mutt_endwin (NULL);
445           fprintf (stderr, _("%s: unable to attach file.\n"), t->data);
446           string_list_wipe(&attach);
447           exit (1);
448         }
449         t = t->next;
450       }
451       string_list_wipe(&attach);
452     }
453
454     ci_send_message (sendflags, msg, tempfile, NULL, NULL);
455
456     if (!option (OPTNOCURSES))
457       mutt_endwin (NULL);
458   }
459   else {
460     if (flags & M_BUFFY) {
461       if (!buffy_check (0)) {
462         mutt_endwin _("No mailbox with new mail.");
463
464         exit (1);
465       }
466       folder[0] = 0;
467       buffy_next (folder, sizeof (folder));
468     }
469
470     if (!folder[0])
471       m_strcpy(folder, sizeof(folder), NONULL(Spoolfile));
472
473     mutt_expand_path (folder, sizeof (folder));
474     m_strreplace(&CurrentFolder, folder);
475     m_strreplace(&LastFolder, folder);
476
477     if (flags & M_IGNORE) {
478       /* check to see if there are any messages in the folder */
479       switch (mx_check_empty (folder)) {
480       case -1:
481         mutt_endwin (strerror (errno));
482         exit (1);
483       case 1:
484         mutt_endwin _("Mailbox is empty.");
485         exit (1);
486       }
487     }
488
489     mutt_folder_hook (folder);
490
491     if ((Context = mx_open_mailbox(folder, (flags & M_RO) ?  M_READONLY : 0,
492                                    NULL)) || !explicit_folder)
493     {
494       mutt_index_menu ();
495
496       if (option (OPTXTERMSETTITLES))
497         printf("\033]2;%s\007", NONULL(XtermLeave));
498       if (Context)
499         p_delete(&Context);
500     }
501     mutt_endwin (Errorbuf);
502   }
503
504   luaM_shutdown();
505   mutt_sasl_shutdown();
506   el_shutdown();
507   exit (0);
508 }