def index(): b3log_file = StringIO() config_log_content = "" config_content = None chat_text = "" chat_consequences = "" playername_text = "" playername_consequences = "" if request.method == "POST": # create a B3 bot b3bot_conf = XmlConfigParser() b3bot_conf.loadFromString("<configuration/>") b3bot = FakeConsole(b3bot_conf) b3bot.screen = b3log_file # create a Censor plugin instance censor_conf = XmlConfigParser() censor_plugin = CensorPlugin(b3bot, censor_conf) # create a player Joe to test the chat messages joe = FakeClient(console=b3bot, name="Joe", guid="000joe000") # monkey patch the Censor plugin penalizeClient method to log its actions # and remove a dependy over the admin plugin censor_plugin.penalizeClient = penalizeClient censor_plugin.penalizeClientBadname = penalizeClientBadname # add our log handler to collect B3 log messages b3log.handlers = [] b3log_handler = logging.StreamHandler(b3log_file) b3log.addHandler(b3log_handler) b3log.setLevel(logging.DEBUG) b3bot.log.disabled = False # read form data config_content = request.form["config_content"] chat_text = request.form["chat"] playername_text = request.form["playername"] if config_content is not None: # we got a config to test b3log_file.truncate(0) b3log.info("--------- loading Censor plugin config ----------") try: censor_conf.loadFromString(config_content) except ConfigFileNotValid, err: b3log.error("bad config file format : %r" % err) b3log_file.seek(0) config_log_content = b3log_file.read() except Exception, err: b3log.error("Unexpected error : %r" % err) b3log_file.seek(0) config_log_content = b3log_file.read() else: censor_plugin.onLoadConfig() b3log.info("--------- Censor plugin config loaded ----------") b3log_file.seek(0) config_log_content = b3log_file.read() if chat_text: # we got some chat to check for badwords b3log_file.truncate(0) try: censor_plugin.checkBadWord(text=chat_text, client=joe) except VetoEvent: pass b3log_file.seek(0) chat_consequences = b3log_file.read() if playername_text: # we got some player name to check for badnames b3log_file.truncate(0) try: joe.name = playername_text with patch.object(threading, "Timer"): # prevent the Censor plugin to check again every minute censor_plugin.checkBadName(client=joe) except VetoEvent: pass b3log_file.seek(0) playername_consequences = b3log_file.read()