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.
16 #include "mutt_crypt.h"
29 int type; /* hook type */
30 REGEXP rx; /* regular expression */
31 char *command; /* filename, command or pattern to execute */
32 pattern_t *pattern; /* used for fcc,save,send-hook */
36 static HOOK *Hooks = NULL;
38 static int current_hook_type = 0;
40 int mutt_parse_hook (BUFFER * buf, BUFFER * s, unsigned long data,
44 BUFFER command, pattern;
47 pattern_t *pat = NULL;
48 char path[_POSIX_PATH_MAX];
50 memset (&pattern, 0, sizeof (pattern));
51 memset (&command, 0, sizeof (command));
53 if (*s->dptr == '!') {
59 mutt_extract_token (&pattern, s, 0);
62 strfcpy (err->data, _("too few arguments"), err->dsize);
66 mutt_extract_token (&command, s,
68 (M_FOLDERHOOK | M_SENDHOOK | M_SEND2HOOK |
69 M_ACCOUNTHOOK | M_REPLYHOOK)) ? M_TOKEN_SPACE : 0);
72 strfcpy (err->data, _("too few arguments"), err->dsize);
77 strfcpy (err->data, _("too many arguments"), err->dsize);
81 if (data & (M_FOLDERHOOK | M_MBOXHOOK)) {
82 strfcpy (path, pattern.data, sizeof (path));
83 _mutt_expand_path (path, sizeof (path), 1);
85 memset (&pattern, 0, sizeof (pattern));
86 pattern.data = safe_strdup (path);
89 else if (data & (M_APPENDHOOK | M_OPENHOOK | M_CLOSEHOOK)) {
90 if (mutt_test_compress_command (command.data)) {
91 strfcpy (err->data, _("bad formatted command string"), err->dsize);
96 else if (DefaultHook && !(data & (M_CHARSETHOOK | M_ACCOUNTHOOK))
97 && (!WithCrypto || !(data & M_CRYPTHOOK))
99 char tmp[HUGE_STRING];
101 strfcpy (tmp, pattern.data, sizeof (tmp));
102 mutt_check_simple (tmp, sizeof (tmp), DefaultHook);
103 FREE (&pattern.data);
104 memset (&pattern, 0, sizeof (pattern));
105 pattern.data = safe_strdup (tmp);
108 if (data & (M_MBOXHOOK | M_SAVEHOOK | M_FCCHOOK)) {
109 strfcpy (path, command.data, sizeof (path));
110 mutt_expand_path (path, sizeof (path));
111 FREE (&command.data);
112 memset (&command, 0, sizeof (command));
113 command.data = safe_strdup (path);
116 /* check to make sure that a matching hook doesn't already exist */
117 for (ptr = Hooks; ptr; ptr = ptr->next) {
118 if (ptr->type == data &&
119 ptr->rx.not == not && !mutt_strcmp (pattern.data, ptr->rx.pattern)) {
121 (M_FOLDERHOOK | M_SENDHOOK | M_SEND2HOOK | M_MESSAGEHOOK |
122 M_ACCOUNTHOOK | M_REPLYHOOK)) {
123 /* these hooks allow multiple commands with the same
124 * pattern, so if we've already seen this pattern/command pair, just
125 * ignore it instead of creating a duplicate */
126 if (!mutt_strcmp (ptr->command, command.data)) {
127 FREE (&command.data);
128 FREE (&pattern.data);
133 /* other hooks only allow one command per pattern, so update the
134 * entry with the new command. this currently does not change the
135 * order of execution of the hooks, which i think is desirable since
136 * a common action to perform is to change the default (.) entry
137 * based upon some other information. */
138 FREE (&ptr->command);
139 ptr->command = command.data;
140 FREE (&pattern.data);
149 (M_SENDHOOK | M_SEND2HOOK | M_SAVEHOOK | M_FCCHOOK | M_MESSAGEHOOK |
152 mutt_pattern_comp (pattern.data,
153 (data & (M_SENDHOOK | M_SEND2HOOK | M_FCCHOOK)) ?
154 0 : M_FULL_MSG, err)) == NULL)
158 rx = safe_malloc (sizeof (regex_t));
161 REGCOMP (rx, NONULL (pattern.data),
162 ((data & (M_CRYPTHOOK | M_CHARSETHOOK)) ? REG_ICASE : 0)))
166 REGCOMP (rx, NONULL (pattern.data),
167 (data & (M_CHARSETHOOK | M_ICONVHOOK)) ? REG_ICASE : 0)) !=
169 #endif /* M_CRYPTHOOK */
171 regerror (rc, rx, err->data, err->dsize);
179 ptr->next = safe_calloc (1, sizeof (HOOK));
183 Hooks = ptr = safe_calloc (1, sizeof (HOOK));
185 ptr->command = command.data;
187 ptr->rx.pattern = pattern.data;
193 FREE (&pattern.data);
194 FREE (&command.data);
198 static void delete_hook (HOOK * h)
201 FREE (&h->rx.pattern);
205 mutt_pattern_free (&h->pattern);
209 /* Deletes all hooks of type ``type'', or all defined hooks if ``type'' is 0 */
210 static void delete_hooks (int type)
215 while (h = Hooks, h && (type == 0 || type == h->type)) {
220 prev = h; /* Unused assignment to avoid compiler warnings */
223 if (type == h->type) {
224 prev->next = h->next;
233 int mutt_parse_unhook (BUFFER * buf, BUFFER * s, unsigned long data,
236 while (MoreArgs (s)) {
237 mutt_extract_token (buf, s, 0);
238 if (mutt_strcmp ("*", buf->data) == 0) {
239 if (current_hook_type) {
240 snprintf (err->data, err->dsize,
241 _("unhook: Can't do unhook * from within a hook."));
247 int type = mutt_get_hook_type (buf->data);
250 snprintf (err->data, err->dsize,
251 _("unhook: unknown hook type: %s"), buf->data);
254 if (current_hook_type == type) {
255 snprintf (err->data, err->dsize,
256 _("unhook: Can't delete a %s from within a %s."), buf->data,
266 void mutt_folder_hook (char *path)
272 current_hook_type = M_FOLDERHOOK;
275 err.dsize = sizeof (buf);
276 memset (&token, 0, sizeof (token));
277 for (; tmp; tmp = tmp->next) {
281 if (tmp->type & M_FOLDERHOOK) {
282 if ((regexec (tmp->rx.rx, path, 0, NULL, 0) == 0) ^ tmp->rx.not) {
283 if (mutt_parse_rc_line (tmp->command, &token, &err) == -1) {
284 mutt_error ("%s", err.data);
286 mutt_sleep (1); /* pause a moment to let the user see the error */
287 current_hook_type = 0;
295 current_hook_type = 0;
298 char *mutt_find_hook (int type, const char *pat)
302 for (; tmp; tmp = tmp->next)
303 if (tmp->type & type) {
304 if (regexec (tmp->rx.rx, pat, 0, NULL, 0) == 0)
305 return (tmp->command);
310 void mutt_message_hook (CONTEXT * ctx, HEADER * hdr, int type)
316 current_hook_type = type;
319 err.dsize = sizeof (buf);
320 memset (&token, 0, sizeof (token));
321 for (hook = Hooks; hook; hook = hook->next) {
325 if (hook->type & type)
326 if ((mutt_pattern_exec (hook->pattern, 0, ctx, hdr) > 0) ^ hook->rx.not)
327 if (mutt_parse_rc_line (hook->command, &token, &err) != 0) {
329 mutt_error ("%s", err.data);
331 current_hook_type = 0;
336 current_hook_type = 0;
340 mutt_addr_hook (char *path, size_t pathlen, int type, CONTEXT * ctx,
345 /* determine if a matching hook exists */
346 for (hook = Hooks; hook; hook = hook->next) {
350 if (hook->type & type)
351 if ((mutt_pattern_exec (hook->pattern, 0, ctx, hdr) > 0) ^ hook->rx.not) {
352 mutt_make_string (path, pathlen, hook->command, ctx, hdr);
360 void mutt_default_save (char *path, size_t pathlen, HEADER * hdr)
363 if (mutt_addr_hook (path, pathlen, M_SAVEHOOK, Context, hdr) != 0) {
364 char tmp[_POSIX_PATH_MAX];
366 ENVELOPE *env = hdr->env;
367 int fromMe = mutt_addr_is_user (env->from);
369 if (!fromMe && env->reply_to && env->reply_to->mailbox)
371 else if (!fromMe && env->from && env->from->mailbox)
373 else if (env->to && env->to->mailbox)
375 else if (env->cc && env->cc->mailbox)
380 mutt_safe_path (tmp, sizeof (tmp), adr);
381 snprintf (path, pathlen, "=%s", tmp);
386 void mutt_select_fcc (char *path, size_t pathlen, HEADER * hdr)
389 char buf[_POSIX_PATH_MAX];
390 ENVELOPE *env = hdr->env;
392 if (mutt_addr_hook (path, pathlen, M_FCCHOOK, NULL, hdr) != 0) {
393 if ((option (OPTSAVENAME) || option (OPTFORCENAME)) &&
394 (env->to || env->cc || env->bcc)) {
395 adr = env->to ? env->to : (env->cc ? env->cc : env->bcc);
396 mutt_safe_path (buf, sizeof (buf), adr);
397 mutt_concat_path (path, NONULL (Maildir), buf, pathlen);
398 if (!option (OPTFORCENAME) && mx_access (path, W_OK) != 0)
399 strfcpy (path, NONULL (Outbox), pathlen);
402 strfcpy (path, NONULL (Outbox), pathlen);
404 mutt_pretty_mailbox (path);
407 static char *_mutt_string_hook (const char *match, int hook)
411 for (; tmp; tmp = tmp->next) {
412 if ((tmp->type & hook) && ((match &&
413 regexec (tmp->rx.rx, match, 0, NULL,
414 0) == 0) ^ tmp->rx.not))
415 return (tmp->command);
420 char *mutt_charset_hook (const char *chs)
422 return _mutt_string_hook (chs, M_CHARSETHOOK);
425 char *mutt_iconv_hook (const char *chs)
427 return _mutt_string_hook (chs, M_ICONVHOOK);
430 char *mutt_crypt_hook (ADDRESS * adr)
432 return _mutt_string_hook (adr->mailbox, M_CRYPTHOOK);
436 void mutt_account_hook (const char *url)
444 err.dsize = sizeof (buf);
445 memset (&token, 0, sizeof (token));
447 for (hook = Hooks; hook; hook = hook->next) {
448 if (!(hook->command && (hook->type & M_ACCOUNTHOOK)))
451 if ((regexec (hook->rx.rx, url, 0, NULL, 0) == 0) ^ hook->rx.not) {
452 if (mutt_parse_rc_line (hook->command, &token, &err) == -1) {
454 mutt_error ("%s", err.data);