-
Notifications
You must be signed in to change notification settings - Fork 2
/
chatMessage.go
515 lines (432 loc) · 14.2 KB
/
chatMessage.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
package TF2RconWrapper
import (
"errors"
"reflect"
"regexp"
"strconv"
"time"
)
const (
// "Username<userId><steamId><Team>"
// "1<2><3><4>" <- regex group
logLineStart = `^"(.*)<(\d+)><(\[U:1:\d+\])><(\w+)>" `
player = `"(.*)<(\d+)><(\[U:1:\d+\])><(\w+)>"`
// "5" <- regex group
logLineEnd = ` "(.*)"`
logLineStartSpec = `^"(.*)<(\d+)><(\[U:1:\d+\])><(\w*)>" `
)
// regexes used in the parser
var (
rPlayerGlobalMessage = regexp.MustCompile(logLineStart + `say` + logLineEnd)
rPlayerChangedClass = regexp.MustCompile(logLineStart + `changed role to "(scout|soldier|pyro|engineer|heavyweapons|demoman|sniper|medic|spy)"`)
rPlayerTeamMessage = regexp.MustCompile(logLineStart + `say_team` + logLineEnd)
rPlayerChangedTeam = regexp.MustCompile(logLineStart + `joined team` + logLineEnd)
rPlayerPickedUp = regexp.MustCompile(logLineStart + `picked up item "(\w+)"(?: \(healing "(\d+)"\))*`)
rPlayerSpawned = regexp.MustCompile(logLineStart + `spawned as "(\w+)"`)
rPlayerKilled = regexp.MustCompile(logLineStart + `killed ` + player + ` with "(\w+)"(?: \(customkill "(\w+)"\)){0,1} \(attacker_position (".+")\) \(victim_position (".+")\)`)
rPlayerDamage = regexp.MustCompile(logLineStart + `triggered "damage" against ` + player + ` \(damage "(\d+)"\)(?: \(realdamage "\d+"\)){0,1} \(weapon "(\w+)"\)(?: \((airshot) "1"\)){0,1}(?: \((headshot) "1"\)){0,1}`)
rPlayerHeal = regexp.MustCompile(logLineStart + `triggered "healed" against ` + player + ` \(healing "(\d+)"\)`)
rPlayerKilledMedic = regexp.MustCompile(logLineStart + `triggered "medic_death" against ` + player + ` \(healing "(\d+)"\) \(ubercharge "(\d+)"\)`)
rPlayerUberFinished = regexp.MustCompile(logLineStart + `triggered "empty_uber"`)
rPlayerBlockedCapture = regexp.MustCompile(logLineStart + `triggered "captureblocked" \(cp "(\d+)"\) \(cpname "(#\w+)"\) \(position "(.+)"\)`)
rPlayerConnected = regexp.MustCompile(logLineStartSpec + `connected, address "\d+.\d+.\d+.\d+\:\d+"`)
rPlayerDisconnected = regexp.MustCompile(logLineStartSpec + `disconnected \(reason "(.*)"\)`)
//Team events
rTeamPointCapture = regexp.MustCompile(`^Team "(Red|Blue)" triggered "pointcaptured" \(cp "(\d+)"\) \(cpname "(#\w+)"\)`)
rTeamScoreUpdate = regexp.MustCompile(`^Team "(Red|Blue)" current score "(\d+)" with "\d+" players`)
//World events
rGameOver = regexp.MustCompile(`^World triggered "Game_Over" reason "(.*)"`)
rRoundWin = regexp.MustCompile(`^World triggered "Round_Win" \(winner "(Red|Blue)"\)`)
rRoundStart = regexp.MustCompile(`^World triggered "Round_Start"`)
rServerCvar = regexp.MustCompile(`^server_cvar: "(.*)" "(.*)"`)
rTournamentStarted = regexp.MustCompile(`^Tournament mode started\nBlue Team: \w+\nRed Team: \w`)
rLogFiledClosed = regexp.MustCompile("^Log file closed.")
rRconCommand = regexp.MustCompile(`^rcon from "(.+)": command "(.+)"`)
)
const (
PlayerGlobalMessage = iota
PlayerTeamMessage
PlayerChangedClass
PlayerChangedTeam
PlayerPickedUpItem
PlayerSpawned
PlayerKilled
PlayerDamaged
PlayerHealed
PlayerKilledMedic
PlayerUberFinished
PlayerBlockedCapture
PlayerConnected
PlayerDisconnected
TeamPointCapture
TeamScoreUpdate
WorldGameOver
WorldRoundWin
WorldRoundStart
ServerCvar
TournamentStarted
LogFileClosed
RconCommand
)
//LogMessage represents a log message in a TF2 server, and contains a timestamp
//and a message. The message can be a player message that contains the sender's
//username, steamid and other info or a server message.
type LogMessage struct {
Timestamp time.Time
Message string
Parsed ParsedMsg
}
type TeamData struct {
CPData
Team string `json:"team"`
Score string `json:"score"`
}
type CPData struct {
CP string `json:"cp"`
CPName string `json:"cpname"`
}
type ItemPickup struct {
PlayerData PlayerData `json:"player"`
Item string `json:"item"`
Healing int `json:"healing"`
}
type PlayerTrigger struct {
//player1 triggered "Foo" against player2
Player1 PlayerData `json:"player1"`
Player2 PlayerData `json:"player2"`
}
type PlayerKill struct {
PlayerTrigger
Weapon string `json:"weapon"`
CustomKill string `json:"customkill"`
}
type PlayerDamage struct {
PlayerTrigger
Damage int `json:"damage"`
Weapon string `json:"weapon"`
Airshot bool `json:"airshot"`
Headshot bool `json:"headshot"`
}
type PlayerHeal struct {
PlayerTrigger
Healed int `json:"healed"` // health gained
}
//When a player says something in the global chat, or when they join the game
type PlayerData struct {
Username string `json:"username"`
SteamId string `json:"steamid"`
UserId string `json:"userid"`
Team string `json:"team"`
NewTeam string `json:"newteam"`
Text string `json:"text"`
Class string `json:"class"`
}
type CvarData struct {
Variable string
Value string
}
type ParsedMsg struct {
Type int
Data interface{}
}
var (
//ErrInvalidPacket represents an error when an invalid packet is found
ErrInvalidPacket = errors.New("invalid packet")
)
func (p *ParsedMsg) CallHandler(handler *EventListener) {
var eventhandler interface{}
var in []interface{}
switch p.Type {
case PlayerGlobalMessage:
d := p.Data.(PlayerData)
eventhandler = handler.PlayerGlobalMessage
in = []interface{}{d, d.Text}
case PlayerTeamMessage:
d := p.Data.(PlayerData)
eventhandler = handler.PlayerTeamMessage
in = []interface{}{d, d.Text}
case PlayerChangedClass:
d := p.Data.(PlayerData)
eventhandler = handler.PlayerClassChanged
in = []interface{}{d, d.Class}
case PlayerPickedUpItem:
eventhandler = handler.PlayerItemPickup
in = []interface{}{p.Data.(ItemPickup)}
case PlayerSpawned:
eventhandler = handler.PlayerSpawned
d := p.Data.(PlayerData)
in = []interface{}{d, d.Class}
case PlayerChangedTeam:
d := p.Data.(PlayerData)
in = []interface{}{d, d.NewTeam}
eventhandler = handler.PlayerTeamChange
case PlayerConnected:
in = []interface{}{p.Data.(PlayerData)}
eventhandler = handler.PlayerConnected
case PlayerKilled:
in = []interface{}{p.Data.(PlayerKill)}
eventhandler = handler.PlayerKilled
case PlayerDamaged:
in = []interface{}{p.Data.(PlayerDamage)}
eventhandler = handler.PlayerDamaged
case PlayerHealed:
in = []interface{}{p.Data.(PlayerHeal)}
eventhandler = handler.PlayerHealed
case PlayerKilledMedic:
in = []interface{}{p.Data.(PlayerTrigger)}
eventhandler = handler.PlayerKilledMedic
case PlayerUberFinished:
in = []interface{}{p.Data.(PlayerData)}
eventhandler = handler.PlayerUberFinished
case PlayerBlockedCapture:
arr := p.Data.([]interface{})
in = []interface{}{arr[0], arr[1]}
eventhandler = handler.PlayerBlockedCapture
case TeamPointCapture:
in = []interface{}{p.Data.(TeamData)}
eventhandler = handler.TeamPointCapture
case TeamScoreUpdate:
in = []interface{}{p.Data.(TeamData)}
eventhandler = handler.TeamScoreUpdate
case WorldRoundWin:
in = []interface{}{p.Data.(string)}
eventhandler = handler.WorldRoundWin
//case WorldRoundStart:
case PlayerDisconnected:
in = []interface{}{p.Data.(PlayerData)}
eventhandler = handler.PlayerDisconnected
case WorldGameOver:
eventhandler = handler.GameOver
case ServerCvar:
in = []interface{}{p.Data.(CvarData).Value, p.Data.(CvarData).Variable}
eventhandler = handler.CVarChange
case LogFileClosed:
eventhandler = handler.LogFileClosed
case TournamentStarted:
eventhandler = handler.TournamentStarted
case RconCommand:
arr := p.Data.([]string)
in = []interface{}{arr[0], arr[1]}
eventhandler = handler.RconCommand
default:
return
}
var args []reflect.Value
for _, arg := range in {
args = append(args, reflect.ValueOf(arg))
}
method := reflect.ValueOf(eventhandler)
if !method.IsValid() || method.IsNil() {
return
}
method.Call(args)
}
/**
Log Message layout:
First Four Bytes: 0xff, 0xff, 0xff, 0xff
Fifth Byte: 0x53 (ASCII code for 'S', denoting that the log message has a secret prepended)
Text between 'S' and 'L': log secret, can be of variable length.
Text after (and including L):
L 01/02/2006 - 15:04:05: <log message>
Second last byte: 0x0A (ASCII code for '\n', newline)
Last byte: 0x00 (ASCII code for '\0', null character)
*/
func getSecret(data []byte) (string, int, error) {
if len(data) <= 30 { //minimum length required
return "", 0, ErrInvalidPacket
}
if data[4] != 0x53 { // 0x53 == 'S'
return "", 0, errors.New("Server trying to send a chat packet without a secret")
}
content := data[5:]
Lpos := 6 //should be position of 'L' (0x4c) after the loop
for content[Lpos] != 0x4C {
Lpos++
if Lpos >= len(content) {
return "", 0, ErrInvalidPacket
}
}
secret := string(content[:Lpos])
if Lpos+1+len(" 03/09/2016 - 02:50:52:") >= len(data) {
//No message/time data
return "", 0, ErrInvalidPacket
}
//content[5 + Lpos:] is where from the actual log message starts
//==L 03/09/2016 - 02:50:52: <log message>\n\0
return secret, 5 + Lpos, nil
}
//TimeFormat is the reference time used by the server
//to represent time
const TimeFormat = "01/02/2006 - 15:04:05"
//ParseLogEntry parses a log entry of the format:
// L 03/09/2016 - 02:50:52: <log message>\n\0
//and returns a LogMessage object
func ParseLogEntry(line string) LogMessage {
timeStr := line[2:23]
message := line[25:]
timeObj, _ := time.Parse(TimeFormat, timeStr)
return LogMessage{timeObj, message, ParseLine(message)}
}
func getPlayerData(matches []string, from int, includeTeam bool) PlayerData {
d := PlayerData{
Username: matches[from+0],
UserId: matches[from+1],
SteamId: matches[from+2],
}
if includeTeam {
d.Team = matches[from+3]
}
return d
}
//ParseLine parses a log message (without the time entry)
func ParseLine(message string) ParsedMsg {
r := ParsedMsg{Type: -1}
switch {
case rPlayerKilled.MatchString(message):
m := rPlayerKilled.FindStringSubmatch(message)
kill := PlayerKill{
PlayerTrigger: PlayerTrigger{
Player1: getPlayerData(m, 1, true),
Player2: getPlayerData(m, 5, true),
},
}
kill.Weapon = m[9]
if len(m) > 10 {
kill.CustomKill = m[10]
}
r.Data = kill
r.Type = PlayerKilled
case rPlayerDamage.MatchString(message):
m := rPlayerDamage.FindStringSubmatch(message)
dmg, _ := strconv.Atoi(m[9])
damage := PlayerDamage{
PlayerTrigger: PlayerTrigger{
Player1: getPlayerData(m, 1, true), // ends at m[4]
Player2: getPlayerData(m, 5, true), // ends at m[8]
},
Damage: dmg,
Weapon: m[10],
Airshot: m[11] == "airshot",
Headshot: m[12] == "headshot",
}
r.Data = damage
r.Type = PlayerDamaged
case rPlayerHeal.MatchString(message):
m := rPlayerHeal.FindStringSubmatch(message)
healing, _ := strconv.Atoi(m[9])
heal := PlayerHeal{
PlayerTrigger: PlayerTrigger{
Player1: getPlayerData(m, 1, true), // end at m[4]
Player2: getPlayerData(m, 5, true), // ends at m[8]
},
Healed: healing,
}
r.Data = heal
r.Type = PlayerHealed
case rPlayerPickedUp.MatchString(message):
m := rPlayerPickedUp.FindStringSubmatch(message)
playerData := getPlayerData(m, 1, true)
pickup := ItemPickup{
PlayerData: playerData,
Item: m[5],
}
if len(m) == 7 {
pickup.Healing, _ = strconv.Atoi(m[6])
}
r.Data = pickup
r.Type = PlayerPickedUpItem
case rPlayerGlobalMessage.MatchString(message):
m := rPlayerGlobalMessage.FindStringSubmatch(message)
playerData := getPlayerData(m, 1, true)
playerData.Text = m[5]
r.Data = playerData
r.Type = PlayerGlobalMessage
case rPlayerTeamMessage.MatchString(message):
m := rPlayerTeamMessage.FindStringSubmatch(message)
playerData := getPlayerData(m, 1, true)
playerData.Text = m[5]
r.Data = playerData
r.Type = PlayerTeamMessage
case rPlayerChangedClass.MatchString(message):
m := rPlayerChangedClass.FindStringSubmatch(message)
playerData := getPlayerData(m, 1, true)
playerData.Class = m[5]
r.Data = playerData
r.Type = PlayerChangedClass
case rPlayerChangedTeam.MatchString(message):
m := rPlayerChangedTeam.FindStringSubmatch(message)
playerData := getPlayerData(m, 1, true)
playerData.NewTeam = m[5]
r.Data = playerData
r.Type = PlayerChangedTeam
case rPlayerSpawned.MatchString(message):
m := rPlayerSpawned.FindStringSubmatch(message)
playerData := getPlayerData(m, 1, true)
playerData.Class = m[5]
r.Data = playerData
r.Type = PlayerSpawned
case rPlayerKilledMedic.MatchString(message):
m := rPlayerKilledMedic.FindStringSubmatch(message)
r.Data = PlayerTrigger{
Player1: getPlayerData(m, 1, true),
Player2: getPlayerData(m, 5, true),
}
r.Type = PlayerKilledMedic
case rPlayerUberFinished.MatchString(message):
m := rPlayerUberFinished.FindStringSubmatch(message)
r.Data = getPlayerData(m, 1, true)
r.Type = PlayerUberFinished
case rPlayerBlockedCapture.MatchString(message):
m := rPlayerBlockedCapture.FindStringSubmatch(message)
r.Data = []interface{}{CPData{m[5], m[6]}, getPlayerData(m, 1, true)}
r.Type = PlayerBlockedCapture
case rPlayerConnected.MatchString(message):
m := rPlayerConnected.FindStringSubmatch(message)
playerData := getPlayerData(m, 1, false)
r.Data = playerData
r.Type = PlayerConnected
case rPlayerDisconnected.MatchString(message):
m := rPlayerDisconnected.FindStringSubmatch(message)
playerData := getPlayerData(m, 1, false)
r.Data = playerData
r.Type = PlayerDisconnected
// Non-Player Messages
case rGameOver.MatchString(message):
r.Type = WorldGameOver
case rRoundWin.MatchString(message):
m := rRoundWin.FindStringSubmatch(message)
r.Data = m[1]
r.Type = WorldRoundWin
case rServerCvar.MatchString(message):
m := rServerCvar.FindStringSubmatch(message)
r.Type = ServerCvar
r.Data = CvarData{Variable: m[1], Value: m[2]}
case rLogFiledClosed.MatchString(message):
r.Type = LogFileClosed
case rTournamentStarted.MatchString(message):
r.Type = TournamentStarted
case rTeamPointCapture.MatchString(message):
m := rTeamPointCapture.FindStringSubmatch(message)
team := TeamData{
Team: m[1],
CPData: CPData{
CP: m[2],
CPName: m[3],
}}
r.Data = team
r.Type = TeamPointCapture
case rTeamScoreUpdate.MatchString(message):
m := rTeamScoreUpdate.FindStringSubmatch(message)
team := TeamData{
Team: m[1],
Score: m[2],
}
r.Data = team
r.Type = TeamScoreUpdate
case rRconCommand.MatchString(message):
m := rRconCommand.FindStringSubmatch(message)
r.Data = []string{m[1], m[2]}
r.Type = RconCommand
}
return r
}