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>
22 int type; /* hook type */
23 rx_t rx; /* regular expression */
24 char *command; /* filename, command or pattern to execute */
25 pattern_t *pattern; /* used for fcc,save,send-hook */
29 static HOOK *Hooks = NULL;
31 static unsigned long current_hook_type = 0;
33 int mutt_parse_hook (BUFFER * buf __attribute__ ((unused)), BUFFER * s,
34 unsigned long data, BUFFER * err)
37 BUFFER command, pattern;
40 pattern_t *pat = NULL;
41 char path[_POSIX_PATH_MAX];
46 if (*s->dptr == '!') {
47 s->dptr = vskipspaces(s->dptr + 1);
51 mutt_extract_token (&pattern, s, 0);
54 m_strcpy(err->data, err->dsize, _("too few arguments"));
58 mutt_extract_token (&command, s,
60 (M_FOLDERHOOK | M_SENDHOOK | M_SEND2HOOK |
61 M_ACCOUNTHOOK | M_REPLYHOOK)) ? M_TOKEN_SPACE : 0);
64 m_strcpy(err->data, err->dsize, _("too few arguments"));
69 m_strcpy(err->data, err->dsize, _("too many arguments"));
73 if (data & (M_FOLDERHOOK | M_MBOXHOOK)) {
74 m_strcpy(path, sizeof(path), pattern.data);
75 _mutt_expand_path (path, sizeof (path), 1);
76 p_delete(&pattern.data);
78 pattern.data = m_strdup(path);
80 else if (data & (M_APPENDHOOK | M_OPENHOOK | M_CLOSEHOOK)) {
81 if (mutt_test_compress_command (command.data)) {
82 m_strcpy(err->data, err->dsize, _("bad formatted command string"));
86 else if (DefaultHook && !(data & (M_CHARSETHOOK | M_ACCOUNTHOOK))
87 && !(data & M_CRYPTHOOK))
89 char tmp[HUGE_STRING];
91 m_strcpy(tmp, sizeof(tmp), pattern.data);
92 mutt_check_simple (tmp, sizeof (tmp), DefaultHook);
93 p_delete(&pattern.data);
95 pattern.data = m_strdup(tmp);
98 if (data & (M_MBOXHOOK | M_SAVEHOOK | M_FCCHOOK)) {
99 m_strcpy(path, sizeof(path), command.data);
100 mutt_expand_path (path, sizeof (path));
101 p_delete(&command.data);
102 p_clear(&command, 1);
103 command.data = m_strdup(path);
106 /* check to make sure that a matching hook doesn't already exist */
107 for (ptr = Hooks; ptr; ptr = ptr->next) {
108 if (ptr->type == (int)data &&
109 ptr->rx.not == not && !m_strcmp(pattern.data, ptr->rx.pattern)) {
111 (M_FOLDERHOOK | M_SENDHOOK | M_SEND2HOOK | M_MESSAGEHOOK |
112 M_ACCOUNTHOOK | M_REPLYHOOK)) {
113 /* these hooks allow multiple commands with the same
114 * pattern, so if we've already seen this pattern/command pair, just
115 * ignore it instead of creating a duplicate */
116 if (!m_strcmp(ptr->command, command.data)) {
117 p_delete(&command.data);
118 p_delete(&pattern.data);
123 /* other hooks only allow one command per pattern, so update the
124 * entry with the new command. this currently does not change the
125 * order of execution of the hooks, which i think is desirable since
126 * a common action to perform is to change the default (.) entry
127 * based upon some other information. */
128 p_delete(&ptr->command);
129 ptr->command = command.data;
130 p_delete(&pattern.data);
139 (M_SENDHOOK | M_SEND2HOOK | M_SAVEHOOK | M_FCCHOOK | M_MESSAGEHOOK |
142 mutt_pattern_comp (pattern.data,
143 (data & (M_SENDHOOK | M_SEND2HOOK | M_FCCHOOK)) ?
144 0 : M_FULL_MSG, err)) == NULL)
148 rx = p_new(regex_t, 1);
151 REGCOMP (rx, NONULL (pattern.data),
152 ((data & (M_CRYPTHOOK | M_CHARSETHOOK)) ? REG_ICASE : 0)))
156 REGCOMP (rx, NONULL (pattern.data),
157 (data & (M_CHARSETHOOK | M_ICONVHOOK)) ? REG_ICASE : 0)) !=
159 #endif /* M_CRYPTHOOK */
161 regerror (rc, rx, err->data, err->dsize);
169 ptr->next = p_new(HOOK, 1);
173 Hooks = ptr = p_new(HOOK, 1);
175 ptr->command = command.data;
177 ptr->rx.pattern = pattern.data;
183 p_delete(&pattern.data);
184 p_delete(&command.data);
188 static void delete_hook (HOOK * h)
190 p_delete(&h->command);
191 p_delete(&h->rx.pattern);
195 pattern_list_wipe(&h->pattern);
199 /* Deletes all hooks of type ``type'', or all defined hooks if ``type'' is 0 */
200 static void delete_hooks (long type)
205 while (h = Hooks, h && (type == 0 || type == h->type)) {
210 prev = h; /* Unused assignment to avoid compiler warnings */
213 if (type == h->type) {
214 prev->next = h->next;
223 int mutt_parse_unhook (BUFFER * buf, BUFFER * s, unsigned long data __attribute__ ((unused)),
226 while (MoreArgs (s)) {
227 mutt_extract_token (buf, s, 0);
228 if (m_strcmp("*", buf->data) == 0) {
229 if (current_hook_type) {
230 snprintf (err->data, err->dsize,
231 _("unhook: Can't do unhook * from within a hook."));
237 unsigned long type = mutt_get_hook_type (buf->data);
240 snprintf (err->data, err->dsize,
241 _("unhook: unknown hook type: %s"), buf->data);
244 if (current_hook_type == type) {
245 snprintf (err->data, err->dsize,
246 _("unhook: Can't delete a %s from within a %s."), buf->data,
256 void mutt_folder_hook (char *path)
262 current_hook_type = M_FOLDERHOOK;
265 err.dsize = sizeof (buf);
267 for (; tmp; tmp = tmp->next) {
271 if (tmp->type & M_FOLDERHOOK) {
272 if ((regexec (tmp->rx.rx, path, 0, NULL, 0) == 0) ^ tmp->rx.not) {
273 if (mutt_parse_rc_line (tmp->command, &token, &err) == -1) {
274 mutt_error ("%s", err.data);
275 mutt_sleep (1); /* pause a moment to let the user see the error */
277 p_delete(&token.data);
278 current_hook_type = 0;
285 p_delete(&token.data);
287 current_hook_type = 0;
290 char *mutt_find_hook (int type, const char *pat)
294 for (; tmp; tmp = tmp->next)
295 if (tmp->type & type) {
296 if (regexec (tmp->rx.rx, pat, 0, NULL, 0) == 0)
297 return (tmp->command);
302 void mutt_message_hook (CONTEXT * ctx, HEADER * hdr, int type)
308 current_hook_type = type;
311 err.dsize = sizeof (buf);
313 for (hook = Hooks; hook; hook = hook->next) {
317 if (hook->type & type)
318 if ((mutt_pattern_exec (hook->pattern, 0, ctx, hdr) > 0) ^ hook->rx.not)
319 if (mutt_parse_rc_line (hook->command, &token, &err) != 0) {
320 mutt_error ("%s", err.data);
323 p_delete(&token.data);
324 current_hook_type = 0;
329 p_delete(&token.data);
330 current_hook_type = 0;
334 mutt_addr_hook (char *path, ssize_t pathlen, unsigned long type, CONTEXT * ctx,
339 /* determine if a matching hook exists */
340 for (hook = Hooks; hook; hook = hook->next) {
344 if (hook->type & type)
345 if ((mutt_pattern_exec (hook->pattern, 0, ctx, hdr) > 0) ^ hook->rx.not) {
346 mutt_make_string (path, pathlen, hook->command, ctx, hdr);
354 void mutt_default_save (char *path, ssize_t pathlen, HEADER * hdr)
357 if (mutt_addr_hook (path, pathlen, M_SAVEHOOK, Context, hdr) != 0) {
358 char tmp[_POSIX_PATH_MAX];
360 ENVELOPE *env = hdr->env;
361 int fromMe = mutt_addr_is_user (env->from);
363 if (!fromMe && env->reply_to && env->reply_to->mailbox)
365 else if (!fromMe && env->from && env->from->mailbox)
367 else if (env->to && env->to->mailbox)
369 else if (env->cc && env->cc->mailbox)
374 mutt_safe_path (tmp, sizeof (tmp), adr);
375 snprintf (path, pathlen, "=%s", tmp);
380 void mutt_select_fcc (char *path, ssize_t pathlen, HEADER * hdr)
383 char buf[_POSIX_PATH_MAX];
384 ENVELOPE *env = hdr->env;
386 if (mutt_addr_hook (path, pathlen, M_FCCHOOK, NULL, hdr) != 0) {
387 if ((option (OPTSAVENAME) || option (OPTFORCENAME)) &&
388 (env->to || env->cc || env->bcc)) {
389 adr = env->to ? env->to : (env->cc ? env->cc : env->bcc);
390 mutt_safe_path (buf, sizeof (buf), adr);
391 mutt_concat_path(path, pathlen, NONULL(Maildir), buf);
392 if (!option (OPTFORCENAME) && mx_access (path, W_OK) != 0)
393 m_strcpy(path, pathlen, NONULL(Outbox));
396 m_strcpy(path, pathlen, NONULL(Outbox));
398 mutt_pretty_mailbox (path);
401 static const char *_mutt_string_hook (const char *match, int hook)
405 for (; tmp; tmp = tmp->next) {
406 if ((tmp->type & hook)
407 && ((match && regexec(tmp->rx.rx, match, 0, NULL, 0) == 0) ^ tmp->rx.not))
408 return (tmp->command);
413 const char *mutt_charset_hook (const char *chs)
415 return _mutt_string_hook (chs, M_CHARSETHOOK);
418 const char *mutt_iconv_hook (const char *chs)
420 return _mutt_string_hook (chs, M_ICONVHOOK);
423 const char *mutt_crypt_hook (address_t * adr)
425 return _mutt_string_hook (adr->mailbox, M_CRYPTHOOK);
428 void mutt_account_hook (const char *url)
436 err.dsize = sizeof (buf);
439 for (hook = Hooks; hook; hook = hook->next) {
440 if (!(hook->command && (hook->type & M_ACCOUNTHOOK)))
443 if ((regexec (hook->rx.rx, url, 0, NULL, 0) == 0) ^ hook->rx.not) {
444 if (mutt_parse_rc_line (hook->command, &token, &err) == -1) {
445 mutt_error ("%s", err.data);
448 p_delete(&token.data);
449 current_hook_type = 0;
456 p_delete(&token.data);