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.
10 #include <lib-lib/lib-lib.h>
11 #include <lib-ui/curses.h>
12 #include <lib-mx/mx.h>
13 #include <lib-mx/compress.h>
14 #include <lib-crypt/crypt.h>
21 int type; /* hook type */
22 rx_t rx; /* regular expression */
23 char *command; /* filename, command or pattern to execute */
24 pattern_t *pattern; /* used for fcc,save,send-hook */
28 static HOOK *Hooks = NULL;
30 static unsigned long current_hook_type = 0;
32 int mutt_parse_hook (BUFFER * buf __attribute__ ((unused)), BUFFER * s,
33 unsigned long data, BUFFER * err)
36 BUFFER command, pattern;
39 pattern_t *pat = NULL;
40 char path[_POSIX_PATH_MAX];
45 if (*s->dptr == '!') {
46 s->dptr = vskipspaces(s->dptr + 1);
50 mutt_extract_token (&pattern, s, 0);
53 m_strcpy(err->data, err->dsize, _("too few arguments"));
57 mutt_extract_token (&command, s,
59 (M_FOLDERHOOK | M_SENDHOOK | M_SEND2HOOK |
60 M_ACCOUNTHOOK | M_REPLYHOOK)) ? M_TOKEN_SPACE : 0);
63 m_strcpy(err->data, err->dsize, _("too few arguments"));
68 m_strcpy(err->data, err->dsize, _("too many arguments"));
72 if (data & (M_FOLDERHOOK | M_MBOXHOOK)) {
73 m_strcpy(path, sizeof(path), pattern.data);
74 _mutt_expand_path (path, sizeof (path), 1);
75 p_delete(&pattern.data);
77 pattern.data = m_strdup(path);
79 else if (data & (M_APPENDHOOK | M_OPENHOOK | M_CLOSEHOOK)) {
80 if (mutt_test_compress_command (command.data)) {
81 m_strcpy(err->data, err->dsize, _("bad formatted command string"));
85 else if (DefaultHook && !(data & (M_CHARSETHOOK | M_ACCOUNTHOOK))
86 && !(data & M_CRYPTHOOK))
88 char tmp[HUGE_STRING];
90 m_strcpy(tmp, sizeof(tmp), pattern.data);
91 mutt_check_simple (tmp, sizeof (tmp), DefaultHook);
92 p_delete(&pattern.data);
94 pattern.data = m_strdup(tmp);
97 if (data & (M_MBOXHOOK | M_SAVEHOOK | M_FCCHOOK)) {
98 m_strcpy(path, sizeof(path), command.data);
99 mutt_expand_path (path, sizeof (path));
100 p_delete(&command.data);
101 p_clear(&command, 1);
102 command.data = m_strdup(path);
105 /* check to make sure that a matching hook doesn't already exist */
106 for (ptr = Hooks; ptr; ptr = ptr->next) {
107 if (ptr->type == data &&
108 ptr->rx.not == not && !m_strcmp(pattern.data, ptr->rx.pattern)) {
110 (M_FOLDERHOOK | M_SENDHOOK | M_SEND2HOOK | M_MESSAGEHOOK |
111 M_ACCOUNTHOOK | M_REPLYHOOK)) {
112 /* these hooks allow multiple commands with the same
113 * pattern, so if we've already seen this pattern/command pair, just
114 * ignore it instead of creating a duplicate */
115 if (!m_strcmp(ptr->command, command.data)) {
116 p_delete(&command.data);
117 p_delete(&pattern.data);
122 /* other hooks only allow one command per pattern, so update the
123 * entry with the new command. this currently does not change the
124 * order of execution of the hooks, which i think is desirable since
125 * a common action to perform is to change the default (.) entry
126 * based upon some other information. */
127 p_delete(&ptr->command);
128 ptr->command = command.data;
129 p_delete(&pattern.data);
138 (M_SENDHOOK | M_SEND2HOOK | M_SAVEHOOK | M_FCCHOOK | M_MESSAGEHOOK |
141 mutt_pattern_comp (pattern.data,
142 (data & (M_SENDHOOK | M_SEND2HOOK | M_FCCHOOK)) ?
143 0 : M_FULL_MSG, err)) == NULL)
147 rx = p_new(regex_t, 1);
150 REGCOMP (rx, NONULL (pattern.data),
151 ((data & (M_CRYPTHOOK | M_CHARSETHOOK)) ? REG_ICASE : 0)))
155 REGCOMP (rx, NONULL (pattern.data),
156 (data & (M_CHARSETHOOK | M_ICONVHOOK)) ? REG_ICASE : 0)) !=
158 #endif /* M_CRYPTHOOK */
160 regerror (rc, rx, err->data, err->dsize);
168 ptr->next = p_new(HOOK, 1);
172 Hooks = ptr = p_new(HOOK, 1);
174 ptr->command = command.data;
176 ptr->rx.pattern = pattern.data;
182 p_delete(&pattern.data);
183 p_delete(&command.data);
187 static void delete_hook (HOOK * h)
189 p_delete(&h->command);
190 p_delete(&h->rx.pattern);
194 mutt_pattern_free (&h->pattern);
198 /* Deletes all hooks of type ``type'', or all defined hooks if ``type'' is 0 */
199 static void delete_hooks (long type)
204 while (h = Hooks, h && (type == 0 || type == h->type)) {
209 prev = h; /* Unused assignment to avoid compiler warnings */
212 if (type == h->type) {
213 prev->next = h->next;
222 int mutt_parse_unhook (BUFFER * buf, BUFFER * s, unsigned long data __attribute__ ((unused)),
225 while (MoreArgs (s)) {
226 mutt_extract_token (buf, s, 0);
227 if (m_strcmp("*", buf->data) == 0) {
228 if (current_hook_type) {
229 snprintf (err->data, err->dsize,
230 _("unhook: Can't do unhook * from within a hook."));
236 unsigned long type = mutt_get_hook_type (buf->data);
239 snprintf (err->data, err->dsize,
240 _("unhook: unknown hook type: %s"), buf->data);
243 if (current_hook_type == type) {
244 snprintf (err->data, err->dsize,
245 _("unhook: Can't delete a %s from within a %s."), buf->data,
255 void mutt_folder_hook (char *path)
261 current_hook_type = M_FOLDERHOOK;
264 err.dsize = sizeof (buf);
266 for (; tmp; tmp = tmp->next) {
270 if (tmp->type & M_FOLDERHOOK) {
271 if ((regexec (tmp->rx.rx, path, 0, NULL, 0) == 0) ^ tmp->rx.not) {
272 if (mutt_parse_rc_line (tmp->command, &token, &err) == -1) {
273 mutt_error ("%s", err.data);
274 mutt_sleep (1); /* pause a moment to let the user see the error */
276 p_delete(&token.data);
277 current_hook_type = 0;
284 p_delete(&token.data);
286 current_hook_type = 0;
289 char *mutt_find_hook (int type, const char *pat)
293 for (; tmp; tmp = tmp->next)
294 if (tmp->type & type) {
295 if (regexec (tmp->rx.rx, pat, 0, NULL, 0) == 0)
296 return (tmp->command);
301 void mutt_message_hook (CONTEXT * ctx, HEADER * hdr, int type)
307 current_hook_type = type;
310 err.dsize = sizeof (buf);
312 for (hook = Hooks; hook; hook = hook->next) {
316 if (hook->type & type)
317 if ((mutt_pattern_exec (hook->pattern, 0, ctx, hdr) > 0) ^ hook->rx.not)
318 if (mutt_parse_rc_line (hook->command, &token, &err) != 0) {
319 mutt_error ("%s", err.data);
322 p_delete(&token.data);
323 current_hook_type = 0;
328 p_delete(&token.data);
329 current_hook_type = 0;
333 mutt_addr_hook (char *path, ssize_t pathlen, unsigned long type, CONTEXT * ctx,
338 /* determine if a matching hook exists */
339 for (hook = Hooks; hook; hook = hook->next) {
343 if (hook->type & type)
344 if ((mutt_pattern_exec (hook->pattern, 0, ctx, hdr) > 0) ^ hook->rx.not) {
345 mutt_make_string (path, pathlen, hook->command, ctx, hdr);
353 void mutt_default_save (char *path, ssize_t pathlen, const HEADER * hdr)
356 if (mutt_addr_hook (path, pathlen, M_SAVEHOOK, Context, hdr) != 0) {
357 char tmp[_POSIX_PATH_MAX];
359 ENVELOPE *env = hdr->env;
360 int fromMe = mutt_addr_is_user (env->from);
362 if (!fromMe && env->reply_to && env->reply_to->mailbox)
364 else if (!fromMe && env->from && env->from->mailbox)
366 else if (env->to && env->to->mailbox)
368 else if (env->cc && env->cc->mailbox)
373 mutt_safe_path (tmp, sizeof (tmp), adr);
374 snprintf (path, pathlen, "=%s", tmp);
379 void mutt_select_fcc (char *path, ssize_t pathlen, HEADER * hdr)
382 char buf[_POSIX_PATH_MAX];
383 ENVELOPE *env = hdr->env;
385 if (mutt_addr_hook (path, pathlen, M_FCCHOOK, NULL, hdr) != 0) {
386 if ((option (OPTSAVENAME) || option (OPTFORCENAME)) &&
387 (env->to || env->cc || env->bcc)) {
388 adr = env->to ? env->to : (env->cc ? env->cc : env->bcc);
389 mutt_safe_path (buf, sizeof (buf), adr);
390 mutt_concat_path(path, pathlen, NONULL(Maildir), buf);
391 if (!option (OPTFORCENAME) && mx_access (path, W_OK) != 0)
392 m_strcpy(path, pathlen, NONULL(Outbox));
395 m_strcpy(path, pathlen, NONULL(Outbox));
397 mutt_pretty_mailbox (path);
400 static const char *_mutt_string_hook (const char *match, int hook)
404 for (; tmp; tmp = tmp->next) {
405 if ((tmp->type & hook)
406 && ((match && regexec(tmp->rx.rx, match, 0, NULL, 0) == 0) ^ tmp->rx.not))
407 return (tmp->command);
412 const char *mutt_charset_hook (const char *chs)
414 return _mutt_string_hook (chs, M_CHARSETHOOK);
417 const char *mutt_iconv_hook (const char *chs)
419 return _mutt_string_hook (chs, M_ICONVHOOK);
422 const char *mutt_crypt_hook (address_t * adr)
424 return _mutt_string_hook (adr->mailbox, M_CRYPTHOOK);
427 void mutt_account_hook (const char *url)
435 err.dsize = sizeof (buf);
438 for (hook = Hooks; hook; hook = hook->next) {
439 if (!(hook->command && (hook->type & M_ACCOUNTHOOK)))
442 if ((regexec (hook->rx.rx, url, 0, NULL, 0) == 0) ^ hook->rx.not) {
443 if (mutt_parse_rc_line (hook->command, &token, &err) == -1) {
444 mutt_error ("%s", err.data);
447 p_delete(&token.data);
448 current_hook_type = 0;
455 p_delete(&token.data);