New "limit" config variable, and some module fixes.
parent
fb2cd452e9
commit
286d4a8497
25
bot.py
25
bot.py
|
@ -161,9 +161,9 @@ class Phenny(irc.Bot):
|
||||||
|
|
||||||
return PhennyWrapper(self)
|
return PhennyWrapper(self)
|
||||||
|
|
||||||
def input(self, origin, text, bytes, match, event):
|
def input(self, origin, text, bytes, match, event, args):
|
||||||
class CommandInput(unicode):
|
class CommandInput(unicode):
|
||||||
def __new__(cls, text, origin, bytes, match, event):
|
def __new__(cls, text, origin, bytes, match, event, args):
|
||||||
s = unicode.__new__(cls, text)
|
s = unicode.__new__(cls, text)
|
||||||
s.sender = origin.sender
|
s.sender = origin.sender
|
||||||
s.nick = origin.nick
|
s.nick = origin.nick
|
||||||
|
@ -172,19 +172,28 @@ class Phenny(irc.Bot):
|
||||||
s.match = match
|
s.match = match
|
||||||
s.group = match.group
|
s.group = match.group
|
||||||
s.groups = match.groups
|
s.groups = match.groups
|
||||||
|
s.args = args
|
||||||
s.admin = origin.nick in self.config.admins
|
s.admin = origin.nick in self.config.admins
|
||||||
s.owner = origin.nick == self.config.owner
|
s.owner = origin.nick == self.config.owner
|
||||||
return s
|
return s
|
||||||
|
|
||||||
return CommandInput(text, origin, bytes, match, event)
|
return CommandInput(text, origin, bytes, match, event, args)
|
||||||
|
|
||||||
def call(self, func, origin, phenny, input):
|
def call(self, func, origin, phenny, input):
|
||||||
try: func(phenny, input)
|
try: func(phenny, input)
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
self.error(origin)
|
self.error(origin)
|
||||||
|
|
||||||
|
def limit(self, origin, func):
|
||||||
|
if origin.sender and origin.sender.startswith('#'):
|
||||||
|
if hasattr(self.config, 'limit'):
|
||||||
|
limits = self.config.limit.get(origin.sender)
|
||||||
|
if limits and (func.__module__ not in limits):
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
def dispatch(self, origin, args):
|
def dispatch(self, origin, args):
|
||||||
bytes, event = args[0], args[1]
|
bytes, event, args = args[0], args[1], args[2:]
|
||||||
text = decode(bytes)
|
text = decode(bytes)
|
||||||
|
|
||||||
for priority in ('high', 'medium', 'low'):
|
for priority in ('high', 'medium', 'low'):
|
||||||
|
@ -195,12 +204,14 @@ class Phenny(irc.Bot):
|
||||||
|
|
||||||
match = regexp.match(text)
|
match = regexp.match(text)
|
||||||
if match:
|
if match:
|
||||||
|
if self.limit(origin, func): continue
|
||||||
|
|
||||||
phenny = self.wrapped(origin, text, match)
|
phenny = self.wrapped(origin, text, match)
|
||||||
input = self.input(origin, text, bytes, match, event)
|
input = self.input(origin, text, bytes, match, event, args)
|
||||||
|
|
||||||
if func.thread:
|
if func.thread:
|
||||||
args = (func, origin, phenny, input)
|
targs = (func, origin, phenny, input)
|
||||||
t = threading.Thread(target=self.call, args=args)
|
t = threading.Thread(target=self.call, args=targs)
|
||||||
t.start()
|
t.start()
|
||||||
else: self.call(func, origin, phenny, input)
|
else: self.call(func, origin, phenny, input)
|
||||||
|
|
||||||
|
|
|
@ -42,7 +42,7 @@ def msg(phenny, input):
|
||||||
if input.sender.startswith('#'): return
|
if input.sender.startswith('#'): return
|
||||||
if input.admin:
|
if input.admin:
|
||||||
phenny.msg(input.group(2), input.group(3))
|
phenny.msg(input.group(2), input.group(3))
|
||||||
msg.rule = (['msg'], r'(#\S+) (.*)')
|
msg.rule = (['msg'], r'(#?\S+) (.*)')
|
||||||
msg.priority = 'low'
|
msg.priority = 'low'
|
||||||
|
|
||||||
def me(phenny, input):
|
def me(phenny, input):
|
||||||
|
@ -51,7 +51,7 @@ def me(phenny, input):
|
||||||
if input.admin:
|
if input.admin:
|
||||||
msg = '\x01ACTION %s\x01' % input.group(3)
|
msg = '\x01ACTION %s\x01' % input.group(3)
|
||||||
phenny.msg(input.group(2), msg)
|
phenny.msg(input.group(2), msg)
|
||||||
me.rule = (['me'], r'(#\S+) (.*)')
|
me.rule = (['me'], r'(#?\S+) (.*)')
|
||||||
me.priority = 'low'
|
me.priority = 'low'
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
|
@ -21,7 +21,8 @@ r_whitespace = re.compile(r'[\t\r\n ]+')
|
||||||
abbrs = [
|
abbrs = [
|
||||||
'cf', 'lit', 'etc', 'Ger', 'Du', 'Skt', 'Rus', 'Eng', 'Amer.Eng', 'Sp',
|
'cf', 'lit', 'etc', 'Ger', 'Du', 'Skt', 'Rus', 'Eng', 'Amer.Eng', 'Sp',
|
||||||
'Fr', 'N', 'E', 'S', 'W', 'L', 'Gen', 'J.C', 'dial', 'Gk',
|
'Fr', 'N', 'E', 'S', 'W', 'L', 'Gen', 'J.C', 'dial', 'Gk',
|
||||||
'19c', '18c', '17c', '16c', 'St', 'Capt'
|
'19c', '18c', '17c', '16c', 'St', 'Capt', 'obs', 'Jan', 'Feb', 'Mar',
|
||||||
|
'Apr', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'
|
||||||
]
|
]
|
||||||
t_sentence = r'^.*?(?<!%s)(?:\.(?= [A-Z0-9]|\Z)|\Z)'
|
t_sentence = r'^.*?(?<!%s)(?:\.(?= [A-Z0-9]|\Z)|\Z)'
|
||||||
r_sentence = re.compile(t_sentence % ')(?<!'.join(abbrs))
|
r_sentence = re.compile(t_sentence % ')(?<!'.join(abbrs))
|
||||||
|
|
|
@ -21,7 +21,8 @@ def head(phenny, input):
|
||||||
else: uri, header = uri, None
|
else: uri, header = uri, None
|
||||||
|
|
||||||
if not uri and hasattr(phenny, 'last_seen_uri'):
|
if not uri and hasattr(phenny, 'last_seen_uri'):
|
||||||
uri = phenny.last_seen_uri
|
try: uri = phenny.last_seen_uri[input.sender]
|
||||||
|
except KeyError: return phenny.say('?')
|
||||||
|
|
||||||
try: info = web.head(uri)
|
try: info = web.head(uri)
|
||||||
except IOError: return phenny.say("Can't connect to %s" % uri)
|
except IOError: return phenny.say("Can't connect to %s" % uri)
|
||||||
|
@ -67,9 +68,9 @@ def f_title(self, origin, match, args):
|
||||||
uri = (uri or '').encode('utf-8')
|
uri = (uri or '').encode('utf-8')
|
||||||
|
|
||||||
if not uri and hasattr(self, 'last_seen_uri'):
|
if not uri and hasattr(self, 'last_seen_uri'):
|
||||||
uri = self.last_seen_uri.get('#swhack')
|
uri = self.last_seen_uri.get(origin.sender)
|
||||||
if not uri:
|
if not uri:
|
||||||
return phenny.msg(origin.sender, 'I need a URI to give the title of...')
|
return self.msg(origin.sender, 'I need a URI to give the title of...')
|
||||||
|
|
||||||
if not ':' in uri:
|
if not ':' in uri:
|
||||||
uri = 'http://' + uri
|
uri = 'http://' + uri
|
||||||
|
@ -145,7 +146,7 @@ def noteuri(phenny, input):
|
||||||
if not hasattr(phenny.bot, 'last_seen_uri'):
|
if not hasattr(phenny.bot, 'last_seen_uri'):
|
||||||
phenny.bot.last_seen_uri = {}
|
phenny.bot.last_seen_uri = {}
|
||||||
phenny.bot.last_seen_uri[input.sender] = uri
|
phenny.bot.last_seen_uri[input.sender] = uri
|
||||||
noteuri.rule = r'.*(http://[^<> "]+)[,.]?'
|
noteuri.rule = r'.*(http://[^<> "\x01]+)[,.]?'
|
||||||
noteuri.priority = 'low'
|
noteuri.priority = 'low'
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
|
@ -49,6 +49,7 @@ def stats(phenny, input):
|
||||||
ignore = set(['f_note', 'startup', 'message', 'noteuri'])
|
ignore = set(['f_note', 'startup', 'message', 'noteuri'])
|
||||||
for (name, user), count in phenny.stats.iteritems():
|
for (name, user), count in phenny.stats.iteritems():
|
||||||
if name in ignore: continue
|
if name in ignore: continue
|
||||||
|
if not user: continue
|
||||||
|
|
||||||
if not user.startswith('#'):
|
if not user.startswith('#'):
|
||||||
try: users[user] += count
|
try: users[user] += count
|
||||||
|
|
|
@ -65,7 +65,7 @@ def f_remind(phenny, input):
|
||||||
msg = msg.encode('utf-8')
|
msg = msg.encode('utf-8')
|
||||||
|
|
||||||
tellee_original = tellee.rstrip(',:;')
|
tellee_original = tellee.rstrip(',:;')
|
||||||
tellee = tellee.lower()
|
tellee = tellee_original.lower()
|
||||||
|
|
||||||
if not os.path.exists(phenny.tell_filename):
|
if not os.path.exists(phenny.tell_filename):
|
||||||
return
|
return
|
||||||
|
@ -90,7 +90,7 @@ def f_remind(phenny, input):
|
||||||
|
|
||||||
rand = random.random()
|
rand = random.random()
|
||||||
if rand > 0.9999: response = "yeah, yeah"
|
if rand > 0.9999: response = "yeah, yeah"
|
||||||
elif rand > 0.999: response = "%s: yeah, sure, whatever" % teller
|
elif rand > 0.999: response = "yeah, sure, whatever"
|
||||||
|
|
||||||
phenny.reply(response)
|
phenny.reply(response)
|
||||||
elif teller.lower() == tellee:
|
elif teller.lower() == tellee:
|
||||||
|
@ -126,10 +126,10 @@ def message(phenny, input):
|
||||||
reminders = []
|
reminders = []
|
||||||
remkeys = list(reversed(sorted(phenny.reminders.keys())))
|
remkeys = list(reversed(sorted(phenny.reminders.keys())))
|
||||||
for remkey in remkeys:
|
for remkey in remkeys:
|
||||||
if not remkey.endswith('*'):
|
if not remkey.endswith('*') or remkey.endswith(':'):
|
||||||
if tellee.lower() == remkey:
|
if tellee.lower() == remkey:
|
||||||
reminders.extend(getReminders(phenny, channel, remkey, tellee))
|
reminders.extend(getReminders(phenny, channel, remkey, tellee))
|
||||||
elif tellee.lower().startswith(remkey.rstrip('*')):
|
elif tellee.lower().startswith(remkey.rstrip('*:')):
|
||||||
reminders.extend(getReminders(phenny, channel, remkey, tellee))
|
reminders.extend(getReminders(phenny, channel, remkey, tellee))
|
||||||
|
|
||||||
for line in reminders[:maximum]:
|
for line in reminders[:maximum]:
|
||||||
|
|
|
@ -68,9 +68,10 @@ def translate(phrase, lang, target='en'):
|
||||||
|
|
||||||
def tr(phenny, input):
|
def tr(phenny, input):
|
||||||
"""Translates a phrase, with an optional language hint."""
|
"""Translates a phrase, with an optional language hint."""
|
||||||
input, output, phrase = input.groups()
|
original_input = input
|
||||||
|
input, output, phrase = original_input.groups()
|
||||||
phrase = phrase.encode('utf-8')
|
phrase = phrase.encode('utf-8')
|
||||||
if (len(phrase) > 350) and (not phenny.admin(input.nick)):
|
if (len(phrase) > 350) and (not original_input.admin):
|
||||||
return phenny.reply('Phrase must be under 350 characters.')
|
return phenny.reply('Phrase must be under 350 characters.')
|
||||||
|
|
||||||
input = input or guess_language(phrase)
|
input = input or guess_language(phrase)
|
||||||
|
|
|
@ -48,6 +48,10 @@ def search(term):
|
||||||
print e
|
print e
|
||||||
return term
|
return term
|
||||||
|
|
||||||
|
if isinstance(term, unicode):
|
||||||
|
term = term.encode('utf-8')
|
||||||
|
else: term = term.decode('utf-8')
|
||||||
|
|
||||||
term = term.replace('_', ' ')
|
term = term.replace('_', ' ')
|
||||||
try: uri = search.result('site:en.wikipedia.org %s' % term)
|
try: uri = search.result('site:en.wikipedia.org %s' % term)
|
||||||
except IndexError: return term
|
except IndexError: return term
|
||||||
|
@ -58,7 +62,12 @@ def search(term):
|
||||||
def wikipedia(term, last=False):
|
def wikipedia(term, last=False):
|
||||||
global wikiuri
|
global wikiuri
|
||||||
if not '%' in term:
|
if not '%' in term:
|
||||||
bytes = web.get(wikiuri % urllib.quote(term))
|
if isinstance(term, unicode):
|
||||||
|
t = term.encode('utf-8')
|
||||||
|
else: t = term
|
||||||
|
q = urllib.quote(t)
|
||||||
|
u = wikiuri % q
|
||||||
|
bytes = web.get(u)
|
||||||
else: bytes = web.get(wikiuri % term)
|
else: bytes = web.get(wikiuri % term)
|
||||||
bytes = r_tr.sub('', bytes)
|
bytes = r_tr.sub('', bytes)
|
||||||
|
|
||||||
|
|
2
phenny
2
phenny
|
@ -37,7 +37,7 @@ def create_default_config(fn):
|
||||||
exclude = ['admin']
|
exclude = ['admin']
|
||||||
|
|
||||||
# If you want to enumerate a list of modules rather than disabling
|
# If you want to enumerate a list of modules rather than disabling
|
||||||
# some, use "enable = ['example']", which takes precedent over disable
|
# some, use "enable = ['example']", which takes precedent over exclude
|
||||||
#
|
#
|
||||||
# enable = []
|
# enable = []
|
||||||
|
|
||||||
|
|
1
tools.py
1
tools.py
|
@ -18,6 +18,7 @@ def deprecated(old):
|
||||||
args = [input.bytes, input.sender, '@@']
|
args = [input.bytes, input.sender, '@@']
|
||||||
|
|
||||||
old(self, origin, match, args)
|
old(self, origin, match, args)
|
||||||
|
new.__module__ = old.__module__
|
||||||
new.__name__ = old.__name__
|
new.__name__ = old.__name__
|
||||||
return new
|
return new
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue