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 == '!') {
69 mutt_extract_token (&pattern, s, 0);
72 m_strcpy(err->data, err->dsize, _("too few arguments"));
76 mutt_extract_token (&command, s,
78 (M_FOLDERHOOK | M_SENDHOOK | M_SEND2HOOK |
79 M_ACCOUNTHOOK | M_REPLYHOOK)) ? M_TOKEN_SPACE : 0);
82 m_strcpy(err->data, err->dsize, _("too few arguments"));
87 m_strcpy(err->data, err->dsize, _("too many arguments"));
91 if (data & (M_FOLDERHOOK | M_MBOXHOOK)) {
92 m_strcpy(path, sizeof(path), pattern.data);
93 _mutt_expand_path (path, sizeof (path), 1);
94 p_delete(&pattern.data);
96 pattern.data = m_strdup(path);
99 else if (data & (M_APPENDHOOK | M_OPENHOOK | M_CLOSEHOOK)) {
100 if (mutt_test_compress_command (command.data)) {
101 m_strcpy(err->data, err->dsize, _("bad formatted command string"));
106 else if (DefaultHook && !(data & (M_CHARSETHOOK | M_ACCOUNTHOOK))
107 && (!WithCrypto || !(data & M_CRYPTHOOK))
109 char tmp[HUGE_STRING];
111 m_strcpy(tmp, sizeof(tmp), pattern.data);
112 mutt_check_simple (tmp, sizeof (tmp), DefaultHook);
113 p_delete(&pattern.data);
114 p_clear(&pattern, 1);
115 pattern.data = m_strdup(tmp);
118 if (data & (M_MBOXHOOK | M_SAVEHOOK | M_FCCHOOK)) {
119 m_strcpy(path, sizeof(path), command.data);
120 mutt_expand_path (path, sizeof (path));
121 p_delete(&command.data);
122 p_clear(&command, 1);
123 command.data = m_strdup(path);
126 /* check to make sure that a matching hook doesn't already exist */
127 for (ptr = Hooks; ptr; ptr = ptr->next) {
128 if (ptr->type == data &&
129 ptr->rx.not == not && !m_strcmp(pattern.data, ptr->rx.pattern)) {
131 (M_FOLDERHOOK | M_SENDHOOK | M_SEND2HOOK | M_MESSAGEHOOK |
132 M_ACCOUNTHOOK | M_REPLYHOOK)) {
133 /* these hooks allow multiple commands with the same
134 * pattern, so if we've already seen this pattern/command pair, just
135 * ignore it instead of creating a duplicate */
136 if (!m_strcmp(ptr->command, command.data)) {
137 p_delete(&command.data);
138 p_delete(&pattern.data);
143 /* other hooks only allow one command per pattern, so update the
144 * entry with the new command. this currently does not change the
145 * order of execution of the hooks, which i think is desirable since
146 * a common action to perform is to change the default (.) entry
147 * based upon some other information. */
148 p_delete(&ptr->command);
149 ptr->command = command.data;
150 p_delete(&pattern.data);
159 (M_SENDHOOK | M_SEND2HOOK | M_SAVEHOOK | M_FCCHOOK | M_MESSAGEHOOK |
162 mutt_pattern_comp (pattern.data,
163 (data & (M_SENDHOOK | M_SEND2HOOK | M_FCCHOOK)) ?
164 0 : M_FULL_MSG, err)) == NULL)
168 rx = p_new(regex_t, 1);
171 REGCOMP (rx, NONULL (pattern.data),
172 ((data & (M_CRYPTHOOK | M_CHARSETHOOK)) ? REG_ICASE : 0)))
176 REGCOMP (rx, NONULL (pattern.data),
177 (data & (M_CHARSETHOOK | M_ICONVHOOK)) ? REG_ICASE : 0)) !=
179 #endif /* M_CRYPTHOOK */
181 regerror (rc, rx, err->data, err->dsize);
189 ptr->next = p_new(HOOK, 1);
193 Hooks = ptr = p_new(HOOK, 1);
195 ptr->command = command.data;
197 ptr->rx.pattern = pattern.data;
203 p_delete(&pattern.data);
204 p_delete(&command.data);
208 static void delete_hook (HOOK * h)
210 p_delete(&h->command);
211 p_delete(&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)) {
230 prev = h; /* Unused assignment to avoid compiler warnings */
233 if (type == h->type) {
234 prev->next = h->next;
243 int mutt_parse_unhook (BUFFER * buf, BUFFER * s, unsigned long data,
246 while (MoreArgs (s)) {
247 mutt_extract_token (buf, s, 0);
248 if (m_strcmp("*", buf->data) == 0) {
249 if (current_hook_type) {
250 snprintf (err->data, err->dsize,
251 _("unhook: Can't do unhook * from within a hook."));
257 int type = mutt_get_hook_type (buf->data);
260 snprintf (err->data, err->dsize,
261 _("unhook: unknown hook type: %s"), buf->data);
264 if (current_hook_type == type) {
265 snprintf (err->data, err->dsize,
266 _("unhook: Can't delete a %s from within a %s."), buf->data,
276 void mutt_folder_hook (char *path)
282 current_hook_type = M_FOLDERHOOK;
285 err.dsize = sizeof (buf);
287 for (; tmp; tmp = tmp->next) {
291 if (tmp->type & M_FOLDERHOOK) {
292 if ((regexec (tmp->rx.rx, path, 0, NULL, 0) == 0) ^ tmp->rx.not) {
293 if (mutt_parse_rc_line (tmp->command, &token, &err) == -1) {
294 mutt_error ("%s", err.data);
295 mutt_sleep (1); /* pause a moment to let the user see the error */
297 p_delete(&token.data);
298 current_hook_type = 0;
305 p_delete(&token.data);
307 current_hook_type = 0;
310 char *mutt_find_hook (int type, const char *pat)
314 for (; tmp; tmp = tmp->next)
315 if (tmp->type & type) {
316 if (regexec (tmp->rx.rx, pat, 0, NULL, 0) == 0)
317 return (tmp->command);
322 void mutt_message_hook (CONTEXT * ctx, HEADER * hdr, int type)
328 current_hook_type = type;
331 err.dsize = sizeof (buf);
333 for (hook = Hooks; hook; hook = hook->next) {
337 if (hook->type & type)
338 if ((mutt_pattern_exec (hook->pattern, 0, ctx, hdr) > 0) ^ hook->rx.not)
339 if (mutt_parse_rc_line (hook->command, &token, &err) != 0) {
340 mutt_error ("%s", err.data);
343 p_delete(&token.data);
344 current_hook_type = 0;
349 p_delete(&token.data);
350 current_hook_type = 0;
354 mutt_addr_hook (char *path, size_t pathlen, int type, CONTEXT * ctx,
359 /* determine if a matching hook exists */
360 for (hook = Hooks; hook; hook = hook->next) {
364 if (hook->type & type)
365 if ((mutt_pattern_exec (hook->pattern, 0, ctx, hdr) > 0) ^ hook->rx.not) {
366 mutt_make_string (path, pathlen, hook->command, ctx, hdr);
374 void mutt_default_save (char *path, size_t pathlen, HEADER * hdr)
377 if (mutt_addr_hook (path, pathlen, M_SAVEHOOK, Context, hdr) != 0) {
378 char tmp[_POSIX_PATH_MAX];
380 ENVELOPE *env = hdr->env;
381 int fromMe = mutt_addr_is_user (env->from);
383 if (!fromMe && env->reply_to && env->reply_to->mailbox)
385 else if (!fromMe && env->from && env->from->mailbox)
387 else if (env->to && env->to->mailbox)
389 else if (env->cc && env->cc->mailbox)
394 mutt_safe_path (tmp, sizeof (tmp), adr);
395 snprintf (path, pathlen, "=%s", tmp);
400 void mutt_select_fcc (char *path, size_t pathlen, HEADER * hdr)
403 char buf[_POSIX_PATH_MAX];
404 ENVELOPE *env = hdr->env;
406 if (mutt_addr_hook (path, pathlen, M_FCCHOOK, NULL, hdr) != 0) {
407 if ((option (OPTSAVENAME) || option (OPTFORCENAME)) &&
408 (env->to || env->cc || env->bcc)) {
409 adr = env->to ? env->to : (env->cc ? env->cc : env->bcc);
410 mutt_safe_path (buf, sizeof (buf), adr);
411 mutt_concat_path(path, pathlen, NONULL(Maildir), buf);
412 if (!option (OPTFORCENAME) && mx_access (path, W_OK) != 0)
413 m_strcpy(path, pathlen, NONULL(Outbox));
416 m_strcpy(path, pathlen, NONULL(Outbox));
418 mutt_pretty_mailbox (path);
421 static char *_mutt_string_hook (const char *match, int hook)
425 for (; tmp; tmp = tmp->next) {
426 if ((tmp->type & hook) && ((match &&
427 regexec (tmp->rx.rx, match, 0, NULL,
428 0) == 0) ^ tmp->rx.not))
429 return (tmp->command);
434 char *mutt_charset_hook (const char *chs)
436 return _mutt_string_hook (chs, M_CHARSETHOOK);
439 char *mutt_iconv_hook (const char *chs)
441 return _mutt_string_hook (chs, M_ICONVHOOK);
444 char *mutt_crypt_hook (ADDRESS * adr)
446 return _mutt_string_hook (adr->mailbox, M_CRYPTHOOK);
450 void mutt_account_hook (const char *url)
458 err.dsize = sizeof (buf);
461 for (hook = Hooks; hook; hook = hook->next) {
462 if (!(hook->command && (hook->type & M_ACCOUNTHOOK)))
465 if ((regexec (hook->rx.rx, url, 0, NULL, 0) == 0) ^ hook->rx.not) {
466 if (mutt_parse_rc_line (hook->command, &token, &err) == -1) {
467 mutt_error ("%s", err.data);
470 p_delete(&token.data);
471 current_hook_type = 0;
478 p_delete(&token.data);