Limnoria/plugins/News.py

241 lines
9.0 KiB
Python
Raw Normal View History

2003-04-09 21:16:52 +02:00
###
2003-09-14 09:38:26 +02:00
# Copyright (c) 2003, Daniel DiPaolo
2003-04-09 21:16:52 +02: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.
###
"""
2004-10-04 15:36:50 +02:00
A module to allow each channel to have "news". News items may have expiration
dates.
2003-04-09 21:16:52 +02:00
"""
2003-11-25 09:23:47 +01:00
__revision__ = "$Id$"
import os
import time
import supybot.dbi as dbi
2004-07-24 07:18:26 +02:00
import supybot.conf as conf
import supybot.ircdb as ircdb
import supybot.utils as utils
import supybot.plugins as plugins
2004-10-13 18:49:19 +02:00
from supybot.commands import additional, wrap
import supybot.ircutils as ircutils
2004-07-24 07:18:26 +02:00
import supybot.privmsgs as privmsgs
import supybot.callbacks as callbacks
2003-04-09 21:16:52 +02:00
class DbiNewsDB(plugins.DbiChannelDB):
class DB(dbi.DB):
class Record(dbi.Record):
__fields__ = [
'subject',
'text',
'at',
'expires',
'by',
]
def __str__(self):
format = conf.supybot.reply.format.time()
2004-12-02 06:33:29 +01:00
user = plugins.getUserName(self.by)
if self.expires == 0:
s = '%s (Subject: "%s", added by %s on %s)' % \
(self.text, self.subject, self.by,
2004-12-02 06:33:29 +01:00
time.strftime(format, time.localtime(self.at)))
else:
s = '%s (Subject: "%s", added by %s on %s, expires at %s)'
s = s % (self.text, self.subject, user,
2004-12-02 06:33:29 +01:00
time.strftime(format, time.localtime(self.at)),
time.strftime(format, time.localtime(self.expires)))
return s
def __init__(self, filename):
# We use self.__class__ here because apparently DB isn't in our
# scope. python--
self.__parent = super(self.__class__, self)
self.__parent.__init__(filename)
def add(self, subject, text, at, expires, by):
return self.__parent.add(self.Record(at=at, by=by, text=text,
subject=subject, expires=expires))
def getOld(self, id=None):
now = time.time()
if id:
return self.get(id)
else:
L = [R for R in self if R.expires < now and R.expires != 0]
if not L:
raise dbi.NoRecordError
else:
return L
def get(self, id=None):
now = time.time()
if id:
return self.__parent.get(id)
else:
L = [R for R in self if R.expires >= now or R.expires == 0]
if not L:
raise dbi.NoRecordError
return L
def change(self, id, f):
news = self.get(id)
s = '%s: %s' % (news.subject, news.text)
s = f(s)
(news.subject, news.text) = s.split(': ', 1)
self.set(id, news)
NewsDB = plugins.DB('News', {'flat': DbiNewsDB})
class News(callbacks.Privmsg):
def __init__(self):
2004-09-20 01:51:21 +02:00
self.__parent = super(News, self)
self.__parent.__init__()
self.db = NewsDB()
def die(self):
2004-09-20 01:51:21 +02:00
self.__parent.die()
self.db.close()
def add(self, irc, msg, args, channel, user, at, expires, news):
2003-09-14 09:38:26 +02:00
"""[<channel>] <expires> <subject>: <text>
2003-04-16 01:29:40 +02:00
2003-09-14 09:38:26 +02:00
Adds a given news item of <text> to a channel with the given <subject>.
If <expires> isn't 0, that news item will expire <expires> seconds from
now. <channel> is only necessary if the message isn't sent in the
channel itself.
2003-04-16 01:29:40 +02:00
"""
try:
(subject, text) = news.split(': ', 1)
except ValueError:
2003-09-14 09:38:26 +02:00
raise callbacks.ArgumentError
id = self.db.add(channel, subject, text, at, expires, user.id)
irc.replySuccess('(News item #%s added)' % id)
add = wrap(add, ['channeldb', 'user', 'now', 'expiry', 'text'])
2003-09-14 09:38:26 +02:00
def news(self, irc, msg, args, channel, id):
"""[<channel>] [<id>]
2003-09-14 09:38:26 +02:00
2003-10-05 22:33:01 +02:00
Display the news items for <channel> in the format of '(#id) subject'.
If <id> is given, retrieve only that news item; otherwise retrieve all
news items. <channel> is only necessary if the message isn't sent in
the channel itself.
2003-09-14 09:38:26 +02:00
"""
if not id:
try:
records = self.db.get(channel)
items = ['(#%s) %s' % (R.id, R.subject) for R in records]
s = 'News for %s: %s' % (channel, '; '.join(items))
irc.reply(s)
except dbi.NoRecordError:
irc.reply('No news for %s.' % channel)
else:
try:
if id < 1:
raise ValueError
record = self.db.get(channel, id)
irc.reply(str(record))
except dbi.NoRecordError, id:
irc.errorInvalid('news item id', id)
except ValueError:
irc.errorInvalid('news item id', id,
'<id> must be a positive integer.')
2004-10-13 18:49:19 +02:00
news = wrap(news, ['channeldb', additional('int')])
2003-04-16 01:29:40 +02:00
def remove(self, irc, msg, args, channel, id):
"""[<channel>] <id>
2003-04-16 01:29:40 +02:00
Removes the news item with <id> from <channel>. <channel> is only
necessary if the message isn't sent in the channel itself.
2003-04-16 01:29:40 +02:00
"""
try:
if id < 1:
raise ValueError
self.db.remove(channel, id)
irc.replySuccess()
except dbi.NoRecordError:
irc.errorInvalid('news item id', id)
except ValueError:
irc.errorInvalid('news item id', id,
'<id> must be a positive integer.')
remove = wrap(remove, ['channeldb', 'int'])
2004-07-21 21:36:35 +02:00
def change(self, irc, msg, args, channel, id, replacer):
"""[<channel>] <id> <regexp>
2003-04-16 01:29:40 +02:00
Changes the news item with <id> from <channel> according to the
2003-04-16 01:29:40 +02:00
regular expression <regexp>. <regexp> should be of the form
s/text/replacement/flags. <channel> is only necessary if the message
isn't sent on the channel itself.
"""
try:
if id < 1:
raise ValueError
self.db.change(channel, id, replacer)
irc.replySuccess()
except dbi.NoRecordError:
irc.errorInvalid('news item id', id)
except ValueError:
irc.errorInvalid('news item id', id,
'<id> must be a positive integer.')
change = wrap(change, ['channeldb', 'int', 'regexpReplacer'])
2003-04-16 01:29:40 +02:00
def old(self, irc, msg, args, channel, id):
"""[<channel>] [<id>]
2003-04-16 01:29:40 +02:00
Returns the old news item for <channel> with <id>. If no number is
given, returns all the old news items in reverse order. <channel> is
only necessary if the message isn't sent in the channel itself.
2003-04-16 01:29:40 +02:00
"""
2003-10-15 07:25:26 +02:00
if id:
try:
if id < 1:
raise ValueError
record = self.db.getOld(channel, id)
irc.reply(str(record))
except dbi.NoRecordError, id:
irc.errorInvalid('news item id', id)
except ValueError:
irc.errorInvalid('news item id', id,
'<id> must be a positive integer.')
2003-10-15 07:25:26 +02:00
else:
try:
records = self.db.getOld(channel)
items = ['(#%s) %s' % (R.id, R.subject) for R in records]
s = 'Old news for %s: %s' % (channel, '; '.join(items))
irc.reply(s)
except dbi.NoRecordError:
irc.reply('No old news for %s.' % channel)
2004-10-13 18:49:19 +02:00
old = wrap(old, ['channeldb', additional('int')])
2004-07-21 21:36:35 +02:00
2003-10-19 23:10:56 +02:00
Class = News
2003-04-09 21:16:52 +02:00
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: