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