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