# 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

irc.ERR_INVALIDCAPCMD = "410" # as defined in http://ircv3.atheme.org/specification/capability-negotiation-3.1

class CapCommand(Command):
    def onUse(self, user, data):
        subcmd = data["subcmd"]
        if user.registered > 0 and "cap_negotiating" not in user.cache:
            user.cache["cap_negotiating"] = True
            user.registered += 1 # On receipt of a CAP command, block registration until CAP END is received
        if subcmd == "LS":
            user.sendMessage("CAP", "LS", ":{}".format(" ".join(self.ircd.module_data_cache["cap"].keys())))
        elif subcmd == "LIST":
            if "cap" not in user.cache:
                user.sendMessage("CAP", "LIST", ":")
            else:
                user.sendMessage("CAP", "LIST", ":{}".format(" ".join(user.cache["cap"])))
        elif subcmd == "REQ":
            ack = []
            nak = []
            for capability in data["list"]:
                if capability:

# 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_cap.py
from twisted.words.protocols import irc
from txircd.modbase import Command
from txircd.server import ModuleMessage


irc.ERR_INVALIDCAPCMD = "410" # as defined in http://ircv3.atheme.org/specification/capability-negotiation-3.1

class CapCommand(Command):
    def onUse(self, user, data):
        subcmd = data["subcmd"]
        if user.registered > 0 and "cap_negotiating" not in user.cache:
            user.cache["cap_negotiating"] = True
            user.registered += 1 # On receipt of a CAP command, block registration until CAP END is received
        if subcmd == "LS":
            user.sendMessage("CAP", "LS", ":{}".format(" ".join(self.ircd.module_data_cache["cap"].keys())))
        elif subcmd == "LIST":
            if "cap" not in user.cache:
                user.sendMessage("CAP", "LIST", ":")
            else:
                user.sendMessage("CAP", "LIST", ":{}".format(" ".join(user.cache["cap"])))
        elif subcmd == "REQ":
            ack = []
            nak = []
            for capability in data["list"]:
                if capability:

                    if capability[0] == "-":