plugin XEP-0050: make plugin usable with components + new adHocError method to easily raise error when needed

This commit is contained in:
Goffi 2019-04-26 11:57:26 +02:00
commit 007575d62b

View file

@ -66,6 +66,7 @@ SHOWS = OrderedDict(
PLUGIN_INFO = {
C.PI_NAME: "Ad-Hoc Commands",
C.PI_IMPORT_NAME: "XEP-0050",
C.PI_MODES: C.PLUG_MODE_BOTH,
C.PI_TYPE: "XEP",
C.PI_PROTOCOLS: ["XEP-0050"],
C.PI_MAIN: "XEP_0050",
@ -319,7 +320,8 @@ class XEP_0050(object):
def profileConnected(self, client):
# map from node to AdHocCommand instance
client._XEP_0050_commands = {}
self.addAdHocCommand(client, self._statusCallback, _("Status"))
if not client.is_component:
self.addAdHocCommand(client, self._statusCallback, _("Status"))
def do(self, client, entity, node, action=ACTION.EXECUTE, session_id=None,
form_values=None, timeout=30):
@ -356,6 +358,13 @@ class XEP_0050(object):
except StopIteration:
raise exceptions.NotFound(_(u"Missing command element"))
def adHocError(self, error_type):
"""Shortcut to raise an AdHocError
@param error_type(unicode): one of XEP_0050.ERROR
"""
raise AdHocError(error_type)
def _items2XMLUI(self, items, no_instructions):
"""Convert discovery items to XMLUI dialog """
# TODO: manage items on different jids
@ -573,22 +582,20 @@ class XEP_0050(object):
answer_form = data_form.Form.fromElement(x_elt)
show = answer_form["show"]
except (KeyError, StopIteration):
raise AdHocError(XEP_0050.ERROR.BAD_PAYLOAD)
self.adHocError(XEP_0050.ERROR.BAD_PAYLOAD)
if show not in SHOWS:
raise AdHocError(XEP_0050.ERROR.BAD_PAYLOAD)
self.adHocError(XEP_0050.ERROR.BAD_PAYLOAD)
if show == "disconnect":
self.host.disconnect(client.profile)
else:
self.host.setPresence(show=show, profile_key=client.profile)
# job done, we can end the session
form = data_form.Form("form", title=_(u"Updated"))
form.addField(data_form.Field("fixed", u"Status updated"))
status = XEP_0050.STATUS.COMPLETED
payload = None
note = (self.NOTE.INFO, _(u"Status updated"))
else:
raise AdHocError(XEP_0050.ERROR.INTERNAL)
self.adHocError(XEP_0050.ERROR.INTERNAL)
return (payload, status, None, note)