2019-05-25 20:40:06 +00:00
|
|
|
#--depends-on commands
|
|
|
|
|
2016-05-09 15:14:12 +00:00
|
|
|
import time
|
2018-10-03 12:22:37 +00:00
|
|
|
from src import ModuleManager, utils
|
2016-05-09 15:14:12 +00:00
|
|
|
|
2019-09-17 09:53:40 +00:00
|
|
|
HIDDEN_MODES = set(["s", "p"])
|
|
|
|
|
2018-09-27 10:46:10 +00:00
|
|
|
class Module(ModuleManager.BaseModule):
|
2018-10-04 15:01:13 +00:00
|
|
|
def _uptime(self):
|
|
|
|
return utils.to_pretty_time(int(time.time()-self.bot.start_time))
|
|
|
|
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.hook("received.command.uptime")
|
2016-05-09 15:14:12 +00:00
|
|
|
def uptime(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-30 16:29:09 +00:00
|
|
|
:help: Show my uptime
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-10-04 15:01:13 +00:00
|
|
|
event["stdout"].write("Uptime: %s" % self._uptime())
|
2018-10-05 21:49:06 +00:00
|
|
|
@utils.hook("api.get.uptime")
|
2018-10-04 15:01:13 +00:00
|
|
|
def uptime_api(self, event):
|
|
|
|
return self._uptime()
|
2016-05-09 15:14:12 +00:00
|
|
|
|
2018-10-04 15:01:13 +00:00
|
|
|
def _stats(self):
|
2019-09-07 13:13:44 +00:00
|
|
|
networks = {}
|
|
|
|
|
2016-05-09 15:14:12 +00:00
|
|
|
for server in self.bot.servers.values():
|
2019-09-07 13:13:44 +00:00
|
|
|
networks[server.alias.lower()] = [len(server.channels),
|
|
|
|
len(server.users)]
|
|
|
|
|
|
|
|
return networks
|
|
|
|
|
|
|
|
def _plural(self, s, count):
|
|
|
|
return "%s%s" % (s, "" if count == 1 else "s")
|
2016-05-09 15:14:12 +00:00
|
|
|
|
2018-10-04 15:01:13 +00:00
|
|
|
@utils.hook("received.command.stats")
|
2019-09-07 13:13:44 +00:00
|
|
|
@utils.kwarg("help", "Show my network/channel/user stats")
|
|
|
|
@utils.kwarg("usage", "[network]")
|
2018-10-04 15:01:13 +00:00
|
|
|
def stats(self, event):
|
2019-09-07 13:13:44 +00:00
|
|
|
networks = self._stats()
|
|
|
|
|
|
|
|
if event["args"]:
|
|
|
|
alias = event["args_split"][0].lower()
|
|
|
|
if not alias in networks:
|
|
|
|
raise utils.EventError("Unknown alias '%s'" % alias)
|
2016-05-09 15:14:12 +00:00
|
|
|
|
2019-09-07 13:13:44 +00:00
|
|
|
channels, users = networks[alias]
|
2019-09-07 13:16:27 +00:00
|
|
|
event["stdout"].write("on %s I have %d %s and %d visible %s" %
|
2019-09-07 13:13:44 +00:00
|
|
|
(alias, channels, self._plural("channel", channels), users,
|
|
|
|
self._plural("user", users)))
|
|
|
|
else:
|
|
|
|
total_channels = 0
|
|
|
|
total_users = 0
|
|
|
|
for channels, users in networks.values():
|
|
|
|
total_channels += channels
|
|
|
|
total_users += users
|
2016-05-09 15:14:12 +00:00
|
|
|
|
2019-09-07 13:13:44 +00:00
|
|
|
network_count = len(networks.keys())
|
|
|
|
network_plural = self._plural("network", network_count)
|
|
|
|
channel_plural = self._plural("channel", total_channels)
|
|
|
|
user_plural = self._plural("user", total_users)
|
|
|
|
event["stdout"].write(
|
|
|
|
"I currently have %d %s, %d %s and %d visible %s" %
|
|
|
|
(network_count, network_plural, total_channels, channel_plural,
|
|
|
|
total_users, user_plural))
|
2018-10-04 15:01:13 +00:00
|
|
|
|
2018-10-05 21:49:06 +00:00
|
|
|
@utils.hook("api.get.stats")
|
2018-10-04 15:01:13 +00:00
|
|
|
def stats_api(self, event):
|
|
|
|
networks, channels, users = self._stats()
|
|
|
|
return {"networks": networks, "channels": channels, "users": users}
|
2018-10-04 16:10:15 +00:00
|
|
|
|
2018-10-05 08:51:17 +00:00
|
|
|
def _server_stats(self, server):
|
|
|
|
return {
|
2018-11-11 08:47:21 +00:00
|
|
|
"hostname": server.connection_params.hostname,
|
|
|
|
"port": server.connection_params.port,
|
|
|
|
"tls": server.connection_params.tls,
|
|
|
|
"alias": server.connection_params.alias,
|
2018-10-05 08:51:17 +00:00
|
|
|
"hostmask": "%s!%s@%s" % (
|
|
|
|
server.nickname, server.username, server.hostname),
|
2018-11-11 08:44:46 +00:00
|
|
|
"users": len(server.users),
|
2019-02-11 18:26:16 +00:00
|
|
|
"bytes-written": server.socket.bytes_written,
|
|
|
|
"bytes-read": server.socket.bytes_read,
|
2019-06-14 15:12:03 +00:00
|
|
|
"connected-since": server.socket.connect_time,
|
2018-11-27 13:24:38 +00:00
|
|
|
"channels": {
|
2019-02-08 17:56:13 +00:00
|
|
|
c.name: self._channel_stats(c) for c in server.channels
|
|
|
|
},
|
2019-05-15 16:13:05 +00:00
|
|
|
"capabilities": list(server.agreed_capabilities),
|
|
|
|
"version": server.version
|
2018-10-05 08:51:17 +00:00
|
|
|
}
|
|
|
|
|
2018-10-05 21:49:06 +00:00
|
|
|
@utils.hook("api.get.servers")
|
2018-10-04 16:18:40 +00:00
|
|
|
def servers_api(self, event):
|
2018-10-05 08:51:17 +00:00
|
|
|
if event["path"]:
|
|
|
|
server_id = event["path"][0]
|
|
|
|
if not server_id.isdigit():
|
|
|
|
return None
|
|
|
|
server_id = int(server_id)
|
|
|
|
|
2019-04-24 11:07:30 +00:00
|
|
|
server = self.bot.get_server_by_id(server_id)
|
2018-10-05 08:51:17 +00:00
|
|
|
if not server:
|
|
|
|
return None
|
|
|
|
return self._server_stats(server)
|
|
|
|
else:
|
|
|
|
servers = {}
|
|
|
|
for server in self.bot.servers.values():
|
|
|
|
servers[server.id] = self._server_stats(server)
|
|
|
|
return servers
|
2018-10-04 16:18:40 +00:00
|
|
|
|
2018-10-04 16:37:34 +00:00
|
|
|
def _channel_stats(self, channel):
|
|
|
|
return {
|
2018-12-26 23:24:43 +00:00
|
|
|
"users": sorted([user.nickname for user in channel.users],
|
|
|
|
key=lambda nickname: nickname.lower()),
|
2018-10-05 08:46:03 +00:00
|
|
|
"topic": channel.topic,
|
|
|
|
"topic-set-at": channel.topic_time,
|
2019-09-16 17:41:47 +00:00
|
|
|
"topic-set-by": channel.topic_setter.nickname,
|
2019-06-13 10:54:13 +00:00
|
|
|
"modes": channel.mode_str()
|
2018-10-04 16:37:34 +00:00
|
|
|
}
|
2018-10-05 21:49:06 +00:00
|
|
|
@utils.hook("api.get.channels")
|
2018-10-04 16:10:15 +00:00
|
|
|
def channels_api(self, event):
|
2018-10-04 16:21:40 +00:00
|
|
|
if event["path"]:
|
|
|
|
server_id = event["path"][0]
|
2018-10-04 16:10:15 +00:00
|
|
|
if not server_id.isdigit():
|
|
|
|
return None
|
|
|
|
server_id = int(server_id)
|
|
|
|
|
2019-04-24 11:07:30 +00:00
|
|
|
server = self.bot.get_server_by_id(server_id)
|
2018-10-04 16:10:15 +00:00
|
|
|
if not server:
|
|
|
|
return None
|
|
|
|
channels = {}
|
|
|
|
for channel in server.channels.values():
|
2018-10-04 16:37:34 +00:00
|
|
|
channels[channel.name] = self._channel_stats(channel)
|
2018-10-04 16:10:15 +00:00
|
|
|
return channels
|
|
|
|
else:
|
|
|
|
channels = {}
|
|
|
|
for server in self.bot.servers.values():
|
|
|
|
channels[server.id] = {}
|
|
|
|
for channel in server.channels.values():
|
2018-10-04 16:37:34 +00:00
|
|
|
channels[server.id][str(channel)] = self._channel_stats(
|
|
|
|
channel)
|
2018-10-04 16:10:15 +00:00
|
|
|
return channels
|
2018-10-04 16:37:47 +00:00
|
|
|
|
2019-09-16 15:05:36 +00:00
|
|
|
@utils.hook("received.command.channels")
|
|
|
|
@utils.kwarg("help", "List all the channel I'm in on this network")
|
|
|
|
@utils.kwarg("permission", "listchannels")
|
|
|
|
def channels_command(self, event):
|
2019-09-17 09:53:40 +00:00
|
|
|
channels = []
|
|
|
|
for channel in event["server"].channels.values():
|
|
|
|
hidden = bool(HIDDEN_MODES&set(channel.modes.keys()))
|
|
|
|
if hidden and (
|
|
|
|
event["is_channel"] and not channel == event["target"]):
|
|
|
|
continue
|
|
|
|
channels.append(channel.name)
|
|
|
|
|
2019-09-16 15:05:36 +00:00
|
|
|
event["stdout"].write("Current channels: %s" %
|
2019-09-17 09:53:40 +00:00
|
|
|
" ".join(sorted(channels)))
|
2019-09-16 15:05:36 +00:00
|
|
|
|
2018-10-05 21:49:06 +00:00
|
|
|
@utils.hook("api.get.modules")
|
2018-10-04 16:37:47 +00:00
|
|
|
def modules_api(self, event):
|
|
|
|
return list(self.bot.modules.modules.keys())
|