1#!/bin/bash 2# 3# OpenJK server utility functions. 4# 5 6# Send and receive UDP datagrams 7function sendrecv 8{ 9 local COMMAND="$*" 10 local HEADER="\0377\0377\0377\0377" 11 echo -e "${HEADER}${COMMAND}" | socat -t3 -T3 - UDP:127.0.0.1:29070 | tr -d "${HEADER}" 2>/dev/null 12} 13 14# Parse a config file and get the value for a cvar 15function parseconfig 16{ 17 local FILE="$1" 18 local KEY="$2" 19 if [ -e "$FILE" ]; then 20 grep -E -i -m 1 "^set[usa]?\s+${KEY}\s+(.*)$" "$FILE" | sed -r 's#^set[usa]?\s+[^ ]+\s+"?([^"]*)"?\s*$#\1#g' 21 fi 22} 23 24# Parse an info string and get the value for a key 25function parseinfo 26{ 27 local INFO="$1" 28 local KEY="$2" 29 echo "$INFO" | grep -o "\\\\${KEY}\\\\[^\\\\]*\\\\" | cut -d"\\" -f 3 30} 31 32# Get live server info 33function getinfo 34{ 35 sendrecv getinfo | tail -n +2 36} 37 38# Get live server status 39function getstatus 40{ 41 sendrecv getstatus | tail -n +2 42} 43 44# Get live server serverinfo 45function getserverinfo 46{ 47 getstatus | head -n 1 48} 49 50# Get live server players 51function getplayers 52{ 53 getstatus | tail -n +2 54} 55 56# Execute a rcon command 57function rcon 58{ 59 local COMMAND="$*" 60 local CONFIG 61 for CONFIG in "$OJK_HOMEPATH/$OJK_MOD/server.cfg" "$OJK_BASEPATH/$OJK_MOD/server.cfg" "$OJK_CDPATH/$OJK_MOD/server.cfg"; do 62 local RCON_PASSWORD=`parseconfig "$CONFIG" rconpassword` 63 if [ -n "$RCON_PASSWORD" ]; then 64 sendrecv "rcon $RCON_PASSWORD $COMMAND" 65 break 66 fi 67 done 68} 69