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

Last change on this file since 51 was 46, checked in by alloc, 10 years ago

Telnet command fix

File size: 5.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# Check if a given port range (baseport, baseport+1, baseport+2 each udp)
99# is already in use by any other instance
100# Params:
101# 1: Baseport
102# 2: Current instance (ignored)
103# Returns:
104# 0/1 not in use/in use
105checkGamePortUsed() {
106 local PORTMIN=$1
107 local PORTMAX=$(( $1 + 2 ))
108 local I
109 for I in $(getInstanceList); do
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
117 fi
118 done
119 echo 0
120}
121
122# Check if a given TCP port is already in use by any instance (either by control
123# panel or telnet)
124# Params:
125# 1: Port
126# Returns:
127# 0/1 not in use/in use
128checkTCPPortUsed() {
129 local I
130 for I in $(getInstanceList); do
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
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# 3: (Optional) Timeout in sec, defaulting to 1
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 local TEMPFILE=$(mktemp)
162 rm -f $TEMPFILE
163 mkfifo $TEMPFILE
164 exec 3<> $TEMPFILE
165 nc 127.0.0.1 $TEL_PORT <&3 &
166 local NCPID=$!
167 printf "$TEL_PASS\n$2\n" >&3
168 sleep ${3:-1}
169 printf "exit\n" >&3
170 sleep 0.2
171 kill -9 $NCPID > /dev/null 2>&1
172 exec 3>&-
173 rm -f $TEMPFILE
174 else
175 echo "Telnet not enabled or no password set."
176 fi
177}
178
179# Get all hook files for the given hook-name
180# Params:
181# 1: Hook name
182# Returns:
183# Names of hook files
184getHooksFor() {
185 if [ -d $SDTD_BASE/hooks/$1 ]; then
186 local H
187 for H in $SDTD_BASE/hooks/$1/*.sh; do
188 echo "$H"
189 done
190 fi
191}
192
193# Lowercase passed string
194# Params:
195# 1: String
196# Returns:
197# Lowercased string
198lowercase() {
199 echo "${1}" | tr "[:upper:]" "[:lower:]"
200}
201
202# Prepare passed string as part of camelcase, i.e. first char upper case, others
203# lowercase
204# Params:
205# 1: String
206# Returns:
207# Transformed string
208camelcasePrep() {
209 echo $(echo "${1:0:1}" | tr "[:lower:]" "[:upper:]")$(echo "${1:1}" | tr "[:upper:]" "[:lower:]")
210}
211
212# Check if given value is a (integer) number
213# Params:
214# 1: Value
215# Returns:
216# 0/1 for NaN / is a number
217isANumber() {
218 if [[ $1 =~ ^[0-9]+$ ]] ; then
219 echo "1"
220 else
221 echo "0"
222 fi
223}
224
225# Check if given value is a boolean (true/false, yes/no, y/n)
226# Params:
227# 1: Value
228# Returns:
229# 0/1
230isABool() {
231 local LOW=$(lowercase "$1")
232 if [ "$LOW" = "false" -o "$LOW" = "true"\
233 -o "$LOW" = "yes" -o "$LOW" = "y"\
234 -o "$LOW" = "no" -o "$LOW" = "n" ]; then
235 echo 1
236 else
237 echo 0
238 fi
239}
240
241# Convert the given value to a boolean 0/1
242# Params:
243# 1: Value
244# Returns:
245# 0/1 as false/true
246getBool() {
247 if [ $(isABool "$1") -eq 0 ]; then
248 echo 0
249 else
250 local LOW=$(lowercase "$1")
251 if [ "$LOW" = "true" -o "$LOW" = "yes" -o "$LOW" = "y" ]; then
252 echo 1
253 else
254 echo 0
255 fi
256 fi
257}
258
259listCommands() {
260 local C
261 for C in $(declare -F | cut -d\ -f3 | grep "^sdtdCommand"\
262 | grep -v "Help$"\
263 | grep -v "Description$"\
264 | grep -v "Expects$"); do
265 local CMD=$(lowercase "${C#sdtdCommand}")
266 printf "%s " "$CMD"
267 done
268}
269
270. /usr/local/lib/7dtd/help.sh
271. /usr/local/lib/7dtd/playerlog.sh
272. /usr/local/lib/7dtd/serverconfig.sh
273for M in /usr/local/lib/7dtd/commands/*.sh; do
274 . $M
275done
276
Note: See TracBrowser for help on using the repository browser.