# Path: txircd/modbase.py
# class Command(object):
#     def hook(self, base):
#         self.ircd = base
#         return self
#     def onUse(self, user, data):
#         pass
#     def processParams(self, user, params):
#         return {
#             "user": user,
#             "params": params
#         }
#     def updateActivity(self, user):
#         user.lastactivity = now()
# 
# Path: txircd/utils.py
# VALID_NICKNAME = re.compile(r"[a-zA-Z\[\]\\`_^{}\|][a-zA-Z0-9-\[\]\\`_^{}\|]{0,31}$") # up to 32 char nicks
# 
# def irc_lower(str):
#     return str.lower().replace("[","{").replace("]","}").replace("/","|").replace("~","^")


from twisted.words.protocols import irc
from txircd.modbase import Command
from txircd.utils import VALID_NICKNAME, irc_lower

class NickCommand(Command):
    def onUse(self, user, data):
        if user.registered == 0:
            user.nick(data["nick"])
        else:
            if not user.nickname:
                user.registered -= 1
            user.nickname = str(data["nick"])
            if user.registered == 0:
                user.register()
    
    def processParams(self, user, params):
        if not params:
            user.sendMessage(irc.ERR_NONICKNAMEGIVEN, ":No nickname given")
            return {}
        if not params[0]:
            user.sendMessage(irc.ERR_ERRONEUSNICKNAME, "*", ":Erroneous nickname")
            return {}
        if not VALID_NICKNAME.match(params[0]):
            user.sendMessage(irc.ERR_ERRONEUSNICKNAME, params[0], ":Erroneous nickname")
            return {}
        if params[0] == user.nickname:
            return {}

# Path: txircd/modbase.py
# class Command(object):
#     def hook(self, base):
#         self.ircd = base
#         return self
#     def onUse(self, user, data):
#         pass
#     def processParams(self, user, params):
#         return {
#             "user": user,
#             "params": params
#         }
#     def updateActivity(self, user):
#         user.lastactivity = now()
# 
# Path: txircd/utils.py
# VALID_NICKNAME = re.compile(r"[a-zA-Z\[\]\\`_^{}\|][a-zA-Z0-9-\[\]\\`_^{}\|]{0,31}$") # up to 32 char nicks
# 
# def irc_lower(str):
#     return str.lower().replace("[","{").replace("]","}").replace("/","|").replace("~","^")


# Path: txircd/modules/cmd_nick.py
from twisted.words.protocols import irc
from txircd.modbase import Command
from txircd.utils import VALID_NICKNAME, irc_lower


class NickCommand(Command):
    def onUse(self, user, data):
        if user.registered == 0:
            user.nick(data["nick"])
        else:
            if not user.nickname:
                user.registered -= 1
            user.nickname = str(data["nick"])
            if user.registered == 0:
                user.register()
    
    def processParams(self, user, params):
        if not params:
            user.sendMessage(irc.ERR_NONICKNAMEGIVEN, ":No nickname given")
            return {}
        if not params[0]:
            user.sendMessage(irc.ERR_ERRONEUSNICKNAME, "*", ":Erroneous nickname")
            return {}
        if not VALID_NICKNAME.match(params[0]):
            user.sendMessage(irc.ERR_ERRONEUSNICKNAME, params[0], ":Erroneous nickname")
            return {}
        if params[0] == user.nickname:
            return {}

        if params[0] in self.ircd.users and (not user.nickname or irc_lower(params[0]) != irc_lower(user.nickname)):