def get_vcard(self, jid, callback=None): if not callback: callback = self.vcard_callback q = Iq(to_jid=jid.bare(), stanza_type='get') q.add_payload(VCardPayload()) self.stanza_processor.set_response_handlers(q, callback, callback) self.send(q)
def test_iq_result_empty(self): iq = Iq( to_jid=JID("[email protected]/res"), from_jid=JID("*****@*****.**"), stanza_type="result", stanza_id=2 ) self.check_iq4(iq) xml = iq.as_xml() self.check_iq4(Iq(xml))
def _set_vcard(self, nick=None, avatar=None, stanza=None): #FIXME: This doesn't seem to work with jabber.org q = Iq( from_jid=self.jid, stanza_type='set', ) vc = ET.Element("{vcard-temp}vCard") if nick is not None: n = ET.SubElement(vc, '{vcard-temp}FN') n.text = nick if avatar is not None: type, picfile = avatar photo = ET.SubElement(vc, '{vcard-temp}PHOTO') t = ET.SubElement(photo, '{vcard-temp}TYPE') t.text = type d = ET.SubElement(photo, '{vcard-temp}BINVAL') data = open(picfile, 'rb').read() d.text = base64.b64encode(data).decode('ascii') self.avatar_hash = hashlib.new('sha1', data).hexdigest() q.add_payload(vc) self.stanza_processor.set_response_handlers(q, self._set_vcard_callback, self._set_vcard_callback) self.send(q)
def get_vcard(self, jid, callback): '''callback is used as both result handler and error handler''' q = Iq(to_jid=jid.bare(), stanza_type='get') vc = ET.Element("{vcard-temp}vCard") q.add_payload(vc) self.stanza_processor.set_response_handlers(q, callback, callback) self.send(q)
def get_vcard(self, jid, callback): """callback is used as both result handler and error handler""" q = Iq(to_jid=jid.bare(), stanza_type="get") vc = ET.Element("{vcard-temp}vCard") q.add_payload(vc) self.stanza_processor.set_response_handlers(q, callback, callback) self.send(q)
def test_iq_make_result_response(self): iq = Iq(ElementTree.XML(IQ1)) iq2 = iq.make_result_response() payload = ElementTree.Element("{http://pyxmpp.jajcus.net/xmlns/test}payload") ElementTree.SubElement(payload, "{http://pyxmpp.jajcus.net/xmlns/test}abc") payload = XMLPayload(payload) iq2.add_payload(payload) self.check_iq2(iq2)
def test_iq_result_empty(self): iq = Iq(to_jid=JID("[email protected]/res"), from_jid=JID("*****@*****.**"), stanza_type="result", stanza_id=2) self.check_iq4(iq) xml = iq.as_xml() self.check_iq4(Iq(xml))
def test_iq_set(self): iq = Iq(from_jid=JID("[email protected]/res"), to_jid=JID("*****@*****.**"), stanza_type="set", stanza_id=2) payload = ElementTree.Element("{http://pyxmpp.jajcus.net/xmlns/test}payload") ElementTree.SubElement(payload, "{http://pyxmpp.jajcus.net/xmlns/test}abc") payload = XMLPayload(payload) iq.add_payload(payload) self.check_iq3(iq) xml = iq.as_xml() self.check_iq3(Iq(xml))
def test_iq_make_result_response(self): iq = Iq(ElementTree.XML(IQ1)) iq2 = iq.make_result_response() payload = ElementTree.Element( "{http://pyxmpp.jajcus.net/xmlns/test}payload") ElementTree.SubElement(payload, "{http://pyxmpp.jajcus.net/xmlns/test}abc") payload = XMLPayload(payload) iq2.add_payload(payload) self.check_iq2(iq2)
def get_vcard(self, jid=None, callback=None): '''callback is used as both result handler and error handler''' q = Iq( to_jid = jid and jid.bare(), stanza_type = 'get', ) vc = ET.Element("{vcard-temp}vCard") q.add_payload(vc) if callback: self.stanza_processor.set_response_handlers(q, callback, callback) self.send(q)
def test_bad_request(self): provider = VersionProvider() processor = Processor([provider]) stanza = Iq(ElementTree.XML(IQ2)) stanza.stanza_type = "set" processor.uplink_receive(stanza) self.assertEqual(len(processor.stanzas_sent), 1) response = processor.stanzas_sent[0] self.assertIsInstance(response, Iq) self.assertEqual(response.stanza_type, "error") self.assertEqual(response.error.condition.tag, "{urn:ietf:params:xml:ns:xmpp-stanzas}service-unavailable")
def test_bad_request(self): provider = VersionProvider() processor = Processor([provider]) stanza = Iq(ElementTree.XML(IQ2)) stanza.stanza_type = 'set' processor.uplink_receive(stanza) self.assertEqual(len(processor.stanzas_sent), 1) response = processor.stanzas_sent[0] self.assertIsInstance(response, Iq) self.assertEqual(response.stanza_type, "error") self.assertEqual( response.error.condition.tag, "{urn:ietf:params:xml:ns:xmpp-stanzas}service-unavailable")
def _set_vcard(self, nick=None, avatar=None, stanza=None): #FIXME: This doesn't seem to work with jabber.org q = Iq( from_jid = self.jid, stanza_type = 'set', ) vc = ET.Element("{vcard-temp}vCard") if nick is not None: n = ET.SubElement(vc, '{vcard-temp}FN') n.text = nick if avatar is not None: type, picfile = avatar photo = ET.SubElement(vc, '{vcard-temp}PHOTO') t = ET.SubElement(photo, '{vcard-temp}TYPE') t.text = type d = ET.SubElement(photo, '{vcard-temp}BINVAL') data = open(picfile, 'rb').read() d.text = base64.b64encode(data).decode('ascii') self.avatar_hash = hashlib.new('sha1', data).hexdigest() q.add_payload(vc) self.stanza_processor.set_response_handlers( q, self._set_vcard_callback, self._set_vcard_callback) self.send(q)
def test_defaults(self): provider = VersionProvider() processor = Processor([provider]) stanza = Iq(ElementTree.XML(IQ1)) processor.uplink_receive(stanza) self.assertEqual(len(processor.stanzas_sent), 1) response = processor.stanzas_sent[0] self.assertIsInstance(response, Iq) self.assertEqual(response.stanza_type, "result") payload = response.get_payload(VersionPayload) self.assertIsInstance(payload, VersionPayload) self.assertEqual(payload.name, "PyXMPP2") self.assertEqual(payload.version, pyxmpp2.version.version) expected = u" ".join( (platform.system(), platform.release(), platform.machine())) self.assertEqual(payload.os_name, expected)
def test_custom(self): settings = XMPPSettings({ "software_name": "NAME", "software_version": "VERSION", "software_os": "OS", }) provider = VersionProvider(settings) processor = Processor([provider]) stanza = Iq(ElementTree.XML(IQ1)) processor.uplink_receive(stanza) self.assertEqual(len(processor.stanzas_sent), 1) response = processor.stanzas_sent[0] self.assertIsInstance(response, Iq) self.assertEqual(response.stanza_type, "result") payload = response.get_payload(VersionPayload) self.assertIsInstance(payload, VersionPayload) self.assertEqual(payload.name, "NAME") self.assertEqual(payload.version, "VERSION") self.assertEqual(payload.os_name, "OS")
def test_iq_set(self): iq = Iq(from_jid=JID("[email protected]/res"), to_jid=JID("*****@*****.**"), stanza_type="set", stanza_id=2) payload = ElementTree.Element( "{http://pyxmpp.jajcus.net/xmlns/test}payload") ElementTree.SubElement(payload, "{http://pyxmpp.jajcus.net/xmlns/test}abc") payload = XMLPayload(payload) iq.add_payload(payload) self.check_iq3(iq) xml = iq.as_xml() self.check_iq3(Iq(xml))
def handle_authorized(self, event): iq = Iq(ElementTree.XML(self.xml)) log.info("send_xml --> {0}".format(ElementTree.tostring(iq.as_xml()))) event.stream.send(iq) event.stream.disconnect()
def test_iq_get_from_xml(self): iq = Iq(ElementTree.XML(IQ1)) self.check_iq1(iq)
def test_iq_make_error_response(self): iq = Iq(ElementTree.XML(IQ1)) iq5 = iq.make_error_response(u"bad-request") self.check_iq5(iq5) xml = iq5.as_xml() self.check_iq5( Iq(xml) )
def test_iq_result_full_from_xml(self): iq = Iq(ElementTree.XML(IQ2)) self.check_iq2(iq)
def test_iq_set_from_xml(self): iq = Iq(ElementTree.XML(IQ3)) self.check_iq3(iq)
def test_iq_result_empty_from_xml(self): iq = Iq(ElementTree.XML(IQ4)) self.check_iq4(iq)
def test_iq_make_error_response(self): iq = Iq(ElementTree.XML(IQ1)) iq5 = iq.make_error_response(u"bad-request") self.check_iq5(iq5) xml = iq5.as_xml() self.check_iq5(Iq(xml))