# 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 LimitMode(Mode):
    def checkSet(self, user, target, param):
        try:
            intParam = int(param)
        except ValueError:
            return [False, param]
        if str(intParam) != param:
            return [False, param]
        return [(intParam > 0), param]
    
    def checkPermission(self, user, cmd, data):
        if cmd != "JOIN":
            return data
        targetChannels = data["targetchan"]
        keys = data["keys"]
        removeChannels = []
        for channel in targetChannels:
            if "l" in channel.mode and len(channel.users) >= int(channel.mode["l"]):
                user.sendMessage(irc.ERR_CHANNELISFULL, channel.name, ":Cannot join channel (Channel is full)")
                removeChannels.append(channel)
        
        for channel in removeChannels:
            index = targetChannels.index(channel)

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


class LimitMode(Mode):
    def checkSet(self, user, target, param):
        try:
            intParam = int(param)
        except ValueError:
            return [False, param]
        if str(intParam) != param:
            return [False, param]
        return [(intParam > 0), param]
    
    def checkPermission(self, user, cmd, data):
        if cmd != "JOIN":
            return data
        targetChannels = data["targetchan"]
        keys = data["keys"]
        removeChannels = []
        for channel in targetChannels:
            if "l" in channel.mode and len(channel.users) >= int(channel.mode["l"]):
                user.sendMessage(irc.ERR_CHANNELISFULL, channel.name, ":Cannot join channel (Channel is full)")
                removeChannels.append(channel)
        
        for channel in removeChannels:
            index = targetChannels.index(channel)

            targetChannels.pop(index)