| [17] | 1 | #!/bin/bash | 
|---|
|  | 2 |  | 
|---|
| [258] | 3 | #   Copyright 2016 Christian 'Alloc' Illy | 
|---|
|  | 4 | # | 
|---|
|  | 5 | #   Licensed under the Apache License, Version 2.0 (the "License"); | 
|---|
|  | 6 | #   you may not use this file except in compliance with the License. | 
|---|
|  | 7 | #   You may obtain a copy of the License at | 
|---|
|  | 8 | # | 
|---|
|  | 9 | #       http://www.apache.org/licenses/LICENSE-2.0 | 
|---|
|  | 10 | # | 
|---|
|  | 11 | #   Unless required by applicable law or agreed to in writing, software | 
|---|
|  | 12 | #   distributed under the License is distributed on an "AS IS" BASIS, | 
|---|
|  | 13 | #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|---|
|  | 14 | #   See the License for the specific language governing permissions and | 
|---|
|  | 15 | #   limitations under the License. | 
|---|
|  | 16 |  | 
|---|
|  | 17 |  | 
|---|
| [17] | 18 | . /usr/local/lib/7dtd/common.sh | 
|---|
| [67] | 19 | . /usr/local/lib/7dtd/playerlog.sh | 
|---|
| [17] | 20 |  | 
|---|
|  | 21 | if [ $(isValidInstance $1) -eq 0 ]; then | 
|---|
|  | 22 | echo "No instance given or not a valid instance!" | 
|---|
|  | 23 | return | 
|---|
|  | 24 | fi | 
|---|
|  | 25 |  | 
|---|
|  | 26 | INSTANCE=$1 | 
|---|
| [240] | 27 | LOGTIMESTAMP=$2 | 
|---|
|  | 28 | LOG=$(getInstancePath $INSTANCE)/logs/${LOGTIMESTAMP}_output_log.txt | 
|---|
|  | 29 | CHATLOG=$(getInstancePath $INSTANCE)/logs/${LOGTIMESTAMP}_chat.log | 
|---|
|  | 30 | COMMANDLOG=$(getInstancePath $INSTANCE)/logs/${LOGTIMESTAMP}_commandExecution.log | 
|---|
| [17] | 31 |  | 
|---|
|  | 32 | timestamp() { | 
|---|
|  | 33 | date '+%Y.%m.%d %H:%M:%S' | 
|---|
|  | 34 | } | 
|---|
|  | 35 |  | 
|---|
|  | 36 | handleConnect() { | 
|---|
| [234] | 37 | local entityId="$1" | 
|---|
|  | 38 | local name="$2" | 
|---|
|  | 39 | local steamId="$3" | 
|---|
|  | 40 | local ip="$4" | 
|---|
| [17] | 41 |  | 
|---|
| [87] | 42 | logPlayerConnect $INSTANCE "$entityId" "$name" "$steamId" "$ip" | 
|---|
| [17] | 43 |  | 
|---|
| [259] | 44 | for H in $(getHooksFor playerConnect $INSTANCE); do | 
|---|
| [234] | 45 | $H $INSTANCE "$entityId" "$name" "$steamId" "$ip" | 
|---|
| [17] | 46 | done | 
|---|
|  | 47 | } | 
|---|
|  | 48 |  | 
|---|
|  | 49 | handleDisconnect() { | 
|---|
| [234] | 50 | local playerId="$1" | 
|---|
| [87] | 51 | local entityId="$2" | 
|---|
| [17] | 52 |  | 
|---|
| [87] | 53 | logPlayerDisconnect $INSTANCE "$entityId" | 
|---|
|  | 54 |  | 
|---|
| [259] | 55 | for H in $(getHooksFor playerDisconnect $INSTANCE); do | 
|---|
| [234] | 56 | $H $INSTANCE "$playerId" "$entityId" "$NICKNAME" "$STEAMID" | 
|---|
| [17] | 57 | done | 
|---|
|  | 58 | } | 
|---|
|  | 59 |  | 
|---|
| [62] | 60 | handleChat() { | 
|---|
| [275] | 61 | echo "$(timestamp): $1: $2" >> $CHATLOG | 
|---|
| [62] | 62 |  | 
|---|
| [259] | 63 | for H in $(getHooksFor chat $INSTANCE); do | 
|---|
| [275] | 64 | $H $INSTANCE "$1" "$2" | 
|---|
|  | 65 | done | 
|---|
|  | 66 | } | 
|---|
|  | 67 |  | 
|---|
|  | 68 | handleGmsg() { | 
|---|
|  | 69 | echo "$(timestamp): GMSG: $1" >> $CHATLOG | 
|---|
|  | 70 |  | 
|---|
|  | 71 | for H in $(getHooksFor gmsg $INSTANCE); do | 
|---|
| [63] | 72 | $H $INSTANCE "$1" | 
|---|
| [62] | 73 | done | 
|---|
|  | 74 | } | 
|---|
|  | 75 |  | 
|---|
| [87] | 76 | handleRemoteCommand() { | 
|---|
|  | 77 | local cmd="$1" | 
|---|
|  | 78 | local name="$2" | 
|---|
|  | 79 |  | 
|---|
|  | 80 | echo "$(timestamp): Player \"$name\" executed \"$cmd\"" >> $COMMANDLOG | 
|---|
|  | 81 |  | 
|---|
| [259] | 82 | for H in $(getHooksFor remoteCommand $INSTANCE); do | 
|---|
| [87] | 83 | $H $INSTANCE "$cmd" "$name" | 
|---|
|  | 84 | done | 
|---|
|  | 85 | } | 
|---|
|  | 86 |  | 
|---|
|  | 87 | handleTelnetCommand() { | 
|---|
|  | 88 | local cmd="$1" | 
|---|
|  | 89 | local ip="$2" | 
|---|
|  | 90 |  | 
|---|
|  | 91 | echo "$(timestamp): Telnet from \"$ip\" executed \"$cmd\"" >> $COMMANDLOG | 
|---|
|  | 92 |  | 
|---|
| [259] | 93 | for H in $(getHooksFor telnetCommand $INSTANCE); do | 
|---|
| [87] | 94 | $H $INSTANCE "$cmd" "$ip" | 
|---|
|  | 95 | done | 
|---|
|  | 96 | } | 
|---|
|  | 97 |  | 
|---|
|  | 98 |  | 
|---|
| [67] | 99 | if [ ! -d "$(getInstancePath $INSTANCE)/logs" ]; then | 
|---|
|  | 100 | mkdir "$(getInstancePath $INSTANCE)/logs" | 
|---|
|  | 101 | fi | 
|---|
|  | 102 |  | 
|---|
|  | 103 | setAllPlayersOffline | 
|---|
|  | 104 |  | 
|---|
| [219] | 105 | rm $(getInstancePath $INSTANCE)/logs/current_output_log.txt | 
|---|
| [87] | 106 | rm $(getInstancePath $INSTANCE)/logs/current_chat.log | 
|---|
|  | 107 | rm $(getInstancePath $INSTANCE)/logs/current_commandExecution.log | 
|---|
| [219] | 108 | ln -s $LOG $(getInstancePath $INSTANCE)/logs/current_output_log.txt | 
|---|
| [87] | 109 | ln -s $CHATLOG $(getInstancePath $INSTANCE)/logs/current_chat.log | 
|---|
|  | 110 | ln -s $COMMANDLOG $(getInstancePath $INSTANCE)/logs/current_commandExecution.log | 
|---|
|  | 111 |  | 
|---|
| [18] | 112 | sleep 5 | 
|---|
|  | 113 |  | 
|---|
| [17] | 114 | NOBUF="stdbuf -e0 -o0" | 
|---|
|  | 115 |  | 
|---|
| [220] | 116 | $NOBUF tail -n 5000 -F $LOG | | 
|---|
| [17] | 117 | $NOBUF tr '\\' '/' | | 
|---|
|  | 118 | $NOBUF tr -d '\r' | | 
|---|
|  | 119 | $NOBUF grep -v "^(Filename: " | | 
|---|
| [219] | 120 | $NOBUF sed -r 's/^[0-9]+-[0-9]+-[0-9]+T[0-9]+:[0-9]+:[0-9]+ [0-9]+[.,][0-9]+ [A-Z]+ (.*)$/\1/' | | 
|---|
| [17] | 121 | while read line ; do | 
|---|
|  | 122 | if [ -n "$line" ]; then | 
|---|
| [234] | 123 | #Player connected, entityid=1278, name=termo2, steamid=76561197997439820, ip=178.203.27.140 | 
|---|
|  | 124 | #Player connected, entityid=[0-9]*, name=.*, steamid=[0-9]*, ip=[0-9.]*$ | 
|---|
| [87] | 125 | if [ -n "$(echo "$line" | grep '^Player connected,')" ]; then | 
|---|
| [234] | 126 | entityId=$(expr "$line" : 'Player connected, entityid=\([0-9]*\), name=.*, steamid=[0-9]*, ip=[0-9.]*$') | 
|---|
|  | 127 | playerName=$(expr "$line" : 'Player connected, entityid=[0-9]*, name=\(.*\), steamid=[0-9]*, ip=[0-9.]*$') | 
|---|
|  | 128 | steamId=$(expr "$line" : 'Player connected, entityid=[0-9]*, name=.*, steamid=\([0-9]*\), ip=[0-9.]*$') | 
|---|
|  | 129 | ip=$(expr "$line" : 'Player connected, entityid=[0-9]*, name=.*, steamid=[0-9]*, ip=\([0-9.]*\)$') | 
|---|
| [17] | 130 | sleep 1 | 
|---|
| [234] | 131 | handleConnect "$entityId" "$playerName" "$steamId" "$ip" | 
|---|
|  | 132 | unset entityId playerName steamId ip | 
|---|
| [62] | 133 | else | 
|---|
| [234] | 134 | #Player disconnected: EntityID=[0-9]*, PlayerID='[0-9]*', OwnerID='[0-9]*', PlayerName='.*'$ | 
|---|
|  | 135 | if [ -n "$(echo "$line" | grep '^Player disconnected: ')" ]; then | 
|---|
|  | 136 | playerId=$(expr "$line" : "Player disconnected: EntityID=[0-9]*, PlayerID='\([0-9]*\)', OwnerID='[0-9]*', PlayerName='.*'$") | 
|---|
|  | 137 | entityId=$(expr "$line" : "Player disconnected: EntityID=\([0-9]*\), PlayerID='[0-9]*', OwnerID='[0-9]*', PlayerName='.*'$") | 
|---|
| [57] | 138 | handleDisconnect "$playerId" "$entityId" | 
|---|
| [17] | 139 | unset playerId entityId | 
|---|
| [62] | 140 | else | 
|---|
| [87] | 141 | #GMSG: .*$ | 
|---|
| [62] | 142 | if [ -n "$(echo "$line" | grep -E '^GMSG: .+')" ]; then | 
|---|
|  | 143 | msg=$(expr "$line" : 'GMSG: \(.*\)$') | 
|---|
| [275] | 144 | handleGmsg "$msg" | 
|---|
| [62] | 145 | unset msg | 
|---|
| [87] | 146 | else | 
|---|
| [275] | 147 | #Chat: 'name': .*$ | 
|---|
|  | 148 | if [ -n "$(echo "$line" | grep -E '^Chat: .+')" ]; then | 
|---|
|  | 149 | name=$(expr "$line" : "Chat: '\(.*\)': .*$") | 
|---|
|  | 150 | msg=$(expr "$line" : "Chat: '.*': \(.*\)$") | 
|---|
|  | 151 | handleChat "$name" "$msg" | 
|---|
|  | 152 | unset name msg | 
|---|
|  | 153 | else | 
|---|
| [234] | 154 | #Executing command ".*" from client ".*"$ | 
|---|
|  | 155 | if [ -n "$(echo "$line" | grep '^Executing command '.*' from client')" ]; then | 
|---|
|  | 156 | cmd=$(expr "$line" : "Executing command '\(.*\)' from client .*$") | 
|---|
|  | 157 | nick=$(expr "$line" : "Executing command '.*' from client \(.*\)$") | 
|---|
| [87] | 158 | handleRemoteCommand "$cmd" "$nick" | 
|---|
|  | 159 | unset cmd nick | 
|---|
|  | 160 | else | 
|---|
| [234] | 161 | #Executing command ".*" by Telnet from .*$ | 
|---|
|  | 162 | if [ -n "$(echo "$line" | grep '^Executing command '.*' by Telnet from ')" ]; then | 
|---|
|  | 163 | cmd=$(expr "$line" : "Executing command '\(.*\)' by Telnet from .*$") | 
|---|
|  | 164 | ip=$(expr "$line" : "Executing command '.*' by Telnet from \(.*\)$") | 
|---|
| [87] | 165 | handleTelnetCommand "$cmd" "$ip" | 
|---|
|  | 166 | unset cmd ip | 
|---|
| [17] | 167 | fi | 
|---|
| [62] | 168 | fi | 
|---|
|  | 169 | fi | 
|---|
| [87] | 170 | fi | 
|---|
|  | 171 | fi | 
|---|
| [275] | 172 | fi | 
|---|
| [17] | 173 | fi | 
|---|
|  | 174 | done | 
|---|