[17] | 1 | #!/bin/bash
|
---|
| 2 |
|
---|
| 3 | # Provides common functions for 7dtd-scripts. Not intended to be run directly.
|
---|
| 4 |
|
---|
| 5 | # Check if the script is run as root (exit otherwise) and load global config
|
---|
| 6 | checkRootLoadConf() {
|
---|
| 7 | if [ `id -u` -ne 0 ]; then
|
---|
| 8 | echo "This script has to be run as root!"
|
---|
| 9 | exit 10
|
---|
| 10 | fi
|
---|
| 11 | . /etc/7dtd.conf
|
---|
| 12 | }
|
---|
| 13 |
|
---|
| 14 | # Get the config path for the given instance
|
---|
| 15 | # Params:
|
---|
| 16 | # 1: Instance name
|
---|
| 17 | # Returns:
|
---|
| 18 | # Config path for instance
|
---|
| 19 | getInstancePath() {
|
---|
| 20 | echo $SDTD_BASE/instances/$1
|
---|
| 21 | }
|
---|
| 22 |
|
---|
[19] | 23 | # Check if the given instance name is valid (no blanks, no special chars,
|
---|
| 24 | # only letters, digits, underscore, hyphen -> [A-Za-z0-9_\-])
|
---|
| 25 | # Params:
|
---|
| 26 | # 1: Instance name
|
---|
| 27 | # Returns:
|
---|
| 28 | # 0/1 instance not valid/valid
|
---|
| 29 | isValidInstanceName() {
|
---|
| 30 | if [[ "$1" =~ ^[A-Za-z0-9_\-]+$ ]]; then
|
---|
| 31 | echo 1
|
---|
| 32 | else
|
---|
| 33 | echo 0
|
---|
| 34 | fi
|
---|
| 35 | }
|
---|
| 36 |
|
---|
[17] | 37 | # Check if the given instance name is an existing instance
|
---|
| 38 | # Params:
|
---|
| 39 | # 1: Instance name
|
---|
| 40 | # Returns:
|
---|
| 41 | # 0/1 instance not valid/valid
|
---|
| 42 | isValidInstance() {
|
---|
[19] | 43 | if [ ! -z "$1" ]; then
|
---|
| 44 | if [ $(isValidInstanceName "$1") -eq 1 ]; then
|
---|
| 45 | if [ -d $(getInstancePath "$1") ]; then
|
---|
| 46 | if [ -f $(getInstancePath "$1")/config.xml ]; then
|
---|
| 47 | echo 1
|
---|
| 48 | return
|
---|
| 49 | fi
|
---|
[17] | 50 | fi
|
---|
| 51 | fi
|
---|
| 52 | fi
|
---|
[19] | 53 | echo 0
|
---|
[17] | 54 | }
|
---|
| 55 |
|
---|
| 56 | # Check if the given instance is currently running
|
---|
| 57 | # Params:
|
---|
| 58 | # 1: Instance name
|
---|
| 59 | # Returns:
|
---|
| 60 | # 0 = not running
|
---|
| 61 | # 1 = running
|
---|
| 62 | isRunning() {
|
---|
[25] | 63 | $SSD --status --pidfile $(getInstancePath $1)/7dtd.pid
|
---|
[17] | 64 | if [ $? -eq 0 ]; then
|
---|
| 65 | echo 1
|
---|
| 66 | else
|
---|
| 67 | echo 0
|
---|
| 68 | fi
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | # Get list of defined instances
|
---|
| 72 | # Returns:
|
---|
| 73 | # List of instances
|
---|
| 74 | getInstanceList() {
|
---|
[19] | 75 | local IF
|
---|
[17] | 76 | for IF in $SDTD_BASE/instances/*; do
|
---|
[19] | 77 | local I=`basename $IF`
|
---|
[17] | 78 | if [ $(isValidInstance $I) -eq 1 ]; then
|
---|
| 79 | echo $I
|
---|
| 80 | fi
|
---|
| 81 | done
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | # Get the PID of the instance if it is running, 0 otherwise
|
---|
| 85 | # Params:
|
---|
| 86 | # 1: Instance name
|
---|
| 87 | # Returns:
|
---|
| 88 | # 0 if not running
|
---|
| 89 | # PID otherwise
|
---|
| 90 | getInstancePID() {
|
---|
| 91 | if [ $(isRunning $1) -eq 1 ]; then
|
---|
| 92 | cat $(getInstancePath $1)/7dtd.pid
|
---|
| 93 | else
|
---|
| 94 | echo 0
|
---|
| 95 | fi
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | # Check if a given port range (baseport, baseport+1, baseport+2 each udp)
|
---|
[19] | 99 | # is already in use by any other instance
|
---|
[17] | 100 | # Params:
|
---|
| 101 | # 1: Baseport
|
---|
[19] | 102 | # 2: Current instance (ignored)
|
---|
[17] | 103 | # Returns:
|
---|
| 104 | # 0/1 not in use/in use
|
---|
| 105 | checkGamePortUsed() {
|
---|
[19] | 106 | local PORTMIN=$1
|
---|
| 107 | local PORTMAX=$(( $1 + 2 ))
|
---|
| 108 | local I
|
---|
[17] | 109 | for I in $(getInstanceList); do
|
---|
[19] | 110 | if [ "$2" != "$I" ]; then
|
---|
| 111 | local CURPORTMIN=$(getConfigValue $I "ServerPort")
|
---|
| 112 | local CURPORTMAX=$(( $CURPORTMIN + 2 ))
|
---|
| 113 | if [ $PORTMAX -ge $CURPORTMIN -a $PORTMIN -le $CURPORTMAX ]; then
|
---|
| 114 | echo 1
|
---|
| 115 | return
|
---|
| 116 | fi
|
---|
[17] | 117 | fi
|
---|
| 118 | done
|
---|
| 119 | echo 0
|
---|
| 120 | }
|
---|
| 121 |
|
---|
[19] | 122 | # Check if a given TCP port is already in use by any instance (either by control
|
---|
| 123 | # panel or telnet)
|
---|
[17] | 124 | # Params:
|
---|
| 125 | # 1: Port
|
---|
| 126 | # Returns:
|
---|
| 127 | # 0/1 not in use/in use
|
---|
[19] | 128 | checkTCPPortUsed() {
|
---|
| 129 | local I
|
---|
[17] | 130 | for I in $(getInstanceList); do
|
---|
[19] | 131 | if [ "$2" != "$I" ]; then
|
---|
| 132 | local CURENABLED=$(getConfigValue $I "TelnetEnabled")
|
---|
| 133 | local CURPORT=$(getConfigValue $I "TelnetPort")
|
---|
| 134 | if [ "$CURENABLED" = "true" -a $CURPORT -eq $1 ]; then
|
---|
| 135 | echo 1
|
---|
| 136 | return
|
---|
| 137 | fi
|
---|
| 138 | CURENABLED=$(getConfigValue $I "ControlPanelEnabled")
|
---|
| 139 | CURPORT=$(getConfigValue $I "ControlPanelPort")
|
---|
| 140 | if [ "$CURENABLED" = "true" -a $CURPORT -eq $1 ]; then
|
---|
| 141 | echo 1
|
---|
| 142 | return
|
---|
| 143 | fi
|
---|
[17] | 144 | fi
|
---|
| 145 | done
|
---|
| 146 | echo 0
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | # Send a single command to the telnet port
|
---|
| 150 | # Params:
|
---|
| 151 | # 1: Instance name
|
---|
| 152 | # 2: Command
|
---|
| 153 | # Returns:
|
---|
| 154 | # String of telnet output
|
---|
| 155 | telnetCommand() {
|
---|
[19] | 156 | local TEL_ENABLED=$(getConfigValue $1 TelnetEnabled)
|
---|
| 157 | local TEL_PORT=$(getConfigValue $1 TelnetPort)
|
---|
| 158 | local TEL_PASS=$(getConfigValue $1 TelnetPassword)
|
---|
[17] | 159 | if [ "$TEL_ENABLED" = "true" ] && [ -n "$TEL_PASS" ]; then
|
---|
| 160 | echo -e "$TEL_PASS\n$2\nexit" | nc -q 2 127.0.0.1 $TEL_PORT
|
---|
| 161 | else
|
---|
| 162 | echo "Telnet not enabled or no password set."
|
---|
| 163 | fi
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | # Get all hook files for the given hook-name
|
---|
| 167 | # Params:
|
---|
| 168 | # 1: Hook name
|
---|
| 169 | # Returns:
|
---|
| 170 | # Names of hook files
|
---|
| 171 | getHooksFor() {
|
---|
| 172 | if [ -d $SDTD_BASE/hooks/$1 ]; then
|
---|
[19] | 173 | local H
|
---|
[17] | 174 | for H in $SDTD_BASE/hooks/$1/*.sh; do
|
---|
| 175 | echo "$H"
|
---|
| 176 | done
|
---|
| 177 | fi
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | # Lowercase passed string
|
---|
| 181 | # Params:
|
---|
| 182 | # 1: String
|
---|
| 183 | # Returns:
|
---|
| 184 | # Lowercased string
|
---|
| 185 | lowercase() {
|
---|
| 186 | echo "${1}" | tr "[:upper:]" "[:lower:]"
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 | # Prepare passed string as part of camelcase, i.e. first char upper case, others
|
---|
| 190 | # lowercase
|
---|
| 191 | # Params:
|
---|
| 192 | # 1: String
|
---|
| 193 | # Returns:
|
---|
| 194 | # Transformed string
|
---|
| 195 | camelcasePrep() {
|
---|
| 196 | echo $(echo "${1:0:1}" | tr "[:lower:]" "[:upper:]")$(echo "${1:1}" | tr "[:upper:]" "[:lower:]")
|
---|
| 197 | }
|
---|
| 198 |
|
---|
[19] | 199 | # Check if given value is a (integer) number
|
---|
| 200 | # Params:
|
---|
| 201 | # 1: Value
|
---|
| 202 | # Returns:
|
---|
| 203 | # 0/1 for NaN / is a number
|
---|
| 204 | isANumber() {
|
---|
| 205 | if [[ $1 =~ ^[0-9]+$ ]] ; then
|
---|
| 206 | echo "1"
|
---|
| 207 | else
|
---|
| 208 | echo "0"
|
---|
| 209 | fi
|
---|
| 210 | }
|
---|
| 211 |
|
---|
| 212 | # Check if given value is a boolean (true/false, yes/no, y/n)
|
---|
| 213 | # Params:
|
---|
| 214 | # 1: Value
|
---|
| 215 | # Returns:
|
---|
| 216 | # 0/1
|
---|
| 217 | isABool() {
|
---|
| 218 | local LOW=$(lowercase "$1")
|
---|
| 219 | if [ "$LOW" = "false" -o "$LOW" = "true"\
|
---|
| 220 | -o "$LOW" = "yes" -o "$LOW" = "y"\
|
---|
| 221 | -o "$LOW" = "no" -o "$LOW" = "n" ]; then
|
---|
| 222 | echo 1
|
---|
| 223 | else
|
---|
| 224 | echo 0
|
---|
| 225 | fi
|
---|
| 226 | }
|
---|
| 227 |
|
---|
| 228 | # Convert the given value to a boolean 0/1
|
---|
| 229 | # Params:
|
---|
| 230 | # 1: Value
|
---|
| 231 | # Returns:
|
---|
| 232 | # 0/1 as false/true
|
---|
| 233 | getBool() {
|
---|
| 234 | if [ $(isABool "$1") -eq 0 ]; then
|
---|
| 235 | echo 0
|
---|
| 236 | else
|
---|
| 237 | local LOW=$(lowercase "$1")
|
---|
| 238 | if [ "$LOW" = "true" -o "$LOW" = "yes" -o "$LOW" = "y" ]; then
|
---|
| 239 | echo 1
|
---|
| 240 | else
|
---|
| 241 | echo 0
|
---|
| 242 | fi
|
---|
| 243 | fi
|
---|
| 244 | }
|
---|
| 245 |
|
---|
[17] | 246 | listCommands() {
|
---|
[19] | 247 | local C
|
---|
[17] | 248 | for C in $(declare -F | cut -d\ -f3 | grep "^sdtdCommand"\
|
---|
| 249 | | grep -v "Help$"\
|
---|
| 250 | | grep -v "Description$"\
|
---|
| 251 | | grep -v "Expects$"); do
|
---|
[19] | 252 | local CMD=$(lowercase "${C#sdtdCommand}")
|
---|
[17] | 253 | printf "%s " "$CMD"
|
---|
| 254 | done
|
---|
| 255 | }
|
---|
| 256 |
|
---|
| 257 | . /usr/local/lib/7dtd/help.sh
|
---|
| 258 | . /usr/local/lib/7dtd/playerlog.sh
|
---|
[19] | 259 | . /usr/local/lib/7dtd/serverconfig.sh
|
---|
[17] | 260 | for M in /usr/local/lib/7dtd/commands/*.sh; do
|
---|
| 261 | . $M
|
---|
| 262 | done
|
---|
| 263 |
|
---|