def onLoadConfig(self):
     CensorPlugin.onLoadConfig(self)
     try:
         self._mute = self.config.getboolean("urbanterror", "mute")
     except:
         pass
     try:
         self._slap = self.config.getboolean("urbanterror", "slap")
     except:
         pass
     try:
         self._muteduration1 = self.config.getfloat("urbanterror", "muteduration1")
     except:
         pass
     try:
         self._muteduration2 = self.config.getfloat("urbanterror", "muteduration2")
     except:
         pass
     try:
         self._muteduration3 = self.config.getfloat("urbanterror", "muteduration3")
     except:
         pass
     try:
         self._warnafter = self.config.getint("urbanterror", "warn_after")
     except:
         pass
 def onLoadConfig(self):
     CensorPlugin.onLoadConfig(self)
     try:
         self._mute = self.config.getboolean('urbanterror', 'mute')
     except:
         pass
     try:
         self._slap = self.config.getboolean('urbanterror', 'slap')
     except:
         pass
     try:
         self._muteduration1 = self.config.getfloat('urbanterror', 'muteduration1')
     except:
         pass
     try:
         self._muteduration2 = self.config.getfloat('urbanterror', 'muteduration2')
     except:
         pass
     try:
         self._muteduration3 = self.config.getfloat('urbanterror', 'muteduration3')
     except:
         pass
     try:
         self._warnafter = self.config.getint('urbanterror', 'warn_after')
     except:
         pass
Exemple #3
0
class CensorTestCase(B3TestCase):
    """
    Ease testcases that need an working B3 console and need to control the censor plugin config
    """
    def setUp(self):
        # Timer needs to be patched or the Censor plugin would schedule a 2nd check one minute after
        # penalizing a player.
        self.timer_patcher = patch('threading.Timer')
        self.timer_patcher.start()

        self.log = logging.getLogger('output')
        self.log.propagate = False

        B3TestCase.setUp(self)
        self.console.startup()
        self.log.propagate = True

        self.joe = FakeClient(self.console, name="Joe", exactName="Joe", guid="zaerezarezar", groupBits=1, team=b3.TEAM_UNKNOWN)

    def tearDown(self):
        B3TestCase.tearDown(self)
        self.timer_patcher.stop()

    def init_plugin(self, config_content):
        self.conf = XmlConfigParser()
        self.conf.setXml(config_content)
        self.p = CensorPlugin(self.console, self.conf)
        self.log.setLevel(logging.DEBUG)
        self.log.info("============================= Censor plugin: loading config ============================")
        self.p.onLoadConfig()
        self.log.info("============================= Censor plugin: starting  =================================")
        self.p.onStartup()
 def onLoadConfig(self):
     CensorPlugin.onLoadConfig(self)
     try:
         self._mute = self.config.getboolean('urbanterror', 'mute')
     except:
         pass
     try:
         self._slap = self.config.getboolean('urbanterror', 'slap')
     except:
         pass
     try:
         self._muteduration1 = self.config.getfloat('urbanterror',
                                                    'muteduration1')
     except:
         pass
     try:
         self._muteduration2 = self.config.getfloat('urbanterror',
                                                    'muteduration2')
     except:
         pass
     try:
         self._muteduration3 = self.config.getfloat('urbanterror',
                                                    'muteduration3')
     except:
         pass
     try:
         self._warnafter = self.config.getint('urbanterror', 'warn_after')
     except:
         pass
Exemple #5
0
class CensorTestCase(B3TestCase):
    """base class for TestCase to apply to the Censor plugin"""

    def setUp(self):
        # Timer needs to be patched or the Censor plugin would schedule a 2nd check one minute after
        # penalizing a player.
        self.timer_patcher = patch('threading.Timer')
        self.timer_patcher.start()

        super(CensorTestCase, self).setUp()
        self.conf = XmlConfigParser()
        self.conf.setXml(r"""
            <configuration plugin="censor">
                <badwords>
                    <penalty type="warning" reasonkeyword="racist"/>
                </badwords>
                <badnames>
                    <penalty type="warning" reasonkeyword="badname"/>
                </badnames>
            </configuration>
        """)
        self.p = CensorPlugin(b3.console, self.conf)
        self.p.onLoadConfig()

    def tearDown(self):
        self.timer_patcher.stop()

    def assert_name_penalized_count(self, name, count):
        self.p.penalizeClientBadname = Mock()

        mock_client = Mock()
        mock_client.connected = True
        mock_client.exactName = name

        self.p.checkBadName(mock_client)
        self.assertEquals(count, self.p.penalizeClientBadname.call_count, "name '%s' should have been penalized %s time" % (name, count))

    def assert_name_is_penalized(self, name):
        self.assert_name_penalized_count(name, 1)

    def assert_name_is_not_penalized(self, name):
        self.assert_name_penalized_count(name, 0)

    def assert_chat_is_penalized(self, text):
        self.p.penalizeClient = Mock()

        mock_client = Mock()
        mock_client.connected = True

        try:
            self.p.checkBadWord(text, mock_client)
            self.fail("text [%s] should have raised a VetoEvent" % text)
        except b3.events.VetoEvent, e:
            self.assertEquals(1, self.p.penalizeClient.call_count, "text [%s] should have been penalized" % text)
            return self.p.penalizeClient.call_args[0] if len(self.p.penalizeClient.call_args) else None
