~isoschiz/endroid/globalplugins

« back to all changes in this revision

Viewing changes to src/endroid/plugins/blacklist.py

  • Committer: Martin Morrison
  • Date: 2013-09-07 22:52:00 UTC
  • Revision ID: martin.morrison@ensoft.co.uk-20130907225200-4ad2zu4un86q4y32
Move closer to full support for GlobalPlugins:

- message and user handling now support multiple places for most operations
- Filled out the APIs for rosters access for plugins (still some missing though)
- Migrated a bunch of plugins to a bunch of the newer APIs
- Various minor improvements to plugins along the way

Key point is: some of the plugins are now declared as Global, so have had all their class variables removed. No testing done as yet though.

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
# Created by Martin Morrison
5
5
# -----------------------------------------------------------------------------
6
6
 
7
 
from endroid.pluginmanager import Plugin
 
7
from endroid.pluginmanager import GlobalPlugin
8
8
from endroid.database import Database
9
9
from endroid.cron import Cron
10
10
 
11
11
# DB constants
12
 
DB_NAME = "Blacklist"
13
12
DB_TABLE = "Blacklist"
14
13
CRON_UNBLACKLIST = "Blacklist_UnBlacklist"
15
14
 
16
 
class Blacklist(Plugin):
 
15
class Blacklist(GlobalPlugin):
17
16
    """
18
17
    Plugin to provide blacklisting capabilities for EnDroid.
19
18
 
25
24
    help = "Maintain a blacklist of JIDs who get ignored by EnDroid."
26
25
    hidden = True
27
26
 
28
 
    _blacklist = set()
29
 
 
30
27
    def endroid_init(self):
31
28
        """
32
29
        Initialise the plugin, and recover the blacklist from the DB.
33
30
        """
34
31
        self.admins = set(map(str.strip, self.vars.get("admins", [])))
 
32
        self._blacklist = set()
35
33
 
36
34
        self.task = self.cron.register(self.unblacklist, CRON_UNBLACKLIST)
37
35
 
44
42
        for row in self.database.fetch(DB_TABLE, ("userjid",)):
45
43
            self.blacklist(row["userjid"])
46
44
 
47
 
    def get_blacklist(self):
48
 
        return self._blacklist
49
 
        
50
45
    def checklist(self, msg):
51
46
        """
52
47
        Receive filter callback - checks the message sender against the
53
48
        blacklist
54
49
        """
55
 
        return msg.sender not in self.get_blacklist()
 
50
        return msg.sender not in self._blacklist
56
51
 
57
52
    def checksend(self, msg):
58
53
        """
59
54
        Send filter callback - checks the message recipient against the
60
55
        blacklist
61
56
        """
62
 
        return msg.sender not in self.get_blacklist()
 
57
        return msg.sender not in self._blacklist
63
58
 
64
59
    def command(self, msg):
65
60
        """
73
68
        bl, cmd, user = parts[:3]
74
69
 
75
70
        if msg.sender in self.admins and bl == "blacklist":
76
 
            if cmd == "add" and user not in self.get_blacklist():
 
71
            if cmd == "add" and user not in self._blacklist:
77
72
                self.blacklist(user)
78
 
            elif cmd == "remove" and user in self.get_blacklist():
 
73
            elif cmd == "remove" and user in self._blacklist:
79
74
                self.unblacklist(user)
80
75
            elif cmd == "list":
81
 
                msg.reply("Blacklist: " + ", ".join(self.get_blacklist() or ['None']))
 
76
                msg.reply("Blacklist: " + ", ".join(self._blacklist or ['None']))
82
77
            else:
83
78
                msg.unhandled()
84
79
        else: