def send_connect_ack(self, key, response, jid): (local_address, remote_address)=(key[0], key[2]) packet=format_header(local_address, remote_address, ElementTree.Element("connect_ack")) packet.attrib['xmlns']="hexchat:connect_ack" response_stanza=ElementTree.Element("response") response_stanza.text=response packet.append(response_stanza) if response=="success": aliases=self.get_aliases() packet=self.add_aliases(packet, aliases) logging.debug("%s:%d" % local_address + " sending result signal to %s:%d" % remote_address) bot=[bot for bot in self.bots if bot.boundjid.bare==jid.bare][0] iq=Iq() iq['to']=set(key[1]).pop() iq['from']=bot.boundjid.full iq['type']='result' iq.append(packet) str_data=tostring(iq.xml, top_level=True) bot.karma_lock.acquire() bot.set_karma(len(str_data)) bot.send_queue.put(str_data) if response=="success": return aliases
def send_connect_iq(self, key, aliases): (local_address, remote_address)=(key[0], key[2]) packet=format_header(local_address, remote_address, ElementTree.Element("connect")) packet.attrib['xmlns']="hexchat:connect" packet=self.add_aliases(packet, aliases) logging.debug("%s:%d" % local_address + " sending connect request to %s:%d" % remote_address) iq=Iq() iq['to']=key[1] iq['type']='set' iq.append(packet) self.send(iq, aliases)
def send_disconnect(self, key, from_aliases, to_alias, iq_id="None"): (local_address, remote_address)=(key[0], key[2]) packet=format_header(local_address, remote_address, ElementTree.Element("disconnect")) packet.attrib['xmlns']="hexchat:disconnect" logging.debug("%s:%d" % local_address + " sending disconnect request to %s:%d" % remote_address) iq=Iq() id_stanza=ElementTree.Element('id') id_stanza.text=str(iq_id) packet.append(id_stanza) iq['to']=to_alias iq['type']='set' iq.append(packet) self.send(iq, from_aliases)
def Iq(self, *args, **kwargs): """ Create an Iq stanza. Uses same arguments as StanzaBase.__init__ Arguments: xml -- An XML object to use for the Iq's values. """ return Iq(self.xmpp, *args, **kwargs)
def send_data(self, key, from_aliases, data, to_alias, iq_id): (local_address, remote_address)=(key[0], key[2]) packet=format_header(local_address, remote_address, ElementTree.Element('packet')) packet.attrib['xmlns']="hexchat:packet" id_stanza=ElementTree.Element('id') id_stanza.text=str(iq_id) packet.append(id_stanza) data_stanza=ElementTree.Element('data') data_stanza.text=data packet.append(data_stanza) iq=Iq() iq['to']=to_alias iq['type']='set' iq.append(packet) self.send(iq, from_aliases)
def stanza_from_string(s): if not isinstance(s, unicode): s = s.decode('utf-8') e = ET.fromstring(s) if e.tag == '{jabber:client}message': stanza = Message(xml=e) elif e.tag == '{jabber:client}presence': stanza = Presence(xml=e) elif e.tag == '{jabber:client}iq': stanza = Iq(xml=e) else: raise ValueError('string is not valid stanza') return stanza
def admin_query(room, nick=None, jid=None, role='none', reason=None): """ Generate an admin query stanza that will change the role of the nick or the full jid within a muc. Default role is 'none', which kicks the nick/jid from the muc. If only sleekxmpp provide a better support for this in their xep_0045 plugin... """ roles = ('moderator', 'none', 'participant', 'visitor') if role not in roles: raise TypeError('role must be one of %s' % str(roles)) query = ET.Element('{http://jabber.org/protocol/muc#admin}query') if nick is not None: item = ET.Element('{http://jabber.org/protocol/muc#admin}item', {'role': role, 'nick': nick}) elif jid is not None: item = ET.Element('{http://jabber.org/protocol/muc#admin}item', {'role': role, 'jid': jid}) else: raise ValueError('either nick or jid must be provided') if reason: el = ET.Element('reason') el.text = reason item.append(el) query.append(item) iq = Iq() iq.append(query) iq['to'] = room iq['type'] = 'set' return iq
def send_disconnect_error(self, key, from_aliases, to_alias, message=False): (local_address, remote_address)=(key[0], key[2]) packet=format_header(local_address, remote_address, ElementTree.Element("disconnect_error")) packet.attrib['xmlns']="hexchat:disconnect_error" packet=self.add_aliases(packet, from_aliases) logging.debug("%s:%d" % local_address + " sending disconnect_error request to %s:%d" % remote_address) if message: msg=Message() msg['type']='chat' else: msg=Iq() msg['type']='set' msg['to']=to_alias msg.append(packet) self.send(msg, from_aliases)
def Iq(self, *args, **kwargs): """Create an Iq stanza associated with this stream.""" return Iq(self, *args, **kwargs)