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