]> kaliko git repositories - sid.git/blob - sid/echo.py
Better use of logging
[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.inbox = dict()
25         self.presence = 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::presence" %    self.bot.room, self.log_presence)
32
33     def log_presence(self, pres):
34         self.log.debug('%s: %s', pres['muc']['nick'], pres['type'])
35         nick = pres['muc']['nick']
36         self.presence.update({nick: (pres['muc']['role'], pres['type'])})
37         self.log.debug(self.presence)
38         if pres['type'] == 'available':
39             while self.inbox.get(nick, []):
40                 self.send(self.bot.room,
41                           self.inbox.get(nick).pop(),
42                           mtype='groupchat')
43             self.inbox.pop(nick)
44
45     @botcmd
46     def tell(self, message, args):
47         """drop a message to be sent when someone gets online.
48         !tell queue        : messages in queue
49         !tell <nick> <msg> : append <msg> to <nick> in queue"""
50         if not len(args):
51             msg = 'Missing arguments:\n{}'.format(self.tell.__doc__)
52             self.reply(message, msg)
53             return
54         if len(args) == 1:
55             if args[0] == 'queue':
56                 msg = '\n'.join(['{0}:\n\t{1}'.format(k, '\n'.join(v))
57                                 for k, v in self.inbox.items()])
58                 self.reply(message, msg)
59                 return
60             if args[0] == 'purge':
61                 sender = message['from'].resource
62                 if self.presence[sender][0] == 'moderator':
63                     self.online = set()
64                 return
65         if len(args) < 2:
66             msg = 'Please provide a message:\n{}'.format(self.tell.__doc__)
67             self.reply(message, msg)
68             return
69         self._handle_msg(message)
70
71     def _handle_msg(self, message):
72         sender = message['from'].resource
73         recipient = message['body'].split()[1]
74         tell_msg = ' '.join(message['body'].split()[2:])
75         self.log.debug('%s: %s', recipient, tell_msg)
76         letter = '{0}, {1} wanted you to know: {2}'.format(recipient, sender, tell_msg)
77         if (self.presence.get(recipient) and
78                 self.presence[recipient][1] == 'available'):
79             return
80         if recipient in self.inbox.keys():
81            self.inbox[recipient].append(letter)
82         else:
83            self.inbox[recipient] = [letter]
84
85
86 # VIM MODLINE
87 # vim: ai ts=4 sw=4 sts=4 expandtab