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.
20 #include <lib-lib/mem.h>
21 #include <lib-lib/str.h>
22 #include <lib-lib/macros.h>
23 #include <lib-lib/buffer.h>
24 #include <lib-lib/file.h>
25 #include <lib-lib/rx.h>
29 #include <lib-crypt/crypt.h>
35 int type; /* hook type */
36 rx_t 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 unsigned long current_hook_type = 0;
46 int mutt_parse_hook (BUFFER * buf __attribute__ ((unused)), BUFFER * s, unsigned long data,
50 BUFFER command, pattern;
53 pattern_t *pat = NULL;
54 char path[_POSIX_PATH_MAX];
59 if (*s->dptr == '!') {
60 s->dptr = vskipspaces(s->dptr + 1);
64 mutt_extract_token (&pattern, s, 0);
67 m_strcpy(err->data, err->dsize, _("too few arguments"));
71 mutt_extract_token (&command, s,
73 (M_FOLDERHOOK | M_SENDHOOK | M_SEND2HOOK |
74 M_ACCOUNTHOOK | M_REPLYHOOK)) ? M_TOKEN_SPACE : 0);
77 m_strcpy(err->data, err->dsize, _("too few arguments"));
82 m_strcpy(err->data, err->dsize, _("too many arguments"));
86 if (data & (M_FOLDERHOOK | M_MBOXHOOK)) {
87 m_strcpy(path, sizeof(path), pattern.data);
88 _mutt_expand_path (path, sizeof (path), 1);
89 p_delete(&pattern.data);
91 pattern.data = m_strdup(path);
93 else if (data & (M_APPENDHOOK | M_OPENHOOK | M_CLOSEHOOK)) {
94 if (mutt_test_compress_command (command.data)) {
95 m_strcpy(err->data, err->dsize, _("bad formatted command string"));
99 else if (DefaultHook && !(data & (M_CHARSETHOOK | M_ACCOUNTHOOK))
100 && !(data & M_CRYPTHOOK))
102 char tmp[HUGE_STRING];
104 m_strcpy(tmp, sizeof(tmp), pattern.data);
105 mutt_check_simple (tmp, sizeof (tmp), DefaultHook);
106 p_delete(&pattern.data);
107 p_clear(&pattern, 1);
108 pattern.data = m_strdup(tmp);
111 if (data & (M_MBOXHOOK | M_SAVEHOOK | M_FCCHOOK)) {
112 m_strcpy(path, sizeof(path), command.data);
113 mutt_expand_path (path, sizeof (path));
114 p_delete(&command.data);
115 p_clear(&command, 1);
116 command.data = m_strdup(path);
119 /* check to make sure that a matching hook doesn't already exist */
120 for (ptr = Hooks; ptr; ptr = ptr->next) {
121 if (ptr->type == data &&
122 ptr->rx.not == not && !m_strcmp(pattern.data, ptr->rx.pattern)) {
124 (M_FOLDERHOOK | M_SENDHOOK | M_SEND2HOOK | M_MESSAGEHOOK |
125 M_ACCOUNTHOOK | M_REPLYHOOK)) {
126 /* these hooks allow multiple commands with the same
127 * pattern, so if we've already seen this pattern/command pair, just
128 * ignore it instead of creating a duplicate */
129 if (!m_strcmp(ptr->command, command.data)) {
130 p_delete(&command.data);
131 p_delete(&pattern.data);
136 /* other hooks only allow one command per pattern, so update the
137 * entry with the new command. this currently does not change the
138 * order of execution of the hooks, which i think is desirable since
139 * a common action to perform is to change the default (.) entry
140 * based upon some other information. */
141 p_delete(&ptr->command);
142 ptr->command = command.data;
143 p_delete(&pattern.data);
152 (M_SENDHOOK | M_SEND2HOOK | M_SAVEHOOK | M_FCCHOOK | M_MESSAGEHOOK |
155 mutt_pattern_comp (pattern.data,
156 (data & (M_SENDHOOK | M_SEND2HOOK | M_FCCHOOK)) ?
157 0 : M_FULL_MSG, err)) == NULL)
161 rx = p_new(regex_t, 1);
164 REGCOMP (rx, NONULL (pattern.data),
165 ((data & (M_CRYPTHOOK | M_CHARSETHOOK)) ? REG_ICASE : 0)))
169 REGCOMP (rx, NONULL (pattern.data),
170 (data & (M_CHARSETHOOK | M_ICONVHOOK)) ? REG_ICASE : 0)) !=
172 #endif /* M_CRYPTHOOK */
174 regerror (rc, rx, err->data, err->dsize);
182 ptr->next = p_new(HOOK, 1);
186 Hooks = ptr = p_new(HOOK, 1);
188 ptr->command = command.data;
190 ptr->rx.pattern = pattern.data;
196 p_delete(&pattern.data);
197 p_delete(&command.data);
201 static void delete_hook (HOOK * h)
203 p_delete(&h->command);
204 p_delete(&h->rx.pattern);
208 mutt_pattern_free (&h->pattern);
212 /* Deletes all hooks of type ``type'', or all defined hooks if ``type'' is 0 */
213 static void delete_hooks (unsigned long type)
218 while (h = Hooks, h && (type == 0 || type == h->type)) {
223 prev = h; /* Unused assignment to avoid compiler warnings */
226 if (type == h->type) {
227 prev->next = h->next;
236 int mutt_parse_unhook (BUFFER * buf, BUFFER * s, unsigned long data __attribute__ ((unused)),
239 while (MoreArgs (s)) {
240 mutt_extract_token (buf, s, 0);
241 if (m_strcmp("*", buf->data) == 0) {
242 if (current_hook_type) {
243 snprintf (err->data, err->dsize,
244 _("unhook: Can't do unhook * from within a hook."));
250 unsigned long type = mutt_get_hook_type (buf->data);
253 snprintf (err->data, err->dsize,
254 _("unhook: unknown hook type: %s"), buf->data);
257 if (current_hook_type == type) {
258 snprintf (err->data, err->dsize,
259 _("unhook: Can't delete a %s from within a %s."), buf->data,
269 void mutt_folder_hook (char *path)
275 current_hook_type = M_FOLDERHOOK;
278 err.dsize = sizeof (buf);
280 for (; tmp; tmp = tmp->next) {
284 if (tmp->type & M_FOLDERHOOK) {
285 if ((regexec (tmp->rx.rx, path, 0, NULL, 0) == 0) ^ tmp->rx.not) {
286 if (mutt_parse_rc_line (tmp->command, &token, &err) == -1) {
287 mutt_error ("%s", err.data);
288 mutt_sleep (1); /* pause a moment to let the user see the error */
290 p_delete(&token.data);
291 current_hook_type = 0;
298 p_delete(&token.data);
300 current_hook_type = 0;
303 char *mutt_find_hook (int type, const char *pat)
307 for (; tmp; tmp = tmp->next)
308 if (tmp->type & type) {
309 if (regexec (tmp->rx.rx, pat, 0, NULL, 0) == 0)
310 return (tmp->command);
315 void mutt_message_hook (CONTEXT * ctx, HEADER * hdr, int type)
321 current_hook_type = type;
324 err.dsize = sizeof (buf);
326 for (hook = Hooks; hook; hook = hook->next) {
330 if (hook->type & type)
331 if ((mutt_pattern_exec (hook->pattern, 0, ctx, hdr) > 0) ^ hook->rx.not)
332 if (mutt_parse_rc_line (hook->command, &token, &err) != 0) {
333 mutt_error ("%s", err.data);
336 p_delete(&token.data);
337 current_hook_type = 0;
342 p_delete(&token.data);
343 current_hook_type = 0;
347 mutt_addr_hook (char *path, size_t pathlen, unsigned long type, CONTEXT * ctx,
352 /* determine if a matching hook exists */
353 for (hook = Hooks; hook; hook = hook->next) {
357 if (hook->type & type)
358 if ((mutt_pattern_exec (hook->pattern, 0, ctx, hdr) > 0) ^ hook->rx.not) {
359 mutt_make_string (path, pathlen, hook->command, ctx, hdr);
367 void mutt_default_save (char *path, size_t pathlen, HEADER * hdr)
370 if (mutt_addr_hook (path, pathlen, M_SAVEHOOK, Context, hdr) != 0) {
371 char tmp[_POSIX_PATH_MAX];
373 ENVELOPE *env = hdr->env;
374 int fromMe = mutt_addr_is_user (env->from);
376 if (!fromMe && env->reply_to && env->reply_to->mailbox)
378 else if (!fromMe && env->from && env->from->mailbox)
380 else if (env->to && env->to->mailbox)
382 else if (env->cc && env->cc->mailbox)
387 mutt_safe_path (tmp, sizeof (tmp), adr);
388 snprintf (path, pathlen, "=%s", tmp);
393 void mutt_select_fcc (char *path, size_t pathlen, HEADER * hdr)
396 char buf[_POSIX_PATH_MAX];
397 ENVELOPE *env = hdr->env;
399 if (mutt_addr_hook (path, pathlen, M_FCCHOOK, NULL, hdr) != 0) {
400 if ((option (OPTSAVENAME) || option (OPTFORCENAME)) &&
401 (env->to || env->cc || env->bcc)) {
402 adr = env->to ? env->to : (env->cc ? env->cc : env->bcc);
403 mutt_safe_path (buf, sizeof (buf), adr);
404 mutt_concat_path(path, pathlen, NONULL(Maildir), buf);
405 if (!option (OPTFORCENAME) && mx_access (path, W_OK) != 0)
406 m_strcpy(path, pathlen, NONULL(Outbox));
409 m_strcpy(path, pathlen, NONULL(Outbox));
411 mutt_pretty_mailbox (path);
414 static char *_mutt_string_hook (const char *match, int hook)
418 for (; tmp; tmp = tmp->next) {
419 if ((tmp->type & hook) && ((match &&
420 regexec (tmp->rx.rx, match, 0, NULL,
421 0) == 0) ^ tmp->rx.not))
422 return (tmp->command);
427 char *mutt_charset_hook (const char *chs)
429 return _mutt_string_hook (chs, M_CHARSETHOOK);
432 char *mutt_iconv_hook (const char *chs)
434 return _mutt_string_hook (chs, M_ICONVHOOK);
437 char *mutt_crypt_hook (address_t * adr)
439 return _mutt_string_hook (adr->mailbox, M_CRYPTHOOK);
442 void mutt_account_hook (const char *url)
450 err.dsize = sizeof (buf);
453 for (hook = Hooks; hook; hook = hook->next) {
454 if (!(hook->command && (hook->type & M_ACCOUNTHOOK)))
457 if ((regexec (hook->rx.rx, url, 0, NULL, 0) == 0) ^ hook->rx.not) {
458 if (mutt_parse_rc_line (hook->command, &token, &err) == -1) {
459 mutt_error ("%s", err.data);
462 p_delete(&token.data);
463 current_hook_type = 0;
470 p_delete(&token.data);