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