comparison sat/plugins/plugin_xep_0050.py @ 2921:a8c2d8b3453f

plugin XEP-0050: make plugin usable with components + new adHocError method to easily raise error when needed
author Goffi <goffi@goffi.org>
date Fri, 26 Apr 2019 11:57:26 +0200
parents 003b8b4b56a7
children ab2696e34d29
comparison
equal deleted inserted replaced
2920:945e53cde9b2 2921:a8c2d8b3453f
64 ) 64 )
65 65
66 PLUGIN_INFO = { 66 PLUGIN_INFO = {
67 C.PI_NAME: "Ad-Hoc Commands", 67 C.PI_NAME: "Ad-Hoc Commands",
68 C.PI_IMPORT_NAME: "XEP-0050", 68 C.PI_IMPORT_NAME: "XEP-0050",
69 C.PI_MODES: C.PLUG_MODE_BOTH,
69 C.PI_TYPE: "XEP", 70 C.PI_TYPE: "XEP",
70 C.PI_PROTOCOLS: ["XEP-0050"], 71 C.PI_PROTOCOLS: ["XEP-0050"],
71 C.PI_MAIN: "XEP_0050", 72 C.PI_MAIN: "XEP_0050",
72 C.PI_HANDLER: "yes", 73 C.PI_HANDLER: "yes",
73 C.PI_DESCRIPTION: _(u"""Implementation of Ad-Hoc Commands"""), 74 C.PI_DESCRIPTION: _(u"""Implementation of Ad-Hoc Commands"""),
317 return XEP_0050_handler(self) 318 return XEP_0050_handler(self)
318 319
319 def profileConnected(self, client): 320 def profileConnected(self, client):
320 # map from node to AdHocCommand instance 321 # map from node to AdHocCommand instance
321 client._XEP_0050_commands = {} 322 client._XEP_0050_commands = {}
322 self.addAdHocCommand(client, self._statusCallback, _("Status")) 323 if not client.is_component:
324 self.addAdHocCommand(client, self._statusCallback, _("Status"))
323 325
324 def do(self, client, entity, node, action=ACTION.EXECUTE, session_id=None, 326 def do(self, client, entity, node, action=ACTION.EXECUTE, session_id=None,
325 form_values=None, timeout=30): 327 form_values=None, timeout=30):
326 """Do an Ad-Hoc Command 328 """Do an Ad-Hoc Command
327 329
353 def getCommandElt(self, iq_elt): 355 def getCommandElt(self, iq_elt):
354 try: 356 try:
355 return iq_elt.elements(NS_COMMANDS, "command").next() 357 return iq_elt.elements(NS_COMMANDS, "command").next()
356 except StopIteration: 358 except StopIteration:
357 raise exceptions.NotFound(_(u"Missing command element")) 359 raise exceptions.NotFound(_(u"Missing command element"))
360
361 def adHocError(self, error_type):
362 """Shortcut to raise an AdHocError
363
364 @param error_type(unicode): one of XEP_0050.ERROR
365 """
366 raise AdHocError(error_type)
358 367
359 def _items2XMLUI(self, items, no_instructions): 368 def _items2XMLUI(self, items, no_instructions):
360 """Convert discovery items to XMLUI dialog """ 369 """Convert discovery items to XMLUI dialog """
361 # TODO: manage items on different jids 370 # TODO: manage items on different jids
362 form_ui = xml_tools.XMLUI("form", submit_id=self.__requesting_id) 371 form_ui = xml_tools.XMLUI("form", submit_id=self.__requesting_id)
571 try: 580 try:
572 x_elt = command_elt.elements(data_form.NS_X_DATA, "x").next() 581 x_elt = command_elt.elements(data_form.NS_X_DATA, "x").next()
573 answer_form = data_form.Form.fromElement(x_elt) 582 answer_form = data_form.Form.fromElement(x_elt)
574 show = answer_form["show"] 583 show = answer_form["show"]
575 except (KeyError, StopIteration): 584 except (KeyError, StopIteration):
576 raise AdHocError(XEP_0050.ERROR.BAD_PAYLOAD) 585 self.adHocError(XEP_0050.ERROR.BAD_PAYLOAD)
577 if show not in SHOWS: 586 if show not in SHOWS:
578 raise AdHocError(XEP_0050.ERROR.BAD_PAYLOAD) 587 self.adHocError(XEP_0050.ERROR.BAD_PAYLOAD)
579 if show == "disconnect": 588 if show == "disconnect":
580 self.host.disconnect(client.profile) 589 self.host.disconnect(client.profile)
581 else: 590 else:
582 self.host.setPresence(show=show, profile_key=client.profile) 591 self.host.setPresence(show=show, profile_key=client.profile)
583 592
584 # job done, we can end the session 593 # job done, we can end the session
585 form = data_form.Form("form", title=_(u"Updated"))
586 form.addField(data_form.Field("fixed", u"Status updated"))
587 status = XEP_0050.STATUS.COMPLETED 594 status = XEP_0050.STATUS.COMPLETED
588 payload = None 595 payload = None
589 note = (self.NOTE.INFO, _(u"Status updated")) 596 note = (self.NOTE.INFO, _(u"Status updated"))
590 else: 597 else:
591 raise AdHocError(XEP_0050.ERROR.INTERNAL) 598 self.adHocError(XEP_0050.ERROR.INTERNAL)
592 599
593 return (payload, status, None, note) 600 return (payload, status, None, note)
594 601
595 def _run(self, service_jid_s="", node="", profile_key=C.PROF_KEY_NONE): 602 def _run(self, service_jid_s="", node="", profile_key=C.PROF_KEY_NONE):
596 client = self.host.getClient(profile_key) 603 client = self.host.getClient(profile_key)