2 * Copyright (C) 1996-2002,2004,2007 Michael R. Elkins <me@mutt.org>, and others
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 #include "mutt_crypt.h"
35 int type; /* hook type */
36 REGEXP 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 int current_hook_type = 0;
46 int mutt_parse_hook (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
49 BUFFER command, pattern;
52 pattern_t *pat = NULL;
53 char path[_POSIX_PATH_MAX];
55 memset (&pattern, 0, sizeof (pattern));
56 memset (&command, 0, sizeof (command));
65 mutt_extract_token (&pattern, s, 0);
69 strfcpy (err->data, _("too few arguments"), err->dsize);
73 mutt_extract_token (&command, s, (data & (M_FOLDERHOOK | M_SENDHOOK | M_SEND2HOOK | M_ACCOUNTHOOK | M_REPLYHOOK)) ? M_TOKEN_SPACE : 0);
77 strfcpy (err->data, _("too few arguments"), err->dsize);
83 strfcpy (err->data, _("too many arguments"), err->dsize);
87 if (data & (M_FOLDERHOOK | M_MBOXHOOK))
89 strfcpy (path, pattern.data, sizeof (path));
90 _mutt_expand_path (path, sizeof (path), 1);
92 memset (&pattern, 0, sizeof (pattern));
93 pattern.data = safe_strdup (path);
95 else if (DefaultHook && !(data & (M_CHARSETHOOK | M_ICONVHOOK | M_ACCOUNTHOOK))
96 && (!WithCrypto || !(data & M_CRYPTHOOK))
99 char tmp[HUGE_STRING];
101 /* At this stage remain only message-hooks, reply-hooks, send-hooks,
102 * send2-hooks, save-hooks, and fcc-hooks: All those allowing full
103 * patterns. If given a simple regexp, we expand $default_hook.
105 strfcpy (tmp, pattern.data, sizeof (tmp));
106 mutt_check_simple (tmp, sizeof (tmp), DefaultHook);
107 FREE (&pattern.data);
108 memset (&pattern, 0, sizeof (pattern));
109 pattern.data = safe_strdup (tmp);
112 if (data & (M_MBOXHOOK | M_SAVEHOOK | M_FCCHOOK))
114 strfcpy (path, command.data, sizeof (path));
115 mutt_expand_path (path, sizeof (path));
116 FREE (&command.data);
117 memset (&command, 0, sizeof (command));
118 command.data = safe_strdup (path);
121 /* check to make sure that a matching hook doesn't already exist */
122 for (ptr = Hooks; ptr; ptr = ptr->next)
124 if (ptr->type == data &&
125 ptr->rx.not == not &&
126 !mutt_strcmp (pattern.data, ptr->rx.pattern))
128 if (data & (M_FOLDERHOOK | M_SENDHOOK | M_SEND2HOOK | M_MESSAGEHOOK | M_ACCOUNTHOOK | M_REPLYHOOK))
130 /* these hooks allow multiple commands with the same
131 * pattern, so if we've already seen this pattern/command pair, just
132 * ignore it instead of creating a duplicate */
133 if (!mutt_strcmp (ptr->command, command.data))
135 FREE (&command.data);
136 FREE (&pattern.data);
142 /* other hooks only allow one command per pattern, so update the
143 * entry with the new command. this currently does not change the
144 * order of execution of the hooks, which i think is desirable since
145 * a common action to perform is to change the default (.) entry
146 * based upon some other information. */
147 FREE (&ptr->command);
148 ptr->command = command.data;
149 FREE (&pattern.data);
157 if (data & (M_SENDHOOK | M_SEND2HOOK | M_SAVEHOOK | M_FCCHOOK | M_MESSAGEHOOK | M_REPLYHOOK))
159 if ((pat = mutt_pattern_comp (pattern.data,
160 (data & (M_SENDHOOK | M_SEND2HOOK | M_FCCHOOK)) ? 0 : M_FULL_MSG,
166 /* Hooks not allowing full patterns: Check syntax of regexp */
167 rx = safe_malloc (sizeof (regex_t));
169 if ((rc = REGCOMP (rx, NONULL(pattern.data), ((data & (M_CRYPTHOOK|M_CHARSETHOOK|M_ICONVHOOK)) ? REG_ICASE : 0))) != 0)
171 if ((rc = REGCOMP (rx, NONULL(pattern.data), (data & (M_CHARSETHOOK|M_ICONVHOOK)) ? REG_ICASE : 0)) != 0)
172 #endif /* M_CRYPTHOOK */
174 regerror (rc, rx, err->data, err->dsize);
182 ptr->next = safe_calloc (1, sizeof (HOOK));
186 Hooks = ptr = safe_calloc (1, sizeof (HOOK));
188 ptr->command = command.data;
190 ptr->rx.pattern = pattern.data;
196 FREE (&pattern.data);
197 FREE (&command.data);
201 static void delete_hook (HOOK *h)
204 FREE (&h->rx.pattern);
209 mutt_pattern_free (&h->pattern);
213 /* Deletes all hooks of type ``type'', or all defined hooks if ``type'' is 0 */
214 static void delete_hooks (int type)
219 while (h = Hooks, h && (type == 0 || type == h->type))
225 prev = h; /* Unused assignment to avoid compiler warnings */
231 prev->next = h->next;
240 int mutt_parse_unhook (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
244 mutt_extract_token (buf, s, 0);
245 if (mutt_strcmp ("*", buf->data) == 0)
247 if (current_hook_type)
249 snprintf (err->data, err->dsize,
250 _("unhook: Can't do unhook * from within a hook."));
257 int type = mutt_get_hook_type (buf->data);
261 snprintf (err->data, err->dsize,
262 _("unhook: unknown hook type: %s"), buf->data);
265 if (current_hook_type == type)
267 snprintf (err->data, err->dsize,
268 _("unhook: Can't delete a %s from within a %s."),
269 buf->data, buf->data);
278 void mutt_folder_hook (char *path)
284 current_hook_type = M_FOLDERHOOK;
287 err.dsize = sizeof (buf);
288 memset (&token, 0, sizeof (token));
289 for (; tmp; tmp = tmp->next)
294 if (tmp->type & M_FOLDERHOOK)
296 if ((regexec (tmp->rx.rx, path, 0, NULL, 0) == 0) ^ tmp->rx.not)
298 if (mutt_parse_rc_line (tmp->command, &token, &err) == -1)
300 mutt_error ("%s", err.data);
302 mutt_sleep (1); /* pause a moment to let the user see the error */
303 current_hook_type = 0;
311 current_hook_type = 0;
314 char *mutt_find_hook (int type, const char *pat)
318 for (; tmp; tmp = tmp->next)
319 if (tmp->type & type)
321 if (regexec (tmp->rx.rx, pat, 0, NULL, 0) == 0)
322 return (tmp->command);
327 void mutt_message_hook (CONTEXT *ctx, HEADER *hdr, int type)
333 current_hook_type = type;
336 err.dsize = sizeof (buf);
337 memset (&token, 0, sizeof (token));
338 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 if (mutt_parse_rc_line (hook->command, &token, &err) != 0)
348 mutt_error ("%s", err.data);
350 current_hook_type = 0;
355 current_hook_type = 0;
359 mutt_addr_hook (char *path, size_t pathlen, int type, CONTEXT *ctx, HEADER *hdr)
363 /* determine if a matching hook exists */
364 for (hook = Hooks; hook; hook = hook->next)
369 if (hook->type & type)
370 if ((mutt_pattern_exec (hook->pattern, 0, ctx, hdr) > 0) ^ hook->rx.not)
372 mutt_make_string (path, pathlen, hook->command, ctx, hdr);
380 void mutt_default_save (char *path, size_t pathlen, HEADER *hdr)
383 if (mutt_addr_hook (path, pathlen, M_SAVEHOOK, Context, hdr) != 0)
385 char tmp[_POSIX_PATH_MAX];
387 ENVELOPE *env = hdr->env;
388 int fromMe = mutt_addr_is_user (env->from);
390 if (!fromMe && env->reply_to && env->reply_to->mailbox)
392 else if (!fromMe && env->from && env->from->mailbox)
394 else if (env->to && env->to->mailbox)
396 else if (env->cc && env->cc->mailbox)
402 mutt_safe_path (tmp, sizeof (tmp), adr);
403 snprintf (path, pathlen, "=%s", tmp);
408 void mutt_select_fcc (char *path, size_t pathlen, HEADER *hdr)
411 char buf[_POSIX_PATH_MAX];
412 ENVELOPE *env = hdr->env;
414 if (mutt_addr_hook (path, pathlen, M_FCCHOOK, NULL, hdr) != 0)
416 if ((option (OPTSAVENAME) || option (OPTFORCENAME)) &&
417 (env->to || env->cc || env->bcc))
419 adr = env->to ? env->to : (env->cc ? env->cc : env->bcc);
420 mutt_safe_path (buf, sizeof (buf), adr);
421 mutt_concat_path (path, NONULL(Maildir), buf, pathlen);
422 if (!option (OPTFORCENAME) && mx_access (path, W_OK) != 0)
423 strfcpy (path, NONULL (Outbox), pathlen);
426 strfcpy (path, NONULL (Outbox), pathlen);
428 mutt_pretty_mailbox (path, pathlen);
431 static char *_mutt_string_hook (const char *match, int hook)
435 for (; tmp; tmp = tmp->next)
437 if ((tmp->type & hook) && ((match &&
438 regexec (tmp->rx.rx, match, 0, NULL, 0) == 0) ^ tmp->rx.not))
439 return (tmp->command);
444 char *mutt_charset_hook (const char *chs)
446 return _mutt_string_hook (chs, M_CHARSETHOOK);
449 char *mutt_iconv_hook (const char *chs)
451 return _mutt_string_hook (chs, M_ICONVHOOK);
454 char *mutt_crypt_hook (ADDRESS *adr)
456 return _mutt_string_hook (adr->mailbox, M_CRYPTHOOK);
460 void mutt_account_hook (const char* url)
462 /* parsing commands with URLs in an account hook can cause a recursive
463 * call. We just skip processing if this occurs. Typically such commands
464 * belong in a folder-hook -- perhaps we should warn the user. */
465 static int inhook = 0;
476 err.dsize = sizeof (buf);
477 memset (&token, 0, sizeof (token));
479 for (hook = Hooks; hook; hook = hook->next)
481 if (! (hook->command && (hook->type & M_ACCOUNTHOOK)))
484 if ((regexec (hook->rx.rx, url, 0, NULL, 0) == 0) ^ hook->rx.not)
488 if (mutt_parse_rc_line (hook->command, &token, &err) == -1)
491 mutt_error ("%s", err.data);