get rid of Months.
[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 void mutt_exit(int code)
64 {
65     mutt_endwin(NULL);
66     exit(code);
67 }
68
69 static void mutt_usage (void)
70 {
71     puts(mutt_make_version());
72
73     puts(_("\
74 usage: madmutt [ -nRyzZ ] [ -e <cmd> ] [ -F <file> ] [ -f <file> ]\n\
75        madmutt [ -n ] [ -e <cmd> ] [ -a <file> ] [ -F <file> ] [ -H <file> ] [ -i <file> ] [ -s <subj> ] [ -b <addr> ] [ -c <addr> ] <addr> [ ... ]\n\
76        madmutt [ -n ] [ -e <cmd> ] [ -F <file> ] -p\n\
77        madmutt -v\n"));
78
79     puts(_("Options:"));
80     puts(_("  -a <file>     attach a file to the message"));
81     puts(_("  -b <address>  specify a blind carbon-copy (BCC) address"));
82     puts(_("  -c <address>  specify a carbon-copy (CC) address"));
83     puts(_("  -e <command>  specify a command to be executed after initialization"));
84     puts(_("  -f <file>     specify which mailbox to read"));
85     puts(_("  -F <file>     specify an alternate Madmuttrc file"));
86 #ifdef USE_NNTP
87     puts(_("  -g <server>   specify a newsserver"));
88     puts(_("  -G            select a newsgroup"));
89 #endif
90     puts(_("  -H <file>     specify a draft file to read header and body from"));
91     puts(_("  -i <file>     specify a file which Madmutt should include in the body"));
92     puts(_("  -n            causes Madmutt not to read the system Madmuttrc"));
93     puts(_("  -p            recall a postponed message"));
94     puts(_("  -R            open mailbox in read-only mode"));
95     puts(_("  -s <subj>     specify a subject (must be in quotes if it has spaces)"));
96     puts(_("  -v            show version and compile-time definitions"));
97     puts(_("  -y            select a mailbox specified in your `mailboxes' list"));
98     puts(_("  -z            exit immediately if there are no messages in the mailbox"));
99     puts(_("  -Z            open the first folder with new message, exit immediately if none"));
100     puts(_("  -h            this help message"));
101
102     exit(0);
103 }
104
105 static void show_version (void)
106 {
107     struct utsname uts;
108     uname(&uts);
109
110     puts(mutt_make_version());
111     puts(_("  Copyright (C) 1996-2002 Michael R. Elkins and others."));
112     puts(_("  Copyright (C) 2005      The Mutt-ng Team"));
113     puts(_("  Copyright (C) 2006-2007 Pierre Habouzit"));
114     puts(_("  MadMutt is based on Mutt-ng wich was based on Mutt before"));
115     puts("");
116
117     printf("System:\n  %s %s (%s)\n", uts.sysname, uts.release, uts.machine);
118     puts("External Libraries:");
119 #ifdef NCURSES_VERSION
120     printf("  ncurses %s\n", NCURSES_VERSION);
121 #endif
122 #ifdef _LIBICONV_VERSION
123     printf("  libiconv %d.%d\n", _LIBICONV_VERSION >> 8,
124            _LIBICONV_VERSION & 0xff);
125 #endif
126 #ifdef STRINGPREP_VERSION
127     printf("  libidn %s\n", STRINGPREP_VERSION);
128 #endif
129 #ifdef USE_HCACHE
130 #if defined(HAVE_QDBM)
131     printf("  qdbm %s\n", dpversion);
132 #elif defined(HAVE_GDBM)
133     printf("  gdbm %s\n", gdbm_version);
134 #endif
135 #endif
136     printf("  gnutls %s\n", LIBGNUTLS_VERSION);
137     printf("  gpgme %s\n",  GPGME_VERSION);
138     puts (_("Compile Options:"));
139
140     puts (
141 #ifdef USE_FCNTL
142         "  +USE_FCNTL"
143 #else
144         "  -USE_FCNTL"
145 #endif
146 #ifdef USE_FLOCK
147         "  +USE_FLOCK"
148 #else
149         "  -USE_FLOCK"
150 #endif
151 #ifdef USE_HCACHE
152         "  +USE_HCACHE"
153 #else
154         "  -USE_HCACHE"
155 #endif
156 #ifdef USE_NNTP
157         "  +USE_NNTP"
158 #else
159         "  -USE_NNTP"
160 #endif
161 #ifdef HAVE_LIBIDN
162         "  +HAVE_LIBIDN"
163 #else
164         "  -HAVE_LIBIDN"
165 #endif
166         );
167
168     puts(_("Built-In Defaults:"));
169     printf("  SENDMAIL   \"%s\"\n", SENDMAIL);
170     printf("  MAILPATH   \"%s\"\n", MAILPATH);
171     printf("  PKGDATADIR \"%s\"\n", PKGDATADIR);
172     printf("  PKGDOCDIR  \"%s\"\n", PKGDOCDIR);
173     printf("  SYSCONFDIR \"%s\"\n", SYSCONFDIR);
174
175     puts("");
176     puts(_("This is free software.  You may redistribute copies of it under the terms of"));
177     puts(_("the GNU General Public License <http://www.gnu.org/licenses/gpl.html>."));
178     puts(_("There is NO WARRANTY, to the extent permitted by law."));
179
180     exit(0);
181 }
182
183 #define M_IGNORE  (1<<0)        /* -z */
184 #define M_BUFFY   (1<<1)        /* -Z */
185 #define M_NOSYSRC (1<<2)        /* -n */
186 #define M_RO      (1<<3)        /* -R */
187 #define M_SELECT  (1<<4)        /* -y */
188 #define M_NEWS    (1<<5)        /* -g and -G */
189
190 __attribute__((format(printf, 1, 0)))
191 static void mutt_nocurses_error (const char *fmt, ...)
192 {
193     va_list ap;
194
195     va_start(ap, fmt);
196     vfprintf(stderr, fmt, ap);
197     va_end(ap);
198     fputc('\n', stderr);
199 }
200
201 int main (int argc, char **argv)
202 {
203   char folder[_POSIX_PATH_MAX] = "";
204   char *subject = NULL;
205   char *includeFile = NULL;
206   char *draftFile = NULL;
207   HEADER *msg = NULL;
208   string_list_t *attach = NULL;
209   string_list_t *commands = NULL;
210   int sendflags = 0;
211   int flags = 0;
212   int version = 0;
213   int i;
214   int explicit_folder = 0;
215
216   /* initialize random number for tmp file creation */ 
217   srand48((unsigned int) time (NULL));
218   
219   /* sanity check against stupid administrators */
220   
221   if (getegid () != getgid ()) {
222     fprintf (stderr, "%s: I don't want to run with privileges!\n", argv[0]);
223     exit (1);
224   }
225
226   setlocale (LC_ALL, "");
227   bindtextdomain (PACKAGE, MUTTLOCALEDIR);
228   textdomain (PACKAGE);
229   setlocale (LC_CTYPE, "");
230
231   mutt_error = mutt_message = mutt_nocurses_error;
232   srand48 (time (NULL));
233   umask (077);
234
235   while ((i = getopt(argc, argv, "a:b:F:f:c:e:H:s:i:hnpRTtvyzZ"
236 #ifdef USE_NNTP
237                                                                     "g:G"
238 #endif
239                     )) >= 0)
240     switch (i) {
241     case 'a':
242       if (strlen(optarg)<=512)
243         attach = mutt_add_list (attach, optarg);
244       else{
245         printf("too long arguments. exiting ...\n");
246         exit(1);
247       }
248       break;
249
250     case 'F':
251       m_strreplace(&Muttrc, optarg);
252       break;
253
254     case 'f':
255       m_strcpy(folder, sizeof(folder), optarg);
256       explicit_folder = 1;
257       break;
258
259     case 'b':
260     case 'c':
261       if (!msg)
262         msg = header_new();
263       if (!msg->env)
264         msg->env = envelope_new();
265       if (i == 'b')
266         msg->env->bcc = rfc822_parse_adrlist (msg->env->bcc, optarg);
267       else
268         msg->env->cc = rfc822_parse_adrlist (msg->env->cc, optarg);
269       break;
270
271     case 'e':
272       commands = mutt_add_list (commands, optarg);
273       break;
274
275     case 'H':
276       draftFile = optarg;
277       break;
278
279     case 'i':
280       includeFile = optarg;
281       break;
282
283     case 'n':
284       flags |= M_NOSYSRC;
285       break;
286
287     case 'p':
288       sendflags |= SENDPOSTPONED;
289       break;
290
291     case 'R':
292       flags |= M_RO;            /* read-only mode */
293       break;
294
295     case 's':
296       subject = optarg;
297       break;
298
299     case 'v':
300       version++;
301       break;
302
303     case 'y':                  /* My special hack mode */
304       flags |= M_SELECT;
305       break;
306
307 #ifdef USE_NNTP
308     case 'g':                  /* Specify a newsserver */
309       {
310         char buf[LONG_STRING];
311
312         snprintf (buf, sizeof (buf), "set nntp_host=%s", optarg);
313         commands = mutt_add_list (commands, buf);
314       }
315
316     case 'G':                  /* List of newsgroups */
317       flags |= M_SELECT | M_NEWS;
318       break;
319 #endif
320
321     case 'z':
322       flags |= M_IGNORE;
323       break;
324
325     case 'Z':
326       flags |= M_BUFFY | M_IGNORE;
327       break;
328
329     default:
330       mutt_usage ();
331     }
332
333   if (version) {
334     show_version ();
335   }
336
337   /* Check for a batch send. */
338   if (!isatty (0)) {
339     set_option(OPTNOCURSES);
340     sendflags = SENDBATCH;
341   }
342
343   /* This must come before mutt_init() because curses needs to be started
344      before calling the init_pair() function to set the color scheme.  */
345   if (!option (OPTNOCURSES)) {
346     km_init();
347     curses_initialize();
348     mutt_signal_initialize();
349   }
350
351   /* set defaults and read init files */
352   mutt_init (flags & M_NOSYSRC, commands);
353   string_list_wipe(&commands);
354
355   if (!option(OPTNOCURSES)) {
356       ui_layout_init();
357   }
358
359   /* Create the Maildir directory if it doesn't exist. */
360   if (!option(OPTNOCURSES) && Maildir) {
361     struct stat sb;
362     char fpath[_POSIX_PATH_MAX];
363     char mesg[STRING];
364
365     m_strcpy(fpath, sizeof(fpath), Maildir);
366     mutt_expand_path (fpath, sizeof (fpath));
367     /* we're not connected yet - skip mail folder creation */
368     if (mx_get_magic (fpath) != M_IMAP)
369       if (stat (fpath, &sb) == -1 && errno == ENOENT) {
370         snprintf (mesg, sizeof (mesg), _("%s does not exist. Create it?"),
371                   Maildir);
372         if (mutt_yesorno (mesg, M_YES) == M_YES) {
373           if (mkdir (fpath, 0700) == -1 && errno != EEXIST)
374             mutt_error (_("Can't create %s: %s."), Maildir, strerror (errno));
375         }
376       }
377   }
378
379   if (sendflags & SENDPOSTPONED) {
380     if (!option (OPTNOCURSES))
381       mutt_flushinp ();
382     ci_send_message (SENDPOSTPONED, NULL, NULL, NULL, NULL);
383     mutt_endwin (NULL);
384   }
385   else if (subject || msg || sendflags || draftFile || includeFile || attach
386            || optind < argc) {
387     FILE *fin = NULL;
388     char buf[LONG_STRING];
389     char *tempfile = NULL, *infile = NULL;
390     char *bodytext = NULL;
391     FILE *fout;
392
393     if (!option (OPTNOCURSES))
394       mutt_flushinp ();
395
396     if (!msg)
397       msg = header_new();
398
399     if (draftFile)
400       infile = draftFile;
401     else {
402       if (!msg->env)
403         msg->env = envelope_new();
404
405       for (i = optind; i < argc; i++) {
406         if (url_check_scheme (argv[i]) == U_MAILTO)
407           url_parse_mailto (msg->env, &bodytext, argv[i]);
408         else
409           msg->env->to = rfc822_parse_adrlist (msg->env->to, argv[i]);
410       }
411
412       if (option (OPTAUTOEDIT) && !msg->env->to && !msg->env->cc) {
413         if (!option (OPTNOCURSES))
414           mutt_endwin (NULL);
415         fputs (_("No recipients specified.\n"), stderr);
416         exit (1);
417       }
418
419       if (subject)
420         msg->env->subject = m_strdup(subject);
421
422       if (includeFile)
423         infile = includeFile;
424     }
425
426     if (infile || bodytext) {
427       if (infile) {
428         if (m_strcmp("-", infile) == 0)
429           fin = stdin;
430         else {
431           char path[_POSIX_PATH_MAX];
432
433           m_strcpy(path, sizeof(path), infile);
434           mutt_expand_path (path, sizeof (path));
435           if ((fin = fopen (path, "r")) == NULL) {
436             if (!option (OPTNOCURSES))
437               mutt_endwin (NULL);
438             perror (path);
439             exit (1);
440           }
441         }
442       }
443       else
444         fin = NULL;
445
446       if (draftFile)
447         msg->env = mutt_read_rfc822_header (fin, NULL, 1, 0);
448
449       /* is the following if still needed? */
450
451       fout = m_tempfile(buf, sizeof(buf), NONULL(mod_core.tmpdir), NULL);
452       tempfile = m_strdup(buf);
453
454       if (tempfile) {
455         if (!fout) {
456           if (!option (OPTNOCURSES))
457             mutt_endwin (NULL);
458           perror (tempfile);
459           m_fclose(&fin);
460           p_delete(&tempfile);
461           exit (1);
462         }
463         if (fin)
464           mutt_copy_stream (fin, fout);
465         else if (bodytext)
466           fputs (bodytext, fout);
467         m_fclose(&fout);
468         if (fin && fin != stdin)
469           m_fclose(&fin);
470       }
471     }
472
473     p_delete(&bodytext);
474
475     if (attach) {
476       string_list_t *t = attach;
477       BODY *a = NULL;
478
479       while (t) {
480         if (a) {
481           a->next = mutt_make_file_attach (t->data);
482           a = a->next;
483         }
484         else
485           msg->content = a = mutt_make_file_attach (t->data);
486         if (!a) {
487           if (!option (OPTNOCURSES))
488             mutt_endwin (NULL);
489           fprintf (stderr, _("%s: unable to attach file.\n"), t->data);
490           string_list_wipe(&attach);
491           exit (1);
492         }
493         t = t->next;
494       }
495       string_list_wipe(&attach);
496     }
497
498     ci_send_message (sendflags, msg, tempfile, NULL, NULL);
499
500     if (!option (OPTNOCURSES))
501       mutt_endwin (NULL);
502   }
503   else {
504     if (flags & M_BUFFY) {
505       if (!buffy_check (0)) {
506         mutt_endwin _("No mailbox with new mail.");
507
508         exit (1);
509       }
510       folder[0] = 0;
511       buffy_next (folder, sizeof (folder));
512     }
513     else if (flags & M_SELECT) {
514 #ifdef USE_NNTP
515       if (flags & M_NEWS) {
516         set_option (OPTNEWS);
517         if (!(CurrentNewsSrv = mutt_select_newsserver (NewsServer))) {
518           mutt_endwin (Errorbuf);
519           exit (1);
520         }
521       } else
522 #endif
523       if (!Incoming.len) {
524         mutt_endwin _("No incoming mailboxes defined.");
525
526         exit (1);
527       }
528       folder[0] = 0;
529       mutt_select_file(folder, sizeof(folder), M_SEL_FOLDER | M_SEL_BUFFY,
530                        NULL, NULL);
531       if (!folder[0]) {
532         mutt_endwin (NULL);
533         exit (0);
534       }
535     }
536
537     if (!folder[0])
538       m_strcpy(folder, sizeof(folder), NONULL(Spoolfile));
539
540 #ifdef USE_NNTP
541     if (option (OPTNEWS)) {
542       unset_option (OPTNEWS);
543       nntp_expand_path (folder, sizeof (folder),
544                         &CurrentNewsSrv->conn->account);
545     } else
546 #endif
547       mutt_expand_path (folder, sizeof (folder));
548
549     m_strreplace(&CurrentFolder, folder);
550     m_strreplace(&LastFolder, folder);
551
552     if (flags & M_IGNORE) {
553       /* check to see if there are any messages in the folder */
554       switch (mx_check_empty (folder)) {
555       case -1:
556         mutt_endwin (strerror (errno));
557         exit (1);
558       case 1:
559         mutt_endwin _("Mailbox is empty.");
560         exit (1);
561       }
562     }
563
564     mutt_folder_hook (folder);
565
566     if ((Context = mx_open_mailbox(folder, (flags & M_RO) ?  M_READONLY : 0,
567                                    NULL)) || !explicit_folder)
568     {
569       mutt_index_menu ();
570       if (option (OPTXTERMSETTITLES))
571         printf("\033]2;%s\007", NONULL(XtermLeave));
572       if (Context)
573         p_delete(&Context);
574     }
575     mutt_endwin (Errorbuf);
576   }
577
578   luaM_shutdown();
579   mutt_sasl_shutdown();
580   exit (0);
581 }