siproxd: add shell helper function to build config file
authorguidosarducci <guidosarducci@users.noreply.github.com>
Mon, 19 Jun 2017 01:55:19 +0000 (18:55 -0700)
committerguidosarducci <guidosarducci@users.noreply.github.com>
Mon, 3 Jul 2017 23:59:04 +0000 (16:59 -0700)
Using the append_conf_if_set() function removes much repetitive and
error-prone code, reduces the source size by ~30%, and fixes a typo
previously uncaught.

Signed-off-by: Tony Ambardar <itugrok@yahoo.com>
net/siproxd/files/siproxd.init

index b175598c577cdd22683baa5ddfe569614814f2c6..a5d2850b45f41b346e69818540957ac66d2a297a 100644 (file)
@@ -12,6 +12,12 @@ siproxd_conf_prefix="$siproxd_conf_dir/siproxd-"
 siproxd_registration_dir="/var/lib/siproxd"
 siproxd_pid_dir="/var/run/siproxd"
 
+append_conf_if_set() {
+       local _val=$(eval "echo \$`echo $1`")
+       [ -n "$_val" ] &&
+       echo "$1" = "$_val" >> "$siproxd_conf_prefix$cfg"
+}
+
 deal_with_lists () {
        echo "$2" = "$1" >> "$siproxd_conf_prefix$cfg"
 }
