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