# 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/utils.py
# def irc_lower(str):
#     return str.lower().replace("[","{").replace("]","}").replace("/","|").replace("~","^")
# 
# def epoch(utc_datetime):
#     delta = utc_datetime - UNIX_EPOCH
#     return int(delta.total_seconds())
# 
# def now():
#     return datetime.datetime.utcnow().replace(microsecond=0)
# 
# class CaseInsensitiveDictionary(MutableMapping):
#     def __init__(self):
#         self._data = {}
# 
#     def __repr__(self):
#         return repr(self._data)
# 
#     def __delitem__(self, key):
#         try:
#             del self._data[irc_lower(key)]
#         except KeyError:
#             raise KeyError(key)
# 
#     def __getitem__(self, key):
#         try:
#             return self._data[irc_lower(key)]
#         except KeyError:
#             raise KeyError(key)
# 
#     def __iter__(self):
#         return iter(self._data)
# 
#     def __len__(self):
#         return len(self._data)
# 
#     def __setitem__(self, key, value):
#         self._data[irc_lower(key)] = value


from twisted.words.protocols import irc
from txircd.modbase import Mode
from txircd.utils import irc_lower, epoch, now, CaseInsensitiveDictionary
from fnmatch import fnmatch
    
    def checkPermission(self, user, cmd, data):
        if cmd != "JOIN":
            return data
        channels = data["targetchan"]
        if "ban_evaluating" not in user.cache:
            user.cache["ban_evaluating"] = channels
            return "again"
        keys = data["keys"]
        for hostmask in (irc_lower(user.prefix()), irc_lower("{}!{}@{}".format(user.nickname, user.username, user.realhost)), irc_lower("{}!{}@{}".format(user.nickname, user.username, user.ip))):
            remove = []
            for chan in user.cache["ban_evaluating"]:
                if "b" in chan.mode:
                    for mask in chan.mode["b"]:
                        if fnmatch(hostmask, irc_lower(mask)):
                            remove.append(chan)
                            user.sendMessage(irc.ERR_BANNEDFROMCHAN, chan.name, ":Cannot join channel (You're banned)")
                            break
            for chan in remove:
                index = channels.index(chan)
                channels.pop(index)
                keys.pop(index)
        data["targetchan"] = channels
        data["keys"] = keys
        del user.cache["ban_evaluating"]
        return data
    
    def showParam(self, user, target):
        if "b" in target.mode:
            for entry in target.mode["b"]:

# 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/utils.py
# def irc_lower(str):
#     return str.lower().replace("[","{").replace("]","}").replace("/","|").replace("~","^")
# 
# def epoch(utc_datetime):
#     delta = utc_datetime - UNIX_EPOCH
#     return int(delta.total_seconds())
# 
# def now():
#     return datetime.datetime.utcnow().replace(microsecond=0)
# 
# class CaseInsensitiveDictionary(MutableMapping):
#     def __init__(self):
#         self._data = {}
# 
#     def __repr__(self):
#         return repr(self._data)
# 
#     def __delitem__(self, key):
#         try:
#             del self._data[irc_lower(key)]
#         except KeyError:
#             raise KeyError(key)
# 
#     def __getitem__(self, key):
#         try:
#             return self._data[irc_lower(key)]
#         except KeyError:
#             raise KeyError(key)
# 
#     def __iter__(self):
#         return iter(self._data)
# 
#     def __len__(self):
#         return len(self._data)
# 
#     def __setitem__(self, key, value):
#         self._data[irc_lower(key)] = value


# Path: txircd/modules/cmode_b.py
from twisted.words.protocols import irc
from txircd.modbase import Mode
from txircd.utils import irc_lower, epoch, now, CaseInsensitiveDictionary
from fnmatch import fnmatch

    
    def checkPermission(self, user, cmd, data):
        if cmd != "JOIN":
            return data
        channels = data["targetchan"]
        if "ban_evaluating" not in user.cache:
            user.cache["ban_evaluating"] = channels
            return "again"
        keys = data["keys"]
        for hostmask in (irc_lower(user.prefix()), irc_lower("{}!{}@{}".format(user.nickname, user.username, user.realhost)), irc_lower("{}!{}@{}".format(user.nickname, user.username, user.ip))):
            remove = []
            for chan in user.cache["ban_evaluating"]:
                if "b" in chan.mode:
                    for mask in chan.mode["b"]:
                        if fnmatch(hostmask, irc_lower(mask)):
                            remove.append(chan)
                            user.sendMessage(irc.ERR_BANNEDFROMCHAN, chan.name, ":Cannot join channel (You're banned)")
                            break
            for chan in remove:
                index = channels.index(chan)
                channels.pop(index)
                keys.pop(index)
        data["targetchan"] = channels
        data["keys"] = keys
        del user.cache["ban_evaluating"]
        return data
    
    def showParam(self, user, target):
        if "b" in target.mode:
            for entry in target.mode["b"]:

                metadata = target.cache["bandata"][entry] if "bandata" in target.cache and entry in target.cache["bandata"] else [ self.ircd.name, epoch(now()) ]