2013-01-19 18:37:14 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
"""
|
|
|
|
rutils.py - Phenny Utility Module
|
|
|
|
Copyright 2012, Sfan5
|
|
|
|
"""
|
2013-08-14 11:59:08 +02:00
|
|
|
import base64, binascii, re, random, time, multiprocessing
|
2013-01-19 18:37:14 +01:00
|
|
|
|
2013-06-01 18:44:40 +02:00
|
|
|
def rs(s):
|
|
|
|
return repr(s)[1:-1]
|
|
|
|
|
2013-01-19 18:37:14 +01:00
|
|
|
def rev(phenny, input):
|
|
|
|
"""reverse string"""
|
2013-02-14 21:27:04 +01:00
|
|
|
for x in phenny.bot.commands["high"].values():
|
|
|
|
if x[0].__name__ == "aa_hook":
|
|
|
|
if x[0](phenny, input):
|
|
|
|
return # Abort function
|
2013-01-19 18:37:14 +01:00
|
|
|
if not input.group(2):
|
|
|
|
return phenny.reply("Nothing to reverse.")
|
|
|
|
q = input.group(2).encode('utf-8')
|
|
|
|
s = ""
|
|
|
|
for i in range(1,len(q)):
|
|
|
|
s += q[-i]
|
|
|
|
s += q[0]
|
2013-06-01 18:44:40 +02:00
|
|
|
return phenny.say(rs(s))
|
2013-01-19 18:37:14 +01:00
|
|
|
|
2013-04-06 13:26:56 +02:00
|
|
|
rev.commands = ['rev','reverse']
|
2013-01-19 18:37:14 +01:00
|
|
|
rev.priority = 'low'
|
|
|
|
|
|
|
|
def b64e(phenny, input):
|
|
|
|
"""base64 encode"""
|
2013-02-14 21:27:04 +01:00
|
|
|
for x in phenny.bot.commands["high"].values():
|
|
|
|
if x[0].__name__ == "aa_hook":
|
|
|
|
if x[0](phenny, input):
|
|
|
|
return # Abort function
|
2013-01-19 18:37:14 +01:00
|
|
|
if not input.group(2):
|
|
|
|
return phenny.reply("Nothing to encode.")
|
|
|
|
q = input.group(2).encode('utf-8')
|
|
|
|
try:
|
2013-06-01 18:44:40 +02:00
|
|
|
return phenny.say(rs(base64.b64encode(q)))
|
2013-01-19 18:37:14 +01:00
|
|
|
except BaseException as e:
|
2013-07-19 21:35:21 +02:00
|
|
|
return phenny.reply("Failed to handle data")
|
2013-01-19 18:37:14 +01:00
|
|
|
|
|
|
|
b64e.commands = ['b64e','base64encode']
|
|
|
|
b64e.priority = 'low'
|
|
|
|
|
|
|
|
def b64d(phenny, input):
|
|
|
|
"""base64 decode"""
|
2013-02-14 21:27:04 +01:00
|
|
|
for x in phenny.bot.commands["high"].values():
|
|
|
|
if x[0].__name__ == "aa_hook":
|
|
|
|
if x[0](phenny, input):
|
|
|
|
return # Abort function
|
2013-01-19 18:37:14 +01:00
|
|
|
if not input.group(2):
|
|
|
|
return phenny.reply("Nothing to decode.")
|
|
|
|
q = input.group(2).encode('utf-8')
|
|
|
|
try:
|
2013-06-01 18:44:40 +02:00
|
|
|
return phenny.say(rs(base64.b64decode(q)))
|
2013-01-19 18:37:14 +01:00
|
|
|
except BaseException as e:
|
2013-07-19 21:35:21 +02:00
|
|
|
return phenny.reply("Failed to handle data")
|
2013-01-19 18:37:14 +01:00
|
|
|
|
|
|
|
b64d.commands = ['b64d','base64decode']
|
|
|
|
b64d.priority = 'low'
|
|
|
|
|
|
|
|
def b32e(phenny, input):
|
|
|
|
"""base32 encode"""
|
2013-02-14 21:27:04 +01:00
|
|
|
for x in phenny.bot.commands["high"].values():
|
|
|
|
if x[0].__name__ == "aa_hook":
|
|
|
|
if x[0](phenny, input):
|
|
|
|
return # Abort function
|
2013-01-19 18:37:14 +01:00
|
|
|
if not input.group(2):
|
|
|
|
return phenny.reply("Nothing to encode.")
|
|
|
|
q = input.group(2).encode('utf-8')
|
|
|
|
try:
|
2013-06-01 18:44:40 +02:00
|
|
|
return phenny.say(rs(base64.b32encode(q)))
|
2013-01-19 18:37:14 +01:00
|
|
|
except BaseException as e:
|
2013-07-19 21:35:21 +02:00
|
|
|
return phenny.reply("Failed to handle data")
|
2013-01-19 18:37:14 +01:00
|
|
|
|
|
|
|
b32e.commands = ['b32e','base32encode']
|
|
|
|
b32e.priority = 'low'
|
|
|
|
|
|
|
|
def b32d(phenny, input):
|
|
|
|
"""base32 decode"""
|
2013-02-14 21:27:04 +01:00
|
|
|
for x in phenny.bot.commands["high"].values():
|
|
|
|
if x[0].__name__ == "aa_hook":
|
|
|
|
if x[0](phenny, input):
|
|
|
|
return # Abort function
|
2013-01-19 18:37:14 +01:00
|
|
|
if not input.group(2):
|
|
|
|
return phenny.reply("Nothing to decode.")
|
|
|
|
q = input.group(2).encode('utf-8')
|
|
|
|
try:
|
2013-06-01 18:44:40 +02:00
|
|
|
return phenny.say(rs(base64.b32decode(q)))
|
2013-01-19 18:37:14 +01:00
|
|
|
except BaseException as e:
|
2013-07-19 21:35:21 +02:00
|
|
|
return phenny.reply("Failed to handle data")
|
2013-01-19 18:37:14 +01:00
|
|
|
|
|
|
|
b32d.commands = ['b32d','base32decode']
|
|
|
|
b32d.priority = 'low'
|
|
|
|
|
|
|
|
def b16e(phenny, input):
|
|
|
|
"""base16 encode"""
|
2013-02-14 21:27:04 +01:00
|
|
|
for x in phenny.bot.commands["high"].values():
|
|
|
|
if x[0].__name__ == "aa_hook":
|
|
|
|
if x[0](phenny, input):
|
|
|
|
return # Abort function
|
2013-01-19 18:37:14 +01:00
|
|
|
if not input.group(2):
|
|
|
|
return phenny.reply("Nothing to encode.")
|
|
|
|
q = input.group(2).encode('utf-8')
|
|
|
|
try:
|
2013-06-01 18:44:40 +02:00
|
|
|
return phenny.say(rs(base64.b16encode(q)))
|
2013-01-19 18:37:14 +01:00
|
|
|
except BaseException as e:
|
2013-07-19 21:35:21 +02:00
|
|
|
return phenny.reply("Failed to handle data")
|
2013-01-19 18:37:14 +01:00
|
|
|
|
|
|
|
b16e.commands = ['b16e','base16encode']
|
|
|
|
b16e.priority = 'low'
|
|
|
|
|
|
|
|
def b16d(phenny, input):
|
|
|
|
"""base16 decode"""
|
|
|
|
if not input.group(2):
|
|
|
|
return phenny.reply("Nothing to decode.")
|
|
|
|
q = input.group(2).encode('utf-8')
|
|
|
|
try:
|
2013-06-01 18:44:40 +02:00
|
|
|
return phenny.say(rs(base64.b16decode(q)))
|
2013-01-19 18:37:14 +01:00
|
|
|
except BaseException as e:
|
2013-07-19 21:35:21 +02:00
|
|
|
return phenny.reply("Failed to handle data")
|
2013-01-19 18:37:14 +01:00
|
|
|
|
|
|
|
b16d.commands = ['b16d','base16decode']
|
|
|
|
b16d.priority = 'low'
|
|
|
|
|
|
|
|
def crc32(phenny, input):
|
|
|
|
"""crc32 hash"""
|
2013-02-14 21:27:04 +01:00
|
|
|
for x in phenny.bot.commands["high"].values():
|
|
|
|
if x[0].__name__ == "aa_hook":
|
|
|
|
if x[0](phenny, input):
|
|
|
|
return # Abort function
|
2013-01-19 18:37:14 +01:00
|
|
|
if not input.group(2):
|
|
|
|
return phenny.reply("Nothing to hash.")
|
|
|
|
q = input.group(2).encode('utf-8')
|
|
|
|
h = binascii.crc32(q)
|
2013-06-01 18:44:40 +02:00
|
|
|
return phenny.say(rs(str(h) + "(" + hex(h) + ")"))
|
2013-01-19 18:37:14 +01:00
|
|
|
|
|
|
|
crc32.commands = ['crc32']
|
|
|
|
crc32.priority = 'low'
|
|
|
|
|
|
|
|
def hex_(phenny, input):
|
|
|
|
"""hexlify http://docs.python.org/2/library/binascii.html#binascii.hexlify"""
|
2013-02-14 21:27:04 +01:00
|
|
|
for x in phenny.bot.commands["high"].values():
|
|
|
|
if x[0].__name__ == "aa_hook":
|
|
|
|
if x[0](phenny, input):
|
|
|
|
return # Abort function
|
2013-01-19 18:37:14 +01:00
|
|
|
if not input.group(2):
|
|
|
|
return phenny.reply("Nothing to hexlify.")
|
|
|
|
q = input.group(2).encode('utf-8')
|
|
|
|
try:
|
2013-06-01 18:44:40 +02:00
|
|
|
return phenny.say(rs(binascii.hexlify(q)))
|
2013-01-19 18:37:14 +01:00
|
|
|
except BaseException as e:
|
2013-07-19 21:35:21 +02:00
|
|
|
return phenny.reply("Failed to handle data")
|
2013-01-19 18:37:14 +01:00
|
|
|
|
|
|
|
hex_.commands = ['hex']
|
|
|
|
hex_.priority = 'low'
|
|
|
|
|
|
|
|
def unhex(phenny, input):
|
|
|
|
"""unhexlify http://docs.python.org/2/library/binascii.html#binascii.unhexlify"""
|
2013-02-14 21:27:04 +01:00
|
|
|
for x in phenny.bot.commands["high"].values():
|
|
|
|
if x[0].__name__ == "aa_hook":
|
|
|
|
if x[0](phenny, input):
|
|
|
|
return # Abort function
|
2013-01-19 18:37:14 +01:00
|
|
|
if not input.group(2):
|
|
|
|
return phenny.reply("Nothing to unhexlify.")
|
|
|
|
q = input.group(2).encode('utf-8')
|
|
|
|
try:
|
2013-06-01 18:44:40 +02:00
|
|
|
return phenny.say(rs(binascii.unhexlify(q)))
|
2013-01-19 18:37:14 +01:00
|
|
|
except BaseException as e:
|
2013-07-19 21:35:21 +02:00
|
|
|
return phenny.reply("Failed to handle data")
|
2013-01-19 18:37:14 +01:00
|
|
|
|
|
|
|
unhex.commands = ['unhex']
|
|
|
|
unhex.priority = 'low'
|
|
|
|
|
|
|
|
def uuencode(phenny, input):
|
|
|
|
"""uuencode"""
|
2013-02-14 21:27:04 +01:00
|
|
|
for x in phenny.bot.commands["high"].values():
|
|
|
|
if x[0].__name__ == "aa_hook":
|
|
|
|
if x[0](phenny, input):
|
|
|
|
return # Abort function
|
2013-01-19 18:37:14 +01:00
|
|
|
if not input.group(2):
|
|
|
|
return phenny.reply("Nothing to encode.")
|
|
|
|
q = input.group(2).encode('utf-8')
|
|
|
|
try:
|
2013-06-01 18:44:40 +02:00
|
|
|
return phenny.say(rs(binascii.b2a_uu(q)))
|
2013-01-19 18:37:14 +01:00
|
|
|
except BaseException as e:
|
2013-07-19 21:35:21 +02:00
|
|
|
return phenny.reply("Failed to handle data")
|
2013-01-19 18:37:14 +01:00
|
|
|
|
|
|
|
uuencode.commands = ['ue','uuencode']
|
|
|
|
uuencode.priority = 'low'
|
|
|
|
|
|
|
|
def uudecode(phenny, input):
|
|
|
|
"""uudecode"""
|
2013-02-14 21:27:04 +01:00
|
|
|
for x in phenny.bot.commands["high"].values():
|
|
|
|
if x[0].__name__ == "aa_hook":
|
|
|
|
if x[0](phenny, input):
|
|
|
|
return # Abort function
|
2013-01-19 18:37:14 +01:00
|
|
|
if not input.group(2):
|
|
|
|
return phenny.reply("Nothing to decode.")
|
|
|
|
q = input.group(2).encode('utf-8')
|
|
|
|
try:
|
2013-06-01 18:44:40 +02:00
|
|
|
return phenny.say(rs(binascii.a2b_uu(q + '\n')))
|
2013-01-19 18:37:14 +01:00
|
|
|
except BaseException as e:
|
2013-07-19 21:35:21 +02:00
|
|
|
return phenny.reply("Failed to handle data")
|
2013-01-19 18:37:14 +01:00
|
|
|
|
|
|
|
uudecode.commands = ['ud','uudecode']
|
|
|
|
uudecode.priority = 'low'
|
|
|
|
|
2013-06-01 19:17:52 +02:00
|
|
|
def regex(phenny, input):
|
|
|
|
"""regex"""
|
|
|
|
for x in phenny.bot.commands["high"].values():
|
|
|
|
if x[0].__name__ == "aa_hook":
|
|
|
|
if x[0](phenny, input):
|
|
|
|
return # Abort function
|
|
|
|
if not input.group(2):
|
2013-08-14 11:59:08 +02:00
|
|
|
return phenny.reply("Give me a regex and a message seperated by `")
|
2013-06-01 19:17:52 +02:00
|
|
|
q = input.group(2).encode('utf-8')
|
2013-06-02 20:53:44 +02:00
|
|
|
rgx = q[:q.find("`")]
|
|
|
|
txt = q[q.find("`")+1:]
|
2013-06-01 19:54:38 +02:00
|
|
|
if rgx == "" or txt == "":
|
2013-06-02 20:53:44 +02:00
|
|
|
return phenny.reply("Give me a regex and a message seperated by `")
|
2013-06-01 19:17:52 +02:00
|
|
|
try:
|
|
|
|
r = re.compile(rgx)
|
|
|
|
except BaseException as e:
|
|
|
|
return phenny.reply("Failed to compile regex: " + e.message)
|
2013-08-14 11:59:08 +02:00
|
|
|
q = multiprocessing.Queue()
|
|
|
|
def compute(q, re, rgx, txt):
|
|
|
|
q.put(list(e.groups()[0] for e in list(re.finditer(rgx, txt))))
|
|
|
|
t = multiprocessing.Process(target=compute, args=(q,re,rgx,txt))
|
|
|
|
t.start()
|
|
|
|
t.join(3.0)
|
|
|
|
if t.is_alive():
|
|
|
|
phenny.reply("Regex took to long to compute")
|
|
|
|
t.terminate()
|
|
|
|
return
|
|
|
|
m = q.get()
|
2013-06-01 19:54:38 +02:00
|
|
|
if m == []:
|
2013-06-01 19:17:52 +02:00
|
|
|
return phenny.say("false")
|
|
|
|
else:
|
2013-06-01 19:54:38 +02:00
|
|
|
return phenny.say("true groups=[" + ', '.join((repr(e) for e in m)) + "]")
|
2013-06-01 19:17:52 +02:00
|
|
|
|
|
|
|
regex.commands = ['re','regex']
|
|
|
|
regex.priority = 'low'
|
2013-08-14 11:59:08 +02:00
|
|
|
regex.thread = True
|
2013-06-01 19:17:52 +02:00
|
|
|
|
2013-07-09 16:30:36 +02:00
|
|
|
def rand(phenny, input):
|
|
|
|
"""Returns a random number"""
|
|
|
|
for x in phenny.bot.commands["high"].values():
|
|
|
|
if x[0].__name__ == "aa_hook":
|
|
|
|
if x[0](phenny, input):
|
|
|
|
return # Abort function
|
|
|
|
if not input.group(2):
|
|
|
|
return
|
|
|
|
arg = input.group(2)
|
|
|
|
if " " in arg:
|
|
|
|
try:
|
|
|
|
a = int(arg.split(" ")[0])
|
|
|
|
except ValueError:
|
|
|
|
return phenny.reply("Could not parse argument 1")
|
|
|
|
try:
|
|
|
|
b = int(arg.split(" ")[1]) + 1
|
|
|
|
except ValueError:
|
|
|
|
return phenny.reply("Could not parse argument 2")
|
2013-08-14 12:59:48 +02:00
|
|
|
if b < a:
|
|
|
|
tmp = a
|
|
|
|
a = b
|
|
|
|
b = tmp
|
|
|
|
del tmp
|
2013-07-15 08:57:10 +02:00
|
|
|
phenny.say(str(random.randrange(a, b)))
|
2013-07-09 16:30:36 +02:00
|
|
|
else:
|
|
|
|
try:
|
|
|
|
a = int(arg.split(" ")[0]) + 1
|
|
|
|
except ValueError:
|
|
|
|
return phenny.reply("Could not parse argument 1")
|
2013-07-15 08:57:10 +02:00
|
|
|
phenny.say(str(random.randrange(a)))
|
2013-07-09 16:30:36 +02:00
|
|
|
|
|
|
|
rand.commands = ['rand', 'random']
|
|
|
|
rand.priority = 'low'
|
|
|
|
|
2013-01-19 18:37:14 +01:00
|
|
|
if __name__ == '__main__':
|
|
|
|
print __doc__.strip()
|