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 16:19:45 +00:00
|
|
|
|
|
|
|
from sleekxmpp.stanza.rootstanza import RootStanza
|
2012-02-18 19:40:34 +00:00
|
|
|
from sleekxmpp.xmlstream import StanzaBase
|
2010-08-03 16:19:45 +00:00
|
|
|
|
2009-12-10 01:23:03 +00:00
|
|
|
|
2010-01-05 21:56:48 +00:00
|
|
|
class Message(RootStanza):
|
2010-08-03 16:19:45 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
XMPP's <message> stanzas are a "push" mechanism to send information
|
|
|
|
to other XMPP entities without requiring a response.
|
|
|
|
|
|
|
|
Chat clients will typically use <message> stanzas that have a type
|
|
|
|
of either "chat" or "groupchat".
|
|
|
|
|
|
|
|
When handling a message event, be sure to check if the message is
|
|
|
|
an error response.
|
|
|
|
|
|
|
|
Example <message> stanzas:
|
|
|
|
<message to="user1@example.com" from="user2@example.com">
|
|
|
|
<body>Hi!</body>
|
|
|
|
</message>
|
|
|
|
|
|
|
|
<message type="groupchat" to="room@conference.example.com">
|
|
|
|
<body>Hi everyone!</body>
|
|
|
|
</message>
|
|
|
|
|
|
|
|
Stanza Interface:
|
|
|
|
body -- The main contents of the message.
|
|
|
|
subject -- An optional description of the message's contents.
|
|
|
|
mucroom -- (Read-only) The name of the MUC room that sent the message.
|
|
|
|
mucnick -- (Read-only) The MUC nickname of message's sender.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
types -- May be one of: normal, chat, headline, groupchat, or error.
|
|
|
|
|
|
|
|
Methods:
|
2010-10-18 01:38:22 +00:00
|
|
|
setup -- Overrides StanzaBase.setup.
|
|
|
|
chat -- Set the message type to 'chat'.
|
|
|
|
normal -- Set the message type to 'normal'.
|
|
|
|
reply -- Overrides StanzaBase.reply
|
|
|
|
get_type -- Overrides StanzaBase interface
|
|
|
|
get_mucroom -- Return the name of the MUC room of the message.
|
|
|
|
set_mucroom -- Dummy method to prevent assignment.
|
|
|
|
del_mucroom -- Dummy method to prevent deletion.
|
|
|
|
get_mucnick -- Return the MUC nickname of the message's sender.
|
|
|
|
set_mucnick -- Dummy method to prevent assignment.
|
|
|
|
del_mucnick -- Dummy method to prevent deletion.
|
2010-08-03 16:19:45 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
namespace = 'jabber:client'
|
|
|
|
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'))
|
|
|
|
|
2010-10-18 01:38:22 +00:00
|
|
|
def get_type(self):
|
2010-08-03 16:19:45 +00:00
|
|
|
"""
|
|
|
|
Return the message type.
|
|
|
|
|
|
|
|
Overrides default stanza interface behavior.
|
|
|
|
|
|
|
|
Returns 'normal' if no type attribute is present.
|
|
|
|
"""
|
2010-10-18 01:38:22 +00:00
|
|
|
return self._get_attr('type', 'normal')
|
2010-08-03 16:19:45 +00:00
|
|
|
|
|
|
|
def chat(self):
|
|
|
|
"""Set the message type to 'chat'."""
|
|
|
|
self['type'] = 'chat'
|
|
|
|
return self
|
|
|
|
|
|
|
|
def normal(self):
|
2011-12-30 19:08:32 +00:00
|
|
|
"""Set the message type to 'normal'."""
|
2010-08-03 16:19:45 +00:00
|
|
|
self['type'] = 'normal'
|
|
|
|
return self
|
|
|
|
|
2011-02-11 20:20:26 +00:00
|
|
|
def reply(self, body=None, clear=True):
|
2010-08-03 16:19:45 +00:00
|
|
|
"""
|
|
|
|
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:
|
2011-02-11 20:20:26 +00:00
|
|
|
body -- Optional text content for the message.
|
|
|
|
clear -- Indicates if existing content should be removed
|
|
|
|
before replying. Defaults to True.
|
2010-08-03 16:19:45 +00:00
|
|
|
"""
|
2011-06-20 23:25:56 +00:00
|
|
|
StanzaBase.reply(self, clear)
|
2010-08-03 16:19:45 +00:00
|
|
|
if self['type'] == 'groupchat':
|
|
|
|
self['to'] = self['to'].bare
|
|
|
|
|
|
|
|
del self['id']
|
|
|
|
|
|
|
|
if body is not None:
|
|
|
|
self['body'] = body
|
|
|
|
return self
|
|
|
|
|
2010-10-18 01:38:22 +00:00
|
|
|
def get_mucroom(self):
|
2010-08-03 16:19:45 +00:00
|
|
|
"""
|
|
|
|
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 ''
|
|
|
|
|
2010-10-18 01:38:22 +00:00
|
|
|
def get_mucnick(self):
|
2010-08-03 16:19:45 +00:00
|
|
|
"""
|
|
|
|
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 ''
|
|
|
|
|
2010-10-18 01:38:22 +00:00
|
|
|
def set_mucroom(self, value):
|
2010-08-03 16:19:45 +00:00
|
|
|
"""Dummy method to prevent modification."""
|
|
|
|
pass
|
|
|
|
|
2010-10-18 01:38:22 +00:00
|
|
|
def del_mucroom(self):
|
2010-08-03 16:19:45 +00:00
|
|
|
"""Dummy method to prevent deletion."""
|
|
|
|
pass
|
|
|
|
|
2010-10-18 01:38:22 +00:00
|
|
|
def set_mucnick(self, value):
|
2010-08-03 16:19:45 +00:00
|
|
|
"""Dummy method to prevent modification."""
|
|
|
|
pass
|
|
|
|
|
2010-10-18 01:38:22 +00:00
|
|
|
def del_mucnick(self):
|
2010-08-03 16:19:45 +00:00
|
|
|
"""Dummy method to prevent deletion."""
|
|
|
|
pass
|
2011-02-14 18:49:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
# To comply with PEP8, method names now use underscores.
|
|
|
|
# Deprecated method names are re-mapped for backwards compatibility.
|
|
|
|
Message.getType = Message.get_type
|
|
|
|
Message.getMucroom = Message.get_mucroom
|
|
|
|
Message.setMucroom = Message.set_mucroom
|
|
|
|
Message.delMucroom = Message.del_mucroom
|
|
|
|
Message.getMucnick = Message.get_mucnick
|
|
|
|
Message.setMucnick = Message.set_mucnick
|
|
|
|
Message.delMucnick = Message.del_mucnick
|