async def run(self): jid = aioxmpp.JID.fromstr(self.wizard().field("jid")) self.client = aioxmpp.Client( jid, aioxmpp.make_security_layer(self.wizard().field("password"), ), logger=self.logger, ) self.client.summon(aioxmpp.DiscoClient) self.user_logger.info("Trying to connect as %s", jid) async with self.client.connected(): self.user_logger.info("Successfully connected!") self.ui.progress.setRange(0, len(self.optional_steps) * 100) self.ui.progress.setValue(0) for i, step in enumerate(self.optional_steps): await step(i) self.ui.progress.setValue((i + 1) * 100) self.user_logger.info("Settings checked, done!") self._complete = True self.completeChanged.emit()
def _create_client(self, sender_id, api_key, loop=None) -> aioxmpp.Client: xmpp_client = aioxmpp.Client( local_jid=aioxmpp.JID.fromstr('*****@*****.**' % sender_id), security_layer=aioxmpp.make_security_layer(api_key), override_peer=[ (self.FCM_HOST, self.FCM_PORT, aioxmpp.connector.XMPPOverTLSConnector()) ], loop=loop ) xmpp_client.on_stream_established.connect( lambda: self._wait_connection.set_result(True) ) xmpp_client.on_stream_destroyed.connect( self._on_stream_destroyed ) xmpp_client.on_failure.connect( lambda exc: self._wait_connection.set_exception(exc) ) xmpp_client.stream.register_message_callback( type_=aioxmpp.MessageType.NORMAL, from_=None, cb=self.on_response ) return xmpp_client
def __init__(self, xmpp_config, client_logger=None): super().__init__() self.__config = xmpp_config override_peer = [] if xmpp_config.get("host"): override_peer.append( (xmpp_config["host"], xmpp_config.get("port", 5222), aioxmpp.connector.STARTTLSConnector())) security_args = {} if xmpp_config.get("public_key_pin"): security_args["pin_store"] = xmpp_config["public_key_pin"] security_args[ "pin_type"] = aioxmpp.security_layer.PinType.PUBLIC_KEY self.client = aioxmpp.Client( aioxmpp.JID.fromstr(xmpp_config["jid"]), aioxmpp.make_security_layer( xmpp_config["password"], **security_args, ), override_peer=override_peer, logger=client_logger, ) self.__nested_cm = None self.buddies = self.client.summon(Buddies) self.buddies.load_buddies(xmpp_config.get("buddies", []))
async def amain(args, password): client = aioxmpp.Client( args.local_jid, aioxmpp.make_security_layer(password) ) async with client.connected() as stream: form_xso = (await stream.send( aioxmpp.IQ( to=args.service_jid, type_=aioxmpp.IQType.GET, payload=xso.Search() ) )).form form_obj = xso.SearchForm.from_xso(form_xso) form_obj.query.value = " ".join(map(shlex.quote, args.query)) form_obj.order_by.value = args.order_by if args.min_users is not None: form_obj.min_users.value = args.min_users request = xso.Search() request.form = form_obj.render_reply() request.rsm = aioxmpp.rsm.xso.ResultSetMetadata() if args.request_page_size is not None: request.rsm.max_ = args.request_page_size nresults = 0 while args.fetch_up_to is None or nresults < args.fetch_up_to: reply = await stream.send(aioxmpp.IQ( to=args.service_jid, type_=aioxmpp.IQType.GET, payload=request )) for item in reply.items: print_item(item) if not reply.items: break nresults += len(reply.items) if reply.rsm.max_ and len(reply.items) < reply.rsm.max_: break request.rsm.after = aioxmpp.rsm.xso.After() request.rsm.after.value = reply.rsm.last.value
async def async_send_messages(self, messages): client = aioxmpp.Client( self.g_jid, self.g_security_layer, ) client.resumption_timeout = 0 async with client.connected() as stream: for message_content in messages: msg = aioxmpp.Message( to=self.to_jid, type_=aioxmpp.MessageType.CHAT, ) msg.body[None] = message_content await stream.send(msg)
def __init__(self, sender_id, api_key, loop=None, max_requests=1000, on_connection_lost=None): self.max_requests = max_requests self.xmpp_client = aioxmpp.Client( local_jid=aioxmpp.JID.fromstr('*****@*****.**' % sender_id), security_layer=aioxmpp.make_security_layer(api_key), override_peer=[(self.FCM_HOST, self.FCM_PORT, aioxmpp.connector.XMPPOverTLSConnector())], loop=loop) self.loop = loop self.on_connection_lost = on_connection_lost self._wait_connection = asyncio.Future() self.inactivity_timer = None self.requests = {}
async def amain(loop, xmpp_cfg, unix_cfg, mucs): message_queue = asyncio.Queue(maxsize=16) message_handler = MessageProtocol(message_queue) sigint_received = asyncio.Event() sigint_future = asyncio.ensure_future(sigint_received.wait()) loop.add_signal_handler(signal.SIGINT, sigint_received.set) loop.add_signal_handler(signal.SIGTERM, sigint_received.set) socket_path, = unix_cfg sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM, 0) sock.bind(str(socket_path)) unix_transport, _ = await loop.create_datagram_endpoint( lambda: message_handler, sock=sock, ) address, password = xmpp_cfg xmpp_client = aioxmpp.Client(address, aioxmpp.make_security_layer(password)) muc_client = xmpp_client.summon(aioxmpp.MUCClient) try: async with xmpp_client.connected() as stream: rooms = {} for muc_info in mucs: room, fut = muc_client.join( muc_info["address"], muc_info["nickname"], autorejoin=True, ) await fut muc_info["room"] = room rooms[muc_info["address"]] = muc_info processor = asyncio.ensure_future( process_queue(message_queue, rooms)) done, pending = await asyncio.wait( [ processor, sigint_future, ], return_when=asyncio.FIRST_COMPLETED, ) if sigint_future in done: if not processor.done(): processor.cancel() try: await processor except asyncio.CancelledError: pass return if processor in done: processor.result() raise RuntimeError("processor exited early!") finally: if not sigint_future.done(): sigint_future.cancel() unix_transport.close()
async def main(local, password, peer, strip_newlines, add_newlines): loop = asyncio.get_event_loop() swrite = await stdout_writer() sread = asyncio.StreamReader() tread, pread = await loop.connect_read_pipe( lambda: asyncio.StreamReaderProtocol(sread), sys.stdin, ) client = aioxmpp.Client( local, aioxmpp.make_security_layer( password, ) ) sigint = asyncio.Event() loop.add_signal_handler(signal.SIGINT, sigint.set) loop.add_signal_handler(signal.SIGTERM, sigint.set) def recv(message): body = message.body.lookup( [aioxmpp.structs.LanguageRange.fromstr("*")] ) if add_newlines: body += "\n" swrite.write(body.encode("utf-8")) client.stream.register_message_callback( "chat", peer, recv ) sigint_future = asyncio.ensure_future(sigint.wait()) read_future = asyncio.ensure_future(sread.readline()) try: async with client.connected() as stream: # we send directed presence to the peer pres = aioxmpp.Presence( type_=aioxmpp.PresenceType.AVAILABLE, to=peer, ) await stream.send(pres) while True: done, pending = await asyncio.wait( [ sigint_future, read_future, ], return_when=asyncio.FIRST_COMPLETED, ) if sigint_future in done: break if read_future in done: line = read_future.result().decode() if not line: break if strip_newlines: line = line.rstrip() msg = aioxmpp.Message( type_="chat", to=peer, ) msg.body[None] = line await stream.send(msg) read_future = asyncio.ensure_future( sread.readline() ) finally: sigint_future.cancel() read_future.cancel()