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"
35 int type; /* hook type */
36 rx_t 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,
50 BUFFER command, pattern;
53 pattern_t *pat = NULL;
54 char path[_POSIX_PATH_MAX];
56 memset (&pattern, 0, sizeof (pattern));
57 memset (&command, 0, sizeof (command));
59 if (*s->dptr == '!') {
65 mutt_extract_token (&pattern, s, 0);
68 strfcpy (err->data, _("too few arguments"), err->dsize);
72 mutt_extract_token (&command, s,
74 (M_FOLDERHOOK | M_SENDHOOK | M_SEND2HOOK |
75 M_ACCOUNTHOOK | M_REPLYHOOK)) ? M_TOKEN_SPACE : 0);
78 strfcpy (err->data, _("too few arguments"), err->dsize);
83 strfcpy (err->data, _("too many arguments"), err->dsize);
87 if (data & (M_FOLDERHOOK | M_MBOXHOOK)) {
88 strfcpy (path, pattern.data, sizeof (path));
89 _mutt_expand_path (path, sizeof (path), 1);
90 mem_free (&pattern.data);
91 memset (&pattern, 0, sizeof (pattern));
92 pattern.data = str_dup (path);
95 else if (data & (M_APPENDHOOK | M_OPENHOOK | M_CLOSEHOOK)) {
96 if (mutt_test_compress_command (command.data)) {
97 strfcpy (err->data, _("bad formatted command string"), err->dsize);
102 else if (DefaultHook && !(data & (M_CHARSETHOOK | M_ACCOUNTHOOK))
103 && (!WithCrypto || !(data & M_CRYPTHOOK))
105 char tmp[HUGE_STRING];
107 strfcpy (tmp, pattern.data, sizeof (tmp));
108 mutt_check_simple (tmp, sizeof (tmp), DefaultHook);
109 mem_free (&pattern.data);
110 memset (&pattern, 0, sizeof (pattern));
111 pattern.data = str_dup (tmp);
114 if (data & (M_MBOXHOOK | M_SAVEHOOK | M_FCCHOOK)) {
115 strfcpy (path, command.data, sizeof (path));
116 mutt_expand_path (path, sizeof (path));
117 mem_free (&command.data);
118 memset (&command, 0, sizeof (command));
119 command.data = str_dup (path);
122 /* check to make sure that a matching hook doesn't already exist */
123 for (ptr = Hooks; ptr; ptr = ptr->next) {
124 if (ptr->type == data &&
125 ptr->rx.not == not && !str_cmp (pattern.data, ptr->rx.pattern)) {
127 (M_FOLDERHOOK | M_SENDHOOK | M_SEND2HOOK | M_MESSAGEHOOK |
128 M_ACCOUNTHOOK | M_REPLYHOOK)) {
129 /* these hooks allow multiple commands with the same
130 * pattern, so if we've already seen this pattern/command pair, just
131 * ignore it instead of creating a duplicate */
132 if (!str_cmp (ptr->command, command.data)) {
133 mem_free (&command.data);
134 mem_free (&pattern.data);
139 /* other hooks only allow one command per pattern, so update the
140 * entry with the new command. this currently does not change the
141 * order of execution of the hooks, which i think is desirable since
142 * a common action to perform is to change the default (.) entry
143 * based upon some other information. */
144 mem_free (&ptr->command);
145 ptr->command = command.data;
146 mem_free (&pattern.data);
155 (M_SENDHOOK | M_SEND2HOOK | M_SAVEHOOK | M_FCCHOOK | M_MESSAGEHOOK |
158 mutt_pattern_comp (pattern.data,
159 (data & (M_SENDHOOK | M_SEND2HOOK | M_FCCHOOK)) ?
160 0 : M_FULL_MSG, err)) == NULL)
164 rx = mem_malloc (sizeof (regex_t));
167 REGCOMP (rx, NONULL (pattern.data),
168 ((data & (M_CRYPTHOOK | M_CHARSETHOOK)) ? REG_ICASE : 0)))
172 REGCOMP (rx, NONULL (pattern.data),
173 (data & (M_CHARSETHOOK | M_ICONVHOOK)) ? REG_ICASE : 0)) !=
175 #endif /* M_CRYPTHOOK */
177 regerror (rc, rx, err->data, err->dsize);
185 ptr->next = mem_calloc (1, sizeof (HOOK));
189 Hooks = ptr = mem_calloc (1, sizeof (HOOK));
191 ptr->command = command.data;
193 ptr->rx.pattern = pattern.data;
199 mem_free (&pattern.data);
200 mem_free (&command.data);
204 static void delete_hook (HOOK * h)
206 mem_free (&h->command);
207 mem_free (&h->rx.pattern);
211 mutt_pattern_free (&h->pattern);
215 /* Deletes all hooks of type ``type'', or all defined hooks if ``type'' is 0 */
216 static void delete_hooks (int type)
221 while (h = Hooks, h && (type == 0 || type == h->type)) {
226 prev = h; /* Unused assignment to avoid compiler warnings */
229 if (type == h->type) {
230 prev->next = h->next;
239 int mutt_parse_unhook (BUFFER * buf, BUFFER * s, unsigned long data,
242 while (MoreArgs (s)) {
243 mutt_extract_token (buf, s, 0);
244 if (str_cmp ("*", buf->data) == 0) {
245 if (current_hook_type) {
246 snprintf (err->data, err->dsize,
247 _("unhook: Can't do unhook * from within a hook."));
253 int type = mutt_get_hook_type (buf->data);
256 snprintf (err->data, err->dsize,
257 _("unhook: unknown hook type: %s"), buf->data);
260 if (current_hook_type == type) {
261 snprintf (err->data, err->dsize,
262 _("unhook: Can't delete a %s from within a %s."), buf->data,
272 void mutt_folder_hook (char *path)
278 current_hook_type = M_FOLDERHOOK;
281 err.dsize = sizeof (buf);
282 memset (&token, 0, sizeof (token));
283 for (; tmp; tmp = tmp->next) {
287 if (tmp->type & M_FOLDERHOOK) {
288 if ((regexec (tmp->rx.rx, path, 0, NULL, 0) == 0) ^ tmp->rx.not) {
289 if (mutt_parse_rc_line (tmp->command, &token, &err) == -1) {
290 mutt_error ("%s", err.data);
291 mem_free (&token.data);
292 mutt_sleep (1); /* pause a moment to let the user see the error */
293 current_hook_type = 0;
299 mem_free (&token.data);
301 current_hook_type = 0;
304 char *mutt_find_hook (int type, const char *pat)
308 for (; tmp; tmp = tmp->next)
309 if (tmp->type & type) {
310 if (regexec (tmp->rx.rx, pat, 0, NULL, 0) == 0)
311 return (tmp->command);
316 void mutt_message_hook (CONTEXT * ctx, HEADER * hdr, int type)
322 current_hook_type = type;
325 err.dsize = sizeof (buf);
326 memset (&token, 0, sizeof (token));
327 for (hook = Hooks; hook; hook = hook->next) {
331 if (hook->type & type)
332 if ((mutt_pattern_exec (hook->pattern, 0, ctx, hdr) > 0) ^ hook->rx.not)
333 if (mutt_parse_rc_line (hook->command, &token, &err) != 0) {
334 mem_free (&token.data);
335 mutt_error ("%s", err.data);
337 current_hook_type = 0;
341 mem_free (&token.data);
342 current_hook_type = 0;
346 mutt_addr_hook (char *path, size_t pathlen, int type, CONTEXT * ctx,
351 /* determine if a matching hook exists */
352 for (hook = Hooks; hook; hook = hook->next) {
356 if (hook->type & type)
357 if ((mutt_pattern_exec (hook->pattern, 0, ctx, hdr) > 0) ^ hook->rx.not) {
358 mutt_make_string (path, pathlen, hook->command, ctx, hdr);
366 void mutt_default_save (char *path, size_t pathlen, HEADER * hdr)
369 if (mutt_addr_hook (path, pathlen, M_SAVEHOOK, Context, hdr) != 0) {
370 char tmp[_POSIX_PATH_MAX];
372 ENVELOPE *env = hdr->env;
373 int fromMe = mutt_addr_is_user (env->from);
375 if (!fromMe && env->reply_to && env->reply_to->mailbox)
377 else if (!fromMe && env->from && env->from->mailbox)
379 else if (env->to && env->to->mailbox)
381 else if (env->cc && env->cc->mailbox)
386 mutt_safe_path (tmp, sizeof (tmp), adr);
387 snprintf (path, pathlen, "=%s", tmp);
392 void mutt_select_fcc (char *path, size_t pathlen, HEADER * hdr)
395 char buf[_POSIX_PATH_MAX];
396 ENVELOPE *env = hdr->env;
398 if (mutt_addr_hook (path, pathlen, M_FCCHOOK, NULL, hdr) != 0) {
399 if ((option (OPTSAVENAME) || option (OPTFORCENAME)) &&
400 (env->to || env->cc || env->bcc)) {
401 adr = env->to ? env->to : (env->cc ? env->cc : env->bcc);
402 mutt_safe_path (buf, sizeof (buf), adr);
403 mutt_concat_path (path, NONULL (Maildir), buf, pathlen);
404 if (!option (OPTFORCENAME) && mx_access (path, W_OK) != 0)
405 strfcpy (path, NONULL (Outbox), pathlen);
408 strfcpy (path, NONULL (Outbox), pathlen);
410 mutt_pretty_mailbox (path);
413 static char *_mutt_string_hook (const char *match, int hook)
417 for (; tmp; tmp = tmp->next) {
418 if ((tmp->type & hook) && ((match &&
419 regexec (tmp->rx.rx, match, 0, NULL,
420 0) == 0) ^ tmp->rx.not))
421 return (tmp->command);
426 char *mutt_charset_hook (const char *chs)
428 return _mutt_string_hook (chs, M_CHARSETHOOK);
431 char *mutt_iconv_hook (const char *chs)
433 return _mutt_string_hook (chs, M_ICONVHOOK);
436 char *mutt_crypt_hook (ADDRESS * adr)
438 return _mutt_string_hook (adr->mailbox, M_CRYPTHOOK);
442 void mutt_account_hook (const char *url)
450 err.dsize = sizeof (buf);
451 memset (&token, 0, sizeof (token));
453 for (hook = Hooks; hook; hook = hook->next) {
454 if (!(hook->command && (hook->type & M_ACCOUNTHOOK)))
457 if ((regexec (hook->rx.rx, url, 0, NULL, 0) == 0) ^ hook->rx.not) {
458 if (mutt_parse_rc_line (hook->command, &token, &err) == -1) {
459 mem_free (&token.data);
460 mutt_error ("%s", err.data);
468 mem_free (&token.data);