# 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
                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:
            user.sendMessage(irc.ERR_NEEDMOREPARAMS, "AUTHENTICATE", ":Not enough parameters")
            return {}
        if "accountname" in user.metadata["ext"]:
            user.sendMessage(irc.ERR_SASLALREADYAUTHED, ":You have already authenticated")
            return {}
        return {
            "user": user,
            "authentication": params[0]
        }
    
    def checkInProgress(self, user):
        if "sasl_authenticating" in user.cache:
            del user.cache["sasl_authenticating"]
            user.sendMessage(irc.ERR_SASLABORTED, ":SASL authentication aborted")
        return True
    
    def sendSuccess(self, user):
        user.sendMessage(irc.RPL_SASLACCOUNT, "{}!{}@{}".format(user.nickname if user.nickname else "unknown", user.username if user.username else "unknown", user.hostname), user.metadata["ext"]["accountname"], ":You are now logged in as {}".format(user.metadata["ext"]["accountname"]))
        user.sendMessage(irc.RPL_SASLSUCCESS, ":SASL authentication successful")
        del user.cache["sasl_authenticating"]
        if user.server != self.ircd.name:

# 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

                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:
            user.sendMessage(irc.ERR_NEEDMOREPARAMS, "AUTHENTICATE", ":Not enough parameters")
            return {}
        if "accountname" in user.metadata["ext"]:
            user.sendMessage(irc.ERR_SASLALREADYAUTHED, ":You have already authenticated")
            return {}
        return {
            "user": user,
            "authentication": params[0]
        }
    
    def checkInProgress(self, user):
        if "sasl_authenticating" in user.cache:
            del user.cache["sasl_authenticating"]
            user.sendMessage(irc.ERR_SASLABORTED, ":SASL authentication aborted")
        return True
    
    def sendSuccess(self, user):
        user.sendMessage(irc.RPL_SASLACCOUNT, "{}!{}@{}".format(user.nickname if user.nickname else "unknown", user.username if user.username else "unknown", user.hostname), user.metadata["ext"]["accountname"], ":You are now logged in as {}".format(user.metadata["ext"]["accountname"]))
        user.sendMessage(irc.RPL_SASLSUCCESS, ":SASL authentication successful")
        del user.cache["sasl_authenticating"]
        if user.server != self.ircd.name:

            self.ircd.servers[user.server].callRemote(ModuleMessage, destserver=user.server, type="SASL", args=["release", user.uuid])