2007-12-05 20:00:41 +01:00
|
|
|
# -*- encoding: utf-8 -*-
|
2005-01-25 21:04:14 +01:00
|
|
|
###
|
|
|
|
# Copyright (c) 2002-2005, Jeremiah Fincher
|
|
|
|
# All rights reserved.
|
|
|
|
#
|
|
|
|
# Redistribution and use in source and binary forms, with or without
|
|
|
|
# modification, are permitted provided that the following conditions are met:
|
|
|
|
#
|
|
|
|
# * Redistributions of source code must retain the above copyright notice,
|
|
|
|
# this list of conditions, and the following disclaimer.
|
|
|
|
# * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
# this list of conditions, and the following disclaimer in the
|
|
|
|
# documentation and/or other materials provided with the distribution.
|
|
|
|
# * Neither the name of the author of this software nor the name of
|
|
|
|
# contributors to this software may be used to endorse or promote products
|
|
|
|
# derived from this software without specific prior written consent.
|
|
|
|
#
|
|
|
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
# POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
###
|
|
|
|
|
2015-08-10 18:52:51 +02:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2005-01-25 21:04:14 +01:00
|
|
|
import re
|
2013-01-06 16:53:07 +01:00
|
|
|
import sys
|
2012-08-04 19:43:11 +02:00
|
|
|
import codecs
|
2005-01-25 21:04:14 +01:00
|
|
|
import string
|
|
|
|
import random
|
|
|
|
|
|
|
|
import supybot.conf as conf
|
|
|
|
import supybot.utils as utils
|
|
|
|
from supybot.commands import *
|
2015-08-11 16:50:23 +02:00
|
|
|
import supybot.utils.minisix as minisix
|
2005-01-25 21:04:14 +01:00
|
|
|
import supybot.ircmsgs as ircmsgs
|
|
|
|
import supybot.ircutils as ircutils
|
|
|
|
import supybot.callbacks as callbacks
|
2010-10-17 14:50:31 +02:00
|
|
|
from supybot.i18n import PluginInternationalization, internationalizeDocstring
|
|
|
|
_ = PluginInternationalization('Filter')
|
2005-01-25 21:04:14 +01:00
|
|
|
|
|
|
|
class MyFilterProxy(object):
|
|
|
|
def reply(self, s):
|
|
|
|
self.s = s
|
|
|
|
|
2005-02-09 08:04:04 +01:00
|
|
|
class Filter(callbacks.Plugin):
|
2005-01-25 21:04:14 +01:00
|
|
|
"""This plugin offers several commands which transform text in some way.
|
|
|
|
It also provides the capability of using such commands to 'filter' the
|
|
|
|
output of the bot -- for instance, you could make everything the bot says
|
|
|
|
be in leetspeak, or Morse code, or any number of other kinds of filters.
|
|
|
|
Not very useful, but definitely quite fun :)"""
|
2005-01-29 20:16:29 +01:00
|
|
|
def __init__(self, irc):
|
|
|
|
self.__parent = super(Filter, self)
|
|
|
|
self.__parent.__init__(irc)
|
2005-01-25 21:04:14 +01:00
|
|
|
self.outFilters = ircutils.IrcDict()
|
|
|
|
|
|
|
|
def outFilter(self, irc, msg):
|
2016-01-02 13:32:03 +01:00
|
|
|
if msg.command in ('PRIVMSG', 'NOTICE'):
|
2019-08-24 17:50:05 +02:00
|
|
|
if msg.channel in self.outFilters:
|
2005-01-25 21:04:14 +01:00
|
|
|
if ircmsgs.isAction(msg):
|
|
|
|
s = ircmsgs.unAction(msg)
|
|
|
|
else:
|
|
|
|
s = msg.args[1]
|
2019-08-24 17:50:05 +02:00
|
|
|
methods = self.outFilters[msg.channel]
|
2005-01-25 21:04:14 +01:00
|
|
|
for filtercommand in methods:
|
|
|
|
myIrc = MyFilterProxy()
|
|
|
|
filtercommand(myIrc, msg, [s])
|
|
|
|
s = myIrc.s
|
|
|
|
if ircmsgs.isAction(msg):
|
|
|
|
msg = ircmsgs.action(msg.args[0], s, msg=msg)
|
|
|
|
else:
|
|
|
|
msg = ircmsgs.IrcMsg(msg=msg, args=(msg.args[0], s))
|
|
|
|
return msg
|
|
|
|
|
2014-10-14 22:34:11 +02:00
|
|
|
_filterCommands = ['jeffk', 'leet', 'rot13', 'hexlify', 'binary',
|
2005-01-25 21:04:14 +01:00
|
|
|
'scramble', 'morse', 'reverse', 'colorize', 'squish',
|
2014-12-31 20:23:18 +01:00
|
|
|
'supa1337', 'stripcolor', 'aol', 'rainbow', 'spellit',
|
2020-05-23 12:06:55 +02:00
|
|
|
'hebrew', 'undup', 'uwu', 'gnu', 'shrink', 'uniud', 'capwords',
|
2015-10-28 02:14:10 +01:00
|
|
|
'caps', 'vowelrot']
|
2010-10-17 14:50:31 +02:00
|
|
|
@internationalizeDocstring
|
2005-01-25 21:04:14 +01:00
|
|
|
def outfilter(self, irc, msg, args, channel, command):
|
|
|
|
"""[<channel>] [<command>]
|
|
|
|
|
|
|
|
Sets the outFilter of this plugin to be <command>. If no command is
|
|
|
|
given, unsets the outFilter. <channel> is only necessary if the
|
|
|
|
message isn't sent in the channel itself.
|
|
|
|
"""
|
|
|
|
if command:
|
|
|
|
if not self.isDisabled(command) and \
|
|
|
|
command in self._filterCommands:
|
|
|
|
method = getattr(self, command)
|
|
|
|
self.outFilters.setdefault(channel, []).append(method)
|
|
|
|
irc.replySuccess()
|
|
|
|
else:
|
2010-10-17 14:50:31 +02:00
|
|
|
irc.error(_('That\'s not a valid filter command.'))
|
2005-01-25 21:04:14 +01:00
|
|
|
else:
|
|
|
|
self.outFilters[channel] = []
|
|
|
|
irc.replySuccess()
|
|
|
|
outfilter = wrap(outfilter,
|
|
|
|
[('checkChannelCapability', 'op'),
|
|
|
|
additional('commandName')])
|
|
|
|
|
2012-08-04 19:43:11 +02:00
|
|
|
_hebrew_remover = utils.str.MultipleRemover('aeiou')
|
2010-10-17 14:50:31 +02:00
|
|
|
@internationalizeDocstring
|
2005-01-25 21:04:14 +01:00
|
|
|
def hebrew(self, irc, msg, args, text):
|
|
|
|
"""<text>
|
|
|
|
|
|
|
|
Removes all the vowels from <text>. (If you're curious why this is
|
|
|
|
named 'hebrew' it's because I (jemfinch) thought of it in Hebrew class,
|
|
|
|
and printed Hebrew often elides the vowels.)
|
|
|
|
"""
|
2012-08-04 19:43:11 +02:00
|
|
|
irc.reply(self._hebrew_remover(text))
|
2005-01-25 21:04:14 +01:00
|
|
|
hebrew = wrap(hebrew, ['text'])
|
2009-08-22 21:03:47 +02:00
|
|
|
|
2015-09-10 04:32:15 +02:00
|
|
|
def _squish(self, text):
|
2015-09-10 15:57:40 +02:00
|
|
|
return text.replace(' ', '')
|
2015-09-10 04:32:15 +02:00
|
|
|
|
2010-10-17 14:50:31 +02:00
|
|
|
@internationalizeDocstring
|
2005-01-25 21:04:14 +01:00
|
|
|
def squish(self, irc, msg, args, text):
|
|
|
|
"""<text>
|
|
|
|
|
|
|
|
Removes all the spaces from <text>.
|
|
|
|
"""
|
2015-09-10 04:32:15 +02:00
|
|
|
irc.reply(self._squish(text))
|
2005-01-25 21:04:14 +01:00
|
|
|
squish = wrap(squish, ['text'])
|
|
|
|
|
2010-10-17 14:50:31 +02:00
|
|
|
@internationalizeDocstring
|
2005-01-25 21:04:14 +01:00
|
|
|
def undup(self, irc, msg, args, text):
|
|
|
|
"""<text>
|
|
|
|
|
|
|
|
Returns <text>, with all consecutive duplicated letters removed.
|
|
|
|
"""
|
|
|
|
L = [text[0]]
|
|
|
|
for c in text:
|
|
|
|
if c != L[-1]:
|
|
|
|
L.append(c)
|
|
|
|
irc.reply(''.join(L))
|
|
|
|
undup = wrap(undup, ['text'])
|
|
|
|
|
2010-10-17 14:50:31 +02:00
|
|
|
@internationalizeDocstring
|
2005-01-25 21:04:14 +01:00
|
|
|
def binary(self, irc, msg, args, text):
|
|
|
|
"""<text>
|
|
|
|
|
|
|
|
Returns the binary representation of <text>.
|
|
|
|
"""
|
|
|
|
L = []
|
2015-08-09 00:23:03 +02:00
|
|
|
if minisix.PY3:
|
2014-05-08 21:57:35 +02:00
|
|
|
if isinstance(text, str):
|
|
|
|
bytes_ = text.encode()
|
|
|
|
else:
|
|
|
|
bytes_ = text
|
|
|
|
else:
|
|
|
|
if isinstance(text, unicode):
|
|
|
|
text = text.encode()
|
|
|
|
bytes_ = map(ord, text)
|
|
|
|
for i in bytes_:
|
2005-01-25 21:04:14 +01:00
|
|
|
LL = []
|
2014-05-08 21:57:35 +02:00
|
|
|
assert i<=256
|
2005-01-25 21:04:14 +01:00
|
|
|
counter = 8
|
|
|
|
while i:
|
|
|
|
counter -= 1
|
|
|
|
if i & 1:
|
|
|
|
LL.append('1')
|
|
|
|
else:
|
|
|
|
LL.append('0')
|
|
|
|
i >>= 1
|
|
|
|
while counter:
|
|
|
|
LL.append('0')
|
|
|
|
counter -= 1
|
|
|
|
LL.reverse()
|
|
|
|
L.extend(LL)
|
|
|
|
irc.reply(''.join(L))
|
|
|
|
binary = wrap(binary, ['text'])
|
|
|
|
|
2010-07-01 21:44:53 +02:00
|
|
|
def unbinary(self, irc, msg, args, text):
|
|
|
|
"""<text>
|
|
|
|
|
|
|
|
Returns the character representation of binary <text>.
|
|
|
|
Assumes ASCII, 8 digits per character.
|
|
|
|
"""
|
2015-09-10 04:32:15 +02:00
|
|
|
text = self._squish(text) # Strip spaces.
|
2010-07-21 18:57:18 +02:00
|
|
|
try:
|
2015-08-10 19:36:07 +02:00
|
|
|
L = [chr(int(text[i:(i+8)], 2)) for i in range(0, len(text), 8)]
|
2010-07-21 18:57:18 +02:00
|
|
|
irc.reply(''.join(L))
|
|
|
|
except ValueError:
|
|
|
|
irc.errorInvalid('binary string', text)
|
2010-07-01 21:44:53 +02:00
|
|
|
unbinary = wrap(unbinary, ['text'])
|
|
|
|
|
2012-08-04 19:43:11 +02:00
|
|
|
_hex_encoder = staticmethod(codecs.getencoder('hex_codec'))
|
2005-01-25 21:04:14 +01:00
|
|
|
def hexlify(self, irc, msg, args, text):
|
|
|
|
"""<text>
|
|
|
|
|
|
|
|
Returns a hexstring from the given string; a hexstring is a string
|
|
|
|
composed of the hexadecimal value of each character in the string
|
|
|
|
"""
|
2012-08-04 19:43:11 +02:00
|
|
|
irc.reply(self._hex_encoder(text.encode('utf8'))[0].decode('utf8'))
|
2005-01-25 21:04:14 +01:00
|
|
|
hexlify = wrap(hexlify, ['text'])
|
|
|
|
|
2012-08-04 19:43:11 +02:00
|
|
|
_hex_decoder = staticmethod(codecs.getdecoder('hex_codec'))
|
2010-10-17 14:50:31 +02:00
|
|
|
@internationalizeDocstring
|
2005-01-25 21:04:14 +01:00
|
|
|
def unhexlify(self, irc, msg, args, text):
|
|
|
|
"""<hexstring>
|
|
|
|
|
|
|
|
Returns the string corresponding to <hexstring>. Obviously,
|
|
|
|
<hexstring> must be a string of hexadecimal digits.
|
|
|
|
"""
|
|
|
|
try:
|
2013-07-16 21:54:10 +02:00
|
|
|
irc.reply(self._hex_decoder(text.encode('utf8'))[0]
|
|
|
|
.decode('utf8', 'replace'))
|
2005-01-25 21:04:14 +01:00
|
|
|
except TypeError:
|
2010-10-17 14:50:31 +02:00
|
|
|
irc.error(_('Invalid input.'))
|
2005-01-25 21:04:14 +01:00
|
|
|
unhexlify = wrap(unhexlify, ['text'])
|
|
|
|
|
2012-08-04 19:43:11 +02:00
|
|
|
_rot13_encoder = codecs.getencoder('rot-13')
|
2010-10-17 14:50:31 +02:00
|
|
|
@internationalizeDocstring
|
2005-01-25 21:04:14 +01:00
|
|
|
def rot13(self, irc, msg, args, text):
|
|
|
|
"""<text>
|
|
|
|
|
|
|
|
Rotates <text> 13 characters to the right in the alphabet. Rot13 is
|
|
|
|
commonly used for text that simply needs to be hidden from inadvertent
|
|
|
|
reading by roaming eyes, since it's easily reversible.
|
|
|
|
"""
|
2015-08-09 00:23:03 +02:00
|
|
|
if minisix.PY2:
|
2013-01-06 16:53:07 +01:00
|
|
|
text = text.decode('utf8')
|
|
|
|
irc.reply(self._rot13_encoder(text)[0])
|
2005-01-25 21:04:14 +01:00
|
|
|
rot13 = wrap(rot13, ['text'])
|
|
|
|
|
2014-01-21 10:40:18 +01:00
|
|
|
_leettrans = utils.str.MultipleReplacer(dict(list(zip('oOaAeElBTiIts',
|
|
|
|
'004433187!1+5'))))
|
2005-01-25 21:04:14 +01:00
|
|
|
_leetres = [(re.compile(r'\b(?:(?:[yY][o0O][oO0uU])|u)\b'), 'j00'),
|
|
|
|
(re.compile(r'fear'), 'ph33r'),
|
|
|
|
(re.compile(r'[aA][tT][eE]'), '8'),
|
|
|
|
(re.compile(r'[aA][tT]'), '@'),
|
|
|
|
(re.compile(r'[sS]\b'), 'z'),
|
|
|
|
(re.compile(r'x'), '><'),]
|
2010-10-17 14:50:31 +02:00
|
|
|
@internationalizeDocstring
|
2005-01-25 21:04:14 +01:00
|
|
|
def leet(self, irc, msg, args, text):
|
|
|
|
"""<text>
|
|
|
|
|
|
|
|
Returns the l33tspeak version of <text>
|
|
|
|
"""
|
|
|
|
for (r, sub) in self._leetres:
|
|
|
|
text = re.sub(r, sub, text)
|
2012-08-04 14:04:15 +02:00
|
|
|
text = self._leettrans(text)
|
2005-01-25 21:04:14 +01:00
|
|
|
irc.reply(text)
|
|
|
|
leet = wrap(leet, ['text'])
|
|
|
|
|
|
|
|
_supaleetreplacers = [('xX', '><'), ('kK', '|<'), ('rR', '|2'),
|
|
|
|
('hH', '|-|'), ('L', '|_'), ('uU', '|_|'),
|
|
|
|
('O', '()'), ('nN', '|\\|'), ('mM', '/\\/\\'),
|
|
|
|
('G', '6'), ('Ss', '$'), ('i', ';'), ('aA', '/-\\'),
|
|
|
|
('eE', '3'), ('t', '+'), ('T', '7'), ('l', '1'),
|
|
|
|
('D', '|)'), ('B', '|3'), ('I', ']['), ('Vv', '\\/'),
|
|
|
|
('wW', '\\/\\/'), ('d', 'c|'), ('b', '|>'),
|
|
|
|
('c', '<'), ('h', '|n'),]
|
2010-10-17 14:50:31 +02:00
|
|
|
@internationalizeDocstring
|
2005-01-25 21:04:14 +01:00
|
|
|
def supa1337(self, irc, msg, args, text):
|
|
|
|
"""<text>
|
|
|
|
|
|
|
|
Replies with an especially k-rad translation of <text>.
|
|
|
|
"""
|
|
|
|
for (r, sub) in self._leetres:
|
|
|
|
text = re.sub(r, sub, text)
|
|
|
|
for (letters, replacement) in self._supaleetreplacers:
|
|
|
|
for letter in letters:
|
|
|
|
text = text.replace(letter, replacement)
|
|
|
|
irc.reply(text)
|
|
|
|
supa1337 = wrap(supa1337, ['text'])
|
|
|
|
|
|
|
|
_scrambleRe = re.compile(r'(?:\b|(?![a-zA-Z]))([a-zA-Z])([a-zA-Z]*)'
|
|
|
|
r'([a-zA-Z])(?:\b|(?![a-zA-Z]))')
|
2010-10-17 14:50:31 +02:00
|
|
|
@internationalizeDocstring
|
2005-01-25 21:04:14 +01:00
|
|
|
def scramble(self, irc, msg, args, text):
|
|
|
|
"""<text>
|
|
|
|
|
|
|
|
Replies with a string where each word is scrambled; i.e., each internal
|
|
|
|
letter (that is, all letters but the first and last) are shuffled.
|
|
|
|
"""
|
|
|
|
def _subber(m):
|
|
|
|
L = list(m.group(2))
|
|
|
|
random.shuffle(L)
|
|
|
|
return '%s%s%s' % (m.group(1), ''.join(L), m.group(3))
|
|
|
|
s = self._scrambleRe.sub(_subber, text)
|
|
|
|
irc.reply(s)
|
|
|
|
scramble = wrap(scramble, ['text'])
|
|
|
|
|
2010-09-01 00:52:09 +02:00
|
|
|
_morseCode = {
|
2005-01-25 21:04:14 +01:00
|
|
|
"A" : ".-",
|
|
|
|
"B" : "-...",
|
|
|
|
"C" : "-.-.",
|
|
|
|
"D" : "-..",
|
|
|
|
"E" : ".",
|
|
|
|
"F" : "..-.",
|
|
|
|
"G" : "--.",
|
|
|
|
"H" : "....",
|
|
|
|
"I" : "..",
|
|
|
|
"J" : ".---",
|
|
|
|
"K" : "-.-",
|
|
|
|
"L" : ".-..",
|
|
|
|
"M" : "--",
|
|
|
|
"N" : "-.",
|
|
|
|
"O" : "---",
|
|
|
|
"P" : ".--.",
|
|
|
|
"Q" : "--.-",
|
|
|
|
"R" : ".-.",
|
|
|
|
"S" : "...",
|
|
|
|
"T" : "-",
|
|
|
|
"U" : "..-",
|
|
|
|
"V" : "...-",
|
|
|
|
"W" : ".--",
|
|
|
|
"X" : "-..-",
|
|
|
|
"Y" : "-.--",
|
|
|
|
"Z" : "--..",
|
|
|
|
"0" : "-----",
|
|
|
|
"1" : ".----",
|
|
|
|
"2" : "..---",
|
|
|
|
"3" : "...--",
|
|
|
|
"4" : "....-",
|
|
|
|
"5" : ".....",
|
|
|
|
"6" : "-....",
|
|
|
|
"7" : "--...",
|
|
|
|
"8" : "---..",
|
|
|
|
"9" : "----.",
|
|
|
|
"." : ".-.-.-",
|
|
|
|
"," : "--..--",
|
|
|
|
":" : "---...",
|
|
|
|
"?" : "..--..",
|
|
|
|
"'" : ".----.",
|
|
|
|
"-" : "-....-",
|
|
|
|
"/" : "-..-.",
|
|
|
|
'"' : ".-..-.",
|
|
|
|
"@" : ".--.-.",
|
|
|
|
"=" : "-...-"
|
|
|
|
}
|
2010-09-01 00:52:09 +02:00
|
|
|
_revMorseCode = dict([(y, x) for (x, y) in _morseCode.items()])
|
2005-01-25 21:04:14 +01:00
|
|
|
_unmorsere = re.compile('([.-]+)')
|
2010-10-17 14:50:31 +02:00
|
|
|
@internationalizeDocstring
|
2005-01-25 21:04:14 +01:00
|
|
|
def unmorse(self, irc, msg, args, text):
|
|
|
|
"""<Morse code text>
|
|
|
|
|
|
|
|
Does the reverse of the morse command.
|
|
|
|
"""
|
|
|
|
text = text.replace('_', '-')
|
|
|
|
def morseToLetter(m):
|
|
|
|
s = m.group(1)
|
2010-09-01 00:52:09 +02:00
|
|
|
return self._revMorseCode.get(s, s)
|
2005-01-25 21:04:14 +01:00
|
|
|
text = self._unmorsere.sub(morseToLetter, text)
|
|
|
|
text = text.replace(' ', '\x00')
|
|
|
|
text = text.replace(' ', '')
|
|
|
|
text = text.replace('\x00', ' ')
|
|
|
|
irc.reply(text)
|
|
|
|
unmorse = wrap(unmorse, ['text'])
|
|
|
|
|
2010-10-17 14:50:31 +02:00
|
|
|
@internationalizeDocstring
|
2005-01-25 21:04:14 +01:00
|
|
|
def morse(self, irc, msg, args, text):
|
|
|
|
"""<text>
|
|
|
|
|
|
|
|
Gives the Morse code equivalent of a given string.
|
|
|
|
"""
|
|
|
|
L = []
|
|
|
|
for c in text.upper():
|
2010-09-01 00:52:09 +02:00
|
|
|
L.append(self._morseCode.get(c, c))
|
2005-01-25 21:04:14 +01:00
|
|
|
irc.reply(' '.join(L))
|
|
|
|
morse = wrap(morse, ['text'])
|
|
|
|
|
2010-10-17 14:50:31 +02:00
|
|
|
@internationalizeDocstring
|
2005-01-25 21:04:14 +01:00
|
|
|
def reverse(self, irc, msg, args, text):
|
|
|
|
"""<text>
|
|
|
|
|
|
|
|
Reverses <text>.
|
|
|
|
"""
|
|
|
|
irc.reply(text[::-1])
|
|
|
|
reverse = wrap(reverse, ['text'])
|
|
|
|
|
2010-10-17 14:50:31 +02:00
|
|
|
@internationalizeDocstring
|
2005-01-25 21:04:14 +01:00
|
|
|
def _color(self, c, fg=None):
|
|
|
|
if c == ' ':
|
|
|
|
return c
|
|
|
|
if fg is None:
|
2012-07-03 03:11:50 +02:00
|
|
|
fg = random.randint(2, 15)
|
|
|
|
fg = str(fg).zfill(2)
|
2005-01-25 21:04:14 +01:00
|
|
|
return '\x03%s%s' % (fg, c)
|
|
|
|
|
2010-10-17 14:50:31 +02:00
|
|
|
@internationalizeDocstring
|
2005-01-25 21:04:14 +01:00
|
|
|
def colorize(self, irc, msg, args, text):
|
|
|
|
"""<text>
|
|
|
|
|
|
|
|
Returns <text> with each character randomly colorized.
|
|
|
|
"""
|
2015-08-31 17:52:45 +02:00
|
|
|
if minisix.PY2:
|
|
|
|
text = text.decode('utf-8')
|
|
|
|
text = ircutils.stripColor(text)
|
2005-01-25 21:04:14 +01:00
|
|
|
L = [self._color(c) for c in text]
|
2015-08-31 17:52:45 +02:00
|
|
|
if minisix.PY2:
|
|
|
|
L = [c.encode('utf-8') for c in L]
|
2005-01-25 21:04:14 +01:00
|
|
|
irc.reply('%s%s' % (''.join(L), '\x03'))
|
|
|
|
colorize = wrap(colorize, ['text'])
|
|
|
|
|
2010-10-17 14:50:31 +02:00
|
|
|
@internationalizeDocstring
|
2005-01-25 21:04:14 +01:00
|
|
|
def rainbow(self, irc, msg, args, text):
|
|
|
|
"""<text>
|
|
|
|
|
|
|
|
Returns <text> colorized like a rainbow.
|
|
|
|
"""
|
2015-08-09 00:23:03 +02:00
|
|
|
if minisix.PY2:
|
2013-07-30 19:35:45 +02:00
|
|
|
text = text.decode('utf-8')
|
2015-08-31 17:52:45 +02:00
|
|
|
text = ircutils.stripColor(text)
|
2014-12-21 08:17:27 +01:00
|
|
|
colors = utils.iter.cycle(['05', '04', '07', '08', '09', '03', '11',
|
|
|
|
'10', '12', '02', '06', '13'])
|
2014-01-20 15:13:01 +01:00
|
|
|
L = [self._color(c, fg=next(colors)) for c in text]
|
2015-08-09 00:23:03 +02:00
|
|
|
if minisix.PY2:
|
2013-07-30 19:35:45 +02:00
|
|
|
L = [c.encode('utf-8') for c in L]
|
2005-01-25 21:04:14 +01:00
|
|
|
irc.reply(''.join(L) + '\x03')
|
|
|
|
rainbow = wrap(rainbow, ['text'])
|
|
|
|
|
2010-10-17 14:50:31 +02:00
|
|
|
@internationalizeDocstring
|
2005-01-25 21:04:14 +01:00
|
|
|
def stripcolor(self, irc, msg, args, text):
|
|
|
|
"""<text>
|
|
|
|
|
|
|
|
Returns <text> stripped of all color codes.
|
|
|
|
"""
|
|
|
|
irc.reply(ircutils.stripColor(text))
|
|
|
|
stripcolor = wrap(stripcolor, ['text'])
|
|
|
|
|
2010-10-17 14:50:31 +02:00
|
|
|
@internationalizeDocstring
|
2005-01-25 21:04:14 +01:00
|
|
|
def aol(self, irc, msg, args, text):
|
|
|
|
"""<text>
|
|
|
|
|
2014-12-05 06:47:46 +01:00
|
|
|
Returns <text> as if an AOL user had said it.
|
2005-01-25 21:04:14 +01:00
|
|
|
"""
|
|
|
|
text = text.replace(' you ', ' u ')
|
|
|
|
text = text.replace(' are ', ' r ')
|
|
|
|
text = text.replace(' love ', ' <3 ')
|
|
|
|
text = text.replace(' luv ', ' <3 ')
|
|
|
|
text = text.replace(' too ', ' 2 ')
|
|
|
|
text = text.replace(' to ', ' 2 ')
|
|
|
|
text = text.replace(' two ', ' 2 ')
|
|
|
|
text = text.replace('fore', '4')
|
|
|
|
text = text.replace(' for ', ' 4 ')
|
|
|
|
text = text.replace('be', 'b')
|
|
|
|
text = text.replace('four', ' 4 ')
|
|
|
|
text = text.replace(' their ', ' there ')
|
|
|
|
text = text.replace(', ', ' ')
|
|
|
|
text = text.replace(',', ' ')
|
|
|
|
text = text.replace("'", '')
|
|
|
|
text = text.replace('one', '1')
|
2005-01-31 16:22:48 +01:00
|
|
|
smiley = utils.iter.choice(['<3', ':)', ':-)', ':D', ':-D'])
|
2005-01-25 21:04:14 +01:00
|
|
|
text += smiley*3
|
|
|
|
irc.reply(text)
|
|
|
|
aol = wrap(aol, ['text'])
|
|
|
|
|
2010-10-17 14:50:31 +02:00
|
|
|
@internationalizeDocstring
|
2005-01-25 21:04:14 +01:00
|
|
|
def jeffk(self, irc, msg, args, text):
|
|
|
|
"""<text>
|
|
|
|
|
|
|
|
Returns <text> as if JeffK had said it himself.
|
|
|
|
"""
|
|
|
|
def randomlyPick(L):
|
2005-01-31 16:22:48 +01:00
|
|
|
return utils.iter.choice(L)
|
2005-01-25 21:04:14 +01:00
|
|
|
def quoteOrNothing(m):
|
|
|
|
return randomlyPick(['"', '']).join(m.groups())
|
|
|
|
def randomlyReplace(s, probability=0.5):
|
|
|
|
def f(m):
|
|
|
|
if random.random() < probability:
|
|
|
|
return m.expand(s)
|
|
|
|
else:
|
|
|
|
return m.group(0)
|
|
|
|
return f
|
|
|
|
def randomExclaims(m):
|
|
|
|
if random.random() < 0.85:
|
|
|
|
return ('!' * random.randrange(1, 5)) + m.group(1)
|
|
|
|
else:
|
|
|
|
return '.' + m.group(1)
|
|
|
|
def randomlyShuffle(m):
|
|
|
|
L = list(m.groups())
|
|
|
|
random.shuffle(L)
|
|
|
|
return ''.join(L)
|
|
|
|
def lessRandomlyShuffle(m):
|
|
|
|
L = list(m.groups())
|
|
|
|
if random.random() < .4:
|
|
|
|
random.shuffle(L)
|
|
|
|
return ''.join(L)
|
|
|
|
def randomlyLaugh(text, probability=.3):
|
|
|
|
if random.random() < probability:
|
|
|
|
if random.random() < .5:
|
2005-01-31 16:22:48 +01:00
|
|
|
insult = utils.iter.choice([' fagot1', ' fagorts',
|
|
|
|
' jerks', 'fagot' ' jerk',
|
|
|
|
'dumbshoes', ' dumbshoe'])
|
2005-01-25 21:04:14 +01:00
|
|
|
else:
|
|
|
|
insult = ''
|
2005-01-31 16:22:48 +01:00
|
|
|
laugh1 = utils.iter.choice(['ha', 'hah', 'lol', 'l0l', 'ahh'])
|
|
|
|
laugh2 = utils.iter.choice(['ha', 'hah', 'lol', 'l0l', 'ahh'])
|
2005-01-25 21:04:14 +01:00
|
|
|
laugh1 = laugh1 * random.randrange(1, 5)
|
|
|
|
laugh2 = laugh2 * random.randrange(1, 5)
|
2005-01-31 16:22:48 +01:00
|
|
|
exclaim = utils.iter.choice(['!', '~', '!~', '~!!~~',
|
|
|
|
'!!~', '~~~!'])
|
|
|
|
exclaim += utils.iter.choice(['!', '~', '!~', '~!!~~',
|
2005-01-25 21:04:14 +01:00
|
|
|
'!!~', '~~~!'])
|
2005-01-31 16:22:48 +01:00
|
|
|
if random.random() < 0.5:
|
|
|
|
exclaim += utils.iter.choice(['!', '~', '!~', '~!!~~',
|
|
|
|
'!!~', '~~~!'])
|
2005-01-25 21:04:14 +01:00
|
|
|
laugh = ''.join([' ', laugh1, laugh2, insult, exclaim])
|
|
|
|
text += laugh
|
|
|
|
return text
|
|
|
|
if random.random() < .03:
|
|
|
|
irc.reply(randomlyLaugh('NO YUO', probability=1))
|
|
|
|
return
|
|
|
|
alwaysInsertions = {
|
|
|
|
r'er\b': 'ar',
|
|
|
|
r'\bthe\b': 'teh',
|
|
|
|
r'\byou\b': 'yuo',
|
|
|
|
r'\bis\b': 'si',
|
|
|
|
r'\blike\b': 'liek',
|
|
|
|
r'[^e]ing\b': 'eing',
|
|
|
|
}
|
2015-08-08 22:20:14 +02:00
|
|
|
for (r, s) in alwaysInsertions.items():
|
2005-01-25 21:04:14 +01:00
|
|
|
text = re.sub(r, s, text)
|
|
|
|
randomInsertions = {
|
|
|
|
r'i': 'ui',
|
|
|
|
r'le\b': 'al',
|
|
|
|
r'i': 'io',
|
|
|
|
r'l': 'll',
|
|
|
|
r'to': 'too',
|
|
|
|
r'that': 'taht',
|
|
|
|
r'[^s]c([ei])': r'sci\1',
|
|
|
|
r'ed\b': r'e',
|
|
|
|
r'\band\b': 'adn',
|
|
|
|
r'\bhere\b': 'hear',
|
|
|
|
r'\bthey\'re': 'their',
|
|
|
|
r'\bthere\b': 'they\'re',
|
|
|
|
r'\btheir\b': 'there',
|
|
|
|
r'[^e]y': 'ey',
|
|
|
|
}
|
2015-08-08 22:20:14 +02:00
|
|
|
for (r, s) in randomInsertions.items():
|
2005-01-25 21:04:14 +01:00
|
|
|
text = re.sub(r, randomlyReplace(s), text)
|
|
|
|
text = re.sub(r'(\w)\'(\w)', quoteOrNothing, text)
|
|
|
|
text = re.sub(r'\.(\s+|$)', randomExclaims, text)
|
|
|
|
text = re.sub(r'([aeiou])([aeiou])', randomlyShuffle, text)
|
|
|
|
text = re.sub(r'([bcdfghkjlmnpqrstvwxyz])([bcdfghkjlmnpqrstvwxyz])',
|
|
|
|
lessRandomlyShuffle, text)
|
|
|
|
text = randomlyLaugh(text)
|
|
|
|
if random.random() < .4:
|
|
|
|
text = text.upper()
|
|
|
|
irc.reply(text)
|
|
|
|
jeffk = wrap(jeffk, ['text'])
|
|
|
|
|
|
|
|
# Keeping these separate so people can just replace the alphabets for
|
|
|
|
# whatever their language of choice
|
|
|
|
_spellLetters = {
|
2010-10-17 14:50:31 +02:00
|
|
|
'a': _('ay'), 'b': _('bee'), 'c': _('see'), 'd': _('dee'),
|
|
|
|
'e': _('ee'), 'f': _('eff'), 'g': _('gee'), 'h': _('aych'),
|
|
|
|
'i': _('eye'), 'j': _('jay'), 'k': _('kay'), 'l': _('ell'),
|
|
|
|
'm': _('em'), 'n': _('en'), 'o': _('oh'), 'p': _('pee'), 'q': _('cue'),
|
|
|
|
'r': _('arr'), 's': _('ess'), 't': _('tee'), 'u': _('you'),
|
|
|
|
'v': _('vee'), 'w': _('double-you'), 'x': _('ecks'), 'y': _('why'),
|
|
|
|
'z': _('zee')
|
2005-01-25 21:04:14 +01:00
|
|
|
}
|
2015-08-08 22:20:14 +02:00
|
|
|
for (k, v) in list(_spellLetters.items()):
|
2005-01-25 21:04:14 +01:00
|
|
|
_spellLetters[k.upper()] = v
|
|
|
|
_spellPunctuation = {
|
2010-10-17 14:50:31 +02:00
|
|
|
'!': _('exclamation point'),
|
|
|
|
'"': _('quote'),
|
|
|
|
'#': _('pound'),
|
|
|
|
'$': _('dollar sign'),
|
|
|
|
'%': _('percent'),
|
|
|
|
'&': _('ampersand'),
|
|
|
|
'\'': _('single quote'),
|
|
|
|
'(': _('left paren'),
|
|
|
|
')': _('right paren'),
|
|
|
|
'*': _('asterisk'),
|
|
|
|
'+': _('plus'),
|
|
|
|
',': _('comma'),
|
|
|
|
'-': _('minus'),
|
|
|
|
'.': _('period'),
|
|
|
|
'/': _('slash'),
|
|
|
|
':': _('colon'),
|
|
|
|
';': _('semicolon'),
|
|
|
|
'<': _('less than'),
|
|
|
|
'=': _('equals'),
|
|
|
|
'>': _('greater than'),
|
|
|
|
'?': _('question mark'),
|
|
|
|
'@': _('at'),
|
|
|
|
'[': _('left bracket'),
|
|
|
|
'\\': _('backslash'),
|
|
|
|
']': _('right bracket'),
|
|
|
|
'^': _('caret'),
|
|
|
|
'_': _('underscore'),
|
|
|
|
'`': _('backtick'),
|
|
|
|
'{': _('left brace'),
|
|
|
|
'|': _('pipe'),
|
|
|
|
'}': _('right brace'),
|
|
|
|
'~': _('tilde')
|
2005-01-25 21:04:14 +01:00
|
|
|
}
|
|
|
|
_spellNumbers = {
|
2010-10-17 14:50:31 +02:00
|
|
|
'0': _('zero'), '1': _('one'), '2': _('two'), '3': _('three'),
|
|
|
|
'4': _('four'), '5': _('five'), '6': _('six'), '7': _('seven'),
|
|
|
|
'8': _('eight'), '9': _('nine')
|
2005-01-25 21:04:14 +01:00
|
|
|
}
|
2010-10-17 14:50:31 +02:00
|
|
|
@internationalizeDocstring
|
2005-01-25 21:04:14 +01:00
|
|
|
def spellit(self, irc, msg, args, text):
|
|
|
|
"""<text>
|
|
|
|
|
|
|
|
Returns <text>, phonetically spelled out.
|
|
|
|
"""
|
|
|
|
d = {}
|
|
|
|
if self.registryValue('spellit.replaceLetters'):
|
|
|
|
d.update(self._spellLetters)
|
|
|
|
if self.registryValue('spellit.replaceNumbers'):
|
|
|
|
d.update(self._spellNumbers)
|
|
|
|
if self.registryValue('spellit.replacePunctuation'):
|
|
|
|
d.update(self._spellPunctuation)
|
|
|
|
# A bug in unicode on OSX prevents me from testing this.
|
|
|
|
## dd = {}
|
2015-08-08 22:20:14 +02:00
|
|
|
## for (c, v) in d.items():
|
2005-01-25 21:04:14 +01:00
|
|
|
## dd[ord(c)] = unicode(v + ' ')
|
|
|
|
## irc.reply(unicode(text).translate(dd))
|
2015-08-10 17:55:25 +02:00
|
|
|
out = minisix.io.StringIO()
|
2005-01-25 21:04:14 +01:00
|
|
|
write = out.write
|
|
|
|
for c in text:
|
|
|
|
try:
|
|
|
|
c = d[c]
|
|
|
|
write(' ')
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
write(c)
|
2015-01-03 02:55:24 +01:00
|
|
|
irc.reply(out.getvalue().strip())
|
2005-01-25 21:04:14 +01:00
|
|
|
spellit = wrap(spellit, ['text'])
|
|
|
|
|
2010-10-17 14:50:31 +02:00
|
|
|
@internationalizeDocstring
|
2005-01-25 21:04:14 +01:00
|
|
|
def gnu(self, irc, msg, args, text):
|
|
|
|
"""<text>
|
|
|
|
|
|
|
|
Returns <text> as GNU/RMS would say it.
|
|
|
|
"""
|
|
|
|
irc.reply(' '.join(['GNU/' + s for s in text.split()]))
|
|
|
|
gnu = wrap(gnu, ['text'])
|
|
|
|
|
2010-10-17 14:50:31 +02:00
|
|
|
@internationalizeDocstring
|
2005-01-25 21:04:14 +01:00
|
|
|
def shrink(self, irc, msg, args, text):
|
|
|
|
"""<text>
|
|
|
|
|
|
|
|
Returns <text> with each word longer than
|
|
|
|
supybot.plugins.Filter.shrink.minimum being shrunken (i.e., like
|
|
|
|
"internationalization" becomes "i18n").
|
|
|
|
"""
|
|
|
|
L = []
|
2019-08-24 17:50:05 +02:00
|
|
|
minimum = self.registryValue('shrink.minimum',
|
|
|
|
msg.channel, irc.network)
|
2005-01-25 21:04:14 +01:00
|
|
|
r = re.compile(r'[A-Za-z]{%s,}' % minimum)
|
|
|
|
def shrink(m):
|
|
|
|
s = m.group(0)
|
|
|
|
return ''.join((s[0], str(len(s)-2), s[-1]))
|
|
|
|
text = r.sub(shrink, text)
|
|
|
|
irc.reply(text)
|
|
|
|
shrink = wrap(shrink, ['text'])
|
|
|
|
|
2007-12-05 20:00:41 +01:00
|
|
|
|
2009-08-22 21:03:47 +02:00
|
|
|
# TODO: 2,4,;
|
|
|
|
# XXX suckiest: B,K,P,Q,T
|
|
|
|
# alternatives: 3: U+2107
|
2007-12-05 20:00:41 +01:00
|
|
|
_uniudMap = {
|
2015-08-10 18:52:51 +02:00
|
|
|
' ': ' ', '0': '0', '@': '@',
|
|
|
|
'!': '\u00a1', '1': '1', 'A': '\u2200',
|
|
|
|
'"': '\u201e', '2': '\u2681', 'B': 'q',
|
|
|
|
'#': '#', '3': '\u0190', 'C': '\u0186',
|
|
|
|
'$': '$', '4': '\u2683', 'D': '\u15e1',
|
|
|
|
'%': '%', '5': '\u1515', 'E': '\u018e',
|
|
|
|
'&': '\u214b', '6': '9', 'F': '\u2132',
|
|
|
|
"'": '\u0375', '7': 'L', 'G': '\u2141',
|
|
|
|
'(': ')', '8': '8', 'H': 'H',
|
|
|
|
')': '(', '9': '6', 'I': 'I',
|
|
|
|
'*': '*', ':': ':', 'J': '\u148b',
|
|
|
|
'+': '+', ';': ';', 'K': '\u029e',
|
|
|
|
',': '\u2018', '<': '>', 'L': '\u2142',
|
|
|
|
'-': '-', '=': '=', 'M': '\u019c',
|
|
|
|
'.': '\u02d9', '>': '<', 'N': 'N',
|
|
|
|
'/': '/', '?': '\u00bf', 'O': 'O',
|
|
|
|
|
|
|
|
'P': 'd', '`': '\u02ce', 'p': 'd',
|
|
|
|
'Q': 'b', 'a': '\u0250', 'q': 'b',
|
|
|
|
'R': '\u1d1a', 'b': 'q', 'r': '\u0279',
|
|
|
|
'S': 'S', 'c': '\u0254', 's': 's',
|
|
|
|
'T': '\u22a5', 'd': 'p', 't': '\u0287',
|
|
|
|
'U': '\u144e', 'e': '\u01dd', '': 'n',
|
|
|
|
'V': '\u039b', 'f': '\u214e', 'v': '\u028c',
|
|
|
|
'W': 'M', 'g': '\u0253', 'w': '\u028d',
|
|
|
|
'X': 'X', 'h': '\u0265', 'x': 'x',
|
|
|
|
'Y': '\u2144', 'i': '\u1d09', 'y': '\u028e',
|
|
|
|
'Z': 'Z', 'j': '\u027f', 'z': 'z',
|
|
|
|
'[': ']', 'k': '\u029e', '{': '}',
|
|
|
|
'\\': '\\', 'l': '\u05df', '|': '|',
|
|
|
|
']': '[', 'm': '\u026f', '}': '{',
|
|
|
|
'^': '\u2335', 'n': '', '~': '~',
|
|
|
|
'_': '\u203e', 'o': 'o',
|
2007-12-05 20:00:41 +01:00
|
|
|
}
|
2009-08-22 21:03:47 +02:00
|
|
|
|
2010-10-17 14:50:31 +02:00
|
|
|
@internationalizeDocstring
|
2007-12-05 20:00:41 +01:00
|
|
|
def uniud(self, irc, msg, args, text):
|
|
|
|
"""<text>
|
|
|
|
|
2009-08-22 21:03:47 +02:00
|
|
|
Returns <text> rotated 180 degrees. Only really works for ASCII
|
|
|
|
printable characters.
|
2007-12-05 20:00:41 +01:00
|
|
|
"""
|
|
|
|
turned = []
|
|
|
|
tlen = 0
|
|
|
|
for c in text:
|
|
|
|
if c in self._uniudMap:
|
|
|
|
tmp = self._uniudMap[c]
|
|
|
|
if not len(tmp):
|
2015-08-10 19:14:22 +02:00
|
|
|
tmp = '\ufffd'
|
2014-05-15 18:45:19 +02:00
|
|
|
turned.append(tmp)
|
2007-12-05 20:00:41 +01:00
|
|
|
tlen += 1
|
|
|
|
elif c == '\t':
|
|
|
|
tablen = 8 - tlen % 8
|
2014-05-15 18:45:19 +02:00
|
|
|
turned.append(' ' * tablen)
|
2007-12-05 20:00:41 +01:00
|
|
|
tlen += tablen
|
|
|
|
elif ord(c) >= 32:
|
2014-05-15 18:45:19 +02:00
|
|
|
turned.append(c)
|
2007-12-05 20:00:41 +01:00
|
|
|
tlen += 1
|
2015-08-10 19:14:22 +02:00
|
|
|
s = '%s \x02 \x02' % ''.join(reversed(turned))
|
2007-12-05 20:00:41 +01:00
|
|
|
irc.reply(s)
|
|
|
|
uniud = wrap(uniud, ['text'])
|
2015-10-28 02:14:10 +01:00
|
|
|
|
|
|
|
def capwords(self, irc, msg, args, text):
|
|
|
|
"""<text>
|
|
|
|
|
|
|
|
Capitalises the first letter of each word.
|
|
|
|
"""
|
|
|
|
text = string.capwords(text)
|
|
|
|
irc.reply(text)
|
|
|
|
capwords = wrap(capwords, ['text'])
|
|
|
|
|
|
|
|
def caps(self, irc, msg, args, text):
|
|
|
|
"""<text>
|
|
|
|
|
|
|
|
EVERYONE LOVES CAPS LOCK.
|
|
|
|
"""
|
|
|
|
irc.reply(text.upper())
|
|
|
|
caps = wrap(caps, ['text'])
|
|
|
|
|
|
|
|
_vowelrottrans = utils.str.MultipleReplacer(dict(list(zip('aeiouAEIOU', 'eiouaEIOUA'))))
|
|
|
|
def vowelrot(self, irc, msg, args, text):
|
|
|
|
"""<text>
|
|
|
|
|
|
|
|
Returns <text> with vowels rotated
|
|
|
|
"""
|
|
|
|
text = self._vowelrottrans(text)
|
|
|
|
irc.reply(text)
|
|
|
|
vowelrot = wrap(vowelrot, ['text'])
|
2020-05-23 12:06:55 +02:00
|
|
|
|
|
|
|
|
2020-05-23 13:32:26 +02:00
|
|
|
_uwutrans = utils.str.MultipleReplacer(dict(list(zip('lrLR', 'wwWW'))))
|
2020-05-23 12:06:55 +02:00
|
|
|
def uwu(self, irc, msg, args, text):
|
|
|
|
"""<text>
|
|
|
|
|
|
|
|
Returns <text> in uwu-speak.
|
|
|
|
"""
|
2020-05-23 13:32:26 +02:00
|
|
|
text = self._uwutrans(text)
|
2020-05-23 12:06:55 +02:00
|
|
|
text += random.choice([''] * 10 + [' uwu', ' UwU', ' owo', ' OwO'])
|
|
|
|
irc.reply(text)
|
|
|
|
uwu = wrap(uwu, ['text'])
|
|
|
|
|
2010-10-26 09:32:12 +02:00
|
|
|
Filter = internationalizeDocstring(Filter)
|
2005-01-25 21:04:14 +01:00
|
|
|
|
|
|
|
Class = Filter
|
|
|
|
|
|
|
|
|
2006-02-11 16:52:51 +01:00
|
|
|
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79:
|