async def wrapper(msg: Message) -> None: if msg.room: # (1) msg.parametrized_room = msg.room else: # (2) # User didn't supply any parameters if not msg.arg: await msg.user.send("Specifica il nome della room") return # Check if user supplied a non-empty roomid target_roomid = utils.to_room_id(msg.args[0], fallback=RoomId("")) msg.args = msg.args[1:] if not target_roomid: await msg.user.send("Specifica il nome della room") return msg.parametrized_room = Room.get(msg.conn, target_roomid) # Check if the room is valid if ( target_roomid not in msg.conn.rooms # bot in room or msg.user not in msg.parametrized_room # user in room ): # Send the same message for both errors to avoid leaking private rooms await msg.user.send("Sei nella room?") return # Every check passed: wrap the original function await func(msg)
async def handler(ws: ServerWs, conn: TestConnection) -> None: room = Room.get(conn, "room1") for message in messages: await ws.add_messages(message) await ws.get_messages() assert list(room.buffer) == buffer
async def changepage(msg: Message) -> None: if len(msg.args) != 3: return pageid = msg.args[0] room = Room.get(msg.conn, msg.args[1]) try: page = int(msg.args[2]) except ValueError: page = 1 await msg.reply_htmlpage(pageid, room, page)
async def requestpage(msg: ProtocolMessage) -> None: if msg.params[2] != "" or msg.params[3] != "requestpage": return user = User.get(msg.conn, msg.params[4]) pageid = msg.params[5].split( "0") # pageids can only contain letters and numbers if len(pageid) != 2: return page_room = Room.get(msg.conn, pageid[1]) await user.send_htmlpage(pageid[0], page_room)
async def handler(ws: ServerWs, conn: TestConnection) -> None: room = Room.get(conn, "room1") users = {} for username in usernames_add: user = User.get(conn, username) rank = usernames_add[username] room.add_user(user, rank) users[user] = rank or " " for username in usernames_remove: user = User.get(conn, username) room.remove_user(user) if user in users: users.pop(user) assert room.users == users
async def demote_old_temporary_voices(conn: Connection) -> None: await asyncio.sleep(3 * 60 * 60) db = Database.open() while True: with db.get_session() as session: stmt = select(d.TemporaryVoices).filter( d.TemporaryVoices.date < datetime.utcnow() - timedelta(days=30)) user: d.TemporaryVoices | None = session.scalar(stmt) if user: room = Room.get(conn, user.roomid) if room.roombot: await room.send(f"/roomdeauth {user.userid}", False) session.delete(user) # sleep for a minute, then try to deauth another user wait_time = 60 else: # sleep for a day if there are no more users to deauth wait_time = 24 * 60 * 60 await asyncio.sleep(wait_time)
async def handler(ws: ServerWs, conn: TestConnection) -> None: # Join a room with only an user in it await ws.add_messages([ ">room1", "|init|chat", "|title|Room 1", "|users|1,*cerbottana", "|:|1500000000", ]) assert await ws.get_messages() == Counter([ "|/cmd roominfo room1", "room1|/roomlanguage", "|/cmd userdetails cerbottana", ]) room1 = Room.get(conn, "room1") # Check if the room has been added to the list of the rooms assert set(conn.rooms.keys()) == {"room1"} # Check the room title assert room1.title == "Room 1" # Check if the only user in the room has been added to the room assert User.get(conn, "cerbottana") in room1 # Users enter the room await ws.add_messages( [ ">room1", "|j| User 1", ], [ ">room1", "|J| User 2", ], [ ">room1", "|join| User 3", ], ) assert await ws.get_messages() == Counter([ "|/cmd userdetails user1", "|/cmd userdetails user2", "|/cmd userdetails user3", ]) assert User.get(conn, "user1") in room1 assert User.get(conn, "user2") in room1 assert User.get(conn, "user3") in room1 # Users change names await ws.add_messages( [ ">room1", "|n| User 4|user1", ], [ ">room1", "|N| User 5|user2", ], [ ">room1", "|name| User 6|user3", ], ) assert await ws.get_messages() == Counter([ "|/cmd userdetails user4", "|/cmd userdetails user5", "|/cmd userdetails user6", ]) assert User.get(conn, "user1") not in room1 assert User.get(conn, "user2") not in room1 assert User.get(conn, "user3") not in room1 assert User.get(conn, "user4") in room1 assert User.get(conn, "user5") in room1 assert User.get(conn, "user6") in room1 # Users change names without changing userid await ws.add_messages( [ ">room1", "|n| USER 4|user4", ], [ ">room1", "|N| USER 5|user5", ], [ ">room1", "|name| USER 6|user6", ], ) assert await ws.get_messages() == Counter([ "|/cmd userdetails user4", "|/cmd userdetails user5", "|/cmd userdetails user6", ]) assert User.get(conn, "user4") in room1 assert User.get(conn, "user5") in room1 assert User.get(conn, "user6") in room1 # Users leave the room await ws.add_messages( [ ">room1", "|l| User 4", ], [ ">room1", "|L| User 5", ], [ ">room1", "|leave| User 6", ], ) assert await ws.get_messages() == Counter() assert User.get(conn, "user4") not in room1 assert User.get(conn, "user5") not in room1 assert User.get(conn, "user6") not in room1 # Global and room rank await ws.add_queryresponse_userdetails("cerbottana", group="+", rooms={"room1": "*"}) assert await ws.get_messages() == Counter() assert User.get(conn, "cerbottana").global_rank == "+" assert User.get(conn, "cerbottana").rank(room1) == "*"
async def handler(ws: ServerWs, conn: TestConnection) -> None: assert Room.get(conn, room).roomid == roomid
async def handler(ws: ServerWs, conn: TestConnection) -> None: room = Room.get(conn, "room1") if language is not None: room.language = language assert room.language_id == language_id
async def handler(ws: ServerWs, conn: TestConnection) -> None: conn.public_roomids = public_roomids assert Room.get(conn, room).is_private == is_private