]> kaliko git repositories - sid.git/blob - sid/echo.py
a9cc400ec730593941f670f98e1c14c108e8ffc9
[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         bot.add_event_handler('groupchat_message', self.muc_message)
25         # The groupchat_presence event is triggered whenever a
26         # presence stanza is received from any chat room, including
27         # any presences you send yourself. To limit event handling
28         # to a single room, use the events muc::room@server::presence,
29         # muc::room@server::got_online, or muc::room@server::got_offline.
30         #bot.add_event_handler("muc::%s::got_online" % self.bot.room, self.muc_online)
31
32     def muc_message(self, msg):
33         """
34         Process incoming message stanzas from any chat room. Be aware
35         that if you also have any handlers for the 'message' event,
36         message stanzas may be processed by both handlers, so check
37         the 'type' attribute when using a 'message' event handler.
38
39         Whenever the bot's nickname is mentioned, respond to
40         the message.
41
42         IMPORTANT: Always check that a message is not from yourself,
43                    otherwise you will create an infinite loop responding
44                    to your own messages.
45
46         This handler will reply to messages that mention
47         the bot's nickname.
48
49         Arguments:
50             msg -- The received message stanza. See the documentation
51                    for stanza objects and the Message stanza to see
52                    how it may be used.
53         """
54         if msg['mucnick'] != self.bot.nick and self.bot.nick in msg['body']:
55             self.bot.send_message(mto=msg['from'].bare,
56                               mbody='I heard that, %s.' % msg['mucnick'],
57                               mtype='groupchat')
58
59     def muc_online(self, presence):
60         """
61         Process a presence stanza from a chat room. In this case,
62         presences from users that have just come online are
63         handled by sending a welcome message that includes
64         the user's nickname and role in the room.
65
66         Arguments:
67             presence -- The received presence stanza. See the
68                         documentation for the Presence stanza
69                         to see how else it may be used.
70         """
71         if presence['muc']['nick'] != self.bot.nick:
72             self.bot.send_message(mto=presence['from'].bare,
73                               mbody='Hello, %s %s' % (presence['muc']['role'],
74                                                       presence['muc']['nick']),
75                               mtype='groupchat')
76
77     @botcmd
78     def tell(self, message, args):
79         pass
80
81 # VIM MODLINE
82 # vim: ai ts=4 sw=4 sts=4 expandtab