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 zip(event, session): """Create 1.5GB zips with all files collectd in this chat.""" to_id, to_type = get_peer_information(event.message.to_id) subscriber = Subscriber.get_or_create(session, to_id, to_type, event.message) chat_path = get_chat_path(subscriber.chat_name) if not os.path.exists(chat_path): return "No files for this chat yet." zip_dir = init_zip_dir(subscriber.chat_name) text = f"Zipping started, this might take some time. Please don't issue this command again until I'm finished." await event.respond(text) create_zips(subscriber.chat_name, zip_dir, chat_path) text = "Zipping is completed. I'll now start uploading." await event.respond(text) for zip_file in os.listdir(zip_dir): zip_file_path = os.path.join(zip_dir, zip_file) await archive.send_file(event.message.to_id, zip_file_path) shutil.rmtree(zip_dir) return "All files are uploaded :)"
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."
async def clear_history(event, session): """Stop the bot.""" to_id, to_type = get_peer_information(event.message.to_id) subscriber = Subscriber.get_or_create(session, to_id, to_type, event.message) chat_path = get_chat_path(subscriber.chat_name) for known_file in subscriber.files: session.delete(known_file) if os.path.exists(chat_path): shutil.rmtree(chat_path) session.commit() return "All files from this chat have been deleted."