# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.
#
# File: zraxfuncs.py

__module_name__ = "ZraxFuncs" 
__module_version__ = "0.11" 
__module_description__ = "Zrax's X-Chat Funcs" 

import xchat
import random

# Useful functions:
def rejoin(word, word_eol, userdata):
    xchat.command("part " + word_eol[1])
    xchat.command("join " + word_eol[1])
    return xchat.EAT_NONE

xchat.hook_command("rj", rejoin, help="USAGE: rj <channels>, rejoins the specified channels")

# Useful (somewhat) functions:
def rot13(word, word_eol, userdata):
    r13msg = ""
    for i in range(len(word_eol[1])):
        if (word_eol[1][i] >= 'a' and word_eol[1][i] <= 'm') or (word_eol[1][i] >= 'A' and word_eol[1][i] <= 'M'):
            ch = chr(ord(word_eol[1][i]) + 13)
        elif (word_eol[1][i] >= 'n' and word_eol[1][i] <= 'z') or (word_eol[1][i] >= 'N' and word_eol[1][i] <= 'Z'):
            ch = chr(ord(word_eol[1][i]) - 13)
        else:
            ch = word_eol[1][i]
        r13msg = r13msg + ch
    xchat.command("say " + r13msg)
    return xchat.EAT_ALL
    
xchat.hook_command("rot13", rot13, help="USAGE: rot13 <string>, encodes the input string through ROT13 encryption")

def oocsay(word, word_eol, userdata):
    xchat.command("say " + "<< 02" + word_eol[1] + " >>")
    return xchat.EAT_ALL

xchat.hook_command("ooc", oocsay, help="USAGE: ooc <message>, Formats the message for out-of-character text")


# Useless functions:
def reversestring(word, word_eol, userdata):
    revmsg = ""
    for ch in word_eol[1]:
        revmsg = ch + revmsg
    xchat.command("say " + revmsg)
    return xchat.EAT_ALL

xchat.hook_command("rv", reversestring, help="USAGE: rv <string>, reverses the input string")

def reversestringorder(word, word_eol, userdata):
    revmsg = ""
    for w in word[1:]:
        revmsg = w + " " + revmsg
    xchat.command("say " + revmsg)
    return xchat.EAT_ALL

xchat.hook_command("rvo", reversestringorder, help="USAGE: rvo <string>, reverses the words in input string")

def randomstr(word, word_eol, userdata): # I have no idea why I wrote this...
    rstr = ""
    sstr = word_eol[1]
    for i in range(len(word_eol[1])):
        c = random.randint(0, len(sstr) - 1)
        rstr = rstr + sstr[c]
        sstr = sstr[:c] + sstr[c+1:]
    xchat.command("say " + rstr)
    return xchat.EAT_ALL

xchat.hook_command("randomstr", randomstr)


# User Message Replies and stuff:
#def user_reply(word, word_eol, userdata):
#    h = word[0].find("!")
#    username = word[0][1:h]
#    sstr = word_eol[3].upper()
#    #if sstr.find("WILLIAMSCHIPS") > 0:
#    #    xchat.command("me eats william's chips")
#    #if sstr.find("WILLSCHIPS") > 0:
#    #    xchat.command("me eats will's chips")
#    if sstr.find("COOKIE REQUEST") > 0:
#        xchat.command("me gives " + username + " a delicious delicacy")
#    return xchat.EAT_NONE

#user_reply_func = 0

#xchat.hook_server("PRIVMSG", user_reply)
#def user_reply_set(word, word_eol, userdata):
#    if word[1] == "on" or word[1] == "yes" or word[1] == "1":
#        userdata = xchat.hook_server("PRIVMSG", user_reply)
#        print "ZraxFuncs's User Replies activated!"
#    elif word[1] == "off" or word[1] == "no" or word[1] == "0":
#        xchat.unhook(userdata)
#        print "ZraxFuncs's User Replies deactivated!"
#    return xchat.EAT_ALL
#
#xchat.hook_command("set_user_replies", user_reply_set, userdata=user_reply_func, help="USAGE: set_user_replies <on|off>, toggles the catching and interpreting of ZraxFuncs's user replies")


# Loading and Unloading Messages:
def unload_us(userdata):
    print "ZraxFuncs " + __module_version__ + " unloaded!"

xchat.hook_unload(unload_us)

print "ZraxFuncs " + __module_version__ + " loaded!"
