changeset 702:a25db3fe3959

plugin XEP-0071: rich messages management for sendMessage
author Goffi <goffi@goffi.org>
date Wed, 13 Nov 2013 14:03:16 +0100
parents 98b2400e17d6
children 0c2c1dfb79e4
files src/plugins/plugin_misc_text_syntaxes.py src/plugins/plugin_xep_0071.py
diffstat 2 files changed, 35 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/src/plugins/plugin_misc_text_syntaxes.py	Wed Nov 13 14:02:02 2013 +0100
+++ b/src/plugins/plugin_misc_text_syntaxes.py	Wed Nov 13 14:03:16 2013 +0100
@@ -121,7 +121,7 @@
         TextSyntaxes.params_data["options"] = u'\n'.join(options)
         self.host.memory.updateParams(TextSyntaxes.params % TextSyntaxes.params_data)
 
-    def getFavoriteSyntax(self, profile):
+    def getCurrentSyntax(self, profile):
         """ Return the selected syntax for the given profile
 
         @param profile: %(doc_profile)s
--- a/src/plugins/plugin_xep_0071.py	Wed Nov 13 14:02:02 2013 +0100
+++ b/src/plugins/plugin_xep_0071.py	Wed Nov 13 14:03:16 2013 +0100
@@ -69,9 +69,10 @@
     def __init__(self, host):
         info(_("XHTML-IM plugin initialization"))
         self.host = host
-        txt_synt_plg = self.host.plugins["TEXT-SYNTAXES"]
-        txt_synt_plg.addSyntax(self.SYNTAX_XHTML_IM, lambda xhtml: xhtml, self.XHTML2XHTML_IM, [txt_synt_plg.OPT_HIDDEN])
+        self.synt_plg = self.host.plugins["TEXT-SYNTAXES"]
+        self.synt_plg.addSyntax(self.SYNTAX_XHTML_IM, lambda xhtml: xhtml, self.XHTML2XHTML_IM, [self.synt_plg.OPT_HIDDEN])
         host.trigger.add("MessageReceived", self.messageReceivedTrigger)
+        host.trigger.add("sendMessage", self.sendMessageTrigger)
 
     def getHandler(self, profile):
         return XEP_0071_handler(self)
@@ -86,11 +87,28 @@
         def converted(xhtml):
             data['extra']['xhtml'] = xhtml
             return data
-        txt_synt_plg = self.host.plugins["TEXT-SYNTAXES"]
-        d = txt_synt_plg.convert(body_elt.toXml(), self.SYNTAX_XHTML_IM, safe=True)
+        d = self.synt_plg.convert(body_elt.toXml(), self.SYNTAX_XHTML_IM, safe=True)
         d.addCallback(converted)
         return d
 
+    def _sendMessageAddRich(self, mess_data, profile):
+        """ Construct XHTML-IM node and add it XML element
+        @param mess_data: message data as sended by sendMessage callback
+        """
+        def syntax_converted(xhtml_im):
+            message_elt = mess_data['xml']
+            html_elt = message_elt.addElement('html', NS_XHTML_IM)
+            body_elt = html_elt.addElement('body', NS_XHTML)
+            body_elt.addRawXml(xhtml_im)
+            mess_data['extra']['xhtml'] = xhtml_im
+            return mess_data
+
+        rich = mess_data['extra'].pop('rich')
+        syntax = self.synt_plg.getCurrentSyntax(profile)
+        d = self.synt_plg.convert(rich, syntax, self.SYNTAX_XHTML_IM)
+        d.addCallback(syntax_converted)
+        return d
+
     def messageReceivedTrigger(self, message, post_treat, profile):
         """ Check presence of XHTML-IM in message
         """
@@ -104,6 +122,18 @@
             pass
         return True
 
+    def sendMessageTrigger(self, mess_data, treatments, profile):
+        """ Check presence of rich text in extra
+        """
+        try:
+            rich = mess_data['extra']['rich']
+            # OK, we have found rich text
+            treatments.addCallback(self._sendMessageAddRich, profile)
+        except KeyError:
+            # No rich text found
+            pass
+        return True
+
     def _purgeStyle(self, styles_raw):
         """ Remove unauthorised styles according to the XEP-0071
         @param styles_raw: raw styles (value of the style attribute)