X-Git-Url: http://git.kaliko.me/?a=blobdiff_plain;f=sid%2Fecho.py;h=5bb90c06768e7e5daaf9e99b6721ef8d3a352784;hb=dc4ac4f37ac6b92014ec41f6233ada96c8484adb;hp=eae5b56d240c04dc553a2325ccba32c7a3e22638;hpb=8050b8698ff1f6294abceb8b022a4aecdbe8375e;p=sid.git diff --git a/sid/echo.py b/sid/echo.py index eae5b56..5bb90c0 100644 --- a/sid/echo.py +++ b/sid/echo.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # Copyright (C) 2007-2012 Thomas Perl -# Copyright (C) 2014 kaliko +# Copyright (C) 2014, 2020 kaliko # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -17,66 +17,79 @@ from .plugin import Plugin, botcmd + class Echo(Plugin): + """Drops a message to be sent when someone gets online. + """ def __init__(self, bot): Plugin.__init__(self, bot) - bot.add_event_handler("groupchat_message", self.muc_message) + 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_online" % self.bot.room, self.muc_online) - - def muc_message(self, msg): - """ - Process incoming message stanzas from any chat room. Be aware - that if you also have any handlers for the 'message' event, - message stanzas may be processed by both handlers, so check - the 'type' attribute when using a 'message' event handler. - - Whenever the bot's nickname is mentioned, respond to - the message. - - IMPORTANT: Always check that a message is not from yourself, - otherwise you will create an infinite loop responding - to your own messages. - - This handler will reply to messages that mention - the bot's nickname. - - Arguments: - msg -- The received message stanza. See the documentation - for stanza objects and the Message stanza to see - how it may be used. - """ - if msg['mucnick'] != self.bot.nick and self.bot.nick in msg['body']: - self.bot.send_message(mto=msg['from'].bare, - mbody="I heard that, %s." % msg['mucnick'], - mtype='groupchat') - - def muc_online(self, presence): - """ - Process a presence stanza from a chat room. In this case, - presences from users that have just come online are - handled by sending a welcome message that includes - the user's nickname and role in the room. - - Arguments: - presence -- The received presence stanza. See the - documentation for the Presence stanza - to see how else it may be used. - """ - if presence['muc']['nick'] != self.bot.nick: - self.bot.send_message(mto=presence['from'].bare, - mbody="Hello, %s %s" % (presence['muc']['role'], - presence['muc']['nick']), - mtype='groupchat') + bot.add_event_handler("muc::%s::presence" % + self.bot.room, self.log_presence) + + def log_presence(self, pres): + """Handler method registering MUC participants presence""" + self.log.debug('%s: %s', pres['muc']['nick'], pres['type']) + nick = pres['muc']['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.bot.room, + self.inbox.get(nick).pop(), + mtype='groupchat') + self.inbox.pop(nick) @botcmd def tell(self, message, args): - pass + """ + **commands**: + + * ``!tell queue`` : messages in queue + * ``!tell `` : append to in queue""" + if not len(args): + msg = 'Missing arguments:\n{}'.format(self.tell.__doc__) + self.reply(message, msg) + return + if len(args) == 1: + if args[0] == 'queue': + msg = '\n'.join(['{0}:\n\t{1}'.format(k, '\n'.join(v)) + for k, v in self.inbox.items()]) + self.reply(message, msg) + return + if args[0] == 'purge': + sender = message['from'].resource + if self.presence[sender][0] == 'moderator': + self.inbox = dict() + return + if len(args) < 2: + msg = 'Please provide a message:\n{}'.format(self.tell.__doc__) + self.reply(message, msg) + return + self._handle_msg(message) + + def _handle_msg(self, message): + """Format and drop message in the inbox""" + sender = message['from'].resource + recipient = message['body'].split()[1] + tell_msg = ' '.join(message['body'].split()[2:]) + self.log.debug('%s: %s', recipient, tell_msg) + letter = '{0}, {1} wanted you to know: {2}'.format(recipient, sender, tell_msg) + if (self.presence.get(recipient) and + self.presence[recipient][1] == 'available'): + return + if recipient in self.inbox.keys(): + self.inbox[recipient].append(letter) + else: + self.inbox[recipient] = [letter] + # VIM MODLINE # vim: ai ts=4 sw=4 sts=4 expandtab