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