-
Notifications
You must be signed in to change notification settings - Fork 2
/
dispatch.py
1223 lines (1101 loc) · 49.1 KB
/
dispatch.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
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import sys
from abc import ABC, abstractmethod
from functools import wraps # so decorators don't swallow docstrings
try:
import hexchat as hc
except ImportError:
hc = None
print("[WARN]: Hexchat module NOT found!")
try:
from playground.shared_resources import Case
except ImportError:
if hc is None:
raise FileNotFoundError("unable to locate playground.shared_resources")
elif hc.get_pluginpref("installDir") is None:
print('\0034[CONFIGURATION ERROR]: please set a installDir preference using the command'
' /installDir path/to/dispatch.py, functionality is next to zero until this is done!'
'\n if you think you are seeing this in error, check that the path you set it correct')
Case = None # that way it at least exists...
else:
try:
sys.path.insert(0, hc.get_pluginpref("installDir"))
from playground.shared_resources import Case
except ImportError:
print("\0034[FATAL]: installDir preference is NOT set correctly! fix me!")
Case = None
from tabulate import tabulate # for outputting pretty tables
# Globals
__module_name__ = "dispatch"
__module_version__ = "0.0.1"
__module_description__ = "Assist with automating trivial FuelRat dispatch interactions"
database = {}
# registered_commands = {} # slash commands
# registered_stage_commands = {} # stage commands
verbose_logging = False # if you want to see everything, it can be deafening.
# Debug constants
debug_constant_a = [':DrillSqueak[BOT][email protected]', 'PRIVMSG', '#DrillRats3', ":ClientName's", 'case', 'opened', 'with:', '"sol', 'pc"', '(Case', '4,', 'PC)']
debug_constant_B = [':DrillSqueak[BOT][email protected]', 'PRIVMSG', '#DrillRats3', ":Potato's", 'case', 'opened', 'with:', '"ki', 'ps"', '(Case', '9,', 'PS4)']
debug_constant_C = ['\x0329RatMama[BOT]', 'Incoming Client: Azrael Wolfmace - System: LP 673-13 - Platform: XB - O2: OK - Language: English (English-US) - IRC Nickname: Azrael_Wolfmace', '&']
pc_rsig_message = [':MechaSqueak[BOT][email protected]', 'PRIVMSG', '#fuelrats', ':RATSIGNAL', '-', 'CMDR', '\x02dan8630\x02', 'potato', '-', 'System:', '\x02Praea', 'Euq', 'PI-B', 'c11\x02', '(377.53', 'LY', 'from', 'Sol)', '-', 'Platform:', '\x02PC\x02', '-', 'O2:', 'OK', '-', 'Language:', 'English', '(en-US)', '(Case', '#4)']
ps_risg_message = [':MechaSqueak[BOT][email protected]', 'PRIVMSG', '#fuelrats', ':RATSIGNAL', '-', 'CMDR', '\x02Rawbird\x02', '-', 'System:',
'\x02OOCHOST', 'FD-N', 'A75-0\x02', '(not', 'in', 'EDDB)', '-', 'Platform:',
'\x02\x0312PS4\x03\x02', '-', 'O2:', 'NOT OK', '-', 'Language:', 'German', '(de-DE)', '(Case', '#2)']
xb_rsig_message = [':MechaSqueak[BOT][email protected]', 'PRIVMSG', '#fuelrats', ':RATSIGNAL', '-', 'CMDR', '\x02XX', 'SAM', 'JR', 'XX\x02', '-', 'System:', '\x02CRUCIS', 'SECTOR', 'BQ-P', 'A5-1\x02', '(24.01', 'LY', 'from', 'Fuelum)', '-', 'Platform:', '\x02\x0303XB\x03\x02', '-', 'O2:', 'OK', '-', 'Language:', 'English', '(en-US)', '-']
clear_msg = [':MechaSqueak[BOT][email protected]', 'PRIVMSG', '#fuelrats', ':Case', 'Potato', 'FrozenDemon', 'got', 'cleared!']
if hc is not None:
print("\0033=============\n\0033custom module dispatch.py loading!\n\0033* Author:theunkn0wn1\n\0034---------")
else:
print("dispatch.py loading\n author: Theunkn0wn1")
# Commands
class CommandBase(ABC):
"""
Abstract for defining stage commands
"""
registered_commands = {} # registry
name = "" # command name
alias = [] # command alias
# commands = {}
@classmethod
def _registerCommand(cls, func_instance) -> None:
"""
:param func_instance:
:return:
"""
new_entry = {func_instance.name:func_instance}
if hc is not None:
hc.hook_command(func_instance.name, func_instance.func)
if func_instance.alias: # type coercion, as long as its not empty nor None this is true
for val in func_instance.alias:
log("CommandBase._registerCommand", "registering hook for {}".format(val))
new_entry.update({val:func_instance})
if hc is not None:
hc.hook_command(val, func_instance.func)
cls.registered_commands.update(new_entry)
@classmethod
def getCommand(cls, name):
"""
Fetches command by name or alias
:param name: name/alias to lookup
:return: CommandBase Instance
"""
if isinstance(name, str):
if name in cls.registered_commands:
return cls.registered_commands[name]
else:
return None
else:
raise TypeError("name was of type {} with data {}".format(type(name), name))
@abstractmethod
def func(self, word, word_eol, userdata=None):
"""Command action"""
raise NotImplementedError("func method not defined, please do tell the developer they missed a spot.")
def __init__(self):
self._registerCommand(self)
class stageBase(CommandBase):
"""
varient of commandBase to register stageCommands
"""
registered_commands = {}
before = None
after = None
# Wrappers
def eat_all(wrapped_function):
""":returns hc.EAT_ALL at end of wrapped function"""
@wraps(wrapped_function) # prevents decorator from swallowing docstrings
def wrapper(arg,*args, **kwargs):
wrapped_function(arg, *args, **kwargs)
if hc is not None:
# print("returning {}".format(hc.EAT_ALL))
return hc.EAT_ALL
else:
return 3 # so i can test commands without hexchat being loaded,3 is the enum value
return wrapper
def log(trace, msg, verbose=False):
global verbose_logging
if verbose_logging:
print("[{Stack}:{trace}]\t{message}".format(Stack=__module_name__, message=msg, trace=trace))
elif verbose:
print("[{Stack}:{trace}]\t{message}".format(Stack=__module_name__, message=msg, trace=trace))
class Translations:
"""container for translations"""
class English:
"""Container for English facts"""
fr = {
'pre': "Please add the following rats to your friends list: {rats}",
'fact': "!{platform}fr-{lang} {client}"
}
wr = {
'pre': "Now, please invite your rats to the wing.",
'fact': "!{platform}wing {client}"
}
wb = {
'pre': "lastly, please blind us with your beacon!",
'fact': "!{platform}beacon {client}"
}
clear = {
'pass': "{client} please stick around with your rats for some useful tips!",
'fail':
{
'+': "{client} Im sorry we could not get you in time. please stick with your rats to learn some"
"useful tips as to avoid happening again",
'-': "{client}, Im sorry we could not get you in time, if you could please join us in"
"#debrief our rats can give you some great advise as to avoid this happening again."
"\n you can do so by typing '/join #debrief ' "
}
}
class Utilities:
@staticmethod
def play_beep():
"""PLay a sound"""
pass
@staticmethod
def strip_fancy(word, allowed_fancy=None):
"""
Strips non alphanumeric values from a string
:param word: word to strip
:param allowed_fancy: non-alpha numeric characters to preserve
:returns ret: sanitized string
"""
ret = ""
for char in word:
if char.isalpha() or char.isnumeric():
ret += char
elif allowed_fancy is not None and char in allowed_fancy:
ret += char
return ret
def on_message_received(*args):
phrase = args[0]
channel = phrase[2]
sender = phrase[0]
try:
if sender == ":MechaSqueak[BOT][email protected]" or sender == ':DrillSqueak[BOT][email protected]':
log("on_message_received", phrase)
parsed_data = Parser.parse(data=phrase)
if parsed_data is not None:
log("on_message_received", "parsed data is {}".format(parsed_data))
if isinstance(parsed_data, Case):
Tracker.append(data=parsed_data)
elif isinstance(parsed_data, str):
log("on_message_received", "case clear message\ndata is {}".format(parsed_data))
for key in database:
entry = database.get(key)
# entry:Case
if entry.client == parsed_data:
log("on_message_received", "found a matching case!")
database.pop(key)
break
# return True
log("on_message_received", "no matching client.")
# return False
# if phrase[3] == ':RATSIGNAL':
# log("on_message_received", "Rsig received.")
# output = Parser.parse_ratsignal(phrase)
# log("on_message_received", "capture data is {}".format(output))
# Tracker.append(data=output)
else:
pass
# log("on_message_received", 'not the expected user')
except Exception as e:
log("on_message_received", 'some error occurred! Error is as follows:\n{}'.format(e))
# return hc.EAT_PLUGIN
class Parser:
"""
Contains methods to parse incoming data. use .parse()
"""
@staticmethod
def parse_inject(capture):
"""Parses inject message events"""
log("parse_inject", "parsing capture event with data: {}".format(capture))
# [':DrillSqueak[BOT][email protected]', 'PRIVMSG', '#DrillRats3', ":ClientName's", 'case', 'opened',
# 'with:', '"sol', 'pc"', '(Case', '3,', 'PC)']
if capture is None:
log("parse_inject", "Invalid capture event")
elif capture[0] != ':DrillSqueak[BOT][email protected]' and capture[0] != ':MechaSqueak[BOT][email protected]':
log("parse_inject", " invalid capture event")
else:
log("parse_inject", 'Beginning parse attempt')
i = 0
# client = inject_args['client']
# platform = inject_args['set_system']
case = -1
log("step1", 'completed!')
client = platform = None # init before use
for phrase in capture:
if phrase == 'PC)' or phrase == 'PS4)' or phrase == 'XB)':
log('step2', 'searching for platform...')
platform = phrase.strip(')')
log('step2', 'platform is {}'.format(platform))
elif phrase[:5] == 'case':
log('step3', 'looking for client name...')
parsed_string = capture[i - 1] # phrase before is the client name
parsed_string = parsed_string[:len(parsed_string) - 2] # strip the 's from the end
parsed_string = parsed_string.strip(':') # and the : from the start
client = parsed_string # And we have our product!
log('step3', 'client is {}'.format(client))
elif phrase == "(Case":
log('step4', 'searching for CaseID...')
case = capture[i + 1].strip(',')
log('step4', 'cid is {CID}'.format(CID=case))
elif phrase.lower() == "with:":
system = capture[i + 1].strip('"')
else:
# log("failed:", "word not read: {}".format(phrase))
pass
i += 1
# log("parse_inject", "append({},{},{},{},{})".format(case, client, platform, False, 'En-us'))
return Case(client, case, platform=platform, system=system)
@staticmethod
def parse_ratsignal(phrase):
"""
parses ratsignal events
"""
i = 0
client = platform = lang = cr = cid = system = None # init before use.. prevent potential errors
for word in phrase:
cleaned_word = word
if cleaned_word == "CMDR":
z = i + 2
client = Utilities.strip_fancy(phrase[i + 1])
while phrase[z] != "-":
client += "_" # as IRC turns spaces into underscores
client += Utilities.strip_fancy(phrase[z])
z += 1
elif cleaned_word == 'Platform:':
platform = Utilities.strip_fancy(phrase[i + 1])
if platform == "12PS4" or platform == "03XB":
platform = platform[2:] # strip the colour code
elif cleaned_word == "Language:":
lang = Utilities.strip_fancy(phrase[i + 2]) # fetch the lang code
elif cleaned_word == "O2:":
log("parser:parse_ratsignal", "o2 is {}".format(phrase[i+1]))
cr = phrase[i + 1]
if cr == 'OK':
cr = False
else:
cr = True
elif cleaned_word == "(Case":
cid = phrase[i + 1].strip("#").strip(")")
elif cleaned_word == "System:":
z = i + 1
system = ""
while phrase[z] != "-":
if "(" in phrase[z] or "not" in phrase[z]: # checks if we have reached the distance from part of the system
break # and discard, since it is not part of the actual system name
else:
system += " "
system += Utilities.strip_fancy(phrase[z], allowed_fancy="-")
z += 1
# set_system.find()
i += 1
if cid is None:
cid = -1 # error handling, so the case can still be deleted
# return Case(client, cid, cr, platform, stage=0)
return Case(client=client, index=cid, cr=cr, platform=platform, system=system, language=lang, raw=phrase)
# return {'client': client, 'platform': platform, 'cr': cr, "case": cid, "lang": lang,
# 'set_system': set_system, 'stage': 0}
@staticmethod
def parse_clear(**kwargs):
# [':MechaSqueak[BOT][email protected]', 'PRIVMSG', '#fuelrats', ':Case', 'Slate_gorgon', 'got', 'cleared!']
data = kwargs['data']
is_valid = False
client = None
if data is not None or "":
i = 0
for word in data:
if word == ":Case":
log("parse_clear", "found client name")
client = data[i+1]
log("parse_clear", "client is {}".format(client))
elif word == "got" and data[i+1] == "cleared!":
is_valid = True
i += 1
return client if is_valid else None
@staticmethod
def parse_cr(**kwargs):
i = 0
client = None
# [':DrillSqueak[BOT][email protected]', 'PRIVMSG', '#DrillRats3', ':CODE', 'RED!', 'rowdy0452', 'is', 'on', 'emergency', 'oxygen.']
data = kwargs['data']
for word in data:
if Utilities.strip_fancy(word).lower() == 'code' and Utilities.strip_fancy(data[i+1]).lower() == "red":
client = data[i+2]
i += 1
case = Tracker.get_case(value=client)
# case: Case
case.toggle_cr()
return case
@staticmethod
def parse(**kwargs):
"""Attempts to parse incoming data,
:returns bool or Case object
"""
# [':MechaSqueak[BOT][email protected]', 'PRIVMSG', '#fuelrats', ':tonyg940:', 'To', 'add', 'th
data = kwargs['data']
event_type = data[3] # What kind of input
if event_type == ":RATSIGNAL":
return Parser.parse_ratsignal(data)
elif Utilities.strip_fancy(event_type).lower() == "case":
return Parser.parse_clear(data=data)
elif event_type[-2:] == "'s": # injected cases open with "{client}'s"
log("Parse.part", "event type = {}".format(event_type))
return Parser.parse_inject(data)
elif Utilities.strip_fancy(event_type).lower() == "code":
return Parser.parse_cr(data=data)
else:
log("Parser.parse", "Unknown phrase.")
return None
class Tracker:
"""Handles case storage and handling"""
def __init__(self):
global database
log("Tracker", "Initializing database...")
database = {}
@staticmethod
@eat_all
def readout(*args):
headers = ["#", "Client", "Platform", "cr", "set_system", "Assigned rats", "stage"]
data = []
# print("readout\t",database)
# log("readout", "- Index - | - client- | - platform - |- - - - - System - - - - -| - - - - Rats - - - -")
for key in database:
case = database.get(key)
# case: Case
assigned_rats = []
for rat in case.rats:
assigned_rats.append(rat)
data.append([key, case.client, case.platform, case.cr, case.system, assigned_rats, case.stage])
log("readout", tabulate(data, headers, "grid", missingval="<ERROR>"), True)
# print("readout", database)
@staticmethod
@eat_all
def debug_print(*args):
global database
for key in database:
data = database.get(key)
log("debug_print", "{key}:{data}".format(key=key, data=data.raw))
@staticmethod
def inject(list_arguments, from_capture=False, capture_data=None):
"""Generates a new case via !inject and via ratsignal text events"""
if list_arguments is None or len(list_arguments) < 3:
pass # not enough arguments
else:
try:
prefix = list_arguments[3]
if prefix.lower() == ":RATSIGNAL".lower():
log("inject", "ratsignal is present")
except Exception as e:
pass
# inject_args = {'client': list_arguments[1], 'set_system': list_arguments[3], 'platform': list_arguments[2]}
# order: client, platform, set_system
# hc.command("say !inject {} {} {}".format(inject_args['client'], inject_args['platform'],
# inject_args['set_system']))
pass
if from_capture:
if capture_data is None or capture_data == "":
log("Tracker", "[FAIL]\t capture_data was None or was blank")
else:
log('is_capture_check', 'PASSED')
try:
log("from_capture", "capture_data={}".format(capture_data))
if capture_data[3] == ":RATSIGNAL":
log("from_capture", "parse_ratsignal returned:")
Tracker.append(data=Parser.parse_ratsignal(capture_data))
else:
Tracker.append(data=Parser.parse_inject(capture_data))
except Exception as e:
log("[FATAL]", "an error occured as follows:\n {error}".format(error=e))
raise e
else:
log("Tracker", "not capture data")
@staticmethod
def append(**kwargs):
"""Appends a new entry to the db
expected: None,id,client,set_system,platform,cr,language
"""
log('toggle_verbose', "args =\t{}".format(kwargs))
data = kwargs['data']
if isinstance(data, Case): # Sanity check. Writing a non-class object would be... less than ideal
new_entry = {int(data.index): data}
log("append", "new entry is {}".format(new_entry))
database.update(new_entry)
log("append", "new entry created...")
return True
else:
log("append", "data is NOT of type Case!")
return False
@staticmethod
@eat_all
def rm(word, word_eol, user_data):
log("rm", "removing case with CID {}...".format(word[1]))
try:
cid = int(word[1])
if database.pop(cid, None) is None:
log("rm", "Failed to remove {cid}, no such case {cid}.".format(cid=cid), True)
log("rm", "raw dict is{}".format(database))
return False
else:
# print(database)
log("rm", "successfully removed case {}".format(cid), True)
return False
except Exception as e:
log("rm", "unable to remove case {}. An unknown error occurred.".format(cid), True)
log("rm", "Exception is: {}".format(e))
return False
@staticmethod
def get_case(**kwargs):
global database
value = kwargs['value']
log("get_case", "value is {val} of type {typ}".format(val=value, typ=type(value)))
for key in database:
obj = database.get(key)
# obj: Case
log("get_case", "value in obj = {}\n{obj}\nvalue={val}".format(value in obj, obj=obj, val=value))
if value in obj:
log("get_case", "match! returning {}".format(obj))
return obj
log("get_case", "no matching case for {}".format(value))
return None
# raise ValueError()
class Commands:
"""contains the Commands invoked via slash hooked during init"""
class SetInstallDirectory(CommandBase):
@eat_all
def set_install_dir(self, word, word_eol, userdata):
print(word_eol[0][1])
log("set_install_dir", "setting to {}".format(word_eol[0][1]), True)
hc.set_pluginpref("installDir", word_eol[0][1])
def func(self, *args):
self.set_install_dir(*args)
name = "setInstallDir"
alias = ['install', 'setup']
class NewCase(CommandBase):
"""
Generates a new case
"""
name = "new"
alias = ['create']
@eat_all
def func(self, word, word_eol, userdata=None):
"""
Create a new stub case
:param word: space delimenated args
:param word_eol:
:param userdata:
:return:
"""
client = None
system = None
platform = None
try:
index = int(word[1])
except IndexError:
log("new_case", "Not enough arguments. expected case number", True)
except TypeError:
log("new_case", "{} cannot be converted to a number, please give me a number.".format(word[1]), True)
else:
try:
client = word[2]
except IndexError:
log("new_case", "No further elements, assuming stub implementation...")
log("new_case", "generating stub case with index {}...".format(index), True)
Tracker.append(data=Case(index=index))
else:
log("new_case", "Got client name {}. Looking for platform next...".format(client))
try:
platform = word[3]
except IndexError:
log("new_case", "no platform data... generating stub with client name only...", True)
Tracker.append(data=Case(index=index, client=client))
else:
try:
system = word_eol[0][4]
except IndexError:
print(word_eol)
log("new_case", "no set_system data.. generating stub with client name and platform...")
Tracker.append(data=Case(index=index, client=client, platform=platform))
else:
log("new_case", "generating stub with client, platform, and set_system...", True)
Tracker.append(data=Case(index=index, client=client, system=system, platform=platform))
class CodeRed(CommandBase):
name = "codered"
alias = ['cr']
@eat_all
def func(self, word, word_eol, userdata=None):
try:
log("code_red", "word = {}".format(word), True)
index = int(word[1])
case = database.get(index)
except ValueError:
log("code_red", "(ve)Expected format: /cr case_number", True)
except IndexError:
log("code_red", "(ie)Expected format: /cr case_number", True)
else:
if case is None:
log("code_red", "case at index position {} does not exist.".format(index), True)
else:
# case: Case
log("code_red",
"case #{index} [{client}]'s CR status has been updated".format(index=index, client=case.client),
True)
case.Cr()
class SetClient(CommandBase):
name = "client"
@eat_all
def client(word, word_eol, userdata):
index = None # just in case the try itself fails before its assigned
try:
index = int(word[1])
case = Tracker.get_case(value=index)
# case : Case
case.Client(word_eol[0][2])
except ValueError:
log("client_name", "\0034 ERROR: {} is not a number!".format(word[1]), True)
except AttributeError:
log("client_name", "\0034 unable to find case {}".format(index))
except IndexError:
log("client", "\0033 Expected form: /client case_number client_irc_name", True)
def func(self, *args,**kwargs):
self.client(*args, **kwargs)
class SetSystem(CommandBase):
name = "set_system"
alias = ["sys"]
@eat_all
# self, word, word_eol, userdata
def func(self, word, word_eol, userdata=None):
try:
# log("set_system", word)
# log("set_system", word_eol)
print("word={}".format(word))
index = int(word[1])
log("set_system", "type of word_eol is {} with data {}".format(type(word_eol), word_eol[1]))
system = word_eol[2] # assuming anything after the case number is part of the set_system...
case = database.get(index)
# case: Case
case.System(system)
except IndexError:
log("set_system", "expected syntax: /sys case_number long-set_system-name-that-can-contain spaces", True)
except ValueError:
log("set_system", "case_number must be an integer, got {}".format(word[1]), True)
# def func(self, *args, **kwargs):
# self.set_system(*args, **kwargs)
class SetPlatform(CommandBase):
name = 'platform'
@eat_all
def func(self, word, word_eol, userdata=None):
"""
updates a client's case to a valid platform
:param word:
:param word_eol:
:param userdata:
:return:
"""
valid_platforms = ["pc", "xb", "ps"]
try:
index = int(word[1])
platform = word[2].lower() if word[2] is not None else ""
if platform not in valid_platforms:
log(
"platform", "{platform} is not recognized, valid options are {options}".format(
platform=platform, options=valid_platforms), True)
raise ValueError()
except IndexError:
log("platform", "Expected form is /platform case_number platform", True)
except ValueError:
log("platform", "invalid argument", True)
else:
case = database.get(index)
# case: Case
case.Platform(platform)
log("platform", "case #{id} ({client}'s) case got updated".format(id=index, client=case.client), True)
class SetVerbose(CommandBase):
name = 'verbose'
@eat_all
def func(self, *args):
"""
Toggles the verbose_logging field
Use this only if you are sure you can withstand the flood!
:param args:
:return:
"""
global verbose_logging
verbose_logging = not verbose_logging
log("toggle_verbose", "Toggling verbose logging to {}".format(verbose_logging), True)
@staticmethod
@eat_all
def run_tests(word, word_eol, userdata):
"""
THIS COMMAND IS DEPRICATED
Runs tests and generates some dummy cases
"""
raise DeprecationWarning("This command is depricated, don't use it. duh.")
log("run_tests", "Running Tracker.inject Test 1...")
Tracker.inject([None], True, None)
# log("run_tests", "running test 2")
# print(Tracker.inject([None, 'clientName', 'systemName', 'PC'], True, debug_constant_a))
log('run_tests', 'running test 3')
Tracker.inject([None, None, None], True, debug_constant_B)
# assert not Tracker.rm([None, 9], None, None) # if we can't remove the case - it didn't get formed as expected
log('run_tests', "Running pc rsig...")
Tracker.inject([None, None, None], True, pc_rsig_message)
# assert not Tracker.rm([None, 4], None, None)
log("run_tests", 'Running pc rsig via on_message_received...')
on_message_received(pc_rsig_message, None, None)
on_message_received(ps_risg_message)
log("run_tests", "running xb rsig via on_message_received")
on_message_received(xb_rsig_message)
log("run_tests", "testing clear...")
on_message_received(clear_msg)
log("run_tests", "done!")
class AddRats(CommandBase):
name = "add"
alias = ['go', 'assign']
@eat_all
def func(self, word, word_eol, userdata=None):
rat = []
try:
index = int(word[1])
case = database.get(index)
if case is None:
log("add_rats", "unable to find case with index {}".format(index))
return # No point continuing... the case is invalid
# mode = word[2]
# case:Case
for value in word[2:]: # taking any word after the index to be a rat
rat.append(value) # and adding it to the case
except IndexError:
log("add_rats", "not enough arguments", True) # not enough arguments
return
except ValueError:
raise # invalid input
except AttributeError:
raise
else:
if len(rat) is 0:
log("add_rats", "not enough arguments", True) # not enough arguments
elif len(rat) is 1:
log("add_rats", "adding single rat...")
case.Rats(rat[0], 'add') # just one
else:
log("add_rats", "addding add_rats...")
print(rat)
case.Rats(rat, 'add') # multiple, pass the list in
class RemoveRats(CommandBase):
name = "unassign"
alias = ['remove']
@eat_all
def func(self, word, word_eol, userdata=None):
rat = []
try:
index = int(word[1])
case = database.get(index)
if case is None:
log("add_rats", "unable to find case with index {}".format(index))
return # No point continuing... the case is invalid
# mode = word[2]
# case:Case
for value in word[2:]: # taking any word after the index to be a rat
rat.append(value) # and adding it to the case
except IndexError:
log("add_rats", "not enough arguments", True) # not enough arguments
return
except ValueError:
raise # invalid input
except AttributeError:
raise
else:
if len(rat) is 0:
log("add_rats", "not enough arguments", True) # not enough arguments
elif len(rat) is 1:
log("add_rats", "adding single rat...")
case.Rats(rat[0], 'remove') # just one
else:
log("add_rats", "addding add_rats...")
print(rat)
case.Rats(rat, 'remove') # multiple, pass the list in
class Board(CommandBase):
name = 'board'
alias = ['readout', 'list']
@eat_all
def func(self, word=None, word_eol=None, userdata=None):
Tracker.readout(None, None, None)
class ListCommands(CommandBase):
name = 'help'
alias = ['commands']
@eat_all
def func(self, word, word_eol=None,userdata=None):
print(self.registered_commands)
@staticmethod
@eat_all
def oxy_check(a, b, c):
name = "oxy_check"
log(name, "checking o2 for client:\t{cmdr}".format(cmdr=a[1]))
hc.command("say greetings " + a[1] + ",are you on emergency o2?(blue timer top right)")
@staticmethod
@eat_all
def oxy_ack(a, b, c):
name = "oxyAck"
log(name, "ackowledging OK o2")
commander = a[1]
hc.command("say {}, Understood, please let me know at once if it makes itself known ;)".format(commander))
@staticmethod
@eat_all
def print_test(a, b, c):
print("a=\t{}".format(a))
print("b=\t{}".format(b))
hc.command("msg TNTom[PC] " + a[1])
@staticmethod
@eat_all
def inject_case(x, y, z):
name = "injectCase"
log(name, "Injecting case")
subject = x[1]
sys = x[2]
platform = x[3]
hc.command("say !inject {} {} {}".format(subject, sys, platform))
@staticmethod
@eat_all
def go(x, y, z):
name = "go"
case = x[1]
rat = x[2]
client = x[3]
log(name, "Giving {} the go signal for case {}".format(rat, case))
hc.command("say {} please add the following rat to your friends list".format(client))
hc.command("say !go {cid} {rat}".format(cid=case, rat=rat))
# time.sleep()
hc.command("say !pcfr {}".format(client))
@staticmethod
@eat_all
def clear(a, b, c):
""" Reminds client to remain with their rats for the DB, then clears the case with Mecha
expected order of a: id,rat,client
"""
name = "clear"
entry = database.get(a[1])
commander = entry['client']
case = a[1]
rat = a[2]
log(name, "Commander= \"{x}\",Case \"{y}\",rat \"{z}\"".format(x=commander, y=case, z=rat))
hc.command("say {cmdr} ,please stick with your rat(s) for a moment to learn some useful tips! ;)".format(
cmdr=commander))
hc.command("say !clear {case} {rat}".format(case=case, rat=rat).format(case=case, rat=rat))
@staticmethod
@eat_all
def clear_drill(a, b, c):
name = "clear"
commander = a[2]
case = a[1]
log(name, "Commander= \"{x}\",Case \"{y}\"".format(x=commander, y=case))
hc.command("say {cmdr} ,please stick with your rat(s) for a moment to learn some useful tips! ;)".format(
cmdr=commander))
hc.command("say !clear {case}".format(case=case))
# ['\x0324Helitony[XB]', 'Comms+ for EldestDrifter']
# target element is the [1] element, will have to parse the hard way. Need that string first
@staticmethod
@eat_all
def print_hook(x, y, z):
log("print_hook", hc.strip(x[1]))
print(x)
print(y)
@staticmethod
@eat_all
def server_hook(x, y, z):
log("server_hook", x)
@staticmethod
@eat_all
def generic_msg(x, y, z):
log("generic_msg", x)
@staticmethod
@eat_all
def change_index(word, word_eol, event_args):
key = word[1]
log("change_index", " key is {}".format(key))
new_key = word[2]
case = database.pop(int(key))
case.index = int(new_key)
Tracker.append(data=case, index=new_key)
@staticmethod
@eat_all
def stage(x, y, z):
# todo make invalid argument count not break things
event_args = []
print("len(x) = {}".format(len(x)))
print("x = {}".format(x))
if len(x) < 1:
log('stage', 'expected format /stage {index} {mode} {param}')
return
# elif len(x) == 3: # no extra arguments
# pass
else:
mode = x[2]
log("stage", "mode = {} and is of type {}".format(mode, type(mode)))
cid = int(x[1])
if len(x)>3: # extra arguments
for val in x[2:]:
event_args.append(val)
else: # no extra arguments
event_args = [None]*3 # prevent index out-of-bounds error
log("stage", "=======================")
log("stage event_args=", event_args)
if StageManager.do_stage(cid, mode, alpha=event_args[0], beta=event_args[1], gamma=event_args[2]):
pass
else:
log("stage", 'unknown mode {}'.format(mode))
# log("stage", 'current stage is {stage}'.format())
class StageManager:
"""Tracks client stage and responds accordingly"""
class Say(stageBase):
"""
Print a message to the channel, with optional colour
"""
name = 'say'
alias = []
@eat_all
def func(self,*args, **kwargs):
"""Output a message into the channel (*this is server-side!*)"""
if hc is not None:
if kwargs['colour'] is None:
hc.command("say {msg}".format(msg=kwargs['message']))
else:
hc.command("say \003{color} {msg}".format(color=kwargs['colour'], msg=kwargs['message']))
else:
print("say {msg}".format(msg=kwargs['message']))
@staticmethod
def change_platform(key, platform, case_object):
"""Changes a client's platform
:param key: database key for client
:param platform: new platform
:param case_object: clients case object
"""
global database
if platform == 'ps' or platform == 'pc' or platform == 'xb':
# formed_dict: Case # technicially this isn't used, but will need to be removed before executing!
formed_dict = case_object # since hex forces usage of 3.5 rather than 3.6...
formed_dict.platform = platform
log('change_platform', "Successfully updated platform for case {}".format(key),True)
@staticmethod
def friend_request(case_object):
"""Tells client to add rat(s) to friends list"""
# case_object: Case # todo remove this line
platform = case_object.platform
client = case_object.client
# StageManager.say(Translations.English.fr['pre'].format(rats=case_object['rats'])) # TODO make work
log("friend_request", "triggered!", True)
StageManager.go(case_object, None)
if case_object.language is None or case_object.language.lower() == 'en':
StageManager.say(Translations.English.fr['fact'].format(
client=client,
platform=platform,
rats=case_object.rats,
language="en" if case_object.language is None else case_object.language))# otherwise we get None's in output (BAD!)
log("friend_request", "Client {client} is on platform {platform} with lang {lang}"
.format(client=client, platform=platform, lang=case_object.language), True)
# TODO: implement other languages, add option to outsource facts to Mecha
@staticmethod
def wing_invite(case_object):
# case_object: Case # todo rm this line
if case_object['language'] == 'English-us':
StageManager.say(Translations.English.wr['pre'], '03')
StageManager.say(Translations.English.wr['fact'].format(client=case_object.client, platform=
case_object.platform), '03')
# TODO: implement other languages,
# TODO: implement Mecha facts