mirror of
https://github.com/jlu5/PyLink.git
synced 2024-11-01 09:19:23 +01:00
105 lines
4.0 KiB
Python
105 lines
4.0 KiB
Python
# 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)
|
|
|
|
def _map(irc, source, args, show_relay=True):
|
|
"""[<network>]
|
|
|
|
Shows the network map for the given network, or the current network if not specified."""
|
|
|
|
permissions.checkPermissions(irc, source, ['servermap.map', 'servermap.*'])
|
|
|
|
try:
|
|
netname = args[0]
|
|
except IndexError:
|
|
netname = irc.name
|
|
|
|
try:
|
|
ircobj = world.networkobjects[netname]
|
|
except KeyError:
|
|
irc.reply('Error: no such network %s' % netname)
|
|
return
|
|
|
|
servers = collections.defaultdict(set)
|
|
hostsid = ircobj.sid
|
|
usercount = len(ircobj.users)
|
|
|
|
# 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)
|
|
|
|
log.debug('(%s) servermaps.map servers fetched for %s: %s', irc.name, netname, servers)
|
|
|
|
reply = lambda text: irc.reply(text, private=True)
|
|
|
|
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
|
|
|
|
if hops == 0:
|
|
# Show our root server once.
|
|
rootusers = len(serverlist[sid].users)
|
|
reply('\x02%s\x02[%s]: %s user(s) (%s%%)' % (serverlist[sid].name, sid,
|
|
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):
|
|
if is_relay_server and hasattr(serverlist[leaf], 'remote'):
|
|
# Don't show relay subservers more than once.
|
|
continue
|
|
|
|
serverusers = len(serverlist[leaf].users)
|
|
if is_relay_server:
|
|
# Skip showing user data for relay servers.
|
|
reply("%s\x02%s\x02[%s] (via PyLink Relay)" % (' '*hops, serverlist[leaf].name, leaf))
|
|
else:
|
|
reply("%s\x02%s\x02[%s]: %s user(s) (%s%%)" % (' '*hops, serverlist[leaf].name, leaf,
|
|
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]
|
|
if remoteirc.protoname != 'clientbot':
|
|
# Only ever show relay subservers once - this prevents infinite loops.
|
|
showall(remoteirc, remoteirc.sid, hops=hops, is_relay_server=True)
|
|
|
|
else:
|
|
# Afterwards, decrement the hopcount.
|
|
hops -= 1
|
|
|
|
# Start the map at our PyLink server
|
|
firstserver = hostsid
|
|
showall(ircobj, firstserver)
|
|
serverlist = irc.servers
|
|
reply('Total %s users on %s local servers - average of %1.f per server' % (usercount, len(serverlist),
|
|
usercount/len(serverlist)))
|
|
|
|
utils.add_cmd(_map, 'map')
|
|
|
|
@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)
|