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

Last change on this file since 21 was 20, checked in by alloc, 10 years ago

Version 5. Fixes #9, #11

File size: 5.3 KB
Line 
1#!/bin/bash
2# Version 5
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
7checkRootLoadConf() {
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
20getInstancePath() {
21 echo $SDTD_BASE/instances/$1
22}
23
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
30isValidInstanceName() {
31 if [[ "$1" =~ ^[A-Za-z0-9_\-]+$ ]]; then
32 echo 1
33 else
34 echo 0
35 fi
36}
37
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
43isValidInstance() {
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
51 fi
52 fi
53 fi
54 echo 0
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
63isRunning() {
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
75getInstanceList() {
76 local IF
77 for IF in $SDTD_BASE/instances/*; do
78 local I=`basename $IF`
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
91getInstancePID() {
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)
100# is already in use by any other instance
101# Params:
102# 1: Baseport
103# 2: Current instance (ignored)
104# Returns:
105# 0/1 not in use/in use
106checkGamePortUsed() {
107 local PORTMIN=$1
108 local PORTMAX=$(( $1 + 2 ))
109 local I
110 for I in $(getInstanceList); do
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
118 fi
119 done
120 echo 0
121}
122
123# Check if a given TCP port is already in use by any instance (either by control
124# panel or telnet)
125# Params:
126# 1: Port
127# Returns:
128# 0/1 not in use/in use
129checkTCPPortUsed() {
130 local I
131 for I in $(getInstanceList); do
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
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
156telnetCommand() {
157 local TEL_ENABLED=$(getConfigValue $1 TelnetEnabled)
158 local TEL_PORT=$(getConfigValue $1 TelnetPort)
159 local TEL_PASS=$(getConfigValue $1 TelnetPassword)
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
172getHooksFor() {
173 if [ -d $SDTD_BASE/hooks/$1 ]; then
174 local H
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
186lowercase() {
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
196camelcasePrep() {
197 echo $(echo "${1:0:1}" | tr "[:lower:]" "[:upper:]")$(echo "${1:1}" | tr "[:upper:]" "[:lower:]")
198}
199
200# Check if given value is a (integer) number
201# Params:
202# 1: Value
203# Returns:
204# 0/1 for NaN / is a number
205isANumber() {
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
218isABool() {
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
234getBool() {
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
247listCommands() {
248 local C
249 for C in $(declare -F | cut -d\ -f3 | grep "^sdtdCommand"\
250 | grep -v "Help$"\
251 | grep -v "Description$"\
252 | grep -v "Expects$"); do
253 local CMD=$(lowercase "${C#sdtdCommand}")
254 printf "%s " "$CMD"
255 done
256}
257
258. /usr/local/lib/7dtd/help.sh
259. /usr/local/lib/7dtd/playerlog.sh
260. /usr/local/lib/7dtd/serverconfig.sh
261for M in /usr/local/lib/7dtd/commands/*.sh; do
262 . $M
263done
264
Note: See TracBrowser for help on using the repository browser.