poezio/plugins/disco.py

62 lines
2 KiB
Python
Raw Normal View History

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
from slixmpp.jid import InvalidJID
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,
usage='<JID>',
short='Get the disco#info of a JID',
help='Get the disco#info of a JID')
2017-12-19 14:37:42 +00:00
def on_disco(self, iq):
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')
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':
title = field['value'][0]
2017-12-19 14:37:42 +00:00
continue
sep = '\n ' + len(var) * ' '
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:
self.api.information('\n'.join(server_info), title)
2017-12-19 14:37:42 +00:00
def command_disco(self, jid):
try:
2018-08-15 11:13:17 +00:00
self.core.xmpp.plugin['xep_0030'].get_info(
jid=jid, cached=False, callback=self.on_disco)
2017-12-19 14:37:42 +00:00
except InvalidJID as e:
self.api.information('Invalid JID “%s”: %s' % (jid, e), 'Error')