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