def initiate(self, request, to, from_=None): """ Initiates a stream to a remote entity. :param request: Request to send. :param to: Destination JID. :param from_: Source JID. """ fform = FeatureForm(methods=self.streams.keys(), type_='form') feature = Feature(methods=fform) request.feature = feature if from_ is None: from_ = self.dispatcher.myjid iq = Iq(from_=from_, to=to, type_='set') request.id_ = sid = unicode(uuid.uuid4()) iq.link(request) result = yield self.dispatcher.send(iq) form = FeatureForm.createFromElement(result.feature.methods, methods=self.streams.keys()) form.validate() method = form.stream_method.value stream = self.streams[method] yield stream.requestStream(to, lambda _buf, _meta:None, sid, from_=from_) defer.returnValue((stream, sid))
def requestStream(self, jid, callback, sid=None, meta=None, from_=None): """ Request bytestream session from another entity. :param jid: JID of entity we want connect to. :param callback: callback which will be called when data will be available to consume. :param sid: session id to use with stream. Generate one if None given. :param meta: metadata for this session. (Will be passed in a callback) :param from_: sender JID for request (if differs from myjid) """ if from_ is None: from_ = self.dispatcher.myjid if sid is None: sid = genSID() streamhosts = [ stanzas.StreamHost(rhost=iface[0], jid=from_, port=iface[1]) for iface in self.ifaces ] for proxy_jid in self.proxies: proxy = self.proxies[proxy_jid] streamhosts.append( stanzas.StreamHost(rhost=proxy[0], port=proxy[1], jid=proxy_jid)) query = stanzas.StreamHostQuery(sid=sid, streamhosts=streamhosts, parent=Iq(type_='set', to=jid, from_=from_)) d = self.registerSession(sid, from_, jid, callback, meta=meta) try: result = yield self.dispatcher.send(query) except: self.unregisterSession(sid=sid) raise pjid = result.streamhost_used.jid if pjid in self.proxies: proxy = self.proxies[pjid] yield _startClient(self, proxy[0], proxy[1], self.sessions[sid]['hash']) query = stanzas.ActivateQuery(jid=jid, sid=sid, parent=Iq(type_='set', from_=from_, to=pjid)) yield self.dispatcher.send(query) else: yield d defer.returnValue(sid)
def init(self): self.dispatcher.registerHandler((RosterQuery, self)) self.dispatcher.registerHandler((RosterPresence, self)) dispatcher.connect(self._send_initial_presence, self.roster_got, sender=self) iq = Iq(type_='get') query = RosterQuery(parent=iq) iq.result_class = RosterQuery iq.deferred.addCallback(self.gotRoster) self.dispatcher.send(iq)
def set(self, vcard): """ Set my own vcard (for clients). :returns: query.iq.deferred """ query = copy.copy(vcard) iq = Iq(type_='set') iq.link(query) self.dispatcher.send(query.iq) return query.iq.deferred
def init(self): """ Register necessary handlers to handle roster queries, send a query to receive a roster. """ self.dispatcher.registerHandler((RosterQuery, self)) self.dispatcher.registerHandler((RosterPresence, self)) dispatcher.connect(self._send_initial_presence, self.roster_got, sender=self) iq = Iq(type_='get') query = RosterQuery(parent=iq) iq.result_class = RosterQuery iq.deferred.addCallback(self.gotRoster) self.dispatcher.send(iq)
def test_setHandler(self): rq = roster.RosterQuery(parent=Iq(type_='get', id='id0')) items = [ itemEmul('fast@wok'), itemEmul('little@nation', presences={ 'q': 'w', 'r': 't' }), itemEmul('gordon@freeman', presences={ '1': '2', '3': '4' }) ] rq.host = hostEmulator(items=items) self.assertEqual(rq.setHandler(), Iq(type_='result', id='id0'))
def examine_proxy(self, jid, from_): query = stanzas.GetStreamHostsQuery( parent=Iq(from_=from_, to=jid, type_='get')) result = yield self.dispatcher.send(query) r = result.streamhost result = (r.rhost, r.port) self.proxies[query.iq.to] = result
def _unregisterConnection(self, sid): s = self.sessions[sid] cq = CQ(sid=sid, parent=Iq(to=s['initiator'], from_=s['target'], type_='set')) if s['is_outgoing']: cq.iq.swapAttributeValues('to', 'from') self.getTransport(sid).unregisterProducer() s['active'] = False del self.sessions[sid] return self.dispatcher.send(cq.iq)
def addItem(self, item): """ Sends set-type query for addition new item in roster :param item: value of new item """ query = RosterQuery(parent=Iq(type_='set')) query.link(item) self.dispatcher.send(query.iq)
def removeItem(self, item): """ Sends set-type query for deletion item from roster :param item: value of removed item """ item.subscription = 'remove' query = RosterQuery(parent=Iq(type_='set')) query.link(item) self.dispatcher.send(query.iq)
def requestStream(self, jid, callback, sid=None, meta=None, from_=None, block_size=4096, stanza_type='iq', wait_for_result_when_send=True): """ Request bytestream session from another entity. :param jid: JID of entity we want connect to. :param callback: callback which will be called when data will be available to consume. :param sid: session id to use with stream. Generate one if None given. :param meta: metadata for this session. (Will be passed in a callback) :param from_: sender JID for request (if differs from myjid) :param block_size: block size to use with the connection. """ if from_ is None: from_ = self.dispatcher.myjid if sid is None: sid = genSID() s = self.registerSession( sid, from_, jid, callback, meta=meta, block_size=block_size, stanza_type=stanza_type, wait_for_result_when_send=wait_for_result_when_send) s['is_outgoing'] = True query = OpenQuery(block_size=block_size, sid=sid, stanza_type=stanza_type, parent=Iq(type_='set', to=jid, from_=from_)) try: yield self.dispatcher.send(query.iq) except: self.unregisterSession(sid=sid) raise s['active'] = True defer.returnValue(sid)
def get(self, jid, from_=None): """ Get a vcard of another XMPP entity. :returns: query.iq.deferred with a VCardQuery instance as a result """ if from_ is None: from_ = self.dispatcher.myjid query = VCardQuery(parent=Iq(type_='get', to=jid, from_=from_)) query.iq.result_class = VCardQuery self.dispatcher.send(query.iq) return query.iq.deferred
def initiate(self, request, to, from_=None): fform = FeatureForm(methods=self.streams.keys(), type_='form') feature = Feature(methods=fform) request.feature = feature if from_ is None: from_ = self.dispatcher.myjid iq = Iq(from_=from_, to=to, type_='set') request.id_ = sid = unicode(uuid.uuid4()) iq.link(request) result = yield self.dispatcher.send(iq) form = FeatureForm.createFromElement(result.feature.methods, methods=self.streams.keys()) form.validate() method = form.stream_method.value stream = self.streams[method] yield stream.requestStream(to, lambda _buf, _meta: None, sid, from_=from_) defer.returnValue((stream, sid))
def set_affiliation(self, room_jid, jid, affiliation, reason=None): """ Sends query for changing user's affiliation to room jid. Uses mostly of chat administrators. :param room_jid: JID of room-conference :param jid: JID of user :param affiliation: value of new user's affiliation :param reason: string-type argument with reason of affiliation changing (default None) """ iq = Iq(type_='set', to=room_jid, from_=self.dispatcher.myjid) i = UserItemInfo(jid=internJID(jid), affiliation=affiliation, reason=reason) return makeAdminQuery(i, iq, self.dispatcher)
def get_list(self, room_jid, affiliation=None, role=None): """ Sends query for receiving list of all users with fixed value of role or affiliation in room. Uses mostly of chat administrators. :param room_jid: JID of room-conference :param role: value of role for users will be filtered (default None) :param affiliation: value of affiliation for users will be filtered (default None) """ iq = Iq(type_='get', to=room_jid, from_=self.dispatcher.myjid) i = UserItemInfo(role=role, affiliation=affiliation) return makeAdminQuery(i, iq, self.dispatcher)
def set_role(self, room_jid, nick, role, reason=None): """ Sends query for changing user's role in chat to room jid. Uses mostly of chat administrators. :param room_jid: JID of room-conference :param nick: string-type user's nickname :param role: value of new user's role :param reason: string-type argument with reason of role changing (default None) """ iq = Iq(type_='set', to=room_jid, from_=self.dispatcher.myjid) i = UserItemInfo(nick=nick, role=role, reason=reason) return makeAdminQuery(i, iq, self.dispatcher)
def getVersion(self, jid, from_=None): """ Makes get version query to some JID. :param jid: reciever for get version query :param from_: sender for get version query (if differs from myjid) :returns: deferred object which waits for result stanza with version info (or error stanza) from query's target """ if from_ is None: from_ = self.dispatcher.myjid query = VersionQuery(host=self, parent=Iq(type_='get', to=jid, from_=from_)) query.iq.result_class = VersionQuery self.dispatcher.send(query.iq) return query.iq.deferred
def _write(self): # XXX: Error handling if not self.session['active']: defer.returnValue(None) toSend = self.buf[:self.session['block_size']] self.buf = self.buf[self.session['block_size']:] toSend = base64.b64encode(toSend) dq = DQ(seq=self.session['outgoing_seq'], sid=self.sid, parent=Iq(to=self.session['initiator'], from_=self.session['target'], type_='set')) self.session['outgoing_seq'] += 1 if self.session['is_outgoing']: dq.iq.swapAttributeValues('to', 'from') dq.content = toSend if self.session['wait_for_result_when_send']: yield self.dispatcher.send(dq.iq) else: self.dispatcher.send(dq.iq)
def getInfo(self, jid, node=None, from_=None): """ Get disco info from another entity. Return deferred object with the result of type DiscoInfoQuery. :param jid: JID of the entity to get items from. :param node: node name to get items from. :param from_: set some specific from address. Uses myjid if none given. :returns: query.iq.deferred - deferrer object with result or error. """ if from_ is None: from_ = self.dispatcher.myjid query = DiscoInfoQuery(host=self, node=node, parent=Iq(type_='get', to=jid, from_=from_)) query.iq.result_class = DiscoInfoQuery self.dispatcher.send(query.iq) return query.iq.deferred
def setUp(self): self.query = disco.VDiscoInfoQuery(parent=Iq(type_='set')) self.query.host = hostEmul(static_items={'': disco.DiscoItemsQuery()}, static_info={'': disco.DiscoInfoQuery()})
def test_getHandler(self): rq = roster.RosterQuery(parent=Iq(type_='get')) self.assertRaises(NotAcceptableException, rq.getHandler)
def setUp(self): self.MyVersionQuery = MyVersionQuery(parent=Iq(type_='get')) self.MyVersionQuery.host = hostEmul(client_name='name', client_os='os', client_version='version')
def set(self, vcard): query = copy.copy(vcard) iq = Iq(type_='set') iq.link(query) self.dispatcher.send(query.iq) return query.iq.deferred