New "limit" config variable, and some module fixes.
parent
fb2cd452e9
commit
286d4a8497
27
bot.py
27
bot.py
|
@ -161,9 +161,9 @@ class Phenny(irc.Bot):
|
|||
|
||||
return PhennyWrapper(self)
|
||||
|
||||
def input(self, origin, text, bytes, match, event):
|
||||
def input(self, origin, text, bytes, match, event, args):
|
||||
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.sender = origin.sender
|
||||
s.nick = origin.nick
|
||||
|
@ -172,19 +172,28 @@ class Phenny(irc.Bot):
|
|||
s.match = match
|
||||
s.group = match.group
|
||||
s.groups = match.groups
|
||||
s.args = args
|
||||
s.admin = origin.nick in self.config.admins
|
||||
s.owner = origin.nick == self.config.owner
|
||||
return s
|
||||
|
||||
return CommandInput(text, origin, bytes, match, event)
|
||||
return CommandInput(text, origin, bytes, match, event, args)
|
||||
|
||||
def call(self, func, origin, phenny, input):
|
||||
try: func(phenny, input)
|
||||
except Exception, e:
|
||||
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):
|
||||
bytes, event = args[0], args[1]
|
||||
bytes, event, args = args[0], args[1], args[2:]
|
||||
text = decode(bytes)
|
||||
|
||||
for priority in ('high', 'medium', 'low'):
|
||||
|
@ -192,15 +201,17 @@ class Phenny(irc.Bot):
|
|||
for regexp, funcs in items:
|
||||
for func in funcs:
|
||||
if event != func.event: continue
|
||||
|
||||
|
||||
match = regexp.match(text)
|
||||
if match:
|
||||
if self.limit(origin, func): continue
|
||||
|
||||
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:
|
||||
args = (func, origin, phenny, input)
|
||||
t = threading.Thread(target=self.call, args=args)
|
||||
targs = (func, origin, phenny, input)
|
||||
t = threading.Thread(target=self.call, args=targs)
|
||||
t.start()
|
||||
else: self.call(func, origin, phenny, input)
|
||||
|
||||
|
|
|
@ -42,7 +42,7 @@ def msg(phenny, input):
|
|||
if input.sender.startswith('#'): return
|
||||
if input.admin:
|
||||
phenny.msg(input.group(2), input.group(3))
|
||||
msg.rule = (['msg'], r'(#\S+) (.*)')
|
||||
msg.rule = (['msg'], r'(#?\S+) (.*)')
|
||||
msg.priority = 'low'
|
||||
|
||||
def me(phenny, input):
|
||||
|
@ -51,7 +51,7 @@ def me(phenny, input):
|
|||
if input.admin:
|
||||
msg = '\x01ACTION %s\x01' % input.group(3)
|
||||
phenny.msg(input.group(2), msg)
|
||||
me.rule = (['me'], r'(#\S+) (.*)')
|
||||
me.rule = (['me'], r'(#?\S+) (.*)')
|
||||
me.priority = 'low'
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
|
|
@ -21,7 +21,8 @@ r_whitespace = re.compile(r'[\t\r\n ]+')
|
|||
abbrs = [
|
||||
'cf', 'lit', 'etc', 'Ger', 'Du', 'Skt', 'Rus', 'Eng', 'Amer.Eng', 'Sp',
|
||||
'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)'
|
||||
r_sentence = re.compile(t_sentence % ')(?<!'.join(abbrs))
|
||||
|
|
|
@ -21,7 +21,8 @@ def head(phenny, input):
|
|||
else: uri, header = uri, None
|
||||
|
||||
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)
|
||||
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')
|
||||
|
||||
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:
|
||||
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:
|
||||
uri = 'http://' + uri
|
||||
|
@ -145,7 +146,7 @@ def noteuri(phenny, input):
|
|||
if not hasattr(phenny.bot, 'last_seen_uri'):
|
||||
phenny.bot.last_seen_uri = {}
|
||||
phenny.bot.last_seen_uri[input.sender] = uri
|
||||
noteuri.rule = r'.*(http://[^<> "]+)[,.]?'
|
||||
noteuri.rule = r'.*(http://[^<> "\x01]+)[,.]?'
|
||||
noteuri.priority = 'low'
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
|
|
@ -49,6 +49,7 @@ def stats(phenny, input):
|
|||
ignore = set(['f_note', 'startup', 'message', 'noteuri'])
|
||||
for (name, user), count in phenny.stats.iteritems():
|
||||
if name in ignore: continue
|
||||
if not user: continue
|
||||
|
||||
if not user.startswith('#'):
|
||||
try: users[user] += count
|
||||
|
|
|
@ -65,7 +65,7 @@ def f_remind(phenny, input):
|
|||
msg = msg.encode('utf-8')
|
||||
|
||||
tellee_original = tellee.rstrip(',:;')
|
||||
tellee = tellee.lower()
|
||||
tellee = tellee_original.lower()
|
||||
|
||||
if not os.path.exists(phenny.tell_filename):
|
||||
return
|
||||
|
@ -90,7 +90,7 @@ def f_remind(phenny, input):
|
|||
|
||||
rand = random.random()
|
||||
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)
|
||||
elif teller.lower() == tellee:
|
||||
|
@ -126,10 +126,10 @@ def message(phenny, input):
|
|||
reminders = []
|
||||
remkeys = list(reversed(sorted(phenny.reminders.keys())))
|
||||
for remkey in remkeys:
|
||||
if not remkey.endswith('*'):
|
||||
if not remkey.endswith('*') or remkey.endswith(':'):
|
||||
if tellee.lower() == remkey:
|
||||
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))
|
||||
|
||||
for line in reminders[:maximum]:
|
||||
|
|
|
@ -68,9 +68,10 @@ def translate(phrase, lang, target='en'):
|
|||
|
||||
def tr(phenny, input):
|
||||
"""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')
|
||||
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.')
|
||||
|
||||
input = input or guess_language(phrase)
|
||||
|
|
|
@ -48,6 +48,10 @@ def search(term):
|
|||
print e
|
||||
return term
|
||||
|
||||
if isinstance(term, unicode):
|
||||
term = term.encode('utf-8')
|
||||
else: term = term.decode('utf-8')
|
||||
|
||||
term = term.replace('_', ' ')
|
||||
try: uri = search.result('site:en.wikipedia.org %s' % term)
|
||||
except IndexError: return term
|
||||
|
@ -58,7 +62,12 @@ def search(term):
|
|||
def wikipedia(term, last=False):
|
||||
global wikiuri
|
||||
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)
|
||||
bytes = r_tr.sub('', bytes)
|
||||
|
||||
|
|
2
phenny
2
phenny
|
@ -37,7 +37,7 @@ def create_default_config(fn):
|
|||
exclude = ['admin']
|
||||
|
||||
# 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 = []
|
||||
|
||||
|
|
Loading…
Reference in New Issue