]> kaliko git repositories - sid.git/blob - sid/echo.py
90ccd900dfc42ff9a2956d21b6220cd547a1a513
[sid.git] / sid / echo.py
1 # -*- coding: utf-8 -*-
2
3 # Copyright (C) 2007-2012 Thomas Perl <thp.io/about>
4 # Copyright (C) 2014, 2020 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
21 class Echo(Plugin):
22     """Drop a message to be sent when someone gets online.
23     """
24
25     def __init__(self, bot):
26         Plugin.__init__(self, bot)
27         self.inbox = dict()
28         self.presence = dict()
29         # The groupchat_presence event is triggered whenever a
30         # presence stanza is received from any chat room, including
31         # any presences you send yourself. To limit event handling
32         # to a single room, use the events muc::room@server::presence,
33         # muc::room@server::got_online, or muc::room@server::got_offline.
34         bot.add_event_handler("muc::%s::presence" %
35                               self.bot.room, self.log_presence)
36
37     def log_presence(self, pres):
38         """Register presence"""
39         self.log.debug('%s: %s', pres['muc']['nick'], pres['type'])
40         nick = pres['muc']['nick']
41         self.presence.update({nick: (pres['muc']['role'], pres['type'])})
42         self.log.debug(self.presence)
43         if pres['type'] == 'available':
44             while self.inbox.get(nick, []):
45                 self.send(self.bot.room,
46                           self.inbox.get(nick).pop(),
47                           mtype='groupchat')
48                 self.inbox.pop(nick)
49
50     @botcmd
51     def tell(self, message, args):
52         """drop a message to be sent when someone gets online.
53         !tell queue        : messages in queue
54         !tell <nick> <msg> : append <msg> to <nick> in queue"""
55         if not len(args):
56             msg = 'Missing arguments:\n{}'.format(self.tell.__doc__)
57             self.reply(message, msg)
58             return
59         if len(args) == 1:
60             if args[0] == 'queue':
61                 msg = '\n'.join(['{0}:\n\t{1}'.format(k, '\n'.join(v))
62                                  for k, v in self.inbox.items()])
63                 self.reply(message, msg)
64                 return
65             if args[0] == 'purge':
66                 sender = message['from'].resource
67                 if self.presence[sender][0] == 'moderator':
68                     self.inbox = dict()
69                 return
70         if len(args) < 2:
71             msg = 'Please provide a message:\n{}'.format(self.tell.__doc__)
72             self.reply(message, msg)
73             return
74         self._handle_msg(message)
75
76     def _handle_msg(self, message):
77         """Format and drop message in the inbox"""
78         sender = message['from'].resource
79         recipient = message['body'].split()[1]
80         tell_msg = ' '.join(message['body'].split()[2:])
81         self.log.debug('%s: %s', recipient, tell_msg)
82         letter = '{0}, {1} wanted you to know: {2}'.format(recipient, sender, tell_msg)
83         if (self.presence.get(recipient) and
84                 self.presence[recipient][1] == 'available'):
85             return
86         if recipient in self.inbox.keys():
87             self.inbox[recipient].append(letter)
88         else:
89             self.inbox[recipient] = [letter]
90
91
92 # VIM MODLINE
93 # vim: ai ts=4 sw=4 sts=4 expandtab