From d826e2ad83156e071c2924d2ea90d3b72f30a2b0 Mon Sep 17 00:00:00 2001 From: Chris Skalenda Date: Sat, 1 Jan 2011 03:01:54 +0800 Subject: [PATCH] Add a censored strings list feature. Censored strings are defined by a censored_strings array in the config file. An offending string will be replaced with [censored] in the bot's output. --- core/config.py | 16 ++++++++++++++-- core/irc.py | 11 ++++++++++- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/core/config.py b/core/config.py index abd064a..0649b50 100644 --- a/core/config.py +++ b/core/config.py @@ -8,7 +8,7 @@ def save(conf): if not os.path.exists('config'): open('config', 'w').write(inspect.cleandoc( - ''' + r''' { "connections": { @@ -21,7 +21,19 @@ if not os.path.exists('config'): }, "disabled_plugins": [], "disabled_commands": [], - "acls": {} + "acls": {}, + "censored_strings": + [ + "DCC SEND", + "1nj3ct", + "thewrestlinggame", + "startkeylogger", + "hybux", + "\\0", + "\\x01", + "!coz", + "!tell /x" + ] }''') + '\n') diff --git a/core/irc.py b/core/irc.py index 6f88631..a5f3567 100644 --- a/core/irc.py +++ b/core/irc.py @@ -16,6 +16,15 @@ def decode(txt): return txt.decode('utf-8', 'ignore') +def censor(text): + replacement = '[censored]' + if 'censored_strings' in bot.config: + words = map(re.escape, bot.config['censored_strings']) + regex = re.compile('(%s)' % "|".join(words)) + text = regex.sub(replacement, text) + return text + + class crlf_tcp(object): "Handles tcp connections that consist of utf-8 lines ending with crlf" @@ -181,7 +190,7 @@ class IRC(object): def cmd(self, command, params=None): if params: params[-1] = ':' + params[-1] - self.send(command + ' ' + ' '.join(params)) + self.send(command + ' ' + ' '.join(map(censor, params))) else: self.send(command)