Add 'thread' and 'parent_thread' interfaces to message stanzas.

These values are perisisted across replies.
This commit is contained in:
Lance Stout 2012-06-22 20:05:34 -07:00
parent 9cec284947
commit 82698672bb

View file

@ -7,7 +7,7 @@
""" """
from sleekxmpp.stanza.rootstanza import RootStanza from sleekxmpp.stanza.rootstanza import RootStanza
from sleekxmpp.xmlstream import StanzaBase from sleekxmpp.xmlstream import StanzaBase, ET
class Message(RootStanza): class Message(RootStanza):
@ -58,10 +58,10 @@ class Message(RootStanza):
namespace = 'jabber:client' namespace = 'jabber:client'
plugin_attrib = name plugin_attrib = name
interfaces = set(['type', 'to', 'from', 'id', 'body', 'subject', interfaces = set(['type', 'to', 'from', 'id', 'body', 'subject',
'mucroom', 'mucnick']) 'thread', 'parent_thread', 'mucroom', 'mucnick'])
sub_interfaces = set(['body', 'subject']) sub_interfaces = set(['body', 'subject', 'thread'])
lang_interfaces = sub_interfaces lang_interfaces = sub_interfaces
types = set([None, 'normal', 'chat', 'headline', 'error', 'groupchat']) types = set(['normal', 'chat', 'headline', 'error', 'groupchat'])
def get_type(self): def get_type(self):
""" """
@ -73,6 +73,31 @@ class Message(RootStanza):
""" """
return self._get_attr('type', 'normal') return self._get_attr('type', 'normal')
def get_parent_thread(self):
"""Return the message thread's parent thread."""
thread = self.xml.find('{%s}thread' % self.namespace)
if thread is not None:
return thread.attrib.get('parent', '')
return ''
def set_parent_thread(self, value):
"""Add or change the message thread's parent thread."""
thread = self.xml.find('{%s}thread' % self.namespace)
if value:
if thread is None:
thread = ET.Element('{%s}thread' % self.namespace)
self.xml.append(thread)
thread.attrib['parent'] = value
else:
if thread is not None and 'parent' in thread.attrib:
del thread.attrib['parent']
def del_parent_thread(self):
"""Delete the message thread's parent reference."""
thread = self.xml.find('{%s}thread' % self.namespace)
if thread is not None and 'parent' in thread.attrib:
del thread.attrib['parent']
def chat(self): def chat(self):
"""Set the message type to 'chat'.""" """Set the message type to 'chat'."""
self['type'] = 'chat' self['type'] = 'chat'
@ -97,10 +122,16 @@ class Message(RootStanza):
clear -- Indicates if existing content should be removed clear -- Indicates if existing content should be removed
before replying. Defaults to True. before replying. Defaults to True.
""" """
thread = self['thread']
parent = self['parent_thread']
StanzaBase.reply(self, clear) StanzaBase.reply(self, clear)
if self['type'] == 'groupchat': if self['type'] == 'groupchat':
self['to'] = self['to'].bare self['to'] = self['to'].bare
self['thread'] = thread
self['parent_thread'] = parent
del self['id'] del self['id']
if body is not None: if body is not None: