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