@@ -76,140 +82,92 @@ start_instance() {
        if [ -f "$siproxd_conf_prefix$cfg" ]; then
                rm "$siproxd_conf_prefix$cfg"
        fi
-       if [ -n "$if_inbound" ]; then
-               echo if_inbound = "$if_inbound" >> "$siproxd_conf_prefix$cfg"
-       fi
-       if [ -n "$if_outbound" ]; then
-               echo if_outbound = "$if_outbound" >> "$siproxd_conf_prefix$cfg"
-       fi
-       if [ -n "$host_outbound" ]; then
-               echo host_outbound = "$host_outbound" >> "$siproxd_conf_prefix$cfg"
-       fi
-       if [ -n "$hosts_allow_reg" ]; then
-               echo hosts_allow_reg = "$hosts_allow_reg" >> "$siproxd_conf_prefix$cfg"
-       fi
-       if [ -n "$hosts_allow_sip" ]; then
-               echo hosts_allow_sip = "$hosts_allow_sip" >> "$siproxd_conf_prefix$cfg"
-       fi
-       if [ -n "$hosts_deny_sip" ]; then
-               echo hosts_deny_sip = "$hosts_deny_sip" >> "$siproxd_conf_prefix$cfg"
-       fi
-       echo sip_listen_port = "$sip_listen_port" >> "$siproxd_conf_prefix$cfg"
-       echo daemonize = "$daemonize" >> "$siproxd_conf_prefix$cfg"
-       echo silence_log = "$silence_log" >> "$siproxd_conf_prefix$cfg"
-       echo user = "$user" >> "$siproxd_conf_prefix$cfg"
+       echo "# auto-generated config file from /etc/config/siproxd" > "$siproxd_conf_prefix$cfg"
+
+       append_conf_if_set if_inbound
+       append_conf_if_set if_outbound
+       append_conf_if_set host_outbound
+       append_conf_if_set hosts_allow_reg
+       append_conf_if_set hosts_allow_sip
+       append_conf_if_set hosts_deny_sip
+       append_conf_if_set sip_listen_port
+       append_conf_if_set daemonize
+       append_conf_if_set silence_log
+       append_conf_if_set user
        if [ -n "$chrootjail" ]; then
                if [ ! -d "$chrootjail" ]; then
                        mkdir -p "$chrootjail"
                        chmod 0755 "$chrootjail"
                fi
-               echo chrootjail = "$chrootjail" >> "$siproxd_conf_prefix$cfg"
-       fi
-       echo registration_file = "$registration_file" >> "$siproxd_conf_prefix$cfg"
-       echo autosave_registrations = "$autosave_registrations" >> "$siproxd_conf_prefix$cfg"
-
-       echo pid_file = "$pid_file" >> "$siproxd_conf_prefix$cfg"
-       echo rtp_proxy_enable = "$rtp_proxy_enable" >> "$siproxd_conf_prefix$cfg"
-       echo rtp_port_low = "$rtp_port_low" >> "$siproxd_conf_prefix$cfg"
-       echo rtp_port_high = "$rtp_port_high" >> "$siproxd_conf_prefix$cfg"
-       echo rtp_timeout = "$rtp_timeout" >> "$siproxd_conf_prefix$cfg"
-       echo rtp_dscp = "$rtp_dscp" >> "$siproxd_conf_prefix$cfg"
-       echo sip_dscp = "$sip_dscp" >> "$siproxd_conf_prefix$cfg"
-       echo rtp_input_dejitter = "$rtp_input_dejitter" >> "$siproxd_conf_prefix$cfg"
-       echo rtp_output_dejitter = "$rtp_output_dejitter" >> "$siproxd_conf_prefix$cfg"
-       echo tcp_timeout = "$tcp_timeout" >> "$siproxd_conf_prefix$cfg"
-       echo tcp_connect_timeout = "$tcp_connect_timeout" >> "$siproxd_conf_prefix$cfg"
-       echo tcp_keepalive = "$tcp_keepalive" >> "$siproxd_conf_prefix$cfg"
-       echo default_expires = "$default_expires" >> "$siproxd_conf_prefix$cfg"
-       if [ -n "$proxy_auth_realm" ]; then
-               echo proxy_auth_realm = "$proxy_auth_realm" >> "$siproxd_conf_prefix$cfg"
-       fi
-       if [ -n "$proxy_auth_passwd" ]; then
-               echo proxy_auth_passwd = "$proxy_auth_passwd" >> "$siproxd_conf_prefix$cfg"
-       fi
-       if [ -n "$proxy_auth_pwfile" ]; then
-               echo proxy_auth_pwfile = "$proxy_auth_pwfile" >> "$siproxd_conf_prefix$cfg"
-       fi
-       echo debug_level = "$debug_level" >> "$siproxd_conf_prefix$cfg"
-       echo debug_port = "$debug_port" >> "$siproxd_conf_prefix$cfg"
-       if [ -n "$mask_host" ]; then
-               echo mask_host = "$mask_host" >> "$siproxd_conf_prefix$cfg"
-       fi
-       if [ -n "$masked_host" ]; then
-               echo masked_host = "$masked_host" >> "$siproxd_conf_prefix$cfg"
-       fi
-       echo ua_string = "$ua_string" >> "$siproxd_conf_prefix$cfg"
-       echo use_rport = "$use_rport" >> "$siproxd_conf_prefix$cfg"
-       if [ -n "$outbound_proxy_host" ]; then
-               echo outbound_proxy_host = "$outbound_proxy_host" >> "$siproxd_conf_prefix$cfg"
-       fi
-       if [ -n "$outbound_proxy_port" ]; then
-               echo outbound_proxy_port = "$outbound_proxy_port" >> "$siproxd_conf_prefix$cfg"
-       fi
-       if [ -n "$outbound_domain_name" ]; then
-               echo outbound_domain_name = "$outbound_domain_name" >> "$siproxd_conf_prefix$cfg"
-       fi
-       if [ -n "$outbound_domain_host" ]; then
-               echo outbound_domain_host = "$outbound_domain_host" >> "$siproxd_conf_prefix$cfg"
-       fi
-       if [ -n "$outbound_domain_port" ]; then
-               echo outbound_domain_port = "$outbound_domain_port" >> "$siproxd_conf_prefix$cfg"
-       fi
+               append_conf_if_set chrootjail
+       fi
+       append_conf_if_set registration_file
+       append_conf_if_set autosave_registrations
+
+       append_conf_if_set pid_file
+       append_conf_if_set rtp_proxy_enable
+       append_conf_if_set rtp_port_low
+       append_conf_if_set rtp_port_high
+       append_conf_if_set rtp_timeout
+       append_conf_if_set rtp_dscp
+       append_conf_if_set sip_dscp
+       append_conf_if_set rtp_input_dejitter
+       append_conf_if_set rtp_output_dejitter
+       append_conf_if_set tcp_timeout
+       append_conf_if_set tcp_connect_timeout
+       append_conf_if_set tcp_keepalive
+       append_conf_if_set default_expires
+       append_conf_if_set proxy_auth_realm
+       append_conf_if_set proxy_auth_passwd
+       append_conf_if_set proxy_auth_pwfile
+       append_conf_if_set debug_level
+       append_conf_if_set debug_port
+       append_conf_if_set mask_host
+       append_conf_if_set masked_host
+       append_conf_if_set ua_string
+       append_conf_if_set use_rport
+       append_conf_if_set outbound_proxy_host
+       append_conf_if_set outbound_proxy_port
+       append_conf_if_set outbound_domain_name
+       append_conf_if_set outbound_domain_host
+       append_conf_if_set outbound_domain_port
 
        # handle plugins
        config_get plugindir "$cfg" plugindir "/usr/lib/siproxd/"
-       echo plugindir = "$plugindir" >> "$siproxd_conf_prefix$cfg"
+       append_conf_if_set plugindir
 
        config_list_foreach "$cfg" 'load_plugin' deal_with_lists "load_plugin"
 
        # plugin_demo.so
        config_get plugin_demo_string "$cfg" plugin_demo_string
-       if [ -n "$plugin_demo_string" ]; then
-               echo plugin_demo_string = "$plugin_demo_string" >> "$siproxd_conf_prefix$cfg"
-       fi
+       append_conf_if_set plugin_demo_string
 
        # plugin_shortdial.so
        config_get plugin_shortdial_akey "$cfg" plugin_shortdial_akey
-       if [ -n "$plugin_shortdial_akey" ]; then
-               echo plugin_shortdial_akey = "$plugin_shortdial_akey" >> "$siproxd_conf_prefix$cfg"
-       fi
+       append_conf_if_set plugin_shortdial_akey
        config_list_foreach "$cfg" 'plugin_shortdial_entry' deal_with_lists "plugin_shortdial_entry"
 
        # plugin_defaulttarget.so
        config_get_bool plugin_defaulttarget_log "$cfg" plugin_defaulttarget_log
-       if [ -n "$plugin_defaulttarget_log" ]; then
-               echo plugin_defaulttarget_log = "$plugin_defaulttarget_log" >> "$siproxd_conf_prefix$cfg"
-       fi
+       append_conf_if_set plugin_defaulttarget_log
        config_get plugin_defaulttarget_target "$cfg" plugin_defaulttarget_target
-       if [ -n "$plugin_defaulttarget_target" ]; then
-               echo plugin_defaulttarget_target = "$plugin_defaulttarget_target" >> "$siproxd_conf_prefix$cfg"
-       fi
+       append_conf_if_set plugin_defaulttarget_target
 
        # plugin_fix_bogus_via.so
        config_get plugin_fix_bogus_via_networks "$cfg" plugin_fix_bogus_via_networks
-       if [ -n "$plugin_fix_bogus_via_networks" ]; then
-               echo plugin_fix_bogus_via_networks = "$plugin_fix_bogus_via_networks" >> "$siproxd_conf_prefix$cfg"
-       fi
+       append_conf_if_set plugin_fix_bogus_via_networks
 
        # plugin_stun.so
        config_get plugin_stun_server "$cfg" plugin_stun_server
-       if [ -n "$plugin_stun_server" ]; then
-               echo plugin_stun_server = "$plugin_stun_server" >> "$siproxd_conf_prefix$cfg"
-       fi
+       append_conf_if_set plugin_stun_server
        config_get plugin_stun_port "$cfg" plugin_stun_port
-       if [ -n "$plugin_stun_port" ]; then
-               echo plugin_stun_port = "$plugin_stun_port" >> "$siproxd_conf_prefix$cfg"
-       fi
+       append_conf_if_set plugin_stun_port
        config_get plugin_stun_period "$cfg" plugin_stun_period
-       if [ -n "$plugin_stun_period" ]; then
-               echo plugin_stun_period = "$plugin_stun_period" >> "$siproxd_conf_prefix$cfg"
-       fi
+       append_conf_if_set plugin_stun_period
 
        # plugin_prefix.so
        config_get plugin_prefix_akey "$cfg" plugin_prefix_akey
-       if [ -n "$plugin_prefix_akey" ]; then
-               echo plugin_prefix_akey = "$plugin_prefix_akey" >> "$siproxd_conf_prefix$cfg"
-       fi
+       append_conf_if_set plugin_prefix_akey
 
        # plugin_regex.so
        config_list_foreach "$cfg" 'plugin_regex_desc' deal_with_lists "plugin_regex_desc"