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