changeset 2080:3626b2813158

plugin XEP-0071: fixed rich and xhtml sending + add simple text body if not present
author Goffi <goffi@goffi.org>
date Sun, 25 Sep 2016 19:17:38 +0200 (2016-09-25)
parents c8e561a5b2b6
children 2265d9df4cfb
files src/plugins/plugin_xep_0071.py
diffstat 1 files changed, 24 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/src/plugins/plugin_xep_0071.py	Sun Sep 25 19:16:25 2016 +0200
+++ b/src/plugins/plugin_xep_0071.py	Sun Sep 25 19:17:38 2016 +0200
@@ -22,6 +22,7 @@
 from sat.core import exceptions
 from sat.core.log import getLogger
 log = getLogger(__name__)
+from sat.tools.common import data_format
 
 from twisted.internet import defer
 from wokkel import disco, iwokkel
@@ -109,6 +110,20 @@
         d_list.addCallback(lambda dummy: data)
         return d_list
 
+    def _fill_body_text(self, text, data, lang):
+        data['message'][lang or ''] = text
+        message_elt = data['xml']
+        body_elt = message_elt.addElement("body", content=text)
+        if lang:
+            body_elt[(C.NS_XML, 'lang')] = lang
+
+    def _check_body_text(self, data, lang, markup, syntax, defers):
+        """check if simple text message exists, and fill if needed"""
+        if not (lang or '') in data['message']:
+            d = self._s.convert(markup, syntax, self._s.SYNTAX_TEXT)
+            d.addCallback(self._fill_body_text, data, lang)
+            defers.append(d)
+
     def _messageSendAddRich(self, data, client):
         """ Construct XHTML-IM node and add it XML element
 
@@ -130,20 +145,22 @@
 
         syntax = self._s.getCurrentSyntax(client.profile)
         defers = []
-        try:
-            rich = data['extra']['rich']
-        except KeyError:
+        if u'xhtml' in data['extra']:
             # we have directly XHTML
-            for lang, xhtml in data['extra']['xhtml'].iteritems():
+            for lang, xhtml in data_format.getSubDict('xhtml', data['extra']):
+                self._check_body_text(data, lang, xhtml, self._s.SYNTAX_XHTML, defers)
                 d = self._s.convert(xhtml, self._s.SYNTAX_XHTML, self.SYNTAX_XHTML_IM)
                 d.addCallback(syntax_converted, lang)
                 defers.append(d)
-        else:
+        elif u'rich' in data['extra']:
             # we have rich syntax to convert
-            for lang, rich_data in rich.iteritems():
+            for lang, rich_data in data_format.getSubDict('rich', data['extra']):
+                self._check_body_text(data, lang, rich_data, syntax, defers)
                 d = self._s.convert(rich_data, syntax, self.SYNTAX_XHTML_IM)
                 d.addCallback(syntax_converted, lang)
                 defers.append(d)
+        else:
+            exceptions.InternalError(u"xhtml or rich should be present at this point")
         d_list = defer.DeferredList(defers)
         d_list.addCallback(lambda dummy: data)
         return d_list
@@ -162,8 +179,7 @@
         return True
 
     def messageSendTrigger(self, client, data, pre_xml_treatments, post_xml_treatments):
-        """ Check presence of rich text in extra
-        """
+        """ Check presence of rich text in extra """
         rich = {}
         xhtml = {}
         for key, value in data['extra'].iteritems():