# 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 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 Command
from txircd.utils import epoch, irc_lower, now
from fnmatch import fnmatch

class WhoCommand(Command):
    def onUse(self, user, data):
        if "target" not in data:
            for u in self.ircd.users.itervalues():
                if "i" in u.mode:
                    continue
                common_channel = False
                for chan in self.ircd.channels.itervalues():
                    if user in chan.users and u in chan.users:
                        common_channel = True
                        break
                if not common_channel:
                    self.sendWhoLine(user, u, "*", None, data["filters"] if "filters" in data else "", data["fields"] if "fields" in data else "")
            user.sendMessage(irc.RPL_ENDOFWHO, "*", ":End of /WHO list.")
        else:
            if data["target"] in self.ircd.channels:
                cdata = self.ircd.channels[data["target"]]
                in_channel = user in cdata.users # cache this value instead of searching every iteration
                if not in_channel and ("p" in cdata.mode or "s" in cdata.mode):
                    irc.sendMessage(irc.RPL_ENDOFWHO, cdata.name, ":End of /WHO list.")
                    return
                for u in cdata.users.iterkeys():
                    self.sendWhoLine(user, u, cdata.name, cdata, data["filters"], data["fields"])
                user.sendMessage(irc.RPL_ENDOFWHO, cdata.name, ":End of /WHO list.")
            else:
                for u in self.ircd.users.itervalues():

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


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


class WhoCommand(Command):
    def onUse(self, user, data):
        if "target" not in data:
            for u in self.ircd.users.itervalues():
                if "i" in u.mode:
                    continue
                common_channel = False
                for chan in self.ircd.channels.itervalues():
                    if user in chan.users and u in chan.users:
                        common_channel = True
                        break
                if not common_channel:
                    self.sendWhoLine(user, u, "*", None, data["filters"] if "filters" in data else "", data["fields"] if "fields" in data else "")
            user.sendMessage(irc.RPL_ENDOFWHO, "*", ":End of /WHO list.")
        else:
            if data["target"] in self.ircd.channels:
                cdata = self.ircd.channels[data["target"]]
                in_channel = user in cdata.users # cache this value instead of searching every iteration
                if not in_channel and ("p" in cdata.mode or "s" in cdata.mode):
                    irc.sendMessage(irc.RPL_ENDOFWHO, cdata.name, ":End of /WHO list.")
                    return
                for u in cdata.users.iterkeys():
                    self.sendWhoLine(user, u, cdata.name, cdata, data["filters"], data["fields"])
                user.sendMessage(irc.RPL_ENDOFWHO, cdata.name, ":End of /WHO list.")
            else:
                for u in self.ircd.users.itervalues():

                    if fnmatch(irc_lower(u.nickname), irc_lower(data["target"])) or fnmatch(irc_lower(u.hostname), irc_lower(data["target"])):