2013-10-01 03:26:40 +13:00
import hashlib
2013-10-01 10:03:46 +13:00
import collections
import re
2014-02-14 16:36:57 +13:00
from util import hook , text
2013-10-01 10:03:46 +13:00
# variables
colors = collections . OrderedDict ( [
2014-02-14 17:03:08 +13:00
( ' red ' , ' \x03 04 ' ) ,
( ' orange ' , ' \x03 07 ' ) ,
( ' yellow ' , ' \x03 08 ' ) ,
( ' green ' , ' \x03 09 ' ) ,
( ' cyan ' , ' \x03 03 ' ) ,
( ' ltblue ' , ' \x03 10 ' ) ,
( ' rylblue ' , ' \x03 12 ' ) ,
( ' blue ' , ' \x03 02 ' ) ,
( ' magenta ' , ' \x03 06 ' ) ,
( ' pink ' , ' \x03 13 ' ) ,
( ' maroon ' , ' \x03 05 ' )
2013-10-01 10:03:46 +13:00
] )
# helper functions
strip_re = re . compile ( " ( \x03 | \x02 | \x1f )(?:,? \ d { 1,2}(?:, \ d { 1,2})?)? " , re . UNICODE )
2013-11-12 07:06:06 +01:00
2014-02-13 15:02:44 +13:00
def strip ( string ) :
return strip_re . sub ( ' ' , string )
2013-10-01 03:26:40 +13:00
2014-02-14 17:03:08 +13:00
2013-10-01 03:26:40 +13:00
# basic text tools
2013-11-12 07:06:06 +01:00
2013-10-01 03:32:06 +13:00
## TODO: make this capitalize sentences correctly
@hook.command ( " capitalise " )
@hook.command
def capitalize ( inp ) :
""" capitalize <string> -- Capitalizes <string>. """
return inp . capitalize ( )
2013-11-12 07:06:06 +01:00
2013-10-01 03:26:40 +13:00
@hook.command
def upper ( inp ) :
""" upper <string> -- Convert string to uppercase. """
return inp . upper ( )
2013-11-12 07:06:06 +01:00
2013-10-01 03:26:40 +13:00
@hook.command
def lower ( inp ) :
""" lower <string> -- Convert string to lowercase. """
return inp . lower ( )
2013-11-12 07:06:06 +01:00
2013-10-01 03:26:40 +13:00
@hook.command
def titlecase ( inp ) :
""" title <string> -- Convert string to title case. """
return inp . title ( )
2013-11-12 07:06:06 +01:00
2013-10-01 03:32:06 +13:00
@hook.command
def swapcase ( inp ) :
""" swapcase <string> -- Swaps the capitalization of <string>. """
return inp . swapcase ( )
2014-02-14 17:03:08 +13:00
2013-10-01 03:26:40 +13:00
# encoding
2013-11-12 07:06:06 +01:00
2013-10-01 03:26:40 +13:00
@hook.command
def rot13 ( inp ) :
""" rot13 <string> -- Encode <string> with rot13. """
return inp . encode ( ' rot13 ' )
2013-11-12 07:06:06 +01:00
2013-10-01 11:40:04 +13:00
@hook.command
def base64 ( inp ) :
""" base64 <string> -- Encode <string> with base64. """
return inp . encode ( ' base64 ' )
2013-11-12 07:06:06 +01:00
2013-10-01 11:40:04 +13:00
@hook.command
def unbase64 ( inp ) :
""" unbase64 <string> -- Decode <string> with base64. """
return inp . decode ( ' base64 ' )
2013-11-12 07:06:06 +01:00
2013-10-01 11:40:04 +13:00
@hook.command
def checkbase64 ( inp ) :
try :
decoded = inp . decode ( ' base64 ' )
recoded = decoded . encode ( ' base64 ' ) . strip ( )
is_base64 = recoded == inp
except :
2014-02-14 00:47:01 +13:00
return ' " {} " is not base64 encoded ' . format ( inp )
2013-10-01 11:40:04 +13:00
if is_base64 :
return ' " {} " is base64 encoded ' . format ( recoded )
else :
return ' " {} " is not base64 encoded ' . format ( inp )
2013-11-12 07:06:06 +01:00
2013-10-01 03:42:55 +13:00
@hook.command
def unescape ( inp ) :
""" unescape <string> -- Unescapes <string>. """
2013-10-01 11:40:04 +13:00
try :
return inp . decode ( ' unicode-escape ' )
except Exception as e :
return " Error: {} " . format ( e )
2013-10-01 03:42:55 +13:00
2013-11-12 07:06:06 +01:00
2013-10-01 03:42:55 +13:00
@hook.command
def escape ( inp ) :
2013-10-01 11:40:04 +13:00
""" escape <string> -- Escapes <string>. """
try :
return inp . encode ( ' unicode-escape ' )
except Exception as e :
return " Error: {} " . format ( e )
2013-10-01 03:42:55 +13:00
2014-02-14 17:03:08 +13:00
2013-10-01 03:26:40 +13:00
# length
2013-11-12 07:06:06 +01:00
2013-10-01 03:26:40 +13:00
@hook.command
def length ( inp ) :
""" length <string> -- gets the length of <string> """
return " The length of that string is {} characters. " . format ( len ( inp ) )
2014-02-14 17:03:08 +13:00
2013-10-05 22:49:52 +10:00
# reverse
2013-11-12 07:06:06 +01:00
2013-10-05 22:49:52 +10:00
@hook.command
def reverse ( inp ) :
2013-10-07 01:20:36 +13:00
""" reverse <string> -- reverses <string>. """
2013-10-07 01:12:12 +13:00
return inp [ : : - 1 ]
2013-11-12 07:06:06 +01:00
2014-02-14 17:03:08 +13:00
2013-10-01 03:26:40 +13:00
# hashing
2013-11-12 07:06:06 +01:00
2014-02-14 17:03:08 +13:00
@hook.command ( " hash " )
def hash_command ( inp ) :
2013-10-01 03:26:40 +13:00
""" hash <string> -- Returns hashes of <string>. """
return ' , ' . join ( x + " : " + getattr ( hashlib , x ) ( inp ) . hexdigest ( )
for x in [ ' md5 ' , ' sha1 ' , ' sha256 ' ] )
2013-10-01 04:08:13 +13:00
2014-02-14 17:03:08 +13:00
2013-10-01 04:08:13 +13:00
# novelty
2013-11-12 07:06:06 +01:00
2013-10-01 04:08:13 +13:00
@hook.command
def munge ( inp ) :
""" munge <text> -- Munges up <text>. """
return text . munge ( inp )
2014-02-14 17:03:08 +13:00
2013-10-01 10:03:46 +13:00
# colors - based on code by Reece Selwood - <https://github.com/hitzler/homero>
2013-11-12 07:06:06 +01:00
2013-10-01 10:03:46 +13:00
@hook.command
def rainbow ( inp ) :
inp = unicode ( inp )
inp = strip ( inp )
col = colors . items ( )
out = " "
l = len ( colors )
for i , t in enumerate ( inp ) :
2013-10-04 17:49:02 +08:00
if t == " " :
out + = t
else :
out + = col [ i % l ] [ 1 ] + t
2013-10-01 10:03:46 +13:00
return out
2013-11-12 07:06:06 +01:00
2013-10-01 10:03:46 +13:00
@hook.command
def wrainbow ( inp ) :
inp = unicode ( inp )
col = colors . items ( )
inp = strip ( inp ) . split ( ' ' )
out = [ ]
l = len ( colors )
for i , t in enumerate ( inp ) :
out . append ( col [ i % l ] [ 1 ] + t )
return ' ' . join ( out )
2013-11-12 07:06:06 +01:00
2013-10-01 10:03:46 +13:00
@hook.command
def usa ( inp ) :
inp = strip ( inp )
c = [ colors [ ' red ' ] , ' \x03 00 ' , colors [ ' blue ' ] ]
l = len ( c )
out = ' '
for i , t in enumerate ( inp ) :
out + = c [ i % l ] + t
return out