2010-03-26 21:32:16 +00:00
|
|
|
"""
|
|
|
|
SleekXMPP: The Sleek XMPP Library
|
|
|
|
Copyright (C) 2010 Nathanael C. Fritz
|
|
|
|
This file is part of SleekXMPP.
|
|
|
|
|
2010-07-20 15:19:49 +00:00
|
|
|
See the file LICENSE for copying permission.
|
2010-03-26 21:32:16 +00:00
|
|
|
"""
|
2010-08-03 21:58:18 +00:00
|
|
|
|
|
|
|
from sleekxmpp.stanza import Message
|
2010-10-18 01:38:22 +00:00
|
|
|
from sleekxmpp.xmlstream import ElementBase, ET, register_stanza_plugin
|
2010-08-03 21:58:18 +00:00
|
|
|
|
2009-12-15 04:37:10 +00:00
|
|
|
|
|
|
|
class HTMLIM(ElementBase):
|
2010-08-03 21:58:18 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
XEP-0071: XHTML-IM defines a method for embedding XHTML content
|
|
|
|
within a <message> stanza so that lightweight markup can be used
|
|
|
|
to format the message contents and to create links.
|
|
|
|
|
|
|
|
Only a subset of XHTML is recommended for use with XHTML-IM.
|
|
|
|
See the full spec at 'http://xmpp.org/extensions/xep-0071.html'
|
|
|
|
for more information.
|
|
|
|
|
|
|
|
Example stanza:
|
|
|
|
<message to="user@example.com">
|
|
|
|
<body>Non-html message content.</body>
|
|
|
|
<html xmlns="http://jabber.org/protocol/xhtml-im">
|
|
|
|
<body xmlns="http://www.w3.org/1999/xhtml">
|
|
|
|
<p><b>HTML!</b></p>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
</message>
|
|
|
|
|
|
|
|
Stanza Interface:
|
|
|
|
body -- The contents of the HTML body tag.
|
|
|
|
|
|
|
|
Methods:
|
2010-10-18 01:38:22 +00:00
|
|
|
setup -- Overrides ElementBase.setup.
|
|
|
|
get_body -- Return the HTML body contents.
|
|
|
|
set_body -- Set the HTML body contents.
|
|
|
|
del_body -- Remove the HTML body contents.
|
2010-08-03 21:58:18 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
namespace = 'http://jabber.org/protocol/xhtml-im'
|
|
|
|
name = 'html'
|
|
|
|
interfaces = set(('body',))
|
|
|
|
plugin_attrib = name
|
|
|
|
|
2010-10-18 01:38:22 +00:00
|
|
|
def setup(self, xml=None):
|
|
|
|
"""
|
|
|
|
Populate the stanza object using an optional XML object.
|
|
|
|
|
|
|
|
Overrides StanzaBase.setup.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
xml -- Use an existing XML object for the stanza's values.
|
|
|
|
"""
|
|
|
|
# To comply with PEP8, method names now use underscores.
|
|
|
|
# Deprecated method names are re-mapped for backwards compatibility.
|
|
|
|
self.setBody = self.set_body
|
|
|
|
self.getBody = self.get_body
|
|
|
|
self.delBody = self.del_body
|
|
|
|
|
|
|
|
return ElementBase.setup(self, xml)
|
|
|
|
|
|
|
|
def set_body(self, html):
|
2010-08-03 21:58:18 +00:00
|
|
|
"""
|
|
|
|
Set the contents of the HTML body.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
html -- Either a string or XML object. If the top level
|
|
|
|
element is not <body> with a namespace of
|
|
|
|
'http://www.w3.org/1999/xhtml', it will be wrapped.
|
|
|
|
"""
|
|
|
|
if isinstance(html, str):
|
|
|
|
html = ET.XML(html)
|
|
|
|
if html.tag != '{http://www.w3.org/1999/xhtml}body':
|
|
|
|
body = ET.Element('{http://www.w3.org/1999/xhtml}body')
|
|
|
|
body.append(html)
|
|
|
|
self.xml.append(body)
|
|
|
|
else:
|
|
|
|
self.xml.append(html)
|
|
|
|
|
2010-10-18 01:38:22 +00:00
|
|
|
def get_body(self):
|
2010-08-03 21:58:18 +00:00
|
|
|
"""Return the contents of the HTML body."""
|
|
|
|
html = self.xml.find('{http://www.w3.org/1999/xhtml}body')
|
|
|
|
if html is None:
|
|
|
|
return ''
|
|
|
|
return html
|
|
|
|
|
2010-10-18 01:38:22 +00:00
|
|
|
def del_body(self):
|
2010-08-03 21:58:18 +00:00
|
|
|
"""Remove the HTML body contents."""
|
|
|
|
if self.parent is not None:
|
|
|
|
self.parent().xml.remove(self.xml)
|
|
|
|
|
|
|
|
|
2010-10-18 01:38:22 +00:00
|
|
|
register_stanza_plugin(Message, HTMLIM)
|