mirror of
https://github.com/correl/SleekXMPP.git
synced 2024-11-24 03:00:15 +00:00
Updated message stanzas and tests with documentation and PEP8 style.
This commit is contained in:
parent
851e90c572
commit
939ae298c2
2 changed files with 170 additions and 89 deletions
|
@ -5,59 +5,139 @@
|
||||||
|
|
||||||
See the file LICENSE for copying permission.
|
See the file LICENSE for copying permission.
|
||||||
"""
|
"""
|
||||||
from .. xmlstream.stanzabase import StanzaBase
|
|
||||||
from xml.etree import cElementTree as ET
|
from sleekxmpp.stanza import Error
|
||||||
from . error import Error
|
from sleekxmpp.stanza.rootstanza import RootStanza
|
||||||
from . rootstanza import RootStanza
|
from sleekxmpp.xmlstream.stanzabase import StanzaBase, ET
|
||||||
|
|
||||||
|
|
||||||
class Message(RootStanza):
|
class Message(RootStanza):
|
||||||
interfaces = set(('type', 'to', 'from', 'id', 'body', 'subject', 'mucroom', 'mucnick'))
|
|
||||||
types = set((None, 'normal', 'chat', 'headline', 'error', 'groupchat'))
|
|
||||||
sub_interfaces = set(('body', 'subject'))
|
|
||||||
name = 'message'
|
|
||||||
plugin_attrib = name
|
|
||||||
namespace = 'jabber:client'
|
|
||||||
|
|
||||||
def getType(self):
|
"""
|
||||||
return self.xml.attrib.get('type', 'normal')
|
XMPP's <message> stanzas are a "push" mechanism to send information
|
||||||
|
to other XMPP entities without requiring a response.
|
||||||
def chat(self):
|
|
||||||
self['type'] = 'chat'
|
Chat clients will typically use <message> stanzas that have a type
|
||||||
return self
|
of either "chat" or "groupchat".
|
||||||
|
|
||||||
def normal(self):
|
When handling a message event, be sure to check if the message is
|
||||||
self['type'] = 'normal'
|
an error response.
|
||||||
return self
|
|
||||||
|
Example <message> stanzas:
|
||||||
def reply(self, body=None):
|
<message to="user1@example.com" from="user2@example.com">
|
||||||
StanzaBase.reply(self)
|
<body>Hi!</body>
|
||||||
if self['type'] == 'groupchat':
|
</message>
|
||||||
self['to'] = self['to'].bare
|
|
||||||
del self['id']
|
<message type="groupchat" to="room@conference.example.com">
|
||||||
if body is not None:
|
<body>Hi everyone!</body>
|
||||||
self['body'] = body
|
</message>
|
||||||
return self
|
|
||||||
|
Stanza Interface:
|
||||||
def getMucroom(self):
|
body -- The main contents of the message.
|
||||||
if self['type'] == 'groupchat':
|
subject -- An optional description of the message's contents.
|
||||||
return self['from'].bare
|
mucroom -- (Read-only) The name of the MUC room that sent the message.
|
||||||
else:
|
mucnick -- (Read-only) The MUC nickname of message's sender.
|
||||||
return ''
|
|
||||||
|
Attributes:
|
||||||
def setMucroom(self, value):
|
types -- May be one of: normal, chat, headline, groupchat, or error.
|
||||||
pass
|
|
||||||
|
Methods:
|
||||||
def delMucroom(self):
|
chat -- Set the message type to 'chat'.
|
||||||
pass
|
normal -- Set the message type to 'normal'.
|
||||||
|
reply -- Overrides StanzaBase.reply
|
||||||
def getMucnick(self):
|
getType -- Overrides StanzaBase interface
|
||||||
if self['type'] == 'groupchat':
|
getMucroom -- Return the name of the MUC room of the message.
|
||||||
return self['from'].resource
|
setMucroom -- Dummy method to prevent assignment.
|
||||||
else:
|
delMucroom -- Dummy method to prevent deletion.
|
||||||
return ''
|
getMucnick -- Return the MUC nickname of the message's sender.
|
||||||
|
setMucnick -- Dummy method to prevent assignment.
|
||||||
def setMucnick(self, value):
|
delMucnick -- Dummy method to prevent deletion.
|
||||||
pass
|
"""
|
||||||
|
|
||||||
def delMucnick(self):
|
namespace = 'jabber:client'
|
||||||
pass
|
name = 'message'
|
||||||
|
interfaces = set(('type', 'to', 'from', 'id', 'body', 'subject',
|
||||||
|
'mucroom', 'mucnick'))
|
||||||
|
sub_interfaces = set(('body', 'subject'))
|
||||||
|
plugin_attrib = name
|
||||||
|
types = set((None, 'normal', 'chat', 'headline', 'error', 'groupchat'))
|
||||||
|
|
||||||
|
def getType(self):
|
||||||
|
"""
|
||||||
|
Return the message type.
|
||||||
|
|
||||||
|
Overrides default stanza interface behavior.
|
||||||
|
|
||||||
|
Returns 'normal' if no type attribute is present.
|
||||||
|
"""
|
||||||
|
return self._getAttr('type', 'normal')
|
||||||
|
|
||||||
|
def chat(self):
|
||||||
|
"""Set the message type to 'chat'."""
|
||||||
|
self['type'] = 'chat'
|
||||||
|
return self
|
||||||
|
|
||||||
|
def normal(self):
|
||||||
|
"""Set the message type to 'chat'."""
|
||||||
|
self['type'] = 'normal'
|
||||||
|
return self
|
||||||
|
|
||||||
|
def reply(self, body=None):
|
||||||
|
"""
|
||||||
|
Create a message reply.
|
||||||
|
|
||||||
|
Overrides StanzaBase.reply.
|
||||||
|
|
||||||
|
Sets proper 'to' attribute if the message is from a MUC, and
|
||||||
|
adds a message body if one is given.
|
||||||
|
|
||||||
|
Arguments:
|
||||||
|
body -- Optional text content for the message.
|
||||||
|
"""
|
||||||
|
StanzaBase.reply(self)
|
||||||
|
if self['type'] == 'groupchat':
|
||||||
|
self['to'] = self['to'].bare
|
||||||
|
|
||||||
|
del self['id']
|
||||||
|
|
||||||
|
if body is not None:
|
||||||
|
self['body'] = body
|
||||||
|
return self
|
||||||
|
|
||||||
|
def getMucroom(self):
|
||||||
|
"""
|
||||||
|
Return the name of the MUC room where the message originated.
|
||||||
|
|
||||||
|
Read-only stanza interface.
|
||||||
|
"""
|
||||||
|
if self['type'] == 'groupchat':
|
||||||
|
return self['from'].bare
|
||||||
|
else:
|
||||||
|
return ''
|
||||||
|
|
||||||
|
def getMucnick(self):
|
||||||
|
"""
|
||||||
|
Return the nickname of the MUC user that sent the message.
|
||||||
|
|
||||||
|
Read-only stanza interface.
|
||||||
|
"""
|
||||||
|
if self['type'] == 'groupchat':
|
||||||
|
return self['from'].resource
|
||||||
|
else:
|
||||||
|
return ''
|
||||||
|
|
||||||
|
def setMucroom(self, value):
|
||||||
|
"""Dummy method to prevent modification."""
|
||||||
|
pass
|
||||||
|
|
||||||
|
def delMucroom(self):
|
||||||
|
"""Dummy method to prevent deletion."""
|
||||||
|
pass
|
||||||
|
|
||||||
|
def setMucnick(self, value):
|
||||||
|
"""Dummy method to prevent modification."""
|
||||||
|
pass
|
||||||
|
|
||||||
|
def delMucnick(self):
|
||||||
|
"""Dummy method to prevent deletion."""
|
||||||
|
pass
|
||||||
|
|
|
@ -2,44 +2,45 @@ from sleektest import *
|
||||||
from sleekxmpp.stanza.message import Message
|
from sleekxmpp.stanza.message import Message
|
||||||
from sleekxmpp.stanza.htmlim import HTMLIM
|
from sleekxmpp.stanza.htmlim import HTMLIM
|
||||||
|
|
||||||
|
|
||||||
class TestMessageStanzas(SleekTest):
|
class TestMessageStanzas(SleekTest):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
registerStanzaPlugin(Message, HTMLIM)
|
registerStanzaPlugin(Message, HTMLIM)
|
||||||
|
|
||||||
def testGroupchatReplyRegression(self):
|
|
||||||
"Regression groupchat reply should be to barejid"
|
|
||||||
msg = self.Message()
|
|
||||||
msg['to'] = 'me@myserver.tld'
|
|
||||||
msg['from'] = 'room@someservice.someserver.tld/somenick'
|
|
||||||
msg['type'] = 'groupchat'
|
|
||||||
msg['body'] = "this is a message"
|
|
||||||
msg.reply()
|
|
||||||
self.failUnless(str(msg['to']) == 'room@someservice.someserver.tld')
|
|
||||||
|
|
||||||
def testAttribProperty(self):
|
def testGroupchatReplyRegression(self):
|
||||||
"Test attrib property returning self"
|
"Regression groupchat reply should be to barejid"
|
||||||
msg = self.Message()
|
msg = self.Message()
|
||||||
msg.attrib.attrib.attrib['to'] = 'usr@server.tld'
|
msg['to'] = 'me@myserver.tld'
|
||||||
self.failUnless(str(msg['to']) == 'usr@server.tld')
|
msg['from'] = 'room@someservice.someserver.tld/somenick'
|
||||||
|
msg['type'] = 'groupchat'
|
||||||
def testHTMLPlugin(self):
|
msg['body'] = "this is a message"
|
||||||
"Test message/html/html stanza"
|
msg.reply()
|
||||||
msg = self.Message()
|
self.failUnless(str(msg['to']) == 'room@someservice.someserver.tld')
|
||||||
msg['to'] = "fritzy@netflint.net/sleekxmpp"
|
|
||||||
msg['body'] = "this is the plaintext message"
|
def testAttribProperty(self):
|
||||||
msg['type'] = 'chat'
|
"Test attrib property returning self"
|
||||||
p = ET.Element('{http://www.w3.org/1999/xhtml}p')
|
msg = self.Message()
|
||||||
p.text = "This is the htmlim message"
|
msg.attrib.attrib.attrib['to'] = 'usr@server.tld'
|
||||||
msg['html']['html'] = p
|
self.failUnless(str(msg['to']) == 'usr@server.tld')
|
||||||
self.checkMessage(msg, """
|
|
||||||
<message to="fritzy@netflint.net/sleekxmpp" type="chat">
|
def testHTMLPlugin(self):
|
||||||
<body>this is the plaintext message</body>
|
"Test message/html/html stanza"
|
||||||
<html xmlns="http://jabber.org/protocol/xhtml-im">
|
msg = self.Message()
|
||||||
<body xmlns="http://www.w3.org/1999/xhtml">
|
msg['to'] = "fritzy@netflint.net/sleekxmpp"
|
||||||
<p>This is the htmlim message</p>
|
msg['body'] = "this is the plaintext message"
|
||||||
</body>
|
msg['type'] = 'chat'
|
||||||
</html>
|
p = ET.Element('{http://www.w3.org/1999/xhtml}p')
|
||||||
</message>""")
|
p.text = "This is the htmlim message"
|
||||||
|
msg['html']['html'] = p
|
||||||
|
self.checkMessage(msg, """
|
||||||
|
<message to="fritzy@netflint.net/sleekxmpp" type="chat">
|
||||||
|
<body>this is the plaintext message</body>
|
||||||
|
<html xmlns="http://jabber.org/protocol/xhtml-im">
|
||||||
|
<body xmlns="http://www.w3.org/1999/xhtml">
|
||||||
|
<p>This is the htmlim message</p>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
</message>""")
|
||||||
|
|
||||||
suite = unittest.TestLoader().loadTestsFromTestCase(TestMessageStanzas)
|
suite = unittest.TestLoader().loadTestsFromTestCase(TestMessageStanzas)
|
||||||
|
|
Loading…
Reference in a new issue