1aea262bfSKyle Evans-- 2aea262bfSKyle Evans-- SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3aea262bfSKyle Evans-- 4aea262bfSKyle Evans-- Copyright (c) 2018 Kyle Evans <kevans@FreeBSD.org> 5aea262bfSKyle Evans-- 6aea262bfSKyle Evans-- Redistribution and use in source and binary forms, with or without 7aea262bfSKyle Evans-- modification, are permitted provided that the following conditions 8aea262bfSKyle Evans-- are met: 9aea262bfSKyle Evans-- 1. Redistributions of source code must retain the above copyright 10aea262bfSKyle Evans-- notice, this list of conditions and the following disclaimer. 11aea262bfSKyle Evans-- 2. Redistributions in binary form must reproduce the above copyright 12aea262bfSKyle Evans-- notice, this list of conditions and the following disclaimer in the 13aea262bfSKyle Evans-- documentation and/or other materials provided with the distribution. 14aea262bfSKyle Evans-- 15aea262bfSKyle Evans-- THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16aea262bfSKyle Evans-- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17aea262bfSKyle Evans-- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18aea262bfSKyle Evans-- ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19aea262bfSKyle Evans-- FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20aea262bfSKyle Evans-- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21aea262bfSKyle Evans-- OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22aea262bfSKyle Evans-- HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23aea262bfSKyle Evans-- LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24aea262bfSKyle Evans-- OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25aea262bfSKyle Evans-- SUCH DAMAGE. 26aea262bfSKyle Evans-- 27aea262bfSKyle Evans-- $FreeBSD$ 28aea262bfSKyle Evans-- 29aea262bfSKyle Evans 30aea262bfSKyle Evanslocal hook = {} 31aea262bfSKyle Evans 32aea262bfSKyle Evanslocal registered_hooks = {} 33aea262bfSKyle Evans 34aea262bfSKyle Evans-- Module exports 35aea262bfSKyle Evans-- Register a hook type; these are the names that hooks may be registered for. 36aea262bfSKyle Evans-- It is expected that modules will register all of their hook types upon 37aea262bfSKyle Evans-- initial load. Other modules may then, at any time, register a hook for these 38aea262bfSKyle Evans-- types. 39aea262bfSKyle Evans-- 40aea262bfSKyle Evans-- Naming convention: hook types should be sensible named, preferably prefixed 41aea262bfSKyle Evans-- with the name of the module that registered them. They would also ideally 42aea262bfSKyle Evans-- describe an action that may or may not match a function name. 43aea262bfSKyle Evans-- e.g. config.reloaded which takes place after config has been reloaded, 44aea262bfSKyle Evans-- possibly from a different source. 45aea262bfSKyle Evansfunction hook.registerType(hooktype) 46aea262bfSKyle Evans registered_hooks[hooktype] = {} 47aea262bfSKyle Evansend 48aea262bfSKyle Evans 49aea262bfSKyle Evansfunction hook.register(hooktype, hookfunc) 50aea262bfSKyle Evans local selected_hooks = registered_hooks[hooktype] 51aea262bfSKyle Evans if selected_hooks == nil then 52aea262bfSKyle Evans print("Tried registering a hook for an unknown hook type: " .. 53aea262bfSKyle Evans hooktype) 54aea262bfSKyle Evans return 55aea262bfSKyle Evans end 56aea262bfSKyle Evans selected_hooks[#selected_hooks + 1] = hookfunc 57aea262bfSKyle Evans registered_hooks[hooktype] = selected_hooks 58aea262bfSKyle Evansend 59aea262bfSKyle Evans 60aea262bfSKyle Evans-- Takes a hooktype and runs all functions associated with that specific hook 61aea262bfSKyle Evans-- type in the order that they were registered in. This ordering should likely 62aea262bfSKyle Evans-- not be relied upon. 63*3cb2f5f3SKyle Evansfunction hook.runAll(hooktype, data) 64aea262bfSKyle Evans local selected_hooks = registered_hooks[hooktype] 65aea262bfSKyle Evans if selected_hooks == nil then 66aea262bfSKyle Evans -- This message, and the print() above, should only be seen by 67aea262bfSKyle Evans -- developers. Hook type registration should have happened at 68aea262bfSKyle Evans -- module load, so if this hasn't happened then we have messed 69aea262bfSKyle Evans -- up the order in which we've loaded modules and we need to 70aea262bfSKyle Evans -- catch that as soon as possible. 71aea262bfSKyle Evans print("Tried to run hooks for an unknown hook type: " .. 72aea262bfSKyle Evans hooktype) 73aea262bfSKyle Evans return 0 74aea262bfSKyle Evans end 75aea262bfSKyle Evans if #selected_hooks > 0 then 76aea262bfSKyle Evans for _, func in ipairs(selected_hooks) do 77*3cb2f5f3SKyle Evans func(data) 78aea262bfSKyle Evans end 79aea262bfSKyle Evans end 80aea262bfSKyle Evans return #selected_hooks 81aea262bfSKyle Evansend 82aea262bfSKyle Evans 83aea262bfSKyle Evansreturn hook 84