add hook.regex -- regex search against private messages
This commit is contained in:
parent
4e0da10b37
commit
24a837c606
15
core/main.py
15
core/main.py
|
@ -106,14 +106,16 @@ def main(conn, out):
|
||||||
for func, args in bot.events[inp.command] + bot.events['*']:
|
for func, args in bot.events[inp.command] + bot.events['*']:
|
||||||
dispatch(Input(conn, *out), "event", func, args)
|
dispatch(Input(conn, *out), "event", func, args)
|
||||||
|
|
||||||
# COMMANDS
|
|
||||||
if inp.command == 'PRIVMSG':
|
if inp.command == 'PRIVMSG':
|
||||||
|
# COMMANDS
|
||||||
if inp.chan == inp.nick: # private message, no command prefix
|
if inp.chan == inp.nick: # private message, no command prefix
|
||||||
prefix = r'^(?:[.!]?|'
|
prefix = r'^(?:[.!]?|'
|
||||||
else:
|
else:
|
||||||
prefix = r'^(?:[.!]|'
|
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)
|
m = re.match(command_re, inp.lastparam)
|
||||||
|
|
||||||
|
@ -126,3 +128,12 @@ def main(conn, out):
|
||||||
|
|
||||||
func, args = bot.commands[command]
|
func, args = bot.commands[command]
|
||||||
dispatch(input, "command", func, args)
|
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)
|
||||||
|
|
|
@ -25,6 +25,10 @@ def format_plug(plug, kind='', lpad=0, width=40):
|
||||||
if kind == 'event':
|
if kind == 'event':
|
||||||
out += ' ' * (50 - len(out)) + ', '.join(plug[1]['events'])
|
out += ' ' * (50 - len(out)) + ', '.join(plug[1]['events'])
|
||||||
|
|
||||||
|
if kind == 'regex':
|
||||||
|
out += ' ' * (50 - len(out)) + plug[1]['regex']
|
||||||
|
|
||||||
|
|
||||||
return out
|
return out
|
||||||
|
|
||||||
|
|
||||||
|
@ -153,7 +157,7 @@ def reload(init=False):
|
||||||
for kind, plugs in sorted(bot.plugs.iteritems()):
|
for kind, plugs in sorted(bot.plugs.iteritems()):
|
||||||
if kind == 'command':
|
if kind == 'command':
|
||||||
continue
|
continue
|
||||||
print ' %s:' % type
|
print ' %s:' % kind
|
||||||
for plug in plugs:
|
for plug in plugs:
|
||||||
print format_plug(plug, kind=kind, lpad=6)
|
print format_plug(plug, kind=kind, lpad=6)
|
||||||
print
|
print
|
||||||
|
|
|
@ -62,11 +62,11 @@ def forget(inp, chan='', db=None):
|
||||||
return "I don't know about that."
|
return "I don't know about that."
|
||||||
|
|
||||||
|
|
||||||
@hook.event('PRIVMSG', hook=r'\?(.+)')
|
@hook.regex(r'\?(.+)')
|
||||||
def question(inp, chan='', say=None, db=None):
|
def question(inp, chan='', say=None, db=None):
|
||||||
"?<word> -- shows what data is associated with word"
|
"?<word> -- shows what data is associated with word"
|
||||||
db_init(db)
|
db_init(db)
|
||||||
|
|
||||||
data = get_memory(db, chan, inp)
|
data = get_memory(db, chan, inp.group(1))
|
||||||
if data:
|
if data:
|
||||||
say(data)
|
say(data)
|
||||||
|
|
|
@ -4,15 +4,13 @@ import urllib2
|
||||||
from util import hook
|
from util import hook
|
||||||
|
|
||||||
|
|
||||||
tinyurl_re = re.compile(r'http://(?:www\.)?tinyurl.com/([A-Za-z0-9\-]+)',
|
tinyurl_re = (r'http://(?:www\.)?tinyurl.com/([A-Za-z0-9\-]+)',
|
||||||
flags=re.IGNORECASE)
|
re.IGNORECASE)
|
||||||
|
|
||||||
|
|
||||||
@hook.event('PRIVMSG')
|
@hook.regex(*tinyurl_re)
|
||||||
def tinyurl(inp):
|
def tinyurl(match):
|
||||||
tumatch = tinyurl_re.search(inp)
|
|
||||||
if tumatch:
|
|
||||||
try:
|
try:
|
||||||
return urllib2.urlopen(tumatch.group()).url.strip()
|
return urllib2.urlopen(match.group()).url.strip()
|
||||||
except urllib2.URLError:
|
except urllib2.URLError:
|
||||||
pass
|
pass
|
||||||
|
|
|
@ -4,7 +4,7 @@ import time
|
||||||
|
|
||||||
from util import hook, urlnorm, timesince
|
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
|
expiration_period = 60 * 60 * 24 # 1 day
|
||||||
|
|
||||||
|
@ -67,15 +67,10 @@ def format_reply(history):
|
||||||
hour_span, nicklist(history), last)
|
hour_span, nicklist(history), last)
|
||||||
|
|
||||||
|
|
||||||
@hook.event('PRIVMSG')
|
@hook.regex(url_re)
|
||||||
def urlinput(inp, nick='', chan='', server='', reply=None, bot=None):
|
def urlinput(match, nick='', chan='', server='', reply=None, bot=None):
|
||||||
m = url_re.search(inp.encode('utf8'))
|
|
||||||
if not m:
|
|
||||||
return
|
|
||||||
|
|
||||||
# URL detected
|
|
||||||
db = db_connect(bot, server)
|
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:
|
if url not in ignored_urls:
|
||||||
history = get_history(db, chan, url)
|
history = get_history(db, chan, url)
|
||||||
insert_history(db, chan, url, nick)
|
insert_history(db, chan, url, nick)
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import inspect
|
import inspect
|
||||||
|
import re
|
||||||
|
|
||||||
|
|
||||||
def _hook_add(func, add, name=''):
|
def _hook_add(func, add, name=''):
|
||||||
|
@ -81,3 +82,19 @@ def event(arg=None, **kwargs):
|
||||||
def thread(func):
|
def thread(func):
|
||||||
func._thread = True
|
func._thread = True
|
||||||
return func
|
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
|
||||||
|
|
|
@ -10,7 +10,8 @@ from urllib import quote_plus
|
||||||
|
|
||||||
locale.setlocale(locale.LC_ALL, '')
|
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/'
|
base_url = 'http://gdata.youtube.com/feeds/api/'
|
||||||
url = base_url + 'videos/%s?v=2&alt=jsonc'
|
url = base_url + 'videos/%s?v=2&alt=jsonc'
|
||||||
|
@ -54,11 +55,9 @@ def get_video_description(vid_id):
|
||||||
return out
|
return out
|
||||||
|
|
||||||
|
|
||||||
@hook.event('PRIVMSG')
|
@hook.regex(*youtube_re)
|
||||||
def youtube_url(inp):
|
def youtube_url(match):
|
||||||
m = youtube_re.search(inp)
|
return get_video_description(match.group(1))
|
||||||
if m:
|
|
||||||
return get_video_description(m.group(1))
|
|
||||||
|
|
||||||
|
|
||||||
@hook.command('y')
|
@hook.command('y')
|
||||||
|
|
Loading…
Reference in New Issue