Getting rid of useless settings.
[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
16 #include "alias.h"
17 #include "crypt.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, (data & (M_FOLDERHOOK | M_SENDHOOK |
66                                            M_SEND2HOOK | M_REPLYHOOK)) ?
67                      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_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 & (M_FOLDERHOOK | M_SENDHOOK | M_SEND2HOOK | M_MESSAGEHOOK |
115            M_REPLYHOOK))
116       {
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       } else {
126         /* other hooks only allow one command per pattern, so update the
127          * entry with the new command.  this currently does not change the
128          * order of execution of the hooks, which i think is desirable since
129          * a common action to perform is to change the default (.) entry
130          * based upon some other information. */
131         p_delete(&ptr->command);
132         ptr->command = command.data;
133         p_delete(&pattern.data);
134         return 0;
135       }
136     }
137     if (!ptr->next)
138       break;
139   }
140
141   if (data & (M_SENDHOOK | M_SEND2HOOK | M_SAVEHOOK | M_FCCHOOK |
142               M_MESSAGEHOOK | M_REPLYHOOK))
143   {
144     if ((pat =
145          mutt_pattern_comp (pattern.data,
146                             (data & (M_SENDHOOK | M_SEND2HOOK | M_FCCHOOK)) ?
147                             0 : M_FULL_MSG, err)) == NULL)
148       goto error;
149   } else {
150     rx = p_new(regex_t, 1);
151     if ((rc = REGCOMP(rx, NONULL(pattern.data),
152                   ((data & M_CRYPTHOOK) ? REG_ICASE : 0))) != 0)
153     {
154       regerror (rc, rx, err->data, err->dsize);
155       regfree (rx);
156       p_delete(&rx);
157       goto error;
158     }
159   }
160
161   if (ptr) {
162     ptr->next = p_new(hook_t, 1);
163     ptr = ptr->next;
164   } else {
165     Hooks = ptr = p_new(hook_t, 1);
166   }
167   ptr->type = data;
168   ptr->command = command.data;
169   ptr->pattern = pat;
170   ptr->rx.pattern = pattern.data;
171   ptr->rx.rx = rx;
172   ptr->rx.neg = neg;
173   return 0;
174
175 error:
176   p_delete(&pattern.data);
177   p_delete(&command.data);
178   return (-1);
179 }
180
181 /* Deletes all hooks of type ``type'', or all defined hooks if ``type'' is 0 */
182 static void delete_hooks (long type)
183 {
184     hook_t **l = &Hooks;
185
186     while (*l) {
187         if ((*l)->type == type) {
188             hook_t *tmp = hook_list_pop(l);
189             hook_delete(&tmp);
190         } else {
191             l = &(*l)->next;
192         }
193     }
194 }
195
196 int mutt_parse_unhook (BUFFER * buf, BUFFER * s, unsigned long data __attribute__ ((unused)),
197                        BUFFER * err)
198 {
199   while (MoreArgs (s)) {
200     mutt_extract_token (buf, s, 0);
201     if (m_strcmp("*", buf->data) == 0) {
202       if (current_hook_type) {
203         snprintf (err->data, err->dsize,
204                   _("unhook: Can't do unhook * from within a hook."));
205         return -1;
206       }
207       hook_list_wipe(&Hooks);
208     } else {
209       unsigned long type = mutt_get_hook_type (buf->data);
210
211       if (!type) {
212         snprintf (err->data, err->dsize,
213                   _("unhook: unknown hook type: %s"), buf->data);
214         return (-1);
215       }
216       if (current_hook_type == type) {
217         snprintf (err->data, err->dsize,
218                   _("unhook: Can't delete a %s from within a %s."), buf->data,
219                   buf->data);
220         return -1;
221       }
222       delete_hooks (type);
223     }
224   }
225   return 0;
226 }
227
228 void mutt_folder_hook (char *path)
229 {
230   hook_t *tmp = Hooks;
231   BUFFER err, token;
232   char buf[STRING];
233
234   current_hook_type = M_FOLDERHOOK;
235
236   err.data = buf;
237   err.dsize = sizeof (buf);
238   p_clear(&token, 1);
239   for (; tmp; tmp = tmp->next) {
240     if (!tmp->command)
241       continue;
242
243     if (tmp->type & M_FOLDERHOOK) {
244       if ((regexec (tmp->rx.rx, path, 0, NULL, 0) == 0) ^ tmp->rx.neg) {
245         if (mutt_parse_rc_line (tmp->command, &token, &err) == -1) {
246           mutt_error ("%s", err.data);
247           mutt_sleep (1);       /* pause a moment to let the user see the error */
248         }
249       }
250     }
251   }
252   p_delete(&token.data);
253
254   current_hook_type = 0;
255
256     {
257         lua_State *L = luaM_getruntime();
258         lua_getfield(L, LUA_GLOBALSINDEX, "mod_core");
259         lua_getfield(L, -1, "folder_hook");
260         lua_remove(L, -2);
261         if (lua_isfunction(L, -1)) {
262             lua_pushstring(L, LastFolder);
263             lua_pushstring(L, CurrentFolder);
264             lua_pcall(L, 2, 0, 0);
265         } else {
266             lua_pop(L, 1);
267         }
268     }
269 }
270
271 char *mutt_find_hook (int type, const char *pat)
272 {
273   hook_t *tmp = Hooks;
274
275   for (; tmp; tmp = tmp->next)
276     if (tmp->type & type) {
277       if (regexec (tmp->rx.rx, pat, 0, NULL, 0) == 0)
278         return (tmp->command);
279     }
280   return (NULL);
281 }
282
283 void mutt_message_hook (CONTEXT * ctx, HEADER * hdr, int type)
284 {
285   BUFFER err, token;
286   hook_t *hook;
287   char buf[STRING];
288
289   current_hook_type = type;
290
291   err.data = buf;
292   err.dsize = sizeof (buf);
293   p_clear(&token, 1);
294   for (hook = Hooks; hook; hook = hook->next) {
295     if (!hook->command)
296       continue;
297
298     if (hook->type & type)
299       if ((mutt_pattern_exec (hook->pattern, 0, ctx, hdr) > 0) ^ hook->rx.neg)
300         if (mutt_parse_rc_line (hook->command, &token, &err) != 0) {
301           mutt_error ("%s", err.data);
302           mutt_sleep (1);
303         }
304   }
305   p_delete(&token.data);
306   current_hook_type = 0;
307 }
308
309 static int
310 mutt_addr_hook (char *path, ssize_t pathlen, unsigned long type, CONTEXT * ctx,
311                 HEADER * hdr)
312 {
313   hook_t *hook;
314
315   /* determine if a matching hook exists */
316   for (hook = Hooks; hook; hook = hook->next) {
317     if (!hook->command)
318       continue;
319
320     if (hook->type & type)
321       if ((mutt_pattern_exec (hook->pattern, 0, ctx, hdr) > 0) ^ hook->rx.neg) {
322         mutt_make_string (path, pathlen, hook->command, ctx, hdr);
323         return 0;
324       }
325   }
326
327   return -1;
328 }
329
330 void mutt_default_save (char *path, ssize_t pathlen, HEADER * hdr)
331 {
332   *path = 0;
333   if (mutt_addr_hook (path, pathlen, M_SAVEHOOK, Context, hdr) != 0) {
334     char tmp[_POSIX_PATH_MAX];
335     address_t *adr;
336     ENVELOPE *env = hdr->env;
337     int fromMe = mutt_addr_is_user (env->from);
338
339     if (!fromMe && env->reply_to && env->reply_to->mailbox)
340       adr = env->reply_to;
341     else if (!fromMe && env->from && env->from->mailbox)
342       adr = env->from;
343     else if (env->to && env->to->mailbox)
344       adr = env->to;
345     else if (env->cc && env->cc->mailbox)
346       adr = env->cc;
347     else
348       adr = NULL;
349     if (adr) {
350       mutt_safe_path (tmp, sizeof (tmp), adr);
351       snprintf (path, pathlen, "=%s", tmp);
352     }
353   }
354 }
355
356 void mutt_select_fcc (char *path, ssize_t pathlen, HEADER * hdr)
357 {
358     if (mutt_addr_hook (path, pathlen, M_FCCHOOK, NULL, hdr) != 0) {
359         m_strcpy(path, pathlen, NONULL(MAlias.record));
360     }
361     mutt_pretty_mailbox (path);
362 }
363
364 static const char *_mutt_string_hook (const char *match, int hook)
365 {
366   hook_t *tmp = Hooks;
367
368   for (; tmp; tmp = tmp->next) {
369     if ((tmp->type & hook)
370     && ((match && regexec(tmp->rx.rx, match, 0, NULL, 0) == 0) ^ tmp->rx.neg))
371       return (tmp->command);
372   }
373   return (NULL);
374 }
375
376 const char *mutt_crypt_hook (address_t * adr)
377 {
378   return _mutt_string_hook (adr->mailbox, M_CRYPTHOOK);
379 }