NeoMutt  2024-04-16-36-g75b6fb
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
hook.c
Go to the documentation of this file.
1
37#include "config.h"
38#include <limits.h>
39#include <stdbool.h>
40#include <string.h>
41#include <unistd.h>
42#include "mutt/lib.h"
43#include "address/lib.h"
44#include "config/lib.h"
45#include "email/lib.h"
46#include "core/lib.h"
47#include "alias/lib.h"
48#include "hook.h"
49#include "attach/lib.h"
50#include "compmbox/lib.h"
51#include "expando/lib.h"
52#include "index/lib.h"
53#include "ncrypt/lib.h"
54#include "parse/lib.h"
55#include "pattern/lib.h"
56#include "commands.h"
57#include "globals.h"
58#include "hdrline.h"
59#include "muttlib.h"
60#include "mx.h"
61
62extern const struct ExpandoDefinition IndexFormatDef[];
63
67struct Hook
68{
70 struct Regex regex;
71 char *command;
73 struct PatternList *pattern;
74 struct Expando *expando;
75 TAILQ_ENTRY(Hook) entries;
76};
77TAILQ_HEAD(HookList, Hook);
78
80static struct HookList Hooks = TAILQ_HEAD_INITIALIZER(Hooks);
81
83static struct HashTable *IdxFmtHooks = NULL;
84
87
92static void hook_free(struct Hook **ptr)
93{
94 if (!ptr || !*ptr)
95 return;
96
97 struct Hook *h = *ptr;
98
99 FREE(&h->command);
100 FREE(&h->source_file);
101 FREE(&h->regex.pattern);
102 if (h->regex.regex)
103 {
104 regfree(h->regex.regex);
105 FREE(&h->regex.regex);
106 }
109 FREE(ptr);
110}
111
116static struct Hook *hook_new(void)
117{
118 return mutt_mem_calloc(1, sizeof(struct Hook));
119}
120
125 intptr_t data, struct Buffer *err)
126{
127 struct Buffer *alias = buf_pool_get();
128 struct Buffer *charset = buf_pool_get();
129
130 int rc = MUTT_CMD_ERROR;
131
132 if (parse_extract_token(alias, s, TOKEN_NO_FLAGS) < 0)
133 goto done;
134 if (parse_extract_token(charset, s, TOKEN_NO_FLAGS) < 0)
135 goto done;
136
138
139 if (buf_is_empty(alias) || buf_is_empty(charset))
140 {
141 buf_printf(err, _("%s: too few arguments"), buf->data);
142 rc = MUTT_CMD_WARNING;
143 }
144 else if (MoreArgs(s))
145 {
146 buf_printf(err, _("%s: too many arguments"), buf->data);
147 buf_reset(s); // clean up buffer to avoid a mess with further rcfile processing
148 rc = MUTT_CMD_WARNING;
149 }
150 else if (mutt_ch_lookup_add(type, buf_string(alias), buf_string(charset), err))
151 {
152 rc = MUTT_CMD_SUCCESS;
153 }
154
155done:
156 buf_pool_release(&alias);
157 buf_pool_release(&charset);
158
159 return rc;
160}
161
167enum CommandResult mutt_parse_hook(struct Buffer *buf, struct Buffer *s,
168 intptr_t data, struct Buffer *err)
169{
170 struct Hook *hook = NULL;
171 int rc = MUTT_CMD_ERROR;
172 bool pat_not = false;
173 bool use_regex = true;
174 regex_t *rx = NULL;
175 struct PatternList *pat = NULL;
176 const bool folder_or_mbox = (data & (MUTT_FOLDER_HOOK | MUTT_MBOX_HOOK));
177
178 struct Buffer *cmd = buf_pool_get();
179 struct Buffer *pattern = buf_pool_get();
180
181 if (~data & MUTT_GLOBAL_HOOK) /* NOT a global hook */
182 {
183 if (*s->dptr == '!')
184 {
185 s->dptr++;
186 SKIPWS(s->dptr);
187 pat_not = true;
188 }
189
191 if (folder_or_mbox && mutt_str_equal(buf_string(pattern), "-noregex"))
192 {
193 use_regex = false;
194 if (!MoreArgs(s))
195 {
196 buf_printf(err, _("%s: too few arguments"), buf->data);
197 rc = MUTT_CMD_WARNING;
198 goto cleanup;
199 }
201 }
202
203 if (!MoreArgs(s))
204 {
205 buf_printf(err, _("%s: too few arguments"), buf->data);
206 rc = MUTT_CMD_WARNING;
207 goto cleanup;
208 }
209 }
210
211 parse_extract_token(cmd, s,
216
217 if (buf_is_empty(cmd))
218 {
219 buf_printf(err, _("%s: too few arguments"), buf->data);
220 rc = MUTT_CMD_WARNING;
221 goto cleanup;
222 }
223
224 if (MoreArgs(s))
225 {
226 buf_printf(err, _("%s: too many arguments"), buf->data);
227 rc = MUTT_CMD_WARNING;
228 goto cleanup;
229 }
230
231 const char *const c_default_hook = cs_subset_string(NeoMutt->sub, "default_hook");
232 if (folder_or_mbox)
233 {
234 /* Accidentally using the ^ mailbox shortcut in the .neomuttrc is a
235 * common mistake */
236 if ((pattern->data[0] == '^') && !CurrentFolder)
237 {
238 buf_strcpy(err, _("current mailbox shortcut '^' is unset"));
239 goto cleanup;
240 }
241
242 struct Buffer *tmp = buf_pool_get();
243 buf_copy(tmp, pattern);
244 buf_expand_path_regex(tmp, use_regex);
245
246 /* Check for other mailbox shortcuts that expand to the empty string.
247 * This is likely a mistake too */
248 if (buf_is_empty(tmp) && !buf_is_empty(pattern))
249 {
250 buf_strcpy(err, _("mailbox shortcut expanded to empty regex"));
251 buf_pool_release(&tmp);
252 goto cleanup;
253 }
254
255 if (use_regex)
256 {
257 buf_copy(pattern, tmp);
258 }
259 else
260 {
262 }
263 buf_pool_release(&tmp);
264 }
266 {
267 if (mutt_comp_valid_command(buf_string(cmd)) == 0)
268 {
269 buf_strcpy(err, _("badly formatted command string"));
270 goto cleanup;
271 }
272 }
273 else if (c_default_hook && (~data & MUTT_GLOBAL_HOOK) &&
275 {
276 /* At this stage only these hooks remain:
277 * fcc-, fcc-save-, index-format-, message-, reply-, save-, send- and send2-hook
278 * If given a plain string, or regex, we expand it using $default_hook. */
279 mutt_check_simple(pattern, c_default_hook);
280 }
281
283 {
284 buf_expand_path(cmd);
285 }
286
287 /* check to make sure that a matching hook doesn't already exist */
288 TAILQ_FOREACH(hook, &Hooks, entries)
289 {
291 {
292 /* Ignore duplicate global hooks */
293 if (mutt_str_equal(hook->command, buf_string(cmd)))
294 {
295 rc = MUTT_CMD_SUCCESS;
296 goto cleanup;
297 }
298 }
299 else if ((hook->type == data) && (hook->regex.pat_not == pat_not) &&
300 mutt_str_equal(buf_string(pattern), hook->regex.pattern))
301 {
305 {
306 /* these hooks allow multiple commands with the same
307 * pattern, so if we've already seen this pattern/command pair, just
308 * ignore it instead of creating a duplicate */
309 if (mutt_str_equal(hook->command, buf_string(cmd)))
310 {
311 rc = MUTT_CMD_SUCCESS;
312 goto cleanup;
313 }
314 }
315 else
316 {
317 /* other hooks only allow one command per pattern, so update the
318 * entry with the new command. this currently does not change the
319 * order of execution of the hooks, which i think is desirable since
320 * a common action to perform is to change the default (.) entry
321 * based upon some other information. */
322 FREE(&hook->command);
323 hook->command = buf_strdup(cmd);
325 rc = MUTT_CMD_SUCCESS;
326 goto cleanup;
327 }
328 }
329 }
330
333 {
334 PatternCompFlags comp_flags;
335
336 if (data & (MUTT_SEND2_HOOK))
337 comp_flags = MUTT_PC_SEND_MODE_SEARCH;
338 else if (data & (MUTT_SEND_HOOK | MUTT_FCC_HOOK))
339 comp_flags = MUTT_PC_NO_FLAGS;
340 else
341 comp_flags = MUTT_PC_FULL_MSG;
342
343 struct MailboxView *mv_cur = get_current_mailbox_view();
344 struct Menu *menu = get_current_menu();
345 pat = mutt_pattern_comp(mv_cur, menu, buf_string(pattern), comp_flags, err);
346 if (!pat)
347 goto cleanup;
348 }
349 else if (~data & MUTT_GLOBAL_HOOK) /* NOT a global hook */
350 {
351 /* Hooks not allowing full patterns: Check syntax of regex */
352 rx = mutt_mem_calloc(1, sizeof(regex_t));
353 int rc2 = REG_COMP(rx, buf_string(pattern), ((data & MUTT_CRYPT_HOOK) ? REG_ICASE : 0));
354 if (rc2 != 0)
355 {
356 regerror(rc2, rx, err->data, err->dsize);
357 FREE(&rx);
358 goto cleanup;
359 }
360 }
361
362 struct Expando *exp = NULL;
364 exp = expando_parse(buf_string(cmd), IndexFormatDef, err);
365
366 hook = hook_new();
367 hook->type = data;
368 hook->command = buf_strdup(cmd);
370 hook->pattern = pat;
371 hook->regex.pattern = buf_strdup(pattern);
372 hook->regex.regex = rx;
373 hook->regex.pat_not = pat_not;
374 hook->expando = exp;
375
376 TAILQ_INSERT_TAIL(&Hooks, hook, entries);
377 rc = MUTT_CMD_SUCCESS;
378
379cleanup:
380 buf_pool_release(&cmd);
381 buf_pool_release(&pattern);
382 return rc;
383}
384
392{
393 struct Hook *h = NULL;
394 struct Hook *tmp = NULL;
395
396 TAILQ_FOREACH_SAFE(h, &Hooks, entries, tmp)
397 {
398 if ((type == MUTT_HOOK_NO_FLAGS) || (type == h->type))
399 {
400 TAILQ_REMOVE(&Hooks, h, entries);
401 hook_free(&h);
402 }
403 }
404}
405
409static void idxfmt_hashelem_free(int type, void *obj, intptr_t data)
410{
411 struct HookList *hl = obj;
412 struct Hook *h = NULL;
413 struct Hook *tmp = NULL;
414
415 TAILQ_FOREACH_SAFE(h, hl, entries, tmp)
416 {
417 TAILQ_REMOVE(hl, h, entries);
418 hook_free(&h);
419 }
420
421 FREE(&hl);
422}
423
427static void delete_idxfmt_hooks(void)
428{
430}
431
435static enum CommandResult mutt_parse_idxfmt_hook(struct Buffer *buf, struct Buffer *s,
436 intptr_t data, struct Buffer *err)
437{
439 bool pat_not = false;
440
441 struct Buffer *name = buf_pool_get();
442 struct Buffer *pattern = buf_pool_get();
443 struct Buffer *fmtstring = buf_pool_get();
444 struct Expando *exp = NULL;
445
446 if (!IdxFmtHooks)
447 {
450 }
451
452 if (!MoreArgs(s))
453 {
454 buf_printf(err, _("%s: too few arguments"), buf->data);
455 goto out;
456 }
458 struct HookList *hl = mutt_hash_find(IdxFmtHooks, buf_string(name));
459
460 if (*s->dptr == '!')
461 {
462 s->dptr++;
463 SKIPWS(s->dptr);
464 pat_not = true;
465 }
467
468 if (!MoreArgs(s))
469 {
470 buf_printf(err, _("%s: too few arguments"), buf->data);
471 goto out;
472 }
473 parse_extract_token(fmtstring, s, TOKEN_NO_FLAGS);
474
475 exp = expando_parse(buf_string(fmtstring), IndexFormatDef, err);
476 if (!exp)
477 goto out;
478
479 if (MoreArgs(s))
480 {
481 buf_printf(err, _("%s: too many arguments"), buf->data);
482 goto out;
483 }
484
485 const char *const c_default_hook = cs_subset_string(NeoMutt->sub, "default_hook");
486 if (c_default_hook)
487 mutt_check_simple(pattern, c_default_hook);
488
489 /* check to make sure that a matching hook doesn't already exist */
490 struct Hook *hook = NULL;
491 if (hl)
492 {
493 TAILQ_FOREACH(hook, hl, entries)
494 {
495 if ((hook->regex.pat_not == pat_not) &&
497 {
498 expando_free(&hook->expando);
499 hook->expando = exp;
500 exp = NULL;
501 rc = MUTT_CMD_SUCCESS;
502 goto out;
503 }
504 }
505 }
506
507 /* MUTT_PC_PATTERN_DYNAMIC sets so that date ranges are regenerated during
508 * matching. This of course is slower, but index-format-hook is commonly
509 * used for date ranges, and they need to be evaluated relative to "now", not
510 * the hook compilation time. */
511 struct MailboxView *mv_cur = get_current_mailbox_view();
512 struct Menu *menu = get_current_menu();
513 struct PatternList *pat = mutt_pattern_comp(mv_cur, menu, buf_string(pattern),
515 err);
516 if (!pat)
517 goto out;
518
519 hook = hook_new();
520 hook->type = MUTT_IDXFMTHOOK;
521 hook->command = NULL;
523 hook->pattern = pat;
524 hook->regex.pattern = buf_strdup(pattern);
525 hook->regex.regex = NULL;
526 hook->regex.pat_not = pat_not;
527 hook->expando = exp;
528 exp = NULL;
529
530 if (!hl)
531 {
532 hl = mutt_mem_calloc(1, sizeof(*hl));
533 TAILQ_INIT(hl);
535 }
536
537 TAILQ_INSERT_TAIL(hl, hook, entries);
538 rc = MUTT_CMD_SUCCESS;
539
540out:
541 buf_pool_release(&name);
542 buf_pool_release(&pattern);
543 buf_pool_release(&fmtstring);
544 expando_free(&exp);
545
546 return rc;
547}
548
555static HookFlags mutt_get_hook_type(const char *name)
556{
557 struct Command *c = NULL;
558 for (size_t i = 0, size = commands_array(&c); i < size; i++)
559 {
560 if (((c[i].parse == mutt_parse_hook) || (c[i].parse == mutt_parse_idxfmt_hook)) &&
562 {
563 return c[i].data;
564 }
565 }
566 return MUTT_HOOK_NO_FLAGS;
567}
568
572static enum CommandResult mutt_parse_unhook(struct Buffer *buf, struct Buffer *s,
573 intptr_t data, struct Buffer *err)
574{
575 while (MoreArgs(s))
576 {
578 if (mutt_str_equal("*", buf->data))
579 {
581 {
582 buf_addstr(err, _("unhook: Can't do unhook * from within a hook"));
583 return MUTT_CMD_WARNING;
584 }
588 }
589 else
590 {
591 HookFlags type = mutt_get_hook_type(buf->data);
592
593 if (type == MUTT_HOOK_NO_FLAGS)
594 {
595 buf_printf(err, _("unhook: unknown hook type: %s"), buf->data);
596 return MUTT_CMD_ERROR;
597 }
598 if (type & (MUTT_CHARSET_HOOK | MUTT_ICONV_HOOK))
599 {
601 return MUTT_CMD_SUCCESS;
602 }
603 if (CurrentHookType == type)
604 {
605 buf_printf(err, _("unhook: Can't delete a %s from within a %s"),
606 buf->data, buf->data);
607 return MUTT_CMD_WARNING;
608 }
609 if (type == MUTT_IDXFMTHOOK)
611 else
612 mutt_delete_hooks(type);
613 }
614 }
615 return MUTT_CMD_SUCCESS;
616}
617
623void mutt_folder_hook(const char *path, const char *desc)
624{
625 if (!path && !desc)
626 return;
627
628 struct Hook *hook = NULL;
629 struct Buffer *err = buf_pool_get();
630
632
633 TAILQ_FOREACH(hook, &Hooks, entries)
634 {
635 if (!hook->command)
636 continue;
637
638 if (!(hook->type & MUTT_FOLDER_HOOK))
639 continue;
640
641 const char *match = NULL;
642 if (mutt_regex_match(&hook->regex, path))
643 match = path;
644 else if (mutt_regex_match(&hook->regex, desc))
645 match = desc;
646
647 if (match)
648 {
649 mutt_debug(LL_DEBUG1, "folder-hook '%s' matches '%s'\n", hook->regex.pattern, match);
650 mutt_debug(LL_DEBUG5, " %s\n", hook->command);
651 if (parse_rc_line_cwd(hook->command, hook->source_file, err) == MUTT_CMD_ERROR)
652 {
653 mutt_error("%s", buf_string(err));
654 break;
655 }
656 }
657 }
658 buf_pool_release(&err);
659
661}
662
671char *mutt_find_hook(HookFlags type, const char *pat)
672{
673 struct Hook *tmp = NULL;
674
675 TAILQ_FOREACH(tmp, &Hooks, entries)
676 {
677 if (tmp->type & type)
678 {
679 if (mutt_regex_match(&tmp->regex, pat))
680 return tmp->command;
681 }
682 }
683 return NULL;
684}
685
692void mutt_message_hook(struct Mailbox *m, struct Email *e, HookFlags type)
693{
694 struct Hook *hook = NULL;
695 struct PatternCache cache = { 0 };
696 struct Buffer *err = buf_pool_get();
697
698 CurrentHookType = type;
699
700 TAILQ_FOREACH(hook, &Hooks, entries)
701 {
702 if (!hook->command)
703 continue;
704
705 if (hook->type & type)
706 {
707 if ((mutt_pattern_exec(SLIST_FIRST(hook->pattern), 0, m, e, &cache) > 0) ^
708 hook->regex.pat_not)
709 {
710 if (parse_rc_line_cwd(hook->command, hook->source_file, err) == MUTT_CMD_ERROR)
711 {
712 mutt_error("%s", buf_string(err));
714 buf_pool_release(&err);
715
716 return;
717 }
718 /* Executing arbitrary commands could affect the pattern results,
719 * so the cache has to be wiped */
720 memset(&cache, 0, sizeof(cache));
721 }
722 }
723 }
724 buf_pool_release(&err);
725
727}
728
738static int addr_hook(struct Buffer *path, HookFlags type, struct Mailbox *m, struct Email *e)
739{
740 struct Hook *hook = NULL;
741 struct PatternCache cache = { 0 };
742
743 /* determine if a matching hook exists */
744 TAILQ_FOREACH(hook, &Hooks, entries)
745 {
746 if (!hook->command)
747 continue;
748
749 if (hook->type & type)
750 {
751 if ((mutt_pattern_exec(SLIST_FIRST(hook->pattern), 0, m, e, &cache) > 0) ^
752 hook->regex.pat_not)
753 {
754 buf_alloc(path, PATH_MAX);
755 mutt_make_string(path, -1, hook->expando, m, -1, e, MUTT_FORMAT_PLAIN, NULL);
756 buf_fix_dptr(path);
757 return 0;
758 }
759 }
760 }
761
762 return -1;
763}
764
770void mutt_default_save(struct Buffer *path, struct Email *e)
771{
772 struct Mailbox *m_cur = get_current_mailbox();
773 if (addr_hook(path, MUTT_SAVE_HOOK, m_cur, e) == 0)
774 return;
775
776 struct Envelope *env = e->env;
777 const struct Address *from = TAILQ_FIRST(&env->from);
778 const struct Address *reply_to = TAILQ_FIRST(&env->reply_to);
779 const struct Address *to = TAILQ_FIRST(&env->to);
780 const struct Address *cc = TAILQ_FIRST(&env->cc);
781 const struct Address *addr = NULL;
782 bool from_me = mutt_addr_is_user(from);
783
784 if (!from_me && reply_to && reply_to->mailbox)
785 addr = reply_to;
786 else if (!from_me && from && from->mailbox)
787 addr = from;
788 else if (to && to->mailbox)
789 addr = to;
790 else if (cc && cc->mailbox)
791 addr = cc;
792 else
793 addr = NULL;
794 if (addr)
795 {
796 struct Buffer *tmp = buf_pool_get();
797 mutt_safe_path(tmp, addr);
798 buf_add_printf(path, "=%s", buf_string(tmp));
799 buf_pool_release(&tmp);
800 }
801}
802
808void mutt_select_fcc(struct Buffer *path, struct Email *e)
809{
810 buf_alloc(path, PATH_MAX);
811
812 if (addr_hook(path, MUTT_FCC_HOOK, NULL, e) != 0)
813 {
814 const struct Address *to = TAILQ_FIRST(&e->env->to);
815 const struct Address *cc = TAILQ_FIRST(&e->env->cc);
816 const struct Address *bcc = TAILQ_FIRST(&e->env->bcc);
817 const bool c_save_name = cs_subset_bool(NeoMutt->sub, "save_name");
818 const bool c_force_name = cs_subset_bool(NeoMutt->sub, "force_name");
819 const char *const c_record = cs_subset_string(NeoMutt->sub, "record");
820 if ((c_save_name || c_force_name) && (to || cc || bcc))
821 {
822 const struct Address *addr = to ? to : (cc ? cc : bcc);
823 struct Buffer *buf = buf_pool_get();
824 mutt_safe_path(buf, addr);
825 const char *const c_folder = cs_subset_string(NeoMutt->sub, "folder");
826 buf_concat_path(path, NONULL(c_folder), buf_string(buf));
827 buf_pool_release(&buf);
828 if (!c_force_name && (mx_access(buf_string(path), W_OK) != 0))
829 buf_strcpy(path, c_record);
830 }
831 else
832 {
833 buf_strcpy(path, c_record);
834 }
835 }
836 else
837 {
838 buf_fix_dptr(path);
839 }
840
841 buf_pretty_mailbox(path);
842}
843
850static void list_hook(struct ListHead *matches, const char *match, HookFlags type)
851{
852 struct Hook *tmp = NULL;
853
854 TAILQ_FOREACH(tmp, &Hooks, entries)
855 {
856 if ((tmp->type & type) && mutt_regex_match(&tmp->regex, match))
857 {
859 }
860 }
861}
862
870void mutt_crypt_hook(struct ListHead *list, struct Address *addr)
871{
873}
874
879void mutt_account_hook(const char *url)
880{
881 /* parsing commands with URLs in an account hook can cause a recursive
882 * call. We just skip processing if this occurs. Typically such commands
883 * belong in a folder-hook -- perhaps we should warn the user. */
884 static bool inhook = false;
885 if (inhook)
886 return;
887
888 struct Hook *hook = NULL;
889 struct Buffer *err = buf_pool_get();
890
891 TAILQ_FOREACH(hook, &Hooks, entries)
892 {
893 if (!(hook->command && (hook->type & MUTT_ACCOUNT_HOOK)))
894 continue;
895
896 if (mutt_regex_match(&hook->regex, url))
897 {
898 inhook = true;
899 mutt_debug(LL_DEBUG1, "account-hook '%s' matches '%s'\n", hook->regex.pattern, url);
900 mutt_debug(LL_DEBUG5, " %s\n", hook->command);
901
902 if (parse_rc_line_cwd(hook->command, hook->source_file, err) == MUTT_CMD_ERROR)
903 {
904 mutt_error("%s", buf_string(err));
905 buf_pool_release(&err);
906
907 inhook = false;
908 goto done;
909 }
910
911 inhook = false;
912 }
913 }
914done:
915 buf_pool_release(&err);
916}
917
925{
926 struct Hook *hook = NULL;
927 struct Buffer *err = buf_pool_get();
928
929 TAILQ_FOREACH(hook, &Hooks, entries)
930 {
931 if (!(hook->command && (hook->type & MUTT_TIMEOUT_HOOK)))
932 continue;
933
934 if (parse_rc_line_cwd(hook->command, hook->source_file, err) == MUTT_CMD_ERROR)
935 {
936 mutt_error("%s", buf_string(err));
937 buf_reset(err);
938
939 /* The hooks should be independent of each other, so even though this on
940 * failed, we'll carry on with the others. */
941 }
942 }
943 buf_pool_release(&err);
944
945 /* Delete temporary attachment files */
947}
948
957{
958 struct Hook *hook = NULL;
959 struct Buffer *err = buf_pool_get();
960
961 TAILQ_FOREACH(hook, &Hooks, entries)
962 {
963 if (!(hook->command && (hook->type & type)))
964 continue;
965
966 if (parse_rc_line_cwd(hook->command, hook->source_file, err) == MUTT_CMD_ERROR)
967 {
968 mutt_error("%s", buf_string(err));
969 buf_reset(err);
970 }
971 }
972 buf_pool_release(&err);
973}
974
983const struct Expando *mutt_idxfmt_hook(const char *name, struct Mailbox *m, struct Email *e)
984{
985 if (!IdxFmtHooks)
986 return NULL;
987
988 struct HookList *hl = mutt_hash_find(IdxFmtHooks, name);
989 if (!hl)
990 return NULL;
991
993
994 struct PatternCache cache = { 0 };
995 const struct Expando *exp = NULL;
996 struct Hook *hook = NULL;
997
998 TAILQ_FOREACH(hook, hl, entries)
999 {
1000 struct Pattern *pat = SLIST_FIRST(hook->pattern);
1001 if ((mutt_pattern_exec(pat, 0, m, e, &cache) > 0) ^ hook->regex.pat_not)
1002 {
1003 exp = hook->expando;
1004 break;
1005 }
1006 }
1007
1009
1010 return exp;
1011}
1012
1016static const struct Command HookCommands[] = {
1017 // clang-format off
1018 { "account-hook", mutt_parse_hook, MUTT_ACCOUNT_HOOK },
1020 { "crypt-hook", mutt_parse_hook, MUTT_CRYPT_HOOK },
1021 { "fcc-hook", mutt_parse_hook, MUTT_FCC_HOOK },
1022 { "fcc-save-hook", mutt_parse_hook, MUTT_FCC_HOOK | MUTT_SAVE_HOOK },
1023 { "folder-hook", mutt_parse_hook, MUTT_FOLDER_HOOK },
1025 { "index-format-hook", mutt_parse_idxfmt_hook, MUTT_IDXFMTHOOK },
1026 { "mbox-hook", mutt_parse_hook, MUTT_MBOX_HOOK },
1027 { "message-hook", mutt_parse_hook, MUTT_MESSAGE_HOOK },
1028 { "pgp-hook", mutt_parse_hook, MUTT_CRYPT_HOOK },
1029 { "reply-hook", mutt_parse_hook, MUTT_REPLY_HOOK },
1030 { "save-hook", mutt_parse_hook, MUTT_SAVE_HOOK },
1031 { "send-hook", mutt_parse_hook, MUTT_SEND_HOOK },
1032 { "send2-hook", mutt_parse_hook, MUTT_SEND2_HOOK },
1033 { "shutdown-hook", mutt_parse_hook, MUTT_SHUTDOWN_HOOK | MUTT_GLOBAL_HOOK },
1034 { "startup-hook", mutt_parse_hook, MUTT_STARTUP_HOOK | MUTT_GLOBAL_HOOK },
1035 { "timeout-hook", mutt_parse_hook, MUTT_TIMEOUT_HOOK | MUTT_GLOBAL_HOOK },
1036 { "unhook", mutt_parse_unhook, 0 },
1037 // clang-format on
1038};
1039
1043void hooks_init(void)
1044{
1046}
Email Address Handling.
Email Aliases.
bool mutt_addr_is_user(const struct Address *addr)
Does the address belong to the user.
Definition: alias.c:600
GUI display the mailboxes in a side panel.
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:160
int buf_add_printf(struct Buffer *buf, const char *fmt,...)
Format a string appending a Buffer.
Definition: buffer.c:203
void buf_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition: buffer.c:75
bool buf_is_empty(const struct Buffer *buf)
Is the Buffer empty?
Definition: buffer.c:290
void buf_fix_dptr(struct Buffer *buf)
Move the dptr to end of the Buffer.
Definition: buffer.c:181
size_t buf_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition: buffer.c:225
size_t buf_strcpy(struct Buffer *buf, const char *s)
Copy a string into a Buffer.
Definition: buffer.c:394
size_t buf_copy(struct Buffer *dst, const struct Buffer *src)
Copy a Buffer's contents to another Buffer.
Definition: buffer.c:600
char * buf_strdup(const struct Buffer *buf)
Copy a Buffer's string.
Definition: buffer.c:570
size_t buf_concat_path(struct Buffer *buf, const char *dir, const char *fname)
Join a directory name and a filename.
Definition: buffer.c:508
void buf_alloc(struct Buffer *buf, size_t new_size)
Make sure a buffer can store at least new_size bytes.
Definition: buffer.c:336
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:96
CommandResult
Error codes for command_t parse functions.
Definition: command.h:36
@ MUTT_CMD_SUCCESS
Success: Command worked.
Definition: command.h:39
@ MUTT_CMD_ERROR
Error: Can't help the user.
Definition: command.h:37
@ MUTT_CMD_WARNING
Warning: Help given to the user.
Definition: command.h:38
enum CommandResult parse_rc_line_cwd(const char *line, char *cwd, struct Buffer *err)
Parse and run a muttrc line in a relative directory.
Definition: commands.c:164
char * mutt_get_sourced_cwd(void)
Get the current file path that is being parsed.
Definition: commands.c:184
Functions to parse commands in a config file.
struct PatternList * mutt_pattern_comp(struct MailboxView *mv, struct Menu *menu, const char *s, PatternCompFlags flags, struct Buffer *err)
Create a Pattern.
Definition: compile.c:905
void mutt_pattern_free(struct PatternList **pat)
Free a Pattern.
Definition: compile.c:777
int mutt_comp_valid_command(const char *cmd)
Is this command string allowed?
Definition: compress.c:414
Compressed mbox local mailbox type.
const char * cs_subset_string(const struct ConfigSubset *sub, const char *name)
Get a string config item by name.
Definition: helpers.c:292
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:48
Convenience wrapper for the config headers.
void commands_register(const struct Command *cmds, const size_t num_cmds)
Add commands to Commands array.
Definition: command.c:53
size_t commands_array(struct Command **first)
Get Commands array.
Definition: command.c:75
Convenience wrapper for the core headers.
Structs that make up an email.
bool mutt_pattern_exec(struct Pattern *pat, PatternExecFlags flags, struct Mailbox *m, struct Email *e, struct PatternCache *cache)
Match a pattern against an email header.
Definition: exec.c:1133
Parse Expando string.
struct Expando * expando_parse(const char *str, const struct ExpandoDefinition *defs, struct Buffer *err)
Parse an Expando string.
Definition: expando.c:74
void expando_free(struct Expando **ptr)
Free an Expando object.
Definition: expando.c:54
int parse_extract_token(struct Buffer *dest, struct Buffer *tok, TokenFlags flags)
Extract one token from a string.
Definition: extract.c:50
#define TOKEN_SPACE
Don't treat whitespace as a term.
Definition: extract.h:49
#define MoreArgs(buf)
Definition: extract.h:32
#define TOKEN_NO_FLAGS
No flags are set.
Definition: extract.h:46
int mutt_file_sanitize_regex(struct Buffer *dest, const char *src)
Escape any regex-magic characters in a string.
Definition: file.c:751
char * CurrentFolder
Currently selected mailbox.
Definition: globals.c:43
Global variables.
static enum CommandResult mutt_parse_unhook(struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
Parse the 'unhook' command - Implements Command::parse() -.
Definition: hook.c:572
static enum CommandResult mutt_parse_idxfmt_hook(struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
Parse the 'index-format-hook' command - Implements Command::parse() -.
Definition: hook.c:435
enum CommandResult mutt_parse_hook(struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
Parse the 'hook' family of commands - Implements Command::parse() -.
Definition: hook.c:167
enum CommandResult mutt_parse_charset_iconv_hook(struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
Parse 'charset-hook' and 'iconv-hook' commands - Implements Command::parse() -.
Definition: hook.c:124
static void idxfmt_hashelem_free(int type, void *obj, intptr_t data)
Free our hash table data - Implements hash_hdata_free_t -.
Definition: hook.c:409
#define mutt_error(...)
Definition: logging2.h:92
#define mutt_debug(LEVEL,...)
Definition: logging2.h:89
struct HashElem * mutt_hash_insert(struct HashTable *table, const char *strkey, void *data)
Add a new element to the Hash Table (with string keys)
Definition: hash.c:335
void * mutt_hash_find(const struct HashTable *table, const char *strkey)
Find the HashElem data in a Hash Table element using a key.
Definition: hash.c:362
struct HashTable * mutt_hash_new(size_t num_elems, HashFlags flags)
Create a new Hash Table (with string keys)
Definition: hash.c:259
void mutt_hash_set_destructor(struct HashTable *table, hash_hdata_free_t fn, intptr_t fn_data)
Set the destructor for a Hash Table.
Definition: hash.c:301
void mutt_hash_free(struct HashTable **ptr)
Free a hash table.
Definition: hash.c:457
#define MUTT_HASH_STRDUP_KEYS
make a copy of the keys
Definition: hash.h:111
int mutt_make_string(struct Buffer *buf, size_t max_cols, const struct Expando *exp, struct Mailbox *m, int inpgr, struct Email *e, MuttFormatFlags flags, const char *progress)
Create formatted strings using mailbox expandos.
Definition: hdrline.c:1797
String processing routines to generate the mail index.
void mutt_default_save(struct Buffer *path, struct Email *e)
Find the default save path for an email.
Definition: hook.c:770
void mutt_timeout_hook(void)
Execute any timeout hooks.
Definition: hook.c:924
static struct HashTable * IdxFmtHooks
All Index Format hooks.
Definition: hook.c:83
char * mutt_find_hook(HookFlags type, const char *pat)
Find a matching hook.
Definition: hook.c:671
void mutt_startup_shutdown_hook(HookFlags type)
Execute any startup/shutdown hooks.
Definition: hook.c:956
void mutt_delete_hooks(HookFlags type)
Delete matching hooks.
Definition: hook.c:391
static void list_hook(struct ListHead *matches, const char *match, HookFlags type)
Find hook strings matching.
Definition: hook.c:850
void mutt_account_hook(const char *url)
Perform an account hook.
Definition: hook.c:879
static void delete_idxfmt_hooks(void)
Delete all the index-format-hooks.
Definition: hook.c:427
static void hook_free(struct Hook **ptr)
Free a Hook.
Definition: hook.c:92
void mutt_folder_hook(const char *path, const char *desc)
Perform a folder hook.
Definition: hook.c:623
void mutt_select_fcc(struct Buffer *path, struct Email *e)
Select the FCC path for an email.
Definition: hook.c:808
const struct Expando * mutt_idxfmt_hook(const char *name, struct Mailbox *m, struct Email *e)
Get index-format-hook format string.
Definition: hook.c:983
static int addr_hook(struct Buffer *path, HookFlags type, struct Mailbox *m, struct Email *e)
Perform an address hook (get a path)
Definition: hook.c:738
const struct ExpandoDefinition IndexFormatDef[]
Expando definitions.
Definition: mutt_config.c:295
void hooks_init(void)
Setup feature commands.
Definition: hook.c:1043
static struct HookList Hooks
All simple hooks, e.g. MUTT_FOLDER_HOOK.
Definition: hook.c:80
static HookFlags mutt_get_hook_type(const char *name)
Find a hook by name.
Definition: hook.c:555
static HookFlags CurrentHookType
The type of the hook currently being executed, e.g. MUTT_SAVE_HOOK.
Definition: hook.c:86
static const struct Command HookCommands[]
Hook Commands.
Definition: hook.c:1016
void mutt_crypt_hook(struct ListHead *list, struct Address *addr)
Find crypto hooks for an Address.
Definition: hook.c:870
static struct Hook * hook_new(void)
Create a Hook.
Definition: hook.c:116
void mutt_message_hook(struct Mailbox *m, struct Email *e, HookFlags type)
Perform a message hook.
Definition: hook.c:692
Parse and execute user-defined hooks.
#define MUTT_OPEN_HOOK
open-hook: to read a compressed mailbox
Definition: hook.h:49
#define MUTT_ICONV_HOOK
iconv-hook: create a system charset alias
Definition: hook.h:43
#define MUTT_FOLDER_HOOK
folder-hook: when entering a mailbox
Definition: hook.h:37
#define MUTT_SAVE_HOOK
save-hook: set a default folder when saving an email
Definition: hook.h:41
#define MUTT_SEND_HOOK
send-hook: when composing a new email
Definition: hook.h:39
uint32_t HookFlags
Flags for mutt_parse_hook(), e.g. MUTT_FOLDER_HOOK.
Definition: hook.h:35
#define MUTT_FCC_HOOK
fcc-hook: to save outgoing email
Definition: hook.h:40
#define MUTT_CLOSE_HOOK
close-hook: write to a compressed mailbox
Definition: hook.h:51
#define MUTT_ACCOUNT_HOOK
account-hook: when changing between accounts
Definition: hook.h:46
#define MUTT_APPEND_HOOK
append-hook: append to a compressed mailbox
Definition: hook.h:50
#define MUTT_GLOBAL_HOOK
Hooks which don't take a regex.
Definition: hook.h:56
#define MUTT_STARTUP_HOOK
startup-hook: run when starting NeoMutt
Definition: hook.h:54
#define MUTT_SEND2_HOOK
send2-hook: when changing fields in the compose menu
Definition: hook.h:48
#define MUTT_CRYPT_HOOK
crypt-hook: automatically select a PGP/SMIME key
Definition: hook.h:45
#define MUTT_MBOX_HOOK
mbox-hook: move messages after reading them
Definition: hook.h:38
#define MUTT_REPLY_HOOK
reply-hook: when replying to an email
Definition: hook.h:47
#define MUTT_TIMEOUT_HOOK
timeout-hook: run a command periodically
Definition: hook.h:53
#define MUTT_MESSAGE_HOOK
message-hook: run before displaying a message
Definition: hook.h:44
#define MUTT_SHUTDOWN_HOOK
shutdown-hook: run when leaving NeoMutt
Definition: hook.h:55
#define MUTT_IDXFMTHOOK
index-format-hook: customise the format of the index
Definition: hook.h:52
#define MUTT_CHARSET_HOOK
charset-hook: create a charset alias for malformed emails
Definition: hook.h:42
#define MUTT_HOOK_NO_FLAGS
No flags are set.
Definition: hook.h:36
GUI manage the main index (list of emails)
struct MailboxView * get_current_mailbox_view(void)
Get the current Mailbox view.
Definition: index.c:683
struct Mailbox * get_current_mailbox(void)
Get the current Mailbox.
Definition: index.c:715
struct Menu * get_current_menu(void)
Get the current Menu.
Definition: index.c:731
struct ListNode * mutt_list_insert_tail(struct ListHead *h, char *s)
Append a string to the end of a List.
Definition: list.c:64
@ LL_DEBUG5
Log at debug level 5.
Definition: logging2.h:47
@ LL_DEBUG1
Log at debug level 1.
Definition: logging2.h:43
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
#define FREE(x)
Definition: memory.h:45
#define mutt_array_size(x)
Definition: memory.h:38
void mutt_ch_lookup_remove(void)
Remove all the character set lookups.
Definition: charset.c:541
bool mutt_ch_lookup_add(enum LookupType type, const char *pat, const char *replace, struct Buffer *err)
Add a new character set lookup.
Definition: charset.c:509
LookupType
Types of character set lookups.
Definition: charset.h:67
@ MUTT_LOOKUP_ICONV
Character set conversion.
Definition: charset.h:69
@ MUTT_LOOKUP_CHARSET
Alias for another character set.
Definition: charset.h:68
Convenience wrapper for the library headers.
#define _(a)
Definition: message.h:28
bool mutt_regex_match(const struct Regex *regex, const char *str)
Shorthand to mutt_regex_capture()
Definition: regex.c:614
bool mutt_istr_equal(const char *a, const char *b)
Compare two strings, ignoring case.
Definition: string.c:666
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:253
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:654
#define PATH_MAX
Definition: mutt.h:42
void mutt_temp_attachments_cleanup(void)
Delete all temporary attachments.
Definition: mutt_attach.c:1307
void buf_expand_path_regex(struct Buffer *buf, bool regex)
Create the canonical path (with regex char escaping)
Definition: muttlib.c:135
void buf_pretty_mailbox(struct Buffer *buf)
Shorten a mailbox path using '~' or '='.
Definition: muttlib.c:554
void buf_expand_path(struct Buffer *buf)
Create the canonical path.
Definition: muttlib.c:328
void mutt_safe_path(struct Buffer *dest, const struct Address *a)
Make a safe filename from an email address.
Definition: muttlib.c:718
Some miscellaneous functions.
int mx_access(const char *path, int flags)
Wrapper for access, checks permissions on a given mailbox.
Definition: mx.c:168
API for mailboxes.
API for encryption/signing of emails.
#define WithCrypto
Definition: lib.h:116
Text parsing functions.
Match patterns to emails.
#define MUTT_PC_SEND_MODE_SEARCH
Allow send-mode body searching.
Definition: lib.h:71
uint8_t PatternCompFlags
Flags for mutt_pattern_comp(), e.g. MUTT_PC_FULL_MSG.
Definition: lib.h:67
#define MUTT_PC_FULL_MSG
Enable body and header matching.
Definition: lib.h:69
void mutt_check_simple(struct Buffer *s, const char *simple)
Convert a simple search into a real request.
Definition: pattern.c:112
#define MUTT_PC_NO_FLAGS
No flags are set.
Definition: lib.h:68
#define MUTT_PC_PATTERN_DYNAMIC
Enable runtime date range evaluation.
Definition: lib.h:70
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:81
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition: pool.c:94
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:725
#define TAILQ_FOREACH_SAFE(var, head, field, tvar)
Definition: queue.h:735
#define TAILQ_INIT(head)
Definition: queue.h:765
#define TAILQ_HEAD(name, type)
Definition: queue.h:623
#define TAILQ_INSERT_TAIL(head, elm, field)
Definition: queue.h:809
#define TAILQ_FIRST(head)
Definition: queue.h:723
#define SLIST_FIRST(head)
Definition: queue.h:229
#define TAILQ_REMOVE(head, elm, field)
Definition: queue.h:841
#define TAILQ_HEAD_INITIALIZER(head)
Definition: queue.h:637
#define TAILQ_ENTRY(type)
Definition: queue.h:640
#define REG_COMP(preg, regex, cflags)
Compile a regular expression.
Definition: regex3.h:49
#define MUTT_FORMAT_PLAIN
Do not prepend DISP_TO, DISP_CC ...
Definition: render.h:39
#define NONULL(x)
Definition: string2.h:37
#define SKIPWS(ch)
Definition: string2.h:45
An email address.
Definition: address.h:36
struct Buffer * mailbox
Mailbox and host address.
Definition: address.h:38
String manipulation buffer.
Definition: buffer.h:36
char * dptr
Current read/write position.
Definition: buffer.h:38
size_t dsize
Length of data.
Definition: buffer.h:39
char * data
Pointer to data.
Definition: buffer.h:37
enum CommandResult(* parse)(struct Buffer *buf, struct Buffer *s, intptr_t data, struct Buffer *err)
Definition: command.h:65
intptr_t data
Data or flags to pass to the command.
Definition: command.h:67
const char * name
Name of the command.
Definition: command.h:52
The envelope/body of an email.
Definition: email.h:39
struct Envelope * env
Envelope information.
Definition: email.h:68
The header of an Email.
Definition: envelope.h:57
struct AddressList to
Email's 'To' list.
Definition: envelope.h:60
struct AddressList reply_to
Email's 'reply-to'.
Definition: envelope.h:64
struct AddressList cc
Email's 'Cc' list.
Definition: envelope.h:61
struct AddressList bcc
Email's 'Bcc' list.
Definition: envelope.h:62
struct AddressList from
Email's 'From' list.
Definition: envelope.h:59
Definition of a format string.
Definition: definition.h:52
Parsed Expando trees.
Definition: expando.h:41
A Hash Table.
Definition: hash.h:97
A list of user hooks.
Definition: hook.c:68
HookFlags type
Hook type.
Definition: hook.c:69
struct PatternList * pattern
Used for fcc,save,send-hook.
Definition: hook.c:73
struct Regex regex
Regular expression.
Definition: hook.c:70
char * command
Filename, command or pattern to execute.
Definition: hook.c:71
struct Expando * expando
Used for format hooks.
Definition: hook.c:74
char * source_file
Used for relative-directory source.
Definition: hook.c:72
View of a Mailbox.
Definition: mview.h:40
A mailbox.
Definition: mailbox.h:79
Definition: lib.h:79
Container for Accounts, Notifications.
Definition: neomutt.h:41
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:45
Cache commonly-used patterns.
Definition: lib.h:117
A simple (non-regex) pattern.
Definition: lib.h:77
Cached regular expression.
Definition: regex3.h:85
char * pattern
printable version
Definition: regex3.h:86
bool pat_not
do not match
Definition: regex3.h:88
regex_t * regex
compiled expression
Definition: regex3.h:87