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$"
|
|
|
|
|
2004-07-24 07:18:26 +02:00
|
|
|
import supybot.plugins as plugins
|
2003-04-09 21:16:52 +02:00
|
|
|
|
2003-10-04 15:53:13 +02:00
|
|
|
import os
|
|
|
|
import time
|
|
|
|
|
2004-09-10 21:37:49 +02:00
|
|
|
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
|
2004-09-10 21:37:49 +02:00
|
|
|
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
|
|
|
|
2003-12-04 00:48:00 +01:00
|
|
|
|
2004-09-24 22:05:34 +02:00
|
|
|
class NewsRecord(dbi.Record):
|
2004-09-10 21:37:49 +02:00
|
|
|
__fields__ = [
|
|
|
|
'subject',
|
|
|
|
'text',
|
|
|
|
'at',
|
|
|
|
'expires',
|
|
|
|
'by'
|
|
|
|
]
|
|
|
|
def __str__(self):
|
|
|
|
format = conf.supybot.humanTimestampFormat()
|
|
|
|
try:
|
|
|
|
user = ircdb.users.getUser(int(self.by)).name
|
|
|
|
except ValueError:
|
|
|
|
user = self.by
|
|
|
|
except KeyError:
|
|
|
|
user = 'a user that is no longer registered'
|
|
|
|
if int(self.expires) == 0:
|
|
|
|
s = '%s (Subject: "%s", added by %s on %s)' % \
|
|
|
|
(self.text, self.subject, self.by,
|
|
|
|
time.strftime(format, time.localtime(int(self.at))))
|
|
|
|
else:
|
|
|
|
s = '%s (Subject: "%s", added by %s on %s, expires at %s)' % \
|
|
|
|
(self.text, self.subject, self.by,
|
|
|
|
time.strftime(format, time.localtime(int(self.at))),
|
|
|
|
time.strftime(format, time.localtime(int(self.expires))))
|
|
|
|
return s
|
|
|
|
|
|
|
|
class SqliteNewsDB(object):
|
2004-09-17 05:01:21 +02:00
|
|
|
def __init__(self, filename):
|
2004-09-10 21:37:49 +02:00
|
|
|
self.dbs = ircutils.IrcDict()
|
2004-09-17 05:01:21 +02:00
|
|
|
self.filename = filename
|
2004-09-10 21:37:49 +02:00
|
|
|
|
|
|
|
def close(self):
|
|
|
|
for db in self.dbs.itervalues():
|
|
|
|
db.close()
|
2003-04-09 21:16:52 +02:00
|
|
|
|
2004-09-10 21:37:49 +02:00
|
|
|
def _getDb(self, channel):
|
|
|
|
try:
|
|
|
|
import sqlite
|
|
|
|
except ImportError:
|
|
|
|
raise callbacks.Error, 'You need to have PySQLite installed to ' \
|
|
|
|
'use this plugin. Download it at ' \
|
|
|
|
'<http://pysqlite.sf.net/>'
|
2004-09-17 05:01:21 +02:00
|
|
|
filename = plugins.makeChannelFilename(self.filename, channel)
|
2004-09-10 21:37:49 +02:00
|
|
|
if filename in self.dbs:
|
|
|
|
return self.dbs[filename]
|
2003-04-09 21:16:52 +02:00
|
|
|
if os.path.exists(filename):
|
2004-09-10 21:37:49 +02:00
|
|
|
self.dbs[filename] = sqlite.connect(filename)
|
|
|
|
return self.dbs[filename]
|
2003-04-09 21:16:52 +02:00
|
|
|
db = sqlite.connect(filename)
|
2004-09-10 21:37:49 +02:00
|
|
|
self.dbs[filename] = db
|
2003-04-09 21:16:52 +02:00
|
|
|
cursor = db.cursor()
|
|
|
|
cursor.execute("""CREATE TABLE news (
|
|
|
|
id INTEGER PRIMARY KEY,
|
2003-09-14 09:38:26 +02:00
|
|
|
subject TEXT,
|
2003-04-09 21:16:52 +02:00
|
|
|
item TEXT,
|
|
|
|
added_at TIMESTAMP,
|
2003-04-16 01:29:40 +02:00
|
|
|
expires_at TIMESTAMP,
|
2003-04-09 21:16:52 +02:00
|
|
|
added_by TEXT
|
|
|
|
)""")
|
|
|
|
db.commit()
|
2003-08-19 12:38:45 +02:00
|
|
|
return db
|
2003-04-09 21:16:52 +02:00
|
|
|
|
2004-09-10 21:37:49 +02:00
|
|
|
def add(self, channel, subject, text, added_at, expires, by):
|
|
|
|
db = self._getDb(channel)
|
|
|
|
cursor = db.cursor()
|
|
|
|
cursor.execute("INSERT INTO news VALUES (NULL, %s, %s, %s, %s, %s)",
|
|
|
|
subject, text, added_at, expires, by)
|
|
|
|
db.commit()
|
|
|
|
|
2004-09-24 22:05:34 +02:00
|
|
|
# XXX This should change only to support id, and the old functionality
|
|
|
|
# should move out to another method.
|
2004-09-10 21:37:49 +02:00
|
|
|
def get(self, channel, id=None, old=False):
|
|
|
|
db = self._getDb(channel)
|
|
|
|
cursor = db.cursor()
|
|
|
|
if id:
|
|
|
|
cursor.execute("""SELECT item, subject, added_at,
|
|
|
|
expires_at, added_by
|
|
|
|
FROM news
|
|
|
|
WHERE id=%s""", id)
|
|
|
|
if cursor.rowcount == 0:
|
|
|
|
raise dbi.NoRecordError, id
|
|
|
|
(text, subject, at, expires, by) = cursor.fetchone()
|
|
|
|
return NewsRecord(id, text=text, subject=subject, at=int(at),
|
|
|
|
expires=int(expires), by=by)
|
|
|
|
else:
|
|
|
|
if old:
|
|
|
|
cursor.execute("""SELECT id, subject
|
|
|
|
FROM news
|
|
|
|
WHERE expires_at <> 0 AND expires_at < %s
|
|
|
|
ORDER BY id DESC""", int(time.time()))
|
|
|
|
else:
|
|
|
|
cursor.execute("""SELECT id, subject
|
|
|
|
FROM news
|
|
|
|
WHERE expires_at > %s OR expires_at=0""",
|
|
|
|
int(time.time()))
|
|
|
|
if cursor.rowcount == 0:
|
|
|
|
raise dbi.NoRecordError
|
|
|
|
else:
|
|
|
|
return cursor.fetchall()
|
|
|
|
|
|
|
|
def remove(self, channel, id):
|
|
|
|
db = self._getDb(channel)
|
|
|
|
cursor = db.cursor()
|
|
|
|
cursor.execute("""DELETE FROM news WHERE id = %s""", id)
|
|
|
|
db.commit()
|
|
|
|
if cursor.rowcount == 0:
|
|
|
|
raise dbi.NoRecordError, id
|
|
|
|
|
|
|
|
def change(self, channel, id, replacer):
|
|
|
|
db = self._getDb(channel)
|
|
|
|
cursor = db.cursor()
|
|
|
|
cursor.execute("""SELECT subject, item FROM news WHERE id=%s""", id)
|
|
|
|
if cursor.rowcount == 0:
|
|
|
|
raise dbi.NoRecordError, id
|
|
|
|
(subject, item) = cursor.fetchone()
|
|
|
|
s = '%s: %s' % (subject, item)
|
|
|
|
s = replacer(s)
|
|
|
|
(newSubject, newItem) = s.split(': ', 1)
|
|
|
|
cursor.execute("""UPDATE news SET subject=%s, item=%s WHERE id=%s""",
|
|
|
|
newSubject, newItem, id)
|
|
|
|
|
2004-09-17 05:01:21 +02:00
|
|
|
NewsDB = plugins.DB('News',
|
|
|
|
{'sqlite': SqliteNewsDB})
|
2004-09-10 21:37:49 +02:00
|
|
|
|
|
|
|
class News(callbacks.Privmsg):
|
|
|
|
def __init__(self):
|
2004-09-20 01:51:21 +02:00
|
|
|
self.__parent = super(News, self)
|
|
|
|
self.__parent.__init__()
|
2004-09-10 21:37:49 +02:00
|
|
|
self.db = NewsDB()
|
|
|
|
self.removeOld = False
|
|
|
|
|
|
|
|
def die(self):
|
2004-09-20 01:51:21 +02:00
|
|
|
self.__parent.die()
|
2004-09-10 21:37:49 +02:00
|
|
|
self.db.close()
|
|
|
|
|
2003-10-21 07:32:57 +02:00
|
|
|
def add(self, irc, msg, args, channel):
|
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
|
|
|
"""
|
2004-09-10 21:37:49 +02:00
|
|
|
(expire_interval, news) = privmsgs.getArgs(args, required=2)
|
|
|
|
try:
|
|
|
|
expire_interval = int(expire_interval)
|
|
|
|
(subject, text) = news.split(': ', 1)
|
|
|
|
except ValueError:
|
2003-09-14 09:38:26 +02:00
|
|
|
raise callbacks.ArgumentError
|
|
|
|
added_at = int(time.time())
|
|
|
|
expires = expire_interval and (added_at + expire_interval)
|
|
|
|
# Set the other stuff needed for the insert.
|
2004-09-10 21:37:49 +02:00
|
|
|
try:
|
|
|
|
by = ircdb.users.getUserId(msg.prefix)
|
|
|
|
except KeyError:
|
|
|
|
by = msg.nick
|
|
|
|
self.db.add(channel, subject, text, added_at, expires, by)
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.replySuccess()
|
2003-10-21 07:32:57 +02:00
|
|
|
add = privmsgs.checkChannelCapability(add, 'news')
|
2003-09-14 09:38:26 +02:00
|
|
|
|
2003-10-05 22:33:01 +02:00
|
|
|
def news(self, irc, msg, args):
|
2004-09-10 21:37:49 +02:00
|
|
|
"""[<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'.
|
2004-09-10 21:37:49 +02:00
|
|
|
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
|
|
|
"""
|
|
|
|
channel = privmsgs.getChannel(msg, args)
|
2004-09-10 21:37:49 +02:00
|
|
|
id = privmsgs.getArgs(args, required=0, optional=1)
|
|
|
|
if id:
|
|
|
|
try:
|
|
|
|
record = self.db.get(channel, int(id))
|
|
|
|
irc.reply(str(record))
|
|
|
|
except dbi.NoRecordError, id:
|
|
|
|
irc.errorInvalid('news item id', id)
|
2003-09-14 09:38:26 +02:00
|
|
|
else:
|
2004-09-10 21:37:49 +02:00
|
|
|
try:
|
|
|
|
records = self.db.get(channel)
|
|
|
|
items = ['(#%s) %s' % (id, s) for (id, s) in records]
|
|
|
|
s = 'News for %s: %s' % (channel, '; '.join(items))
|
|
|
|
irc.reply(s)
|
|
|
|
except dbi.NoRecordError:
|
|
|
|
irc.reply('No news for %s.' % channel)
|
2003-04-16 01:29:40 +02:00
|
|
|
|
2003-10-21 07:32:57 +02:00
|
|
|
def remove(self, irc, msg, args, channel):
|
2003-04-16 01:29:40 +02:00
|
|
|
"""[<channel>] <number>
|
|
|
|
|
|
|
|
Removes the news item with id <number> from <channel>. <channel> is
|
|
|
|
only necessary if the message isn't sent in the channel itself.
|
|
|
|
"""
|
2003-09-14 09:38:26 +02:00
|
|
|
id = privmsgs.getArgs(args)
|
2004-09-10 21:37:49 +02:00
|
|
|
try:
|
|
|
|
self.db.remove(channel, id)
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.replySuccess()
|
2004-09-10 21:37:49 +02:00
|
|
|
except dbi.NoRecordError:
|
|
|
|
irc.errorInvalid('news item id', id)
|
2003-10-21 07:32:57 +02:00
|
|
|
remove = privmsgs.checkChannelCapability(remove, 'news')
|
2004-07-21 21:36:35 +02:00
|
|
|
|
2003-10-21 07:32:57 +02:00
|
|
|
def change(self, irc, msg, args, channel):
|
2003-04-16 01:29:40 +02:00
|
|
|
"""[<channel>] <number> <regexp>
|
|
|
|
|
|
|
|
Changes the news item with id <number> from <channel> according to the
|
|
|
|
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.
|
|
|
|
"""
|
2003-11-11 14:20:06 +01:00
|
|
|
(id, regexp) = privmsgs.getArgs(args, required=2)
|
2003-10-15 07:25:26 +02:00
|
|
|
try:
|
|
|
|
replacer = utils.perlReToReplacer(regexp)
|
|
|
|
except ValueError, e:
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.error(str(e))
|
2003-10-15 07:25:26 +02:00
|
|
|
return
|
2004-09-10 21:37:49 +02:00
|
|
|
try:
|
|
|
|
self.db.change(channel, id, replacer)
|
|
|
|
irc.replySuccess()
|
|
|
|
except dbi.NoRecordError:
|
|
|
|
irc.errorInvalid('news item id', id)
|
2003-10-21 07:32:57 +02:00
|
|
|
change = privmsgs.checkChannelCapability(change, 'news')
|
2003-04-16 01:29:40 +02:00
|
|
|
|
2003-10-21 07:32:57 +02:00
|
|
|
def old(self, irc, msg, args):
|
2003-10-07 11:32:42 +02:00
|
|
|
"""[<channel>] [<number>]
|
2003-04-16 01:29:40 +02:00
|
|
|
|
2003-10-07 11:32:42 +02:00
|
|
|
Returns the old news item for <channel> with id <number>. If no number
|
|
|
|
is given, returns all the old news items in reverse order. <channel>
|
2003-04-16 01:29:40 +02:00
|
|
|
is only necessary if the message isn't sent in the channel itself.
|
|
|
|
"""
|
2003-10-15 07:25:26 +02:00
|
|
|
channel = privmsgs.getChannel(msg, args)
|
2003-11-11 14:20:06 +01:00
|
|
|
id = privmsgs.getArgs(args, required=0, optional=1)
|
2003-10-15 07:25:26 +02:00
|
|
|
if id:
|
|
|
|
try:
|
2004-09-10 21:37:49 +02:00
|
|
|
record = self.db.get(channel, id, old=True)
|
|
|
|
irc.reply(str(record))
|
|
|
|
except dbi.NoRecordError, id:
|
|
|
|
irc.errorInvalid('news item id', id)
|
2003-10-15 07:25:26 +02:00
|
|
|
else:
|
2004-09-10 21:37:49 +02:00
|
|
|
try:
|
|
|
|
records = self.db.get(channel, old=True)
|
|
|
|
items = ['(#%s) %s' % (id, s) for (id, s) 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-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:
|