]> kaliko git repositories - sid.git/commitdiff
tell: trigger on status available
authorkaliko <kaliko@azylum.org>
Mon, 24 Nov 2014 16:10:13 +0000 (17:10 +0100)
committerkaliko <kaliko@azylum.org>
Mon, 24 Nov 2014 16:10:13 +0000 (17:10 +0100)
sid/echo.py

index bb7e66a8163ae6246ba84b14a3351f9ec62fdcd9..cbf2eabf272f098be976327fb760d3d3286a5a57 100644 (file)
@@ -21,31 +21,24 @@ class Echo(Plugin):
 
     def __init__(self, bot):
         Plugin.__init__(self, bot)
-        self.online = set()
         self.inbox = dict()
+        self.presence = dict()
         # The groupchat_presence event is triggered whenever a
         # presence stanza is received from any chat room, including
         # any presences you send yourself. To limit event handling
         # to a single room, use the events muc::room@server::presence,
         # muc::room@server::got_online, or muc::room@server::got_offline.
-        bot.add_event_handler("muc::%s::got_offline" % self.bot.room, self._went_offline)
-        bot.add_event_handler("muc::%s::got_online" %  self.bot.room, self._went_online)
         bot.add_event_handler("muc::%s::presence" %    self.bot.room, self.log_presence)
 
     def log_presence(self, pres):
         self.log.debug('{0}: {1}'.format(pres['muc']['nick'], pres['type']))
-
-    def _went_online(self, pres):
-        nick = pres['muc']['nick']
-        self.online.add(nick)
-        while self.inbox.get(nick, []):
-            self.send(self.inbox.get(nick).pop())
-        self.inbox.pop(nick)
-
-    def _went_offline(self, pres):
         nick = pres['muc']['nick']
-        if nick in self.online:
-            self.online.remove(nick)
+        self.presence.update({nick: (pres['muc']['role'], pres['type'])})
+        self.log.debug(self.presence)
+        if pres['type'] == 'available':
+            while self.inbox.get(nick, []):
+                self.send(self.inbox.get(nick).pop())
+            self.inbox.pop(nick)
 
     @botcmd
     def tell(self, message, args):
@@ -55,10 +48,16 @@ class Echo(Plugin):
         if not len(args):
             self.send('Missing arguments:\n{}'.format(self.tell.__doc__))
             return
-        if len(args) == 1 and args[0] == 'queue':
-            self.send('\n'.join(['{0}:\n\t{1}'.format(k, '\n'.join(v))
-                              for k, v in self.inbox.items()]))
-            return
+        if len(args) == 1:
+            if args[0] == 'queue':
+                self.send('\n'.join(['{0}:\n\t{1}'.format(k, '\n'.join(v))
+                                      for k, v in self.inbox.items()]))
+                return
+            if args[0] == 'purge':
+                sender = message['from'].resource
+                if self.presence[sender][0] == 'moderator':
+                    self.online = set()
+                return
         if len(args) < 2:
             self.send('Please provide a message:\n{}'.format(self.tell.__doc__))
             return
@@ -67,7 +66,8 @@ class Echo(Plugin):
         tell_msg = ' '.join(message['body'].split()[2:])
         self.log.debug('{0}: {1}'.format(recipient, tell_msg))
         letter = '{0}, {1} told me to tell you: {2}'.format(recipient, sender, tell_msg)
-        if recipient in self.online:
+        if (self.presence.get(recipient) and
+                self.presence[recipient][1] == 'available'):
             return
         if recipient in self.inbox.keys():
            self.inbox[recipient].append(letter)