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