Limnoria/plugins/Herald.py

193 lines
6.7 KiB
Python
Raw Normal View History

2004-01-20 08:15:37 +01:00
#!/usr/bin/env python
2004-01-01 20:45:15 +01:00
###
# Copyright (c) 2002, Jeremiah Fincher
# 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
little greeting. Otherwise, can greet
"""
import plugins
import os
import time
import log
import conf
import utils
import ircdb
import ircmsgs
import ircutils
import privmsgs
import registry
2004-01-01 20:45:15 +01:00
import callbacks
class HeraldDB(object):
def __init__(self):
self.heralds = {}
2004-01-18 08:58:26 +01:00
dataDir = conf.supybot.directories.data()
self.filename = os.path.join(dataDir, 'Herald.db')
self.open()
2004-01-01 20:45:15 +01:00
def open(self):
2004-01-18 08:58:26 +01:00
dataDir = conf.supybot.directories.data()
if os.path.exists(self.filename):
fd = file(self.filename)
2004-01-04 13:59:10 +01:00
for line in fd:
line = line.rstrip()
try:
(idChannel, msg) = line.split(':', 1)
(id, channel) = idChannel.split(',', 1)
id = int(id)
except ValueError:
log.warning('Invalid line in HeraldDB: %r', line)
continue
self.heralds[(id, channel)] = msg
fd.close()
2004-01-01 20:45:15 +01:00
def close(self):
2004-01-18 08:58:26 +01:00
fd = file(self.filename, 'w')
2004-01-01 20:45:15 +01:00
L = self.heralds.items()
L.sort()
2004-01-04 13:59:10 +01:00
for ((id, channel), msg) in L:
fd.write('%s,%s:%s%s' % (id, channel, msg, os.linesep))
2004-01-01 20:45:15 +01:00
fd.close()
2004-01-04 13:59:10 +01:00
def getHerald(self, id, channel):
2004-01-01 20:45:15 +01:00
return self.heralds[(id, channel)]
2004-01-04 13:59:10 +01:00
def setHerald(self, id, channel, msg):
2004-01-01 20:45:15 +01:00
self.heralds[(id, channel)] = msg
2004-01-04 13:59:10 +01:00
def delHerald(self, id, channel):
2004-01-01 20:45:15 +01:00
del self.heralds[(id, channel)]
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, '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-01-04 13:59:10 +01:00
callbacks.Privmsg.__init__(self)
2004-01-01 20:45:15 +01:00
self.db = HeraldDB()
self.lastParts = {}
self.lastHerald = {}
def die(self):
self.db.close()
2004-01-04 13:59:10 +01:00
callbacks.Privmsg.die(self)
2004-01-01 20:45:15 +01:00
def doJoin(self, irc, msg):
channel = msg.args[0]
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)
herald = self.db.getHerald(id, channel)
except KeyError:
return
now = time.time()
2004-01-27 16:20:21 +01:00
throttle = self.registryValue('throttleTime', channel)
2004-01-04 13:59:10 +01:00
if now - self.lastHerald.get((id, channel), 0) > throttle:
2004-01-01 20:45:15 +01:00
if (id, channel) in self.lastParts:
2004-01-27 16:20:21 +01:00
i = self.registryValue('throttleTimeAfterPart', channel)
2004-01-01 20:45:15 +01:00
if now - self.lastParts[(id, channel)] < i:
return
2004-01-04 14:02:24 +01:00
self.lastHerald[(id, channel)] = now
2004-01-01 20:45:15 +01:00
irc.queueMsg(ircmsgs.privmsg(channel, herald))
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)
2004-01-04 14:02:24 +01:00
self.lastParts[(id, msg.args[0])] = time.time()
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
def add(self, irc, msg, args):
"""[<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.
"""
channel = privmsgs.getChannel(msg, args)
(userNickHostmask, herald) = privmsgs.getArgs(args, required=2)
try:
2004-01-18 05:55:18 +01:00
id = self._getId(irc, userNickHostmask)
2004-01-01 20:45:15 +01:00
except KeyError:
2004-01-08 16:47:38 +01:00
irc.errorNoUser()
2004-01-01 20:45:15 +01:00
return
self.db.setHerald(id, channel, herald)
irc.replySuccess()
2004-01-01 20:45:15 +01:00
def remove(self, irc, msg, args):
"""[<channel>] <user|nick|hostmask>
Removes the herald message set for <user>, or the user
<nick|hostmask> is currently identified or recognized as.
<channel> is only necessary if the message isn't sent in the channel
itself.
"""
channel = privmsgs.getChannel(msg, args)
userNickHostmask = privmsgs.getArgs(args)
try:
2004-01-18 05:55:18 +01:00
id = self._getId(irc, userNickHostmask)
2004-01-01 20:45:15 +01:00
except KeyError:
2004-01-08 16:47:38 +01:00
irc.errorNoUser()
2004-01-01 20:45:15 +01:00
return
self.db.delHerald(id, channel)
irc.replySuccess()
2004-01-01 20:45:15 +01:00
Class = Herald
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: