admin.py 4.53 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
#encoding: utf-8
"""
Monitoring methods.
"""

import re
import sqlite3

import config
from logs import Log
11
from utils import is_moderator
12 13 14 15 16 17 18 19 20

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
Mindiell's avatar
Mindiell committed
21
        command = re.search("[~!]admin (list|add|del|timer|testtimer)", msg)
22 23 24 25 26 27 28 29 30 31
        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)
32 33
            elif command == "testtimer":
                return self.admin_testtimer(user)
34 35 36 37 38 39 40 41 42 43 44

    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"))
45
            return "Liste des modérateurs actuels: %s" % ", ".join(sorted(names))
46
        else:
47
            return "Désolé, il ne semble pas que vous ayez les droits pour cette commande."
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71

    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()
72
                    return "%s a(ont) été ajouté(s) à la liste des modérateurs" % ", ".join(names)
73
                else:
74
                    return "Ces utilisateurs sont déjà modérateurs"
75 76 77
            except Exception:
                return ""
        else:
78
            return "Désolé, il ne semble pas que vous ayez les droits pour cette commande."
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97

    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()
98
                return "%s a(ont) été retiré(s) de la liste des modérateurs." % ", ".join(names)
99 100 101
            except Exception:
                return ""
        else:
102
            return "Désolé, il ne semble pas que vous ayez les droits pour cette commande."
103 104 105 106 107 108 109 110 111

    def admin_timer(self, user):
        """
        Relaunch a timer.
        """
        Log.debug("admin_timer command")
        if is_moderator(user):
            return "REACTOR"
        else:
112
            return "Désolé, il ne semble pas que vous ayez les droits pour cette commande."
113 114 115 116 117 118 119 120 121 122

    def admin_testtimer(self, user):
        """
        Test if timer is still alive.
        """
        Log.debug("admin_testtimer command")
        if is_moderator(user):
            return "TEST_REACTOR"
        else:
            return "Désolé, il ne semble pas que vous ayez les droits pour cette commande."