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