def send_presence(self, to=None, delay=None, priority=10, **params): """sends presence :param to: jid to receive presence. :param delay: if set, it must be a ISO compatible date string :param priority: the priority of this resource """ from_jid = params.get('from') if from_jid: from_jid = JID(from_jid) elif self.bound_jid: from_jid = self.bound_jid else: msg = 'Presence cannot be sent when missing the "from" jid' raise MissingJID(msg) params['from'] = from_jid.full if to: params['to'] = JID(to).full presence = Presence.create(**params) if delay: delay_params = {'stamp': delay, 'from': from_jid.full} presence.append(PresenceDelay.create(**delay_params)) if priority: node = PresencePriority.create(bytes(priority)) presence.append(node) self.send(presence)
def test_jid_from_string(): ('JID() should accept a string') # Given a JID from string item = JID('[email protected]/resource') # It can be represented as a full jid item.full.should.equal('[email protected]/resource') # It can be represented as a bare jid item.bare.should.equal('*****@*****.**') # It has the nick item.nick.should.equal('user') # It has the domain item.domain.should.equal('domain.com') # It has the resource item.resource.should.equal('resource') # It has the text representation item.text.should.equal('[email protected]/resource') repr(item).should.equal( "JID({'bare': '*****@*****.**', 'nick': 'user', 'domain': 'domain.com', 'full': '[email protected]/resource', 'resource': 'resource'})" ) str(item).should.equal('[email protected]/resource') # It has the muc representation item.muc.should.equal({ 'nick': 'resource', 'room': 'user', 'server': 'domain.com' })
def add_contact(self, contact_jid, from_jid=None, groups=None): """adds a contact to the roster of the ``bound_jid`` or the provided ``from_jid`` parameter. `Automatically <https://xmpp.org/rfcs/rfc3921.html#int>`_ sends a ``<presence type="subscribe">`` with a subsequent ``<iq type="set">``. :param contact_jid: the jid to add in the roster :param from_jid: custom ``from=`` field to designate the owner of the roster :param groups: a list of strings with group names to categorize this contact in the roster """ from_jid = JID(from_jid or self.bound_jid) contact_jid = JID(contact_jid) new_contact = RosterQuery.create() item = RosterItem.create(jid=contact_jid.full, name=contact_jid.nick.title()) if isinstance(groups, basestring): groups = [groups] elif not isinstance(groups, list): groups = [] for group_name in groups: group = RosterGroup.create(group_name) item.append(group) new_contact.append(item) params = { 'type': 'set', 'from': from_jid.full, 'to': contact_jid.bare, } self.send_presence(**{ 'from': from_jid.bare, 'to': contact_jid.full, 'type': 'subscribe' }) self.send(IQ.with_child_and_attributes(new_contact, **params))
def __init__(self, mechanism, jid, password): self.jid = JID(jid) self.password = password self.mechanism = mechanism self.sasl = sasl.client_authenticator_factory(mechanism) self.on = Events( 'sasl', [ 'success', # sasl authentication succeeded 'failure', # sasl authentication failure 'unknown', # received a sasl response but the stream is already authenticated ]) self.stream = None self.connection = None
def query_info(self, **params): to_jid = JID(params.get('to', self.stream.bound_jid.domain)).bare iq = IQ.create(type='get', to=to_jid) query = QueryInfo.create(**params) iq.append(query) self.stream.send(iq)
def handle_bound_jid(self, node): jid = JID(node.value.strip()) self.__bound_jid = jid self.on.bound_jid.shout(jid)