comparison src/plugins/plugin_xep_0071.py @ 1955:633b5c21aefd

backend, frontend: messages refactoring (huge commit, not finished): /!\ database schema has been modified, do a backup before updating message have been refactored, here are the main changes: - languages are now handled - all messages have an uid (internal to SàT) - message updating is anticipated - subject is now first class - new naming scheme is used newMessage => messageNew, getHistory => historyGet, sendMessage => messageSend - minimal compatibility refactoring in quick_frontend/Primitivus, better refactoring should follow - threads handling - delayed messages are saved into history - info messages may also be saved in history (e.g. to keep track of people joining/leaving a room) - duplicate messages should be avoided - historyGet return messages in right order, no need to sort again - plugins have been updated to follow new features, some of them need to be reworked (e.g. OTR) - XEP-0203 (Delayed Delivery) is now fully handled in core, the plugin just handle disco and creation of a delay element - /!\ jp and Libervia are currently broken, as some features of Primitivus It has been put in one huge commit to avoid breaking messaging between changes. This is the main part of message refactoring, other commits will follow to take profit of the new features/behaviour.
author Goffi <goffi@goffi.org>
date Tue, 24 May 2016 22:11:04 +0200
parents 2daf7b4c6756
children a2bc5089c2eb
comparison
equal deleted inserted replaced
1943:ccfe45302a5c 1955:633b5c21aefd
20 from sat.core.i18n import _ 20 from sat.core.i18n import _
21 from sat.core import exceptions 21 from sat.core import exceptions
22 from sat.core.log import getLogger 22 from sat.core.log import getLogger
23 log = getLogger(__name__) 23 log = getLogger(__name__)
24 24
25 from twisted.internet import defer
25 from wokkel import disco, iwokkel 26 from wokkel import disco, iwokkel
26 from zope.interface import implements 27 from zope.interface import implements
27 # from lxml import etree 28 # from lxml import etree
28 try: 29 try:
29 from lxml import html 30 from lxml import html
73 SYNTAX_XHTML_IM = "XHTML-IM" 74 SYNTAX_XHTML_IM = "XHTML-IM"
74 75
75 def __init__(self, host): 76 def __init__(self, host):
76 log.info(_("XHTML-IM plugin initialization")) 77 log.info(_("XHTML-IM plugin initialization"))
77 self.host = host 78 self.host = host
78 self.synt_plg = self.host.plugins["TEXT-SYNTAXES"] 79 self._s = self.host.plugins["TEXT-SYNTAXES"]
79 self.synt_plg.addSyntax(self.SYNTAX_XHTML_IM, lambda xhtml: xhtml, self.XHTML2XHTML_IM, [self.synt_plg.OPT_HIDDEN]) 80 self._s.addSyntax(self.SYNTAX_XHTML_IM, lambda xhtml: xhtml, self.XHTML2XHTML_IM, [self._s.OPT_HIDDEN])
80 host.trigger.add("MessageReceived", self.messageReceivedTrigger) 81 host.trigger.add("MessageReceived", self.messageReceivedTrigger)
81 host.trigger.add("sendMessage", self.sendMessageTrigger) 82 host.trigger.add("messageSend", self.messageSendTrigger)
82 83
83 def getHandler(self, profile): 84 def getHandler(self, profile):
84 return XEP_0071_handler(self) 85 return XEP_0071_handler(self)
85 86
86 def _messagePostTreat(self, data, body_elt): 87 def _messagePostTreat(self, data, body_elts):
87 """ Callback which manage the post treatment of the message in case of XHTML-IM found 88 """Callback which manage the post treatment of the message in case of XHTML-IM found
88 @param data: data send by MessageReceived trigger through post_treat deferred 89 @param data: data send by MessageReceived trigger through post_treat deferred
89 @param xhtml_im: XHTML-IM body element found 90 @param body_elts: XHTML-IM body elements found
90 @return: the data with the extra parameter updated 91 @return: the data with the extra parameter updated
91 """ 92 """
92 #TODO: check if text only body is empty, then try to convert XHTML-IM to pure text and show a warning message 93 # TODO: check if text only body is empty, then try to convert XHTML-IM to pure text and show a warning message
93 def converted(xhtml): 94 def converted(xhtml):
94 data['extra']['xhtml'] = xhtml 95 if lang:
95 return data 96 data['extra']['xhtml_{}'.format(lang)] = xhtml
96 d = self.synt_plg.convert(body_elt.toXml(), self.SYNTAX_XHTML_IM, safe=True) 97 else:
97 d.addCallback(converted) 98 data['extra']['xhtml'] = xhtml
98 return d 99
99 100 defers = []
100 def _sendMessageAddRich(self, mess_data, profile): 101 for body_elt in body_elts:
102 lang = body_elt.getAttribute('xml:lang', '')
103 d = self._s.convert(body_elt.toXml(), self.SYNTAX_XHTML_IM, safe=True)
104 d.addCallback(converted, lang)
105 defers.append(d)
106
107 d_list = defer.DeferredList(defers)
108 d_list.addCallback(lambda dummy: data)
109 return d_list
110
111 def _messageSendAddRich(self, data, client):
101 """ Construct XHTML-IM node and add it XML element 112 """ Construct XHTML-IM node and add it XML element
102 @param mess_data: message data as sended by sendMessage callback 113
103 """ 114 @param data: message data as sended by messageSend callback
104 def syntax_converted(xhtml_im): 115 """
105 message_elt = mess_data['xml'] 116 # at this point, either ['extra']['rich'] or ['extra']['xhtml'] exists
106 html_elt = message_elt.addElement('html', NS_XHTML_IM) 117 # but both can't exist at the same time
107 body_elt = html_elt.addElement('body', NS_XHTML) 118 message_elt = data['xml']
119 html_elt = message_elt.addElement((NS_XHTML_IM, 'html'))
120
121 def syntax_converted(xhtml_im, lang):
122 body_elt = html_elt.addElement((NS_XHTML, 'body'))
123 if lang:
124 body_elt['xml:lang'] = lang
125 data['extra']['xhtml_{}'.format(lang)] = xhtml_im
126 else:
127 data['extra']['xhtml'] = xhtml_im
108 body_elt.addRawXml(xhtml_im) 128 body_elt.addRawXml(xhtml_im)
109 mess_data['extra']['xhtml'] = xhtml_im 129
110 return mess_data 130 syntax = self._s.getCurrentSyntax(client.profile)
111 131 defers = []
112 syntax = self.synt_plg.getCurrentSyntax(profile) 132 try:
113 rich = mess_data['extra'].get('rich', '') 133 rich = data['extra']['rich']
114 xhtml = mess_data['extra'].get('xhtml', '') 134 except KeyError:
115 if rich: 135 # we have directly XHTML
116 d = self.synt_plg.convert(rich, syntax, self.SYNTAX_XHTML_IM) 136 for lang, xhtml in data['extra']['xhtml'].iteritems():
117 if xhtml: 137 d = self._s.convert(xhtml, self._s.SYNTAX_XHTML, self.SYNTAX_XHTML_IM)
118 raise exceptions.DataError(_("Can't have xhtml and rich content at the same time")) 138 d.addCallback(syntax_converted, lang)
119 d.addCallback(syntax_converted) 139 defers.append(d)
120 return d 140 else:
121 141 # we have rich syntax to convert
122 def messageReceivedTrigger(self, message, post_treat, profile): 142 for lang, rich_data in rich.iteritems():
143 d = self._s.convert(rich_data, syntax, self.SYNTAX_XHTML_IM)
144 d.addCallback(syntax_converted, lang)
145 defers.append(d)
146 d_list = defer.DeferredList(defers)
147 d_list.addCallback(lambda dummy: data)
148 return d_list
149
150 def messageReceivedTrigger(self, client, message, post_treat):
123 """ Check presence of XHTML-IM in message 151 """ Check presence of XHTML-IM in message
124 """ 152 """
125 try: 153 try:
126 html_elt = message.elements(NS_XHTML_IM, 'html').next() 154 html_elt = message.elements(NS_XHTML_IM, 'html').next()
127 body_elt = html_elt.elements(NS_XHTML, 'body').next()
128 # OK, we have found rich text
129 post_treat.addCallback(self._messagePostTreat, body_elt)
130 except StopIteration: 155 except StopIteration:
131 # No XHTML-IM 156 # No XHTML-IM
132 pass 157 pass
158 else:
159 body_elts = html_elt.elements(NS_XHTML, 'body')
160 post_treat.addCallback(self._messagePostTreat, body_elts)
133 return True 161 return True
134 162
135 def sendMessageTrigger(self, mess_data, pre_xml_treatments, post_xml_treatments, profile): 163 def messageSendTrigger(self, client, data, pre_xml_treatments, post_xml_treatments):
136 """ Check presence of rich text in extra 164 """ Check presence of rich text in extra
137 """ 165 """
138 if 'rich' in mess_data['extra'] or 'xhtml' in mess_data['extra']: 166 rich = {}
139 post_xml_treatments.addCallback(self._sendMessageAddRich, profile) 167 xhtml = {}
168 for key, value in data['extra'].iteritems():
169 if key.startswith('rich'):
170 rich[key[5:]] = value
171 elif key.startswith('xhtml'):
172 xhtml[key[6:]] = value
173 if rich and xhtml:
174 raise exceptions.DataError(_(u"Can't have XHTML and rich content at the same time"))
175 if rich or xhtml:
176 if rich:
177 data['rich'] = rich
178 else:
179 data['xhtml'] = xhtml
180 post_xml_treatments.addCallback(self._messageSendAddRich, client)
140 return True 181 return True
141 182
142 def _purgeStyle(self, styles_raw): 183 def _purgeStyle(self, styles_raw):
143 """ Remove unauthorised styles according to the XEP-0071 184 """ Remove unauthorised styles according to the XEP-0071
144 @param styles_raw: raw styles (value of the style attribute) 185 @param styles_raw: raw styles (value of the style attribute)