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