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