plugin XEP-0071: rich messages management for sendMessage
This commit is contained in:
parent
1769ca0492
commit
7fb9d0c309
1 changed files with 35 additions and 5 deletions
|
|
@ -69,9 +69,10 @@ class XEP_0071(object):
|
|||
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 @@ class XEP_0071(object):
|
|||
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 @@ class XEP_0071(object):
|
|||
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)
|
||||
|
|
|
|||
Loading…
Reference in a new issue