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