# 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/server.py
# class ModuleMessage(Command):
#     arguments = [
#         ("destserver", String()),
#         ("type", String()),
#         ("args", ListOf(String()))
#     ]
#     errors = {
#         HandshakeNotYetComplete: "HANDSHAKE_NOT_COMPLETE"
#     }


from twisted.words.protocols import irc
from txircd.modbase import Command
from txircd.server import ModuleMessage
    def capClear(self, user, capability):
        return True
    
    def onUse(self, user, data):
        if "sasl_authenticating" not in user.cache:
            mechanism = data["authentication"].upper()
            user.cache["sasl_authenticating"] = [mechanism, ""]
            if "server_sasl_agent" in self.ircd.servconfig and self.ircd.servconfig["server_sasl_agent"]:
                if self.ircd.servconfig["server_sasl_agent"] not in self.ircd.servers:
                    del user.cache["sasl_authenticating"]
                    user.sendMessage(irc.ERR_SASLFAILED, ":SASL authentication failed")
                else:
                    server = self.ircd.servers[self.ircd.servconfig["server_sasl_agent"]]
                    server.callRemote(ModuleMessage, destserver=server.name, type="SASL", args=["start", user.uuid, mechanism])
            elif "sasl_agent" in self.ircd.module_data_cache:
                self.startSaslAuth(user, mechanism)
            else:
                del user.cache["sasl_authenticating"]
                user.sendMessage(irc.ERR_SASLFAILED, ":SASL authentication failed")
        else:
            if "server_sasl_agent" in self.ircd.servconfig and self.ircd.servconfig["server_sasl_agent"]:
                server = self.ircd.servers[self.ircd.servconfig["server_sasl_agent"]]
                server.callRemote(ModuleMessage, destserver=server.name, type="SASL", args=["process", user.uuid, data["authentication"]])
            else:
                self.processSaslAuth(user, data["authentication"])
    
    def processParams(self, user, params):
        if user.registered == 0:
            user.sendMessage(irc.ERR_ALREADYREGISTRED, ":You may not reregister")
            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/server.py
# class ModuleMessage(Command):
#     arguments = [
#         ("destserver", String()),
#         ("type", String()),
#         ("args", ListOf(String()))
#     ]
#     errors = {
#         HandshakeNotYetComplete: "HANDSHAKE_NOT_COMPLETE"
#     }


# Path: txircd/modules/ircv3_sasl.py
from twisted.words.protocols import irc
from txircd.modbase import Command
from txircd.server import ModuleMessage

    def capClear(self, user, capability):
        return True
    
    def onUse(self, user, data):
        if "sasl_authenticating" not in user.cache:
            mechanism = data["authentication"].upper()
            user.cache["sasl_authenticating"] = [mechanism, ""]
            if "server_sasl_agent" in self.ircd.servconfig and self.ircd.servconfig["server_sasl_agent"]:
                if self.ircd.servconfig["server_sasl_agent"] not in self.ircd.servers:
                    del user.cache["sasl_authenticating"]
                    user.sendMessage(irc.ERR_SASLFAILED, ":SASL authentication failed")
                else:
                    server = self.ircd.servers[self.ircd.servconfig["server_sasl_agent"]]
                    server.callRemote(ModuleMessage, destserver=server.name, type="SASL", args=["start", user.uuid, mechanism])
            elif "sasl_agent" in self.ircd.module_data_cache:
                self.startSaslAuth(user, mechanism)
            else:
                del user.cache["sasl_authenticating"]
                user.sendMessage(irc.ERR_SASLFAILED, ":SASL authentication failed")
        else:
            if "server_sasl_agent" in self.ircd.servconfig and self.ircd.servconfig["server_sasl_agent"]:
                server = self.ircd.servers[self.ircd.servconfig["server_sasl_agent"]]
                server.callRemote(ModuleMessage, destserver=server.name, type="SASL", args=["process", user.uuid, data["authentication"]])
            else:
                self.processSaslAuth(user, data["authentication"])
    
    def processParams(self, user, params):
        if user.registered == 0:
            user.sendMessage(irc.ERR_ALREADYREGISTRED, ":You may not reregister")
            return {}

        if not params: