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.
17 #include "mutt_crypt.h"
37 int type; /* hook type */
38 rx_t rx; /* regular expression */
39 char *command; /* filename, command or pattern to execute */
40 pattern_t *pattern; /* used for fcc,save,send-hook */
44 static HOOK *Hooks = NULL;
46 static int current_hook_type = 0;
48 int mutt_parse_hook (BUFFER * buf, BUFFER * s, unsigned long data,
52 BUFFER command, pattern;
55 pattern_t *pat = NULL;
56 char path[_POSIX_PATH_MAX];
58 memset (&pattern, 0, sizeof (pattern));
59 memset (&command, 0, sizeof (command));
61 if (*s->dptr == '!') {
67 mutt_extract_token (&pattern, s, 0);
70 strfcpy (err->data, _("too few arguments"), err->dsize);
74 mutt_extract_token (&command, s,
76 (M_FOLDERHOOK | M_SENDHOOK | M_SEND2HOOK |
77 M_ACCOUNTHOOK | M_REPLYHOOK)) ? M_TOKEN_SPACE : 0);
80 strfcpy (err->data, _("too few arguments"), err->dsize);
85 strfcpy (err->data, _("too many arguments"), err->dsize);
89 if (data & (M_FOLDERHOOK | M_MBOXHOOK)) {
90 strfcpy (path, pattern.data, sizeof (path));
91 _mutt_expand_path (path, sizeof (path), 1);
92 mem_free (&pattern.data);
93 memset (&pattern, 0, sizeof (pattern));
94 pattern.data = str_dup (path);
97 else if (data & (M_APPENDHOOK | M_OPENHOOK | M_CLOSEHOOK)) {
98 if (mutt_test_compress_command (command.data)) {
99 strfcpy (err->data, _("bad formatted command string"), err->dsize);
104 else if (DefaultHook && !(data & (M_CHARSETHOOK | M_ACCOUNTHOOK))
105 && (!WithCrypto || !(data & M_CRYPTHOOK))
107 char tmp[HUGE_STRING];
109 strfcpy (tmp, pattern.data, sizeof (tmp));
110 mutt_check_simple (tmp, sizeof (tmp), DefaultHook);
111 mem_free (&pattern.data);
112 memset (&pattern, 0, sizeof (pattern));
113 pattern.data = str_dup (tmp);
116 if (data & (M_MBOXHOOK | M_SAVEHOOK | M_FCCHOOK)) {
117 strfcpy (path, command.data, sizeof (path));
118 mutt_expand_path (path, sizeof (path));
119 mem_free (&command.data);
120 memset (&command, 0, sizeof (command));
121 command.data = str_dup (path);
124 /* check to make sure that a matching hook doesn't already exist */
125 for (ptr = Hooks; ptr; ptr = ptr->next) {
126 if (ptr->type == data &&
127 ptr->rx.not == not && !str_cmp (pattern.data, ptr->rx.pattern)) {
129 (M_FOLDERHOOK | M_SENDHOOK | M_SEND2HOOK | M_MESSAGEHOOK |
130 M_ACCOUNTHOOK | M_REPLYHOOK)) {
131 /* these hooks allow multiple commands with the same
132 * pattern, so if we've already seen this pattern/command pair, just
133 * ignore it instead of creating a duplicate */
134 if (!str_cmp (ptr->command, command.data)) {
135 mem_free (&command.data);
136 mem_free (&pattern.data);
141 /* other hooks only allow one command per pattern, so update the
142 * entry with the new command. this currently does not change the
143 * order of execution of the hooks, which i think is desirable since
144 * a common action to perform is to change the default (.) entry
145 * based upon some other information. */
146 mem_free (&ptr->command);
147 ptr->command = command.data;
148 mem_free (&pattern.data);
157 (M_SENDHOOK | M_SEND2HOOK | M_SAVEHOOK | M_FCCHOOK | M_MESSAGEHOOK |
160 mutt_pattern_comp (pattern.data,
161 (data & (M_SENDHOOK | M_SEND2HOOK | M_FCCHOOK)) ?
162 0 : M_FULL_MSG, err)) == NULL)
166 rx = mem_malloc (sizeof (regex_t));
169 REGCOMP (rx, NONULL (pattern.data),
170 ((data & (M_CRYPTHOOK | M_CHARSETHOOK)) ? REG_ICASE : 0)))
174 REGCOMP (rx, NONULL (pattern.data),
175 (data & (M_CHARSETHOOK | M_ICONVHOOK)) ? REG_ICASE : 0)) !=
177 #endif /* M_CRYPTHOOK */
179 regerror (rc, rx, err->data, err->dsize);
187 ptr->next = mem_calloc (1, sizeof (HOOK));
191 Hooks = ptr = mem_calloc (1, sizeof (HOOK));
193 ptr->command = command.data;
195 ptr->rx.pattern = pattern.data;
201 mem_free (&pattern.data);
202 mem_free (&command.data);
206 static void delete_hook (HOOK * h)
208 mem_free (&h->command);
209 mem_free (&h->rx.pattern);
213 mutt_pattern_free (&h->pattern);
217 /* Deletes all hooks of type ``type'', or all defined hooks if ``type'' is 0 */
218 static void delete_hooks (int type)
223 while (h = Hooks, h && (type == 0 || type == h->type)) {
228 prev = h; /* Unused assignment to avoid compiler warnings */
231 if (type == h->type) {
232 prev->next = h->next;
241 int mutt_parse_unhook (BUFFER * buf, BUFFER * s, unsigned long data,
244 while (MoreArgs (s)) {
245 mutt_extract_token (buf, s, 0);
246 if (str_cmp ("*", buf->data) == 0) {
247 if (current_hook_type) {
248 snprintf (err->data, err->dsize,
249 _("unhook: Can't do unhook * from within a hook."));
255 int type = mutt_get_hook_type (buf->data);
258 snprintf (err->data, err->dsize,
259 _("unhook: unknown hook type: %s"), buf->data);
262 if (current_hook_type == type) {
263 snprintf (err->data, err->dsize,
264 _("unhook: Can't delete a %s from within a %s."), buf->data,
274 void mutt_folder_hook (char *path)
280 current_hook_type = M_FOLDERHOOK;
283 err.dsize = sizeof (buf);
284 memset (&token, 0, sizeof (token));
285 for (; tmp; tmp = tmp->next) {
289 if (tmp->type & M_FOLDERHOOK) {
290 if ((regexec (tmp->rx.rx, path, 0, NULL, 0) == 0) ^ tmp->rx.not) {
291 if (mutt_parse_rc_line (tmp->command, &token, &err) == -1) {
292 mutt_error ("%s", err.data);
293 mutt_sleep (1); /* pause a moment to let the user see the error */
295 mem_free (&token.data);
296 current_hook_type = 0;
303 mem_free (&token.data);
305 current_hook_type = 0;
308 char *mutt_find_hook (int type, const char *pat)
312 for (; tmp; tmp = tmp->next)
313 if (tmp->type & type) {
314 if (regexec (tmp->rx.rx, pat, 0, NULL, 0) == 0)
315 return (tmp->command);
320 void mutt_message_hook (CONTEXT * ctx, HEADER * hdr, int type)
326 current_hook_type = type;
329 err.dsize = sizeof (buf);
330 memset (&token, 0, sizeof (token));
331 for (hook = Hooks; hook; hook = hook->next) {
335 if (hook->type & type)
336 if ((mutt_pattern_exec (hook->pattern, 0, ctx, hdr) > 0) ^ hook->rx.not)
337 if (mutt_parse_rc_line (hook->command, &token, &err) != 0) {
338 mutt_error ("%s", err.data);
341 mem_free (&token.data);
342 current_hook_type = 0;
347 mem_free (&token.data);
348 current_hook_type = 0;
352 mutt_addr_hook (char *path, size_t pathlen, int type, CONTEXT * ctx,
357 /* determine if a matching hook exists */
358 for (hook = Hooks; hook; hook = hook->next) {
362 if (hook->type & type)
363 if ((mutt_pattern_exec (hook->pattern, 0, ctx, hdr) > 0) ^ hook->rx.not) {
364 mutt_make_string (path, pathlen, hook->command, ctx, hdr);
372 void mutt_default_save (char *path, size_t pathlen, HEADER * hdr)
375 if (mutt_addr_hook (path, pathlen, M_SAVEHOOK, Context, hdr) != 0) {
376 char tmp[_POSIX_PATH_MAX];
378 ENVELOPE *env = hdr->env;
379 int fromMe = mutt_addr_is_user (env->from);
381 if (!fromMe && env->reply_to && env->reply_to->mailbox)
383 else if (!fromMe && env->from && env->from->mailbox)
385 else if (env->to && env->to->mailbox)
387 else if (env->cc && env->cc->mailbox)
392 mutt_safe_path (tmp, sizeof (tmp), adr);
393 snprintf (path, pathlen, "=%s", tmp);
398 void mutt_select_fcc (char *path, size_t pathlen, HEADER * hdr)
401 char buf[_POSIX_PATH_MAX];
402 ENVELOPE *env = hdr->env;
404 if (mutt_addr_hook (path, pathlen, M_FCCHOOK, NULL, hdr) != 0) {
405 if ((option (OPTSAVENAME) || option (OPTFORCENAME)) &&
406 (env->to || env->cc || env->bcc)) {
407 adr = env->to ? env->to : (env->cc ? env->cc : env->bcc);
408 mutt_safe_path (buf, sizeof (buf), adr);
409 mutt_concat_path (path, NONULL (Maildir), buf, pathlen);
410 if (!option (OPTFORCENAME) && mx_access (path, W_OK) != 0)
411 strfcpy (path, NONULL (Outbox), pathlen);
414 strfcpy (path, NONULL (Outbox), pathlen);
416 mutt_pretty_mailbox (path);
419 static char *_mutt_string_hook (const char *match, int hook)
423 for (; tmp; tmp = tmp->next) {
424 if ((tmp->type & hook) && ((match &&
425 regexec (tmp->rx.rx, match, 0, NULL,
426 0) == 0) ^ tmp->rx.not))
427 return (tmp->command);
432 char *mutt_charset_hook (const char *chs)
434 return _mutt_string_hook (chs, M_CHARSETHOOK);
437 char *mutt_iconv_hook (const char *chs)
439 return _mutt_string_hook (chs, M_ICONVHOOK);
442 char *mutt_crypt_hook (ADDRESS * adr)
444 return _mutt_string_hook (adr->mailbox, M_CRYPTHOOK);
448 void mutt_account_hook (const char *url)
456 err.dsize = sizeof (buf);
457 memset (&token, 0, sizeof (token));
459 for (hook = Hooks; hook; hook = hook->next) {
460 if (!(hook->command && (hook->type & M_ACCOUNTHOOK)))
463 if ((regexec (hook->rx.rx, url, 0, NULL, 0) == 0) ^ hook->rx.not) {
464 if (mutt_parse_rc_line (hook->command, &token, &err) == -1) {
465 mutt_error ("%s", err.data);
468 mem_free (&token.data);
469 current_hook_type = 0;
476 mem_free (&token.data);