Make MAlias public. migrate $signature, so that I can test the new MCore.folder_hook...
[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-lua/lib-lua.h>
12 #include <lib-ui/curses.h>
13 #include <lib-mx/mx.h>
14 #include <lib-mx/compress.h>
15 #include <lib-crypt/crypt.h>
16
17 #include "alias.h"
18 #include "pattern.h"
19
20 typedef struct hook_t {
21   int type;                     /* hook type */
22   rx_t rx;                      /* regular expression */
23   char *command;                /* filename, command or pattern to execute */
24   pattern_t *pattern;           /* used for fcc,save,send-hook */
25   struct hook_t *next;
26 } hook_t;
27
28 DO_INIT(hook_t, hook);
29 static inline void hook_wipe(hook_t *hk) {
30     p_delete(&hk->command);
31     pattern_list_wipe(&hk->pattern);
32 }
33 DO_NEW(hook_t, hook);
34 DO_DELETE(hook_t, hook);
35 DO_SLIST(hook_t, hook, hook_delete);
36
37 static hook_t *Hooks = NULL;
38 static unsigned long current_hook_type = 0;
39
40 int mutt_parse_hook (BUFFER * buf __attribute__ ((unused)), BUFFER * s,
41                      unsigned long data, BUFFER * err)
42 {
43   hook_t *ptr;
44   BUFFER command, pattern;
45   int rc, neg = 0;
46   regex_t *rx = NULL;
47   pattern_t *pat = NULL;
48   char path[_POSIX_PATH_MAX];
49
50   p_clear(&pattern, 1);
51   p_clear(&command, 1);
52
53   if (*s->dptr == '!') {
54     s->dptr = vskipspaces(s->dptr + 1);
55     neg = 1;
56   }
57
58   mutt_extract_token (&pattern, s, 0);
59
60   if (!MoreArgs (s)) {
61     m_strcpy(err->data, err->dsize, _("too few arguments"));
62     goto error;
63   }
64
65   mutt_extract_token (&command, s,
66                       (data &
67                        (M_FOLDERHOOK | M_SENDHOOK | M_SEND2HOOK |
68                         M_ACCOUNTHOOK | M_REPLYHOOK)) ? M_TOKEN_SPACE : 0);
69
70   if (!command.data) {
71     m_strcpy(err->data, err->dsize, _("too few arguments"));
72     goto error;
73   }
74
75   if (MoreArgs (s)) {
76     m_strcpy(err->data, err->dsize, _("too many arguments"));
77     goto error;
78   }
79
80   if (data & (M_FOLDERHOOK | M_MBOXHOOK)) {
81     _mutt_expand_path (path, sizeof (path), pattern.data, 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_ACCOUNTHOOK) && !(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.neg == neg && !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 = REGCOMP(rx, NONULL(pattern.data),
156                   ((data & M_CRYPTHOOK) ? REG_ICASE : 0))) != 0)
157     {
158       regerror (rc, rx, err->data, err->dsize);
159       regfree (rx);
160       p_delete(&rx);
161       goto error;
162     }
163 #endif /* M_CRYPTHOOK */
164   }
165
166   if (ptr) {
167     ptr->next = p_new(hook_t, 1);
168     ptr = ptr->next;
169   }
170   else
171     Hooks = ptr = p_new(hook_t, 1);
172   ptr->type = data;
173   ptr->command = command.data;
174   ptr->pattern = pat;
175   ptr->rx.pattern = pattern.data;
176   ptr->rx.rx = rx;
177   ptr->rx.neg = neg;
178   return 0;
179
180 error:
181   p_delete(&pattern.data);
182   p_delete(&command.data);
183   return (-1);
184 }
185
186 /* Deletes all hooks of type ``type'', or all defined hooks if ``type'' is 0 */
187 static void delete_hooks (long type)
188 {
189     hook_t **l = &Hooks;
190
191     while (*l) {
192         if ((*l)->type == type) {
193             hook_t *tmp = hook_list_pop(l);
194             hook_delete(&tmp);
195         } else {
196             l = &(*l)->next;
197         }
198     }
199 }
200
201 int mutt_parse_unhook (BUFFER * buf, BUFFER * s, unsigned long data __attribute__ ((unused)),
202                        BUFFER * err)
203 {
204   while (MoreArgs (s)) {
205     mutt_extract_token (buf, s, 0);
206     if (m_strcmp("*", buf->data) == 0) {
207       if (current_hook_type) {
208         snprintf (err->data, err->dsize,
209                   _("unhook: Can't do unhook * from within a hook."));
210         return -1;
211       }
212       hook_list_wipe(&Hooks);
213     } else {
214       unsigned long type = mutt_get_hook_type (buf->data);
215
216       if (!type) {
217         snprintf (err->data, err->dsize,
218                   _("unhook: unknown hook type: %s"), buf->data);
219         return (-1);
220       }
221       if (current_hook_type == type) {
222         snprintf (err->data, err->dsize,
223                   _("unhook: Can't delete a %s from within a %s."), buf->data,
224                   buf->data);
225         return -1;
226       }
227       delete_hooks (type);
228     }
229   }
230   return 0;
231 }
232
233 void mutt_folder_hook (char *path)
234 {
235   hook_t *tmp = Hooks;
236   BUFFER err, token;
237   char buf[STRING];
238
239   current_hook_type = M_FOLDERHOOK;
240
241   err.data = buf;
242   err.dsize = sizeof (buf);
243   p_clear(&token, 1);
244   for (; tmp; tmp = tmp->next) {
245     if (!tmp->command)
246       continue;
247
248     if (tmp->type & M_FOLDERHOOK) {
249       if ((regexec (tmp->rx.rx, path, 0, NULL, 0) == 0) ^ tmp->rx.neg) {
250         if (mutt_parse_rc_line (tmp->command, &token, &err) == -1) {
251           mutt_error ("%s", err.data);
252           mutt_sleep (1);       /* pause a moment to let the user see the error */
253         }
254       }
255     }
256   }
257   p_delete(&token.data);
258
259   current_hook_type = 0;
260
261     {
262         lua_State *L = luaM_getruntime();
263         lua_getfield(L, LUA_GLOBALSINDEX, "MCore");
264         lua_getfield(L, -1, "folder_hook");
265         lua_remove(L, -2);
266         if (lua_isfunction(L, -1)) {
267             lua_pushstring(L, LastFolder);
268             lua_pushstring(L, CurrentFolder);
269             lua_pcall(L, 2, 0, 0);
270         } else {
271             lua_pop(L, 1);
272         }
273     }
274 }
275
276 char *mutt_find_hook (int type, const char *pat)
277 {
278   hook_t *tmp = Hooks;
279
280   for (; tmp; tmp = tmp->next)
281     if (tmp->type & type) {
282       if (regexec (tmp->rx.rx, pat, 0, NULL, 0) == 0)
283         return (tmp->command);
284     }
285   return (NULL);
286 }
287
288 void mutt_message_hook (CONTEXT * ctx, HEADER * hdr, int type)
289 {
290   BUFFER err, token;
291   hook_t *hook;
292   char buf[STRING];
293
294   current_hook_type = type;
295
296   err.data = buf;
297   err.dsize = sizeof (buf);
298   p_clear(&token, 1);
299   for (hook = Hooks; hook; hook = hook->next) {
300     if (!hook->command)
301       continue;
302
303     if (hook->type & type)
304       if ((mutt_pattern_exec (hook->pattern, 0, ctx, hdr) > 0) ^ hook->rx.neg)
305         if (mutt_parse_rc_line (hook->command, &token, &err) != 0) {
306           mutt_error ("%s", err.data);
307           mutt_sleep (1);
308         }
309   }
310   p_delete(&token.data);
311   current_hook_type = 0;
312 }
313
314 static int
315 mutt_addr_hook (char *path, ssize_t pathlen, unsigned long type, CONTEXT * ctx,
316                 HEADER * hdr)
317 {
318   hook_t *hook;
319
320   /* determine if a matching hook exists */
321   for (hook = Hooks; hook; hook = hook->next) {
322     if (!hook->command)
323       continue;
324
325     if (hook->type & type)
326       if ((mutt_pattern_exec (hook->pattern, 0, ctx, hdr) > 0) ^ hook->rx.neg) {
327         mutt_make_string (path, pathlen, hook->command, ctx, hdr);
328         return 0;
329       }
330   }
331
332   return -1;
333 }
334
335 void mutt_default_save (char *path, ssize_t pathlen, HEADER * hdr)
336 {
337   *path = 0;
338   if (mutt_addr_hook (path, pathlen, M_SAVEHOOK, Context, hdr) != 0) {
339     char tmp[_POSIX_PATH_MAX];
340     address_t *adr;
341     ENVELOPE *env = hdr->env;
342     int fromMe = mutt_addr_is_user (env->from);
343
344     if (!fromMe && env->reply_to && env->reply_to->mailbox)
345       adr = env->reply_to;
346     else if (!fromMe && env->from && env->from->mailbox)
347       adr = env->from;
348     else if (env->to && env->to->mailbox)
349       adr = env->to;
350     else if (env->cc && env->cc->mailbox)
351       adr = env->cc;
352     else
353       adr = NULL;
354     if (adr) {
355       mutt_safe_path (tmp, sizeof (tmp), adr);
356       snprintf (path, pathlen, "=%s", tmp);
357     }
358   }
359 }
360
361 void mutt_select_fcc (char *path, ssize_t pathlen, HEADER * hdr)
362 {
363   address_t *adr;
364   char buf[_POSIX_PATH_MAX];
365   ENVELOPE *env = hdr->env;
366
367   if (mutt_addr_hook (path, pathlen, M_FCCHOOK, NULL, hdr) != 0) {
368     if ((option (OPTSAVENAME) || option (OPTFORCENAME)) &&
369         (env->to || env->cc || env->bcc)) {
370       adr = env->to ? env->to : (env->cc ? env->cc : env->bcc);
371       mutt_safe_path (buf, sizeof (buf), adr);
372       mutt_concat_path(path, pathlen, NONULL(Maildir), buf);
373       if (!option (OPTFORCENAME) && mx_access (path, W_OK) != 0)
374         m_strcpy(path, pathlen, NONULL(Outbox));
375     }
376     else
377       m_strcpy(path, pathlen, NONULL(Outbox));
378   }
379   mutt_pretty_mailbox (path);
380 }
381
382 static const char *_mutt_string_hook (const char *match, int hook)
383 {
384   hook_t *tmp = Hooks;
385
386   for (; tmp; tmp = tmp->next) {
387     if ((tmp->type & hook)
388     && ((match && regexec(tmp->rx.rx, match, 0, NULL, 0) == 0) ^ tmp->rx.neg))
389       return (tmp->command);
390   }
391   return (NULL);
392 }
393
394 const char *mutt_crypt_hook (address_t * adr)
395 {
396   return _mutt_string_hook (adr->mailbox, M_CRYPTHOOK);
397 }
398
399 void mutt_account_hook (const char *url)
400 {
401   hook_t *hook;
402   BUFFER token;
403   BUFFER err;
404   char buf[STRING];
405
406   err.data = buf;
407   err.dsize = sizeof (buf);
408   p_clear(&token, 1);
409
410   for (hook = Hooks; hook; hook = hook->next) {
411     if (!(hook->command && (hook->type & M_ACCOUNTHOOK)))
412       continue;
413
414     if ((regexec (hook->rx.rx, url, 0, NULL, 0) == 0) ^ hook->rx.neg) {
415       if (mutt_parse_rc_line (hook->command, &token, &err) == -1) {
416         mutt_error ("%s", err.data);
417         mutt_sleep (1);
418       }
419     }
420   }
421
422   p_delete(&token.data);
423 }