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