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