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