def initialise(self): self._logger.debug("initialising anonymous provisioner") client = yield from self.get_connected_client() disco = client.summon(aioxmpp.DiscoClient) self._featuremap.update( (yield from discover_server_features( disco, self.__domain, blockmap=self.__blockmap, )) ) self._logger.debug("found %d features", len(self._featuremap)) if self._logger.isEnabledFor(logging.DEBUG): for feature, providers in self._featuremap.items(): self._logger.debug( "%s provided by %s", feature, ", ".join(sorted(map(str, providers))) ) self._account_info = yield from disco.query_info(None) # clean up state del client yield from self.teardown()
def run_simple_example(self): # we are polite and ask the server whether it actually supports the # XEP-0191 block list protocol disco = self.client.summon(aioxmpp.DiscoClient) server_info = yield from disco.query_info( self.client.local_jid.replace( resource=None, localpart=None, ) ) if "urn:xmpp:blocking" not in server_info.features: print("server does not support block lists!", file=sys.stderr) sys.exit(2) # now that we are sure that the server supports it, we can send # requests. if self.args.jids_to_block: yield from self.blocking.block_jids(self.args.jids_to_block) else: print("nothing to block") if self.args.jids_to_unblock: yield from self.blocking.unblock_jids(self.args.jids_to_unblock) else: print("nothing to unblock") if self.args.show_list: # print all the items; again, .items is a list of JIDs print("current block list:") for item in sorted(self.blocking.blocklist): print("\t", item, sep="")
def run_simple_example(self): disco = self.client.summon(aioxmpp.DiscoClient) try: info = yield from disco.query_info( self.g_jid.replace(resource=None, localpart=None), timeout=10 ) except Exception as exc: print("could not get info: ") print("{}: {}".format(type(exc).__name__, exc)) raise print("features:") for feature in info.features: print(" {!r}".format(feature)) print("identities:") identities = list(info.identities) def identity_key(ident): return (ident.category, ident.type_) identities.sort(key=identity_key) for (category, type_), identities in ( itertools.groupby(info.identities, identity_key)): print(" category={!r} type={!r}".format(category, type_)) subidentities = list(identities) subidentities.sort(key=lambda ident: ident.lang) for identity in subidentities: print(" [{}] {!r}".format(identity.lang, identity.name))
def discover_server_features(disco, peer, recurse_into_items=True, blockmap={}): """ Use :xep:`30` service discovery to discover features supported by the server. :param disco: Service discovery client which can query the `peer` server. :type disco: :class:`aioxmpp.DiscoClient` :param peer: The JID of the server to query :type peer: :class:`~aioxmpp.JID` :param recurse_into_items: If set to true, the :xep:`30` items exposed by the server will also be queried for their features. Only one level of recursion is performed. :return: A mapping which maps :xep:`30` feature vars to the JIDs at which the service is provided. This uses :xep:`30` service discovery to obtain a set of features supported at `peer`. The set of features is returned as a mapping which maps the ``var`` values of the features to the JID at which they were discovered. If `recurse_into_items` is true, a :xep:`30` items query is run against `peer`. For each JID discovered that way, :func:`discover_server_features` is re-invoked (with `recurse_into_items` set to false). The resulting mappings are merged with the mapping obtained from querying the features of `peer` (existing entries are *not* overriden -- so `peer` takes precedence). """ server_info = yield from disco.query_info(peer) all_features = { feature: [peer] for feature in server_info.features if not _is_feature_blocked(peer, feature, blockmap) } if recurse_into_items: server_items = yield from disco.query_items(peer) features_list = yield from asyncio.gather( *( discover_server_features( disco, item.jid, recurse_into_items=False, ) for item in server_items.items if item.jid is not None and item.node is None ) ) for features in features_list: for feature, providers in features.items(): all_features.setdefault(feature, []).extend(providers) return all_features
def run_simple_example(self): disco = self.client.summon(aioxmpp.DiscoClient) try: info = yield from disco.query_info( self.args.target_entity or self.client.local_jid.bare(), node=self.args.target_node, timeout=10 ) except Exception as exc: print("could not get info: ") print("{}: {}".format(type(exc).__name__, exc)) raise print("features:") for feature in info.features: print(" {!r}".format(feature)) print("identities:") identities = list(info.identities) def identity_key(ident): return (ident.category, ident.type_) identities.sort(key=identity_key) for (category, type_), identities in ( itertools.groupby(info.identities, identity_key)): print(" category={!r} type={!r}".format(category, type_)) subidentities = list(identities) subidentities.sort(key=lambda ident: ident.lang) for identity in subidentities: print(" [{}] {!r}".format(identity.lang, identity.name)) print("extensions:") for ext in info.exts: print(" ", ext.get_form_type()) for field in ext.fields: if (field.var == "FORM_TYPE" and field.type_ == aioxmpp.forms.xso.FieldType.HIDDEN): continue print(" var={!r} values=".format(field.var), end="") if len(field.values) == 1: print("{!r}".format([field.values[0]])) elif len(field.values) == 0: print("[]") else: print("[") for value in field.values: print(" {!r}".format(value)) print("]")
def supports_commands(self, peer_jid): """ Detect whether a peer supports :xep:`50` Ad-Hoc commands. :param peer_jid: JID of the peer to query :type peer_jid: :class:`aioxmpp.JID` :rtype: :class:`bool` :return: True if the peer supports the Ad-Hoc commands protocol, false otherwise. Note that the fact that a peer supports the protocol does not imply that it offers any commands. """ disco = self.dependencies[aioxmpp.disco.DiscoClient] response = yield from disco.query_info(peer_jid, ) return namespaces.xep0050_commands in response.features
def show_info(from_, disco): info = yield from disco.query_info(from_) print("{}:".format(from_)) print(" features:") for feature in info.features: print(" {!r}".format(feature)) print(" identities:") identities = list(info.identities) identity_key = lambda ident: (ident.category, ident.type_) identities.sort(key=identity_key) for (category, type_), identities in (itertools.groupby(info.identities, identity_key)): print(" category={!r} type={!r}".format(category, type_)) subidentities = list(identities) subidentities.sort(key=lambda ident: ident.lang) for identity in subidentities: print(" [{}] {!r}".format(identity.lang, identity.name))
def initialise(self): self._logger.debug("initialising anonymous provisioner") client = yield from self.get_connected_client() disco = client.summon(aioxmpp.DiscoClient) self._featuremap.update((yield from discover_server_features( disco, self.__domain, blockmap=self.__blockmap, ))) self._logger.debug("found %d features", len(self._featuremap)) if self._logger.isEnabledFor(logging.DEBUG): for feature, providers in self._featuremap.items(): self._logger.debug("%s provided by %s", feature, ", ".join(sorted(map(str, providers)))) self._account_info = yield from disco.query_info(None) # clean up state del client yield from self.teardown()
def main(jid, password): @asyncio.coroutine def get_password(client_jid, nattempt): if nattempt > 1: # abort, as we cannot properly re-ask the user return None return password connected_future = asyncio.Future() disconnected_future = asyncio.Future() def connected(): connected_future.set_result(None) def disconnected(): disconnected_future.set_result(None) tls_provider = aioxmpp.security_layer.STARTTLSProvider( aioxmpp.security_layer.default_ssl_context, ) sasl_provider = aioxmpp.security_layer.PasswordSASLProvider(get_password) client = aioxmpp.node.PresenceManagedClient( jid, aioxmpp.security_layer.security_layer(tls_provider, [sasl_provider])) client.on_stream_established.connect(connected) client.on_stopped.connect(disconnected) disco = client.summon(aioxmpp.disco.Service) client.presence = aioxmpp.structs.PresenceState(True) yield from connected_future try: try: info = yield from disco.query_info(jid.replace(resource=None, localpart=None), timeout=10) except Exception as exc: print("could not get info: ") print("{}: {}".format(type(exc).__name__, exc)) raise print("features:") for feature in info.features: print(" {!r}".format(feature)) print("identities:") identities = list(info.identities) identity_key = lambda ident: (ident.category, ident.type_) identities.sort(key=identity_key) for (category, type_), identities in (itertools.groupby(info.identities, identity_key)): print(" category={!r} type={!r}".format(category, type_)) subidentities = list(identities) subidentities.sort(key=lambda ident: ident.lang) for identity in subidentities: print(" [{}] {!r}".format(identity.lang, identity.name)) finally: client.presence = aioxmpp.structs.PresenceState(False) yield from disconnected_future
def run_simple_example(self): # we are polite and ask the server whether it actually supports the # XEP-0191 block list protocol disco = self.client.summon(aioxmpp.DiscoClient) server_info = yield from disco.query_info( self.client.local_jid.replace( resource=None, localpart=None, )) if "urn:xmpp:blocking" not in server_info.features: print("server does not support block lists!", file=sys.stderr) sys.exit(2) # now that we are sure that the server supports it, we can send # requests. if self.args.jids_to_block: # construct the block list request and add the JIDs by simply # placing them in the items attribute cmd = BlockCommand() # note that self.args.jids_to_block is a list of JID objects, not a # list of strings (that would not work, because BlockItem requires # the jid attribute to be a JID) cmd.items[:] = self.args.jids_to_block # construct the IQ request iq = aioxmpp.IQ( type_=aioxmpp.IQType.SET, payload=cmd, ) # send it and wait for a response yield from self.client.stream.send(iq) else: print("nothing to block") if self.args.jids_to_unblock: # construct the unblock list request and add the JIDs by simply # placing them in the items attribute cmd = UnblockCommand() cmd.items[:] = self.args.jids_to_unblock # construct the IQ request iq = aioxmpp.IQ( type_=aioxmpp.IQType.SET, payload=cmd, ) # send it and wait for a response yield from self.client.stream.send(iq) else: print("nothing to unblock") if self.args.show_list: # construct the request to retrieve the block list iq = aioxmpp.IQ( type_=aioxmpp.IQType.GET, payload=BlockList(), ) result = yield from self.client.stream.send(iq) # print all the items; again, .items is a list of JIDs print("current block list:") for item in sorted(result.items): print("\t", item, sep="")
def main(jid, password): @asyncio.coroutine def get_password(client_jid, nattempt): if nattempt > 1: # abort, as we cannot properly re-ask the user return None return password connected_future = asyncio.Future() disconnected_future = asyncio.Future() def connected(): connected_future.set_result(None) def disconnected(): disconnected_future.set_result(None) tls_provider = aioxmpp.security_layer.STARTTLSProvider( aioxmpp.security_layer.default_ssl_context, ) sasl_provider = aioxmpp.security_layer.PasswordSASLProvider( get_password ) client = aioxmpp.node.PresenceManagedClient( jid, aioxmpp.security_layer.security_layer( tls_provider, [sasl_provider] ) ) client.on_stream_established.connect(connected) client.on_stopped.connect(disconnected) disco = client.summon(aioxmpp.disco.Service) client.presence = aioxmpp.structs.PresenceState(True) yield from connected_future try: try: info = yield from disco.query_info( jid.replace(resource=None, localpart=None), timeout=10) except Exception as exc: print("could not get info: ") print("{}: {}".format(type(exc).__name__, exc)) raise print("features:") for feature in info.features: print(" {!r}".format(feature)) print("identities:") identities = list(info.identities) identity_key = lambda ident: (ident.category, ident.type_) identities.sort(key=identity_key) for (category, type_), identities in ( itertools.groupby(info.identities, identity_key)): print(" category={!r} type={!r}".format(category, type_)) subidentities = list(identities) subidentities.sort(key=lambda ident: ident.lang) for identity in subidentities: print(" [{}] {!r}".format(identity.lang, identity.name)) finally: client.presence = aioxmpp.structs.PresenceState(False) yield from disconnected_future