| 1 | #!/bin/bash | 
|---|
| 2 | # Provides common functions for 7dtd-scripts. Not intended to be run directly. | 
|---|
| 3 |  | 
|---|
| 4 | # Check if the script is run as root (exit otherwise) and load global config | 
|---|
| 5 | checkRootLoadConf() { | 
|---|
| 6 | if [ `id -u` -ne 0 ]; then | 
|---|
| 7 | echo "This script has to be run as root!" | 
|---|
| 8 | exit 10 | 
|---|
| 9 | fi | 
|---|
| 10 | . /etc/7dtd.conf | 
|---|
| 11 | } | 
|---|
| 12 |  | 
|---|
| 13 | # Get the config path for the given instance | 
|---|
| 14 | # Params: | 
|---|
| 15 | #   1: Instance name | 
|---|
| 16 | # Returns: | 
|---|
| 17 | #   Config path for instance | 
|---|
| 18 | getInstancePath() { | 
|---|
| 19 | echo $SDTD_BASE/instances/$1 | 
|---|
| 20 | } | 
|---|
| 21 |  | 
|---|
| 22 | # Check if the given instance name is an existing instance | 
|---|
| 23 | # On failure exit the script! | 
|---|
| 24 | # Params: | 
|---|
| 25 | #   1: Instance name | 
|---|
| 26 | checkInstance() { | 
|---|
| 27 | if [ -z $1 ]; then | 
|---|
| 28 | echo "No instance given!" | 
|---|
| 29 | exit 2 | 
|---|
| 30 | fi | 
|---|
| 31 | if [ ! -d $(getInstancePath $1) ]; then | 
|---|
| 32 | echo "Instance $1 does not exist!" | 
|---|
| 33 | exit 3 | 
|---|
| 34 | fi | 
|---|
| 35 | } | 
|---|
| 36 |  | 
|---|
| 37 | # Check if the given instance is currently running | 
|---|
| 38 | # Params: | 
|---|
| 39 | #   1: Instance name | 
|---|
| 40 | # Returns: | 
|---|
| 41 | #   0 = not running | 
|---|
| 42 | #   1 = running | 
|---|
| 43 | isRunning() { | 
|---|
| 44 | start-stop-daemon --status --pidfile $(getInstancePath $1)/7dtd.pid | 
|---|
| 45 | if [ $? -eq 0 ]; then | 
|---|
| 46 | echo 1 | 
|---|
| 47 | else | 
|---|
| 48 | echo 0 | 
|---|
| 49 | fi | 
|---|
| 50 | } | 
|---|
| 51 |  | 
|---|
| 52 | # Get a single value from a serverconfig | 
|---|
| 53 | # Params: | 
|---|
| 54 | #   1: Instance name | 
|---|
| 55 | #   2: Property name | 
|---|
| 56 | # Returns: | 
|---|
| 57 | #   Property value | 
|---|
| 58 | getConfigValue() { | 
|---|
| 59 | CONF=$(getInstancePath $1)/serverconfig.xml | 
|---|
| 60 | xmllint --xpath "string(/ServerSettings/property[@name='$2']/@value)" $CONF | 
|---|
| 61 | } | 
|---|
| 62 |  | 
|---|
| 63 | # Send a single command to the telnet port | 
|---|
| 64 | # Params: | 
|---|
| 65 | #   1: Instance name | 
|---|
| 66 | #   2: Command | 
|---|
| 67 | # Returns: | 
|---|
| 68 | #   String of telnet output | 
|---|
| 69 | telnetCommand() { | 
|---|
| 70 | TEL_ENABLED=$(getConfigValue $1 TelnetEnabled) | 
|---|
| 71 | TEL_PORT=$(getConfigValue $1 TelnetPort) | 
|---|
| 72 | TEL_PASS=$(getConfigValue $1 TelnetPassword) | 
|---|
| 73 | if [ "$TEL_ENABLED" = "true" ] && [ -n "$TEL_PASS" ]; then | 
|---|
| 74 | echo -e "$TEL_PASS\n$2\nexit" | nc -q 2 127.0.0.1 $TEL_PORT | 
|---|
| 75 | else | 
|---|
| 76 | echo "Telnet not enabled or no password set." | 
|---|
| 77 | fi | 
|---|
| 78 | } | 
|---|