]> git.ipfire.org Git - people/stevee/network.git/blobdiff - src/functions/functions.hook
zone: Make zone_config_{read,write} behave the same as zone_port_{read,write} do
[people/stevee/network.git] / src / functions / functions.hook
index 4c21f1ace6134539819c105b900c2dfebda23a54..f8e2b6b5f02a465cd72ad2c3d2cc59f64782d7c8 100644 (file)
 #                                                                             #
 ###############################################################################
 
-HOOK_COMMANDS_CONFIG="hook_create hook_down hook_edit hook_status hook_remove"
-HOOK_COMMANDS_CONFIG="${HOOK_COMMANDS_CONFIG} hook_up"
-
-HOOK_COMMANDS_PORT="hook_create hook_down hook_edit hook_hotplug \
-       hook_hotplug_rename hook_info hook_status hook_up"
-
-HOOK_COMMANDS_ZONE="hook_add hook_create hook_discover hook_down hook_edit hook_help \
-       hook_info hook_remove hook_status hook_up hook_hotplug \
-       \
-       hook_config_create hook_config_edit hook_config_remove hook_config_show \
-       \
-       hook_port hook_port_add hook_port_edit hook_port_remove hook_port_show \
-       hook_port_status hook_port_up hook_port_down \
-       \
-       hook_ppp_ip_pre_up hook_ppp_ipv4_down hook_ppp_ipv4_up \
-       hook_ipv6_down hook_ipv6_up hook_ppp_write_config"
-
 function hook_dir() {
        local type=${1}
 
@@ -47,6 +30,22 @@ function hook_dir() {
 }
 NETWORK_HOOKS_DIR_ZONES="$(hook_dir zone)"
 
+function hook_list() {
+       local type="${1}"
+       assert isoneof type port zone
+
+       local dir="$(hook_dir "${type}")"
+       assert isset dir
+
+       local hook
+       for hook in ${dir}/*; do
+               hook_exists "${hook}" || continue
+               print "${hook}"
+       done
+
+       return ${EXIT_OK}
+}
+
 function hook_exists() {
        local type=${1}
        local hook=${2}
@@ -77,19 +76,7 @@ function hook_exec() {
        local hook_cmd="hook_${cmd}"
 
        # Check if the hook action is valid.
-       local valid_commands
-       case "${type}" in
-               "config")
-                       valid_commands="${HOOK_COMMANDS_CONFIG}"
-                       ;;
-               "port")
-                       valid_commands="${HOOK_COMMANDS_PORT}"
-                       ;;
-               "zone")
-                       valid_commands="${HOOK_COMMANDS_ZONE}"
-                       ;;
-       esac
-       isset valid_commands && assert list_match "${hook_cmd}" ${valid_commands}
+       assert hook_valid_command "${type}" "${cmd}"
 
        local hook_path="$(hook_dir ${type})/${hook}"
 
@@ -107,7 +94,7 @@ function hook_exec() {
                assert isset HOOK
 
                # Execute the requested command.
-               cmd "${hook_cmd}" "$@"
+               "${hook_cmd}" "$@"
        )
        local ret=$?
 
@@ -190,3 +177,112 @@ function hook_config_exec() {
 function hook_config_get_all() {
        hook_list config
 }
+
+function hook_valid_command() {
+       local type="${1}"
+       local cmd="${2}"
+
+       case "${type}" in
+               config)
+                       hook_valid_command_config "${cmd}"
+                       return ${?}
+                       ;;
+               port)
+                       hook_valid_command_port "${cmd}"
+                       return ${?}
+                       ;;
+               zone)
+                       hook_valid_command_zone "${cmd}"
+                       return ${?}
+                       ;;
+       esac
+
+       return ${EXIT_FALSE}
+}
+
+function hook_valid_command_config() {
+       local cmd="${1}"
+
+       case "${cmd}" in
+               create|remove|edit|up|down|status)
+                       return ${EXIT_TRUE}
+                       ;;
+       esac
+
+       return ${EXIT_FALSE}
+}
+
+function hook_valid_command_port() {
+       local cmd="${1}"
+
+       case "${cmd}" in
+               # Configuration hooks
+               new|edit|destroy)
+                       return ${EXIT_TRUE}
+                       ;;
+
+               # Control hooks
+               create|remove|up|down)
+                       return ${EXIT_TRUE}
+                       ;;
+
+               # Hotplug
+               hotplug|hotplug_rename)
+                       return ${EXIT_TRUE}
+                       ;;
+
+               # Status
+               status|info)
+                       return ${EXIT_TRUE}
+                       ;;
+       esac
+
+       return ${EXIT_FALSE}
+}
+
+function hook_valid_command_zone() {
+       local cmd="${1}"
+
+       case "${cmd}" in
+               # Configuration hooks
+               new|edit|destroy)
+                       return ${EXIT_TRUE}
+                       ;;
+
+               config_create|config_edit|config_remove|config_show)
+                       return ${EXIT_TRUE}
+                       ;;
+
+               # Control hooks
+               up|down)
+                       return ${EXIT_TRUE}
+                       ;;
+
+               # Hotplug
+               hotplug)
+                       return ${EXIT_TRUE}
+                       ;;
+
+               # Ports
+               port_attach|port_detach|port_edit|port_create|port_remove|port_status|port_up|port_down)
+                       return ${EXIT_TRUE}
+                       ;;
+
+               # Status
+               status|info|help)
+                       return ${EXIT_TRUE}
+                       ;;
+
+               # Discovery
+               discover)
+                       return ${EXIT_TRUE}
+                       ;;
+
+               # PPP
+               ppp_ip_pre_up|ppp_ipv[64]_up|ppp_ipv[64]_down|ppp_write_config)
+                       return ${EXIT_TRUE}
+                       ;;
+       esac
+
+       return ${EXIT_FALSE}
+}