# Path: txircd/modbase.py
# 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 Mode

class InviteOnlyMode(Mode):
    def checkPermission(self, user, cmd, data):
        if cmd != "JOIN":
            return data
        targetChannels = data["targetchan"]
        keys = data["keys"]
        removeChannels = []
        for channel in targetChannels:
            if "i" in channel.mode and ("invites" not in user.cache or ("invites" in user.cache and channel.name not in user.cache["invites"])):
                if "invite_except" not in user.cache:
                    user.cache["invite_except"] = { channel.name: False }

# Path: txircd/modbase.py
# 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/cmode_i.py
from twisted.words.protocols import irc
from txircd.modbase import Mode


class InviteOnlyMode(Mode):
    def checkPermission(self, user, cmd, data):
        if cmd != "JOIN":
            return data
        targetChannels = data["targetchan"]
        keys = data["keys"]
        removeChannels = []
        for channel in targetChannels:
            if "i" in channel.mode and ("invites" not in user.cache or ("invites" in user.cache and channel.name not in user.cache["invites"])):
                if "invite_except" not in user.cache:
                    user.cache["invite_except"] = { channel.name: False }

                    return "again"