c6686ae0612aa92b9c5a3935f33d83ebfba7ffae
[apps/madmutt.git] / hook.c
1 /*
2  * Copyright notice from original mutt:
3  * Copyright (C) 1996-2002 Michael R. Elkins <me@mutt.org>, and others
4  *
5  * This file is part of mutt-ng, see http://www.muttng.org/.
6  * It's licensed under the GNU General Public License,
7  * please see the file GPL in the top level source directory.
8  */
9
10 #if HAVE_CONFIG_H
11 # include "config.h"
12 #endif
13
14 #include <lib-lib/mem.h>
15 #include <lib-lib/str.h>
16 #include <lib-lib/macros.h>
17 #include <lib-lib/buffer.h>
18 #include <lib-lib/file.h>
19
20 #include "mutt.h"
21 #include "mx.h"
22 #include "mutt_crypt.h"
23
24 #ifdef USE_COMPRESSED
25 #include "compress.h"
26 #endif
27
28 #include "lib/rx.h"
29
30 #include <limits.h>
31 #include <string.h>
32 #include <stdlib.h>
33 #include <ctype.h>
34 #include <unistd.h>
35
36 #define ERROR_STOP      0
37
38 typedef struct hook {
39   int type;                     /* hook type */
40   rx_t rx;                      /* regular expression */
41   char *command;                /* filename, command or pattern to execute */
42   pattern_t *pattern;           /* used for fcc,save,send-hook */
43   struct hook *next;
44 } HOOK;
45
46 static HOOK *Hooks = NULL;
47
48 static int current_hook_type = 0;
49
50 int mutt_parse_hook (BUFFER * buf, BUFFER * s, unsigned long data,
51                      BUFFER * err)
52 {
53   HOOK *ptr;
54   BUFFER command, pattern;
55   int rc, not = 0;
56   regex_t *rx = NULL;
57   pattern_t *pat = NULL;
58   char path[_POSIX_PATH_MAX];
59
60   p_clear(&pattern, 1);
61   p_clear(&command, 1);
62
63   if (*s->dptr == '!') {
64     s->dptr = vskipspaces(s->dptr + 1);
65     not = 1;
66   }
67
68   mutt_extract_token (&pattern, s, 0);
69
70   if (!MoreArgs (s)) {
71     m_strcpy(err->data, err->dsize, _("too few arguments"));
72     goto error;
73   }
74
75   mutt_extract_token (&command, s,
76                       (data &
77                        (M_FOLDERHOOK | M_SENDHOOK | M_SEND2HOOK |
78                         M_ACCOUNTHOOK | M_REPLYHOOK)) ? M_TOKEN_SPACE : 0);
79
80   if (!command.data) {
81     m_strcpy(err->data, err->dsize, _("too few arguments"));
82     goto error;
83   }
84
85   if (MoreArgs (s)) {
86     m_strcpy(err->data, err->dsize, _("too many arguments"));
87     goto error;
88   }
89
90   if (data & (M_FOLDERHOOK | M_MBOXHOOK)) {
91     m_strcpy(path, sizeof(path), pattern.data);
92     _mutt_expand_path (path, sizeof (path), 1);
93     p_delete(&pattern.data);
94     p_clear(&pattern, 1);
95     pattern.data = m_strdup(path);
96   }
97 #ifdef USE_COMPRESSED
98   else if (data & (M_APPENDHOOK | M_OPENHOOK | M_CLOSEHOOK)) {
99     if (mutt_test_compress_command (command.data)) {
100       m_strcpy(err->data, err->dsize, _("bad formatted command string"));
101       return (-1);
102     }
103   }
104 #endif
105   else if (DefaultHook && !(data & (M_CHARSETHOOK | M_ACCOUNTHOOK))
106            && (!WithCrypto || !(data & M_CRYPTHOOK))
107     ) {
108     char tmp[HUGE_STRING];
109
110     m_strcpy(tmp, sizeof(tmp), pattern.data);
111     mutt_check_simple (tmp, sizeof (tmp), DefaultHook);
112     p_delete(&pattern.data);
113     p_clear(&pattern, 1);
114     pattern.data = m_strdup(tmp);
115   }
116
117   if (data & (M_MBOXHOOK | M_SAVEHOOK | M_FCCHOOK)) {
118     m_strcpy(path, sizeof(path), command.data);
119     mutt_expand_path (path, sizeof (path));
120     p_delete(&command.data);
121     p_clear(&command, 1);
122     command.data = m_strdup(path);
123   }
124
125   /* check to make sure that a matching hook doesn't already exist */
126   for (ptr = Hooks; ptr; ptr = ptr->next) {
127     if (ptr->type == data &&
128         ptr->rx.not == not && !m_strcmp(pattern.data, ptr->rx.pattern)) {
129       if (data &
130           (M_FOLDERHOOK | M_SENDHOOK | M_SEND2HOOK | M_MESSAGEHOOK |
131            M_ACCOUNTHOOK | M_REPLYHOOK)) {
132         /* these hooks allow multiple commands with the same
133          * pattern, so if we've already seen this pattern/command pair, just
134          * ignore it instead of creating a duplicate */
135         if (!m_strcmp(ptr->command, command.data)) {
136           p_delete(&command.data);
137           p_delete(&pattern.data);
138           return 0;
139         }
140       }
141       else {
142         /* other hooks only allow one command per pattern, so update the
143          * entry with the new command.  this currently does not change the
144          * order of execution of the hooks, which i think is desirable since
145          * a common action to perform is to change the default (.) entry
146          * based upon some other information. */
147         p_delete(&ptr->command);
148         ptr->command = command.data;
149         p_delete(&pattern.data);
150         return 0;
151       }
152     }
153     if (!ptr->next)
154       break;
155   }
156
157   if (data &
158       (M_SENDHOOK | M_SEND2HOOK | M_SAVEHOOK | M_FCCHOOK | M_MESSAGEHOOK |
159        M_REPLYHOOK)) {
160     if ((pat =
161          mutt_pattern_comp (pattern.data,
162                             (data & (M_SENDHOOK | M_SEND2HOOK | M_FCCHOOK)) ?
163                             0 : M_FULL_MSG, err)) == NULL)
164       goto error;
165   }
166   else {
167     rx = p_new(regex_t, 1);
168 #ifdef M_CRYPTHOOK
169     if ((rc =
170          REGCOMP (rx, NONULL (pattern.data),
171                   ((data & (M_CRYPTHOOK | M_CHARSETHOOK)) ? REG_ICASE : 0)))
172         != 0)
173 #else
174     if ((rc =
175          REGCOMP (rx, NONULL (pattern.data),
176                   (data & (M_CHARSETHOOK | M_ICONVHOOK)) ? REG_ICASE : 0)) !=
177         0)
178 #endif /* M_CRYPTHOOK */
179     {
180       regerror (rc, rx, err->data, err->dsize);
181       regfree (rx);
182       p_delete(&rx);
183       goto error;
184     }
185   }
186
187   if (ptr) {
188     ptr->next = p_new(HOOK, 1);
189     ptr = ptr->next;
190   }
191   else
192     Hooks = ptr = p_new(HOOK, 1);
193   ptr->type = data;
194   ptr->command = command.data;
195   ptr->pattern = pat;
196   ptr->rx.pattern = pattern.data;
197   ptr->rx.rx = rx;
198   ptr->rx.not = not;
199   return 0;
200
201 error:
202   p_delete(&pattern.data);
203   p_delete(&command.data);
204   return (-1);
205 }
206
207 static void delete_hook (HOOK * h)
208 {
209   p_delete(&h->command);
210   p_delete(&h->rx.pattern);
211   if (h->rx.rx) {
212     regfree (h->rx.rx);
213   }
214   mutt_pattern_free (&h->pattern);
215   p_delete(&h);
216 }
217
218 /* Deletes all hooks of type ``type'', or all defined hooks if ``type'' is 0 */
219 static void delete_hooks (int type)
220 {
221   HOOK *h;
222   HOOK *prev;
223
224   while (h = Hooks, h && (type == 0 || type == h->type)) {
225     Hooks = h->next;
226     delete_hook (h);
227   }
228
229   prev = h;                     /* Unused assignment to avoid compiler warnings */
230
231   while (h) {
232     if (type == h->type) {
233       prev->next = h->next;
234       delete_hook (h);
235     }
236     else
237       prev = h;
238     h = prev->next;
239   }
240 }
241
242 int mutt_parse_unhook (BUFFER * buf, BUFFER * s, unsigned long data,
243                        BUFFER * err)
244 {
245   while (MoreArgs (s)) {
246     mutt_extract_token (buf, s, 0);
247     if (m_strcmp("*", buf->data) == 0) {
248       if (current_hook_type) {
249         snprintf (err->data, err->dsize,
250                   _("unhook: Can't do unhook * from within a hook."));
251         return -1;
252       }
253       delete_hooks (0);
254     }
255     else {
256       int type = mutt_get_hook_type (buf->data);
257
258       if (!type) {
259         snprintf (err->data, err->dsize,
260                   _("unhook: unknown hook type: %s"), buf->data);
261         return (-1);
262       }
263       if (current_hook_type == type) {
264         snprintf (err->data, err->dsize,
265                   _("unhook: Can't delete a %s from within a %s."), buf->data,
266                   buf->data);
267         return -1;
268       }
269       delete_hooks (type);
270     }
271   }
272   return 0;
273 }
274
275 void mutt_folder_hook (char *path)
276 {
277   HOOK *tmp = Hooks;
278   BUFFER err, token;
279   char buf[STRING];
280
281   current_hook_type = M_FOLDERHOOK;
282
283   err.data = buf;
284   err.dsize = sizeof (buf);
285   p_clear(&token, 1);
286   for (; tmp; tmp = tmp->next) {
287     if (!tmp->command)
288       continue;
289
290     if (tmp->type & M_FOLDERHOOK) {
291       if ((regexec (tmp->rx.rx, path, 0, NULL, 0) == 0) ^ tmp->rx.not) {
292         if (mutt_parse_rc_line (tmp->command, &token, &err) == -1) {
293           mutt_error ("%s", err.data);
294           mutt_sleep (1);       /* pause a moment to let the user see the error */
295           if (ERROR_STOP) {
296             p_delete(&token.data);
297             current_hook_type = 0;
298             return;
299           }
300         }
301       }
302     }
303   }
304   p_delete(&token.data);
305
306   current_hook_type = 0;
307 }
308
309 char *mutt_find_hook (int type, const char *pat)
310 {
311   HOOK *tmp = Hooks;
312
313   for (; tmp; tmp = tmp->next)
314     if (tmp->type & type) {
315       if (regexec (tmp->rx.rx, pat, 0, NULL, 0) == 0)
316         return (tmp->command);
317     }
318   return (NULL);
319 }
320
321 void mutt_message_hook (CONTEXT * ctx, HEADER * hdr, int type)
322 {
323   BUFFER err, token;
324   HOOK *hook;
325   char buf[STRING];
326
327   current_hook_type = type;
328
329   err.data = buf;
330   err.dsize = sizeof (buf);
331   p_clear(&token, 1);
332   for (hook = Hooks; hook; hook = hook->next) {
333     if (!hook->command)
334       continue;
335
336     if (hook->type & type)
337       if ((mutt_pattern_exec (hook->pattern, 0, ctx, hdr) > 0) ^ hook->rx.not)
338         if (mutt_parse_rc_line (hook->command, &token, &err) != 0) {
339           mutt_error ("%s", err.data);
340           mutt_sleep (1);
341           if (ERROR_STOP) {
342             p_delete(&token.data);
343             current_hook_type = 0;
344             return;
345           }
346         }
347   }
348   p_delete(&token.data);
349   current_hook_type = 0;
350 }
351
352 static int
353 mutt_addr_hook (char *path, size_t pathlen, int type, CONTEXT * ctx,
354                 HEADER * hdr)
355 {
356   HOOK *hook;
357
358   /* determine if a matching hook exists */
359   for (hook = Hooks; hook; hook = hook->next) {
360     if (!hook->command)
361       continue;
362
363     if (hook->type & type)
364       if ((mutt_pattern_exec (hook->pattern, 0, ctx, hdr) > 0) ^ hook->rx.not) {
365         mutt_make_string (path, pathlen, hook->command, ctx, hdr);
366         return 0;
367       }
368   }
369
370   return -1;
371 }
372
373 void mutt_default_save (char *path, size_t pathlen, HEADER * hdr)
374 {
375   *path = 0;
376   if (mutt_addr_hook (path, pathlen, M_SAVEHOOK, Context, hdr) != 0) {
377     char tmp[_POSIX_PATH_MAX];
378     ADDRESS *adr;
379     ENVELOPE *env = hdr->env;
380     int fromMe = mutt_addr_is_user (env->from);
381
382     if (!fromMe && env->reply_to && env->reply_to->mailbox)
383       adr = env->reply_to;
384     else if (!fromMe && env->from && env->from->mailbox)
385       adr = env->from;
386     else if (env->to && env->to->mailbox)
387       adr = env->to;
388     else if (env->cc && env->cc->mailbox)
389       adr = env->cc;
390     else
391       adr = NULL;
392     if (adr) {
393       mutt_safe_path (tmp, sizeof (tmp), adr);
394       snprintf (path, pathlen, "=%s", tmp);
395     }
396   }
397 }
398
399 void mutt_select_fcc (char *path, size_t pathlen, HEADER * hdr)
400 {
401   ADDRESS *adr;
402   char buf[_POSIX_PATH_MAX];
403   ENVELOPE *env = hdr->env;
404
405   if (mutt_addr_hook (path, pathlen, M_FCCHOOK, NULL, hdr) != 0) {
406     if ((option (OPTSAVENAME) || option (OPTFORCENAME)) &&
407         (env->to || env->cc || env->bcc)) {
408       adr = env->to ? env->to : (env->cc ? env->cc : env->bcc);
409       mutt_safe_path (buf, sizeof (buf), adr);
410       mutt_concat_path(path, pathlen, NONULL(Maildir), buf);
411       if (!option (OPTFORCENAME) && mx_access (path, W_OK) != 0)
412         m_strcpy(path, pathlen, NONULL(Outbox));
413     }
414     else
415       m_strcpy(path, pathlen, NONULL(Outbox));
416   }
417   mutt_pretty_mailbox (path);
418 }
419
420 static char *_mutt_string_hook (const char *match, int hook)
421 {
422   HOOK *tmp = Hooks;
423
424   for (; tmp; tmp = tmp->next) {
425     if ((tmp->type & hook) && ((match &&
426                                 regexec (tmp->rx.rx, match, 0, NULL,
427                                          0) == 0) ^ tmp->rx.not))
428       return (tmp->command);
429   }
430   return (NULL);
431 }
432
433 char *mutt_charset_hook (const char *chs)
434 {
435   return _mutt_string_hook (chs, M_CHARSETHOOK);
436 }
437
438 char *mutt_iconv_hook (const char *chs)
439 {
440   return _mutt_string_hook (chs, M_ICONVHOOK);
441 }
442
443 char *mutt_crypt_hook (ADDRESS * adr)
444 {
445   return _mutt_string_hook (adr->mailbox, M_CRYPTHOOK);
446 }
447
448 #ifdef USE_SOCKET
449 void mutt_account_hook (const char *url)
450 {
451   HOOK *hook;
452   BUFFER token;
453   BUFFER err;
454   char buf[STRING];
455
456   err.data = buf;
457   err.dsize = sizeof (buf);
458   p_clear(&token, 1);
459
460   for (hook = Hooks; hook; hook = hook->next) {
461     if (!(hook->command && (hook->type & M_ACCOUNTHOOK)))
462       continue;
463
464     if ((regexec (hook->rx.rx, url, 0, NULL, 0) == 0) ^ hook->rx.not) {
465       if (mutt_parse_rc_line (hook->command, &token, &err) == -1) {
466         mutt_error ("%s", err.data);
467         mutt_sleep (1);
468         if (ERROR_STOP) {
469           p_delete(&token.data);
470           current_hook_type = 0;
471           return;
472         }
473       }
474     }
475   }
476
477   p_delete(&token.data);
478 }
479 #endif