get rid of slang
[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/curses.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 static const char *Notice = N_("\
64 Copyright (C) 1996-2002 Michael R. Elkins and others.\n\
65 Copyright (C) 2005 The Mutt-ng Team\n\
66 Copyright (C) 2006 Pierre Habouzit undation, Inc.\n\
67 This is free software.  You may redistribute copies of it under the terms of\n\
68 the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.\n\
69 There is NO WARRANTY, to the extent permitted by law.\n");
70
71 void mutt_exit (int code)
72 {
73   mutt_endwin (NULL);
74   exit (code);
75 }
76
77 static void mutt_usage (void)
78 {
79   puts (mutt_make_version());
80
81   puts
82     _("usage: madmutt [ -nRyzZ ] [ -e <cmd> ] [ -F <file> ] [ -m <type> ] [ -f <file> ]\n\
83        madmutt [ -nR ] [ -e <cmd> ] [ -F <file> ] -Q <query> [ -Q <query> ] [...]\n\
84        madmutt [ -nR ] [ -e <cmd> ] [ -F <file> ] -A <alias> [ -A <alias> ] [...]\n\
85        madmutt [ -nR ] [ -e <cmd> ] [ -F <file> ] -t");
86   puts
87     _("\
88        madmutt [ -nR ] [ -e <cmd> ] [ -F <file> ] -T\n\
89        madmutt [ -nx ] [ -e <cmd> ] [ -a <file> ] [ -F <file> ] [ -H <file> ] [ -i <file> ] [ -s <subj> ] [ -b <addr> ] [ -c <addr> ] <addr> [ ... ]\n\
90        madmutt [ -n ] [ -e <cmd> ] [ -F <file> ] -p\n\
91        madmutt -v\n\
92 \n\
93 options:\n\
94   -A <alias>\texpand the given alias\n\
95   -a <file>\tattach a file to the message\n\
96   -b <address>\tspecify a blind carbon-copy (BCC) address\n\
97   -c <address>\tspecify a carbon-copy (CC) address");
98   puts _("\
99   -e <command>\tspecify a command to be executed after initialization\n\
100   -f <file>\tspecify which mailbox to read\n\
101   -F <file>\tspecify an alternate muttrngc file\n\
102   -g <server>\tspecify a newsserver (if compiled with NNTP)\n\
103   -G\t\tselect a newsgroup (if compiled with NNTP)\n\
104   -H <file>\tspecify a draft file to read header and body from\n\
105   -i <file>\tspecify a file which Madmutt should include in the body");
106   puts _("\
107   -d <level>\t specify debugging level of Madmutt\n\
108   -m <type>\tspecify a default mailbox type\n\
109   -n\t\tcauses Madmutt not to read the system Madmuttrc\n\
110   -p\t\trecall a postponed message\n\
111   -Q <variable>\tquery a configuration variable\n\
112   -R\t\topen mailbox in read-only mode\n\
113   -s <subj>\tspecify a subject (must be in quotes if it has spaces)");
114   puts _("\
115   -t\t\tprint the value of all variables to stdout\n\
116   -T\t\tprint the value of all changed variables to stdout\n\
117   -v\t\tshow version and compile-time definitions\n\
118   -y\t\tselect a mailbox specified in your `mailboxes' list\n\
119   -z\t\texit immediately if there are no messages in the mailbox\n\
120   -Z\t\topen the first folder with new message, exit immediately if none\n\
121   -h\t\tthis help message");
122
123   exit (0);
124 }
125
126 static void show_version (void)
127 {
128   struct utsname uts;
129
130   puts (mutt_make_version());
131   puts (_(Notice));
132
133   uname (&uts);
134
135 #ifdef _AIX
136   printf ("System:\n  %s %s.%s", uts.sysname, uts.version, uts.release);
137 #elif defined (SCO)
138   printf ("System:\n  SCO %s", uts.release);
139 #else
140   printf ("System:\n  %s %s", uts.sysname, uts.release);
141 #endif
142
143   printf (" (%s)\nExternal Libraries:\n", uts.machine);
144
145 #ifdef NCURSES_VERSION
146   printf ("  ncurses %s\n", NCURSES_VERSION);
147 #endif
148
149 #ifdef _LIBICONV_VERSION
150   printf ("  libiconv %d.%d\n", _LIBICONV_VERSION >> 8,
151           _LIBICONV_VERSION & 0xff);
152 #endif
153
154 #ifdef HAVE_LIBIDN
155   printf ("  libidn %s (compiled with %s)\n",
156           stringprep_check_version (NULL), STRINGPREP_VERSION);
157 #endif
158
159 #ifdef USE_HCACHE
160 #if defined(HAVE_QDBM)
161   printf ("  qdbm %s\n", dpversion);
162 #elif defined(HAVE_GDBM)
163   printf ("  gdbm %s\n", gdbm_version);
164 #endif
165 #endif
166
167   printf ("  gnutls %s\n", LIBGNUTLS_VERSION);
168   printf ("  gpgme %s\n", GPGME_VERSION);
169   puts (_("Compile Options:"));
170
171   puts (
172 #ifdef USE_SETGID
173          "+USE_SETGID  "
174 #else
175          "-USE_SETGID  "
176 #endif
177 #ifdef USE_FCNTL
178          "+USE_FCNTL  "
179 #else
180          "-USE_FCNTL  "
181 #endif
182 #ifdef USE_FLOCK
183          "+USE_FLOCK   "
184 #else
185          "-USE_FLOCK   "
186 #endif
187 #ifdef USE_HCACHE
188          "+USE_HCACHE  "
189 #else
190          "-USE_HCACHE  "
191 #endif
192     );
193   puts (
194 #ifdef USE_NNTP
195          "+USE_NNTP  "
196 #else
197          "-USE_NNTP  "
198 #endif
199 #ifdef HAVE_LIBIDN
200          "+HAVE_LIBIDN  "
201 #else
202          "-HAVE_LIBIDN  "
203 #endif
204     );
205
206   puts (_("Built-In Defaults:"));
207   printf ("  +SENDMAIL=\"%s\"\n", SENDMAIL);
208   printf ("  +MAILPATH=\"%s\"\n", MAILPATH);
209   printf ("  +PKGDATADIR=\"%s\"\n", PKGDATADIR);
210   printf ("  +PKGDOCDIR=\"%s\"\n", PKGDOCDIR);
211   printf ("  +SYSCONFDIR=\"%s\"\n", SYSCONFDIR);
212   printf ("  +MIXMASTER=\"%s\"\n\n", MIXMASTER);
213
214   puts (_("MadMutt is based on Madmutt wich was based on Mutt before\n"));
215
216   exit (0);
217 }
218
219 static void start_curses (void)
220 {
221   km_init ();                   /* must come before mutt_init */
222
223   /* should come before initscr() so that ncurses 4.2 doesn't try to install
224      its own SIGWINCH handler */
225   mutt_signal_initialize ();
226   if (initscr () == NULL) {
227     puts _("Error initializing terminal.");
228
229     exit (1);
230   }
231   mutt_signal_initialize ();
232   ci_start_color ();
233   keypad (stdscr, TRUE);
234   cbreak ();
235   noecho ();
236 #ifdef HAVE_TYPEAHEAD
237   typeahead (-1);               /* simulate smooth scrolling */
238 #endif
239 #ifdef HAVE_META
240   meta (stdscr, TRUE);
241 #endif
242 }
243
244 #define M_IGNORE  (1<<0)        /* -z */
245 #define M_BUFFY   (1<<1)        /* -Z */
246 #define M_NOSYSRC (1<<2)        /* -n */
247 #define M_RO      (1<<3)        /* -R */
248 #define M_SELECT  (1<<4)        /* -y */
249 #ifdef USE_NNTP
250 #define M_NEWS    (1<<5)        /* -g and -G */
251 #endif
252
253 __attribute__((format(printf, 1, 0)))
254 static void mutt_nocurses_error (const char *fmt, ...)
255 {
256     va_list ap;
257
258     va_start(ap, fmt);
259     vfprintf(stderr, fmt, ap);
260     va_end(ap);
261     fputc('\n', stderr);
262 }
263
264 int main (int argc, char **argv)
265 {
266   char folder[_POSIX_PATH_MAX] = "";
267   char *subject = NULL;
268   char *includeFile = NULL;
269   char *draftFile = NULL;
270   char *newMagic = NULL;
271   HEADER *msg = NULL;
272   string_list_t *attach = NULL;
273   string_list_t *commands = NULL;
274   string_list_t *queries = NULL;
275   string_list_t *alias_queries = NULL;
276   int sendflags = 0;
277   int flags = 0;
278   int version = 0;
279   int i;
280   int explicit_folder = 0;
281   int dump_variables = -1;
282
283   /* initialize random number for tmp file creation */ 
284   srand48((unsigned int) time (NULL));
285   
286   /* sanity check against stupid administrators */
287   
288   if (getegid () != getgid ()) {
289     fprintf (stderr, "%s: I don't want to run with privileges!\n", argv[0]);
290     exit (1);
291   }
292
293   setlocale (LC_ALL, "");
294   bindtextdomain (PACKAGE, MUTTLOCALEDIR);
295   textdomain (PACKAGE);
296   setlocale (LC_CTYPE, "");
297
298   mutt_error = mutt_message = mutt_nocurses_error;
299   srand48 (time (NULL));
300   umask (077);
301
302   p_clear(Options, countof(Options));
303   p_clear(QuadOptions, countof(QuadOptions));
304
305   while ((i = getopt(argc, argv, "A:a:b:F:f:c:e:H:s:i:hm:npQ:RTtvyzZ"
306 #ifdef USE_NNTP
307                                                                     "g:G"
308 #endif
309                     )) >= 0)
310     switch (i) {
311     case 'A':
312       alias_queries = mutt_add_list (alias_queries, optarg);
313       break;
314     case 'a':
315       if (strlen(optarg)<=512)
316         attach = mutt_add_list (attach, optarg);
317       else{
318         printf("too long arguments. exiting ...\n");
319         exit(1);
320       }
321       break;
322
323     case 'F':
324       m_strreplace(&Muttrc, optarg);
325       break;
326
327     case 'f':
328       m_strcpy(folder, sizeof(folder), optarg);
329       explicit_folder = 1;
330       break;
331
332     case 'b':
333     case 'c':
334       if (!msg)
335         msg = header_new();
336       if (!msg->env)
337         msg->env = envelope_new();
338       if (i == 'b')
339         msg->env->bcc = rfc822_parse_adrlist (msg->env->bcc, optarg);
340       else
341         msg->env->cc = rfc822_parse_adrlist (msg->env->cc, optarg);
342       break;
343
344     case 't':
345       dump_variables = 2;
346       break;
347
348     case 'T':
349       dump_variables = 1;
350       break;
351
352     case 'e':
353       commands = mutt_add_list (commands, optarg);
354       break;
355
356     case 'H':
357       draftFile = optarg;
358       break;
359
360     case 'i':
361       includeFile = optarg;
362       break;
363
364     case 'm':
365       /* should take precedence over .muttrc setting, so save it for later */
366       newMagic = optarg;
367       break;
368
369     case 'n':
370       flags |= M_NOSYSRC;
371       break;
372
373     case 'p':
374       sendflags |= SENDPOSTPONED;
375       break;
376
377     case 'Q':
378       queries = mutt_add_list (queries, optarg);
379       break;
380
381     case 'R':
382       flags |= M_RO;            /* read-only mode */
383       break;
384
385     case 's':
386       subject = optarg;
387       break;
388
389     case 'v':
390       version++;
391       break;
392
393     case 'y':                  /* My special hack mode */
394       flags |= M_SELECT;
395       break;
396
397 #ifdef USE_NNTP
398     case 'g':                  /* Specify a newsserver */
399       {
400         char buf[LONG_STRING];
401
402         snprintf (buf, sizeof (buf), "set nntp_host=%s", optarg);
403         commands = mutt_add_list (commands, buf);
404       }
405
406     case 'G':                  /* List of newsgroups */
407       flags |= M_SELECT | M_NEWS;
408       break;
409 #endif
410
411     case 'z':
412       flags |= M_IGNORE;
413       break;
414
415     case 'Z':
416       flags |= M_BUFFY | M_IGNORE;
417       break;
418
419     default:
420       mutt_usage ();
421     }
422
423   if (version) {
424     show_version ();
425   }
426
427   /* Check for a batch send. */
428   if (!isatty (0) || queries || alias_queries || dump_variables > 0) {
429     set_option (OPTNOCURSES);
430     sendflags = SENDBATCH;
431   }
432
433   /* This must come before mutt_init() because curses needs to be started
434      before calling the init_pair() function to set the color scheme.  */
435   if (!option (OPTNOCURSES))
436     start_curses ();
437
438   /* set defaults and read init files */
439   mutt_init (flags & M_NOSYSRC, commands);
440   string_list_wipe(&commands);
441
442   if (queries)
443     return mutt_query_variables (queries);
444   if (dump_variables > 0)
445     return (mutt_dump_variables (dump_variables-1));
446
447   if (alias_queries) {
448     int rv = 0;
449     address_t *a;
450
451     for (; alias_queries; alias_queries = alias_queries->next) {
452       if ((a = address_list_dup(alias_lookup(alias_queries->data)))) {
453         /* output in machine-readable form */
454         mutt_addrlist_to_idna (a, NULL);
455         mutt_write_address_list (a, stdout, 0, 0);
456         address_list_wipe(&a);
457       } else {
458         rv = 1;
459         printf ("%s\n", alias_queries->data);
460       }
461     }
462     return rv;
463   }
464
465   if (newMagic)
466     mx_set_magic (newMagic);
467
468   if (!option (OPTNOCURSES)) {
469     SETCOLOR (MT_COLOR_NORMAL);
470     clear ();
471     mutt_error = mutt_curses_error;
472     mutt_message = mutt_curses_message;
473   }
474
475   /* Create the Maildir directory if it doesn't exist. */
476   if (!option (OPTNOCURSES) && Maildir) {
477     struct stat sb;
478     char fpath[_POSIX_PATH_MAX];
479     char mesg[STRING];
480
481     m_strcpy(fpath, sizeof(fpath), Maildir);
482     mutt_expand_path (fpath, sizeof (fpath));
483     /* we're not connected yet - skip mail folder creation */
484     if (mx_get_magic (fpath) != M_IMAP)
485       if (stat (fpath, &sb) == -1 && errno == ENOENT) {
486         snprintf (mesg, sizeof (mesg), _("%s does not exist. Create it?"),
487                   Maildir);
488         if (mutt_yesorno (mesg, M_YES) == M_YES) {
489           if (mkdir (fpath, 0700) == -1 && errno != EEXIST)
490             mutt_error (_("Can't create %s: %s."), Maildir, strerror (errno));
491         }
492       }
493   }
494
495   if (sendflags & SENDPOSTPONED) {
496     if (!option (OPTNOCURSES))
497       mutt_flushinp ();
498     ci_send_message (SENDPOSTPONED, NULL, NULL, NULL, NULL);
499     mutt_endwin (NULL);
500   }
501   else if (subject || msg || sendflags || draftFile || includeFile || attach
502            || optind < argc) {
503     FILE *fin = NULL;
504     char buf[LONG_STRING];
505     char *tempfile = NULL, *infile = NULL;
506     char *bodytext = NULL;
507     FILE *fout;
508
509     if (!option (OPTNOCURSES))
510       mutt_flushinp ();
511
512     if (!msg)
513       msg = header_new();
514
515     if (draftFile)
516       infile = draftFile;
517     else {
518       if (!msg->env)
519         msg->env = envelope_new();
520
521       for (i = optind; i < argc; i++) {
522         if (url_check_scheme (argv[i]) == U_MAILTO)
523           url_parse_mailto (msg->env, &bodytext, argv[i]);
524         else
525           msg->env->to = rfc822_parse_adrlist (msg->env->to, argv[i]);
526       }
527
528       if (option (OPTAUTOEDIT) && !msg->env->to && !msg->env->cc) {
529         if (!option (OPTNOCURSES))
530           mutt_endwin (NULL);
531         fputs (_("No recipients specified.\n"), stderr);
532         exit (1);
533       }
534
535       if (subject)
536         msg->env->subject = m_strdup(subject);
537
538       if (includeFile)
539         infile = includeFile;
540     }
541
542     if (infile || bodytext) {
543       if (infile) {
544         if (m_strcmp("-", infile) == 0)
545           fin = stdin;
546         else {
547           char path[_POSIX_PATH_MAX];
548
549           m_strcpy(path, sizeof(path), infile);
550           mutt_expand_path (path, sizeof (path));
551           if ((fin = fopen (path, "r")) == NULL) {
552             if (!option (OPTNOCURSES))
553               mutt_endwin (NULL);
554             perror (path);
555             exit (1);
556           }
557         }
558       }
559       else
560         fin = NULL;
561
562       if (draftFile)
563         msg->env = mutt_read_rfc822_header (fin, NULL, 1, 0);
564
565       /* is the following if still needed? */
566
567       fout = m_tempfile(buf, sizeof(buf), NONULL(mod_core.tmpdir), NULL);
568       tempfile = m_strdup(buf);
569
570       if (tempfile) {
571         if (!fout) {
572           if (!option (OPTNOCURSES))
573             mutt_endwin (NULL);
574           perror (tempfile);
575           m_fclose(&fin);
576           p_delete(&tempfile);
577           exit (1);
578         }
579         if (fin)
580           mutt_copy_stream (fin, fout);
581         else if (bodytext)
582           fputs (bodytext, fout);
583         m_fclose(&fout);
584         if (fin && fin != stdin)
585           m_fclose(&fin);
586       }
587     }
588
589     p_delete(&bodytext);
590
591     if (attach) {
592       string_list_t *t = attach;
593       BODY *a = NULL;
594
595       while (t) {
596         if (a) {
597           a->next = mutt_make_file_attach (t->data);
598           a = a->next;
599         }
600         else
601           msg->content = a = mutt_make_file_attach (t->data);
602         if (!a) {
603           if (!option (OPTNOCURSES))
604             mutt_endwin (NULL);
605           fprintf (stderr, _("%s: unable to attach file.\n"), t->data);
606           string_list_wipe(&attach);
607           exit (1);
608         }
609         t = t->next;
610       }
611       string_list_wipe(&attach);
612     }
613
614     ci_send_message (sendflags, msg, tempfile, NULL, NULL);
615
616     if (!option (OPTNOCURSES))
617       mutt_endwin (NULL);
618   }
619   else {
620     if (flags & M_BUFFY) {
621       if (!buffy_check (0)) {
622         mutt_endwin _("No mailbox with new mail.");
623
624         exit (1);
625       }
626       folder[0] = 0;
627       buffy_next (folder, sizeof (folder));
628     }
629     else if (flags & M_SELECT) {
630 #ifdef USE_NNTP
631       if (flags & M_NEWS) {
632         set_option (OPTNEWS);
633         if (!(CurrentNewsSrv = mutt_select_newsserver (NewsServer))) {
634           mutt_endwin (Errorbuf);
635           exit (1);
636         }
637       } else
638 #endif
639       if (!Incoming.len) {
640         mutt_endwin _("No incoming mailboxes defined.");
641
642         exit (1);
643       }
644       folder[0] = 0;
645       mutt_select_file(folder, sizeof(folder), M_SEL_FOLDER | M_SEL_BUFFY,
646                        NULL, NULL);
647       if (!folder[0]) {
648         mutt_endwin (NULL);
649         exit (0);
650       }
651     }
652
653     if (!folder[0])
654       m_strcpy(folder, sizeof(folder), NONULL(Spoolfile));
655
656 #ifdef USE_NNTP
657     if (option (OPTNEWS)) {
658       unset_option (OPTNEWS);
659       nntp_expand_path (folder, sizeof (folder),
660                         &CurrentNewsSrv->conn->account);
661     } else
662 #endif
663       mutt_expand_path (folder, sizeof (folder));
664
665     m_strreplace(&CurrentFolder, folder);
666     m_strreplace(&LastFolder, folder);
667
668     if (flags & M_IGNORE) {
669       /* check to see if there are any messages in the folder */
670       switch (mx_check_empty (folder)) {
671       case -1:
672         mutt_endwin (strerror (errno));
673         exit (1);
674       case 1:
675         mutt_endwin _("Mailbox is empty.");
676         exit (1);
677       }
678     }
679
680     mutt_folder_hook (folder);
681
682     if ((Context = mx_open_mailbox (folder, ((flags & M_RO)
683                                              || option (OPTREADONLY)) ?
684                                     M_READONLY : 0, NULL))
685         || !explicit_folder) {
686       mutt_index_menu ();
687       if (option (OPTXTERMSETTITLES))
688         printf("\033]2;%s\007", NONULL(XtermLeave));
689       if (Context)
690         p_delete(&Context);
691     }
692     mutt_endwin (Errorbuf);
693   }
694
695   luaM_shutdown();
696   mutt_sasl_shutdown();
697   exit (0);
698 }