source: scripts/usr/local/lib/7dtd/common.sh@ 234

Last change on this file since 234 was 88, checked in by alloc, 10 years ago

v.32 hotfix: Allow empty Telnet password

File size: 6.6 KB
Line 
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
6checkRootLoadConf() {
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
19getInstancePath() {
20 echo $SDTD_BASE/instances/$1
21}
22
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
29isValidInstanceName() {
30 if [[ "$1" =~ ^[A-Za-z0-9_\-]+$ ]]; then
31 echo 1
32 else
33 echo 0
34 fi
35}
36
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
42isValidInstance() {
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
50 fi
51 fi
52 fi
53 echo 0
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
62isRunning() {
63 $SSD --status --pidfile $(getInstancePath $1)/7dtd.pid
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
74getInstanceList() {
75 local IF
76 for IF in $SDTD_BASE/instances/*; do
77 local I=`basename $IF`
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
90getInstancePID() {
91 if [ $(isRunning $1) -eq 1 ]; then
92 cat $(getInstancePath $1)/7dtd.pid
93 else
94 echo 0
95 fi
96}
97
98# Get the local engine version number (i.e. build id)
99# Returns:
100# 0 if no engine installed or no appmanifest found or buildid could not be read
101# Build Id otherwise
102getLocalEngineVersion() {
103 local APPMANIFEST=$(find $SDTD_BASE/engine -type f -name "appmanifest_294420.acf")
104 local LOCAL=0
105 if [ -f "$APPMANIFEST" ]; then
106 LOCAL=$(grep buildid "$APPMANIFEST" | tr '[:blank:]"' ' ' | tr -s ' ' | cut -d\ -f3)
107 if [ $(isANumber "$LOCAL") -eq 0 ]; then
108 LOCAL=0
109 fi
110 fi
111 echo $LOCAL
112}
113
114# Get the latest remote engine version number (i.e. build id)
115# Returns:
116# 1 if build id could not be retrieved
117# Build Id otherwise
118getRemoteEngineVersion() {
119 cd $SDTD_BASE/steamcmd
120 local REMOTE=$(wget -qO- http://steamdb.info/api/GetRawDepots/?appid=294420 | sed 's/\\n/\n/g' | grep -EA 1000 "^\s+\[branches\]" | grep -EA 5 "^\s+\[public\]" | grep -m 1 -EB 10 "^\s+\)$" | grep -E "^\s+\[buildid\]\s+" | tr '[:blank:]"' ' ' | tr -s ' ' | cut -d\ -f4)
121
122 if [ $(isANumber "$REMOTE") -eq 0 ]; then
123 REMOTE=1
124 fi
125 echo $REMOTE
126}
127
128# Check if a given port range (baseport, baseport+1, baseport+2 each udp)
129# is already in use by any other instance
130# Params:
131# 1: Baseport
132# 2: Current instance (ignored)
133# Returns:
134# 0/1 not in use/in use
135checkGamePortUsed() {
136 local PORTMIN=$1
137 local PORTMAX=$(( $1 + 2 ))
138 local I
139 for I in $(getInstanceList); do
140 if [ "$2" != "$I" ]; then
141 local CURPORTMIN=$(getConfigValue $I "ServerPort")
142 local CURPORTMAX=$(( $CURPORTMIN + 2 ))
143 if [ $PORTMAX -ge $CURPORTMIN -a $PORTMIN -le $CURPORTMAX ]; then
144 echo 1
145 return
146 fi
147 fi
148 done
149 echo 0
150}
151
152# Check if a given TCP port is already in use by any instance (either by control
153# panel or telnet)
154# Params:
155# 1: Port
156# Returns:
157# 0/1 not in use/in use
158checkTCPPortUsed() {
159 local I
160 for I in $(getInstanceList); do
161 if [ "$2" != "$I" ]; then
162 local CURENABLED=$(getConfigValue $I "TelnetEnabled")
163 local CURPORT=$(getConfigValue $I "TelnetPort")
164 if [ "$CURENABLED" = "true" -a $CURPORT -eq $1 ]; then
165 echo 1
166 return
167 fi
168 CURENABLED=$(getConfigValue $I "ControlPanelEnabled")
169 CURPORT=$(getConfigValue $I "ControlPanelPort")
170 if [ "$CURENABLED" = "true" -a $CURPORT -eq $1 ]; then
171 echo 1
172 return
173 fi
174 fi
175 done
176 echo 0
177}
178
179# Send a single command to the telnet port
180# Params:
181# 1: Instance name
182# 2: Command
183# 3: (Optional) Timeout in sec, defaulting to 1
184# Returns:
185# String of telnet output
186telnetCommand() {
187 local TEL_ENABLED=$(getConfigValue $1 TelnetEnabled)
188 local TEL_PORT=$(getConfigValue $1 TelnetPort)
189 local TEL_PASS=$(getConfigValue $1 TelnetPassword)
190 if [ "$TEL_ENABLED" = "true" ]; then
191 local TEMPFILE=$(mktemp)
192 rm -f $TEMPFILE
193 mkfifo $TEMPFILE
194 exec 3<> $TEMPFILE
195 nc 127.0.0.1 $TEL_PORT <&3 &
196 local NCPID=$!
197 if [ -n "$TEL_PASS" ]; then
198 printf "$TEL_PASS\n$2\n" >&3
199 else
200 printf "$2\n" >&3
201 fi
202 sleep ${3:-1}
203 printf "exit\n" >&3
204 sleep 0.2
205 kill -9 $NCPID > /dev/null 2>&1
206 exec 3>&-
207 rm -f $TEMPFILE
208 else
209 echo "Telnet not enabled."
210 fi
211}
212
213# Get all hook files for the given hook-name
214# Params:
215# 1: Hook name
216# Returns:
217# Names of hook files
218getHooksFor() {
219 if [ -d $SDTD_BASE/hooks/$1 ]; then
220 local H
221 for H in $SDTD_BASE/hooks/$1/*.sh; do
222 echo "$H"
223 done
224 fi
225}
226
227# Lowercase passed string
228# Params:
229# 1: String
230# Returns:
231# Lowercased string
232lowercase() {
233 echo "${1}" | tr "[:upper:]" "[:lower:]"
234}
235
236# Prepare passed string as part of camelcase, i.e. first char upper case, others
237# lowercase
238# Params:
239# 1: String
240# Returns:
241# Transformed string
242camelcasePrep() {
243 echo $(echo "${1:0:1}" | tr "[:lower:]" "[:upper:]")$(echo "${1:1}" | tr "[:upper:]" "[:lower:]")
244}
245
246# Check if given value is a (integer) number
247# Params:
248# 1: Value
249# Returns:
250# 0/1 for NaN / is a number
251isANumber() {
252 if [[ $1 =~ ^[0-9]+$ ]] ; then
253 echo "1"
254 else
255 echo "0"
256 fi
257}
258
259# Check if given value is a boolean (true/false, yes/no, y/n)
260# Params:
261# 1: Value
262# Returns:
263# 0/1
264isABool() {
265 local LOW=$(lowercase "$1")
266 if [ "$LOW" = "false" -o "$LOW" = "true"\
267 -o "$LOW" = "yes" -o "$LOW" = "y"\
268 -o "$LOW" = "no" -o "$LOW" = "n" ]; then
269 echo 1
270 else
271 echo 0
272 fi
273}
274
275# Convert the given value to a boolean 0/1
276# Params:
277# 1: Value
278# Returns:
279# 0/1 as false/true
280getBool() {
281 if [ $(isABool "$1") -eq 0 ]; then
282 echo 0
283 else
284 local LOW=$(lowercase "$1")
285 if [ "$LOW" = "true" -o "$LOW" = "yes" -o "$LOW" = "y" ]; then
286 echo 1
287 else
288 echo 0
289 fi
290 fi
291}
292
293listCommands() {
294 local C
295 for C in $(declare -F | cut -d\ -f3 | grep "^sdtdCommand"\
296 | grep -v "Help$"\
297 | grep -v "Description$"\
298 | grep -v "Expects$"); do
299 local CMD=$(lowercase "${C#sdtdCommand}")
300 printf "%s " "$CMD"
301 done
302}
303
304. /usr/local/lib/7dtd/help.sh
305. /usr/local/lib/7dtd/serverconfig.sh
306for M in /usr/local/lib/7dtd/commands/*.sh; do
307 . $M
308done
309
310checkRootLoadConf
311
Note: See TracBrowser for help on using the repository browser.