admin.py 3.88 KB
Newer Older
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#encoding: utf-8
"""
Monitoring methods.
"""

import re
import sqlite3

import config
from logs import Log
from messages import messages
from utils import get_cursor, get_url, is_moderator

class Admin():
    def admin(self, user, msg):
        """
        Manage moderation.
        A sub-command should be behind the !~admin command.
        """
        Log.debug("admin command")
        # Searching for a command after admin keyword
        command = re.search("[~!]admin (list|add|del|timer)", msg)
        if command:
            command = command.group(1)
            if command == "list":
                return self.admin_list(user)
            elif command == "add":
                return self.admin_add(user, msg)
            elif command == "del":
                return self.admin_del(user, msg)
            elif command == "timer":
                return self.admin_timer(user)

    def admin_list(self, user):
        """
        List actual moderators.
        """
        Log.debug("admin_list command")
        if is_moderator(user):
            connection = sqlite3.connect(config.sqlite_db)
            names = []
            for row in connection.execute("SELECT name FROM moderator"):
                names.append(row[0].encode("utf-8"))
            return messages["admin_list"] % ", ".join(sorted(names))
        else:
            return messages["not_moderator"]

    def admin_add(self, user, msg):
        """
        Add some new moderators if not existing yet.
        """
        Log.debug("admin_add command")
        if is_moderator(user):
            try:
                names = []
                connection = sqlite3.connect(config.sqlite_db)
                result = re.search("[!~]admin add (([^,]+, ?)+)?(.*)", msg)
                if result.group(1):
                    names = [name.strip() for name in result.group(1).split(",") if name.strip() != ""]
                names.append(result.group(3))
                # Do not add actual moderators
                moderators = []
                for row in connection.execute("SELECT name FROM moderator"):
                    moderators.append(row[0].encode("utf-8"))
                names = list(set([name for name in names if name not in moderators]))
                if names:
                    # Converting set in list of tuples
                    values = [(name,) for name in names]
                    connection.executemany("INSERT INTO moderator (name) VALUES (?)", values)
                    connection.commit()
                    return messages["admin_add"] % ", ".join(names)
                else:
                    return messages["admin_add_empty"]
            except Exception:
                return ""
        else:
            return messages["not_moderator"]

    def admin_del(self, user, msg):
        """
        Delete a moderator from list.
        """
        Log.debug("admin_del command")
        if is_moderator(user):
            try:
                names = []
                result = re.search("[!~]admin del (([^,]+, ?)+)?(.*)", msg)
                if result.group(1):
                    names = [name.strip() for name in result.group(1).split(",") if name.strip() != ""]
                names.append(result.group(3))
                names = list(set(names))
                Log.debug(names)
                connection = sqlite3.connect(config.sqlite_db)
                for name in names:
                    connection.execute("DELETE FROM moderator WHERE name=?", (name, ))
                connection.commit()
                return messages["admin_del"] % ", ".join(names)
            except Exception:
                return ""
        else:
            return messages["not_moderator"]

    def admin_timer(self, user):
        """
        Relaunch a timer.
        """
        Log.debug("admin_timer command")
        if is_moderator(user):
            return "REACTOR"
        else:
            return messages["not_moderator"]