[17] | 1 | #!/bin/bash
|
---|
| 2 |
|
---|
| 3 | . /usr/local/lib/7dtd/common.sh
|
---|
[67] | 4 | . /usr/local/lib/7dtd/playerlog.sh
|
---|
[17] | 5 |
|
---|
| 6 | if [ $(isValidInstance $1) -eq 0 ]; then
|
---|
| 7 | echo "No instance given or not a valid instance!"
|
---|
| 8 | return
|
---|
| 9 | fi
|
---|
| 10 |
|
---|
| 11 | INSTANCE=$1
|
---|
[219] | 12 | LOG=$2
|
---|
| 13 | #LOG=$(getInstancePath $INSTANCE)/logs/$(date '+%Y-%m-%d_%H-%M-%S')_output_log.txt
|
---|
[67] | 14 | CHATLOG=$(getInstancePath $INSTANCE)/logs/$(date '+%Y-%m-%d_%H-%M-%S')_chat.log
|
---|
[87] | 15 | COMMANDLOG=$(getInstancePath $INSTANCE)/logs/$(date '+%Y-%m-%d_%H-%M-%S')_commandExecution.log
|
---|
[17] | 16 |
|
---|
| 17 | timestamp() {
|
---|
| 18 | date '+%Y.%m.%d %H:%M:%S'
|
---|
| 19 | }
|
---|
| 20 |
|
---|
| 21 | handleConnect() {
|
---|
[87] | 22 | local clientId="$1"
|
---|
| 23 | local entityId="$2"
|
---|
| 24 | local name="$3"
|
---|
| 25 | local steamId="$4"
|
---|
| 26 | local ip="$5"
|
---|
[17] | 27 |
|
---|
[87] | 28 | logPlayerConnect $INSTANCE "$entityId" "$name" "$steamId" "$ip"
|
---|
[17] | 29 |
|
---|
| 30 | for H in $(getHooksFor playerConnect); do
|
---|
[87] | 31 | $H $INSTANCE "$clientId" "$entityId" "$name" "$steamId" "$ip"
|
---|
[17] | 32 | done
|
---|
| 33 | }
|
---|
| 34 |
|
---|
| 35 | handleDisconnect() {
|
---|
[87] | 36 | local clientId="$1"
|
---|
| 37 | local entityId="$2"
|
---|
[17] | 38 |
|
---|
[87] | 39 | logPlayerDisconnect $INSTANCE "$entityId"
|
---|
| 40 |
|
---|
[17] | 41 | for H in $(getHooksFor playerDisconnect); do
|
---|
[87] | 42 | $H $INSTANCE "$clientId" "$entityId" "$NICKNAME" "$STEAMID"
|
---|
[17] | 43 | done
|
---|
| 44 | }
|
---|
| 45 |
|
---|
[62] | 46 | handleChat() {
|
---|
| 47 | echo "$(timestamp): $1" >> $CHATLOG
|
---|
| 48 |
|
---|
| 49 | for H in $(getHooksFor chat); do
|
---|
[63] | 50 | $H $INSTANCE "$1"
|
---|
[62] | 51 | done
|
---|
| 52 | }
|
---|
| 53 |
|
---|
[87] | 54 | handleRemoteCommand() {
|
---|
| 55 | local cmd="$1"
|
---|
| 56 | local name="$2"
|
---|
| 57 |
|
---|
| 58 | echo "$(timestamp): Player \"$name\" executed \"$cmd\"" >> $COMMANDLOG
|
---|
| 59 |
|
---|
| 60 | for H in $(getHooksFor remoteCommand); do
|
---|
| 61 | $H $INSTANCE "$cmd" "$name"
|
---|
| 62 | done
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | handleTelnetCommand() {
|
---|
| 66 | local cmd="$1"
|
---|
| 67 | local ip="$2"
|
---|
| 68 |
|
---|
| 69 | echo "$(timestamp): Telnet from \"$ip\" executed \"$cmd\"" >> $COMMANDLOG
|
---|
| 70 |
|
---|
| 71 | for H in $(getHooksFor telnetCommand); do
|
---|
| 72 | $H $INSTANCE "$cmd" "$ip"
|
---|
| 73 | done
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 |
|
---|
[67] | 77 | if [ ! -d "$(getInstancePath $INSTANCE)/logs" ]; then
|
---|
| 78 | mkdir "$(getInstancePath $INSTANCE)/logs"
|
---|
| 79 | fi
|
---|
| 80 |
|
---|
| 81 | setAllPlayersOffline
|
---|
| 82 |
|
---|
[17] | 83 | echo >> $LOG
|
---|
| 84 | echo >> $LOG
|
---|
| 85 | echo "Starting instance $INSTANCE at $(timestamp)" >> $LOG
|
---|
| 86 | echo >> $LOG
|
---|
| 87 |
|
---|
[219] | 88 | rm $(getInstancePath $INSTANCE)/logs/current_output_log.txt
|
---|
[87] | 89 | rm $(getInstancePath $INSTANCE)/logs/current_chat.log
|
---|
| 90 | rm $(getInstancePath $INSTANCE)/logs/current_commandExecution.log
|
---|
[219] | 91 | ln -s $LOG $(getInstancePath $INSTANCE)/logs/current_output_log.txt
|
---|
[87] | 92 | ln -s $CHATLOG $(getInstancePath $INSTANCE)/logs/current_chat.log
|
---|
| 93 | ln -s $COMMANDLOG $(getInstancePath $INSTANCE)/logs/current_commandExecution.log
|
---|
| 94 |
|
---|
[18] | 95 | sleep 5
|
---|
| 96 |
|
---|
[17] | 97 | NOBUF="stdbuf -e0 -o0"
|
---|
| 98 |
|
---|
[219] | 99 | $NOBUF tail -n 5000 -F $(getInstancePath $INSTANCE)/logs/$LOG |
|
---|
[17] | 100 | $NOBUF tr '\\' '/' |
|
---|
| 101 | $NOBUF tr -d '\r' |
|
---|
| 102 | $NOBUF grep -v "^(Filename: " |
|
---|
[219] | 103 | $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] | 104 | while read line ; do
|
---|
| 105 | if [ -n "$line" ]; then
|
---|
[87] | 106 | #Player connected, clientid=[0-9]*, entityid=[0-9]*, name=.*, steamid=[0-9]*, ip=[0-9.]*$
|
---|
| 107 | if [ -n "$(echo "$line" | grep '^Player connected,')" ]; then
|
---|
| 108 | clientId=$(expr "$line" : 'Player connected, clientid=\([0-9]*\), entityid=[0-9]*, name=.*, steamid=[0-9]*, ip=[0-9.]*$')
|
---|
| 109 | entityId=$(expr "$line" : 'Player connected, clientid=[0-9]*, entityid=\([0-9]*\), name=.*, steamid=[0-9]*, ip=[0-9.]*$')
|
---|
| 110 | playerName=$(expr "$line" : 'Player connected, clientid=[0-9]*, entityid=[0-9]*, name=\(.*\), steamid=[0-9]*, ip=[0-9.]*$')
|
---|
| 111 | steamId=$(expr "$line" : 'Player connected, clientid=[0-9]*, entityid=[0-9]*, name=.*, steamid=\([0-9]*\), ip=[0-9.]*$')
|
---|
| 112 | ip=$(expr "$line" : 'Player connected, clientid=[0-9]*, entityid=[0-9]*, name=.*, steamid=[0-9]*, ip=\([0-9.]*\)$')
|
---|
[17] | 113 | sleep 1
|
---|
[87] | 114 | handleConnect "$clientId" "$entityId" "$playerName" "$steamId" "$ip"
|
---|
| 115 | unset clientId entityId playerName steamId ip
|
---|
[62] | 116 | else
|
---|
[87] | 117 | #Removing player with id clientId=[0-9]*, entityId=[0-9]*$
|
---|
[17] | 118 | if [ -n "$(echo "$line" | grep '^Removing player with id ')" ]; then
|
---|
[57] | 119 | playerId=$(expr "$line" : 'Removing player with id clientId=\([0-9]*\), entityId=[0-9]*$')
|
---|
| 120 | entityId=$(expr "$line" : 'Removing player with id clientId=[0-9]*, entityId=\([0-9]*\)$')
|
---|
| 121 | handleDisconnect "$playerId" "$entityId"
|
---|
[17] | 122 | unset playerId entityId
|
---|
[62] | 123 | else
|
---|
[87] | 124 | #GMSG: .*$
|
---|
[62] | 125 | if [ -n "$(echo "$line" | grep -E '^GMSG: .+')" ]; then
|
---|
| 126 | msg=$(expr "$line" : 'GMSG: \(.*\)$')
|
---|
| 127 | handleChat "$msg"
|
---|
| 128 | unset msg
|
---|
[87] | 129 | else
|
---|
| 130 | #Executed command ".*" from player ".*"$
|
---|
| 131 | if [ -n "$(echo "$line" | grep '^Executed command ')" ]; then
|
---|
| 132 | cmd=$(expr "$line" : 'Executed command "\(.*\)" from player ".*"$')
|
---|
| 133 | nick=$(expr "$line" : 'Executed command ".*" from player "\(.*\)"$')
|
---|
| 134 | handleRemoteCommand "$cmd" "$nick"
|
---|
| 135 | unset cmd nick
|
---|
| 136 | else
|
---|
| 137 | #Telnet executed ".*" from: .*$
|
---|
| 138 | if [ -n "$(echo "$line" | grep '^Telnet executed ')" ]; then
|
---|
| 139 | cmd=$(expr "$line" : 'Telnet executed "\(.*\)" from: .*$')
|
---|
| 140 | ip=$(expr "$line" : 'Telnet executed ".*" from: \(.*\)$')
|
---|
| 141 | handleTelnetCommand "$cmd" "$ip"
|
---|
| 142 | unset cmd ip
|
---|
[17] | 143 | fi
|
---|
[62] | 144 | fi
|
---|
| 145 | fi
|
---|
[87] | 146 | fi
|
---|
| 147 | fi
|
---|
[17] | 148 | fi
|
---|
| 149 | done
|
---|