# 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 Mode(object):
#     def hook(self, base):
#         self.ircd = base
#         return self
#     def checkSet(self, user, target, param):
#         return [True, param]
#     def checkUnset(self, user, target, param):
#         return [True, param]
#     def showParam(self, user, target, param):
#         return param
#     def checkPermission(self, user, cmd, data):
#         return data


from twisted.words.protocols import irc
from txircd.modbase import Command, Mode

irc.RPL_KNOCK = "710"
irc.RPL_KNOCKDLVR = "711"
irc.ERR_TOOMANYKNOCK = "712"
irc.ERR_CHANOPEN = "713"
irc.ERR_KNOCKONCHAN = "714"

class KnockCommand(Command):
    def onUse(self, user, data):
        cdata = data["targetchan"]
        if "knocks" not in user.cache:
            user.cache["knocks"] = []
        user.cache["knocks"].append(cdata.name)
        reason = data["reason"]
        for u in cdata.users.iterkeys():
            if u.hasAccess(cdata, self.ircd.servconfig["channel_minimum_level"]["INVITE"] if "channel_minimum_level" in self.ircd.servconfig and "INVITE" in self.ircd.servconfig["channel_minimum_level"] else "o"):

# 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 Mode(object):
#     def hook(self, base):
#         self.ircd = base
#         return self
#     def checkSet(self, user, target, param):
#         return [True, param]
#     def checkUnset(self, user, target, param):
#         return [True, param]
#     def showParam(self, user, target, param):
#         return param
#     def checkPermission(self, user, cmd, data):
#         return data


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


irc.RPL_KNOCK = "710"
irc.RPL_KNOCKDLVR = "711"
irc.ERR_TOOMANYKNOCK = "712"
irc.ERR_CHANOPEN = "713"
irc.ERR_KNOCKONCHAN = "714"

class KnockCommand(Command):
    def onUse(self, user, data):
        cdata = data["targetchan"]
        if "knocks" not in user.cache:
            user.cache["knocks"] = []
        user.cache["knocks"].append(cdata.name)
        reason = data["reason"]
        for u in cdata.users.iterkeys():
            if u.hasAccess(cdata, self.ircd.servconfig["channel_minimum_level"]["INVITE"] if "channel_minimum_level" in self.ircd.servconfig and "INVITE" in self.ircd.servconfig["channel_minimum_level"] else "o"):

                u.sendMessage(irc.RPL_KNOCK, cdata.name, user.prefix(), ":{}".format(reason))