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