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