summaryrefslogtreecommitdiff
path: root/plugins/regex_admin.py
blob: befbcac20edc3e9b7837d90de95d1ae91270e2df (plain)
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
"""
This plugins adds a :term:`/rkick` and a :term:`/rban` command,
in order to kick/ban according to a regex on a nick.

Commands
--------

Those commands take a regular expression (as defined in the
`re module documentation`_) as a parameter.

.. glossary::
    :sorted:

    /rkick
        **Usage:** ``/rkick <regex>``

        Kick a participant using a regex.


    /rban
        **Usage:** ``/rban <regex>``

        Ban a participant using a regex.

.. _re module documentation: http://docs.python.org/3/library/re.html
"""

from poezio.plugin import BasePlugin
from poezio.tabs import MucTab

import re


class Plugin(BasePlugin):
    def init(self):
        self.api.add_tab_command(
            MucTab,
            'rkick',
            self.command_rkick,
            usage='<regex>',
            help='Kick occupants of a room according to a regex',
            short='Regex Kick')

        self.api.add_tab_command(
            MucTab,
            'rban',
            self.command_rban,
            usage='<regex>',
            help='Ban occupants of a room according to a regex',
            short='Regex Ban')

    def return_users(self, users, regex):
        try:
            reg = re.compile(regex)
        except:
            return []

        ret = []
        for user in users:
            if reg.match(user.nick):
                ret.append(user)

        return ret

    def command_rban(self, regex):
        tab = self.api.current_tab()
        users = self.return_users(tab.users, regex)
        for user in users:
            tab.command_ban(user.nick)

    def command_rkick(self, regex):
        tab = self.api.current_tab()
        users = self.return_users(tab.users, regex)
        for user in users:
            tab.command_kick(user.nick)