2017-12-19 14:37:42 +00:00
|
|
|
"""
|
|
|
|
Do a disco#info query on a JID
|
|
|
|
|
|
|
|
Usage
|
|
|
|
-----
|
|
|
|
|
|
|
|
.. glossary::
|
|
|
|
|
|
|
|
/disco
|
|
|
|
**Usage:** ``/disco <JID>``
|
|
|
|
|
|
|
|
This command queries a JID for its disco#info.
|
|
|
|
|
|
|
|
There is no cache, as this is generally used for debug more than
|
|
|
|
anything user-related.
|
|
|
|
"""
|
|
|
|
|
|
|
|
from poezio.plugin import BasePlugin
|
2020-03-26 16:42:37 +00:00
|
|
|
from poezio.decorators import command_args_parser
|
2017-12-19 14:37:42 +00:00
|
|
|
from slixmpp.jid import InvalidJID
|
2021-12-21 23:06:07 +00:00
|
|
|
from slixmpp.exceptions import IqError, IqTimeout
|
2017-12-19 14:37:42 +00:00
|
|
|
|
2018-08-15 11:13:17 +00:00
|
|
|
|
2017-12-19 14:37:42 +00:00
|
|
|
class Plugin(BasePlugin):
|
|
|
|
def init(self):
|
2018-08-15 11:13:17 +00:00
|
|
|
self.api.add_command(
|
|
|
|
'disco',
|
|
|
|
self.command_disco,
|
2020-03-26 16:42:37 +00:00
|
|
|
usage='<JID> [node] [info|items]',
|
2018-08-15 11:13:17 +00:00
|
|
|
short='Get the disco#info of a JID',
|
|
|
|
help='Get the disco#info of a JID')
|
2017-12-19 14:37:42 +00:00
|
|
|
|
2020-03-26 16:42:37 +00:00
|
|
|
def on_info(self, iq):
|
2018-12-25 22:20:12 +00:00
|
|
|
if iq['type'] == 'error':
|
|
|
|
self.api.information(iq['error']['text'] or iq['error']['condition'], 'Error')
|
|
|
|
return
|
|
|
|
|
2017-12-19 14:37:42 +00:00
|
|
|
info = iq['disco_info']
|
|
|
|
identities = (str(identity) for identity in info['identities'])
|
|
|
|
self.api.information('\n'.join(identities), 'Identities')
|
|
|
|
features = sorted(str(feature) for feature in info['features'])
|
|
|
|
self.api.information('\n'.join(features), 'Features')
|
2018-05-30 19:04:02 +00:00
|
|
|
title = 'Server Info'
|
2017-12-19 14:37:42 +00:00
|
|
|
server_info = []
|
|
|
|
for field in info['form']:
|
|
|
|
var = field['var']
|
|
|
|
if field['type'] == 'hidden' and var == 'FORM_TYPE':
|
2018-05-30 19:04:02 +00:00
|
|
|
title = field['value'][0]
|
2017-12-19 14:37:42 +00:00
|
|
|
continue
|
|
|
|
sep = '\n ' + len(var) * ' '
|
2018-05-30 19:04:02 +00:00
|
|
|
field_value = field.get_value(convert=False)
|
2018-08-15 11:13:17 +00:00
|
|
|
value = sep.join(field_value) if isinstance(field_value,
|
|
|
|
list) else field_value
|
2017-12-19 14:37:42 +00:00
|
|
|
server_info.append('%s: %s' % (var, value))
|
|
|
|
if server_info:
|
2018-05-30 19:04:02 +00:00
|
|
|
self.api.information('\n'.join(server_info), title)
|
2017-12-19 14:37:42 +00:00
|
|
|
|
2020-03-26 16:42:37 +00:00
|
|
|
def on_items(self, iq):
|
|
|
|
if iq['type'] == 'error':
|
|
|
|
self.api.information(iq['error']['text'] or iq['error']['condition'], 'Error')
|
|
|
|
return
|
|
|
|
|
|
|
|
def describe(item):
|
|
|
|
text = item[0]
|
|
|
|
node = item[1]
|
|
|
|
name = item[2]
|
|
|
|
if node is not None:
|
|
|
|
text += ', node=' + node
|
|
|
|
if name is not None:
|
|
|
|
text += ', name=' + name
|
|
|
|
return text
|
|
|
|
|
|
|
|
items = iq['disco_items']
|
|
|
|
self.api.information('\n'.join(describe(item) for item in items['items']), 'Items')
|
|
|
|
|
|
|
|
@command_args_parser.quoted(1, 3)
|
2021-01-30 12:59:42 +00:00
|
|
|
async def command_disco(self, args):
|
2020-03-26 16:42:37 +00:00
|
|
|
if args is None:
|
|
|
|
self.core.command.help('disco')
|
|
|
|
return
|
|
|
|
if len(args) == 1:
|
|
|
|
jid, = args
|
|
|
|
node = None
|
|
|
|
type_ = 'info'
|
|
|
|
elif len(args) == 2:
|
|
|
|
jid, node = args
|
|
|
|
type_ = 'info'
|
|
|
|
else:
|
|
|
|
jid, node, type_ = args
|
2017-12-19 14:37:42 +00:00
|
|
|
try:
|
2020-03-26 16:42:37 +00:00
|
|
|
if type_ == 'info':
|
2021-01-30 12:59:42 +00:00
|
|
|
iq = await self.core.xmpp.plugin['xep_0030'].get_info(
|
|
|
|
jid=jid, node=node, cached=False
|
|
|
|
)
|
|
|
|
self.on_info(iq)
|
2020-03-26 16:42:37 +00:00
|
|
|
elif type_ == 'items':
|
2021-01-30 12:59:42 +00:00
|
|
|
iq = await self.core.xmpp.plugin['xep_0030'].get_items(
|
|
|
|
jid=jid, node=node, cached=False
|
|
|
|
)
|
|
|
|
self.on_items(iq)
|
2017-12-19 14:37:42 +00:00
|
|
|
except InvalidJID as e:
|
|
|
|
self.api.information('Invalid JID “%s”: %s' % (jid, e), 'Error')
|
2021-12-21 23:06:07 +00:00
|
|
|
except IqError as e:
|
|
|
|
self.api.information('Received iq error while querying “%s”: %s' % (jid, e), 'Error')
|
|
|
|
except IqTimeout:
|
|
|
|
self.api.information('Received no reply querying “%s”…' % jid, 'Error')
|