# 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()
# 
# class Module(object):
#     def hook(self, base):
#         self.ircd = base
#         return self


from twisted.words.protocols import irc
from txircd.modbase import Command, Module
    def onUse(self, user, data):
        if "server_password" in self.ircd.servconfig and self.ircd.servconfig["server_password"] and not user.password:
            user.registered -= 1
        user.password = data["password"]
        if user.registered == 0:
            user.register()
    
    def processParams(self, user, params):
        if user.registered == 0:
            user.sendMessage(irc.ERR_ALREADYREGISTRED, ":Unauthorized command (already registered)")
            return {}
        if not params:
            user.sendMessage(irc.ERR_NEEDMOREPARAMS, "PASS", ":Not enough parameters")
            return {}
        return {
            "user": user,
            "password": params[0]
        }
    
    def onConnect(self, user):
        if "server_password" in self.ircd.servconfig and self.ircd.servconfig["server_password"]:
            user.registered += 1 # Make password a required step in registration
        return True
    
    def onRegister(self, user):
        if "server_password" in self.ircd.servconfig and self.ircd.servconfig["server_password"] and self.ircd.servconfig["server_password"] != user.password:
            user.sendMessage("ERROR", ":Closing link: ({}@{}) [Access denied]".format(user.username, user.hostname), to=None, prefix=None)
            return False
        return True


# 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()
# 
# class Module(object):
#     def hook(self, base):
#         self.ircd = base
#         return self


# Path: txircd/modules/cmd_pass.py
from twisted.words.protocols import irc
from txircd.modbase import Command, Module

    def onUse(self, user, data):
        if "server_password" in self.ircd.servconfig and self.ircd.servconfig["server_password"] and not user.password:
            user.registered -= 1
        user.password = data["password"]
        if user.registered == 0:
            user.register()
    
    def processParams(self, user, params):
        if user.registered == 0:
            user.sendMessage(irc.ERR_ALREADYREGISTRED, ":Unauthorized command (already registered)")
            return {}
        if not params:
            user.sendMessage(irc.ERR_NEEDMOREPARAMS, "PASS", ":Not enough parameters")
            return {}
        return {
            "user": user,
            "password": params[0]
        }
    
    def onConnect(self, user):
        if "server_password" in self.ircd.servconfig and self.ircd.servconfig["server_password"]:
            user.registered += 1 # Make password a required step in registration
        return True
    
    def onRegister(self, user):
        if "server_password" in self.ircd.servconfig and self.ircd.servconfig["server_password"] and self.ircd.servconfig["server_password"] != user.password:
            user.sendMessage("ERROR", ":Closing link: ({}@{}) [Access denied]".format(user.username, user.hostname), to=None, prefix=None)
            return False
        return True


class Spawner(object):