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