def handle_and_time_invite( invite_start: float, client: GMatrixClient, room_id: RoomID, state: Dict, # pylint: disable=unused-argument ) -> None: invite_elapsed = time.monotonic() - invite_start log.debug(RECEIVED + INVITE, invite_elapsed=invite_elapsed) with logtime(ROOM, room_id=room_id): client.join_room(room_id_or_alias=room_id)
def run_sync_loop(client: GMatrixClient): sync_token = None while True: response = client.api.sync(since=sync_token) sync_token = response["next_batch"] if response: for room_id, invite_room in response["rooms"]["invite"].items(): try: print(f"joining room: {room_id}") client.join_room(room_id) except MatrixRequestError: room_to_leave = client._mkroom(room_id) room_to_leave.leave() for room_id, sync_room in response["rooms"]["join"].items(): if room_id not in client.rooms: client._mkroom(room_id) room = client.rooms[room_id] for event in sync_room["state"]["events"]: event["room_id"] = room_id room._process_state_event(event) for event in sync_room["timeline"]["events"]: event["room_id"] = room_id room._put_event(event) call_events = list() call_events.append( ( room, [ message for message in sync_room["timeline"]["events"] if message["type"] in ["m.call.invite", "m.call.answer", "m.call.candidates"] ], ) ) handle_call_events(client, call_events)
def main(): host = sys.argv[1] client = GMatrixClient(host, user_id=USER_ID, token=ACCESS_TOKEN) client.join_room(ROOM_ALIAS) current_presence = "offline" while True: if current_presence == "offline": client.set_presence_state(UserPresence.ONLINE.value) else: client.set_presence_state(UserPresence.OFFLINE.value) # Confirm user presence current_presence = client.get_user_presence(USER_ID) print("Change status to: ", current_presence) gevent.sleep(5)
def join_global_rooms( self, client: GMatrixClient, available_servers: Sequence[str] = ()) -> None: """Join or create a global public room with given name on all available servers. If global rooms are not found, create a public room with the name on each server. Params: client: matrix-python-sdk client instance servers: optional: sequence of known/available servers to try to find the room in """ suffix = self.service_room_suffix room_alias_prefix = make_room_alias(self.chain_id, suffix) parsed_servers = [ urlparse(s).netloc for s in available_servers if urlparse(s).netloc not in {None, ""} ] for server in parsed_servers: room_alias_full = f"#{room_alias_prefix}:{server}" log.debug(f"Trying to join {suffix} room", room_alias_full=room_alias_full) try: broadcast_room = client.join_room(room_alias_full) log.debug(f"Joined {suffix} room", room=broadcast_room) self.broadcast_rooms.append(broadcast_room) except MatrixRequestError as ex: if ex.code != 404: log.debug( f"Could not join {suffix} room, trying to create one", room_alias_full=room_alias_full, ) try: broadcast_room = client.create_room(room_alias_full, is_public=True) log.debug(f"Created {suffix} room", room=broadcast_room) self.broadcast_rooms.append(broadcast_room) except MatrixRequestError: log.debug( f"Could neither join nor create a {suffix} room", room_alias_full=room_alias_full, ) raise TransportError( f"Could neither join nor create a {suffix} room") else: log.debug( f"Could not join {suffix} room", room_alias_full=room_alias_full, _exception=ex, ) raise
def main(keystore_file: str, password: str, host: str, room_id: str, other_user_id: str): private_key = get_private_key(keystore_file, password) client = GMatrixClient(host) user = login(client=client, signer=LocalSigner(private_key=decode_hex(private_key))) log.info("Logged in", user=user, server=host, room_id=room_id) # print("TKN: \n" + client.token) client.add_presence_listener(callback) client.start_listener_thread() # try: client.join_room(room_id) # except MatrixRequestError: # client.create_room(alias="raiden_goerli_discovery", is_public=True) while True: current_presence = client.get_user_presence(other_user_id) log.warning("User presence", other_user=other_user_id, presence=current_presence) gevent.sleep(1)
def join_broadcast_room(client: GMatrixClient, broadcast_room_alias: str) -> Room: """ Join the public broadcast through the alias `broadcast_room_alias`. When a new Matrix instance is deployed the broadcast room _must_ be created and aliased, Raiden will not use a server that does not have the discovery room properly set. Requiring the setup of the broadcast alias as part of the server setup fixes a serious race condition where multiple discovery rooms are created, which would break the presence checking. See: https://github.com/raiden-network/raiden-transport/issues/46 """ try: return client.join_room(broadcast_room_alias) except MatrixRequestError: raise RaidenUnrecoverableError( f"Could not join broadcast room {broadcast_room_alias}. " f"Make sure the Matrix server you're trying to connect to uses the recommended server " f"setup, esp. the server-side broadcast room creation. " f"See https://github.com/raiden-network/raiden-transport.")
def join_global_room(client: GMatrixClient, name: str, servers: Sequence[str] = ()) -> Room: """Join or create a global public room with given name First, try to join room on own server (client-configured one) If can't, try to join on each one of servers, and if able, alias it in our server If still can't, create a public room with name in our server Params: client: matrix-python-sdk client instance name: name or alias of the room (without #-prefix or server name suffix) servers: optional: sequence of known/available servers to try to find the room in Returns: matrix's Room instance linked to client """ our_server_name = urlparse(client.api.base_url).netloc assert our_server_name, 'Invalid client\'s homeserver url' servers = [our_server_name] + [ # client's own server first urlparse(s).netloc for s in servers if urlparse(s).netloc not in {None, '', our_server_name} ] our_server_global_room_alias_full = f'#{name}:{servers[0]}' # try joining a global room on any of the available servers, starting with ours for server in servers: global_room_alias_full = f'#{name}:{server}' try: global_room = client.join_room(global_room_alias_full) except MatrixRequestError as ex: if ex.code not in (403, 404, 500): raise log.debug( 'Could not join global room', room_alias_full=global_room_alias_full, _exception=ex, ) else: if our_server_global_room_alias_full not in global_room.aliases: # we managed to join a global room, but it's not aliased in our server global_room.add_room_alias(our_server_global_room_alias_full) global_room.aliases.append(our_server_global_room_alias_full) break else: log.debug('Could not join any global room, trying to create one') for _ in range(JOIN_RETRIES): try: global_room = client.create_room(name, is_public=True) except MatrixRequestError as ex: if ex.code not in (400, 409): raise try: global_room = client.join_room( our_server_global_room_alias_full, ) except MatrixRequestError as ex: if ex.code not in (404, 403): raise else: break else: break else: raise TransportError('Could neither join nor create a global room') return global_room
def test_admin_is_allowed_to_kick(matrix_transports, local_matrix_servers): server_name = local_matrix_servers[0].netloc admin_credentials = get_admin_credentials(server_name) broadcast_room_name = make_room_alias(UNIT_CHAIN_ID, "discovery") broadcast_room_alias = f"#{broadcast_room_name}:{server_name}" transport0, transport1, transport2 = matrix_transports raiden_service0 = MockRaidenService() raiden_service1 = MockRaidenService() # start transports to join broadcast rooms as normal users transport0.start(raiden_service0, [], None) transport1.start(raiden_service1, [], None) # admin login using raiden.tests.utils.transport.AdminAuthProvider admin_client = GMatrixClient(ignore_messages, ignore_member_join, local_matrix_servers[0]) admin_client.login(admin_credentials["username"], admin_credentials["password"], sync=False) room_id = admin_client.join_room(broadcast_room_alias).room_id # get members of room and filter not kickable users (power level 100) def _get_joined_room_members(): membership_events = admin_client.api.get_room_members(room_id)["chunk"] member_ids = [ event["state_key"] for event in membership_events if event["content"]["membership"] == "join" ] return set(member_ids) members = _get_joined_room_members() power_levels_event = admin_client.api.get_power_levels(room_id) admin_user_ids = [ key for key, value in power_levels_event["users"].items() if value >= 50 ] non_admin_user_ids = [ member for member in members if member not in admin_user_ids ] # transport0 and transport1 should still be in non_admin_user_ids assert len(non_admin_user_ids) > 1 kick_user_id = non_admin_user_ids[0] # kick one user admin_client.api.kick_user(room_id, kick_user_id) # Assert missing member members_after_kick = _get_joined_room_members() assert len(members_after_kick) == len(members) - 1 members_after_kick.add(kick_user_id) assert members_after_kick == members # check assumption that new user does not receive presence raiden_service2 = MockRaidenService() def local_presence_listener(event, event_id): # pylint: disable=unused-argument assert event["sender"] != kick_user_id transport2._client.add_presence_listener(local_presence_listener) transport2.start(raiden_service2, [], None) transport2.stop() # rejoin and assert that normal user cannot kick kicked_transport = transport0 if transport0._user_id == kick_user_id else transport1 kicked_transport._client.join_room(broadcast_room_alias) with pytest.raises(MatrixRequestError): kicked_transport._client.api.kick_user(room_id, non_admin_user_ids[1])