NeoMutt  2023-05-17-56-ga67199
Teaching an old dog new tricks
DOXYGEN
hook.c
Go to the documentation of this file.
1
31#include "config.h"
32#include <limits.h>
33#include <stdbool.h>
34#include <stdio.h>
35#include <string.h>
36#include <unistd.h>
37#include "mutt/lib.h"
38#include "address/lib.h"
39#include "config/lib.h"
40#include "email/lib.h"
41#include "core/lib.h"
42#include "alias/lib.h"
43#include "hook.h"
44#include "attach/lib.h"
45#include "index/lib.h"
46#include "ncrypt/lib.h"
47#include "parse/lib.h"
48#include "pattern/lib.h"
49#include "commands.h"
50#include "format_flags.h"
51#include "globals.h" // IWYU pragma: keep
52#include "hdrline.h"
53#include "muttlib.h"
54#include "mx.h"
55#ifdef USE_COMP_MBOX
56#include "compmbox/lib.h"
57#endif
58
62struct Hook
63{
65 struct Regex regex;
66 char *command;
68 struct PatternList *pattern;
69 TAILQ_ENTRY(Hook) entries;
70};
71TAILQ_HEAD(HookList, Hook);
72
74static struct HookList Hooks = TAILQ_HEAD_INITIALIZER(Hooks);
75
77static struct HashTable *IdxFmtHooks = NULL;
78
81
86 intptr_t data, struct Buffer *err)
87{
88 struct Buffer *alias = buf_pool_get();
89 struct Buffer *charset = buf_pool_get();
90
91 int rc = MUTT_CMD_ERROR;
92 int retval = 0;
93
94 retval += parse_extract_token(alias, s, TOKEN_NO_FLAGS);
95 retval += parse_extract_token(charset, s, TOKEN_NO_FLAGS);
96 if (retval != 0)
97 goto done;
98
100
101 if (buf_is_empty(alias) || buf_is_empty(charset))
102 {
103 buf_printf(err, _("%s: too few arguments"), buf->data);
104 rc = MUTT_CMD_WARNING;
105 }
106 else if (MoreArgs(s))
107 {
108 buf_printf(err, _("%s: too many arguments"), buf->data);
109 buf_reset(s); // clean up buffer to avoid a mess with further rcfile processing
110 rc = MUTT_CMD_WARNING;
111 }
112 else if (mutt_ch_lookup_add(type, buf_string(alias), buf_string(charset), err))
113 {
114 rc = MUTT_CMD_SUCCESS;
115 }
116
117done:
118 buf_pool_release(&alias);
119 buf_pool_release(&charset);
120
121 return rc;
122}
123
129enum CommandResult mutt_parse_hook(struct Buffer *buf, struct Buffer *s,
130 intptr_t data, struct Buffer *err)
131{
132 struct Hook *hook = NULL;
133 int rc = MUTT_CMD_ERROR;
134 bool pat_not = false;
135 bool use_regex = true;
136 regex_t *rx = NULL;
137 struct PatternList *pat = NULL;
138 const bool folder_or_mbox = (data & (MUTT_FOLDER_HOOK | MUTT_MBOX_HOOK));
139
140 struct Buffer *cmd = buf_pool_get();
141 struct Buffer *pattern = buf_pool_get();
142
143 if (~data & MUTT_GLOBAL_HOOK) /* NOT a global hook */
144 {
145 if (*s->dptr == '!')
146 {
147 s->dptr++;
148 SKIPWS(s->dptr);
149 pat_not = true;
150 }
151
153 if (folder_or_mbox && mutt_str_equal(buf_string(pattern), "-noregex"))
154 {
155 use_regex = false;
156 if (!MoreArgs(s))
157 {
158 buf_printf(err, _("%s: too few arguments"), buf->data);
159 rc = MUTT_CMD_WARNING;
160 goto cleanup;
161 }
163 }
164
165 if (!MoreArgs(s))
166 {
167 buf_printf(err, _("%s: too few arguments"), buf->data);
168 rc = MUTT_CMD_WARNING;
169 goto cleanup;
170 }
171 }
172
173 parse_extract_token(cmd, s,
178
179 if (buf_is_empty(cmd))
180 {
181 buf_printf(err, _("%s: too few arguments"), buf->data);
182 rc = MUTT_CMD_WARNING;
183 goto cleanup;
184 }
185
186 if (MoreArgs(s))
187 {
188 buf_printf(err, _("%s: too many arguments"), buf->data);
189 rc = MUTT_CMD_WARNING;
190 goto cleanup;
191 }
192
193 const char *const c_default_hook = cs_subset_string(NeoMutt->sub, "default_hook");
194 if (folder_or_mbox)
195 {
196 /* Accidentally using the ^ mailbox shortcut in the .neomuttrc is a
197 * common mistake */
198 if ((pattern->data[0] == '^') && !CurrentFolder)
199 {
200 buf_strcpy(err, _("current mailbox shortcut '^' is unset"));
201 goto cleanup;
202 }
203
204 struct Buffer *tmp = buf_pool_get();
205 buf_copy(tmp, pattern);
206 buf_expand_path_regex(tmp, use_regex);
207
208 /* Check for other mailbox shortcuts that expand to the empty string.
209 * This is likely a mistake too */
210 if (buf_is_empty(tmp) && !buf_is_empty(pattern))
211 {
212 buf_strcpy(err, _("mailbox shortcut expanded to empty regex"));
213 buf_pool_release(&tmp);
214 goto cleanup;
215 }
216
217 if (use_regex)
218 {
219 buf_copy(pattern, tmp);
220 }
221 else
222 {
224 }
225 buf_pool_release(&tmp);
226 }
227#ifdef USE_COMP_MBOX
229 {
230 if (mutt_comp_valid_command(buf_string(cmd)) == 0)
231 {
232 buf_strcpy(err, _("badly formatted command string"));
233 goto cleanup;
234 }
235 }
236#endif
237 else if (c_default_hook && (~data & MUTT_GLOBAL_HOOK) &&
239 {
240 /* At this stage only these hooks remain:
241 * fcc-, fcc-save-, index-format-, message-, reply-, save-, send- and send2-hook
242 * If given a plain string, or regex, we expand it using $default_hook. */
243 mutt_check_simple(pattern, c_default_hook);
244 }
245
247 {
248 buf_expand_path(cmd);
249 }
250
251 /* check to make sure that a matching hook doesn't already exist */
252 TAILQ_FOREACH(hook, &Hooks, entries)
253 {
255 {
256 /* Ignore duplicate global hooks */
257 if (mutt_str_equal(hook->command, buf_string(cmd)))
258 {
259 rc = MUTT_CMD_SUCCESS;
260 goto cleanup;
261 }
262 }
263 else if ((hook->type == data) && (hook->regex.pat_not == pat_not) &&
264 mutt_str_equal(buf_string(pattern), hook->regex.pattern))
265 {
269 {
270 /* these hooks allow multiple commands with the same
271 * pattern, so if we've already seen this pattern/command pair, just
272 * ignore it instead of creating a duplicate */
273 if (mutt_str_equal(hook->command, buf_string(cmd)))
274 {
275 rc = MUTT_CMD_SUCCESS;
276 goto cleanup;
277 }
278 }
279 else
280 {
281 /* other hooks only allow one command per pattern, so update the
282 * entry with the new command. this currently does not change the
283 * order of execution of the hooks, which i think is desirable since
284 * a common action to perform is to change the default (.) entry
285 * based upon some other information. */
286 FREE(&hook->command);
287 hook->command = buf_strdup(cmd);
289 rc = MUTT_CMD_SUCCESS;
290 goto cleanup;
291 }
292 }
293 }
294
297 {
298 PatternCompFlags comp_flags;
299
300 if (data & (MUTT_SEND2_HOOK))
301 comp_flags = MUTT_PC_SEND_MODE_SEARCH;
302 else if (data & (MUTT_SEND_HOOK | MUTT_FCC_HOOK))
303 comp_flags = MUTT_PC_NO_FLAGS;
304 else
305 comp_flags = MUTT_PC_FULL_MSG;
306
307 struct MailboxView *mv_cur = get_current_mailbox_view();
308 struct Menu *menu = get_current_menu();
309 pat = mutt_pattern_comp(mv_cur, menu, buf_string(pattern), comp_flags, err);
310 if (!pat)
311 goto cleanup;
312 }
313 else if (~data & MUTT_GLOBAL_HOOK) /* NOT a global hook */
314 {
315 /* Hooks not allowing full patterns: Check syntax of regex */
316 rx = mutt_mem_calloc(1, sizeof(regex_t));
317 int rc2 = REG_COMP(rx, buf_string(pattern), ((data & MUTT_CRYPT_HOOK) ? REG_ICASE : 0));
318 if (rc2 != 0)
319 {
320 regerror(rc2, rx, err->data, err->dsize);
321 FREE(&rx);
322 goto cleanup;
323 }
324 }
325
326 hook = mutt_mem_calloc(1, sizeof(struct Hook));
327 hook->type = data;
328 hook->command = buf_strdup(cmd);
330 hook->pattern = pat;
331 hook->regex.pattern = buf_strdup(pattern);
332 hook->regex.regex = rx;
333 hook->regex.pat_not = pat_not;
334 TAILQ_INSERT_TAIL(&Hooks, hook, entries);
335 rc = MUTT_CMD_SUCCESS;
336
337cleanup:
338 buf_pool_release(&cmd);
339 buf_pool_release(&pattern);
340 return rc;
341}
342
347static void delete_hook(struct Hook *h)
348{
349 FREE(&h->command);
350 FREE(&h->source_file);
351 FREE(&h->regex.pattern);
352 if (h->regex.regex)
353 {
354 regfree(h->regex.regex);
355 FREE(&h->regex.regex);
356 }
358 FREE(&h);
359}
360
368{
369 struct Hook *h = NULL;
370 struct Hook *tmp = NULL;
371
372 TAILQ_FOREACH_SAFE(h, &Hooks, entries, tmp)
373 {
374 if ((type == MUTT_HOOK_NO_FLAGS) || (type == h->type))
375 {
376 TAILQ_REMOVE(&Hooks, h, entries);
377 delete_hook(h);
378 }
379 }
380}
381
385static void idxfmt_hashelem_free(int type, void *obj, intptr_t data)
386{
387 struct HookList *hl = obj;
388 struct Hook *h = NULL;
389 struct Hook *tmp = NULL;
390
391 TAILQ_FOREACH_SAFE(h, hl, entries, tmp)
392 {
393 TAILQ_REMOVE(hl, h, entries);
394 delete_hook(h);
395 }
396
397 FREE(&hl);
398}
399
403static void delete_idxfmt_hooks(void)
404{
406}
407
411static enum CommandResult mutt_parse_idxfmt_hook(struct Buffer *buf, struct Buffer *s,
412 intptr_t data, struct Buffer *err)
413{
415 bool pat_not = false;
416
417 struct Buffer *name = buf_pool_get();
418 struct Buffer *pattern = buf_pool_get();
419 struct Buffer *fmtstring = buf_pool_get();
420
421 if (!IdxFmtHooks)
422 {
425 }
426
427 if (!MoreArgs(s))
428 {
429 buf_printf(err, _("%s: too few arguments"), buf->data);
430 goto out;
431 }
433 struct HookList *hl = mutt_hash_find(IdxFmtHooks, buf_string(name));
434
435 if (*s->dptr == '!')
436 {
437 s->dptr++;
438 SKIPWS(s->dptr);
439 pat_not = true;
440 }
442
443 if (!MoreArgs(s))
444 {
445 buf_printf(err, _("%s: too few arguments"), buf->data);
446 goto out;
447 }
448 parse_extract_token(fmtstring, s, TOKEN_NO_FLAGS);
449
450 if (MoreArgs(s))
451 {
452 buf_printf(err, _("%s: too many arguments"), buf->data);
453 goto out;
454 }
455
456 const char *const c_default_hook = cs_subset_string(NeoMutt->sub, "default_hook");
457 if (c_default_hook)
458 mutt_check_simple(pattern, c_default_hook);
459
460 /* check to make sure that a matching hook doesn't already exist */
461 struct Hook *hook = NULL;
462 if (hl)
463 {
464 TAILQ_FOREACH(hook, hl, entries)
465 {
466 if ((hook->regex.pat_not == pat_not) &&
468 {
469 mutt_str_replace(&hook->command, buf_string(fmtstring));
470 rc = MUTT_CMD_SUCCESS;
471 goto out;
472 }
473 }
474 }
475
476 /* MUTT_PC_PATTERN_DYNAMIC sets so that date ranges are regenerated during
477 * matching. This of course is slower, but index-format-hook is commonly
478 * used for date ranges, and they need to be evaluated relative to "now", not
479 * the hook compilation time. */
480 struct MailboxView *mv_cur = get_current_mailbox_view();
481 struct Menu *menu = get_current_menu();
482 struct PatternList *pat = mutt_pattern_comp(mv_cur, menu, buf_string(pattern),
484 err);
485 if (!pat)
486 goto out;
487
488 hook = mutt_mem_calloc(1, sizeof(struct Hook));
489 hook->type = MUTT_IDXFMTHOOK;
490 hook->command = buf_strdup(fmtstring);
492 hook->pattern = pat;
493 hook->regex.pattern = buf_strdup(pattern);
494 hook->regex.regex = NULL;
495 hook->regex.pat_not = pat_not;
496
497 if (!hl)
498 {
499 hl = mutt_mem_calloc(1, sizeof(*hl));
500 TAILQ_INIT(hl);
502 }
503
504 TAILQ_INSERT_TAIL(hl, hook, entries);
505 rc = MUTT_CMD_SUCCESS;
506
507out:
508 buf_pool_release(&name);
509 buf_pool_release(&pattern);
510 buf_pool_release(&fmtstring);
511
512 return rc;
513}
514
521static HookFlags mutt_get_hook_type(const char *name)
522{
523 struct Command *c = NULL;
524 for (size_t i = 0, size = commands_array(&c); i < size; i++)
525 {
526 if (((c[i].parse == mutt_parse_hook) || (c[i].parse == mutt_parse_idxfmt_hook)) &&
528 {
529 return c[i].data;
530 }
531 }
532 return MUTT_HOOK_NO_FLAGS;
533}
534
538static enum CommandResult mutt_parse_unhook(struct Buffer *buf, struct Buffer *s,
539 intptr_t data, struct Buffer *err)
540{
541 while (MoreArgs(s))
542 {
544 if (mutt_str_equal("*", buf->data))
545 {
547 {
548 buf_printf(err, "%s", _("unhook: Can't do unhook * from within a hook"));
549 return MUTT_CMD_WARNING;
550 }
554 }
555 else
556 {
557 HookFlags type = mutt_get_hook_type(buf->data);
558
559 if (type == MUTT_HOOK_NO_FLAGS)
560 {
561 buf_printf(err, _("unhook: unknown hook type: %s"), buf->data);
562 return MUTT_CMD_ERROR;
563 }
564 if (type & (MUTT_CHARSET_HOOK | MUTT_ICONV_HOOK))
565 {
567 return MUTT_CMD_SUCCESS;
568 }
569 if (CurrentHookType == type)
570 {
571 buf_printf(err, _("unhook: Can't delete a %s from within a %s"),
572 buf->data, buf->data);
573 return MUTT_CMD_WARNING;
574 }
575 if (type == MUTT_IDXFMTHOOK)
577 else
578 mutt_delete_hooks(type);
579 }
580 }
581 return MUTT_CMD_SUCCESS;
582}
583
589void mutt_folder_hook(const char *path, const char *desc)
590{
591 if (!path && !desc)
592 return;
593
594 struct Hook *hook = NULL;
595 struct Buffer *err = buf_pool_get();
596
598
599 TAILQ_FOREACH(hook, &Hooks, entries)
600 {
601 if (!hook->command)
602 continue;
603
604 if (!(hook->type & MUTT_FOLDER_HOOK))
605 continue;
606
607 const char *match = NULL;
608 if (mutt_regex_match(&hook->regex, path))
609 match = path;
610 else if (mutt_regex_match(&hook->regex, desc))
611 match = desc;
612
613 if (match)
614 {
615 mutt_debug(LL_DEBUG1, "folder-hook '%s' matches '%s'\n", hook->regex.pattern, match);
616 mutt_debug(LL_DEBUG5, " %s\n", hook->command);
617 if (parse_rc_line_cwd(hook->command, hook->source_file, err) == MUTT_CMD_ERROR)
618 {
619 mutt_error("%s", buf_string(err));
620 break;
621 }
622 }
623 }
624 buf_pool_release(&err);
625
627}
628
637char *mutt_find_hook(HookFlags type, const char *pat)
638{
639 struct Hook *tmp = NULL;
640
641 TAILQ_FOREACH(tmp, &Hooks, entries)
642 {
643 if (tmp->type & type)
644 {
645 if (mutt_regex_match(&tmp->regex, pat))
646 return tmp->command;
647 }
648 }
649 return NULL;
650}
651
658void mutt_message_hook(struct Mailbox *m, struct Email *e, HookFlags type)
659{
660 struct Hook *hook = NULL;
661 struct PatternCache cache = { 0 };
662 struct Buffer *err = buf_pool_get();
663
664 CurrentHookType = type;
665
666 TAILQ_FOREACH(hook, &Hooks, entries)
667 {
668 if (!hook->command)
669 continue;
670
671 if (hook->type & type)
672 {
673 if ((mutt_pattern_exec(SLIST_FIRST(hook->pattern), 0, m, e, &cache) > 0) ^
674 hook->regex.pat_not)
675 {
676 if (parse_rc_line_cwd(hook->command, hook->source_file, err) == MUTT_CMD_ERROR)
677 {
678 mutt_error("%s", buf_string(err));
680 buf_pool_release(&err);
681
682 return;
683 }
684 /* Executing arbitrary commands could affect the pattern results,
685 * so the cache has to be wiped */
686 memset(&cache, 0, sizeof(cache));
687 }
688 }
689 }
690 buf_pool_release(&err);
691
693}
694
705static int addr_hook(char *path, size_t pathlen, HookFlags type,
706 struct Mailbox *m, struct Email *e)
707{
708 struct Hook *hook = NULL;
709 struct PatternCache cache = { 0 };
710
711 /* determine if a matching hook exists */
712 TAILQ_FOREACH(hook, &Hooks, entries)
713 {
714 if (!hook->command)
715 continue;
716
717 if (hook->type & type)
718 {
719 if ((mutt_pattern_exec(SLIST_FIRST(hook->pattern), 0, m, e, &cache) > 0) ^
720 hook->regex.pat_not)
721 {
722 mutt_make_string(path, pathlen, 0, hook->command, m, -1, e, MUTT_FORMAT_PLAIN, NULL);
723 return 0;
724 }
725 }
726 }
727
728 return -1;
729}
730
737void mutt_default_save(char *path, size_t pathlen, struct Email *e)
738{
739 *path = '\0';
740 struct Mailbox *m_cur = get_current_mailbox();
741 if (addr_hook(path, pathlen, MUTT_SAVE_HOOK, m_cur, e) == 0)
742 return;
743
744 struct Envelope *env = e->env;
745 const struct Address *from = TAILQ_FIRST(&env->from);
746 const struct Address *reply_to = TAILQ_FIRST(&env->reply_to);
747 const struct Address *to = TAILQ_FIRST(&env->to);
748 const struct Address *cc = TAILQ_FIRST(&env->cc);
749 const struct Address *addr = NULL;
750 bool from_me = mutt_addr_is_user(from);
751
752 if (!from_me && reply_to && reply_to->mailbox)
753 addr = reply_to;
754 else if (!from_me && from && from->mailbox)
755 addr = from;
756 else if (to && to->mailbox)
757 addr = to;
758 else if (cc && cc->mailbox)
759 addr = cc;
760 else
761 addr = NULL;
762 if (addr)
763 {
764 struct Buffer *tmp = buf_pool_get();
765 mutt_safe_path(tmp, addr);
766 snprintf(path, pathlen, "=%s", buf_string(tmp));
767 buf_pool_release(&tmp);
768 }
769}
770
776void mutt_select_fcc(struct Buffer *path, struct Email *e)
777{
778 buf_alloc(path, PATH_MAX);
779
780 if (addr_hook(path->data, path->dsize, MUTT_FCC_HOOK, NULL, e) != 0)
781 {
782 const struct Address *to = TAILQ_FIRST(&e->env->to);
783 const struct Address *cc = TAILQ_FIRST(&e->env->cc);
784 const struct Address *bcc = TAILQ_FIRST(&e->env->bcc);
785 const bool c_save_name = cs_subset_bool(NeoMutt->sub, "save_name");
786 const bool c_force_name = cs_subset_bool(NeoMutt->sub, "force_name");
787 const char *const c_record = cs_subset_string(NeoMutt->sub, "record");
788 if ((c_save_name || c_force_name) && (to || cc || bcc))
789 {
790 const struct Address *addr = to ? to : (cc ? cc : bcc);
791 struct Buffer *buf = buf_pool_get();
792 mutt_safe_path(buf, addr);
793 const char *const c_folder = cs_subset_string(NeoMutt->sub, "folder");
794 buf_concat_path(path, NONULL(c_folder), buf_string(buf));
795 buf_pool_release(&buf);
796 if (!c_force_name && (mx_access(buf_string(path), W_OK) != 0))
797 buf_strcpy(path, c_record);
798 }
799 else
800 {
801 buf_strcpy(path, c_record);
802 }
803 }
804 else
805 {
806 buf_fix_dptr(path);
807 }
808
809 buf_pretty_mailbox(path);
810}
811
818static void list_hook(struct ListHead *matches, const char *match, HookFlags type)
819{
820 struct Hook *tmp = NULL;
821
822 TAILQ_FOREACH(tmp, &Hooks, entries)
823 {
824 if ((tmp->type & type) && mutt_regex_match(&tmp->regex, match))
825 {
827 }
828 }
829}
830
838void mutt_crypt_hook(struct ListHead *list, struct Address *addr)
839{
841}
842
847void mutt_account_hook(const char *url)
848{
849 /* parsing commands with URLs in an account hook can cause a recursive
850 * call. We just skip processing if this occurs. Typically such commands
851 * belong in a folder-hook -- perhaps we should warn the user. */
852 static bool inhook = false;
853 if (inhook)
854 return;
855
856 struct Hook *hook = NULL;
857 struct Buffer *err = buf_pool_get();
858
859 TAILQ_FOREACH(hook, &Hooks, entries)
860 {
861 if (!(hook->command && (hook->type & MUTT_ACCOUNT_HOOK)))
862 continue;
863
864 if (mutt_regex_match(&hook->regex, url))
865 {
866 inhook = true;
867 mutt_debug(LL_DEBUG1, "account-hook '%s' matches '%s'\n", hook->regex.pattern, url);
868 mutt_debug(LL_DEBUG5, " %s\n", hook->command);
869
870 if (parse_rc_line_cwd(hook->command, hook->source_file, err) == MUTT_CMD_ERROR)
871 {
872 mutt_error("%s", buf_string(err));
873 buf_pool_release(&err);
874
875 inhook = false;
876 goto done;
877 }
878
879 inhook = false;
880 }
881 }
882done:
883 buf_pool_release(&err);
884}
885
893{
894 struct Hook *hook = NULL;
895 struct Buffer err;
896 char buf[256] = { 0 };
897
898 buf_init(&err);
899 err.data = buf;
900 err.dsize = sizeof(buf);
901
902 TAILQ_FOREACH(hook, &Hooks, entries)
903 {
904 if (!(hook->command && (hook->type & MUTT_TIMEOUT_HOOK)))
905 continue;
906
907 if (parse_rc_line_cwd(hook->command, hook->source_file, &err) == MUTT_CMD_ERROR)
908 {
909 mutt_error("%s", err.data);
910 buf_reset(&err);
911
912 /* The hooks should be independent of each other, so even though this on
913 * failed, we'll carry on with the others. */
914 }
915 }
916
917 /* Delete temporary attachment files */
919}
920
929{
930 struct Hook *hook = NULL;
931 struct Buffer err = buf_make(0);
932 char buf[256] = { 0 };
933
934 err.data = buf;
935 err.dsize = sizeof(buf);
936
937 TAILQ_FOREACH(hook, &Hooks, entries)
938 {
939 if (!(hook->command && (hook->type & type)))
940 continue;
941
942 if (parse_rc_line_cwd(hook->command, hook->source_file, &err) == MUTT_CMD_ERROR)
943 {
944 mutt_error("%s", err.data);
945 buf_reset(&err);
946 }
947 }
948}
949
958const char *mutt_idxfmt_hook(const char *name, struct Mailbox *m, struct Email *e)
959{
960 if (!IdxFmtHooks)
961 return NULL;
962
963 struct HookList *hl = mutt_hash_find(IdxFmtHooks, name);
964 if (!hl)
965 return NULL;
966
968
969 struct PatternCache cache = { 0 };
970 const char *fmtstring = NULL;
971 struct Hook *hook = NULL;
972
973 TAILQ_FOREACH(hook, hl, entries)
974 {
975 struct Pattern *pat = SLIST_FIRST(hook->pattern);
976 if ((mutt_pattern_exec(pat, 0, m, e, &cache) > 0) ^ hook->regex.pat_not)
977 {
978 fmtstring = hook->command;
979 break;
980 }
981 }
982
984
985 return fmtstring;
986}
987
991static const struct Command HookCommands[] = {
992 // clang-format off
993 { "account-hook", mutt_parse_hook, MUTT_ACCOUNT_HOOK },
995 { "crypt-hook", mutt_parse_hook, MUTT_CRYPT_HOOK },
996 { "fcc-hook", mutt_parse_hook, MUTT_FCC_HOOK },
997 { "fcc-save-hook", mutt_parse_hook, MUTT_FCC_HOOK | MUTT_SAVE_HOOK },
998 { "folder-hook", mutt_parse_hook, MUTT_FOLDER_HOOK },
1000 { "index-format-hook", mutt_parse_idxfmt_hook, MUTT_IDXFMTHOOK },
1001 { "mbox-hook", mutt_parse_hook, MUTT_MBOX_HOOK },
1002 { "message-hook", mutt_parse_hook, MUTT_MESSAGE_HOOK },
1003 { "pgp-hook", mutt_parse_hook, MUTT_CRYPT_HOOK },
1004 { "reply-hook", mutt_parse_hook, MUTT_REPLY_HOOK },
1005 { "save-hook", mutt_parse_hook, MUTT_SAVE_HOOK },
1006 { "send-hook", mutt_parse_hook, MUTT_SEND_HOOK },
1007 { "send2-hook", mutt_parse_hook, MUTT_SEND2_HOOK },
1008 { "shutdown-hook", mutt_parse_hook, MUTT_SHUTDOWN_HOOK | MUTT_GLOBAL_HOOK },
1009 { "startup-hook", mutt_parse_hook, MUTT_STARTUP_HOOK | MUTT_GLOBAL_HOOK },
1010 { "timeout-hook", mutt_parse_hook, MUTT_TIMEOUT_HOOK | MUTT_GLOBAL_HOOK },
1011 { "unhook", mutt_parse_unhook, 0 },
1012 // clang-format on
1013};
1014
1018void hooks_init(void)
1019{
1021}
Email Address Handling.
Email Aliases.
bool mutt_addr_is_user(const struct Address *addr)
Does the address belong to the user.
Definition: alias.c:569
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:173
void buf_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition: buffer.c:88
bool buf_is_empty(const struct Buffer *buf)
Is the Buffer empty?
Definition: buffer.c:303
struct Buffer buf_make(size_t size)
Make a new buffer on the stack.
Definition: buffer.c:70
void buf_fix_dptr(struct Buffer *buf)
Move the dptr to end of the Buffer.
Definition: buffer.c:194
struct Buffer * buf_init(struct Buffer *buf)
Initialise a new Buffer.
Definition: buffer.c:55
size_t buf_strcpy(struct Buffer *buf, const char *s)
Copy a string into a Buffer.
Definition: buffer.c:401
size_t buf_copy(struct Buffer *dst, const struct Buffer *src)
Copy a Buffer's contents to another Buffer.
Definition: buffer.c:566
char * buf_strdup(const struct Buffer *buf)
Copy a Buffer's string.
Definition: buffer.c:536
size_t buf_concat_path(struct Buffer *buf, const char *dir, const char *fname)
Join a directory name and a filename.
Definition: buffer.c:478
void buf_alloc(struct Buffer *buf, size_t new_size)
Make sure a buffer can store at least new_size bytes.
Definition: buffer.c:347
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:90
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:150
char * mutt_get_sourced_cwd(void)
Get the current file path that is being parsed.
Definition: commands.c:170
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:415
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:317
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:73
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
int parse_extract_token(struct Buffer *dest, struct Buffer *tok, TokenFlags flags)
Extract one token from a string.
Definition: extract.c:47
#define TOKEN_SPACE
Don't treat whitespace as a term.
Definition: extract.h:47
#define MoreArgs(buf)
Definition: extract.h:30
#define TOKEN_NO_FLAGS
No flags are set.
Definition: extract.h:44
int mutt_file_sanitize_regex(struct Buffer *dest, const char *src)
Escape any regex-magic characters in a string.
Definition: file.c:684
Flags to control mutt_expando_format()
#define MUTT_FORMAT_PLAIN
Do not prepend DISP_TO, DISP_CC ...
Definition: format_flags.h:38
char * CurrentFolder
Currently selected mailbox.
Definition: globals.c:44
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:538
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:411
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:129
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:85
static void idxfmt_hashelem_free(int type, void *obj, intptr_t data)
Delete an index-format-hook from the Hash Table - Implements hash_hdata_free_t -.
Definition: hook.c:385
#define mutt_error(...)
Definition: logging2.h:90
#define mutt_debug(LEVEL,...)
Definition: logging2.h:87
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:112
void mutt_make_string(char *buf, size_t buflen, int cols, const char *s, struct Mailbox *m, int inpgr, struct Email *e, MuttFormatFlags flags, const char *progress)
Create formatted strings using mailbox expandos.
Definition: hdrline.c:1489
String processing routines to generate the mail index.
static int addr_hook(char *path, size_t pathlen, HookFlags type, struct Mailbox *m, struct Email *e)
Perform an address hook (get a path)
Definition: hook.c:705
void mutt_timeout_hook(void)
Execute any timeout hooks.
Definition: hook.c:892
static struct HashTable * IdxFmtHooks
All Index Format hooks.
Definition: hook.c:77
char * mutt_find_hook(HookFlags type, const char *pat)
Find a matching hook.
Definition: hook.c:637
void mutt_startup_shutdown_hook(HookFlags type)
Execute any startup/shutdown hooks.
Definition: hook.c:928
void mutt_delete_hooks(HookFlags type)
Delete matching hooks.
Definition: hook.c:367
static void list_hook(struct ListHead *matches, const char *match, HookFlags type)
Find hook strings matching.
Definition: hook.c:818
void mutt_account_hook(const char *url)
Perform an account hook.
Definition: hook.c:847
static void delete_idxfmt_hooks(void)
Delete all the index-format-hooks.
Definition: hook.c:403
const char * mutt_idxfmt_hook(const char *name, struct Mailbox *m, struct Email *e)
Get index-format-hook format string.
Definition: hook.c:958
void mutt_default_save(char *path, size_t pathlen, struct Email *e)
Find the default save path for an email.
Definition: hook.c:737
void mutt_folder_hook(const char *path, const char *desc)
Perform a folder hook.
Definition: hook.c:589
void mutt_select_fcc(struct Buffer *path, struct Email *e)
Select the FCC path for an email.
Definition: hook.c:776
void hooks_init(void)
Setup feature commands.
Definition: hook.c:1018
static struct HookList Hooks
All simple hooks, e.g. MUTT_FOLDER_HOOK.
Definition: hook.c:74
static HookFlags mutt_get_hook_type(const char *name)
Find a hook by name.
Definition: hook.c:521
TAILQ_HEAD(HookList, Hook)
static HookFlags CurrentHookType
The type of the hook currently being executed, e.g. MUTT_SAVE_HOOK.
Definition: hook.c:80
static const struct Command HookCommands[]
Hook Commands.
Definition: hook.c:991
void mutt_crypt_hook(struct ListHead *list, struct Address *addr)
Find crypto hooks for an Address.
Definition: hook.c:838
static void delete_hook(struct Hook *h)
Delete a Hook.
Definition: hook.c:347
void mutt_message_hook(struct Mailbox *m, struct Email *e, HookFlags type)
Perform a message hook.
Definition: hook.c:658
Parse and execute user-defined hooks.
#define MUTT_OPEN_HOOK
open-hook: to read a compressed mailbox
Definition: hook.h:51
#define MUTT_ICONV_HOOK
iconv-hook: create a system charset alias
Definition: hook.h:44
#define MUTT_FOLDER_HOOK
folder-hook: when entering a mailbox
Definition: hook.h:38
#define MUTT_SAVE_HOOK
save-hook: set a default folder when saving an email
Definition: hook.h:42
#define MUTT_SEND_HOOK
send-hook: when composing a new email
Definition: hook.h:40
uint32_t HookFlags
Flags for mutt_parse_hook(), e.g. MUTT_FOLDER_HOOK.
Definition: hook.h:36
#define MUTT_FCC_HOOK
fcc-hook: to save outgoing email
Definition: hook.h:41
#define MUTT_CLOSE_HOOK
close-hook: write to a compressed mailbox
Definition: hook.h:53
#define MUTT_ACCOUNT_HOOK
account-hook: when changing between accounts
Definition: hook.h:47
#define MUTT_APPEND_HOOK
append-hook: append to a compressed mailbox
Definition: hook.h:52
#define MUTT_GLOBAL_HOOK
Hooks which don't take a regex.
Definition: hook.h:59
#define MUTT_STARTUP_HOOK
startup-hook: run when starting NeoMutt
Definition: hook.h:57
#define MUTT_SEND2_HOOK
send2-hook: when changing fields in the compose menu
Definition: hook.h:49
#define MUTT_CRYPT_HOOK
crypt-hook: automatically select a PGP/SMIME key
Definition: hook.h:46
#define MUTT_MBOX_HOOK
mbox-hook: move messages after reading them
Definition: hook.h:39
#define MUTT_REPLY_HOOK
reply-hook: when replying to an email
Definition: hook.h:48
#define MUTT_TIMEOUT_HOOK
timeout-hook: run a command periodically
Definition: hook.h:56
#define MUTT_MESSAGE_HOOK
message-hook: run before displaying a message
Definition: hook.h:45
#define MUTT_SHUTDOWN_HOOK
shutdown-hook: run when leaving NeoMutt
Definition: hook.h:58
#define MUTT_IDXFMTHOOK
index-format-hook: customise the format of the index
Definition: hook.h:55
#define MUTT_CHARSET_HOOK
charset-hook: create a charset alias for malformed emails
Definition: hook.h:43
#define MUTT_HOOK_NO_FLAGS
No flags are set.
Definition: hook.h:37
GUI manage the main index (list of emails)
struct MailboxView * get_current_mailbox_view(void)
Get the current Mailbox view.
Definition: index.c:630
struct Mailbox * get_current_mailbox(void)
Get the current Mailbox.
Definition: index.c:662
struct Menu * get_current_menu(void)
Get the current Menu.
Definition: index.c:678
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:43
#define mutt_array_size(x)
Definition: memory.h:36
void mutt_ch_lookup_remove(void)
Remove all the character set lookups.
Definition: charset.c:532
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:500
LookupType
Types of character set lookups.
Definition: charset.h:66
@ MUTT_LOOKUP_ICONV
Character set conversion.
Definition: charset.h:68
@ MUTT_LOOKUP_CHARSET
Alias for another character set.
Definition: charset.h:67
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:635
bool mutt_istr_equal(const char *a, const char *b)
Compare two strings, ignoring case.
Definition: string.c:810
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:251
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:798
char * mutt_str_replace(char **p, const char *s)
Replace one string with another.
Definition: string.c:327
#define PATH_MAX
Definition: mutt.h:41
void mutt_unlink_temp_attachments(void)
Delete all temporary attachments.
Definition: mutt_attach.c:1315
void buf_expand_path_regex(struct Buffer *buf, bool regex)
Create the canonical path (with regex char escaping)
Definition: muttlib.c:136
void buf_pretty_mailbox(struct Buffer *buf)
Shorten a mailbox path using '~' or '='.
Definition: muttlib.c:560
void buf_expand_path(struct Buffer *buf)
Create the canonical path.
Definition: muttlib.c:333
void mutt_safe_path(struct Buffer *dest, const struct Address *a)
Make a safe filename from an email address.
Definition: muttlib.c:723
Some miscellaneous functions.
int mx_access(const char *path, int flags)
Wrapper for access, checks permissions on a given mailbox.
Definition: mx.c:185
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:65
uint8_t PatternCompFlags
Flags for mutt_pattern_comp(), e.g. MUTT_PC_FULL_MSG.
Definition: lib.h:61
#define MUTT_PC_FULL_MSG
Enable body and header matching.
Definition: lib.h:63
void mutt_check_simple(struct Buffer *s, const char *simple)
Convert a simple search into a real request.
Definition: pattern.c:115
#define MUTT_PC_NO_FLAGS
No flags are set.
Definition: lib.h:62
#define MUTT_PC_PATTERN_DYNAMIC
Enable runtime date range evaluation.
Definition: lib.h:64
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_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:53
#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:34
char * dptr
Current read/write position.
Definition: buffer.h:36
size_t dsize
Length of data.
Definition: buffer.h:37
char * data
Pointer to data.
Definition: buffer.h:35
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:37
struct Envelope * env
Envelope information.
Definition: email.h:66
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
A Hash Table.
Definition: hash.h:98
A list of user hooks.
Definition: hook.c:63
HookFlags type
Hook type.
Definition: hook.c:64
struct PatternList * pattern
Used for fcc,save,send-hook.
Definition: hook.c:68
struct Regex regex
Regular expression.
Definition: hook.c:65
char * command
Filename, command or pattern to execute.
Definition: hook.c:66
char * source_file
Used for relative-directory source.
Definition: hook.c:67
View of a Mailbox.
Definition: mview.h:39
A mailbox.
Definition: mailbox.h:79
Definition: lib.h:70
enum MenuType type
Menu definition for keymap entries.
Definition: lib.h:74
Container for Accounts, Notifications.
Definition: neomutt.h:37
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:39
Cache commonly-used patterns.
Definition: lib.h:111
A simple (non-regex) pattern.
Definition: lib.h:71
Cached regular expression.
Definition: regex3.h:89
char * pattern
printable version
Definition: regex3.h:90
bool pat_not
do not match
Definition: regex3.h:92
regex_t * regex
compiled expression
Definition: regex3.h:91