From 31f1e285c667c2204194ad6db20d343dc622affd Mon Sep 17 00:00:00 2001 From: urlbot Date: Fri, 28 Nov 2014 19:13:45 +0100 Subject: [PATCH] add URL blacklist; add command_show_blacklist() --- local_config.py.skel | 5 +++++ plugins.py | 21 ++++++++++++++++++++- urlbot.py | 12 ++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/local_config.py.skel b/local_config.py.skel index 77f57f1..ef74c76 100644 --- a/local_config.py.skel +++ b/local_config.py.skel @@ -26,6 +26,11 @@ config['path_event_files'] = 'event_files' config['path_cmdfifo'] = 'cmdfifo' config['persistent_storage'] = 'urlbot.persistent' +config['url_blacklist'] = [ + r'^.*heise\.de/newsticker/.*$', + r'^.*wikipedia\.org/wiki/.*$' +] + # the "dice" feature will use more efficient random data (0) for given users config['enhanced-random-user'] = ( 'FIXME', 'FIXME' ) diff --git a/plugins.py b/plugins.py index 9b4efaf..76515f4 100644 --- a/plugins.py +++ b/plugins.py @@ -419,6 +419,25 @@ def command_decode(args): 'msg': args['reply_user'] + ': usage: decode {single character}' } +def command_show_blacklist(args): + if 'register' == args: + return { + 'name': 'show-blacklist', + 'desc': 'show the current URL blacklist', + 'args': ('data', 'reply_user'), + 'ratelimit_class': RATE_GLOBAL + } + + if 'show-blacklist' in args['data']: + logger('plugin', 'sent URL blacklist') + + return { + 'msg': '\n'.join([ + args['reply_user'] + ': URL blacklist: ' + b + for b in conf('url_blacklist') + ]) + } + #def command_dummy(args): # if 'register' == args: # return { @@ -518,7 +537,7 @@ funcs['parse'] = (parse_mental_ill, parse_skynet, parse_debbug) funcs['command'] = ( command_command, command_help, command_version, command_unicode, command_klammer, command_source, command_dice, command_uptime, command_ping, - command_info, command_teatimer, command_decode + command_info, command_teatimer, command_decode, command_show_blacklist ) _dir = dir() diff --git a/urlbot.py b/urlbot.py index 37b077d..1b3d59c 100755 --- a/urlbot.py +++ b/urlbot.py @@ -140,6 +140,18 @@ def extract_url(data): if ratelimit_exceeded(): return False + flag = False + for b in conf('url_blacklist'): + if not None is re.match(b, url): + flag = True + message = 'url blacklist match for ' + url + logger('info', message) + chat_write(message) + + if flag: + # an URL has matched the blacklist, continue to the next URL + continue + # urllib.request is broken: # >>> '.'.encode('idna') # ....