From 286d4a8497621d2f3c0f1f8d66db60b11c87fad5 Mon Sep 17 00:00:00 2001 From: "Sean B. Palmer" Date: Mon, 31 Mar 2008 16:17:32 +0100 Subject: New "limit" config variable, and some module fixes. --- bot.py | 27 +++++++++++++++++++-------- modules/admin.py | 4 ++-- modules/etymology.py | 3 ++- modules/head.py | 9 +++++---- modules/info.py | 1 + modules/tell.py | 8 ++++---- modules/translate.py | 5 +++-- modules/wikipedia.py | 11 ++++++++++- phenny | 2 +- tools.py | 1 + 10 files changed, 48 insertions(+), 23 deletions(-) diff --git a/bot.py b/bot.py index ae97d93..37f892c 100755 --- a/bot.py +++ b/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) diff --git a/modules/admin.py b/modules/admin.py index 24b8fb6..e1dd908 100755 --- a/modules/admin.py +++ b/modules/admin.py @@ -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__': diff --git a/modules/etymology.py b/modules/etymology.py index 52ae1bc..a26f06f 100755 --- a/modules/etymology.py +++ b/modules/etymology.py @@ -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'^.*?(? "]+)[,.]?' +noteuri.rule = r'.*(http://[^<> "\x01]+)[,.]?' noteuri.priority = 'low' if __name__ == '__main__': diff --git a/modules/info.py b/modules/info.py index a40a34f..a5113ba 100755 --- a/modules/info.py +++ b/modules/info.py @@ -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 diff --git a/modules/tell.py b/modules/tell.py index dc8ea46..ef9f171 100755 --- a/modules/tell.py +++ b/modules/tell.py @@ -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]: diff --git a/modules/translate.py b/modules/translate.py index d90de43..f7cf0a2 100755 --- a/modules/translate.py +++ b/modules/translate.py @@ -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) diff --git a/modules/wikipedia.py b/modules/wikipedia.py index 540ba4a..ea32443 100755 --- a/modules/wikipedia.py +++ b/modules/wikipedia.py @@ -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) diff --git a/phenny b/phenny index 0137239..5a632ed 100755 --- a/phenny +++ b/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 = [] diff --git a/tools.py b/tools.py index b1da848..47d582a 100755 --- a/tools.py +++ b/tools.py @@ -18,6 +18,7 @@ def deprecated(old): args = [input.bytes, input.sender, '@@'] old(self, origin, match, args) + new.__module__ = old.__module__ new.__name__ = old.__name__ return new -- cgit v1.2.3-1-g7c22