add hook.regex -- regex search against private messages

This commit is contained in:
Ryan Hitchman 2010-03-11 17:19:36 -07:00
parent 4e0da10b37
commit 24a837c606
7 changed files with 54 additions and 30 deletions

View File

@ -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)

View File

@ -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

View File

@ -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):
"?<word> -- 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)

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -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')