transform: port TextTransform to bot v2

this just has some simple (and occasionally handy?) text transformations
as an irc plugin, like upper/lower case, base64, and similar
This commit is contained in:
Brian S. Stephan 2015-05-27 20:05:38 -05:00
parent fefbcf695e
commit 91f0b8c651
2 changed files with 105 additions and 0 deletions

View File

View File

@ -0,0 +1,105 @@
"""Assorted text transformations (e.g. rot13)."""
from __future__ import unicode_literals
import base64
from ircbot.lib import Plugin
class Transform(Plugin):
"""Do a number of text transformations, like rot13."""
def start(self):
"""Set up the handlers."""
self.connection.reactor.add_global_regex_handler(['pubmsg', 'privmsg'], r'^!rot13\s+(.*)$',
self.handle_rot13, -20)
self.connection.reactor.add_global_regex_handler(['pubmsg', 'privmsg'], r'^!base64\s+(encode|decode)\s+(.*)$',
self.handle_base64, -20)
self.connection.reactor.add_global_regex_handler(['pubmsg', 'privmsg'], r'^!upper\s+(.*)$',
self.handle_upper, -20)
self.connection.reactor.add_global_regex_handler(['pubmsg', 'privmsg'], r'^!lower\s+(.*)$',
self.handle_lower, -20)
self.connection.reactor.add_global_regex_handler(['pubmsg', 'privmsg'], r'^!(de)??albhed\s+(.*)$',
self.handle_albhed, -20)
super(Transform, self).start()
def stop(self):
"""Tear down handlers."""
self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], self.handle_rot13)
self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], self.handle_base64)
self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], self.handle_upper)
self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], self.handle_lower)
self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], self.handle_albhed)
super(Transform, self).stop()
def handle_rot13(self, connection, event, match):
"""Apply a rot13 method to the text."""
text = match.group(1)
return self.bot.reply(event, text.encode('rot13', 'ignore'))
def handle_base64(self, connection, event, match):
"""Apply a base64 encoding to the text."""
direction = match.group(1)
text = match.group(2)
if direction == 'encode':
return self.bot.reply(event, base64.encodestring(text).replace('\n', ''))
elif direction == 'decode':
return self.bot.reply(event, base64.decodestring(text).replace('\n', ''))
def handle_upper(self, connection, event, match):
"""Uppercase the text."""
text = match.group(1)
return self.bot.reply(event, text.upper())
def handle_lower(self, connection, event, match):
"""Lowercase the text."""
text = match.group(1)
return self.bot.reply(event, text.lower())
def handle_albhed(self, connection, event, match):
"""Al-Bhedify the text."""
reverse = match.group(1)
text = match.group(2)
cipher = {'A': 'Y', 'B': 'P', 'C': 'L', 'D': 'T', 'E': 'A', 'F': 'V', 'G': 'K', 'H': 'R', 'I': 'E',
'J': 'Z', 'K': 'G', 'L': 'M', 'M': 'S', 'N': 'H', 'O': 'U', 'P': 'B', 'Q': 'X', 'R': 'N',
'S': 'C', 'T': 'D', 'U': 'I', 'V': 'J', 'W': 'F', 'X': 'Q', 'Y': 'O', 'Z': 'W'}
decipher = dict([(v, k) for (k, v) in cipher.iteritems()])
if not reverse:
trans = []
for i in range(len(text)):
if text[i] in cipher:
trans.append(cipher[text[i]])
elif text[i].upper() in cipher:
trans.append(cipher[text[i].upper()].lower())
else:
trans.append(text[i])
return self.bot.reply(event, "".join(trans))
elif reverse == 'de':
trans = []
for i in range(len(text)):
if text[i] in decipher:
trans.append(decipher[text[i]])
elif text[i].upper() in decipher:
trans.append(decipher[text[i].upper()].lower())
else:
trans.append(text[i])
return self.bot.reply(event, "".join(trans))
plugin = Transform