Limnoria/plugins/Herald.py

241 lines
9.8 KiB
Python
Raw Normal View History

2004-01-01 20:45:15 +01:00
###
# Copyright (c) 2002-2004, Jeremiah Fincher
2004-01-01 20:45:15 +01:00
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions, and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions, and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the author of this software nor the name of
# contributors to this software may be used to endorse or promote products
# derived from this software without specific prior written consent.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
###
"""
Greets users who join the channel with a recognized hostmask with a nice
2004-04-01 13:47:54 +02:00
little greeting.
2004-01-01 20:45:15 +01:00
"""
2004-03-02 01:20:32 +01:00
__revision__ = "$Id$"
2004-01-01 20:45:15 +01:00
import os
import time
import getopt
2004-01-01 20:45:15 +01:00
2004-07-24 07:18:26 +02:00
import supybot.log as log
import supybot.conf as conf
import supybot.utils as utils
import supybot.world as world
import supybot.ircdb as ircdb
2004-10-19 18:48:52 +02:00
from supybot.commands import *
2004-07-24 07:18:26 +02:00
import supybot.ircmsgs as ircmsgs
import supybot.plugins as plugins
import supybot.ircutils as ircutils
import supybot.registry as registry
import supybot.callbacks as callbacks
2004-01-01 20:45:15 +01:00
2004-02-07 22:23:00 +01:00
filename = os.path.join(conf.supybot.directories.data(), 'Herald.db')
2004-01-01 20:45:15 +01:00
class HeraldDB(plugins.ChannelUserDB):
2004-02-07 22:23:00 +01:00
def serialize(self, v):
return [v]
2004-01-01 20:45:15 +01:00
def deserialize(self, channel, id, L):
2004-02-07 22:23:00 +01:00
if len(L) != 1:
raise ValueError
return L[0]
2004-01-01 20:45:15 +01:00
2004-01-27 16:20:21 +01:00
conf.registerPlugin('Herald')
conf.registerChannelValue(conf.supybot.plugins.Herald, 'heralding',
registry.Boolean(True, """Determines whether messages will be sent to the
channel when a recognized user joins; basically enables or disables the
plugin."""))
conf.registerChannelValue(conf.supybot.plugins.Herald, 'throttleTime',
registry.PositiveInteger(600, """Determines the minimum number of seconds
between heralds."""))
conf.registerChannelValue(conf.supybot.plugins.Herald, 'default',
registry.String('', """Sets the default herald to use. If a user has a
personal herald specified, that will be used instead. If set to the empty
string, the default herald will be disabled."""))
2004-08-22 22:45:05 +02:00
conf.registerChannelValue(conf.supybot.plugins.Herald.default, 'notice',
registry.Boolean(True, """Determines whether the default herald will be
sent as a NOTICE instead of a PRIVMSG."""))
conf.registerChannelValue(conf.supybot.plugins.Herald.default, 'public',
registry.Boolean(False, """Determines whether the default herald will be
sent publicly."""))
2004-01-27 16:20:21 +01:00
conf.registerChannelValue(conf.supybot.plugins.Herald, 'throttleTimeAfterPart',
registry.PositiveInteger(60, """Determines the minimum number of seconds
after parting that the bot will not herald the person when he or she
rejoins."""))
class Herald(callbacks.Privmsg):
2004-01-01 20:45:15 +01:00
def __init__(self):
2004-09-20 01:51:21 +02:00
self.__parent = super(Herald, self)
self.__parent.__init__()
2004-02-07 22:23:00 +01:00
self.db = HeraldDB(filename)
world.flushers.append(self.db.flush)
self.lastParts = plugins.ChannelUserDictionary()
self.lastHerald = plugins.ChannelUserDictionary()
2004-01-01 20:45:15 +01:00
def die(self):
2004-02-07 22:23:00 +01:00
if self.db.flush in world.flushers:
world.flushers.remove(self.db.flush)
2004-01-01 20:45:15 +01:00
self.db.close()
2004-09-20 01:51:21 +02:00
self.__parent.die()
2004-01-01 20:45:15 +01:00
def doJoin(self, irc, msg):
channel = msg.args[0]
2004-09-01 23:32:43 +02:00
irc = callbacks.SimpleProxy(irc, msg)
2004-01-27 16:20:21 +01:00
if self.registryValue('heralding', channel):
2004-01-01 20:45:15 +01:00
try:
id = ircdb.users.getUserId(msg.prefix)
2004-02-07 22:23:00 +01:00
herald = self.db[channel, id]
2004-01-01 20:45:15 +01:00
except KeyError:
default = self.registryValue('default', channel)
if default:
default = plugins.standardSubstitute(irc, msg, default)
2004-08-22 22:45:05 +02:00
msgmaker = ircmsgs.privmsg
if self.registryValue('default.notice', channel):
msgmaker = ircmsgs.notice
target = msg.nick
if self.registryValue('default.public', channel):
target = channel
2004-08-22 22:45:05 +02:00
irc.queueMsg(msgmaker(target, default))
2004-01-01 20:45:15 +01:00
return
now = time.time()
2004-01-27 16:20:21 +01:00
throttle = self.registryValue('throttleTime', channel)
2004-02-07 22:23:00 +01:00
if now - self.lastHerald.get((channel, id), 0) > throttle:
if (channel, id) in self.lastParts:
2004-07-21 21:36:35 +02:00
i = self.registryValue('throttleTimeAfterPart', channel)
2004-02-07 22:23:00 +01:00
if now - self.lastParts[channel, id] < i:
2004-01-01 20:45:15 +01:00
return
2004-02-07 22:23:00 +01:00
self.lastHerald[channel, id] = now
herald = plugins.standardSubstitute(irc, msg, herald)
irc.reply(herald, prefixName=False)
2004-01-01 20:45:15 +01:00
def doPart(self, irc, msg):
2004-01-04 14:02:24 +01:00
try:
2004-01-18 05:55:18 +01:00
id = self._getId(irc, msg.prefix)
self.lastParts[msg.args[0], id] = time.time()
2004-01-04 14:02:24 +01:00
except KeyError:
pass
2004-01-01 20:45:15 +01:00
2004-01-18 05:55:18 +01:00
def _getId(self, irc, userNickHostmask):
2004-01-01 20:45:15 +01:00
try:
id = ircdb.users.getUserId(userNickHostmask)
except KeyError:
if not ircutils.isUserHostmask(userNickHostmask):
hostmask = irc.state.nickToHostmask(userNickHostmask)
id = ircdb.users.getUserId(hostmask)
else:
raise KeyError
2004-01-04 13:59:10 +01:00
return id
2004-01-01 20:45:15 +01:00
2004-10-19 18:48:52 +02:00
def default(self, irc, msg, args, channel, optlist, text):
"""[<channel>] [--remove|<msg>]
If <msg> is given, sets the default herald to <msg>. A <msg> of ""
will remove the default herald. If <msg> is not given, returns the
current default herald. <channel> is only necessary if the message
isn't sent in the channel itself.
"""
2004-10-19 18:48:52 +02:00
if optlist and text:
raise callbacks.ArgumentError
for (option, _) in optlist:
2004-10-19 18:48:52 +02:00
if option == 'remove':
self.setRegistryValue('default', '', channel)
irc.replySuccess()
return
2004-10-19 18:48:52 +02:00
if text:
self.setRegistryValue('default', text, channel)
irc.replySuccess()
else:
resp = self.registryValue('default', channel) or \
'I do not have a default herald set for %s.' % channel
irc.reply(resp)
default = wrap(default, ['channel',
getopts({'remove': ''}),
additional('text')])
2004-10-19 18:48:52 +02:00
def get(self, irc, msg, args, channel, user):
"""[<channel>] [<user|nick|hostmask>]
2004-02-13 07:41:59 +01:00
Returns the current herald message for <user> (or the user
2004-10-19 18:48:52 +02:00
<nick|hostmask> is currently identified or recognized as). If <user>
is not given, defaults to the user giving the command. <channel>
2004-02-13 07:41:59 +01:00
is only necessary if the message isn't sent in the channel itself.
"""
try:
2004-10-19 18:48:52 +02:00
herald = self.db[channel, user.id]
2004-02-13 07:41:59 +01:00
irc.reply(herald)
except KeyError:
2004-10-19 18:48:52 +02:00
irc.error('I have no herald for %s.' % user.name)
get = wrap(get, ['channel', first('otherUser', 'user')])
2004-02-13 07:41:59 +01:00
2004-10-19 18:48:52 +02:00
# I chose not to make <user|nick|hostmask> optional in this command because
# if it's not a valid username (e.g., if the user tyops and misspells a
# username), it may be nice not to clobber the user's herald.
def add(self, irc, msg, args, channel, user, herald):
2004-01-01 20:45:15 +01:00
"""[<channel>] <user|nick|hostmask> <msg>
Sets the herald message for <user> (or the user <nick|hostmask> is
currently identified or recognized as) to <msg>. <channel> is only
necessary if the message isn't sent in the channel itself.
"""
2004-10-19 18:48:52 +02:00
self.db[channel, user.id] = herald
irc.replySuccess()
2004-10-19 18:48:52 +02:00
add = wrap(add, ['channel', 'otherUser', 'text'])
2004-01-01 20:45:15 +01:00
2004-10-19 18:48:52 +02:00
def remove(self, irc, msg, args, channel, user):
"""[<channel>] [<user|nick|hostmask>]
2004-01-01 20:45:15 +01:00
Removes the herald message set for <user>, or the user
2004-10-19 18:48:52 +02:00
<nick|hostmask> is currently identified or recognized as. If <user>
is not given, defaults to the user giving the command.
2004-01-01 20:45:15 +01:00
<channel> is only necessary if the message isn't sent in the channel
itself.
"""
try:
2004-10-19 18:48:52 +02:00
del self.db[channel, user.id]
irc.replySuccess()
except KeyError:
irc.error('I have no herald for that user.')
2004-10-19 18:48:52 +02:00
remove = wrap(remove, ['channel', first('otherUser', 'user')])
2004-01-01 20:45:15 +01:00
2004-10-19 18:48:52 +02:00
def change(self, irc, msg, args, channel, user, changer):
"""[<channel>] [<user|nick|hostmask>] <regexp>
2004-08-15 20:38:07 +02:00
Changes the herald message for <user>, or the user <nick|hostmask> is
2004-10-19 18:48:52 +02:00
currently identified or recognized as, according to <regexp>. If
<user> is not given, defaults to the calling user. <channel> is only
necessary if the message isn't sent in the channel itself.
2004-08-15 20:38:07 +02:00
"""
2004-10-19 18:48:52 +02:00
s = self.db[channel, user.id]
2004-08-15 20:38:07 +02:00
newS = changer(s)
2004-10-19 18:48:52 +02:00
self.db[channel, user.id] = newS
2004-08-15 20:38:07 +02:00
irc.replySuccess()
2004-10-19 18:48:52 +02:00
change = wrap(change, ['channel',
first('otherUser', 'user'),
'regexpReplacer'])
2004-08-15 20:38:07 +02:00
2004-01-01 20:45:15 +01:00
Class = Herald
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: