From 24a837c606fd536820a8255c733ca0f51f16a44c Mon Sep 17 00:00:00 2001 From: Ryan Hitchman Date: Thu, 11 Mar 2010 17:19:36 -0700 Subject: [PATCH] add hook.regex -- regex search against private messages --- core/main.py | 15 +++++++++++++-- core/reload.py | 6 +++++- plugins/remember.py | 4 ++-- plugins/tinyurl.py | 18 ++++++++---------- plugins/urlhistory.py | 13 ++++--------- plugins/util/hook.py | 17 +++++++++++++++++ plugins/youtube.py | 11 +++++------ 7 files changed, 54 insertions(+), 30 deletions(-) diff --git a/core/main.py b/core/main.py index 361fdb1..ba0bcfc 100644 --- a/core/main.py +++ b/core/main.py @@ -106,14 +106,16 @@ def main(conn, out): for func, args in bot.events[inp.command] + bot.events['*']: dispatch(Input(conn, *out), "event", func, args) - # COMMANDS + if inp.command == 'PRIVMSG': + # COMMANDS if inp.chan == inp.nick: # private message, no command prefix prefix = r'^(?:[.!]?|' else: prefix = r'^(?:[.!]|' - command_re = prefix + inp.conn.nick + r'[:,]*\s+)(\w+)\s+(.*)$' + command_re = prefix + inp.conn.nick + command_re += r'[:,]*\s+)(\w+)(?:$|\s+)(.*)' m = re.match(command_re, inp.lastparam) @@ -126,3 +128,12 @@ def main(conn, out): func, args = bot.commands[command] dispatch(input, "command", func, args) + + # REGEXES + for func, args in bot.plugs['regex']: + m = args['re'].search(inp.lastparam) + if m: + input = Input(conn, *out) + input.inp = m + + dispatch(input, "regex", func, args) diff --git a/core/reload.py b/core/reload.py index d6910b2..6a88ef7 100644 --- a/core/reload.py +++ b/core/reload.py @@ -25,6 +25,10 @@ def format_plug(plug, kind='', lpad=0, width=40): if kind == 'event': out += ' ' * (50 - len(out)) + ', '.join(plug[1]['events']) + if kind == 'regex': + out += ' ' * (50 - len(out)) + plug[1]['regex'] + + return out @@ -153,7 +157,7 @@ def reload(init=False): for kind, plugs in sorted(bot.plugs.iteritems()): if kind == 'command': continue - print ' %s:' % type + print ' %s:' % kind for plug in plugs: print format_plug(plug, kind=kind, lpad=6) print diff --git a/plugins/remember.py b/plugins/remember.py index 8f449bb..0305c51 100644 --- a/plugins/remember.py +++ b/plugins/remember.py @@ -62,11 +62,11 @@ def forget(inp, chan='', db=None): return "I don't know about that." -@hook.event('PRIVMSG', hook=r'\?(.+)') +@hook.regex(r'\?(.+)') def question(inp, chan='', say=None, db=None): "? -- shows what data is associated with word" db_init(db) - data = get_memory(db, chan, inp) + data = get_memory(db, chan, inp.group(1)) if data: say(data) diff --git a/plugins/tinyurl.py b/plugins/tinyurl.py index 379467a..9285451 100644 --- a/plugins/tinyurl.py +++ b/plugins/tinyurl.py @@ -4,15 +4,13 @@ import urllib2 from util import hook -tinyurl_re = re.compile(r'http://(?:www\.)?tinyurl.com/([A-Za-z0-9\-]+)', - flags=re.IGNORECASE) +tinyurl_re = (r'http://(?:www\.)?tinyurl.com/([A-Za-z0-9\-]+)', + re.IGNORECASE) -@hook.event('PRIVMSG') -def tinyurl(inp): - tumatch = tinyurl_re.search(inp) - if tumatch: - try: - return urllib2.urlopen(tumatch.group()).url.strip() - except urllib2.URLError: - pass +@hook.regex(*tinyurl_re) +def tinyurl(match): + try: + return urllib2.urlopen(match.group()).url.strip() + except urllib2.URLError: + pass diff --git a/plugins/urlhistory.py b/plugins/urlhistory.py index 47d4680..e6eaadf 100644 --- a/plugins/urlhistory.py +++ b/plugins/urlhistory.py @@ -4,7 +4,7 @@ import time from util import hook, urlnorm, timesince -url_re = re.compile(r'([a-zA-Z]+://|www\.)[^ ]*') +url_re = r'([a-zA-Z]+://|www\.)[^ ]*' expiration_period = 60 * 60 * 24 # 1 day @@ -67,15 +67,10 @@ def format_reply(history): hour_span, nicklist(history), last) -@hook.event('PRIVMSG') -def urlinput(inp, nick='', chan='', server='', reply=None, bot=None): - m = url_re.search(inp.encode('utf8')) - if not m: - return - - # URL detected +@hook.regex(url_re) +def urlinput(match, nick='', chan='', server='', reply=None, bot=None): db = db_connect(bot, server) - url = urlnorm.normalize(m.group(0)) + url = urlnorm.normalize(match.group().encode('utf-8')) if url not in ignored_urls: history = get_history(db, chan, url) insert_history(db, chan, url, nick) diff --git a/plugins/util/hook.py b/plugins/util/hook.py index 90b3c2f..4baeb1f 100644 --- a/plugins/util/hook.py +++ b/plugins/util/hook.py @@ -1,4 +1,5 @@ import inspect +import re def _hook_add(func, add, name=''): @@ -81,3 +82,19 @@ def event(arg=None, **kwargs): def thread(func): func._thread = True return func + + +def regex(regex, flags=0, **kwargs): + args = kwargs + + def regex_wrapper(func): + args['name'] = func.func_name + args['regex'] = regex + args['re'] = re.compile(regex, flags) + _hook_add(func, ['regex', (func, args)], 'regex') + return func + + if inspect.isfunction(regex): + raise ValueError("regex decorators require a regex to match against") + else: + return regex_wrapper diff --git a/plugins/youtube.py b/plugins/youtube.py index 5aa83e5..bb80955 100644 --- a/plugins/youtube.py +++ b/plugins/youtube.py @@ -10,7 +10,8 @@ from urllib import quote_plus locale.setlocale(locale.LC_ALL, '') -youtube_re = re.compile(r'(?:youtube.*?(?:v=|/v/)|youtu\.be/|yooouuutuuube.*?id=)([-_a-z0-9]+)', flags=re.I) +youtube_re = (r'(?:youtube.*?(?:v=|/v/)|youtu\.be/|yooouuutuuube.*?id=)' + '([-_a-z0-9]+)', re.I) base_url = 'http://gdata.youtube.com/feeds/api/' url = base_url + 'videos/%s?v=2&alt=jsonc' @@ -54,11 +55,9 @@ def get_video_description(vid_id): return out -@hook.event('PRIVMSG') -def youtube_url(inp): - m = youtube_re.search(inp) - if m: - return get_video_description(m.group(1)) +@hook.regex(*youtube_re) +def youtube_url(match): + return get_video_description(match.group(1)) @hook.command('y')