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