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-lua/lib-lua.h>
12 #include <lib-ui/curses.h>
13 #include <lib-mx/mx.h>
14 #include <lib-mx/compress.h>
20 typedef struct hook_t {
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 DO_INIT(hook_t, hook);
29 static inline void hook_wipe(hook_t *hk) {
30 p_delete(&hk->command);
31 pattern_list_wipe(&hk->pattern);
34 DO_DELETE(hook_t, hook);
35 DO_SLIST(hook_t, hook, hook_delete);
37 static hook_t *Hooks = NULL;
38 static unsigned long current_hook_type = 0;
40 int mutt_parse_hook (BUFFER * buf __attribute__ ((unused)), BUFFER * s,
41 unsigned long data, BUFFER * err)
44 BUFFER command, pattern;
47 pattern_t *pat = NULL;
48 char path[_POSIX_PATH_MAX];
53 if (*s->dptr == '!') {
54 s->dptr = vskipspaces(s->dptr + 1);
58 mutt_extract_token (&pattern, s, 0);
61 m_strcpy(err->data, err->dsize, _("too few arguments"));
65 mutt_extract_token (&command, s,
67 (M_FOLDERHOOK | M_SENDHOOK | M_SEND2HOOK |
68 M_ACCOUNTHOOK | M_REPLYHOOK)) ? M_TOKEN_SPACE : 0);
71 m_strcpy(err->data, err->dsize, _("too few arguments"));
76 m_strcpy(err->data, err->dsize, _("too many arguments"));
80 if (data & (M_FOLDERHOOK | M_MBOXHOOK)) {
81 _mutt_expand_path (path, sizeof (path), pattern.data, 1);
82 p_delete(&pattern.data);
84 pattern.data = m_strdup(path);
86 else if (data & (M_APPENDHOOK | M_OPENHOOK | M_CLOSEHOOK)) {
87 if (mutt_test_compress_command (command.data)) {
88 m_strcpy(err->data, err->dsize, _("bad formatted command string"));
92 else if (DefaultHook && !(data & M_ACCOUNTHOOK) && !(data & M_CRYPTHOOK))
94 char tmp[HUGE_STRING];
96 m_strcpy(tmp, sizeof(tmp), pattern.data);
97 mutt_check_simple (tmp, sizeof (tmp), DefaultHook);
98 p_delete(&pattern.data);
100 pattern.data = m_strdup(tmp);
103 if (data & (M_MBOXHOOK | M_SAVEHOOK | M_FCCHOOK)) {
104 m_strcpy(path, sizeof(path), command.data);
105 mutt_expand_path (path, sizeof (path));
106 p_delete(&command.data);
107 p_clear(&command, 1);
108 command.data = m_strdup(path);
111 /* check to make sure that a matching hook doesn't already exist */
112 for (ptr = Hooks; ptr; ptr = ptr->next) {
113 if (ptr->type == (int)data &&
114 ptr->rx.neg == neg && !m_strcmp(pattern.data, ptr->rx.pattern)) {
116 (M_FOLDERHOOK | M_SENDHOOK | M_SEND2HOOK | M_MESSAGEHOOK |
117 M_ACCOUNTHOOK | M_REPLYHOOK)) {
118 /* these hooks allow multiple commands with the same
119 * pattern, so if we've already seen this pattern/command pair, just
120 * ignore it instead of creating a duplicate */
121 if (!m_strcmp(ptr->command, command.data)) {
122 p_delete(&command.data);
123 p_delete(&pattern.data);
127 /* other hooks only allow one command per pattern, so update the
128 * entry with the new command. this currently does not change the
129 * order of execution of the hooks, which i think is desirable since
130 * a common action to perform is to change the default (.) entry
131 * based upon some other information. */
132 p_delete(&ptr->command);
133 ptr->command = command.data;
134 p_delete(&pattern.data);
142 if (data & (M_SENDHOOK | M_SEND2HOOK | M_SAVEHOOK | M_FCCHOOK |
143 M_MESSAGEHOOK | M_REPLYHOOK))
146 mutt_pattern_comp (pattern.data,
147 (data & (M_SENDHOOK | M_SEND2HOOK | M_FCCHOOK)) ?
148 0 : M_FULL_MSG, err)) == NULL)
151 rx = p_new(regex_t, 1);
152 if ((rc = REGCOMP(rx, NONULL(pattern.data),
153 ((data & M_CRYPTHOOK) ? REG_ICASE : 0))) != 0)
155 regerror (rc, rx, err->data, err->dsize);
163 ptr->next = p_new(hook_t, 1);
166 Hooks = ptr = p_new(hook_t, 1);
169 ptr->command = command.data;
171 ptr->rx.pattern = pattern.data;
177 p_delete(&pattern.data);
178 p_delete(&command.data);
182 /* Deletes all hooks of type ``type'', or all defined hooks if ``type'' is 0 */
183 static void delete_hooks (long type)
188 if ((*l)->type == type) {
189 hook_t *tmp = hook_list_pop(l);
197 int mutt_parse_unhook (BUFFER * buf, BUFFER * s, unsigned long data __attribute__ ((unused)),
200 while (MoreArgs (s)) {
201 mutt_extract_token (buf, s, 0);
202 if (m_strcmp("*", buf->data) == 0) {
203 if (current_hook_type) {
204 snprintf (err->data, err->dsize,
205 _("unhook: Can't do unhook * from within a hook."));
208 hook_list_wipe(&Hooks);
210 unsigned long type = mutt_get_hook_type (buf->data);
213 snprintf (err->data, err->dsize,
214 _("unhook: unknown hook type: %s"), buf->data);
217 if (current_hook_type == type) {
218 snprintf (err->data, err->dsize,
219 _("unhook: Can't delete a %s from within a %s."), buf->data,
229 void mutt_folder_hook (char *path)
235 current_hook_type = M_FOLDERHOOK;
238 err.dsize = sizeof (buf);
240 for (; tmp; tmp = tmp->next) {
244 if (tmp->type & M_FOLDERHOOK) {
245 if ((regexec (tmp->rx.rx, path, 0, NULL, 0) == 0) ^ tmp->rx.neg) {
246 if (mutt_parse_rc_line (tmp->command, &token, &err) == -1) {
247 mutt_error ("%s", err.data);
248 mutt_sleep (1); /* pause a moment to let the user see the error */
253 p_delete(&token.data);
255 current_hook_type = 0;
258 lua_State *L = luaM_getruntime();
259 lua_getfield(L, LUA_GLOBALSINDEX, "MCore");
260 lua_getfield(L, -1, "folder_hook");
262 if (lua_isfunction(L, -1)) {
263 lua_pushstring(L, LastFolder);
264 lua_pushstring(L, CurrentFolder);
265 lua_pcall(L, 2, 0, 0);
272 char *mutt_find_hook (int type, const char *pat)
276 for (; tmp; tmp = tmp->next)
277 if (tmp->type & type) {
278 if (regexec (tmp->rx.rx, pat, 0, NULL, 0) == 0)
279 return (tmp->command);
284 void mutt_message_hook (CONTEXT * ctx, HEADER * hdr, int type)
290 current_hook_type = type;
293 err.dsize = sizeof (buf);
295 for (hook = Hooks; hook; hook = hook->next) {
299 if (hook->type & type)
300 if ((mutt_pattern_exec (hook->pattern, 0, ctx, hdr) > 0) ^ hook->rx.neg)
301 if (mutt_parse_rc_line (hook->command, &token, &err) != 0) {
302 mutt_error ("%s", err.data);
306 p_delete(&token.data);
307 current_hook_type = 0;
311 mutt_addr_hook (char *path, ssize_t pathlen, unsigned long type, CONTEXT * ctx,
316 /* determine if a matching hook exists */
317 for (hook = Hooks; hook; hook = hook->next) {
321 if (hook->type & type)
322 if ((mutt_pattern_exec (hook->pattern, 0, ctx, hdr) > 0) ^ hook->rx.neg) {
323 mutt_make_string (path, pathlen, hook->command, ctx, hdr);
331 void mutt_default_save (char *path, ssize_t pathlen, HEADER * hdr)
334 if (mutt_addr_hook (path, pathlen, M_SAVEHOOK, Context, hdr) != 0) {
335 char tmp[_POSIX_PATH_MAX];
337 ENVELOPE *env = hdr->env;
338 int fromMe = mutt_addr_is_user (env->from);
340 if (!fromMe && env->reply_to && env->reply_to->mailbox)
342 else if (!fromMe && env->from && env->from->mailbox)
344 else if (env->to && env->to->mailbox)
346 else if (env->cc && env->cc->mailbox)
351 mutt_safe_path (tmp, sizeof (tmp), adr);
352 snprintf (path, pathlen, "=%s", tmp);
357 void mutt_select_fcc (char *path, ssize_t pathlen, HEADER * hdr)
360 char buf[_POSIX_PATH_MAX];
361 ENVELOPE *env = hdr->env;
363 if (mutt_addr_hook (path, pathlen, M_FCCHOOK, NULL, hdr) != 0) {
364 if ((option (OPTSAVENAME) || option (OPTFORCENAME)) &&
365 (env->to || env->cc || env->bcc)) {
366 adr = env->to ? env->to : (env->cc ? env->cc : env->bcc);
367 mutt_safe_path (buf, sizeof (buf), adr);
368 mutt_concat_path(path, pathlen, NONULL(Maildir), buf);
369 if (!option (OPTFORCENAME) && mx_access (path, W_OK) != 0)
370 m_strcpy(path, pathlen, NONULL(MAlias.record));
373 m_strcpy(path, pathlen, NONULL(MAlias.record));
375 mutt_pretty_mailbox (path);
378 static const char *_mutt_string_hook (const char *match, int hook)
382 for (; tmp; tmp = tmp->next) {
383 if ((tmp->type & hook)
384 && ((match && regexec(tmp->rx.rx, match, 0, NULL, 0) == 0) ^ tmp->rx.neg))
385 return (tmp->command);
390 const char *mutt_crypt_hook (address_t * adr)
392 return _mutt_string_hook (adr->mailbox, M_CRYPTHOOK);
395 void mutt_account_hook (const char *url)
403 err.dsize = sizeof (buf);
406 for (hook = Hooks; hook; hook = hook->next) {
407 if (!(hook->command && (hook->type & M_ACCOUNTHOOK)))
410 if ((regexec (hook->rx.rx, url, 0, NULL, 0) == 0) ^ hook->rx.neg) {
411 if (mutt_parse_rc_line (hook->command, &token, &err) == -1) {
412 mutt_error ("%s", err.data);
418 p_delete(&token.data);