aboutsummaryrefslogtreecommitdiff
path: root/modules/badwords.py
blob: d23a4137fb7564a2b9d7b28eb815bf8f8ef1090e (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#--depends-on check_mode
#--depends-on commands

import time
from src import ModuleManager, utils

class Module(ModuleManager.BaseModule):
    @utils.hook("received.command.badwordslist", channel_only=True)
    def badwords_list(self, event):
        """
        :help: List the badwords in the current channel
        :require_mode: o
        """
        badwords = event["target"].get_setting("badwords", [])
        badwords = ("(%d) %s" % (i+1, badword["pattern"]) for i, badword in
            enumerate(badwords))
        event["stdout"].write("%s: %s" % (event["target"].name,
            ", ".join(badwords)))

    @utils.hook("received.command.badwordsadd", channel_only=True, min_args=2)
    def badwords_add(self, event):
        """
        :help: Add a badword to the badwords list for the current channel
        :usage: kick|ban|kickban <pattern>
        :require_mode: o
        """
        action = event["args_split"][0].lower()
        if not action in ["kick", "ban", "kickban"]:
            raise utils.EventError("Unknown action '%s'" % action)

        badwords = event["target"].get_setting("badwords", [])
        badwords.append({
            "pattern": " ".join(event["args_split"][1:]).lower(),
            "action": action,
            "added_by": event["user"].nickname,
            "added_at": time.time()})
        event["target"].set_setting("badwords", badwords)
        event["stdout"].write("%s: added to badwords" % event["user"].nickname)

    @utils.hook("received.command.badwordsdel", channel_only=True, min_args=1)
    def badwords_del(self, event):
        """
        :help: Remove a badwords from the current channel's badwords list
        :usage: <index>
        :require_mode: o
        """
        index = event["args_split"][0]
        if not index.isdigit() or int(index) == 0:
            raise utils.EventError("%s: index must be a positive number" %
                event["user"].nickname)

        index_int = int(event["args_split"][0])-1
        badwords = event["target"].get_setting("badwords", [])
        if index_int >= len(badwords):
           raise utils.EventError("%s: unknown badwords index %s" % (
                event["user"].nickname, index))

        badwords.pop(index_int)
        event["target"].set_setting("badwords", badwords)
        event["stdout"].write("%s: removed from badwords" %
            event["user"].nickname)

    @utils.hook("received.message.channel")
    def channel_message(self, event):
        if event["channel"].mode_or_above(event["user"], "v"):
            return

        badwords = event["channel"].get_setting("badwords", [])
        message_lower = event["message"].lower()
        for badword in badwords:
            if badword["pattern"] in message_lower:
                kick = False
                ban = False
                if badword["action"] == "kick":
                    kick = True
                elif badword["action"] == "kickban":
                    ban = True
                    kick = True
                elif badword["action"] == "ban":
                    ban = True

                if ban:
                    event["channel"].send_ban("*!%s@%s" % (
                        event["user"].username,
                        event["user"].realname))
                if kick:
                    event["channel"].send_kick(event["user"].nickname,
                        "You said a badword!")