Exemple #6
0
class CensorTestCase(B3TestCase):
    """base class for TestCase to apply to the Censor plugin"""

    def setUp(self):
        # Timer needs to be patched or the Censor plugin would schedule a 2nd check one minute after
        # penalizing a player.
        self.timer_patcher = patch('threading.Timer')
        self.timer_patcher.start()

        super(CensorTestCase, self).setUp()
        self.conf = XmlConfigParser()
        self.conf.setXml(r"""
            <configuration plugin="censor">
                <badwords>
                    <penalty type="warning" reasonkeyword="racist"/>
                </badwords>
                <badnames>
                    <penalty type="warning" reasonkeyword="badname"/>
                </badnames>
            </configuration>
        """)
        self.p = CensorPlugin(self.console, self.conf)
        self.p.onLoadConfig()

    def tearDown(self):
        self.timer_patcher.stop()

    def assert_name_penalized_count(self, name, count):
        self.p.penalizeClientBadname = Mock()

        mock_client = Mock()
        mock_client.connected = True
        mock_client.exactName = name

        self.p.checkBadName(mock_client)
        self.assertEquals(count, self.p.penalizeClientBadname.call_count, "name '%s' should have been penalized %s time" % (name, count))

    def assert_name_is_penalized(self, name):
        self.assert_name_penalized_count(name, 1)

    def assert_name_is_not_penalized(self, name):
        self.assert_name_penalized_count(name, 0)

    def assert_chat_is_penalized(self, text):
        self.p.penalizeClient = Mock()

        mock_client = Mock()
        mock_client.connected = True

        try:
            self.p.checkBadWord(text, mock_client)
            self.fail("text [%s] should have raised a VetoEvent" % text)
        except b3.events.VetoEvent, e:
            self.assertEquals(1, self.p.penalizeClient.call_count, "text [%s] should have been penalized" % text)
            return self.p.penalizeClient.call_args[0] if len(self.p.penalizeClient.call_args) else None
