Mercurial > libervia-backend
comparison src/plugins/plugin_xep_0071.py @ 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 | bfabeedbf32e |
comparison
equal
deleted
inserted
replaced
701:98b2400e17d6 | 702:a25db3fe3959 |
---|---|
67 SYNTAX_XHTML_IM = "XHTML-IM" | 67 SYNTAX_XHTML_IM = "XHTML-IM" |
68 | 68 |
69 def __init__(self, host): | 69 def __init__(self, host): |
70 info(_("XHTML-IM plugin initialization")) | 70 info(_("XHTML-IM plugin initialization")) |
71 self.host = host | 71 self.host = host |
72 txt_synt_plg = self.host.plugins["TEXT-SYNTAXES"] | 72 self.synt_plg = self.host.plugins["TEXT-SYNTAXES"] |
73 txt_synt_plg.addSyntax(self.SYNTAX_XHTML_IM, lambda xhtml: xhtml, self.XHTML2XHTML_IM, [txt_synt_plg.OPT_HIDDEN]) | 73 self.synt_plg.addSyntax(self.SYNTAX_XHTML_IM, lambda xhtml: xhtml, self.XHTML2XHTML_IM, [self.synt_plg.OPT_HIDDEN]) |
74 host.trigger.add("MessageReceived", self.messageReceivedTrigger) | 74 host.trigger.add("MessageReceived", self.messageReceivedTrigger) |
75 host.trigger.add("sendMessage", self.sendMessageTrigger) | |
75 | 76 |
76 def getHandler(self, profile): | 77 def getHandler(self, profile): |
77 return XEP_0071_handler(self) | 78 return XEP_0071_handler(self) |
78 | 79 |
79 def _messagePostTreat(self, data, body_elt): | 80 def _messagePostTreat(self, data, body_elt): |
84 """ | 85 """ |
85 #TODO: check if text only body is empty, then try to convert XHTML-IM to pure text and show a warning message | 86 #TODO: check if text only body is empty, then try to convert XHTML-IM to pure text and show a warning message |
86 def converted(xhtml): | 87 def converted(xhtml): |
87 data['extra']['xhtml'] = xhtml | 88 data['extra']['xhtml'] = xhtml |
88 return data | 89 return data |
89 txt_synt_plg = self.host.plugins["TEXT-SYNTAXES"] | 90 d = self.synt_plg.convert(body_elt.toXml(), self.SYNTAX_XHTML_IM, safe=True) |
90 d = txt_synt_plg.convert(body_elt.toXml(), self.SYNTAX_XHTML_IM, safe=True) | |
91 d.addCallback(converted) | 91 d.addCallback(converted) |
92 return d | |
93 | |
94 def _sendMessageAddRich(self, mess_data, profile): | |
95 """ Construct XHTML-IM node and add it XML element | |
96 @param mess_data: message data as sended by sendMessage callback | |
97 """ | |
98 def syntax_converted(xhtml_im): | |
99 message_elt = mess_data['xml'] | |
100 html_elt = message_elt.addElement('html', NS_XHTML_IM) | |
101 body_elt = html_elt.addElement('body', NS_XHTML) | |
102 body_elt.addRawXml(xhtml_im) | |
103 mess_data['extra']['xhtml'] = xhtml_im | |
104 return mess_data | |
105 | |
106 rich = mess_data['extra'].pop('rich') | |
107 syntax = self.synt_plg.getCurrentSyntax(profile) | |
108 d = self.synt_plg.convert(rich, syntax, self.SYNTAX_XHTML_IM) | |
109 d.addCallback(syntax_converted) | |
92 return d | 110 return d |
93 | 111 |
94 def messageReceivedTrigger(self, message, post_treat, profile): | 112 def messageReceivedTrigger(self, message, post_treat, profile): |
95 """ Check presence of XHTML-IM in message | 113 """ Check presence of XHTML-IM in message |
96 """ | 114 """ |
99 body_elt = html_elt.elements(NS_XHTML, 'body').next() | 117 body_elt = html_elt.elements(NS_XHTML, 'body').next() |
100 # OK, we have found rich text | 118 # OK, we have found rich text |
101 post_treat.addCallback(self._messagePostTreat, body_elt) | 119 post_treat.addCallback(self._messagePostTreat, body_elt) |
102 except StopIteration: | 120 except StopIteration: |
103 # No XHTML-IM | 121 # No XHTML-IM |
122 pass | |
123 return True | |
124 | |
125 def sendMessageTrigger(self, mess_data, treatments, profile): | |
126 """ Check presence of rich text in extra | |
127 """ | |
128 try: | |
129 rich = mess_data['extra']['rich'] | |
130 # OK, we have found rich text | |
131 treatments.addCallback(self._sendMessageAddRich, profile) | |
132 except KeyError: | |
133 # No rich text found | |
104 pass | 134 pass |
105 return True | 135 return True |
106 | 136 |
107 def _purgeStyle(self, styles_raw): | 137 def _purgeStyle(self, styles_raw): |
108 """ Remove unauthorised styles according to the XEP-0071 | 138 """ Remove unauthorised styles according to the XEP-0071 |