From 19c7dce931ced32bf1e9de003e83242ff5ecf518 Mon Sep 17 00:00:00 2001 From: James Lu Date: Wed, 26 Jun 2019 13:46:04 -0700 Subject: [PATCH] commands: add a 'shownet' command Basic info available to everyone include network name, protocol module, and encoding. For those with the commands.shownet.extended permission, this also allows looking up disconnected networks defined in the config, and shows configured IP:port, PyLink hostname, SID, and SID range. Closes #578. --- docs/permissions-reference.md | 2 + plugins/commands.py | 73 ++++++++++++++++++++++++++++++++++- 2 files changed, 73 insertions(+), 2 deletions(-) diff --git a/docs/permissions-reference.md b/docs/permissions-reference.md index db2bd8f..de60e55 100644 --- a/docs/permissions-reference.md +++ b/docs/permissions-reference.md @@ -52,6 +52,8 @@ Remote versions of the `manage`, `list`, `sync`, and `clear` commands also exist - `commands.loglevel` - Grants access to the `loglevel` command. - `commands.logout.force` - Allows forcing logouts on other users via the `logout` command. - `commands.showchan` - Grants access to the `showchan` command. **With the default permissions set, this is granted to all users.** +- `commands.shownet` - Grants access to the `shownet` command (basic info including netname, protocol module, and encoding). **With the default permissions set, this is granted to all users.** +- `commands.shownet.extended` - Grants access to extended info in `shownet`, including connected status, target IP:port, and configured PyLink hostname / SID. - `commands.showuser` - Grants access to the `showuser` command. **With the default permissions set, this is granted to all users.** - `commands.status` - Grants access to the `status` command. **With the default permissions set, this is granted to all users.** diff --git a/plugins/commands.py b/plugins/commands.py index 3bb7fbb..109be2d 100644 --- a/plugins/commands.py +++ b/plugins/commands.py @@ -1,13 +1,13 @@ # commands.py: base PyLink commands import time -from pylinkirc import utils, __version__, world, real_version +from pylinkirc import conf, utils, __version__, world, real_version from pylinkirc.log import log from pylinkirc.coremods import permissions from pylinkirc.coremods.login import pwd_context -default_permissions = {"*!*@*": ['commands.status', 'commands.showuser', 'commands.showchan']} +default_permissions = {"*!*@*": ['commands.status', 'commands.showuser', 'commands.showchan', 'commands.shownet']} def main(irc=None): """Commands plugin main function, called on plugin load.""" @@ -123,6 +123,75 @@ def showuser(irc, source, args): for user in users: _do_showuser(irc, source, user) +@utils.add_cmd +def shownet(irc, source, args): + """[] + + Shows information about , or the current network if no argument is given.""" + permissions.check_permissions(irc, source, ['commands.shownet']) + try: + extended = permissions.check_permissions(irc, source, ['commands.shownet.extended']) + except utils.NotAuthorizedError: + extended = False + + try: + target = args[0] + except IndexError: + target = irc.name + + try: + netobj = world.networkobjects[target] + serverdata = netobj.serverdata + except KeyError: + netobj = None + + # If we have extended access, also look for disconnected networks + if extended and target in conf.conf['servers']: + serverdata = conf.conf['servers'][target] + else: + irc.error('Unknown network %r' % target) + return + + # Get extended protocol details: IRCd type, virtual server info + protocol_name = serverdata.get('protocol') + ircd_type = None + + # A bit of hardcoding here :( + if protocol_name == 'ts6': + ircd_type = serverdata.get('ircd', 'charybdis[default]') + elif protocol_name == 'inspircd': + ircd_type = serverdata.get('target_version', 'insp20[default]') + elif protocol_name == 'p10': + ircd_type = serverdata.get('ircd') or serverdata.get('p10_ircd') or 'nefarious[default]' + + if protocol_name and ircd_type: + protocol_name = '%s/%s' % (protocol_name, ircd_type) + elif netobj and not protocol_name: # Show virtual server detail if applicable + try: + parent_name = netobj.virtual_parent.name + except AttributeError: + parent_name = None + protocol_name = 'none; virtual server defined by \x02%s\x02' % parent_name + + irc.reply('Information on network \x02%s\x02: \x02%s\x02' % + (target, netobj.get_full_network_name() if netobj else '\x1dCurrently not connected\x1d')) + + irc.reply('\x02PyLink protocol module\x02: %s; \x02Encoding\x02: %s' % + (protocol_name, netobj.encoding if netobj else serverdata.get('encoding', 'utf-8[default]'))) + + # Extended info: target host, defined hostname / SID + if extended: + connected = netobj and netobj.connected.is_set() + irc.reply('\x02Connected?\x02 %s' % ('\x0303true' if connected else '\x0304false')) + + if serverdata.get('ip'): + irc.reply('\x02Server target\x02: \x1f%s:%s' % (serverdata['ip'], serverdata.get('port'))) + if serverdata.get('hostname'): + irc.reply('\x02PyLink hostname\x02: %s; \x02SID:\x02 %s; \x02SID range:\x02 %s' % + (serverdata.get('hostname') or _none, + serverdata.get('sid') or _none, + serverdata.get('sidrange') or _none)) + @utils.add_cmd def showchan(irc, source, args): """