Exemple #7
0
class CensorTestCase(B3TestCase):
    """
    Ease testcases that need an working B3 console and need to control the censor plugin config
    """
    def setUp(self):
        # Timer needs to be patched or the Censor plugin would schedule a 2nd check one minute after
        # penalizing a player.
        self.timer_patcher = patch('threading.Timer')
        self.timer_patcher.start()

        self.log = logging.getLogger('output')
        self.log.propagate = False

        B3TestCase.setUp(self)
        self.console.startup()
        self.log.propagate = True

        self.joe = FakeClient(self.console,
                              name="Joe",
                              exactName="Joe",
                              guid="zaerezarezar",
                              groupBits=1,
                              team=b3.TEAM_UNKNOWN)

    def tearDown(self):
        B3TestCase.tearDown(self)
        self.timer_patcher.stop()

    def init_plugin(self, config_content):
        self.conf = XmlConfigParser()
        self.conf.setXml(config_content)
        self.p = CensorPlugin(self.console, self.conf)
        self.log.setLevel(logging.DEBUG)
        self.log.info(
            "============================= Censor plugin: loading config ============================"
        )
        self.p.onLoadConfig()
        self.log.info(
            "============================= Censor plugin: starting  ================================="
        )
        self.p.onStartup()
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()
Exemple #9
0
class Censor_functional_tests(unittest.TestCase):

    def setUp(self):
        # create a FakeConsole parser
        self.parser_conf = XmlConfigParser()
        self.parser_conf.loadFromString(r"""<configuration/>""")
        self.console = FakeConsole(self.parser_conf)
        self.console.startup()

        self.joe = FakeClient(self.console, name="Joe", exactName="Joe", guid="zaerezarezar", groupBits=1, team=b3.TEAM_UNKNOWN)

    def init_plugin(self, config_content):
        self.conf = XmlConfigParser()
        self.conf.setXml(config_content)
        self.p = CensorPlugin(self.console, self.conf)
        self.p.onLoadConfig()
        self.p.onStartup()


    def test_conf(self, timer_patch):
        self.init_plugin(r"""
            <configuration plugin="censor">
                <settings name="settings">
                    <set name="max_level">40</set>
                    <!-- ignore bad words that have equal or less characters: -->
                    <set name="ignore_length">3</set>
                </settings>
                <badwords>
                    <penalty type="warning" reasonkeyword="default_reason"/>
                    <badword name="foo" lang="en">
                        <regexp>\bf[o0]{2}\b</regexp>
                    </badword>
                </badwords>
                <badnames>
                    <penalty type="warning" reasonkeyword="badname"/>
                    <badname name="c**t">
                        <word>c**t</word>
                    </badname>
                </badnames>
            </configuration>
        """)
        self.assertEqual(1, len(self.p._badWords))


    def test_joe_says_badword(self, timer_patch):
        self.init_plugin(r"""
            <configuration plugin="censor">
                <settings name="settings">
                    <set name="max_level">40</set>
                    <!-- ignore bad words that have equal or less characters: -->
                    <set name="ignore_length">3</set>
                </settings>
                <badwords>
                    <penalty type="warning" reasonkeyword="default_reason"/>
                    <badword name="foo" lang="en">
                        <regexp>\bf[o0]{2}\b</regexp>
                    </badword>
                </badwords>
                <badnames>
                    <penalty type="warning" reasonkeyword="badname"/>
                    <badname name="c**t">
                        <word>c**t</word>
                    </badname>
                </badnames>
            </configuration>
        """)
        self.joe.warn = Mock()
        self.joe.connects(0)
        self.joe.says("qsfdl f0o!")
        self.assertEqual(1, self.joe.warn.call_count)


    def test_cunt_connects(self, timer_patch):
        self.init_plugin(r"""
            <configuration plugin="censor">
                <settings name="settings">
                    <set name="max_level">40</set>
                    <!-- ignore bad words that have equal or less characters: -->
                    <set name="ignore_length">3</set>
                </settings>
                <badwords>
                    <penalty type="warning" reasonkeyword="default_reason"/>
                    <badword name="foo" lang="en">
                        <regexp>\bf[o0]{2}\b</regexp>
                    </badword>
                </badwords>
                <badnames>
                    <penalty type="warning" reasonkeyword="badname"/>
                    <badname name="c**t">
                        <word>c**t</word>
                    </badname>
                </badnames>
            </configuration>
        """)
        self.joe.name = self.joe.exactName = "c**t"
        self.joe.warn = Mock()
        self.joe.connects(0)
        self.assertEqual(1, self.joe.warn.call_count)


    def test_2_letters_badword_when_ignore_length_is_2(self, timer_patch):
        self.init_plugin(r"""
            <configuration plugin="censor">
                <settings name="settings">
                    <set name="max_level">40</set>
                    <!-- ignore bad words that have equal or less characters: -->
                    <set name="ignore_length">2</set>
                </settings>
                <badwords>
                    <penalty type="warning" reasonkeyword="default_reason"/>
                    <badword name="TG" lang="fr">
                        <regexp>\bTG\b</regexp>
                    </badword>
                </badwords>
                <badnames>
                    <penalty type="warning" reasonkeyword="badname"/>
                </badnames>
            </configuration>
        """)

        self.joe.warn = Mock()
        self.joe.warn.reset_mock()
        self.joe.connects(0)
        self.joe.says("tg")
        self.assertEqual(0, self.joe.warn.call_count)


    def test_2_letters_badword_when_ignore_length_is_1(self, timer_patch):
        self.init_plugin(r"""
            <configuration plugin="censor">
                <settings name="settings">
                    <set name="max_level">40</set>
                    <!-- ignore bad words that have equal or less characters: -->
                    <set name="ignore_length">1</set>
                </settings>
                <badwords>
                    <penalty type="warning" reasonkeyword="default_reason"/>
                    <badword name="TG" lang="fr">
                        <regexp>\bTG\b</regexp>
                    </badword>
                </badwords>
                <badnames>
                    <penalty type="warning" reasonkeyword="badname"/>
                </badnames>
            </configuration>
        """)

        self.joe.warn = Mock()
        self.joe.warn.reset_mock()
        self.joe.connects(0)
        self.joe.says("tg")
        self.assertEqual(1, self.joe.warn.call_count)