]> git.ipfire.org Git - people/stevee/network.git/blobdiff - src/functions/functions.hook
Convert HOOK_SETTINGS into an array
[people/stevee/network.git] / src / functions / functions.hook
index 4b1e110654dbb139c27070fc82415d4556d8d4f4..fb680374e74080644c5fdca9cbd35348d4082903 100644 (file)
 #                                                                             #
 ###############################################################################
 
-HOOK_COMMANDS_CONFIG="hook_create hook_down hook_status hook_up"
-
-HOOK_COMMANDS_PORT="hook_create hook_down 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_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() {
+hook_dir() {
        local type=${1}
 
        if [ -n "${type}" ]; then
@@ -46,7 +30,7 @@ function hook_dir() {
 }
 NETWORK_HOOKS_DIR_ZONES="$(hook_dir zone)"
 
-function hook_exists() {
+hook_exists() {
        local type=${1}
        local hook=${2}
 
@@ -59,7 +43,7 @@ function hook_exists() {
        [ ! -d "${hook}" ] && [ -x "${hook}" ]
 }
 
-function hook_exec() {
+hook_exec() {
        local type="${1}"
        assert isset type
 
@@ -76,19 +60,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}"
 
@@ -106,25 +78,64 @@ function hook_exec() {
                assert isset HOOK
 
                # Execute the requested command.
-               cmd "${hook_cmd}" "$@"
+               "${hook_cmd}" "$@"
        )
        local ret=$?
 
        case "${ret}" in
-               ${EXIT_COMMAND_NOT_FOUND})
-                       log ERROR "Hook command not implemented: ${hook_command} ($@)"
-                       exit ${EXIT_COMMAND_NOT_FOUND}
+               ${EXIT_COMMAND_NOT_FOUND}|${EXIT_NOT_SUPPORTED})
+                       log ERROR "Hook '${hook}' does not implement the method '${cmd}':"
+                       log ERROR "  arguments: $@"
+                       return ${EXIT_COMMAND_NOT_FOUND}
                        ;;
                ${EXIT_ERROR_ASSERT})
                        log ERROR "Hook exited with an assertion error."
-                       exit ${EXIT_ERROR_ASSERT}
+                       return ${EXIT_ERROR_ASSERT}
                        ;;
        esac
 
        return ${ret}
 }
 
