diff sat/plugins/plugin_xep_0359.py @ 3103:f0f48b4deb35

plugin XEP-0359: handle origin-id
author Goffi <goffi@goffi.org>
date Mon, 30 Dec 2019 20:44:05 +0100
parents ab2696e34d29
children 9d0df638c8b4
line wrap: on
line diff
--- a/sat/plugins/plugin_xep_0359.py	Mon Dec 30 20:44:04 2019 +0100
+++ b/sat/plugins/plugin_xep_0359.py	Mon Dec 30 20:44:05 2019 +0100
@@ -18,13 +18,14 @@
 # You should have received a copy of the GNU Affero General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
+import uuid
+from zope.interface import implementer
+from twisted.words.protocols.jabber import xmlstream
+from wokkel import disco
 from sat.core.constants import Const as C
 from sat.core import exceptions
 from sat.core.i18n import _
 from sat.core.log import getLogger
-from twisted.words.protocols.jabber import xmlstream
-from zope.interface import implementer
-from wokkel import disco
 
 log = getLogger(__name__)
 
@@ -88,6 +89,32 @@
         sid_elt["by"] = client.jid.userhost() if by is None else by.userhost()
         sid_elt["id"] = stanza_id
 
+    def getOriginId(self, element):
+        """Return origin-id if found in element
+
+        @param element(domish.Element): element to parse
+        @return (unicode, None): origin-id if found
+        """
+        try:
+            origin_elt = next(element.elements(NS_SID, "origin-id"))
+        except StopIteration:
+            return None
+        else:
+            return origin_elt.getAttribute("id")
+
+    def addOriginId(self, element, origin_id=None):
+        """Add a <origin-id/> to a stanza
+
+        @param element(domish.Element): stanza where the <origin-id/> must be added
+        @param origin_id(str): id to use, None to automatically generate
+        @return (str): origin_id
+        """
+        if origin_id is None:
+            origin_id = str(uuid.uuid4())
+        sid_elt = element.addElement((NS_SID, "origin-id"))
+        sid_elt["id"] = origin_id
+        return origin_id
+
     def getHandler(self, client):
         return XEP_0359_handler()