# 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/server.py
# class ModuleMessage(Command):
#     arguments = [
#         ("destserver", String()),
#         ("type", String()),
#         ("args", ListOf(String()))
#     ]
#     errors = {
#         HandshakeNotYetComplete: "HANDSHAKE_NOT_COMPLETE"
#     }
# 
# 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.server import ModuleMessage
from txircd.utils import epoch, now
import collections
    
    def processParams(self, user, params):
        if user.registered > 0:
            user.sendMessage(irc.ERR_NOTREGISTERED, "STATS", ":You have not registered")
            return {}
        if not params or not params[0]:
            user.sendMessage(irc.ERR_NEEDMOREPARAMS, "STATS", ":Not enough parameters")
            return {}
        statschar = params[0][0]
        if "o" not in user.mode and statschar not in self.ircd.servconfig["server_stats_public"]:
            user.sendMessage(irc.ERR_NOPRIVILEGES, ":Permission denied - Stats {} requires operator privileges".format(statschar))
            return {}
        if len(params) == 1:
            return {
                "user": user,
                "statstype": statschar
            }
        if params[1] not in self.ircd.servers:
            user.sendMessage(irc.ERR_NOSUCHSERVER, params[1], ":No such server")
            return {}
        return {
            "user": user,
            "statstype": statschar,
            "server": self.ircd.servers[params[1]]
        }
    
    def statsChars(self, user, statschar):
        if statschar == "o":
            for user in self.ircd.users.itervalues():
                if "o" in user.mode:

# 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/server.py
# class ModuleMessage(Command):
#     arguments = [
#         ("destserver", String()),
#         ("type", String()),
#         ("args", ListOf(String()))
#     ]
#     errors = {
#         HandshakeNotYetComplete: "HANDSHAKE_NOT_COMPLETE"
#     }
# 
# 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_stats.py
from twisted.words.protocols import irc
from txircd.modbase import Command
from txircd.server import ModuleMessage
from txircd.utils import epoch, now
import collections

    
    def processParams(self, user, params):
        if user.registered > 0:
            user.sendMessage(irc.ERR_NOTREGISTERED, "STATS", ":You have not registered")
            return {}
        if not params or not params[0]:
            user.sendMessage(irc.ERR_NEEDMOREPARAMS, "STATS", ":Not enough parameters")
            return {}
        statschar = params[0][0]
        if "o" not in user.mode and statschar not in self.ircd.servconfig["server_stats_public"]:
            user.sendMessage(irc.ERR_NOPRIVILEGES, ":Permission denied - Stats {} requires operator privileges".format(statschar))
            return {}
        if len(params) == 1:
            return {
                "user": user,
                "statstype": statschar
            }
        if params[1] not in self.ircd.servers:
            user.sendMessage(irc.ERR_NOSUCHSERVER, params[1], ":No such server")
            return {}
        return {
            "user": user,
            "statstype": statschar,
            "server": self.ircd.servers[params[1]]
        }
    
    def statsChars(self, user, statschar):
        if statschar == "o":
            for user in self.ircd.users.itervalues():
                if "o" in user.mode:

                    user.sendMessage(irc.RPL_STATSOPERS, ":{} ({}@{}) Idle: {} secs".format(user.nickname, user.username, user.hostname, epoch(now()) - epoch(user.lastactivity)))