[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"
|
---|
[294] | 41 | local ownerId="$5"
|
---|
[17] | 42 |
|
---|
[294] | 43 | logPlayerConnect $INSTANCE "$entityId" "$name" "$steamId" "$ip" "$ownerId"
|
---|
[17] | 44 |
|
---|
[259] | 45 | for H in $(getHooksFor playerConnect $INSTANCE); do
|
---|
[294] | 46 | $H $INSTANCE "$entityId" "$name" "$steamId" "$ip" "$ownerId"
|
---|
[17] | 47 | done
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | handleDisconnect() {
|
---|
[234] | 51 | local playerId="$1"
|
---|
[87] | 52 | local entityId="$2"
|
---|
[17] | 53 |
|
---|
[87] | 54 | logPlayerDisconnect $INSTANCE "$entityId"
|
---|
| 55 |
|
---|
[259] | 56 | for H in $(getHooksFor playerDisconnect $INSTANCE); do
|
---|
[234] | 57 | $H $INSTANCE "$playerId" "$entityId" "$NICKNAME" "$STEAMID"
|
---|
[17] | 58 | done
|
---|
| 59 | }
|
---|
| 60 |
|
---|
[62] | 61 | handleChat() {
|
---|
[275] | 62 | echo "$(timestamp): $1: $2" >> $CHATLOG
|
---|
[62] | 63 |
|
---|
[259] | 64 | for H in $(getHooksFor chat $INSTANCE); do
|
---|
[275] | 65 | $H $INSTANCE "$1" "$2"
|
---|
| 66 | done
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | handleGmsg() {
|
---|
| 70 | echo "$(timestamp): GMSG: $1" >> $CHATLOG
|
---|
| 71 |
|
---|
| 72 | for H in $(getHooksFor gmsg $INSTANCE); do
|
---|
[63] | 73 | $H $INSTANCE "$1"
|
---|
[62] | 74 | done
|
---|
| 75 | }
|
---|
| 76 |
|
---|
[87] | 77 | handleRemoteCommand() {
|
---|
| 78 | local cmd="$1"
|
---|
| 79 | local name="$2"
|
---|
| 80 |
|
---|
| 81 | echo "$(timestamp): Player \"$name\" executed \"$cmd\"" >> $COMMANDLOG
|
---|
| 82 |
|
---|
[259] | 83 | for H in $(getHooksFor remoteCommand $INSTANCE); do
|
---|
[87] | 84 | $H $INSTANCE "$cmd" "$name"
|
---|
| 85 | done
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | handleTelnetCommand() {
|
---|
| 89 | local cmd="$1"
|
---|
| 90 | local ip="$2"
|
---|
| 91 |
|
---|
| 92 | echo "$(timestamp): Telnet from \"$ip\" executed \"$cmd\"" >> $COMMANDLOG
|
---|
| 93 |
|
---|
[259] | 94 | for H in $(getHooksFor telnetCommand $INSTANCE); do
|
---|
[87] | 95 | $H $INSTANCE "$cmd" "$ip"
|
---|
| 96 | done
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 |
|
---|
[67] | 100 | if [ ! -d "$(getInstancePath $INSTANCE)/logs" ]; then
|
---|
| 101 | mkdir "$(getInstancePath $INSTANCE)/logs"
|
---|
| 102 | fi
|
---|
| 103 |
|
---|
| 104 | setAllPlayersOffline
|
---|
| 105 |
|
---|
[219] | 106 | rm $(getInstancePath $INSTANCE)/logs/current_output_log.txt
|
---|
[87] | 107 | rm $(getInstancePath $INSTANCE)/logs/current_chat.log
|
---|
| 108 | rm $(getInstancePath $INSTANCE)/logs/current_commandExecution.log
|
---|
[219] | 109 | ln -s $LOG $(getInstancePath $INSTANCE)/logs/current_output_log.txt
|
---|
[87] | 110 | ln -s $CHATLOG $(getInstancePath $INSTANCE)/logs/current_chat.log
|
---|
| 111 | ln -s $COMMANDLOG $(getInstancePath $INSTANCE)/logs/current_commandExecution.log
|
---|
| 112 |
|
---|
[18] | 113 | sleep 5
|
---|
| 114 |
|
---|
[17] | 115 | NOBUF="stdbuf -e0 -o0"
|
---|
| 116 |
|
---|
[220] | 117 | $NOBUF tail -n 5000 -F $LOG |
|
---|
[17] | 118 | $NOBUF tr '\\' '/' |
|
---|
| 119 | $NOBUF tr -d '\r' |
|
---|
| 120 | $NOBUF grep -v "^(Filename: " |
|
---|
[219] | 121 | $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] | 122 | while read line ; do
|
---|
| 123 | if [ -n "$line" ]; then
|
---|
[294] | 124 | #Player connected, entityid=1278, name=termo2, steamid=76561197997439820, steamOwner=76561197997439820, ip=178.203.27.140
|
---|
[298] | 125 | #Player connected, entityid=[0-9]*, name=.*, steamid=[0-9]*, steamOwner=[0-9]*, ip=[a-fA-F:0-9.]*$
|
---|
[87] | 126 | if [ -n "$(echo "$line" | grep '^Player connected,')" ]; then
|
---|
[298] | 127 | entityId=$(expr "$line" : 'Player connected, entityid=\([0-9]*\), name=.*, steamid=[0-9]*, steamOwner=[0-9]*, ip=[a-fA-F:0-9.]*$')
|
---|
| 128 | playerName=$(expr "$line" : 'Player connected, entityid=[0-9]*, name=\(.*\), steamid=[0-9]*, steamOwner=[0-9]*, ip=[a-fA-F:0-9.]*$')
|
---|
| 129 | steamId=$(expr "$line" : 'Player connected, entityid=[0-9]*, name=.*, steamid=\([0-9]*\), steamOwner=[0-9]*, ip=[a-fA-F:0-9.]*$')
|
---|
| 130 | steamOwner=$(expr "$line" : 'Player connected, entityid=[0-9]*, name=.*, steamid=[0-9]*, steamOwner=\([0-9]*\), ip=[a-fA-F:0-9.]*$')
|
---|
| 131 | ip=$(expr "$line" : 'Player connected, entityid=[0-9]*, name=.*, steamid=[0-9]*, steamOwner=[0-9]*, ip=\([a-fA-F:0-9.]*\)$')
|
---|
[17] | 132 | sleep 1
|
---|
[294] | 133 | handleConnect "$entityId" "$playerName" "$steamId" "$ip" "$steamOwner"
|
---|
| 134 | unset entityId playerName steamId steamOwner ip
|
---|
[62] | 135 | else
|
---|
[234] | 136 | #Player disconnected: EntityID=[0-9]*, PlayerID='[0-9]*', OwnerID='[0-9]*', PlayerName='.*'$
|
---|
| 137 | if [ -n "$(echo "$line" | grep '^Player disconnected: ')" ]; then
|
---|
| 138 | playerId=$(expr "$line" : "Player disconnected: EntityID=[0-9]*, PlayerID='\([0-9]*\)', OwnerID='[0-9]*', PlayerName='.*'$")
|
---|
| 139 | entityId=$(expr "$line" : "Player disconnected: EntityID=\([0-9]*\), PlayerID='[0-9]*', OwnerID='[0-9]*', PlayerName='.*'$")
|
---|
[57] | 140 | handleDisconnect "$playerId" "$entityId"
|
---|
[17] | 141 | unset playerId entityId
|
---|
[62] | 142 | else
|
---|
[87] | 143 | #GMSG: .*$
|
---|
[62] | 144 | if [ -n "$(echo "$line" | grep -E '^GMSG: .+')" ]; then
|
---|
| 145 | msg=$(expr "$line" : 'GMSG: \(.*\)$')
|
---|
[275] | 146 | handleGmsg "$msg"
|
---|
[62] | 147 | unset msg
|
---|
[87] | 148 | else
|
---|
[275] | 149 | #Chat: 'name': .*$
|
---|
| 150 | if [ -n "$(echo "$line" | grep -E '^Chat: .+')" ]; then
|
---|
| 151 | name=$(expr "$line" : "Chat: '\(.*\)': .*$")
|
---|
| 152 | msg=$(expr "$line" : "Chat: '.*': \(.*\)$")
|
---|
| 153 | handleChat "$name" "$msg"
|
---|
| 154 | unset name msg
|
---|
| 155 | else
|
---|
[234] | 156 | #Executing command ".*" from client ".*"$
|
---|
| 157 | if [ -n "$(echo "$line" | grep '^Executing command '.*' from client')" ]; then
|
---|
| 158 | cmd=$(expr "$line" : "Executing command '\(.*\)' from client .*$")
|
---|
| 159 | nick=$(expr "$line" : "Executing command '.*' from client \(.*\)$")
|
---|
[87] | 160 | handleRemoteCommand "$cmd" "$nick"
|
---|
| 161 | unset cmd nick
|
---|
| 162 | else
|
---|
[234] | 163 | #Executing command ".*" by Telnet from .*$
|
---|
| 164 | if [ -n "$(echo "$line" | grep '^Executing command '.*' by Telnet from ')" ]; then
|
---|
| 165 | cmd=$(expr "$line" : "Executing command '\(.*\)' by Telnet from .*$")
|
---|
| 166 | ip=$(expr "$line" : "Executing command '.*' by Telnet from \(.*\)$")
|
---|
[87] | 167 | handleTelnetCommand "$cmd" "$ip"
|
---|
| 168 | unset cmd ip
|
---|
[17] | 169 | fi
|
---|
[62] | 170 | fi
|
---|
| 171 | fi
|
---|
[87] | 172 | fi
|
---|
| 173 | fi
|
---|
[275] | 174 | fi
|
---|
[17] | 175 | fi
|
---|
| 176 | done
|
---|