1###
2# Copyright (c) 2005, Daniel DiPaolo
3# All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions are met:
7#
8#   * Redistributions of source code must retain the above copyright notice,
9#     this list of conditions, and the following disclaimer.
10#   * Redistributions in binary form must reproduce the above copyright notice,
11#     this list of conditions, and the following disclaimer in the
12#     documentation and/or other materials provided with the distribution.
13#   * Neither the name of the author of this software nor the name of
14#     contributors to this software may be used to endorse or promote products
15#     derived from this software without specific prior written consent.
16#
17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20# ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27# POSSIBILITY OF SUCH DAMAGE.
28###
29
30import re
31
32from supybot.commands import *
33import supybot.plugins as plugins
34import supybot.ircutils as ircutils
35from supybot.i18n import PluginInternationalization, internationalizeDocstring
36_ = PluginInternationalization('Praise')
37
38class Praise(plugins.ChannelIdDatabasePlugin):
39    """Praise is a plugin for ... well, praising things.  Feel free to add
40    your own flavor to it by customizing what praises it gives.  Use "praise
41    add <text>" to add new ones, making sure to include "$who" in <text> where
42    you want to insert the thing being praised.
43    """
44    _meRe = re.compile(r'\bme\b', re.I)
45    _myRe = re.compile(r'\bmy\b', re.I)
46    def _replaceFirstPerson(self, s, nick):
47        s = self._meRe.sub(nick, s)
48        s = self._myRe.sub('%s\'s' % nick, s)
49        return s
50
51    def addValidator(self, irc, text):
52        if '$who' not in text:
53            irc.error(_('Praises must contain $who.'), Raise=True)
54
55    @internationalizeDocstring
56    def praise(self, irc, msg, args, channel, id, text):
57        """[<channel>] [<id>] <who|what> [for <reason>]
58
59        Praises <who|what> (for <reason>, if given).  If <id> is given, uses
60        that specific praise.  <channel> is only necessary if the message isn't
61        sent in the channel itself.
62        """
63        if ' for ' in text:
64            (target, reason) = list(map(str.strip, text.split(' for ', 1)))
65        else:
66            (target, reason) = (text, '')
67        if ircutils.strEqual(target, irc.nick):
68            target = 'itself'
69        if id is not None:
70            try:
71                praise = self.db.get(channel, id)
72            except KeyError:
73                irc.error(format(_('There is no praise with id #%i.'), id))
74                return
75        else:
76            praise = self.db.random(channel)
77            if not praise:
78                irc.error(format(_('There are no praises in my database ' \
79                                 'for %s.'), channel))
80                return
81        text = self._replaceFirstPerson(praise.text, msg.nick)
82        reason = self._replaceFirstPerson(reason, msg.nick)
83        target = self._replaceFirstPerson(target, msg.nick)
84        text = text.replace('$who', target)
85        if reason:
86            text += _(' for ') + reason
87        if self.registryValue('showIds', channel):
88            text += format(' (#%i)', praise.id)
89        irc.reply(text, action=True)
90    praise = wrap(praise, ['channeldb', optional('id'), 'text'])
91Praise = internationalizeDocstring(Praise)
92
93Class = Praise
94
95# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79:
96