2016-09-24 20:19:16 +02:00
|
|
|
# servermaps.py: Maps out connected IRC servers.
|
|
|
|
|
|
|
|
from pylinkirc import utils, world
|
|
|
|
from pylinkirc.log import log
|
|
|
|
from pylinkirc.coremods import permissions
|
|
|
|
|
|
|
|
import collections
|
|
|
|
|
|
|
|
def _percent(num, total):
|
|
|
|
return '%.1f' % (num/total*100)
|
|
|
|
|
2016-09-24 20:54:53 +02:00
|
|
|
def _map(irc, source, args, show_relay=True):
|
2016-09-24 20:19:16 +02:00
|
|
|
"""[<network>]
|
|
|
|
|
|
|
|
Shows the network map for the given network, or the current network if not specified."""
|
|
|
|
|
2016-12-10 06:43:39 +01:00
|
|
|
permissions.checkPermissions(irc, source, ['servermaps.map'])
|
2016-09-24 20:19:16 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
netname = args[0]
|
|
|
|
except IndexError:
|
|
|
|
netname = irc.name
|
|
|
|
|
|
|
|
try:
|
|
|
|
ircobj = world.networkobjects[netname]
|
|
|
|
except KeyError:
|
2016-11-19 07:52:08 +01:00
|
|
|
irc.error('no such network %s' % netname)
|
2016-09-24 20:19:16 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
servers = collections.defaultdict(set)
|
|
|
|
hostsid = ircobj.sid
|
|
|
|
usercount = len(ircobj.users)
|
|
|
|
|
2016-09-24 20:54:53 +02:00
|
|
|
# Iterate over every connected server on every network.
|
|
|
|
for remotenet, remoteirc in world.networkobjects.items():
|
|
|
|
for sid, serverobj in remoteirc.servers.copy().items():
|
|
|
|
if sid == remoteirc.sid: # Don't re-add our own SID to the index
|
|
|
|
continue
|
|
|
|
|
|
|
|
# Save the server as UNDER its uplink.
|
|
|
|
servers[(remotenet, serverobj.uplink or remoteirc.sid)].add(sid)
|
2016-09-24 20:19:16 +02:00
|
|
|
|
|
|
|
log.debug('(%s) servermaps.map servers fetched for %s: %s', irc.name, netname, servers)
|
|
|
|
|
|
|
|
reply = lambda text: irc.reply(text, private=True)
|
|
|
|
|
2016-09-24 20:54:53 +02:00
|
|
|
def showall(ircobj, sid, hops=0, is_relay_server=False):
|
|
|
|
log.debug('servermaps: got showall() for SID %s on network %s', sid, ircobj.name)
|
|
|
|
serverlist = ircobj.servers.copy()
|
|
|
|
netname = ircobj.name
|
|
|
|
|
2016-09-24 20:19:16 +02:00
|
|
|
if hops == 0:
|
|
|
|
# Show our root server once.
|
|
|
|
rootusers = len(serverlist[sid].users)
|
2016-09-24 20:54:53 +02:00
|
|
|
reply('\x02%s\x02[%s]: %s user(s) (%s%%)' % (serverlist[sid].name, sid,
|
2016-09-24 20:19:16 +02:00
|
|
|
rootusers, _percent(rootusers, usercount)))
|
|
|
|
|
|
|
|
log.debug('(%s) servermaps: servers under sid %s: %s', irc.name, sid, servers)
|
|
|
|
|
|
|
|
# Every time we descend a server to process its map, raise the hopcount used in formatting.
|
|
|
|
hops += 1
|
|
|
|
leaves = servers[(netname, sid)]
|
|
|
|
for leafcount, leaf in enumerate(leaves):
|
2016-09-24 20:54:53 +02:00
|
|
|
if is_relay_server and hasattr(serverlist[leaf], 'remote'):
|
|
|
|
# Don't show relay subservers more than once.
|
|
|
|
continue
|
2016-09-24 20:19:16 +02:00
|
|
|
|
|
|
|
serverusers = len(serverlist[leaf].users)
|
2016-09-24 20:54:53 +02:00
|
|
|
if is_relay_server:
|
|
|
|
# Skip showing user data for relay servers.
|
2016-10-08 03:51:31 +02:00
|
|
|
reply("%s\x02%s\x02[%s] (via PyLink Relay)" % (' '*hops, serverlist[leaf].name, leaf))
|
2016-09-24 20:54:53 +02:00
|
|
|
else:
|
2016-10-08 03:51:31 +02:00
|
|
|
reply("%s\x02%s\x02[%s]: %s user(s) (%s%%)" % (' '*hops, serverlist[leaf].name, leaf,
|
2016-09-24 20:54:53 +02:00
|
|
|
serverusers, _percent(serverusers, usercount)))
|
|
|
|
showall(ircobj, leaf, hops, is_relay_server=is_relay_server)
|
|
|
|
|
|
|
|
if (not is_relay_server) and hasattr(serverlist[leaf], 'remote') and show_relay:
|
|
|
|
# This is a relay server - display the remote map of the network it represents
|
|
|
|
relay_server = serverlist[leaf].remote
|
|
|
|
remoteirc = world.networkobjects[relay_server]
|
2017-06-30 08:01:39 +02:00
|
|
|
if remoteirc.has_cap('can-track-servers'):
|
2016-09-25 01:36:10 +02:00
|
|
|
# Only ever show relay subservers once - this prevents infinite loops.
|
|
|
|
showall(remoteirc, remoteirc.sid, hops=hops, is_relay_server=True)
|
2016-09-24 20:54:53 +02:00
|
|
|
|
2016-09-24 20:19:16 +02:00
|
|
|
else:
|
|
|
|
# Afterwards, decrement the hopcount.
|
|
|
|
hops -= 1
|
|
|
|
|
|
|
|
# Start the map at our PyLink server
|
|
|
|
firstserver = hostsid
|
2016-09-24 20:54:53 +02:00
|
|
|
showall(ircobj, firstserver)
|
|
|
|
serverlist = irc.servers
|
|
|
|
reply('Total %s users on %s local servers - average of %1.f per server' % (usercount, len(serverlist),
|
2016-09-24 20:19:16 +02:00
|
|
|
usercount/len(serverlist)))
|
|
|
|
|
|
|
|
utils.add_cmd(_map, 'map')
|
2016-09-24 20:58:28 +02:00
|
|
|
|
|
|
|
@utils.add_cmd
|
|
|
|
def localmap(irc, source, args):
|
|
|
|
"""[<network>]
|
|
|
|
|
|
|
|
Shows the network map for the given network, or the current network if not specified.
|
|
|
|
This command does not expand Relay subservers."""
|
|
|
|
_map(irc, source, args, show_relay=False)
|