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