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