2 * Copyright notice from original mutt:
3 * Copyright (C) 1996-2002 Michael R. Elkins <me@mutt.org>, and others
5 * This file is part of mutt-ng, see http://www.muttng.org/.
6 * It's licensed under the GNU General Public License,
7 * please see the file GPL in the top level source directory.
14 #include <lib-lib/mem.h>
15 #include <lib-lib/str.h>
16 #include <lib-lib/macros.h>
17 #include <lib-lib/buffer.h>
18 #include <lib-lib/file.h>
22 #include "mutt_crypt.h"
39 int type; /* hook type */
40 rx_t 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,
54 BUFFER command, pattern;
57 pattern_t *pat = NULL;
58 char path[_POSIX_PATH_MAX];
63 if (*s->dptr == '!') {
64 s->dptr = vskipspaces(s->dptr + 1);
68 mutt_extract_token (&pattern, s, 0);
71 m_strcpy(err->data, err->dsize, _("too few arguments"));
75 mutt_extract_token (&command, s,
77 (M_FOLDERHOOK | M_SENDHOOK | M_SEND2HOOK |
78 M_ACCOUNTHOOK | M_REPLYHOOK)) ? M_TOKEN_SPACE : 0);
81 m_strcpy(err->data, err->dsize, _("too few arguments"));
86 m_strcpy(err->data, err->dsize, _("too many arguments"));
90 if (data & (M_FOLDERHOOK | M_MBOXHOOK)) {
91 m_strcpy(path, sizeof(path), pattern.data);
92 _mutt_expand_path (path, sizeof (path), 1);
93 p_delete(&pattern.data);
95 pattern.data = m_strdup(path);
98 else if (data & (M_APPENDHOOK | M_OPENHOOK | M_CLOSEHOOK)) {
99 if (mutt_test_compress_command (command.data)) {
100 m_strcpy(err->data, err->dsize, _("bad formatted command string"));
105 else if (DefaultHook && !(data & (M_CHARSETHOOK | M_ACCOUNTHOOK))
106 && (!WithCrypto || !(data & M_CRYPTHOOK))
108 char tmp[HUGE_STRING];
110 m_strcpy(tmp, sizeof(tmp), pattern.data);
111 mutt_check_simple (tmp, sizeof (tmp), DefaultHook);
112 p_delete(&pattern.data);
113 p_clear(&pattern, 1);
114 pattern.data = m_strdup(tmp);
117 if (data & (M_MBOXHOOK | M_SAVEHOOK | M_FCCHOOK)) {
118 m_strcpy(path, sizeof(path), command.data);
119 mutt_expand_path (path, sizeof (path));
120 p_delete(&command.data);
121 p_clear(&command, 1);
122 command.data = m_strdup(path);
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 && !m_strcmp(pattern.data, ptr->rx.pattern)) {
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 (!m_strcmp(ptr->command, command.data)) {
136 p_delete(&command.data);
137 p_delete(&pattern.data);
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 p_delete(&ptr->command);
148 ptr->command = command.data;
149 p_delete(&pattern.data);
158 (M_SENDHOOK | M_SEND2HOOK | M_SAVEHOOK | M_FCCHOOK | M_MESSAGEHOOK |
161 mutt_pattern_comp (pattern.data,
162 (data & (M_SENDHOOK | M_SEND2HOOK | M_FCCHOOK)) ?
163 0 : M_FULL_MSG, err)) == NULL)
167 rx = p_new(regex_t, 1);
170 REGCOMP (rx, NONULL (pattern.data),
171 ((data & (M_CRYPTHOOK | M_CHARSETHOOK)) ? REG_ICASE : 0)))
175 REGCOMP (rx, NONULL (pattern.data),
176 (data & (M_CHARSETHOOK | M_ICONVHOOK)) ? REG_ICASE : 0)) !=
178 #endif /* M_CRYPTHOOK */
180 regerror (rc, rx, err->data, err->dsize);
188 ptr->next = p_new(HOOK, 1);
192 Hooks = ptr = p_new(HOOK, 1);
194 ptr->command = command.data;
196 ptr->rx.pattern = pattern.data;
202 p_delete(&pattern.data);
203 p_delete(&command.data);
207 static void delete_hook (HOOK * h)
209 p_delete(&h->command);
210 p_delete(&h->rx.pattern);
214 mutt_pattern_free (&h->pattern);
218 /* Deletes all hooks of type ``type'', or all defined hooks if ``type'' is 0 */
219 static void delete_hooks (int type)
224 while (h = Hooks, h && (type == 0 || type == h->type)) {
229 prev = h; /* Unused assignment to avoid compiler warnings */
232 if (type == h->type) {
233 prev->next = h->next;
242 int mutt_parse_unhook (BUFFER * buf, BUFFER * s, unsigned long data,
245 while (MoreArgs (s)) {
246 mutt_extract_token (buf, s, 0);
247 if (m_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."));
256 int type = mutt_get_hook_type (buf->data);
259 snprintf (err->data, err->dsize,
260 _("unhook: unknown hook type: %s"), buf->data);
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,
275 void mutt_folder_hook (char *path)
281 current_hook_type = M_FOLDERHOOK;
284 err.dsize = sizeof (buf);
286 for (; tmp; tmp = tmp->next) {
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 mutt_sleep (1); /* pause a moment to let the user see the error */
296 p_delete(&token.data);
297 current_hook_type = 0;
304 p_delete(&token.data);
306 current_hook_type = 0;
309 char *mutt_find_hook (int type, const char *pat)
313 for (; tmp; tmp = tmp->next)
314 if (tmp->type & type) {
315 if (regexec (tmp->rx.rx, pat, 0, NULL, 0) == 0)
316 return (tmp->command);
321 void mutt_message_hook (CONTEXT * ctx, HEADER * hdr, int type)
327 current_hook_type = type;
330 err.dsize = sizeof (buf);
332 for (hook = Hooks; hook; hook = hook->next) {
336 if (hook->type & type)
337 if ((mutt_pattern_exec (hook->pattern, 0, ctx, hdr) > 0) ^ hook->rx.not)
338 if (mutt_parse_rc_line (hook->command, &token, &err) != 0) {
339 mutt_error ("%s", err.data);
342 p_delete(&token.data);
343 current_hook_type = 0;
348 p_delete(&token.data);
349 current_hook_type = 0;
353 mutt_addr_hook (char *path, size_t pathlen, int type, CONTEXT * ctx,
358 /* determine if a matching hook exists */
359 for (hook = Hooks; hook; hook = hook->next) {
363 if (hook->type & type)
364 if ((mutt_pattern_exec (hook->pattern, 0, ctx, hdr) > 0) ^ hook->rx.not) {
365 mutt_make_string (path, pathlen, hook->command, ctx, hdr);
373 void mutt_default_save (char *path, size_t pathlen, HEADER * hdr)
376 if (mutt_addr_hook (path, pathlen, M_SAVEHOOK, Context, hdr) != 0) {
377 char tmp[_POSIX_PATH_MAX];
379 ENVELOPE *env = hdr->env;
380 int fromMe = mutt_addr_is_user (env->from);
382 if (!fromMe && env->reply_to && env->reply_to->mailbox)
384 else if (!fromMe && env->from && env->from->mailbox)
386 else if (env->to && env->to->mailbox)
388 else if (env->cc && env->cc->mailbox)
393 mutt_safe_path (tmp, sizeof (tmp), adr);
394 snprintf (path, pathlen, "=%s", tmp);
399 void mutt_select_fcc (char *path, size_t pathlen, HEADER * hdr)
402 char buf[_POSIX_PATH_MAX];
403 ENVELOPE *env = hdr->env;
405 if (mutt_addr_hook (path, pathlen, M_FCCHOOK, NULL, hdr) != 0) {
406 if ((option (OPTSAVENAME) || option (OPTFORCENAME)) &&
407 (env->to || env->cc || env->bcc)) {
408 adr = env->to ? env->to : (env->cc ? env->cc : env->bcc);
409 mutt_safe_path (buf, sizeof (buf), adr);
410 mutt_concat_path(path, pathlen, NONULL(Maildir), buf);
411 if (!option (OPTFORCENAME) && mx_access (path, W_OK) != 0)
412 m_strcpy(path, pathlen, NONULL(Outbox));
415 m_strcpy(path, pathlen, NONULL(Outbox));
417 mutt_pretty_mailbox (path);
420 static char *_mutt_string_hook (const char *match, int hook)
424 for (; tmp; tmp = tmp->next) {
425 if ((tmp->type & hook) && ((match &&
426 regexec (tmp->rx.rx, match, 0, NULL,
427 0) == 0) ^ tmp->rx.not))
428 return (tmp->command);
433 char *mutt_charset_hook (const char *chs)
435 return _mutt_string_hook (chs, M_CHARSETHOOK);
438 char *mutt_iconv_hook (const char *chs)
440 return _mutt_string_hook (chs, M_ICONVHOOK);
443 char *mutt_crypt_hook (address_t * adr)
445 return _mutt_string_hook (adr->mailbox, M_CRYPTHOOK);
449 void mutt_account_hook (const char *url)
457 err.dsize = sizeof (buf);
460 for (hook = Hooks; hook; hook = hook->next) {
461 if (!(hook->command && (hook->type & M_ACCOUNTHOOK)))
464 if ((regexec (hook->rx.rx, url, 0, NULL, 0) == 0) ^ hook->rx.not) {
465 if (mutt_parse_rc_line (hook->command, &token, &err) == -1) {
466 mutt_error ("%s", err.data);
469 p_delete(&token.data);
470 current_hook_type = 0;
477 p_delete(&token.data);