# 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 epoch(utc_datetime):
#     delta = utc_datetime - UNIX_EPOCH
#     return int(delta.total_seconds())
# 
# def irc_lower(str):
#     return str.lower().replace("[","{").replace("]","}").replace("/","|").replace("~","^")
# 
# def now():
#     return datetime.datetime.utcnow().replace(microsecond=0)


from twisted.words.protocols import irc
from txircd.modbase import Mode
from txircd.utils import epoch, irc_lower, now
irc.RPL_LISTMODE = "728" # I made both of these up.  They're based on freenode's quiet lists,
irc.RPL_ENDOFLISTMODE = "729" # which have a parameter for the mode being queried.

class ServerNoticeMode(Mode):
    tellLists = {}
    
    def checkSet(self, user, target, param):
        if "o" not in user.mode:
            user.sendMessage(irc.ERR_NOPRIVILEGES, ":Permission denied - Only operators may set user mode s")
            return [False, param]
        mask = irc_lower(param)
        if mask not in self.tellLists:
            self.tellLists[mask] = []
        self.tellLists[mask].append(target)
        if "servernoticedata" not in target.cache:
            target.cache["servernoticedata"] = {}
        target.cache["servernoticedata"][mask] = epoch(now())
        return [True, mask]
    
    def checkUnset(self, user, target, param):
        mask = irc_lower(param)
        if mask in self.tellLists and target in self.tellLists[mask]:
            self.tellLists[mask].remove(target)
        if "servernoticedata" in target.cache and mask in target.cache["servernoticedata"]:
            del target.cache["servernoticedata"][mask]
        return [True, mask]
    
    def showParam(self, user, target):
        if "s" in target.mode:
            for mask in target.mode["s"]:

# 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 epoch(utc_datetime):
#     delta = utc_datetime - UNIX_EPOCH
#     return int(delta.total_seconds())
# 
# def irc_lower(str):
#     return str.lower().replace("[","{").replace("]","}").replace("/","|").replace("~","^")
# 
# def now():
#     return datetime.datetime.utcnow().replace(microsecond=0)


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

irc.RPL_LISTMODE = "728" # I made both of these up.  They're based on freenode's quiet lists,
irc.RPL_ENDOFLISTMODE = "729" # which have a parameter for the mode being queried.

class ServerNoticeMode(Mode):
    tellLists = {}
    
    def checkSet(self, user, target, param):
        if "o" not in user.mode:
            user.sendMessage(irc.ERR_NOPRIVILEGES, ":Permission denied - Only operators may set user mode s")
            return [False, param]
        mask = irc_lower(param)
        if mask not in self.tellLists:
            self.tellLists[mask] = []
        self.tellLists[mask].append(target)
        if "servernoticedata" not in target.cache:
            target.cache["servernoticedata"] = {}
        target.cache["servernoticedata"][mask] = epoch(now())
        return [True, mask]
    
    def checkUnset(self, user, target, param):
        mask = irc_lower(param)
        if mask in self.tellLists and target in self.tellLists[mask]:
            self.tellLists[mask].remove(target)
        if "servernoticedata" in target.cache and mask in target.cache["servernoticedata"]:
            del target.cache["servernoticedata"][mask]
        return [True, mask]
    
    def showParam(self, user, target):
        if "s" in target.mode:
            for mask in target.mode["s"]:

                time = target.cache["servernoticedata"][mask] if "servernoticedata" in target.cache and mask in target.cache["servernoticedata"] else epoch(now())