-function config_get_hook() {
+hook_list() {
+       local type="${1}"
+
+       local dir="$(hook_dir "${type}")"
+       assert isset dir
+
+       local hook
+       for hook in $(list_directory "${dir}"); do
+               if hook_exists "${type}" "${hook}"; then
+                       echo "${hook}"
+               fi
+       done
+}
+
+# The default help function.
+hook_help() {
+       # If no man page has been configured, we print an error message.
+       if [ -z "${HOOK_MANPAGE}" ]; then
+               error "There is no help available for hook '${HOOK}'. Exiting."
+               exit ${EXIT_ERROR}
+       fi
+
+       cli_show_man "${HOOK_MANPAGE}"
+
+       exit $?
+}
+
+# Sets all settings in HOOK_SETTINGS to their DEFAULT_* values
+hook_set_defaults() {
+       local setting
+       for setting in ${HOOK_SETTINGS[*]}; do
+               local default="DEFAULT_${setting}"
+
+               # Sets the default or empty
+               assign "${setting}" "${!default}"
+       done
+}
+
+config_get_hook() {
        local config=${1}
 
        assert isset config
@@ -136,108 +147,135 @@ function config_get_hook() {
        )
 }
 
-function hook_zone_exists() {
-       hook_exists zone $@
+hook_zone_exists() {
+       hook_exists zone "$@"
 }
 
-function hook_zone_port_exists() {
-       local hook_zone=${1}
-       local hook_port=${2}
-
-       hook_exists zone "${hook_zone}.ports/${hook_port}"
+hook_zone_exec() {
+       hook_exec zone "$@"
 }
 
-function hook_zone_config_exists() {
-       local hook_zone=${1}
-       local hook_config=${2}
+hook_zone_get_all() {
+       hook_list zone
+}
 
-       hook_exists zone "${hook_zone}.configs/${hook_config}"
+hook_config_exists() {
+       hook_exists config "$@"
 }
 
-function hook_zone_has_ports() {
-       local hook=${1}
+hook_config_exec() {
+       hook_exec config "$@"
+}
 
-       [ -d "${NETWORK_HOOKS_DIR_ZONES}/${hook}.ports" ]
+hook_config_get_all() {
+       hook_list config
 }
 
-function hook_zone_has_configs() {
-       local hook=${1}
+hook_valid_command() {
+       local type="${1}"
+       local cmd="${2}"
 
-       [ -d "${NETWORK_HOOKS_DIR_ZONES}/${hook}.configs" ]
-}
+       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
 
-function hook_zone_exec() {
-       hook_exec zone $@
+       return ${EXIT_FALSE}
 }
 
-function hook_zone_port_exec() {
-       local hook_zone=${1}
-       local hook_port=${2}
-       shift 2
+hook_valid_command_config() {
+       local cmd="${1}"
 
-       hook_zone_exec "${hook_zone}.ports/${hook_port}" $@
+       case "${cmd}" in
+               new|destroy|edit|up|down|status|hid)
+                       return ${EXIT_TRUE}
+                       ;;
+       esac
+
+       return ${EXIT_FALSE}
 }
 
-function hook_zone_config_exec() {
-       local hook_zone=${1}
-       local hook_port=${2}
-       shift 2
+hook_valid_command_port() {
+       local cmd="${1}"
 
-       hook_zone_exec "${hook_zone}.configs/${hook_port}" $@
-}
+       case "${cmd}" in
+               # Configuration hooks
+               new|edit|destroy)
+                       return ${EXIT_TRUE}
+                       ;;
 
-function hook_zone_get_all() {
-       local type=${1}
+               # Control hooks
+               create|remove|up|down)
+                       return ${EXIT_TRUE}
+                       ;;
 
-       local hook
-       for hook in $(hook_dir zone)/*; do
-               hook=$(basename ${hook})
-               hook_zone_exists ${hook} && echo "${hook}"
-       done
+               # Hotplug
+               hotplug|hotplug_rename)
+                       return ${EXIT_TRUE}
+                       ;;
+
+               # Status
+               status|children)
+                       return ${EXIT_TRUE}
+                       ;;
+       esac
+
+       return ${EXIT_FALSE}
 }
 
-function hook_zone_ports_get_all() {
-       local hook=${1}
+hook_valid_command_zone() {
+       local cmd="${1}"
 
-       if ! hook_exists zone ${hook}; then
-               error "Hook '${hook}' does not exist."
-               return ${EXIT_ERROR}
-       fi
+       case "${cmd}" in
+               # Configuration hooks
+               new|edit|destroy)
+                       return ${EXIT_TRUE}
+                       ;;
 
-       # If the zone hook has got no ports we exit silently
-       if ! hook_zone_has_ports ${hook}; then
-               return ${EXIT_OK}
-       fi
+               config_new|config_destroy|config_edit|config_show)
+                       return ${EXIT_TRUE}
+                       ;;
 
-       local h
-       for h in $(hook_dir zone)/${hook}.ports/*; do
-               h=$(basename ${h})
-               if hook_zone_port_exists ${hook} ${h}; then
-                       echo "${h}"
-               fi
-       done
-}
+               # Control hooks
+               up|down)
+                       return ${EXIT_TRUE}
+                       ;;
 
-function hook_zone_configs_get_all() {
-       local hook=${1}
+               # Hotplug
+               hotplug)
+                       return ${EXIT_TRUE}
+                       ;;
 
-       if ! hook_exists zone ${hook}; then
-               error "Hook '${hook}' does not exist."
-               return ${EXIT_ERROR}
-       fi
+               # Ports
+               port_attach|port_detach|port_edit|port_create|port_remove|port_status|port_up|port_down)
+                       return ${EXIT_TRUE}
+                       ;;
 
-       # If the zone hook has got no configurations we exit silently
-       if ! hook_zone_has_configs ${hook}; then
-               return ${EXIT_OK}
-       fi
+               # Status
+               status|info|help)
+                       return ${EXIT_TRUE}
+                       ;;
 
-       local h
-       for h in $(hook_dir zone)/${hook}.configs/*; do
-               h=$(basename ${h})
-               if hook_zone_config_exists ${hook} ${h}; then
-                       echo "${h}"
-               fi
-       done
+               # 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_OK}
+       return ${EXIT_FALSE}
 }