From fd161988600411371e458d88d6a62fc4da00a3d1 Mon Sep 17 00:00:00 2001 From: Edward Powell Date: Sat, 8 Jun 2013 23:54:50 -0400 Subject: [PATCH] [adminchannel, spellcheck, translate] Update to 4.0 (issue #276) --- willie/modules/adminchannel.py | 136 +++++++++++++++------------------ willie/modules/spellcheck.py | 12 +-- willie/modules/translate.py | 98 +++++++++++++----------- 3 files changed, 122 insertions(+), 124 deletions(-) diff --git a/willie/modules/adminchannel.py b/willie/modules/adminchannel.py index ecb90163c5..778b088a88 100644 --- a/willie/modules/adminchannel.py +++ b/willie/modules/adminchannel.py @@ -10,15 +10,17 @@ """ import re +from willie.module import command, commands, priority -def setup(willie): - #Having a db means pref's exists. Later, we can just use `if willie.db`. - if willie.db and not willie.db.preferences.has_columns('topic_mask'): - willie.db.preferences.add_columns(['topic_mask']) +def setup(bot): + #Having a db means pref's exists. Later, we can just use `if bot.db`. + if bot.db and not bot.db.preferences.has_columns('topic_mask'): + bot.db.preferences.add_columns(['topic_mask']) -def op(willie, trigger): +@command('op') +def op(bot, trigger): """ Command to op users in a room. If no nick is given, willie will op the nick who sent the command @@ -28,12 +30,11 @@ def op(willie, trigger): channel = trigger.sender if not nick: nick = trigger.nick - willie.write(['MODE', channel, "+o", nick]) -op.rule = (['op'], r'(\S+)?') -op.priority = 'low' + bot.write(['MODE', channel, "+o", nick]) -def deop(willie, trigger): +@command('deop') +def deop(bot, trigger): """ Command to deop users in a room. If no nick is given, willie will deop the nick who sent the command @@ -43,12 +44,11 @@ def deop(willie, trigger): channel = trigger.sender if not nick: nick = trigger.nick - willie.write(['MODE', channel, "-o", nick]) -deop.rule = (['deop'], r'(\S+)?') -deop.priority = 'low' + bot.write(['MODE', channel, "-o", nick]) -def voice(willie, trigger): +@command('voice') +def voice(bot, trigger): """ Command to voice users in a room. If no nick is given, willie will voice the nick who sent the command @@ -58,12 +58,11 @@ def voice(willie, trigger): channel = trigger.sender if not nick: nick = trigger.nick - willie.write(['MODE', channel, "+v", nick]) -voice.rule = (['voice'], r'(\S+)?') -voice.priority = 'low' + bot.write(['MODE', channel, "+v", nick]) -def devoice(willie, trigger): +@command('devoice') +def devoice(bot, trigger): """ Command to devoice users in a room. If no nick is given, willie will devoice the nick who sent the command @@ -73,12 +72,12 @@ def devoice(willie, trigger): channel = trigger.sender if not nick: nick = trigger.nick - willie.write(['MODE', channel, "-v", nick]) -devoice.rule = (['devoice'], r'(\S+)?') -devoice.priority = 'low' + bot.write(['MODE', channel, "-v", nick]) -def kick(willie, trigger): +@command('kick') +@priority('high') +def kick(bot, trigger): """ Kick a user from the channel. """ @@ -99,10 +98,8 @@ def kick(willie, trigger): channel = opt reasonidx = 3 reason = ' '.join(text[reasonidx:]) - if nick != willie.config.nick: - willie.write(['KICK', channel, nick, reason]) -kick.commands = ['kick'] -kick.priority = 'high' + if nick != bot.config.nick: + bot.write(['KICK', channel, nick, reason]) def configureHostMask(mask): @@ -127,7 +124,9 @@ def configureHostMask(mask): return '' -def ban(willie, trigger): +@command('ban') +@priority('high') +def ban(bot, trigger): """ This give admins the ability to ban a user. The bot must be a Channel Operator for this command to work. @@ -149,12 +148,11 @@ def ban(willie, trigger): banmask = configureHostMask(banmask) if banmask == '': return - willie.write(['MODE', channel, '+b', banmask]) -ban.commands = ['ban', 'banish'] -ban.priority = 'high' + bot.write(['MODE', channel, '+b', banmask]) -def unban(willie, trigger): +@command('unban') +def unban(bot, trigger): """ This give admins the ability to unban a user. The bot must be a Channel Operator for this command to work. @@ -176,12 +174,11 @@ def unban(willie, trigger): banmask = configureHostMask(banmask) if banmask == '': return - willie.write(['MODE', channel, '-b', banmask]) -unban.commands = ['unban'] -unban.priority = 'high' + bot.write(['MODE', channel, '-b', banmask]) -def quiet(willie, trigger): +@command('quiet') +def quiet(bot, trigger): """ This gives admins the ability to quiet a user. The bot must be a Channel Operator for this command to work @@ -203,12 +200,11 @@ def quiet(willie, trigger): quietmask = configureHostMask(quietmask) if quietmask == '': return - willie.write(['MODE', channel, '+q', quietmask]) -quiet.commands = ['quiet'] -quiet.priority = 'high' + bot.write(['MODE', channel, '+q', quietmask]) -def unquiet(willie, trigger): +@command('unquiet') +def unquiet(bot, trigger): """ This gives admins the ability to unquiet a user. The bot must be a Channel Operator for this command to work @@ -230,12 +226,12 @@ def unquiet(willie, trigger): quietmask = configureHostMask(quietmask) if quietmask == '': return - willie.write(['MODE', opt, '-q', quietmask]) -unquiet.commands = ['unquiet'] -unquiet.priority = 'high' + bot.write(['MODE', opt, '-q', quietmask]) -def kickban(willie, trigger): +@commands('kickban', 'kb') +@priority('high') +def kickban(bot, trigger): """ This gives admins the ability to kickban a user. The bot must be a Channel Operator for this command to work @@ -262,13 +258,12 @@ def kickban(willie, trigger): mask = configureHostMask(mask) if mask == '': return - willie.write(['MODE', channel, '+b', mask]) - willie.write(['KICK', channel, nick, ' :', reason]) -kickban.commands = ['kickban', 'kb'] -kickban.priority = 'high' + bot.write(['MODE', channel, '+b', mask]) + bot.write(['KICK', channel, nick, ' :', reason]) -def topic(willie, trigger): +@command('topic') +def topic(bot, trigger): """ This gives ops the ability to change the topic. """ @@ -282,8 +277,8 @@ def topic(willie, trigger): narg = 1 mask = None - if willie.db and channel in willie.db.preferences: - mask = willie.db.preferences.get(channel, 'topic_mask') + if bot.db and channel in bot.db.preferences: + mask = bot.db.preferences.get(channel, 'topic_mask') narg = len(re.findall('%s', mask)) if not mask or mask == '': mask = purple + 'Welcome to: ' + green + channel + purple \ @@ -296,49 +291,44 @@ def topic(willie, trigger): if len(text) != narg: message = "Not enough arguments. You gave " + str(len(text)) + ', it requires ' + str(narg) + '.' - return willie.say(message) + return bot.say(message) topic = mask % text - willie.write(('TOPIC', channel + ' :' + topic)) -topic.commands = ['topic'] -topic.priority = 'low' + bot.write(('TOPIC', channel + ' :' + topic)) -def set_mask(willie, trigger): +@command('tmask') +def set_mask(bot, trigger): """ Set the mask to use for .topic in the current channel. %s is used to allow substituting in chunks of text. """ if not trigger.isop: return - if not willie.db: - willie.say("I'm afraid I can't do that.") + if not bot.db: + bot.say("I'm afraid I can't do that.") else: - willie.db.preferences.update(trigger.sender, {'topic_mask': trigger.group(2)}) - willie.say("Gotcha, " + trigger.nick) -set_mask.commands = ['tmask'] + bot.db.preferences.update(trigger.sender, {'topic_mask': trigger.group(2)}) + bot.say("Gotcha, " + trigger.nick) -def show_mask(willie, trigger): +@command('showmask') +def show_mask(bot, trigger): """Show the topic mask for the current channel.""" if not trigger.isop: return - if not willie.db: - willie.say("I'm afraid I can't do that.") - elif trigger.sender in willie.db.preferences: - willie.say(willie.db.preferences.get(trigger.sender, 'topic_mask')) + if not bot.db: + bot.say("I'm afraid I can't do that.") + elif trigger.sender in bot.db.preferences: + bot.say(bot.db.preferences.get(trigger.sender, 'topic_mask')) else: - willie.say("%s") -show_mask.commands = ['showmask'] + bot.say("%s") -def isop(willie, trigger): +@command('isop') +def isop(bot, trigger): """Show if you are an operator in the current channel""" if trigger.isop: - willie.reply('yes') + bot.reply('yes') else: - willie.reply('no') -isop.commands = ['isop'] - -if __name__ == '__main__': - print __doc__.strip() + bot.reply('no') diff --git a/willie/modules/spellcheck.py b/willie/modules/spellcheck.py index 1bbbebd87a..a532440cb0 100644 --- a/willie/modules/spellcheck.py +++ b/willie/modules/spellcheck.py @@ -9,7 +9,12 @@ This module relies on pyenchant, on Fedora and Red Hat based system, it can be found in the package python-enchant """ -import enchant +import enchant +from willie.module import commands, example + + +@commands('spellcheck', 'spell') +@example('.spellcheck stuff') def spellcheck(willie, trigger): """ Says whether the given word is spelled correctly, and gives suggestions if @@ -39,8 +44,3 @@ def spellcheck(willie, trigger): for suggested_word in sorted(set(sugWords)): # removes duplicates msg = msg + " '"+suggested_word+"'," willie.say(msg) -spellcheck.commands = ['spellcheck', 'spell'] -spellcheck.example = '.spellcheck stuff' - -if __name__ == '__main__': - print __doc__.strip() diff --git a/willie/modules/translate.py b/willie/modules/translate.py index 48d76f6303..f444557e88 100644 --- a/willie/modules/translate.py +++ b/willie/modules/translate.py @@ -9,16 +9,20 @@ """ import re -import willie.web as web +from willie import web +from willie.module import rule, commands, priority, example from time import sleep -import urllib2, json +import urllib2 +import json import random import os mangle_lines = {} -def setup(willie): + +def setup(bot): random.seed() + def configure(config): """ @@ -30,9 +34,10 @@ def configure(config): if config.option('Configure mangle module', False): config.add_section('translate') if config.option("Enable research mode"): - config.translate.research = True + config.translate.research = True if config.option("Collect mangle lines"): - config.translate.collect_mangle_lines = True + config.translate.collect_mangle_lines = True + def translate(text, input='auto', output='en'): raw = False @@ -40,7 +45,6 @@ def translate(text, input='auto', output='en'): output = output[:-4] raw = True - opener = urllib2.build_opener() opener.addheaders = [( 'User-Agent', 'Mozilla/5.0' + @@ -66,19 +70,25 @@ def translate(text, input='auto', output='en'): if raw: return str(data), 'en-raw' - try: language = data[2] # -2][0][0] - except: language = '?' + try: + language = data[2] # -2][0][0] + except: + language = '?' return ''.join(x[0] for x in data[0]), language -def tr(willie, trigger): + +@rule(ur'$nickname[,:]\s+(?:([a-z]{2}) +)?(?:([a-z]{2}|en-raw) +)?["“](.+?)["”]\? *$') +@example('$nickname: "mon chien"? or $nickname: fr "mon chien"?') +@priority('low') +def tr(bot, trigger): """Translates a phrase, with an optional language hint.""" input, output, phrase = trigger.groups() phrase = phrase.encode('utf-8') if (len(phrase) > 350) and (not trigger.admin): - return willie.reply('Phrase must be under 350 characters.') + return bot.reply('Phrase must be under 350 characters.') input = input or 'auto' input = input.encode('utf-8') @@ -89,18 +99,18 @@ def tr(willie, trigger): if isinstance(msg, str): msg = msg.decode('utf-8') if msg: - msg = web.decode(msg) # msg.replace(''', "'") + msg = web.decode(msg) # msg.replace(''', "'") msg = '"%s" (%s to %s, translate.google.com)' % (msg, input, output) - else: msg = 'The %s to %s translation failed, sorry!' % (input, output) + else: + msg = 'The %s to %s translation failed, sorry!' % (input, output) - willie.reply(msg) - else: willie.reply('Language guessing failed, so try suggesting one!') + bot.reply(msg) + else: + bot.reply('Language guessing failed, so try suggesting one!') -tr.rule = ('$nick', ur'(?:([a-z]{2}) +)?(?:([a-z]{2}|en-raw) +)?["“](.+?)["”]\? *$') -tr.example = '$nickname: "mon chien"? or $nickname: fr "mon chien"?' -tr.priority = 'low' -def tr2(willie, trigger): +@commands('translate', 'tr') +def tr2(bot, trigger): """Translates a phrase, with an optional language hint.""" command = trigger.group(2).encode('utf-8') @@ -110,7 +120,8 @@ def langcode(p): args = ['auto', 'en'] for i in xrange(2): - if not ' ' in command: break + if not ' ' in command: + break prefix, cmd = command.split(' ', 1) if langcode(prefix): args[i] = prefix[1:] @@ -118,7 +129,7 @@ def langcode(p): phrase = command if (len(phrase) > 350) and (not trigger.admin): - return willie.reply('Phrase must be under 350 characters.') + return bot.reply('Phrase must be under 350 characters.') src, dest = args if src != dest: @@ -126,18 +137,18 @@ def langcode(p): if isinstance(msg, str): msg = msg.decode('utf-8') if msg: - msg = web.decode(msg) # msg.replace(''', "'") + msg = web.decode(msg) # msg.replace(''', "'") msg = '"%s" (%s to %s, translate.google.com)' % (msg, src, dest) - else: msg = 'The %s to %s translation failed, sorry!' % (src, dest) + else: + msg = 'The %s to %s translation failed, sorry!' % (src, dest) - willie.reply(msg) - else: willie.reply('Language guessing failed, so try suggesting one!') + bot.reply(msg) + else: + bot.reply('Language guessing failed, so try suggesting one!') -tr2.commands = ['tr', 'translate'] -tr2.priority = 'low' def get_random_lang(long_list, short_list): - random_index = random.randint(0, len(long_list)-1) + random_index = random.randint(0, len(long_list) - 1) random_lang = long_list[random_index] if not random_lang in short_list: short_list.append(random_lang) @@ -145,7 +156,9 @@ def get_random_lang(long_list, short_list): return get_random_lang(long_list, short_list) return short_list -def mangle(willie, trigger): + +@commands('mangle', 'mangle2') +def mangle(bot, trigger): """Repeatedly translate the input until it makes absolutely no sense.""" global mangle_lines long_lang_list = ['fr', 'de', 'es', 'it', 'no', 'he', 'la', 'ja', 'cy', 'ar', 'yi', 'zh', 'nl', 'ru', 'fi', 'hi', 'af', 'jw', 'mr', 'ceb', 'cs', 'ga', 'sv', 'eo', 'el', 'ms', 'lv'] @@ -157,15 +170,15 @@ def mangle(willie, trigger): try: phrase = (mangle_lines[trigger.sender.lower()], '') except: - willie.reply("What do you want me to mangle?") + bot.reply("What do you want me to mangle?") return else: phrase = (trigger.group(2).encode('utf-8').strip(), '') if phrase[0] == '': - willie.reply("What do you want me to mangle?") + bot.reply("What do you want me to mangle?") return - if willie.config.has_section('translate') and willie.config.translate.research == True: - research_logfile = open(os.path.join(willie.config.logdir, 'mangle.log'), 'a') + if bot.config.has_section('translate') and bot.config.translate.research == True: + research_logfile = open(os.path.join(bot.config.logdir, 'mangle.log'), 'a') research_logfile.write('Phrase: %s\n' % str(phrase)) research_logfile.write('Lang_list: %s\n' % lang_list) for lang in lang_list: @@ -183,26 +196,21 @@ def mangle(willie, trigger): phrase = translate(phrase[0], lang, 'en') except: break - if willie.config.has_section('translate') and willie.config.translate.research == True: + if bot.config.has_section('translate') and bot.config.translate.research == True: research_logfile.write('-> %s\n' % str(phrase)) if not phrase: phrase = backup break - if willie.config.has_section('translate') and willie.config.translate.research == True: + if bot.config.has_section('translate') and bot.config.translate.research == True: research_logfile.write('->[FINAL] %s\n' % str(phrase)) research_logfile.write('----------------------------\n\n\n') research_logfile.close() - willie.reply(phrase[0]) - -mangle.commands = ['mangle', 'mangle2'] + bot.reply(phrase[0]) -def collect_mangle_lines(willie, trigger): - if willie.config.has_section('translate') and willie.config.translate.collect_mangle_lines == True: + +@rule('(.*)') +@priority('low') +def collect_mangle_lines(bot, trigger): + if bot.config.has_section('translate') and bot.config.translate.collect_mangle_lines == True: global mangle_lines mangle_lines[trigger.sender.lower()] = "%s said '%s'" % (trigger.nick, (trigger.group(0).strip())) -collect_mangle_lines.rule = ('(.*)') -collect_mangle_lines.priority = 'low' - -if __name__ == '__main__': - print __doc__.strip() -