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.
25 #include "mutt_crypt.h"
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 */
46 static HOOK *Hooks = NULL;
48 static int current_hook_type = 0;
50 int mutt_parse_hook (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
53 BUFFER command, pattern;
56 pattern_t *pat = NULL;
57 char path[_POSIX_PATH_MAX];
59 memset (&pattern, 0, sizeof (pattern));
60 memset (&command, 0, sizeof (command));
69 mutt_extract_token (&pattern, s, 0);
73 strfcpy (err->data, _("too few arguments"), err->dsize);
77 mutt_extract_token (&command, s, (data & (M_FOLDERHOOK | M_SENDHOOK | M_SEND2HOOK | M_ACCOUNTHOOK | M_REPLYHOOK)) ? M_TOKEN_SPACE : 0);
81 strfcpy (err->data, _("too few arguments"), err->dsize);
87 strfcpy (err->data, _("too many arguments"), err->dsize);
91 if (data & (M_FOLDERHOOK | M_MBOXHOOK))
93 strfcpy (path, pattern.data, sizeof (path));
94 _mutt_expand_path (path, sizeof (path), 1);
96 memset (&pattern, 0, sizeof (pattern));
97 pattern.data = safe_strdup (path);
100 else if (data & (M_APPENDHOOK | M_OPENHOOK | M_CLOSEHOOK))
102 if (mutt_test_compress_command (command.data))
104 strfcpy (err->data, _("bad formatted command string"), err->dsize);
109 else if (DefaultHook && !(data & (M_CHARSETHOOK | M_ACCOUNTHOOK))
110 && (!WithCrypto || !(data & M_CRYPTHOOK))
113 char tmp[HUGE_STRING];
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);
122 if (data & (M_MBOXHOOK | M_SAVEHOOK | M_FCCHOOK))
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);
131 /* check to make sure that a matching hook doesn't already exist */
132 for (ptr = Hooks; ptr; ptr = ptr->next)
134 if (ptr->type == data &&
135 ptr->rx.not == not &&
136 !mutt_strcmp (pattern.data, ptr->rx.pattern))
138 if (data & (M_FOLDERHOOK | M_SENDHOOK | M_SEND2HOOK | M_MESSAGEHOOK | M_ACCOUNTHOOK | M_REPLYHOOK))
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))
145 FREE (&command.data);
146 FREE (&pattern.data);
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);
167 if (data & (M_SENDHOOK | M_SEND2HOOK | M_SAVEHOOK | M_FCCHOOK | M_MESSAGEHOOK | M_REPLYHOOK))
169 if ((pat = mutt_pattern_comp (pattern.data,
170 (data & (M_SENDHOOK | M_SEND2HOOK | M_FCCHOOK)) ? 0 : M_FULL_MSG,
176 rx = safe_malloc (sizeof (regex_t));
178 if ((rc = REGCOMP (rx, NONULL(pattern.data), ((data & (M_CRYPTHOOK|M_CHARSETHOOK)) ? REG_ICASE : 0))) != 0)
180 if ((rc = REGCOMP (rx, NONULL(pattern.data), (data & (M_CHARSETHOOK|M_ICONVHOOK)) ? REG_ICASE : 0)) != 0)
181 #endif /* M_CRYPTHOOK */
183 regerror (rc, rx, err->data, err->dsize);
192 ptr->next = safe_calloc (1, sizeof (HOOK));
196 Hooks = ptr = safe_calloc (1, sizeof (HOOK));
198 ptr->command = command.data;
200 ptr->rx.pattern = pattern.data;
206 FREE (&pattern.data);
207 FREE (&command.data);
211 static void delete_hook (HOOK *h)
214 FREE (&h->rx.pattern);
219 mutt_pattern_free (&h->pattern);
223 /* Deletes all hooks of type ``type'', or all defined hooks if ``type'' is 0 */
224 static void delete_hooks (int type)
229 while (h = Hooks, h && (type == 0 || type == h->type))
235 prev = h; /* Unused assignment to avoid compiler warnings */
241 prev->next = h->next;
250 int mutt_parse_unhook (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
254 mutt_extract_token (buf, s, 0);
255 if (mutt_strcmp ("*", buf->data) == 0)
257 if (current_hook_type)
259 snprintf (err->data, err->dsize,
260 _("unhook: Can't do unhook * from within a hook."));
267 int type = mutt_get_hook_type (buf->data);
271 snprintf (err->data, err->dsize,
272 _("unhook: unknown hook type: %s"), buf->data);
275 if (current_hook_type == type)
277 snprintf (err->data, err->dsize, _("unhook: Can't delete a %s from within a %s."),
278 buf->data, buf->data);
287 void mutt_folder_hook (char *path)
293 current_hook_type = M_FOLDERHOOK;
296 err.dsize = sizeof (buf);
297 memset (&token, 0, sizeof (token));
298 for (; tmp; tmp = tmp->next)
303 if (tmp->type & M_FOLDERHOOK)
305 if ((regexec (tmp->rx.rx, path, 0, NULL, 0) == 0) ^ tmp->rx.not)
307 if (mutt_parse_rc_line (tmp->command, &token, &err) == -1)
309 mutt_error ("%s", err.data);
311 mutt_sleep (1); /* pause a moment to let the user see the error */
312 current_hook_type = 0;
320 current_hook_type = 0;
323 char *mutt_find_hook (int type, const char *pat)
327 for (; tmp; tmp = tmp->next)
328 if (tmp->type & type)
330 if (regexec (tmp->rx.rx, pat, 0, NULL, 0) == 0)
331 return (tmp->command);
336 void mutt_message_hook (CONTEXT *ctx, HEADER *hdr, int type)
342 current_hook_type = type;
345 err.dsize = sizeof (buf);
346 memset (&token, 0, sizeof (token));
347 for (hook = Hooks; hook; hook = hook->next)
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)
357 mutt_error ("%s", err.data);
359 current_hook_type = 0;
364 current_hook_type = 0;
368 mutt_addr_hook (char *path, size_t pathlen, int type, CONTEXT *ctx, HEADER *hdr)
372 /* determine if a matching hook exists */
373 for (hook = Hooks; hook; hook = hook->next)
378 if (hook->type & type)
379 if ((mutt_pattern_exec (hook->pattern, 0, ctx, hdr) > 0) ^ hook->rx.not)
381 mutt_make_string (path, pathlen, hook->command, ctx, hdr);
389 void mutt_default_save (char *path, size_t pathlen, HEADER *hdr)
392 if (mutt_addr_hook (path, pathlen, M_SAVEHOOK, Context, hdr) != 0)
394 char tmp[_POSIX_PATH_MAX];
396 ENVELOPE *env = hdr->env;
397 int fromMe = mutt_addr_is_user (env->from);
399 if (!fromMe && env->reply_to && env->reply_to->mailbox)
401 else if (!fromMe && env->from && env->from->mailbox)
403 else if (env->to && env->to->mailbox)
405 else if (env->cc && env->cc->mailbox)
411 mutt_safe_path (tmp, sizeof (tmp), adr);
412 snprintf (path, pathlen, "=%s", tmp);
417 void mutt_select_fcc (char *path, size_t pathlen, HEADER *hdr)
420 char buf[_POSIX_PATH_MAX];
421 ENVELOPE *env = hdr->env;
423 if (mutt_addr_hook (path, pathlen, M_FCCHOOK, NULL, hdr) != 0)
425 if ((option (OPTSAVENAME) || option (OPTFORCENAME)) &&
426 (env->to || env->cc || env->bcc))
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);
435 strfcpy (path, NONULL (Outbox), pathlen);
437 mutt_pretty_mailbox (path);
440 static char *_mutt_string_hook (const char *match, int hook)
444 for (; tmp; tmp = tmp->next)
446 if ((tmp->type & hook) && ((match &&
447 regexec (tmp->rx.rx, match, 0, NULL, 0) == 0) ^ tmp->rx.not))
448 return (tmp->command);
453 char *mutt_charset_hook (const char *chs)
455 return _mutt_string_hook (chs, M_CHARSETHOOK);
458 char *mutt_iconv_hook (const char *chs)
460 return _mutt_string_hook (chs, M_ICONVHOOK);
463 char *mutt_crypt_hook (ADDRESS *adr)
465 return _mutt_string_hook (adr->mailbox, M_CRYPTHOOK);
469 void mutt_account_hook (const char* url)
477 err.dsize = sizeof (buf);
478 memset (&token, 0, sizeof (token));
480 for (hook = Hooks; hook; hook = hook->next)
482 if (! (hook->command && (hook->type & M_ACCOUNTHOOK)))
485 if ((regexec (hook->rx.rx, url, 0, NULL, 0) == 0) ^ hook->rx.not)
487 if (mutt_parse_rc_line (hook->command, &token, &err) == -1)
490 mutt_error ("%s", err.data);