2003-03-12 07:26:59 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
###
|
|
|
|
# Copyright (c) 2002, 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.
|
|
|
|
###
|
|
|
|
|
2003-03-25 07:53:51 +01:00
|
|
|
"""
|
|
|
|
Provides a multitude of fun, useless commands.
|
|
|
|
"""
|
|
|
|
|
2003-11-25 09:20:41 +01:00
|
|
|
__revision__ = "$Id$"
|
|
|
|
|
2003-10-05 14:56:56 +02:00
|
|
|
import plugins
|
2003-03-12 07:26:59 +01:00
|
|
|
|
|
|
|
import gc
|
|
|
|
import re
|
2003-04-02 09:27:00 +02:00
|
|
|
import sys
|
2003-03-12 07:26:59 +01:00
|
|
|
import md5
|
|
|
|
import sha
|
|
|
|
import random
|
|
|
|
import urllib
|
|
|
|
import inspect
|
|
|
|
import mimetypes
|
2003-11-15 05:37:04 +01:00
|
|
|
from itertools import imap
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-10-20 23:15:06 +02:00
|
|
|
import conf
|
2003-04-03 11:11:57 +02:00
|
|
|
import utils
|
2003-10-20 23:15:06 +02:00
|
|
|
import ircmsgs
|
2003-10-11 23:03:02 +02:00
|
|
|
import ircutils
|
2003-03-12 07:26:59 +01:00
|
|
|
import privmsgs
|
|
|
|
import callbacks
|
|
|
|
|
2003-10-20 23:15:06 +02:00
|
|
|
class MyFunProxy(object):
|
|
|
|
def reply(self, msg, s):
|
|
|
|
self.s = s
|
|
|
|
|
2003-10-19 16:53:34 +02:00
|
|
|
class Fun(callbacks.Privmsg):
|
2003-10-20 23:15:06 +02:00
|
|
|
def __init__(self):
|
2003-10-25 00:43:48 +02:00
|
|
|
self.outFilters = ircutils.IrcDict()
|
2003-10-20 23:15:06 +02:00
|
|
|
callbacks.Privmsg.__init__(self)
|
|
|
|
|
2003-12-04 00:56:06 +01:00
|
|
|
def ping(self, irc, msg, args):
|
|
|
|
"""takes no arguments
|
|
|
|
|
|
|
|
Checks to see if the bot is alive.
|
|
|
|
"""
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply('pong', prefixName=False)
|
2004-01-07 17:09:44 +01:00
|
|
|
|
2003-08-28 00:43:29 +02:00
|
|
|
def hexip(self, irc, msg, args):
|
|
|
|
"""<ip>
|
|
|
|
|
|
|
|
Returns the hexadecimal IP for that IP.
|
|
|
|
"""
|
|
|
|
ip = privmsgs.getArgs(args)
|
2004-01-16 18:33:51 +01:00
|
|
|
if not utils.isIP(ip):
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.error('%r is not a valid IP.' % ip)
|
2003-08-28 00:43:29 +02:00
|
|
|
return
|
|
|
|
quads = ip.split('.')
|
|
|
|
ret = ""
|
|
|
|
for quad in quads:
|
|
|
|
i = int(quad)
|
|
|
|
ret += '%02x' % i
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply(ret.upper())
|
2003-08-28 00:43:29 +02:00
|
|
|
|
2003-03-15 12:18:28 +01:00
|
|
|
def ord(self, irc, msg, args):
|
|
|
|
"""<letter>
|
2003-03-15 11:00:49 +01:00
|
|
|
|
2003-03-15 12:18:28 +01:00
|
|
|
Returns the 8-bit value of <letter>.
|
|
|
|
"""
|
|
|
|
letter = privmsgs.getArgs(args)
|
|
|
|
if len(letter) != 1:
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.error('Letter must be of length 1 (for obvious reasons)')
|
2003-03-15 12:18:28 +01:00
|
|
|
else:
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply(str(ord(letter)))
|
2003-03-15 11:00:49 +01:00
|
|
|
|
2003-03-15 12:18:28 +01:00
|
|
|
def chr(self, irc, msg, args):
|
|
|
|
"""<number>
|
2003-03-15 11:00:49 +01:00
|
|
|
|
2003-03-15 12:18:28 +01:00
|
|
|
Returns the character associated with the 8-bit value <number>
|
|
|
|
"""
|
|
|
|
try:
|
2003-03-15 13:00:31 +01:00
|
|
|
i = privmsgs.getArgs(args)
|
|
|
|
if i.startswith('0x'):
|
|
|
|
base = 16
|
|
|
|
elif i.startswith('0b'):
|
|
|
|
base = 2
|
|
|
|
i = i[2:]
|
|
|
|
elif i.startswith('0'):
|
|
|
|
base = 8
|
|
|
|
else:
|
|
|
|
base = 10
|
|
|
|
i = int(i, base)
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply(chr(i))
|
2003-03-15 12:18:28 +01:00
|
|
|
except ValueError:
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.error('That number doesn\'t map to an 8-bit character.')
|
2003-08-20 18:26:23 +02:00
|
|
|
|
2003-07-31 08:43:19 +02:00
|
|
|
def encode(self, irc, msg, args):
|
|
|
|
"""<encoding> <text>
|
|
|
|
|
|
|
|
Returns an encoded form of the given text; the valid encodings are
|
2003-09-06 10:39:50 +02:00
|
|
|
available in the documentation of the Python codecs module:
|
2004-01-09 20:01:54 +01:00
|
|
|
<http://www.python.org/doc/lib/node127.html>.
|
2003-07-31 08:43:19 +02:00
|
|
|
"""
|
2003-11-11 14:20:06 +01:00
|
|
|
encoding, text = privmsgs.getArgs(args, required=2)
|
2003-11-18 00:10:14 +01:00
|
|
|
try:
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply(text.encode(encoding))
|
2003-11-18 00:10:14 +01:00
|
|
|
except LookupError:
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.error('There is no such encoding %r' % encoding)
|
2003-07-31 08:43:19 +02:00
|
|
|
|
|
|
|
def decode(self, irc, msg, args):
|
|
|
|
"""<encoding> <text>
|
|
|
|
|
|
|
|
Returns an un-encoded form of the given text; the valid encodings are
|
2003-09-06 10:39:50 +02:00
|
|
|
available in the documentation of the Python codecs module:
|
2004-01-09 20:01:54 +01:00
|
|
|
<http://www.python.org/doc/lib/node127.html>.
|
2003-07-31 08:43:19 +02:00
|
|
|
"""
|
2003-11-11 14:20:06 +01:00
|
|
|
encoding, text = privmsgs.getArgs(args, required=2)
|
2003-11-18 00:10:14 +01:00
|
|
|
try:
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply(text.decode(encoding).encode('utf-8'))
|
2003-11-18 00:10:14 +01:00
|
|
|
except LookupError:
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.error('There is no such encoding %r' % encoding)
|
2003-08-20 18:26:23 +02:00
|
|
|
|
2003-03-12 07:26:59 +01:00
|
|
|
def xor(self, irc, msg, args):
|
2003-04-16 10:26:58 +02:00
|
|
|
"""<password> <text>
|
|
|
|
|
|
|
|
Returns <text> XOR-encrypted with <password>. See
|
|
|
|
http://www.yoe.org/developer/xor.html for information about XOR
|
|
|
|
encryption.
|
|
|
|
"""
|
2003-03-12 07:26:59 +01:00
|
|
|
(password, text) = privmsgs.getArgs(args, 2)
|
|
|
|
passwordlen = len(password)
|
|
|
|
i = 0
|
|
|
|
ret = []
|
|
|
|
for c in text:
|
|
|
|
ret.append(chr(ord(c) ^ ord(password[i])))
|
|
|
|
i = (i + 1) % passwordlen
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply(''.join(ret))
|
2003-03-12 07:26:59 +01:00
|
|
|
|
|
|
|
def mimetype(self, irc, msg, args):
|
2003-04-16 10:26:58 +02:00
|
|
|
"""<filename>
|
|
|
|
|
|
|
|
Returns the mime type associated with <filename>
|
|
|
|
"""
|
2003-03-12 07:26:59 +01:00
|
|
|
filename = privmsgs.getArgs(args)
|
|
|
|
(type, encoding) = mimetypes.guess_type(filename)
|
|
|
|
if type is not None:
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply(type)
|
2003-03-12 07:26:59 +01:00
|
|
|
else:
|
|
|
|
s = 'I couldn\'t figure out that filename.'
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply(s)
|
2003-03-12 07:26:59 +01:00
|
|
|
|
|
|
|
def md5(self, irc, msg, args):
|
2003-04-16 10:26:58 +02:00
|
|
|
"""<text>
|
|
|
|
|
|
|
|
Returns the md5 hash of a given string. Read
|
|
|
|
http://www.rsasecurity.com/rsalabs/faq/3-6-6.html for more information
|
|
|
|
about md5.
|
|
|
|
"""
|
2003-03-12 07:26:59 +01:00
|
|
|
text = privmsgs.getArgs(args)
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply(md5.md5(text).hexdigest())
|
2003-03-12 07:26:59 +01:00
|
|
|
|
|
|
|
def sha(self, irc, msg, args):
|
2003-04-16 10:26:58 +02:00
|
|
|
"""<text>
|
|
|
|
|
|
|
|
Returns the SHA hash of a given string. Read
|
|
|
|
http://www.secure-hash-algorithm-md5-sha-1.co.uk/ for more information
|
|
|
|
about SHA.
|
|
|
|
"""
|
2003-03-12 07:26:59 +01:00
|
|
|
text = privmsgs.getArgs(args)
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply(sha.sha(text).hexdigest())
|
2003-03-12 07:26:59 +01:00
|
|
|
|
|
|
|
def urlquote(self, irc, msg, args):
|
2003-04-16 10:26:58 +02:00
|
|
|
"""<text>
|
|
|
|
|
|
|
|
Returns the URL quoted form of the text.
|
|
|
|
"""
|
2003-03-12 07:26:59 +01:00
|
|
|
text = privmsgs.getArgs(args)
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply(urllib.quote(text))
|
2003-03-12 07:26:59 +01:00
|
|
|
|
|
|
|
def urlunquote(self, irc, msg, args):
|
2003-04-16 10:26:58 +02:00
|
|
|
"""<text>
|
|
|
|
|
|
|
|
Returns the text un-URL quoted.
|
|
|
|
"""
|
2003-03-12 07:26:59 +01:00
|
|
|
text = privmsgs.getArgs(args)
|
|
|
|
s = urllib.unquote(text)
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply(s)
|
2003-03-12 07:26:59 +01:00
|
|
|
|
|
|
|
def coin(self, irc, msg, args):
|
2003-04-16 10:26:58 +02:00
|
|
|
"""takes no arguments
|
|
|
|
|
|
|
|
Flips a coin and returns the result.
|
|
|
|
"""
|
2003-03-12 07:26:59 +01:00
|
|
|
if random.randrange(0, 2):
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply('heads')
|
2003-03-12 07:26:59 +01:00
|
|
|
else:
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply('tails')
|
2003-03-12 07:26:59 +01:00
|
|
|
|
|
|
|
_dicere = re.compile(r'(\d+)d(\d+)')
|
|
|
|
def dice(self, irc, msg, args):
|
2003-04-16 10:26:58 +02:00
|
|
|
"""<dice>d<sides>
|
|
|
|
|
|
|
|
Rolls a die with <sides> number of sides <dice> times.
|
|
|
|
For example, 2d6 will roll 2 six-sided dice; 10d10 will roll 10
|
|
|
|
ten-sided dice.
|
|
|
|
"""
|
2003-03-12 07:26:59 +01:00
|
|
|
arg = privmsgs.getArgs(args)
|
|
|
|
m = re.match(self._dicere, arg)
|
|
|
|
if m:
|
2003-11-15 05:37:04 +01:00
|
|
|
(dice, sides) = imap(int, m.groups())
|
2003-03-12 07:26:59 +01:00
|
|
|
if dice > 6:
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.error('You can\'t roll more than 6 dice.')
|
2003-03-12 07:26:59 +01:00
|
|
|
elif sides > 100:
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.error('Dice can\'t have more than 100 sides.')
|
2003-03-12 07:26:59 +01:00
|
|
|
else:
|
|
|
|
L = [0] * dice
|
|
|
|
for i in xrange(dice):
|
|
|
|
L[i] = random.randrange(1, sides+1)
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply(utils.commaAndify([str(x) for x in L]))
|
2003-03-12 07:26:59 +01:00
|
|
|
else:
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.error('Dice must be of the form <dice>d<sides>')
|
2003-03-12 07:26:59 +01:00
|
|
|
|
|
|
|
def objects(self, irc, msg, args):
|
2003-04-16 10:26:58 +02:00
|
|
|
"""takes no arguments.
|
|
|
|
|
2003-10-19 16:49:38 +02:00
|
|
|
Returns the number and types of Python objects in memory.
|
|
|
|
"""
|
2003-08-07 11:19:40 +02:00
|
|
|
classes = 0
|
|
|
|
functions = 0
|
|
|
|
modules = 0
|
2003-11-25 09:20:41 +01:00
|
|
|
strings = 0
|
2003-08-07 11:19:40 +02:00
|
|
|
dicts = 0
|
|
|
|
lists = 0
|
|
|
|
tuples = 0
|
2003-08-11 07:06:41 +02:00
|
|
|
refcounts = 0
|
2003-03-12 07:26:59 +01:00
|
|
|
objs = gc.get_objects()
|
2003-08-07 11:19:40 +02:00
|
|
|
for obj in objs:
|
2003-11-25 09:20:41 +01:00
|
|
|
if isinstance(obj, str):
|
|
|
|
strings += 1
|
2003-08-07 11:19:40 +02:00
|
|
|
if isinstance(obj, tuple):
|
|
|
|
tuples += 1
|
|
|
|
elif inspect.isroutine(obj):
|
|
|
|
functions += 1
|
|
|
|
elif isinstance(obj, dict):
|
|
|
|
dicts += 1
|
|
|
|
elif isinstance(obj, list):
|
|
|
|
lists += 1
|
|
|
|
elif inspect.isclass(obj):
|
|
|
|
classes += 1
|
|
|
|
elif inspect.ismodule(obj):
|
|
|
|
modules += 1
|
2003-08-11 07:06:41 +02:00
|
|
|
refcounts += sys.getrefcount(obj)
|
2003-03-12 07:26:59 +01:00
|
|
|
response = 'I have %s objects: %s modules, %s classes, %s functions, '\
|
2003-11-25 09:20:41 +01:00
|
|
|
'%s dictionaries, %s lists, %s tuples, %s strings, and a ' \
|
|
|
|
'few other odds and ends. ' \
|
2004-01-09 00:03:48 +01:00
|
|
|
'I have a total of %s references.' % \
|
2003-03-12 07:26:59 +01:00
|
|
|
(len(objs), modules, classes, functions,
|
2003-11-25 09:20:41 +01:00
|
|
|
dicts, lists, tuples, strings, refcounts)
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply(response)
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-04-04 17:49:24 +02:00
|
|
|
def levenshtein(self, irc, msg, args):
|
|
|
|
"""<string1> <string2>
|
|
|
|
|
|
|
|
Returns the levenshtein distance (also known as the "edit distance"
|
|
|
|
between <string1> and <string2>
|
|
|
|
"""
|
2003-11-11 14:20:06 +01:00
|
|
|
(s1, s2) = privmsgs.getArgs(args, required=2)
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply(str(utils.distance(s1, s2)))
|
2003-04-04 17:49:24 +02:00
|
|
|
|
2003-04-05 13:23:28 +02:00
|
|
|
def soundex(self, irc, msg, args):
|
|
|
|
"""<string> [<length>]
|
|
|
|
|
|
|
|
Returns the Soundex hash to a given length. The length defaults to
|
|
|
|
4, since that's the standard length for a soundex hash. For unlimited
|
|
|
|
length, use 0.
|
|
|
|
"""
|
|
|
|
(s, length) = privmsgs.getArgs(args, optional=1)
|
|
|
|
if length:
|
2003-10-22 22:09:45 +02:00
|
|
|
try:
|
|
|
|
length = int(length)
|
|
|
|
except ValueError:
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.error('%r isn\'t a valid length.' % length)
|
2003-10-22 22:09:45 +02:00
|
|
|
return
|
2003-04-05 13:23:28 +02:00
|
|
|
else:
|
|
|
|
length = 4
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply(utils.soundex(s, length))
|
2003-04-05 13:23:28 +02:00
|
|
|
|
2003-09-07 07:26:18 +02:00
|
|
|
_eightballs = (
|
2003-08-29 09:49:11 +02:00
|
|
|
'outlook not so good.',
|
|
|
|
'my reply is no.',
|
|
|
|
'don\'t count on it.',
|
|
|
|
'you may rely on it.',
|
|
|
|
'ask again later.',
|
|
|
|
'most likely.',
|
|
|
|
'cannot predict now.',
|
|
|
|
'yes.',
|
|
|
|
'yes, most definitely.',
|
|
|
|
'better not tell you now.',
|
|
|
|
'it is certain.',
|
|
|
|
'very doubtful.',
|
|
|
|
'it is decidedly so.',
|
|
|
|
'concentrate and ask again.',
|
|
|
|
'signs point to yes.',
|
|
|
|
'my sources say no.',
|
|
|
|
'without a doubt.',
|
|
|
|
'reply hazy, try again.',
|
|
|
|
'as I see it, yes.',
|
2003-09-07 07:26:18 +02:00
|
|
|
)
|
2003-08-29 09:49:11 +02:00
|
|
|
def eightball(self, irc, msg, args):
|
|
|
|
"""[<question>]
|
|
|
|
|
|
|
|
Asks the magic eightball a question.
|
|
|
|
"""
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply(random.choice(self._eightballs))
|
2003-08-29 09:49:11 +02:00
|
|
|
|
2003-12-03 02:43:38 +01:00
|
|
|
def roulette(self, irc, msg, args):
|
|
|
|
"""takes no arguments.
|
|
|
|
|
|
|
|
Randomly picks a number from 1 to 6 and if it comes up 1, you get
|
|
|
|
kicked. Otherwise -- *click*
|
|
|
|
"""
|
|
|
|
nick = msg.nick
|
|
|
|
channel = msg.args[0]
|
2003-12-03 02:55:19 +01:00
|
|
|
if not ircutils.isChannel(channel):
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.error('This message must be sent in a channel.')
|
2003-12-03 02:43:38 +01:00
|
|
|
if random.randint(1, 6) == 1:
|
2004-01-31 08:31:11 +01:00
|
|
|
if irc.nick in irc.state.channels[channel].ops:
|
2004-01-30 22:41:52 +01:00
|
|
|
irc.queueMsg(ircmsgs.kick(channel, nick, 'BANG!'))
|
2004-01-31 08:31:11 +01:00
|
|
|
else:
|
|
|
|
irc.reply('*BANG* Hey, who put a blank in here?!')
|
2003-12-03 02:43:38 +01:00
|
|
|
else:
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply('*click*')
|
2003-12-03 02:43:38 +01:00
|
|
|
|
|
|
|
|
2003-10-19 16:53:34 +02:00
|
|
|
Class = Fun
|
2003-04-03 10:24:52 +02:00
|
|
|
|
2003-03-24 09:41:19 +01:00
|
|
|
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|