]> kaliko git repositories - sid.git/blob - sid/echo.py
Prototype of Echo plugin, yet to be renamed
[sid.git] / sid / echo.py
1 # -*- coding: utf-8 -*-
2
3 # Copyright (C) 2007-2012 Thomas Perl <thp.io/about>
4 # Copyright (C) 2014 kaliko <kaliko@azylum.org>
5
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, version 3 only.
9
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14
15 # You should have received a copy of the GNU General Public License
16 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 from .plugin import Plugin, botcmd
19
20 class Echo(Plugin):
21
22     def __init__(self, bot):
23         Plugin.__init__(self, bot)
24         self.online = set()
25         self.inbox = dict()
26         # The groupchat_presence event is triggered whenever a
27         # presence stanza is received from any chat room, including
28         # any presences you send yourself. To limit event handling
29         # to a single room, use the events muc::room@server::presence,
30         # muc::room@server::got_online, or muc::room@server::got_offline.
31         bot.add_event_handler("muc::%s::got_offline" % self.bot.room, self._went_offline)
32         bot.add_event_handler("muc::%s::got_online" %  self.bot.room, self._went_online)
33         bot.add_event_handler("muc::%s::presence" %    self.bot.room, self.log_presence)
34
35     def log_presence(self, pres):
36         self.log.debug('{0}: {1}'.format(pres['muc']['nick'], pres['type']))
37
38     def _went_online(self, pres):
39         nick = pres['muc']['nick']
40         self.online.add(nick)
41         while self.inbox.get(nick, []):
42             self.bot.send_message(mto=self.bot.room,
43                               mbody=self.inbox.get(nick).pop(),
44                               mtype='groupchat')
45         self.inbox.pop(nick)
46
47     def _went_offline(self, pres):
48         nick = pres['muc']['nick']
49         if nick in self.online:
50             self.online.remove(nick)
51
52     @botcmd
53     def tell(self, message, args):
54         """drop a message to be delivred when someone gets online.
55         !tell queue        : messages in queue
56         !tell <nick> <msg> : append <msg> to <nick> in queue"""
57         if not len(args):
58             return 'Missing arguments:\n{}'.format(self.tell.__doc__)
59         if len(args) == 1 and args[0] == 'queue':
60             return '\n'.join(['{0}:\n\t{1}'.format(k, '\n'.join(v))
61                               for k, v in self.inbox.items()])
62         if len(args) < 2:
63             return 'Please provide a message:\n{}'.format(self.tell.__doc__)
64         sender = message['from'].resource
65         recipient = message['body'].split()[1]
66         tell_msg = ' '.join(message['body'].split()[2:])
67         self.log.debug('{0}: {1}'.format(recipient, tell_msg))
68         letter = '{0}, {1} told me to tell you: {2}'.format(recipient, sender, tell_msg)
69         if recipient in self.online:
70             return
71         if recipient in self.inbox.keys():
72            self.inbox[recipient].append(letter)
73         else:
74            self.inbox[recipient] = [letter]
75
76
77 # VIM MODLINE
78 # vim: ai ts=4 sw=4 sts=4 expandtab