def history(self, roomJID, messages): """ Send history to create a MUC based on a one on one chat. See: http://xmpp.org/extensions/xep-0045.html#continue @param roomJID: The room jabber/xmpp entity id. @type roomJID: L{JID<twisted.words.protocols.jabber.jid.JID>} @param messages: The history to send to the room as an ordered list of message, represented by a dictionary with the keys L{'stanza'}, holding the original stanza a L{domish.Element}, and L{'timestamp'} with the timestamp. @type messages: L{list} of L{domish.Element} """ for message in messages: stanza = message['stanza'] stanza['type'] = 'groupchat' delay = Delay(stamp=message['timestamp']) sender = stanza.getAttribute('from') if sender is not None: delay.sender = jid.JID(sender) stanza.addChild(delay.toElement()) stanza['to'] = roomJID.userhost() if stanza.hasAttribute('from'): del stanza['from'] self.xmlstream.send(stanza)
def history(self, roomJID, messages): """ Send history to create a MUC based on a one on one chat. See: http://xmpp.org/extensions/xep-0045.html#continue @param roomJID: The room jabber/xmpp entity id. @type roomJID: L{JID<twisted.words.protocols.jabber.jid.JID>} @param messages: The history to send to the room as an ordered list of message, represented by a dictionary with the keys C{'stanza'}, holding the original stanza a L{domish.Element}, and C{'timestamp'} with the timestamp. @type messages: C{list} of L{domish.Element} """ for message in messages: stanza = message["stanza"] stanza["type"] = "groupchat" delay = Delay(stamp=message["timestamp"]) sender = stanza.getAttribute("from") if sender is not None: delay.sender = jid.JID(sender) stanza.addChild(delay.toElement()) stanza["to"] = roomJID.userhost() if stanza.hasAttribute("from"): del stanza["from"] self.xmlstream.send(stanza)
def test_toElement(self): """ The DOM structure has the serialized timestamp and sender address. """ delay = Delay(stamp=datetime(2002, 9, 10, 23, 8, 25, tzinfo=dateutil.tz.tzutc()), sender=JID(u'*****@*****.**')) element = delay.toElement() self.assertEqual(u'urn:xmpp:delay', element.uri) self.assertEqual(u'delay', element.name) self.assertEqual(u'2002-09-10T23:08:25Z', element.getAttribute('stamp')) self.assertEqual(u'*****@*****.**', element.getAttribute('from'))
def test_toElementLegacy(self): """ The legacy format uses C{CCYYMMDDThh:mm:ss} in the old namespace. """ delay = Delay(stamp=datetime(2002, 9, 10, 23, 8, 25, tzinfo=dateutil.tz.tzutc()), sender=JID(u'*****@*****.**')) element = delay.toElement(legacy=True) self.assertEqual(u'jabber:x:delay', element.uri) self.assertEqual(u'x', element.name) self.assertEqual(u'20020910T23:08:25', element.getAttribute('stamp')) self.assertEqual(u'*****@*****.**', element.getAttribute('from'))
def test_fromElementMissingStamp(self): """ A missing timestamp results in C{None} for the stamp attribute. """ xml = parseXml(u""" <delay xmlns="urn:xmpp:delay"/> """) delay = Delay.fromElement(xml) self.assertIdentical(None, delay.stamp)
def test_fromElementBadStamp(self): """ A malformed timestamp results in C{None} for the stamp attribute. """ xml = parseXml(u""" <delay xmlns="urn:xmpp:delay" stamp="foobar"/> """) delay = Delay.fromElement(xml) self.assertIdentical(None, delay.stamp)
def test_fromElementSenderBad(self): """ An invalid original sender address results in C{None}. """ xml = parseXml(u""" <delay xmlns="urn:xmpp:delay" stamp="2002-09-10T23:08:25Z" from="user@@example.org"/> """) delay = Delay.fromElement(xml) self.assertIdentical(None, delay.sender)
def test_fromElementSender(self): """ The optional original sender address is parsed as a JID. """ xml = parseXml(u""" <delay xmlns="urn:xmpp:delay" stamp="2002-09-10T23:08:25Z" from="*****@*****.**"/> """) delay = Delay.fromElement(xml) self.assertEqual(JID(u'*****@*****.**'), delay.sender)
def test_fromElementLegacy(self): """ For legacy XEP-0091 support, the timestamp is assumed to be in UTC. """ xml = parseXml(u""" <x xmlns="jabber:x:delay" stamp="20020910T23:08:25"/> """) delay = Delay.fromElement(xml) self.assertEqual(datetime(2002, 9, 10, 23, 8, 25, tzinfo=dateutil.tz.tzutc()), delay.stamp) self.assertIdentical(None, delay.sender)
def test_fromElement(self): """ The timestamp is parsed with the proper timezone (UTC). """ xml = parseXml(u""" <delay xmlns="urn:xmpp:delay" stamp="2002-09-10T23:08:25Z"/> """) delay = Delay.fromElement(xml) self.assertEqual(datetime(2002, 9, 10, 23, 8, 25, tzinfo=dateutil.tz.tzutc()), delay.stamp) self.assertIdentical(None, delay.sender)
def test_fromElement(self): """ The timestamp is parsed with the proper timezone (UTC). """ xml = parseXml(u""" <delay xmlns="urn:xmpp:delay" stamp="2002-09-10T23:08:25Z"/> """) delay = Delay.fromElement(xml) self.assertEqual( datetime(2002, 9, 10, 23, 8, 25, tzinfo=dateutil.tz.tzutc()), delay.stamp) self.assertIdentical(None, delay.sender)
def test_fromElementLegacy(self): """ For legacy XEP-0091 support, the timestamp is assumed to be in UTC. """ xml = parseXml(u""" <x xmlns="jabber:x:delay" stamp="20020910T23:08:25"/> """) delay = Delay.fromElement(xml) self.assertEqual( datetime(2002, 9, 10, 23, 8, 25, tzinfo=dateutil.tz.tzutc()), delay.stamp) self.assertIdentical(None, delay.sender)
def test_toElementStampMissing(self): """ To render to XML, at least a timestamp must be provided. """ delay = Delay(stamp=None) self.assertRaises(ValueError, delay.toElement)
def test_toElementStampOffsetNaive(self): """ The provided timestamp must be offset aware. """ delay = Delay(stamp=datetime(2002, 9, 10, 23, 8, 25)) self.assertRaises(ValueError, delay.toElement)