diff 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
line wrap: on
line diff
--- a/sat/plugins/plugin_xep_0050.py	Fri Apr 26 11:57:26 2019 +0200
+++ b/sat/plugins/plugin_xep_0050.py	Fri Apr 26 11:57:26 2019 +0200
@@ -66,6 +66,7 @@
 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 @@
     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 @@
         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 @@
                 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)