# Slixmpp: The Slick XMPP Library# Copyright (C) 2010 Nathanael C. Fritz# This file is part of Slixmpp.# See the file LICENSE for copying permission.fromslixmpp.stanza.rootstanzaimportRootStanzafromslixmpp.xmlstreamimportStanzaBase,ETORIGIN_NAME='{urn:xmpp:sid:0}origin-id'
[docs]classMessage(RootStanza):""" 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: .. code-block:: xml <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. """name='message'namespace='jabber:client'plugin_attrib=nameinterfaces={'type','to','from','id','body','subject','thread','parent_thread','mucroom','mucnick'}sub_interfaces={'body','subject','thread'}lang_interfaces=sub_interfacestypes={'normal','chat','headline','error','groupchat'}def__init__(self,*args,recv=False,**kwargs):""" Initialize a new <message /> stanza with an optional 'id' value. Overrides StanzaBase.__init__. """StanzaBase.__init__(self,*args,**kwargs)ifnotrecvandself['id']=='':ifself.stream:use_ids=getattr(self.stream,'use_message_ids',None)ifuse_ids:self.set_id(self.stream.new_id())else:self.del_origin_id()
[docs]defget_type(self):""" Return the message type. Overrides default stanza interface behavior. Returns 'normal' if no type attribute is present. :rtype: str """returnself._get_attr('type','normal')
[docs]defget_parent_thread(self):"""Return the message thread's parent thread. :rtype: str """thread=self.xml.find('{%s}thread'%self.namespace)ifthreadisnotNone:returnthread.attrib.get('parent','')return''
[docs]defset_parent_thread(self,value):"""Add or change the message thread's parent thread. :param str value: identifier of the thread"""thread=self.xml.find('{%s}thread'%self.namespace)ifvalue:ifthreadisNone:thread=ET.Element('{%s}thread'%self.namespace)self.xml.append(thread)thread.attrib['parent']=valueelse:ifthreadisnotNoneand'parent'inthread.attrib:delthread.attrib['parent']
[docs]defdel_parent_thread(self):"""Delete the message thread's parent reference."""thread=self.xml.find('{%s}thread'%self.namespace)ifthreadisnotNoneand'parent'inthread.attrib:delthread.attrib['parent']
[docs]defchat(self):"""Set the message type to 'chat'."""self['type']='chat'returnself
[docs]defnormal(self):"""Set the message type to 'normal'."""self['type']='normal'returnself
[docs]defreply(self,body=None,clear=True):""" 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. :param str body: Optional text content for the message. :param bool clear: Indicates if existing content should be removed before replying. Defaults to True. :rtype: :class:`~.Message` """new_message=StanzaBase.reply(self,clear)ifnotgetattr(self.stream,"is_component",False)andself['type']=='groupchat':new_message['to']=new_message['to'].barenew_message['thread']=self['thread']new_message['parent_thread']=self['parent_thread']delnew_message['id']ifself.streamisnotNoneandself.stream.use_message_ids:new_message['id']=self.stream.new_id()ifbodyisnotNone:new_message['body']=bodyreturnnew_message
[docs]defget_mucroom(self):""" Return the name of the MUC room where the message originated. Read-only stanza interface. :rtype: str """ifself['type']=='groupchat':returnself['from'].bareelse:return''
[docs]defget_mucnick(self):""" Return the nickname of the MUC user that sent the message. Read-only stanza interface. :rtype: str """ifself['type']=='groupchat':returnself['from'].resourceelse:return''
[docs]defset_mucroom(self,value):"""Dummy method to prevent modification."""pass
[docs]defdel_mucroom(self):"""Dummy method to prevent deletion."""pass
[docs]defset_mucnick(self,value):"""Dummy method to prevent modification."""pass
[docs]defdel_mucnick(self):"""Dummy method to prevent deletion."""pass