wibble
[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_NNTP
49 #include "nntp.h"
50 #endif
51
52 #ifdef USE_HCACHE
53 #if defined(HAVE_QDBM)
54 #include <depot.h>
55 #elif defined(HAVE_GDBM)
56 #include <gdbm.h>
57 #endif
58 #endif
59
60 #include <gnutls/gnutls.h>
61 #include <gpgme.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(mutt_make_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 #ifdef USE_NNTP
87     puts(_("  -g <server>   specify a newsserver"));
88     puts(_("  -G            select a newsgroup"));
89 #endif
90     puts(_("  -H <file>     specify a draft file to read header and body from"));
91     puts(_("  -i <file>     specify a file which Madmutt should include in the body"));
92     puts(_("  -n            causes Madmutt not to read the system Madmuttrc"));
93     puts(_("  -p            recall a postponed message"));
94     puts(_("  -R            open mailbox in read-only mode"));
95     puts(_("  -s <subj>     specify a subject (must be in quotes if it has spaces)"));
96     puts(_("  -v            show version and compile-time definitions"));
97     puts(_("  -y            select a mailbox specified in your `mailboxes' list"));
98     puts(_("  -z            exit immediately if there are no messages in the mailbox"));
99     puts(_("  -Z            open the first folder with new message, exit immediately if none"));
100     puts(_("  -h            this help message"));
101
102     exit(0);
103 }
104
105 static void show_version (void)
106 {
107     struct utsname uts;
108     uname(&uts);
109
110     puts(mutt_make_version());
111     puts(_("  Copyright (C) 1996-2002 Michael R. Elkins and others."));
112     puts(_("  Copyright (C) 2005      The Mutt-ng Team"));
113     puts(_("  Copyright (C) 2006-2007 Pierre Habouzit"));
114     puts(_("  MadMutt is based on Mutt-ng wich was based on Mutt before"));
115     puts("");
116
117     printf("System:\n  %s %s (%s)\n", uts.sysname, uts.release, uts.machine);
118     puts("External Libraries:");
119 #ifdef NCURSES_VERSION
120     printf("  ncurses %s\n", NCURSES_VERSION);
121 #endif
122 #ifdef _LIBICONV_VERSION
123     printf("  libiconv %d.%d\n", _LIBICONV_VERSION >> 8,
124            _LIBICONV_VERSION & 0xff);
125 #endif
126 #ifdef STRINGPREP_VERSION
127     printf("  libidn %s\n", STRINGPREP_VERSION);
128 #endif
129 #ifdef USE_HCACHE
130 #if defined(HAVE_QDBM)
131     printf("  qdbm %s\n", dpversion);
132 #elif defined(HAVE_GDBM)
133     printf("  gdbm %s\n", gdbm_version);
134 #endif
135 #endif
136     printf("  gnutls %s\n", LIBGNUTLS_VERSION);
137     printf("  gpgme %s\n",  GPGME_VERSION);
138     puts (_("Compile Options:"));
139
140     puts (
141 #ifdef USE_SETGID
142         "  +USE_SETGID"
143 #else
144         "  -USE_SETGID"
145 #endif
146 #ifdef USE_FCNTL
147         "  +USE_FCNTL"
148 #else
149         "  -USE_FCNTL"
150 #endif
151 #ifdef USE_FLOCK
152         "  +USE_FLOCK"
153 #else
154         "  -USE_FLOCK"
155 #endif
156 #ifdef USE_HCACHE
157         "  +USE_HCACHE"
158 #else
159         "  -USE_HCACHE"
160 #endif
161 #ifdef USE_NNTP
162         "  +USE_NNTP"
163 #else
164         "  -USE_NNTP"
165 #endif
166 #ifdef HAVE_LIBIDN
167         "  +HAVE_LIBIDN"
168 #else
169         "  -HAVE_LIBIDN"
170 #endif
171         );
172
173     puts(_("Built-In Defaults:"));
174     printf("  SENDMAIL   \"%s\"\n", SENDMAIL);
175     printf("  MAILPATH   \"%s\"\n", MAILPATH);
176     printf("  PKGDATADIR \"%s\"\n", PKGDATADIR);
177     printf("  PKGDOCDIR  \"%s\"\n", PKGDOCDIR);
178     printf("  SYSCONFDIR \"%s\"\n", SYSCONFDIR);
179
180     puts("");
181     puts(_("This is free software.  You may redistribute copies of it under the terms of"));
182     puts(_("the GNU General Public License <http://www.gnu.org/licenses/gpl.html>."));
183     puts(_("There is NO WARRANTY, to the extent permitted by law."));
184
185     exit(0);
186 }
187
188 static void start_curses (void)
189 {
190   km_init ();                   /* must come before mutt_init */
191   mutt_signal_initialize ();
192   if (initscr () == NULL) {
193     puts _("Error initializing terminal.");
194
195     exit (1);
196   }
197   mutt_signal_initialize ();
198   curses_initialize();
199 }
200
201 #define M_IGNORE  (1<<0)        /* -z */
202 #define M_BUFFY   (1<<1)        /* -Z */
203 #define M_NOSYSRC (1<<2)        /* -n */
204 #define M_RO      (1<<3)        /* -R */
205 #define M_SELECT  (1<<4)        /* -y */
206 #ifdef USE_NNTP
207 #define M_NEWS    (1<<5)        /* -g and -G */
208 #endif
209
210 __attribute__((format(printf, 1, 0)))
211 static void mutt_nocurses_error (const char *fmt, ...)
212 {
213     va_list ap;
214
215     va_start(ap, fmt);
216     vfprintf(stderr, fmt, ap);
217     va_end(ap);
218     fputc('\n', stderr);
219 }
220
221 int main (int argc, char **argv)
222 {
223   char folder[_POSIX_PATH_MAX] = "";
224   char *subject = NULL;
225   char *includeFile = NULL;
226   char *draftFile = NULL;
227   HEADER *msg = NULL;
228   string_list_t *attach = NULL;
229   string_list_t *commands = NULL;
230   int sendflags = 0;
231   int flags = 0;
232   int version = 0;
233   int i;
234   int explicit_folder = 0;
235
236   /* initialize random number for tmp file creation */ 
237   srand48((unsigned int) time (NULL));
238   
239   /* sanity check against stupid administrators */
240   
241   if (getegid () != getgid ()) {
242     fprintf (stderr, "%s: I don't want to run with privileges!\n", argv[0]);
243     exit (1);
244   }
245
246   setlocale (LC_ALL, "");
247   bindtextdomain (PACKAGE, MUTTLOCALEDIR);
248   textdomain (PACKAGE);
249   setlocale (LC_CTYPE, "");
250
251   mutt_error = mutt_message = mutt_nocurses_error;
252   srand48 (time (NULL));
253   umask (077);
254
255   while ((i = getopt(argc, argv, "a:b:F:f:c:e:H:s:i:hnpRTtvyzZ"
256 #ifdef USE_NNTP
257                                                                     "g:G"
258 #endif
259                     )) >= 0)
260     switch (i) {
261     case 'a':
262       if (strlen(optarg)<=512)
263         attach = mutt_add_list (attach, optarg);
264       else{
265         printf("too long arguments. exiting ...\n");
266         exit(1);
267       }
268       break;
269
270     case 'F':
271       m_strreplace(&Muttrc, optarg);
272       break;
273
274     case 'f':
275       m_strcpy(folder, sizeof(folder), optarg);
276       explicit_folder = 1;
277       break;
278
279     case 'b':
280     case 'c':
281       if (!msg)
282         msg = header_new();
283       if (!msg->env)
284         msg->env = envelope_new();
285       if (i == 'b')
286         msg->env->bcc = rfc822_parse_adrlist (msg->env->bcc, optarg);
287       else
288         msg->env->cc = rfc822_parse_adrlist (msg->env->cc, optarg);
289       break;
290
291     case 'e':
292       commands = mutt_add_list (commands, optarg);
293       break;
294
295     case 'H':
296       draftFile = optarg;
297       break;
298
299     case 'i':
300       includeFile = optarg;
301       break;
302
303     case 'n':
304       flags |= M_NOSYSRC;
305       break;
306
307     case 'p':
308       sendflags |= SENDPOSTPONED;
309       break;
310
311     case 'R':
312       flags |= M_RO;            /* read-only mode */
313       break;
314
315     case 's':
316       subject = optarg;
317       break;
318
319     case 'v':
320       version++;
321       break;
322
323     case 'y':                  /* My special hack mode */
324       flags |= M_SELECT;
325       break;
326
327 #ifdef USE_NNTP
328     case 'g':                  /* Specify a newsserver */
329       {
330         char buf[LONG_STRING];
331
332         snprintf (buf, sizeof (buf), "set nntp_host=%s", optarg);
333         commands = mutt_add_list (commands, buf);
334       }
335
336     case 'G':                  /* List of newsgroups */
337       flags |= M_SELECT | M_NEWS;
338       break;
339 #endif
340
341     case 'z':
342       flags |= M_IGNORE;
343       break;
344
345     case 'Z':
346       flags |= M_BUFFY | M_IGNORE;
347       break;
348
349     default:
350       mutt_usage ();
351     }
352
353   if (version) {
354     show_version ();
355   }
356
357   /* Check for a batch send. */
358   if (!isatty (0)) {
359     set_option(OPTNOCURSES);
360     sendflags = SENDBATCH;
361   }
362
363   /* This must come before mutt_init() because curses needs to be started
364      before calling the init_pair() function to set the color scheme.  */
365   if (!option (OPTNOCURSES))
366     start_curses();
367
368   /* set defaults and read init files */
369   mutt_init (flags & M_NOSYSRC, commands);
370   string_list_wipe(&commands);
371
372   if (!option(OPTNOCURSES)) {
373       ui_layout_init();
374   }
375
376   /* Create the Maildir directory if it doesn't exist. */
377   if (!option(OPTNOCURSES) && Maildir) {
378     struct stat sb;
379     char fpath[_POSIX_PATH_MAX];
380     char mesg[STRING];
381
382     m_strcpy(fpath, sizeof(fpath), Maildir);
383     mutt_expand_path (fpath, sizeof (fpath));
384     /* we're not connected yet - skip mail folder creation */
385     if (mx_get_magic (fpath) != M_IMAP)
386       if (stat (fpath, &sb) == -1 && errno == ENOENT) {
387         snprintf (mesg, sizeof (mesg), _("%s does not exist. Create it?"),
388                   Maildir);
389         if (mutt_yesorno (mesg, M_YES) == M_YES) {
390           if (mkdir (fpath, 0700) == -1 && errno != EEXIST)
391             mutt_error (_("Can't create %s: %s."), Maildir, strerror (errno));
392         }
393       }
394   }
395
396   if (sendflags & SENDPOSTPONED) {
397     if (!option (OPTNOCURSES))
398       mutt_flushinp ();
399     ci_send_message (SENDPOSTPONED, NULL, NULL, NULL, NULL);
400     mutt_endwin (NULL);
401   }
402   else if (subject || msg || sendflags || draftFile || includeFile || attach
403            || optind < argc) {
404     FILE *fin = NULL;
405     char buf[LONG_STRING];
406     char *tempfile = NULL, *infile = NULL;
407     char *bodytext = NULL;
408     FILE *fout;
409
410     if (!option (OPTNOCURSES))
411       mutt_flushinp ();
412
413     if (!msg)
414       msg = header_new();
415
416     if (draftFile)
417       infile = draftFile;
418     else {
419       if (!msg->env)
420         msg->env = envelope_new();
421
422       for (i = optind; i < argc; i++) {
423         if (url_check_scheme (argv[i]) == U_MAILTO)
424           url_parse_mailto (msg->env, &bodytext, argv[i]);
425         else
426           msg->env->to = rfc822_parse_adrlist (msg->env->to, argv[i]);
427       }
428
429       if (option (OPTAUTOEDIT) && !msg->env->to && !msg->env->cc) {
430         if (!option (OPTNOCURSES))
431           mutt_endwin (NULL);
432         fputs (_("No recipients specified.\n"), stderr);
433         exit (1);
434       }
435
436       if (subject)
437         msg->env->subject = m_strdup(subject);
438
439       if (includeFile)
440         infile = includeFile;
441     }
442
443     if (infile || bodytext) {
444       if (infile) {
445         if (m_strcmp("-", infile) == 0)
446           fin = stdin;
447         else {
448           char path[_POSIX_PATH_MAX];
449
450           m_strcpy(path, sizeof(path), infile);
451           mutt_expand_path (path, sizeof (path));
452           if ((fin = fopen (path, "r")) == NULL) {
453             if (!option (OPTNOCURSES))
454               mutt_endwin (NULL);
455             perror (path);
456             exit (1);
457           }
458         }
459       }
460       else
461         fin = NULL;
462
463       if (draftFile)
464         msg->env = mutt_read_rfc822_header (fin, NULL, 1, 0);
465
466       /* is the following if still needed? */
467
468       fout = m_tempfile(buf, sizeof(buf), NONULL(mod_core.tmpdir), NULL);
469       tempfile = m_strdup(buf);
470
471       if (tempfile) {
472         if (!fout) {
473           if (!option (OPTNOCURSES))
474             mutt_endwin (NULL);
475           perror (tempfile);
476           m_fclose(&fin);
477           p_delete(&tempfile);
478           exit (1);
479         }
480         if (fin)
481           mutt_copy_stream (fin, fout);
482         else if (bodytext)
483           fputs (bodytext, fout);
484         m_fclose(&fout);
485         if (fin && fin != stdin)
486           m_fclose(&fin);
487       }
488     }
489
490     p_delete(&bodytext);
491
492     if (attach) {
493       string_list_t *t = attach;
494       BODY *a = NULL;
495
496       while (t) {
497         if (a) {
498           a->next = mutt_make_file_attach (t->data);
499           a = a->next;
500         }
501         else
502           msg->content = a = mutt_make_file_attach (t->data);
503         if (!a) {
504           if (!option (OPTNOCURSES))
505             mutt_endwin (NULL);
506           fprintf (stderr, _("%s: unable to attach file.\n"), t->data);
507           string_list_wipe(&attach);
508           exit (1);
509         }
510         t = t->next;
511       }
512       string_list_wipe(&attach);
513     }
514
515     ci_send_message (sendflags, msg, tempfile, NULL, NULL);
516
517     if (!option (OPTNOCURSES))
518       mutt_endwin (NULL);
519   }
520   else {
521     if (flags & M_BUFFY) {
522       if (!buffy_check (0)) {
523         mutt_endwin _("No mailbox with new mail.");
524
525         exit (1);
526       }
527       folder[0] = 0;
528       buffy_next (folder, sizeof (folder));
529     }
530     else if (flags & M_SELECT) {
531 #ifdef USE_NNTP
532       if (flags & M_NEWS) {
533         set_option (OPTNEWS);
534         if (!(CurrentNewsSrv = mutt_select_newsserver (NewsServer))) {
535           mutt_endwin (Errorbuf);
536           exit (1);
537         }
538       } else
539 #endif
540       if (!Incoming.len) {
541         mutt_endwin _("No incoming mailboxes defined.");
542
543         exit (1);
544       }
545       folder[0] = 0;
546       mutt_select_file(folder, sizeof(folder), M_SEL_FOLDER | M_SEL_BUFFY,
547                        NULL, NULL);
548       if (!folder[0]) {
549         mutt_endwin (NULL);
550         exit (0);
551       }
552     }
553
554     if (!folder[0])
555       m_strcpy(folder, sizeof(folder), NONULL(Spoolfile));
556
557 #ifdef USE_NNTP
558     if (option (OPTNEWS)) {
559       unset_option (OPTNEWS);
560       nntp_expand_path (folder, sizeof (folder),
561                         &CurrentNewsSrv->conn->account);
562     } else
563 #endif
564       mutt_expand_path (folder, sizeof (folder));
565
566     m_strreplace(&CurrentFolder, folder);
567     m_strreplace(&LastFolder, folder);
568
569     if (flags & M_IGNORE) {
570       /* check to see if there are any messages in the folder */
571       switch (mx_check_empty (folder)) {
572       case -1:
573         mutt_endwin (strerror (errno));
574         exit (1);
575       case 1:
576         mutt_endwin _("Mailbox is empty.");
577         exit (1);
578       }
579     }
580
581     mutt_folder_hook (folder);
582
583     if ((Context = mx_open_mailbox(folder, (flags & M_RO) ?  M_READONLY : 0,
584                                    NULL)) || !explicit_folder)
585     {
586       mutt_index_menu ();
587       if (option (OPTXTERMSETTITLES))
588         printf("\033]2;%s\007", NONULL(XtermLeave));
589       if (Context)
590         p_delete(&Context);
591     }
592     mutt_endwin (Errorbuf);
593   }
594
595   luaM_shutdown();
596   mutt_sasl_shutdown();
597   exit (0);
598 }