async def set_name(event, session): """Set the name of the current chat (also affects the saving directory.""" to_id, to_type = get_peer_information(event.message.to_id) new_chat_name = event.message.message.split(" ", maxsplit=1)[1].strip() # We already save to zips, prevent that if new_chat_name == "zips": return "Invalid chat name. Pick another." subscriber = Subscriber.get_or_create( session, to_id, to_type, event.message, chat_name=new_chat_name ) old_chat_path = get_chat_path(subscriber.chat_name) new_chat_path = get_chat_path(new_chat_name) # Handle any command that tries to escape the download directory new_real_path = os.path.realpath(new_chat_path) target_real_path = os.path.realpath(config["download"]["target_dir"]) if ( not new_real_path.startswith(target_real_path) or new_real_path == target_real_path ): user = await archive.get_entity(types.PeerUser(event.message.from_id)) sentry.captureMessage( "User tried to escape directory.", extra={ "new_chat_name": new_chat_name, "chat": subscriber.chat_name, "user": get_username(user), }, tags={"level": "info"}, ) return "Please stop fooling around and don't try to escape the directory. I have been notified about this." # Check whether we already have a chat with this name if ( session.query(Subscriber) .filter(Subscriber.chat_name == new_chat_name) .one_or_none() ): return "Chat name already exists. Please choose another one." # Move the old directory to the new location elif old_chat_path != new_chat_path: subscriber.chat_name = new_chat_name if os.path.exists(old_chat_path): os.rename(old_chat_path, new_chat_path) return "Chat name changed."
async def set_name(event, session): """Set query attributes.""" to_id, to_type = get_peer_information(event.message.to_id) new_chat_name = event.message.message.split(' ', maxsplit=1)[1].strip() if new_chat_name == 'zips': return "Invalid chat name. Pick another." subscriber = Subscriber.get_or_create(session, to_id, to_type, event.message, chat_name=new_chat_name) old_chat_path = get_chat_path(subscriber.chat_name) new_chat_path = get_chat_path(new_chat_name) new_real_path = os.path.realpath(new_chat_path) target_real_path = os.path.realpath(config.TARGET_DIR) if not new_real_path.startswith(target_real_path) or \ new_real_path == target_real_path: user = await archive.get_entity(types.PeerUser(event.message.from_id)) sentry.captureMessage("User tried to escape directory.", extra={ 'new_chat_name': new_chat_name, 'chat': subscriber.chat_name, 'user': get_username(user) }, tags={'level': 'info'}) return "Please stop fooling around and don't try to escape the directory. I have been notified about this." if session.query(Subscriber) \ .filter(Subscriber.chat_name == new_chat_name) \ .one_or_none(): return "Chat name already exists. Please choose another one." elif old_chat_path != new_chat_path: subscriber.chat_name = new_chat_name if os.path.exists(old_chat_path): os.rename(old_chat_path, new_chat_path) return "Chat name changed."