# 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
            param = "*!{}".format(param)
        if "bandata" not in target.cache:
            target.cache["bandata"] = {}
        target.cache["bandata"][param] = [user.nickname, epoch(now())]
        return [True, param]
    
    def checkUnset(self, user, target, param):
        if " " in param:
            param = param[:param.index(" ")]
        if "!" not in param and "@" not in param:
            param = "{}!*@*".format(param)
        elif "@" not in param:
            param = "{}@*".format(param)
        elif "!" not in param:
            param = "*!{}".format(param)
        for banmask in target.mode["b"]:
            if param == banmask:
                if "bandata" in target.cache and param in target.cache["bandata"]: # Just in case something happened, although bandata shouldn't just disappear
                    del target.cache["bandata"][param]
                return [True, param]
        return [False, param]
    
    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"]

# 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

            param = "*!{}".format(param)
        if "bandata" not in target.cache:
            target.cache["bandata"] = {}
        target.cache["bandata"][param] = [user.nickname, epoch(now())]
        return [True, param]
    
    def checkUnset(self, user, target, param):
        if " " in param:
            param = param[:param.index(" ")]
        if "!" not in param and "@" not in param:
            param = "{}!*@*".format(param)
        elif "@" not in param:
            param = "{}@*".format(param)
        elif "!" not in param:
            param = "*!{}".format(param)
        for banmask in target.mode["b"]:
            if param == banmask:
                if "bandata" in target.cache and param in target.cache["bandata"]: # Just in case something happened, although bandata shouldn't just disappear
                    del target.cache["bandata"][param]
                return [True, param]
        return [False, param]
    
    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))):