-
Notifications
You must be signed in to change notification settings - Fork 8
/
Events.py
686 lines (543 loc) · 18.7 KB
/
Events.py
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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
# pylint: disable=missing-function-docstring
"""Events generated by the TS3Connection class."""
import logging
import sys
import traceback
from enum import IntEnum, Enum
class EventType(Enum):
"""
Enum of event names used by TS3.
"""
CLIENT_ENTER = "notifycliententerview"
CLIENT_LEFT = "notifyclientleftview"
CLIENT_MOVED = "notifyclientmoved"
TEXT_MESSAGE = "notifytextmessage"
SERVER_EDITED = "notifyserveredited"
CHANNEL_DESC_CHANGED = "notifychanneldescriptionchanged"
CHANNEL_EDITED = "notifychanneledited"
CHANNEL_CREATED = "notifychannelcreated"
CHANNEL_DELETED = "notifychanneldeleted"
CHANNEL_MOVED = "notifychannelmoved"
CHANNEL_PASSWORD_CHANGED = "notifychannelpasswordchanged"
UNKNOWN = None
# Sadly, there seems to be no way to differentiate between the last two server events and the
# client events ...
server_events = [EventType.SERVER_EDITED, EventType.CLIENT_ENTER, EventType.CLIENT_LEFT]
text_events = [EventType.TEXT_MESSAGE]
channel_events = [
EventType.CLIENT_ENTER,
EventType.CLIENT_LEFT,
EventType.CLIENT_MOVED,
EventType.CHANNEL_DESC_CHANGED,
EventType.CHANNEL_EDITED,
EventType.CHANNEL_CREATED,
EventType.CHANNEL_MOVED,
EventType.CHANNEL_DELETED,
EventType.CHANNEL_PASSWORD_CHANGED,
]
class ReasonID(IntEnum):
"""
Enum of ReasonID given for events.
"""
SELF_JOINED = 0
MOVED = 1
TIMEOUT = 3
CHANNEL_KICK = 4
SERVER_KICK = 5
BAN = 6
LEFT = 8
EDITED = 10
SERVER_SHUTDOWN = 11
class TS3Event:
"""
Event class for Teamspeak 3 events. This is a stub for all other events.
"""
event_type = EventType.UNKNOWN
def __init__(self, data, event_type=None):
self._data = data
if event_type is not None:
self._event_type = event_type
self._logger = logging.getLogger(__name__)
@property
def data(self):
"""
Get event data.
"""
return self._data
def __getattr__(self, item):
"""
Allows access to not explicitly specified attributes in event.
:param item: Attribute name
:type item: str
:return: Attribute value string
:rtype: str
"""
return self._data[item]
class EventParser:
@staticmethod
def parse_event(event, event_type):
"""
Parse an event.
:param event: Event dictionary
:type event: dict
:param event_type: Event type to parse.
:type event_type: str
:return: Parsed Event
:rtype: TS3Event
"""
if EventType.TEXT_MESSAGE.value == event_type:
parsed_event = TextMessageEvent(event)
return parsed_event
if EventType.CLIENT_MOVED.value == event_type:
if "invokerid" in event:
parsed_event = ClientMovedEvent(event)
else:
parsed_event = ClientMovedSelfEvent(event)
return parsed_event
if EventType.CLIENT_ENTER.value == event_type:
parsed_event = ClientEnteredEvent(event)
return parsed_event
if EventType.CLIENT_LEFT.value == event_type:
reason_id = int(event.get("reasonid", "-1"))
if reason_id == int(ReasonID.SERVER_KICK) or reason_id == int(
ReasonID.CHANNEL_KICK
):
parsed_event = ClientKickedEvent(event)
elif reason_id == int(ReasonID.BAN):
parsed_event = ClientBannedEvent(event)
else:
parsed_event = ClientLeftEvent(event)
return parsed_event
if EventType.CHANNEL_DESC_CHANGED.value == event_type:
parsed_event = ChannelDescriptionEditedEvent(event)
return parsed_event
if EventType.CHANNEL_EDITED.value == event_type:
parsed_event = ChannelEditedEvent(event)
return parsed_event
if EventType.CHANNEL_CREATED.value == event_type:
parsed_event = ChannelCreatedEvent(event)
return parsed_event
if EventType.CHANNEL_DELETED.value == event_type:
parsed_event = ChannelDeletedEvent(event)
return parsed_event
if EventType.CHANNEL_MOVED.value == event_type:
parsed_event = ChannelMovedEvent(event)
return parsed_event
if EventType.SERVER_EDITED.value == event_type:
parsed_event = ServerEditedEvent(event)
return parsed_event
if EventType.CHANNEL_PASSWORD_CHANGED.value == event_type:
parsed_event = ChannelPasswordChangedEvent(event)
return parsed_event
logging.warning("Unknown event! %s", str(event_type))
return TS3Event(event, event_type)
class ServerEditedEvent(TS3Event):
"""
Event created when the server is edited.
"""
event_type = EventType.SERVER_EDITED
def __init__(self, data):
super().__init__(data)
self._reason_id = data.get("reasonid", "")
if "reasonid" in data:
del data["reasonid"]
self._invoker_id = data.get("invokerid", "-1")
if "invokerid" in data:
del data["invokerid"]
self._invoker_uid = data.get("invokeruid", "-1")
if "invokeruid" in data:
del data["invokeruid"]
self._invoker_name = data.get("invokername", "")
if "invokername" in data:
del data["invokername"]
self._changed_properties = data
@property
def invoker_id(self):
return self._invoker_id
@property
def invoker_name(self):
return self._invoker_name
@property
def invoker_uid(self):
return self._invoker_uid
@property
def reason_id(self):
return self._reason_id
@property
def changed_properties(self):
return self._changed_properties
class ChannelEditedEvent(TS3Event):
"""
Event created on channel edits.
"""
event_type = EventType.CHANNEL_EDITED
def __init__(self, data):
super().__init__(data)
self._channel_id = data.get("cid", "-1")
self._channel_topic = data.get("channel_topic", "")
self._invoker_id = data.get("invokerid", "-1")
self._invoker_name = data.get("invokername", "")
self._invoker_uid = data.get("invokeruid", "-1")
self._reason_id = data.get("reasonid", "-1")
@property
def channel_id(self):
return self._channel_id
@property
def channel_topic(self):
return self._channel_topic
@property
def invoker_id(self):
return self._invoker_id
@property
def invoker_name(self):
return self._invoker_name
@property
def invoker_uid(self):
return self._invoker_uid
@property
def reason_id(self):
return self._reason_id
class ChannelCreatedEvent(TS3Event):
"""
Event created on channel creation.
"""
event_type = EventType.CHANNEL_CREATED
def __init__(self, data):
super().__init__(data)
self._channel_id = data.get("cid", "-1")
self._channel_topic = data.get("channel_topic", "")
self._invoker_id = data.get("invokerid", "-1")
self._invoker_name = data.get("invokername", "")
self._invoker_uid = data.get("invokeruid", "-1")
self._reason_id = data.get("reasonid", "-1")
@property
def channel_id(self):
return self._channel_id
@property
def channel_topic(self):
return self._channel_topic
@property
def invoker_id(self):
return self._invoker_id
@property
def invoker_name(self):
return self._invoker_name
@property
def invoker_uid(self):
return self._invoker_uid
@property
def reason_id(self):
return self._reason_id
class ChannelDeletedEvent(TS3Event):
"""
Event created on channel deletion.
"""
event_type = EventType.CHANNEL_DELETED
def __init__(self, data):
super().__init__(data)
self._channel_id = data.get("cid", "-1")
self._invoker_id = data.get("invokerid", "-1")
self._invoker_name = data.get("invokername", "")
self._invoker_uid = data.get("invokeruid", "-1")
@property
def channel_id(self):
return self._channel_id
@property
def invoker_id(self):
return self._invoker_id
@property
def invoker_name(self):
return self._invoker_name
@property
def invoker_uid(self):
return self._invoker_uid
class ChannelMovedEvent(TS3Event):
"""
Event created when a channel is moved in the server hierarchy.
"""
event_type = EventType.CHANNEL_MOVED
def __init__(self, data):
super().__init__(data)
self._cid = data.get("cid", "-1")
self._cpid = data.get("cpid", "-1")
self._order = data.get("order", "-1")
self._reasonid = data.get("reasonid", "-1")
self._invoker_id = data.get("invokerid", "-1")
self._invoker_name = data.get("invokername", "")
self._invoker_uid = data.get("invokeruid", "-1")
@property
def channel_id(self):
return self._cid
@property
def channel_pid(self):
return self._cpid
@property
def channel_order(self):
return self._order
@property
def reason_id(self):
return self._reasonid
@property
def invoker_id(self):
return self._invoker_id
@property
def invoker_name(self):
return self._invoker_name
@property
def invoker_uid(self):
return self._invoker_uid
class ChannelDescriptionEditedEvent(TS3Event):
"""
Event created on channel description change.
"""
event_type = EventType.CHANNEL_DESC_CHANGED
def __init__(self, data):
super().__init__(data)
self._channel_id = int(data.get("cid", "-1"))
@property
def channel_id(self):
return self._channel_id
class ChannelPasswordChangedEvent(TS3Event):
"""
Event created on channel password change.
"""
event_type = EventType.CHANNEL_PASSWORD_CHANGED
def __init__(self, data):
super().__init__(data)
self._channel_id = int(data.get("cid", "-1"))
@property
def channel_id(self):
return self._channel_id
class ClientEnteredEvent(TS3Event):
"""
Event created when a client enters a channel.
"""
event_type = EventType.CLIENT_ENTER
def __init__(self, data):
super().__init__(data)
try:
self._client_id = int(data.get("clid", "-1"))
self._client_name = data.get("client_nickname", "")
self._client_uid = data.get("client_unique_identifier", "")
self._client_description = data.get("client_description", "")
self._client_country = data.get("client_country", "")
self._client_away = data.get("client_away", "")
self._client_away_msg = data.get("client_away_message", "")
self._client_input_muted = data.get("client_input_muted", "")
self._client_output_muted = data.get("client_output_muted", "")
self._client_outputonly_muted = data.get("client_outputonly_muted", "")
self._client_input_hardware = data.get("client_input_hardware", "")
self._client_output_hardware = data.get("client_output_hardware", "")
self._target_channel_id = int(data.get("ctid", "-1"))
self._from_channel_id = int(data.get("cfid", "-1"))
self._reason_id = int(data.get("reasonid", "-1"))
self._client_is_recording = data.get("client_is_recording", "")
self._client_dbid = data.get("client_database_id", "")
self._client_servergroups = data.get("client_servergroups", "")
self._client_channel_group_id = int(
data.get("client_channel_group_id", "-1")
)
except:
self._logger.error("Failed to parse ClientEnterEvent:")
self._logger.error(data)
self._logger.error("\n\n")
self._logger.error("Uncaught exception: %s", str(sys.exc_info()[0]))
self._logger.error(str(sys.exc_info()[1]))
self._logger.error(traceback.format_exc())
@property
def client_id(self):
return self._client_id
@property
def client_name(self):
return self._client_name
@property
def client_uid(self):
return self._client_uid
@property
def client_description(self):
return self._client_description
@property
def client_country(self):
return self._client_country
@property
def client_away(self):
return self._client_away
@property
def client_away_msg(self):
return self._client_away_msg
@property
def client_input_muted(self):
return self._client_input_muted
@property
def client_output_muted(self):
return self._client_output_muted
@property
def client_outputonly_muted(self):
return self._client_outputonly_muted
@property
def client_input_hardware(self):
return self._client_input_hardware
@property
def client_output_hardware(self):
return self._client_output_hardware
@property
def target_channel_id(self):
return self._target_channel_id
@property
def from_channel_id(self):
return self._from_channel_id
@property
def reason_id(self):
return self._reason_id
@property
def client_is_recording(self):
return self._client_is_recording
@property
def client_dbid(self):
return self._client_dbid
@property
def client_servergroups(self):
return self._client_servergroups
@property
def client_channel_group_id(self):
return self._client_channel_group_id
class ClientLeftEvent(TS3Event):
"""
Event created when a client leaves a channel.
"""
event_type = EventType.CLIENT_LEFT
def __init__(self, data):
super().__init__(data)
self._client_id = int(data.get("clid", "-1"))
self._target_channel_id = int(data.get("ctid", "-1"))
self._from_channel_id = int(data.get("cfid", "-1"))
self._reason_id = int(data.get("reasonid", "-1"))
self._reason_msg = data.get("reasonmsg", "")
@property
def client_id(self):
return self._client_id
@property
def target_channel_id(self):
return self._target_channel_id
@property
def reason_id(self):
return self._reason_id
@property
def reason_msg(self):
return self._reason_msg
class ClientKickedEvent(ClientLeftEvent):
"""
Event created when a client is kicked.
"""
def __init__(self, data):
super().__init__(data)
self._invoker_id = int(data.get("invokerid", "-1"))
self._invoker_name = data.get("invokername", "")
self._invoker_uid = data.get("invokeruid", "")
@property
def invoker_id(self):
return self._invoker_id
@property
def invoker_name(self):
return self._invoker_name
@property
def invoker_uid(self):
return self._invoker_uid
class ClientBannedEvent(ClientKickedEvent):
"""
Event created when a client is banned.
"""
def __init__(self, data):
super().__init__(data)
self._ban_time = int(data.get("bantime", "-1"))
@property
def ban_time(self):
return self._ban_time
class ClientMovedEvent(TS3Event):
"""
Event created when a client is moved from/to a channel.
"""
event_type = EventType.CLIENT_MOVED
def __init__(self, data):
super().__init__(data)
self._client_id = int(data.get("clid", "-1"))
self._target_channel_id = int(data.get("ctid", "-1"))
self._reason_id = int(data.get("reasonid", "-1"))
self._invoker_id = int(data.get("invokerid", "-1"))
self._invoker_name = data.get("invokername", "")
self._invoker_uid = data.get("invokeruid", "")
@property
def client_id(self):
return self._client_id
@property
def target_channel_id(self):
return self._target_channel_id
@property
def reason_id(self):
return self._reason_id
@property
def invoker_id(self):
return self._invoker_id
@property
def invoker_name(self):
return self._invoker_name
@property
def invoker_uid(self):
return self._invoker_uid
class ClientMovedSelfEvent(TS3Event):
"""
Event created when a client is moves themselves from/to a channel.
"""
event_type = EventType.CLIENT_MOVED
def __init__(self, data):
super().__init__(data)
self._client_id = int(data.get("clid", "-1"))
self._target_channel_id = int(data.get("ctid", "-1"))
self._reason_id = int(data.get("reasonid", "-1"))
@property
def client_id(self):
return self._client_id
@property
def target_channel_id(self):
return self._target_channel_id
@property
def reason_id(self):
return self._reason_id
class TextMessageEvent(TS3Event):
"""
Event created when a Text Message is received.
"""
event_type = EventType.TEXT_MESSAGE
def __init__(self, data):
super().__init__(data)
if data.get("targetmode") == "1":
self._targetmode = "Private"
self._target = data.get("target")
elif data.get("targetmode") == "2":
self._targetmode = "Channel"
elif data.get("targetmode") == "3":
self._targetmode = "Server"
self._message = data.get("msg")
self._invoker_id = int(data.get("invokerid", "-1"))
self._invoker_name = data.get("invokername", "")
self._invoker_uid = data.get("invokeruid", "-1")
@property
def invoker_id(self):
return self._invoker_id
@property
def invoker_name(self):
return self._invoker_name
@property
def invoker_uid(self):
return self._invoker_uid
@property
def message(self):
return self._message
@property
def targetmode(self):
return self._targetmode
@property
def target(self):
if self.targetmode == "Private":
return self._target
return None