def remove_link_whitelist(self, bot, source, message, **rest): if not message: bot.whisper(source, "Usage: !remove link whitelist ID") return False id = None try: id = int(message) except ValueError: pass link = self.db_session.query(WhitelistedLink).filter_by( id=id).one_or_none() if link: self.whitelisted_links.remove(link) self.db_session.delete(link) self.db_session.commit() else: bot.whisper(source, "No link with the given id found") return False AdminLogManager.post("Whitelist link removed", source, link.domain) bot.whisper( source, f"Successfully removed whitelisted link with id {link.id}")
def remove_link_blacklist(self, bot, source, message, **rest) -> bool: if self.db_session is None: raise ValueError("LinkChecker module db not initialized") if not message: bot.whisper(source, "Usage: !remove link blacklist ID") return False id = None try: id = int(message) except ValueError: pass link = self.db_session.query(BlacklistedLink).filter_by( id=id).one_or_none() if link: self.blacklisted_links.remove(link) self.db_session.delete(link) self.db_session.commit() else: bot.whisper(source, "No link with the given id found") return False AdminLogManager.post("Blacklist link removed", source, link.domain) bot.whisper( source, f"Successfully removed blacklisted link with id {link.id}") return True
def remove_banphrase(self, **options): message = options['message'] bot = options['bot'] source = options['source'] if message: id = None try: id = int(message) except ValueError: pass banphrase = bot.banphrase_manager.find_match(message=message, id=id) if banphrase is None: bot.whisper(source.username, 'No banphrase with the given parameters found') return False AdminLogManager.post('Banphrase removed', source, banphrase.phrase) bot.whisper( source.username, 'Successfully removed banphrase with id {0}'.format( banphrase.id)) bot.banphrase_manager.remove_banphrase(banphrase) else: bot.whisper(source.username, 'Usage: !remove banphrase (BANPHRASE_ID)') return False
def remove_banphrase(**options): message = options["message"] bot = options["bot"] source = options["source"] if message: banphrase_id = None try: banphrase_id = int(message) except ValueError: pass banphrase = bot.banphrase_manager.find_match( message=message, banphrase_id=banphrase_id) if banphrase is None: bot.whisper(source.username, "No banphrase with the given parameters found") return False AdminLogManager.post("Banphrase removed", source, banphrase.id, banphrase.phrase) bot.whisper( source.username, "Successfully removed banphrase with id {0}".format( banphrase.id)) bot.banphrase_manager.remove_banphrase(banphrase) else: bot.whisper(source.username, "Usage: !remove banphrase (BANPHRASE_ID)") return False
def timer_toggle(timer_id: int, **options) -> ResponseReturnValue: try: json_data = request.get_json() if not json_data: return {"error": "Missing json body"}, 400 data: ToggleState = ToggleStateSchema().load(json_data) except ValidationError as err: return { "error": f"Did not match schema: {json.dumps(err.messages)}" }, 400 with DBManager.create_session_scope() as db_session: row = db_session.query(Timer).filter_by(id=timer_id).one_or_none() if not row: return {"error": "Timer with this ID not found"}, 404 row.enabled = data.new_state db_session.commit() payload = {"id": row.id, "new_state": data.new_state} AdminLogManager.post("Timer toggled", options["user"], "Enabled" if data.new_state else "Disabled", row.name) SocketClientManager.send("timer.update", payload) return { "success": "successful toggle", "new_state": data.new_state }
def add_link_blacklist(self, **options): bot = options["bot"] message = options["message"] source = options["source"] options, new_links = self.parse_link_blacklist_arguments(message) if new_links: parts = new_links.split(" ") try: for link in parts: if len(link) > 1: self.blacklist_url(link, **options) AdminLogManager.post("Blacklist link added", source, link) bot.whisper(source.username, "Successfully added your links") return True except: log.exception("Unhandled exception in add_link_blacklist") bot.whisper(source.username, "Some error occurred while adding your links") return False else: bot.whisper(source.username, "Usage: !add link blacklist LINK") return False
def remove_link_whitelist(self, **options): message = options['message'] bot = options['bot'] source = options['source'] if message: id = None try: id = int(message) except ValueError: pass link = self.db_session.query(WhitelistedLink).filter_by(id=id).one_or_none() if link: self.whitelisted_links.remove(link) self.db_session.delete(link) self.db_session.commit() else: bot.whisper(source.username, 'No link with the given id found') return False AdminLogManager.post('Whitelist link removed', source, link.domain) bot.whisper(source.username, 'Successfully removed whitelisted link with id {0}'.format(link.id)) else: bot.whisper(source.username, 'Usage: !remove link whitelist ID') return False
def add_link_blacklist(self, **options): bot = options['bot'] message = options['message'] source = options['source'] options, new_links = self.parse_link_blacklist_arguments(message) if new_links: parts = new_links.split(' ') try: for link in parts: if len(link) > 1: self.blacklist_url(link, **options) AdminLogManager.post('Blacklist link added', source, link) bot.whisper(source.username, 'Successfully added your links') return True except: log.exception('Unhandled exception in add_link_blacklist') bot.whisper(source.username, 'Some error occurred while adding your links') return False else: bot.whisper(source.username, 'Usage: !add link blacklist LINK') return False
def add_autoresponse(self, **options): """Method for creating and editing autoresponses. Usage: !add autoresponse TRIGGER RESPONSE [options] Multiple options available: --whisper/--no-whisper """ message = options['message'] bot = options['bot'] source = options['source'] if message: options, phrase = bot.autoresponse_manager.parse_autoresponse_arguments(message) if options is False: bot.whisper(source.username, 'Invalid autoresponse') return False options['added_by'] = source.id options['edited_by'] = source.id autoresponse, new_autoresponse = bot.autoresponse_manager.create_autoresponse(phrase, **options) if new_autoresponse is True: bot.whisper(source.username, 'Added your autoresponse (ID: {autoresponse.id})'.format(autoresponse=autoresponse)) AdminLogManager.post('Banphrase added', source, phrase) return True autoresponse.set(**options) autoresponse.data.set(edited_by=options['edited_by']) DBManager.session_add_expunge(autoresponse) bot.autoresponse_manager.commit() bot.whisper(source.username, 'Updated your autoresponse (ID: {autoresponse.id}) with ({what})'.format(autoresponse=autoresponse, what=', '.join([key for key in options if key != 'added_by']))) AdminLogManager.post('Banphrase edited', source, phrase)
def remove_link_whitelist(self, **options): message = options['message'] bot = options['bot'] source = options['source'] if message: id = None try: id = int(message) except ValueError: pass link = self.db_session.query(WhitelistedLink).filter_by( id=id).one_or_none() if link: self.whitelisted_links.remove(link) self.db_session.delete(link) self.db_session.commit() else: bot.whisper(source.username, 'No link with the given id found') return False AdminLogManager.post('Whitelist link removed', source, link.domain) bot.whisper( source.username, 'Successfully removed whitelisted link with id {0}'.format( link.id)) else: bot.whisper(source.username, 'Usage: !remove link whitelist ID') return False
def remove_link_blacklist(self, **options): message = options["message"] bot = options["bot"] source = options["source"] if message: id = None try: id = int(message) except ValueError: pass link = self.db_session.query(BlacklistedLink).filter_by( id=id).one_or_none() if link: self.blacklisted_links.remove(link) self.db_session.delete(link) self.db_session.commit() else: bot.whisper(source.username, "No link with the given id found") return False AdminLogManager.post("Blacklist link removed", source, link.domain) bot.whisper( source.username, "Successfully removed blacklisted link with id {0}".format( link.id)) else: bot.whisper(source.username, "Usage: !remove link blacklist ID") return False
def add_banphrase(bot, source, message, **rest): """Method for creating and editing banphrases. Usage: !add banphrase BANPHRASE [options] Multiple options available: --length LENGTH --perma/--no-perma --notify/--no-notify """ if message: options, phrase = bot.banphrase_manager.parse_banphrase_arguments(message) if options is False: bot.whisper(source, "Invalid banphrase") return False options["added_by"] = source.id options["edited_by"] = source.id banphrase, new_banphrase = bot.banphrase_manager.create_banphrase(phrase, **options) if new_banphrase is True: bot.whisper(source, f"Added your banphrase (ID: {banphrase.id})") AdminLogManager.post("Banphrase added", source, banphrase.id, banphrase.phrase) return True banphrase.set(**options) banphrase.data.set(edited_by=options["edited_by"]) DBManager.session_add_expunge(banphrase) bot.banphrase_manager.commit() bot.whisper( source, f"Updated your banphrase (ID: {banphrase.id}) with ({', '.join([key for key in options if key != 'added_by'])})", ) AdminLogManager.post("Banphrase edited", source, banphrase.id, banphrase.phrase)
def module_toggle(row_id: str, **options) -> ResponseReturnValue: try: json_data = request.get_json() if not json_data: return {"error": "Missing json body"}, 400 data: ToggleState = ToggleStateSchema().load(json_data) except ValidationError as err: return { "error": f"Did not match schema: {json.dumps(err.messages)}" }, 400 with DBManager.create_session_scope() as db_session: row = db_session.query(Module).filter_by(id=row_id).one_or_none() if not row: return {"error": "Module with this ID not found"}, 404 if validate_module(row_id) is False: return {"error": "cannot modify module"}, 400 row.enabled = data.new_state db_session.commit() payload = {"id": row.id, "new_state": data.new_state} AdminLogManager.post("Module toggled", options["user"], "Enabled" if row.enabled else "Disabled", row.id) SocketClientManager.send("module.update", payload) log.info(f"new state: {data} - {data.new_state}") return { "success": "successful toggle", "new_state": data.new_state }
def post(self, row_id, **options): args = self.post_parser.parse_args() try: new_state = int(args["new_state"]) except (ValueError, KeyError): return {"error": "Invalid `new_state` parameter."}, 400 with DBManager.create_session_scope() as db_session: row = db_session.query(Module).filter_by(id=row_id).one_or_none() if not row: return {"error": "Module with this ID not found"}, 404 if validate_module(row_id) is False: return {"error": "cannot modify module"}, 400 row.enabled = True if new_state == 1 else False db_session.commit() payload = {"id": row.id, "new_state": row.enabled} AdminLogManager.post("Module toggled", options["user"], "Enabled" if row.enabled else "Disabled", row.id) SocketClientManager.send("module.update", payload) return {"success": "successful toggle", "new_state": new_state}
def post(self, row_id, **options): args = self.post_parser.parse_args() try: new_state = int(args['new_state']) except (ValueError, KeyError): return {'error': 'Invalid `new_state` parameter.'}, 400 with DBManager.create_session_scope() as db_session: row = db_session.query(Module).filter_by(id=row_id).one_or_none() if not row: return { 'error': 'Module with this ID not found' }, 404 if validate_module(row_id) is False: return {'error': 'cannot modify module'}, 400 row.enabled = True if new_state == 1 else False db_session.commit() payload = { 'id': row.id, 'new_state': row.enabled, } AdminLogManager.post('Module toggled', options['user'], 'Enabled' if row.enabled else 'Disabled', row.id) SocketClientManager.send('module.update', payload) return {'success': 'successful toggle', 'new_state': new_state}
def modules_edit(module_id, **options): module_manager = ModuleManager(None).load(do_reload=False) current_module = find(lambda m: m.ID == module_id, module_manager.all_modules) if current_module is None: return render_template('admin/module_404.html'), 404 sub_modules = [] for module in module_manager.all_modules: module.db_module = None with DBManager.create_session_scope() as db_session: for db_module in db_session.query(Module): module = find(lambda m: m.ID == db_module.id, module_manager.all_modules) if module: module.db_module = db_module if module.PARENT_MODULE == current_module.__class__: sub_modules.append(module) if current_module.db_module is None: return render_template('admin/module_404.html'), 404 if request.method == 'POST': form_values = {key: value for key, value in request.form.items()} res = current_module.parse_settings(**form_values) if res is False: return render_template('admin/module_404.html'), 404 current_module.db_module.settings = json.dumps(res) db_session.commit() settings = None try: settings = json.loads(current_module.db_module.settings) except (TypeError, ValueError): pass current_module.load(settings=settings) payload = { 'id': current_module.db_module.id, } SocketClientManager.send('module.update', payload) AdminLogManager.post('Module edited', options['user'], current_module.NAME) return render_template('admin/configure_module.html', module=current_module, sub_modules=sub_modules) else: settings = None try: settings = json.loads(current_module.db_module.settings) except (TypeError, ValueError): pass current_module.load(settings=settings) return render_template('admin/configure_module.html', module=current_module, sub_modules=sub_modules)
def get(self, timer_id, **options): with DBManager.create_session_scope() as db_session: timer = db_session.query(Timer).filter_by(id=timer_id).one_or_none() if timer is None: return {'error': 'Invalid timer ID'}, 404 AdminLogManager.post('Timer removed', options['user'], timer.name) db_session.delete(timer) SocketClientManager.send('timer.remove', {'id': timer.id}) return {'success': 'good job'}
def get(self, timer_id, **options): with DBManager.create_session_scope() as db_session: timer = db_session.query(Timer).filter_by(id=timer_id).one_or_none() if timer is None: return {"error": "Invalid timer ID"}, 404 AdminLogManager.post("Timer removed", options["user"], timer.name) db_session.delete(timer) SocketClientManager.send("timer.remove", {"id": timer.id}) return {"success": "good job"}
def banphrases_remove(banphrase_id, **options): with DBManager.create_session_scope() as db_session: banphrase = db_session.query(Banphrase).filter_by(id=banphrase_id).one_or_none() if banphrase is None: return {"error": "Invalid banphrase ID"}, 404 AdminLogManager.post("Banphrase removed", options["user"], banphrase.id, banphrase.phrase) db_session.delete(banphrase) db_session.delete(banphrase.data) SocketClientManager.send("banphrase.remove", {"id": banphrase.id}) return {"success": "good job"}, 200
def get(self, banphrase_id, **options): with DBManager.create_session_scope() as db_session: banphrase = db_session.query(Banphrase).filter_by(id=banphrase_id).one_or_none() if banphrase is None: return {'error': 'Invalid banphrase ID'}, 404 AdminLogManager.post('Banphrase removed', options['user'], banphrase.phrase) db_session.delete(banphrase) db_session.delete(banphrase.data) SocketClientManager.send('banphrase.remove', {'id': banphrase.id}) return {'success': 'good job'}, 200
def add_link_whitelist(self, bot, source, message, **rest): parts = message.split(" ") try: for link in parts: self.whitelist_url(link) AdminLogManager.post("Whitelist link added", source, link) except: log.exception("Unhandled exception in add_link") bot.whisper(source, "Some error occurred white adding your links") return False bot.whisper(source, "Successfully added your links")
def add_link_whitelist(self, **options): bot = options['bot'] message = options['message'] source = options['source'] parts = message.split(' ') try: for link in parts: self.whitelist_url(link) AdminLogManager.post('Whitelist link added', source, link) except: log.exception('Unhandled exception in add_link') bot.whisper(source.username, 'Some error occurred white adding your links') return False bot.whisper(source.username, 'Successfully added your links')
def add_link_whitelist(self, **options): bot = options["bot"] message = options["message"] source = options["source"] parts = message.split(" ") try: for link in parts: self.whitelist_url(link) AdminLogManager.post("Whitelist link added", source, link) except: log.exception("Unhandled exception in add_link") bot.whisper(source.username, "Some error occurred white adding your links") return False bot.whisper(source.username, "Successfully added your links")
def add_banphrase(**options): """Method for creating and editing banphrases. Usage: !add banphrase BANPHRASE [options] Multiple options available: --length LENGTH --perma/--no-perma --notify/--no-notify """ message = options["message"] bot = options["bot"] source = options["source"] if message: options, phrase = bot.banphrase_manager.parse_banphrase_arguments( message) if options is False: bot.whisper(source.username, "Invalid banphrase") return False options["added_by"] = source.id options["edited_by"] = source.id banphrase, new_banphrase = bot.banphrase_manager.create_banphrase( phrase, **options) if new_banphrase is True: bot.whisper( source.username, "Added your banphrase (ID: {banphrase.id})".format( banphrase=banphrase)) AdminLogManager.post("Banphrase added", source, phrase) return True banphrase.set(**options) banphrase.data.set(edited_by=options["edited_by"]) DBManager.session_add_expunge(banphrase) bot.banphrase_manager.commit() bot.whisper( source.username, "Updated your banphrase (ID: {banphrase.id}) with ({what})". format(banphrase=banphrase, what=", ".join( [key for key in options if key != "added_by"])), ) AdminLogManager.post("Banphrase edited", source, phrase)
def add_banphrase(self, **options): """Method for creating and editing banphrases. Usage: !add banphrase BANPHRASE [options] Multiple options available: --length LENGTH --perma/--no-perma --notify/--no-notify """ message = options['message'] bot = options['bot'] source = options['source'] if message: options, phrase = bot.banphrase_manager.parse_banphrase_arguments( message) if options is False: bot.whisper(source.username, 'Invalid banphrase') return False options['added_by'] = source.id options['edited_by'] = source.id banphrase, new_banphrase = bot.banphrase_manager.create_banphrase( phrase, **options) if new_banphrase is True: bot.whisper( source.username, 'Added your banphrase (ID: {banphrase.id})'.format( banphrase=banphrase)) AdminLogManager.post('Banphrase added', source, phrase) return True banphrase.set(**options) banphrase.data.set(edited_by=options['edited_by']) DBManager.session_add_expunge(banphrase) bot.banphrase_manager.commit() bot.whisper( source.username, 'Updated your banphrase (ID: {banphrase.id}) with ({what})'. format(banphrase=banphrase, what=', '.join( [key for key in options if key != 'added_by']))) AdminLogManager.post('Banphrase edited', source, phrase)
def remove_banphrase(bot, source, message, **rest): if not message: bot.whisper(source, "Usage: !remove banphrase (BANPHRASE_ID)") return False banphrase_id = None try: banphrase_id = int(message) except ValueError: pass banphrase = bot.banphrase_manager.find_match(message=message, banphrase_id=banphrase_id) if banphrase is None: bot.whisper(source, "No banphrase with the given parameters found") return False AdminLogManager.post("Banphrase removed", source, banphrase.id, banphrase.phrase) bot.whisper(source, f"Successfully removed banphrase with id {banphrase.id}") bot.banphrase_manager.remove_banphrase(banphrase)
def add_banphrase(self, **options): """Method for creating and editing banphrases. Usage: !add banphrase BANPHRASE [options] Multiple options available: --length LENGTH --perma/--no-perma --notify/--no-notify """ message = options['message'] bot = options['bot'] source = options['source'] if message: options, phrase = bot.banphrase_manager.parse_banphrase_arguments(message) if options is False: bot.whisper(source.username, 'Invalid banphrase') return False options['added_by'] = source.id options['edited_by'] = source.id banphrase, new_banphrase = bot.banphrase_manager.create_banphrase(phrase, **options) if new_banphrase is True: bot.whisper(source.username, 'Added your banphrase (ID: {banphrase.id})'.format(banphrase=banphrase)) AdminLogManager.post('Banphrase added', source, phrase) return True banphrase.set(**options) banphrase.data.set(edited_by=options['edited_by']) DBManager.session_add_expunge(banphrase) bot.banphrase_manager.commit() bot.whisper(source.username, 'Updated your banphrase (ID: {banphrase.id}) with ({what})'.format(banphrase=banphrase, what=', '.join([key for key in options if key != 'added_by']))) AdminLogManager.post('Banphrase edited', source, phrase)
def remove_banphrase(self, **options): message = options['message'] bot = options['bot'] source = options['source'] if message: id = None try: id = int(message) except ValueError: pass banphrase = bot.banphrase_manager.find_match(message=message, id=id) if banphrase is None: bot.whisper(source.username, 'No banphrase with the given parameters found') return False AdminLogManager.post('Banphrase removed', source, banphrase.phrase) bot.whisper(source.username, 'Successfully removed banphrase with id {0}'.format(banphrase.id)) bot.banphrase_manager.remove_banphrase(banphrase) else: bot.whisper(source.username, 'Usage: !remove banphrase (BANPHRASE_ID)') return False
def banphrases_toggle(row_id, **options): json_data = request.get_json() if not json_data: return {"error": "No input data provided"}, 400 try: data: ToggleState = ToggleStateSchema().load(json_data) except ValidationError as err: return {"error": f"Did not match schema: {json.dumps(err.messages)}"}, 400 with DBManager.create_session_scope() as db_session: row = db_session.query(Banphrase).filter_by(id=row_id).one_or_none() if not row: return {"error": "Banphrase with this ID not found"}, 404 row.enabled = data.new_state db_session.commit() payload = {"id": row.id, "new_state": data.new_state} AdminLogManager.post( "Banphrase toggled", options["user"], "Enabled" if data.new_state else "Disabled", row.id, row.phrase ) SocketClientManager.send("banphrase.update", payload) return {"success": "successful toggle", "new_state": data.new_state}
def remove_autoresponse(self, **options): message = options['message'] bot = options['bot'] source = options['source'] if message: id = None try: id = int(message) except ValueError: pass autoresponse = bot.autoresponse_manager.find_match(message=message, id=id) if autoresponse is None: bot.whisper(source.username, 'No autoresponse with the given parameters found') return False AdminLogManager.post('Autoresponse removed', source, autoresponse.trigger, autoresponse.response) bot.whisper(source.username, 'Successfully removed autoresponse with id {0}'.format(autoresponse.id)) bot.autoresponse_manager.remove_autoresponse(autoresponse) else: bot.whisper(source.username, 'Usage: !remove autoresponse (AUTORESPONSE_ID)') return False
def banphrases_create(**options): session.pop('banphrase_created_id', None) session.pop('banphrase_edited_id', None) if request.method == 'POST': id = None try: if 'id' in request.form: id = int(request.form['id']) name = request.form['name'].strip() permanent = request.form.get('permanent', 'off') warning = request.form.get('warning', 'off') notify = request.form.get('notify', 'off') case_sensitive = request.form.get('case_sensitive', 'off') sub_immunity = request.form.get('sub_immunity', 'off') remove_accents = request.form.get('remove_accents', 'off') length = int(request.form['length']) phrase = request.form['phrase'] operator = request.form['operator'].strip().lower() except (KeyError, ValueError): abort(403) permanent = True if permanent == 'on' else False warning = True if warning == 'on' else False notify = True if notify == 'on' else False case_sensitive = True if case_sensitive == 'on' else False sub_immunity = True if sub_immunity == 'on' else False remove_accents = True if remove_accents == 'on' else False if len(name) == 0: abort(403) if len(phrase) == 0: abort(403) if length < 0 or length > 1209600: abort(403) valid_operators = [ 'contains', 'startswith', 'endswith', 'exact', 'regex' ] if operator not in valid_operators: abort(403) user = options.get('user', None) if user is None: abort(403) options = { 'name': name, 'phrase': phrase, 'permanent': permanent, 'warning': warning, 'notify': notify, 'case_sensitive': case_sensitive, 'sub_immunity': sub_immunity, 'remove_accents': remove_accents, 'length': length, 'added_by': user.id, 'edited_by': user.id, 'operator': operator, } if id is None: banphrase = Banphrase(**options) banphrase.data = BanphraseData(banphrase.id, added_by=options['added_by']) with DBManager.create_session_scope( expire_on_commit=False) as db_session: if id is not None: banphrase = db_session.query(Banphrase).options( joinedload( Banphrase.data)).filter_by(id=id).one_or_none() if banphrase is None: return redirect('/admin/banphrases/', 303) banphrase.set(**options) banphrase.data.set(edited_by=options['edited_by']) log.info('Updated banphrase ID {} by user ID {}'.format( banphrase.id, options['edited_by'])) AdminLogManager.post('Banphrase edited', user, banphrase.phrase) else: db_session.add(banphrase) db_session.add(banphrase.data) log.info('Added a new banphrase by user ID {}'.format( options['added_by'])) AdminLogManager.post('Banphrase added', user, banphrase.phrase) SocketClientManager.send('banphrase.update', {'id': banphrase.id}) if id is None: session['banphrase_created_id'] = banphrase.id else: session['banphrase_edited_id'] = banphrase.id return redirect('/admin/banphrases/', 303) else: return render_template('admin/create_banphrase.html')
def banphrases_create(**options): session.pop("banphrase_created_id", None) session.pop("banphrase_edited_id", None) if request.method == "POST": id = None try: if "id" in request.form: id = int(request.form["id"]) name = request.form["name"].strip() permanent = request.form.get("permanent", "off") warning = request.form.get("warning", "off") notify = request.form.get("notify", "off") case_sensitive = request.form.get("case_sensitive", "off") sub_immunity = request.form.get("sub_immunity", "off") remove_accents = request.form.get("remove_accents", "off") length = int(request.form["length"]) phrase = request.form["phrase"] operator = request.form["operator"].strip().lower() except (KeyError, ValueError): abort(403) permanent = permanent == "on" warning = warning == "on" notify = notify == "on" case_sensitive = case_sensitive == "on" sub_immunity = sub_immunity == "on" remove_accents = remove_accents == "on" if not name: abort(403) if not phrase: abort(403) if length < 0 or length > 1209600: abort(403) valid_operators = [ "contains", "startswith", "endswith", "exact", "regex" ] if operator not in valid_operators: abort(403) user = options.get("user", None) if user is None: abort(403) options = { "name": name, "phrase": phrase, "permanent": permanent, "warning": warning, "notify": notify, "case_sensitive": case_sensitive, "sub_immunity": sub_immunity, "remove_accents": remove_accents, "length": length, "added_by": user.id, "edited_by": user.id, "operator": operator, } if id is None: banphrase = Banphrase(**options) banphrase.data = BanphraseData(banphrase.id, added_by=options["added_by"]) with DBManager.create_session_scope( expire_on_commit=False) as db_session: if id is not None: banphrase = (db_session.query(Banphrase).options( joinedload( Banphrase.data)).filter_by(id=id).one_or_none()) if banphrase is None: return redirect("/admin/banphrases/", 303) banphrase.set(**options) banphrase.data.set(edited_by=options["edited_by"]) log.info( f"Updated banphrase ID {banphrase.id} by user ID {options['edited_by']}" ) AdminLogManager.post("Banphrase edited", user, banphrase.id, banphrase.phrase) else: db_session.add(banphrase) db_session.add(banphrase.data) db_session.flush() log.info( f"Added a new banphrase by user ID {options['added_by']}" ) AdminLogManager.post("Banphrase added", user, banphrase.id, banphrase.phrase) SocketClientManager.send("banphrase.update", {"id": banphrase.id}) if id is None: session["banphrase_created_id"] = banphrase.id else: session["banphrase_edited_id"] = banphrase.id return redirect("/admin/banphrases/", 303) else: return render_template("admin/create_banphrase.html")
def cmd_module(bot, source, message, **options): module_manager = bot.module_manager if not message: return msg_args = message.split(" ") if len(msg_args) < 1: return sub_command = msg_args[0].lower() if sub_command == "list": messages = split_into_chunks_with_prefix( [{"prefix": "Available modules:", "parts": [module.ID for module in module_manager.all_modules]}], " ", default="No modules available.", ) for message in messages: bot.say(message) elif sub_command == "disable": if len(msg_args) < 2: return module_id = msg_args[1].lower() module = module_manager.get_module(module_id) if not module: bot.say(f"No module with the id {module_id} found") return if module.MODULE_TYPE > ModuleType.TYPE_NORMAL: bot.say(f"Unable to disable module {module_id}") return if not module_manager.disable_module(module_id): bot.say(f"Unable to disable module {module_id}, maybe it's not enabled?") return # Rebuild command cache bot.commands.rebuild() with DBManager.create_session_scope() as db_session: db_module = db_session.query(Module).filter_by(id=module_id).one() db_module.enabled = False AdminLogManager.post("Module toggled", source, "Disabled", module_id) bot.say(f"Disabled module {module_id}") elif sub_command == "enable": if len(msg_args) < 2: return module_id = msg_args[1].lower() module = module_manager.get_module(module_id) if not module: bot.say(f"No module with the id {module_id} found") return if module.MODULE_TYPE > ModuleType.TYPE_NORMAL: bot.say(f"Unable to enable module {module_id}") return if not module_manager.enable_module(module_id): bot.say(f"Unable to enable module {module_id}, maybe it's already enabled?") return # Rebuild command cache bot.commands.rebuild() with DBManager.create_session_scope() as db_session: db_module = db_session.query(Module).filter_by(id=module_id).one() db_module.enabled = True AdminLogManager.post("Module toggled", source, "Enabled", module_id) bot.say(f"Enabled module {module_id}")
def banphrases_create(**options): session.pop('banphrase_created_id', None) session.pop('banphrase_edited_id', None) if request.method == 'POST': id = None try: if 'id' in request.form: id = int(request.form['id']) name = request.form['name'].strip() permanent = request.form.get('permanent', 'off') warning = request.form.get('warning', 'off') notify = request.form.get('notify', 'off') case_sensitive = request.form.get('case_sensitive', 'off') sub_immunity = request.form.get('sub_immunity', 'off') remove_accents = request.form.get('remove_accents', 'off') length = int(request.form['length']) phrase = request.form['phrase'] operator = request.form['operator'].strip().lower() except (KeyError, ValueError): abort(403) permanent = True if permanent == 'on' else False warning = True if warning == 'on' else False notify = True if notify == 'on' else False case_sensitive = True if case_sensitive == 'on' else False sub_immunity = True if sub_immunity == 'on' else False remove_accents = True if remove_accents == 'on' else False if len(name) == 0: abort(403) if len(phrase) == 0: abort(403) if length < 0 or length > 1209600: abort(403) valid_operators = ['contains', 'startswith', 'endswith', 'exact', 'regex'] if operator not in valid_operators: abort(403) user = options.get('user', None) if user is None: abort(403) options = { 'name': name, 'phrase': phrase, 'permanent': permanent, 'warning': warning, 'notify': notify, 'case_sensitive': case_sensitive, 'sub_immunity': sub_immunity, 'remove_accents': remove_accents, 'length': length, 'added_by': user.id, 'edited_by': user.id, 'operator': operator, } if id is None: banphrase = Banphrase(**options) banphrase.data = BanphraseData(banphrase.id, added_by=options['added_by']) with DBManager.create_session_scope(expire_on_commit=False) as db_session: if id is not None: banphrase = db_session.query(Banphrase).options(joinedload(Banphrase.data)).filter_by(id=id).one_or_none() if banphrase is None: return redirect('/admin/banphrases/', 303) banphrase.set(**options) banphrase.data.set(edited_by=options['edited_by']) log.info('Updated banphrase ID {} by user ID {}'.format(banphrase.id, options['edited_by'])) AdminLogManager.post('Banphrase edited', user, banphrase.phrase) else: db_session.add(banphrase) db_session.add(banphrase.data) log.info('Added a new banphrase by user ID {}'.format(options['added_by'])) AdminLogManager.post('Banphrase added', user, banphrase.phrase) SocketClientManager.send('banphrase.update', {'id': banphrase.id}) if id is None: session['banphrase_created_id'] = banphrase.id else: session['banphrase_edited_id'] = banphrase.id return redirect('/admin/banphrases/', 303) else: return render_template('admin/create_banphrase.html')
def timers_create(**options): session.pop("timer_created_id", None) session.pop("timer_edited_id", None) if request.method != "POST": return render_template("admin/create_timer.html") id = None try: if "id" in request.form: id = int(request.form["id"]) name = request.form["name"].strip() interval_online = int(request.form["interval_online"]) interval_offline = int(request.form["interval_offline"]) message_type = request.form["message_type"] message = request.form["message"].strip() except (KeyError, ValueError): abort(403) if interval_online < 0 or interval_offline < 0: abort(403) if message_type not in ["say", "me"]: abort(403) if not message: abort(403) user = options.get("user", None) if user is None: abort(403) options = {"name": name, "interval_online": interval_online, "interval_offline": interval_offline} action = {"type": message_type, "message": message} options["action"] = action if id is None: timer = Timer(**options) with DBManager.create_session_scope(expire_on_commit=False) as db_session: if id is not None: timer = db_session.query(Timer).filter_by(id=id).one_or_none() if timer is None: return redirect("/admin/timers/", 303) old_message = "" new_message = "" try: old_message = timer.action.response new_message = action["message"] except: pass timer.set(**options) if old_message and old_message != new_message: log_msg = f'Timer "{timer.name}" has been updated from "{old_message}" to "{new_message}"' else: log_msg = f'Timer "{timer.name}" has been updated' AdminLogManager.add_entry( "Timer edited", user, log_msg, data={"old_message": old_message, "new_message": new_message} ) else: db_session.add(timer) AdminLogManager.post("Timer added", user, timer.name) SocketClientManager.send("timer.update", {"id": timer.id}) if id is None: session["timer_created_id"] = timer.id else: session["timer_edited_id"] = timer.id return redirect("/admin/timers/", 303)