Rocco Rutte:
[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 "mailbox.h"
16 #include "mutt_crypt.h"
17
18 #ifdef USE_COMPRESSED
19 #include "compress.h"
20 #endif
21
22 #include "lib/mem.h"
23 #include "lib/intl.h"
24 #include "lib/str.h"
25
26 #include <limits.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #include <ctype.h>
30 #include <unistd.h>
31
32 typedef struct hook {
33   int type;                     /* hook type */
34   REGEXP rx;                    /* regular expression */
35   char *command;                /* filename, command or pattern to execute */
36   pattern_t *pattern;           /* used for fcc,save,send-hook */
37   struct hook *next;
38 } HOOK;
39
40 static HOOK *Hooks = NULL;
41
42 static int current_hook_type = 0;
43
44 int mutt_parse_hook (BUFFER * buf, BUFFER * s, unsigned long data,
45                      BUFFER * err)
46 {
47   HOOK *ptr;
48   BUFFER command, pattern;
49   int rc, not = 0;
50   regex_t *rx = NULL;
51   pattern_t *pat = NULL;
52   char path[_POSIX_PATH_MAX];
53
54   memset (&pattern, 0, sizeof (pattern));
55   memset (&command, 0, sizeof (command));
56
57   if (*s->dptr == '!') {
58     s->dptr++;
59     SKIPWS (s->dptr);
60     not = 1;
61   }
62
63   mutt_extract_token (&pattern, s, 0);
64
65   if (!MoreArgs (s)) {
66     strfcpy (err->data, _("too few arguments"), err->dsize);
67     goto error;
68   }
69
70   mutt_extract_token (&command, s,
71                       (data &
72                        (M_FOLDERHOOK | M_SENDHOOK | M_SEND2HOOK |
73                         M_ACCOUNTHOOK | M_REPLYHOOK)) ? M_TOKEN_SPACE : 0);
74
75   if (!command.data) {
76     strfcpy (err->data, _("too few arguments"), err->dsize);
77     goto error;
78   }
79
80   if (MoreArgs (s)) {
81     strfcpy (err->data, _("too many arguments"), err->dsize);
82     goto error;
83   }
84
85   if (data & (M_FOLDERHOOK | M_MBOXHOOK)) {
86     strfcpy (path, pattern.data, sizeof (path));
87     _mutt_expand_path (path, sizeof (path), 1);
88     FREE (&pattern.data);
89     memset (&pattern, 0, sizeof (pattern));
90     pattern.data = safe_strdup (path);
91   }
92 #ifdef USE_COMPRESSED
93   else if (data & (M_APPENDHOOK | M_OPENHOOK | M_CLOSEHOOK)) {
94     if (mutt_test_compress_command (command.data)) {
95       strfcpy (err->data, _("bad formatted command string"), err->dsize);
96       return (-1);
97     }
98   }
99 #endif
100   else if (DefaultHook && !(data & (M_CHARSETHOOK | M_ACCOUNTHOOK))
101            && (!WithCrypto || !(data & M_CRYPTHOOK))
102     ) {
103     char tmp[HUGE_STRING];
104
105     strfcpy (tmp, pattern.data, sizeof (tmp));
106     mutt_check_simple (tmp, sizeof (tmp), DefaultHook);
107     FREE (&pattern.data);
108     memset (&pattern, 0, sizeof (pattern));
109     pattern.data = safe_strdup (tmp);
110   }
111
112   if (data & (M_MBOXHOOK | M_SAVEHOOK | M_FCCHOOK)) {
113     strfcpy (path, command.data, sizeof (path));
114     mutt_expand_path (path, sizeof (path));
115     FREE (&command.data);
116     memset (&command, 0, sizeof (command));
117     command.data = safe_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 && !mutt_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 (!mutt_strcmp (ptr->command, command.data)) {
131           FREE (&command.data);
132           FREE (&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         FREE (&ptr->command);
143         ptr->command = command.data;
144         FREE (&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 = safe_malloc (sizeof (regex_t));
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       FREE (&rx);
178       goto error;
179     }
180   }
181
182   if (ptr) {
183     ptr->next = safe_calloc (1, sizeof (HOOK));
184     ptr = ptr->next;
185   }
186   else
187     Hooks = ptr = safe_calloc (1, sizeof (HOOK));
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   FREE (&pattern.data);
198   FREE (&command.data);
199   return (-1);
200 }
201
202 static void delete_hook (HOOK * h)
203 {
204   FREE (&h->command);
205   FREE (&h->rx.pattern);
206   if (h->rx.rx) {
207     regfree (h->rx.rx);
208   }
209   mutt_pattern_free (&h->pattern);
210   FREE (&h);
211 }
212
213 /* Deletes all hooks of type ``type'', or all defined hooks if ``type'' is 0 */
214 static void delete_hooks (int 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,
238                        BUFFER * err)
239 {
240   while (MoreArgs (s)) {
241     mutt_extract_token (buf, s, 0);
242     if (mutt_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       int 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   memset (&token, 0, sizeof (token));
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           FREE (&token.data);
290           mutt_sleep (1);       /* pause a moment to let the user see the error */
291           current_hook_type = 0;
292           return;
293         }
294       }
295     }
296   }
297   FREE (&token.data);
298
299   current_hook_type = 0;
300 }
301
302 char *mutt_find_hook (int type, const char *pat)
303 {
304   HOOK *tmp = Hooks;
305
306   for (; tmp; tmp = tmp->next)
307     if (tmp->type & type) {
308       if (regexec (tmp->rx.rx, pat, 0, NULL, 0) == 0)
309         return (tmp->command);
310     }
311   return (NULL);
312 }
313
314 void mutt_message_hook (CONTEXT * ctx, HEADER * hdr, int type)
315 {
316   BUFFER err, token;
317   HOOK *hook;
318   char buf[STRING];
319
320   current_hook_type = type;
321
322   err.data = buf;
323   err.dsize = sizeof (buf);
324   memset (&token, 0, sizeof (token));
325   for (hook = Hooks; hook; hook = hook->next) {
326     if (!hook->command)
327       continue;
328
329     if (hook->type & type)
330       if ((mutt_pattern_exec (hook->pattern, 0, ctx, hdr) > 0) ^ hook->rx.not)
331         if (mutt_parse_rc_line (hook->command, &token, &err) != 0) {
332           FREE (&token.data);
333           mutt_error ("%s", err.data);
334           mutt_sleep (1);
335           current_hook_type = 0;
336           return;
337         }
338   }
339   FREE (&token.data);
340   current_hook_type = 0;
341 }
342
343 static int
344 mutt_addr_hook (char *path, size_t pathlen, int type, CONTEXT * ctx,
345                 HEADER * hdr)
346 {
347   HOOK *hook;
348
349   /* determine if a matching hook exists */
350   for (hook = Hooks; hook; hook = hook->next) {
351     if (!hook->command)
352       continue;
353
354     if (hook->type & type)
355       if ((mutt_pattern_exec (hook->pattern, 0, ctx, hdr) > 0) ^ hook->rx.not) {
356         mutt_make_string (path, pathlen, hook->command, ctx, hdr);
357         return 0;
358       }
359   }
360
361   return -1;
362 }
363
364 void mutt_default_save (char *path, size_t pathlen, HEADER * hdr)
365 {
366   *path = 0;
367   if (mutt_addr_hook (path, pathlen, M_SAVEHOOK, Context, hdr) != 0) {
368     char tmp[_POSIX_PATH_MAX];
369     ADDRESS *adr;
370     ENVELOPE *env = hdr->env;
371     int fromMe = mutt_addr_is_user (env->from);
372
373     if (!fromMe && env->reply_to && env->reply_to->mailbox)
374       adr = env->reply_to;
375     else if (!fromMe && env->from && env->from->mailbox)
376       adr = env->from;
377     else if (env->to && env->to->mailbox)
378       adr = env->to;
379     else if (env->cc && env->cc->mailbox)
380       adr = env->cc;
381     else
382       adr = NULL;
383     if (adr) {
384       mutt_safe_path (tmp, sizeof (tmp), adr);
385       snprintf (path, pathlen, "=%s", tmp);
386     }
387   }
388 }
389
390 void mutt_select_fcc (char *path, size_t pathlen, HEADER * hdr)
391 {
392   ADDRESS *adr;
393   char buf[_POSIX_PATH_MAX];
394   ENVELOPE *env = hdr->env;
395
396   if (mutt_addr_hook (path, pathlen, M_FCCHOOK, NULL, hdr) != 0) {
397     if ((option (OPTSAVENAME) || option (OPTFORCENAME)) &&
398         (env->to || env->cc || env->bcc)) {
399       adr = env->to ? env->to : (env->cc ? env->cc : env->bcc);
400       mutt_safe_path (buf, sizeof (buf), adr);
401       mutt_concat_path (path, NONULL (Maildir), buf, pathlen);
402       if (!option (OPTFORCENAME) && mx_access (path, W_OK) != 0)
403         strfcpy (path, NONULL (Outbox), pathlen);
404     }
405     else
406       strfcpy (path, NONULL (Outbox), pathlen);
407   }
408   mutt_pretty_mailbox (path);
409 }
410
411 static char *_mutt_string_hook (const char *match, int hook)
412 {
413   HOOK *tmp = Hooks;
414
415   for (; tmp; tmp = tmp->next) {
416     if ((tmp->type & hook) && ((match &&
417                                 regexec (tmp->rx.rx, match, 0, NULL,
418                                          0) == 0) ^ tmp->rx.not))
419       return (tmp->command);
420   }
421   return (NULL);
422 }
423
424 char *mutt_charset_hook (const char *chs)
425 {
426   return _mutt_string_hook (chs, M_CHARSETHOOK);
427 }
428
429 char *mutt_iconv_hook (const char *chs)
430 {
431   return _mutt_string_hook (chs, M_ICONVHOOK);
432 }
433
434 char *mutt_crypt_hook (ADDRESS * adr)
435 {
436   return _mutt_string_hook (adr->mailbox, M_CRYPTHOOK);
437 }
438
439 #ifdef USE_SOCKET
440 void mutt_account_hook (const char *url)
441 {
442   HOOK *hook;
443   BUFFER token;
444   BUFFER err;
445   char buf[STRING];
446
447   err.data = buf;
448   err.dsize = sizeof (buf);
449   memset (&token, 0, sizeof (token));
450
451   for (hook = Hooks; hook; hook = hook->next) {
452     if (!(hook->command && (hook->type & M_ACCOUNTHOOK)))
453       continue;
454
455     if ((regexec (hook->rx.rx, url, 0, NULL, 0) == 0) ^ hook->rx.not) {
456       if (mutt_parse_rc_line (hook->command, &token, &err) == -1) {
457         FREE (&token.data);
458         mutt_error ("%s", err.data);
459         mutt_sleep (1);
460
461         return;
462       }
463     }
464   }
465
466   FREE (&token.data);
467 }
468 #endif