Use liblockfile, no need for dotlock anymore.
[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 static void start_curses (void)
184 {
185   km_init ();                   /* must come before mutt_init */
186   mutt_signal_initialize ();
187   if (initscr () == NULL) {
188     puts _("Error initializing terminal.");
189
190     exit (1);
191   }
192   mutt_signal_initialize ();
193   curses_initialize();
194 }
195
196 #define M_IGNORE  (1<<0)        /* -z */
197 #define M_BUFFY   (1<<1)        /* -Z */
198 #define M_NOSYSRC (1<<2)        /* -n */
199 #define M_RO      (1<<3)        /* -R */
200 #define M_SELECT  (1<<4)        /* -y */
201 #ifdef USE_NNTP
202 #define M_NEWS    (1<<5)        /* -g and -G */
203 #endif
204
205 __attribute__((format(printf, 1, 0)))
206 static void mutt_nocurses_error (const char *fmt, ...)
207 {
208     va_list ap;
209
210     va_start(ap, fmt);
211     vfprintf(stderr, fmt, ap);
212     va_end(ap);
213     fputc('\n', stderr);
214 }
215
216 int main (int argc, char **argv)
217 {
218   char folder[_POSIX_PATH_MAX] = "";
219   char *subject = NULL;
220   char *includeFile = NULL;
221   char *draftFile = NULL;
222   HEADER *msg = NULL;
223   string_list_t *attach = NULL;
224   string_list_t *commands = NULL;
225   int sendflags = 0;
226   int flags = 0;
227   int version = 0;
228   int i;
229   int explicit_folder = 0;
230
231   /* initialize random number for tmp file creation */ 
232   srand48((unsigned int) time (NULL));
233   
234   /* sanity check against stupid administrators */
235   
236   if (getegid () != getgid ()) {
237     fprintf (stderr, "%s: I don't want to run with privileges!\n", argv[0]);
238     exit (1);
239   }
240
241   setlocale (LC_ALL, "");
242   bindtextdomain (PACKAGE, MUTTLOCALEDIR);
243   textdomain (PACKAGE);
244   setlocale (LC_CTYPE, "");
245
246   mutt_error = mutt_message = mutt_nocurses_error;
247   srand48 (time (NULL));
248   umask (077);
249
250   while ((i = getopt(argc, argv, "a:b:F:f:c:e:H:s:i:hnpRTtvyzZ"
251 #ifdef USE_NNTP
252                                                                     "g:G"
253 #endif
254                     )) >= 0)
255     switch (i) {
256     case 'a':
257       if (strlen(optarg)<=512)
258         attach = mutt_add_list (attach, optarg);
259       else{
260         printf("too long arguments. exiting ...\n");
261         exit(1);
262       }
263       break;
264
265     case 'F':
266       m_strreplace(&Muttrc, optarg);
267       break;
268
269     case 'f':
270       m_strcpy(folder, sizeof(folder), optarg);
271       explicit_folder = 1;
272       break;
273
274     case 'b':
275     case 'c':
276       if (!msg)
277         msg = header_new();
278       if (!msg->env)
279         msg->env = envelope_new();
280       if (i == 'b')
281         msg->env->bcc = rfc822_parse_adrlist (msg->env->bcc, optarg);
282       else
283         msg->env->cc = rfc822_parse_adrlist (msg->env->cc, optarg);
284       break;
285
286     case 'e':
287       commands = mutt_add_list (commands, optarg);
288       break;
289
290     case 'H':
291       draftFile = optarg;
292       break;
293
294     case 'i':
295       includeFile = optarg;
296       break;
297
298     case 'n':
299       flags |= M_NOSYSRC;
300       break;
301
302     case 'p':
303       sendflags |= SENDPOSTPONED;
304       break;
305
306     case 'R':
307       flags |= M_RO;            /* read-only mode */
308       break;
309
310     case 's':
311       subject = optarg;
312       break;
313
314     case 'v':
315       version++;
316       break;
317
318     case 'y':                  /* My special hack mode */
319       flags |= M_SELECT;
320       break;
321
322 #ifdef USE_NNTP
323     case 'g':                  /* Specify a newsserver */
324       {
325         char buf[LONG_STRING];
326
327         snprintf (buf, sizeof (buf), "set nntp_host=%s", optarg);
328         commands = mutt_add_list (commands, buf);
329       }
330
331     case 'G':                  /* List of newsgroups */
332       flags |= M_SELECT | M_NEWS;
333       break;
334 #endif
335
336     case 'z':
337       flags |= M_IGNORE;
338       break;
339
340     case 'Z':
341       flags |= M_BUFFY | M_IGNORE;
342       break;
343
344     default:
345       mutt_usage ();
346     }
347
348   if (version) {
349     show_version ();
350   }
351
352   /* Check for a batch send. */
353   if (!isatty (0)) {
354     set_option(OPTNOCURSES);
355     sendflags = SENDBATCH;
356   }
357
358   /* This must come before mutt_init() because curses needs to be started
359      before calling the init_pair() function to set the color scheme.  */
360   if (!option (OPTNOCURSES))
361     start_curses();
362
363   /* set defaults and read init files */
364   mutt_init (flags & M_NOSYSRC, commands);
365   string_list_wipe(&commands);
366
367   if (!option(OPTNOCURSES)) {
368       ui_layout_init();
369   }
370
371   /* Create the Maildir directory if it doesn't exist. */
372   if (!option(OPTNOCURSES) && Maildir) {
373     struct stat sb;
374     char fpath[_POSIX_PATH_MAX];
375     char mesg[STRING];
376
377     m_strcpy(fpath, sizeof(fpath), Maildir);
378     mutt_expand_path (fpath, sizeof (fpath));
379     /* we're not connected yet - skip mail folder creation */
380     if (mx_get_magic (fpath) != M_IMAP)
381       if (stat (fpath, &sb) == -1 && errno == ENOENT) {
382         snprintf (mesg, sizeof (mesg), _("%s does not exist. Create it?"),
383                   Maildir);
384         if (mutt_yesorno (mesg, M_YES) == M_YES) {
385           if (mkdir (fpath, 0700) == -1 && errno != EEXIST)
386             mutt_error (_("Can't create %s: %s."), Maildir, strerror (errno));
387         }
388       }
389   }
390
391   if (sendflags & SENDPOSTPONED) {
392     if (!option (OPTNOCURSES))
393       mutt_flushinp ();
394     ci_send_message (SENDPOSTPONED, NULL, NULL, NULL, NULL);
395     mutt_endwin (NULL);
396   }
397   else if (subject || msg || sendflags || draftFile || includeFile || attach
398            || optind < argc) {
399     FILE *fin = NULL;
400     char buf[LONG_STRING];
401     char *tempfile = NULL, *infile = NULL;
402     char *bodytext = NULL;
403     FILE *fout;
404
405     if (!option (OPTNOCURSES))
406       mutt_flushinp ();
407
408     if (!msg)
409       msg = header_new();
410
411     if (draftFile)
412       infile = draftFile;
413     else {
414       if (!msg->env)
415         msg->env = envelope_new();
416
417       for (i = optind; i < argc; i++) {
418         if (url_check_scheme (argv[i]) == U_MAILTO)
419           url_parse_mailto (msg->env, &bodytext, argv[i]);
420         else
421           msg->env->to = rfc822_parse_adrlist (msg->env->to, argv[i]);
422       }
423
424       if (option (OPTAUTOEDIT) && !msg->env->to && !msg->env->cc) {
425         if (!option (OPTNOCURSES))
426           mutt_endwin (NULL);
427         fputs (_("No recipients specified.\n"), stderr);
428         exit (1);
429       }
430
431       if (subject)
432         msg->env->subject = m_strdup(subject);
433
434       if (includeFile)
435         infile = includeFile;
436     }
437
438     if (infile || bodytext) {
439       if (infile) {
440         if (m_strcmp("-", infile) == 0)
441           fin = stdin;
442         else {
443           char path[_POSIX_PATH_MAX];
444
445           m_strcpy(path, sizeof(path), infile);
446           mutt_expand_path (path, sizeof (path));
447           if ((fin = fopen (path, "r")) == NULL) {
448             if (!option (OPTNOCURSES))
449               mutt_endwin (NULL);
450             perror (path);
451             exit (1);
452           }
453         }
454       }
455       else
456         fin = NULL;
457
458       if (draftFile)
459         msg->env = mutt_read_rfc822_header (fin, NULL, 1, 0);
460
461       /* is the following if still needed? */
462
463       fout = m_tempfile(buf, sizeof(buf), NONULL(mod_core.tmpdir), NULL);
464       tempfile = m_strdup(buf);
465
466       if (tempfile) {
467         if (!fout) {
468           if (!option (OPTNOCURSES))
469             mutt_endwin (NULL);
470           perror (tempfile);
471           m_fclose(&fin);
472           p_delete(&tempfile);
473           exit (1);
474         }
475         if (fin)
476           mutt_copy_stream (fin, fout);
477         else if (bodytext)
478           fputs (bodytext, fout);
479         m_fclose(&fout);
480         if (fin && fin != stdin)
481           m_fclose(&fin);
482       }
483     }
484
485     p_delete(&bodytext);
486
487     if (attach) {
488       string_list_t *t = attach;
489       BODY *a = NULL;
490
491       while (t) {
492         if (a) {
493           a->next = mutt_make_file_attach (t->data);
494           a = a->next;
495         }
496         else
497           msg->content = a = mutt_make_file_attach (t->data);
498         if (!a) {
499           if (!option (OPTNOCURSES))
500             mutt_endwin (NULL);
501           fprintf (stderr, _("%s: unable to attach file.\n"), t->data);
502           string_list_wipe(&attach);
503           exit (1);
504         }
505         t = t->next;
506       }
507       string_list_wipe(&attach);
508     }
509
510     ci_send_message (sendflags, msg, tempfile, NULL, NULL);
511
512     if (!option (OPTNOCURSES))
513       mutt_endwin (NULL);
514   }
515   else {
516     if (flags & M_BUFFY) {
517       if (!buffy_check (0)) {
518         mutt_endwin _("No mailbox with new mail.");
519
520         exit (1);
521       }
522       folder[0] = 0;
523       buffy_next (folder, sizeof (folder));
524     }
525     else if (flags & M_SELECT) {
526 #ifdef USE_NNTP
527       if (flags & M_NEWS) {
528         set_option (OPTNEWS);
529         if (!(CurrentNewsSrv = mutt_select_newsserver (NewsServer))) {
530           mutt_endwin (Errorbuf);
531           exit (1);
532         }
533       } else
534 #endif
535       if (!Incoming.len) {
536         mutt_endwin _("No incoming mailboxes defined.");
537
538         exit (1);
539       }
540       folder[0] = 0;
541       mutt_select_file(folder, sizeof(folder), M_SEL_FOLDER | M_SEL_BUFFY,
542                        NULL, NULL);
543       if (!folder[0]) {
544         mutt_endwin (NULL);
545         exit (0);
546       }
547     }
548
549     if (!folder[0])
550       m_strcpy(folder, sizeof(folder), NONULL(Spoolfile));
551
552 #ifdef USE_NNTP
553     if (option (OPTNEWS)) {
554       unset_option (OPTNEWS);
555       nntp_expand_path (folder, sizeof (folder),
556                         &CurrentNewsSrv->conn->account);
557     } else
558 #endif
559       mutt_expand_path (folder, sizeof (folder));
560
561     m_strreplace(&CurrentFolder, folder);
562     m_strreplace(&LastFolder, folder);
563
564     if (flags & M_IGNORE) {
565       /* check to see if there are any messages in the folder */
566       switch (mx_check_empty (folder)) {
567       case -1:
568         mutt_endwin (strerror (errno));
569         exit (1);
570       case 1:
571         mutt_endwin _("Mailbox is empty.");
572         exit (1);
573       }
574     }
575
576     mutt_folder_hook (folder);
577
578     if ((Context = mx_open_mailbox(folder, (flags & M_RO) ?  M_READONLY : 0,
579                                    NULL)) || !explicit_folder)
580     {
581       mutt_index_menu ();
582       if (option (OPTXTERMSETTITLES))
583         printf("\033]2;%s\007", NONULL(XtermLeave));
584       if (Context)
585         p_delete(&Context);
586     }
587     mutt_endwin (Errorbuf);
588   }
589
590   luaM_shutdown();
591   mutt_sasl_shutdown();
592   exit (0);
593 }