admin.py 4.12 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 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

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

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

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

    def admin_timer(self, user):
        """
        Relaunch a timer.
        """
        Log.debug("admin_timer command")
        if is_moderator(user):
            return "REACTOR"
        else:
110
            return "Désolé, il ne semble pas que vous ayez les droits pour cette commande."