# Path: txircd/modbase.py
# class Command(object):
#     def hook(self, base):
#         self.ircd = base
#         return self
#     def onUse(self, user, data):
#         pass
#     def processParams(self, user, params):
#         return {
#             "user": user,
#             "params": params
#         }
#     def updateActivity(self, user):
#         user.lastactivity = now()
# 
# Path: txircd/utils.py
# def epoch(utc_datetime):
#     delta = utc_datetime - UNIX_EPOCH
#     return int(delta.total_seconds())
# 
# def now():
#     return datetime.datetime.utcnow().replace(microsecond=0)


from twisted.words.protocols import irc
from txircd.modbase import Command
from txircd.utils import epoch, now

class TopicCommand(Command):
    def onUse(self, user, data):
        cdata = data["targetchan"]
        if "topic" not in data:
            if cdata.topic:
                user.sendMessage(irc.RPL_TOPIC, cdata.name, ":{}".format(cdata.topic))
                user.sendMessage(irc.RPL_TOPICWHOTIME, cdata.name, cdata.topicSetter, str(epoch(cdata.topicTime)))
            else:
                user.sendMessage(irc.RPL_NOTOPIC, cdata.name, "No topic is set")
        else:
            cdata.setTopic(data["topic"], user.prefix())
            cdata.sendChannelMessage("TOPIC", ":{}".format(cdata.topic), prefix=user.prefix())
    
    def processParams(self, user, params):
        if user.registered > 0:
            user.sendMessage(irc.ERR_NOTREGISTERED, "TOPIC", ":You have not registered")
            return {}
        if not params:
            user.sendMessage(irc.ERR_NEEDMOREPARAMS, "TOPIC", ":Not enough parameters")
            return {}
        if params[0] not in self.ircd.channels:
            user.sendMessage(irc.ERR_NOSUCHCHANNEL, params[0], ":No such channel")
            return {}
        cdata = self.ircd.channels[params[0]]

# Path: txircd/modbase.py
# class Command(object):
#     def hook(self, base):
#         self.ircd = base
#         return self
#     def onUse(self, user, data):
#         pass
#     def processParams(self, user, params):
#         return {
#             "user": user,
#             "params": params
#         }
#     def updateActivity(self, user):
#         user.lastactivity = now()
# 
# Path: txircd/utils.py
# def epoch(utc_datetime):
#     delta = utc_datetime - UNIX_EPOCH
#     return int(delta.total_seconds())
# 
# def now():
#     return datetime.datetime.utcnow().replace(microsecond=0)


# Path: txircd/modules/cmd_topic.py
from twisted.words.protocols import irc
from txircd.modbase import Command
from txircd.utils import epoch, now


class TopicCommand(Command):
    def onUse(self, user, data):
        cdata = data["targetchan"]
        if "topic" not in data:
            if cdata.topic:
                user.sendMessage(irc.RPL_TOPIC, cdata.name, ":{}".format(cdata.topic))
                user.sendMessage(irc.RPL_TOPICWHOTIME, cdata.name, cdata.topicSetter, str(epoch(cdata.topicTime)))
            else:
                user.sendMessage(irc.RPL_NOTOPIC, cdata.name, "No topic is set")
        else:
            cdata.setTopic(data["topic"], user.prefix())
            cdata.sendChannelMessage("TOPIC", ":{}".format(cdata.topic), prefix=user.prefix())
    
    def processParams(self, user, params):
        if user.registered > 0:
            user.sendMessage(irc.ERR_NOTREGISTERED, "TOPIC", ":You have not registered")
            return {}
        if not params:
            user.sendMessage(irc.ERR_NEEDMOREPARAMS, "TOPIC", ":Not enough parameters")
            return {}
        if params[0] not in self.ircd.channels:
            user.sendMessage(irc.ERR_NOSUCHCHANNEL, params[0], ":No such channel")
            return {}
        cdata = self.ircd.channels[params[0]]

        if user not in cdata.users: