This repository has been archived by the owner on Aug 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbrain.py
61 lines (52 loc) · 2.22 KB
/
brain.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
import re
import json
from job import JobQueue
class Brain(object):
"""Respond to incoming commands"""
def __init__(self, config, sink=None):
self.config = config
self.sink = sink
if "irc" in self.config:
if "ownermail" in self.config["irc"]:
self.config["irc"]["ownermail"] = "[email protected]"
if "regverify" in self.config["irc"]:
self.config["irc"]["regverify"] = "*******"
self.source_url = self.config["source_url"]
def say(self, message, force=False):
if not force and 'mute' in self.config and int(self.config['mute']):
print("muffling msg: " + message)
return
self.sink.say(self.sink.channel, message)
def respond(self, user, message):
if re.search(r'\bhelp\b', message):
self.say("If I only had a brain: %s -- Commands: help config kill last" % (self.source_url, ))
#elif re.search(r'\bconfig\b', message):
# match = re.search(r'\b(?P<name>[^=\s]+)\s*=\s*(?P<value>\S+)', message)
# if match:
# self.config[match.group('name')] = match.group('value')
#
# dump = self.config
# dump['jobs'] = JobQueue.describe()
# dumpstr = json.dumps(self.redact(dump))
# self.say("Configuration: [{dump}]".format(dump=dumpstr), force=True)
#elif re.search(r'\bkill\b', message):
# self.say("Squeal! Killed by %s" % (user, ))
# JobQueue.killall()
# #self.quit()
# #self.factory.stopTrying()
elif re.search(r'\blast\b', message):
if self.sink.timestamp:
self.say("My last post was %s UTC" % (self.sink.timestamp, ))
else:
self.say("No activity.")
else:
print "Failed to handle incoming command: %s said %s" % (user, message)
def redact(self, data):
FORBIDDEN_KEY_PATTERN = r'pw|pass|password'
BLACKOUT = "p***word"
for key, value in data.items():
if re.match(FORBIDDEN_KEY_PATTERN, key):
data[key] = BLACKOUT
elif hasattr(value, 'items'):
data[key] = self.redact(value)
return data