2 * Copyright (C) 1996-2002 Michael R. Elkins <me@mutt.org>, and others
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.
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.
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.
21 #include "mutt_crypt.h"
35 int type; /* hook type */
36 REGEXP rx; /* regular expression */
37 char *command; /* filename, command or pattern to execute */
38 pattern_t *pattern; /* used for fcc,save,send-hook */
42 static HOOK *Hooks = NULL;
44 static int current_hook_type = 0;
46 int mutt_parse_hook (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
49 BUFFER command, pattern;
52 pattern_t *pat = NULL;
53 char path[_POSIX_PATH_MAX];
55 memset (&pattern, 0, sizeof (pattern));
56 memset (&command, 0, sizeof (command));
65 mutt_extract_token (&pattern, s, 0);
69 strfcpy (err->data, _("too few arguments"), err->dsize);
73 mutt_extract_token (&command, s, (data & (M_FOLDERHOOK | M_SENDHOOK | M_ACCOUNTHOOK | M_REPLYHOOK)) ? M_TOKEN_SPACE : 0);
77 strfcpy (err->data, _("too few arguments"), err->dsize);
83 strfcpy (err->data, _("too many arguments"), err->dsize);
87 if (data & (M_FOLDERHOOK | M_MBOXHOOK))
89 strfcpy (path, pattern.data, sizeof (path));
90 _mutt_expand_path (path, sizeof (path), 1);
92 memset (&pattern, 0, sizeof (pattern));
93 pattern.data = safe_strdup (path);
96 else if (data & (M_APPENDHOOK | M_OPENHOOK | M_CLOSEHOOK))
98 if (mutt_test_compress_command (command.data))
100 strfcpy (err->data, _("bad formatted command string"), err->dsize);
105 else if (DefaultHook && !(data & (M_CHARSETHOOK | M_ACCOUNTHOOK))
106 && (!WithCrypto || !(data & M_CRYPTHOOK))
109 char tmp[HUGE_STRING];
111 strfcpy (tmp, pattern.data, sizeof (tmp));
112 mutt_check_simple (tmp, sizeof (tmp), DefaultHook);
113 FREE (&pattern.data);
114 memset (&pattern, 0, sizeof (pattern));
115 pattern.data = safe_strdup (tmp);
118 if (data & (M_MBOXHOOK | M_SAVEHOOK | M_FCCHOOK))
120 strfcpy (path, command.data, sizeof (path));
121 mutt_expand_path (path, sizeof (path));
122 FREE (&command.data);
123 memset (&command, 0, sizeof (command));
124 command.data = safe_strdup (path);
127 /* check to make sure that a matching hook doesn't already exist */
128 for (ptr = Hooks; ptr; ptr = ptr->next)
130 if (ptr->type == data &&
131 ptr->rx.not == not &&
132 !mutt_strcmp (pattern.data, ptr->rx.pattern))
134 if (data & (M_FOLDERHOOK | M_SENDHOOK | M_MESSAGEHOOK | M_ACCOUNTHOOK | M_REPLYHOOK))
136 /* these hooks allow multiple commands with the same
137 * pattern, so if we've already seen this pattern/command pair, just
138 * ignore it instead of creating a duplicate */
139 if (!mutt_strcmp (ptr->command, command.data))
141 FREE (&command.data);
142 FREE (&pattern.data);
148 /* other hooks only allow one command per pattern, so update the
149 * entry with the new command. this currently does not change the
150 * order of execution of the hooks, which i think is desirable since
151 * a common action to perform is to change the default (.) entry
152 * based upon some other information. */
153 FREE (&ptr->command);
154 ptr->command = command.data;
155 FREE (&pattern.data);
163 if (data & (M_SENDHOOK | M_SAVEHOOK | M_FCCHOOK | M_MESSAGEHOOK | M_REPLYHOOK))
165 if ((pat = mutt_pattern_comp (pattern.data,
166 (data & (M_SENDHOOK | M_FCCHOOK)) ? 0 : M_FULL_MSG,
172 rx = safe_malloc (sizeof (regex_t));
174 if ((rc = REGCOMP (rx, NONULL(pattern.data), ((data & (M_CRYPTHOOK|M_CHARSETHOOK)) ? REG_ICASE : 0))) != 0)
176 if ((rc = REGCOMP (rx, NONULL(pattern.data), (data & (M_CHARSETHOOK|M_ICONVHOOK)) ? REG_ICASE : 0)) != 0)
177 #endif /* M_CRYPTHOOK */
179 regerror (rc, rx, err->data, err->dsize);
188 ptr->next = safe_calloc (1, sizeof (HOOK));
192 Hooks = ptr = safe_calloc (1, sizeof (HOOK));
194 ptr->command = command.data;
196 ptr->rx.pattern = pattern.data;
202 FREE (&pattern.data);
203 FREE (&command.data);
207 static void delete_hook (HOOK *h)
210 FREE (&h->rx.pattern);
215 mutt_pattern_free (&h->pattern);
219 /* Deletes all hooks of type ``type'', or all defined hooks if ``type'' is 0 */
220 static void delete_hooks (int type)
225 while (h = Hooks, h && (type == 0 || type == h->type))
231 prev = h; /* Unused assignment to avoid compiler warnings */
237 prev->next = h->next;
246 int mutt_parse_unhook (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
250 mutt_extract_token (buf, s, 0);
251 if (mutt_strcmp ("*", buf->data) == 0)
253 if (current_hook_type)
255 snprintf (err->data, err->dsize,
256 _("unhook: Can't do unhook * from within a hook."));
263 int type = mutt_get_hook_type (buf->data);
267 snprintf (err->data, err->dsize,
268 _("unhook: unknown hook type: %s"), buf->data);
271 if (current_hook_type == type)
273 snprintf (err->data, err->dsize, _("unhook: Can't delete a %s from within a %s."),
274 buf->data, buf->data);
283 void mutt_folder_hook (char *path)
289 current_hook_type = M_FOLDERHOOK;
292 err.dsize = sizeof (buf);
293 memset (&token, 0, sizeof (token));
294 for (; tmp; tmp = tmp->next)
299 if (tmp->type & M_FOLDERHOOK)
301 if ((regexec (tmp->rx.rx, path, 0, NULL, 0) == 0) ^ tmp->rx.not)
303 if (mutt_parse_rc_line (tmp->command, &token, &err) == -1)
305 mutt_error ("%s", err.data);
307 mutt_sleep (1); /* pause a moment to let the user see the error */
308 current_hook_type = 0;
316 current_hook_type = 0;
319 char *mutt_find_hook (int type, const char *pat)
323 for (; tmp; tmp = tmp->next)
324 if (tmp->type & type)
326 if (regexec (tmp->rx.rx, pat, 0, NULL, 0) == 0)
327 return (tmp->command);
332 void mutt_message_hook (CONTEXT *ctx, HEADER *hdr, int type)
338 current_hook_type = type;
341 err.dsize = sizeof (buf);
342 memset (&token, 0, sizeof (token));
343 for (hook = Hooks; hook; hook = hook->next)
348 if (hook->type & type)
349 if ((mutt_pattern_exec (hook->pattern, 0, ctx, hdr) > 0) ^ hook->rx.not)
350 if (mutt_parse_rc_line (hook->command, &token, &err) != 0)
353 mutt_error ("%s", err.data);
355 current_hook_type = 0;
360 current_hook_type = 0;
364 mutt_addr_hook (char *path, size_t pathlen, int type, CONTEXT *ctx, HEADER *hdr)
368 /* determine if a matching hook exists */
369 for (hook = Hooks; hook; hook = hook->next)
374 if (hook->type & type)
375 if ((mutt_pattern_exec (hook->pattern, 0, ctx, hdr) > 0) ^ hook->rx.not)
377 mutt_make_string (path, pathlen, hook->command, ctx, hdr);
385 void mutt_default_save (char *path, size_t pathlen, HEADER *hdr)
388 if (mutt_addr_hook (path, pathlen, M_SAVEHOOK, Context, hdr) != 0)
390 char tmp[_POSIX_PATH_MAX];
392 ENVELOPE *env = hdr->env;
393 int fromMe = mutt_addr_is_user (env->from);
395 if (!fromMe && env->reply_to && env->reply_to->mailbox)
397 else if (!fromMe && env->from && env->from->mailbox)
399 else if (env->to && env->to->mailbox)
401 else if (env->cc && env->cc->mailbox)
407 mutt_safe_path (tmp, sizeof (tmp), adr);
408 snprintf (path, pathlen, "=%s", tmp);
413 void mutt_select_fcc (char *path, size_t pathlen, HEADER *hdr)
416 char buf[_POSIX_PATH_MAX];
417 ENVELOPE *env = hdr->env;
419 if (mutt_addr_hook (path, pathlen, M_FCCHOOK, NULL, hdr) != 0)
421 if ((option (OPTSAVENAME) || option (OPTFORCENAME)) &&
422 (env->to || env->cc || env->bcc))
424 adr = env->to ? env->to : (env->cc ? env->cc : env->bcc);
425 mutt_safe_path (buf, sizeof (buf), adr);
426 mutt_concat_path (path, NONULL(Maildir), buf, pathlen);
427 if (!option (OPTFORCENAME) && mx_access (path, W_OK) != 0)
428 strfcpy (path, NONULL (Outbox), pathlen);
431 strfcpy (path, NONULL (Outbox), pathlen);
433 mutt_pretty_mailbox (path);
436 static char *_mutt_string_hook (const char *match, int hook)
440 for (; tmp; tmp = tmp->next)
442 if ((tmp->type & hook) && ((match &&
443 regexec (tmp->rx.rx, match, 0, NULL, 0) == 0) ^ tmp->rx.not))
444 return (tmp->command);
449 char *mutt_charset_hook (const char *chs)
451 return _mutt_string_hook (chs, M_CHARSETHOOK);
454 char *mutt_iconv_hook (const char *chs)
456 return _mutt_string_hook (chs, M_ICONVHOOK);
459 char *mutt_crypt_hook (ADDRESS *adr)
461 return _mutt_string_hook (adr->mailbox, M_CRYPTHOOK);
465 void mutt_account_hook (const char* url)
473 err.dsize = sizeof (buf);
474 memset (&token, 0, sizeof (token));
476 for (hook = Hooks; hook; hook = hook->next)
478 if (! (hook->command && (hook->type & M_ACCOUNTHOOK)))
481 if ((regexec (hook->rx.rx, url, 0, NULL, 0) == 0) ^ hook->rx.not)
483 if (mutt_parse_rc_line (hook->command, &token, &err) == -1)
486 mutt_error ("%s", err.data);