1
0
mirror of https://github.com/Mikaela/Limnoria.git synced 2024-12-24 03:33:11 +01:00
Limnoria/plugins/News.py

256 lines
9.9 KiB
Python
Raw Normal View History

2003-04-09 21:16:52 +02:00
#!/usr/bin/env python
###
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.
###
"""
2003-04-16 01:29:40 +02:00
A module to allow each channel to have "news" which people will be notified of
when they join the channel. 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 plugins
2003-04-09 21:16:52 +02:00
import os
import time
import conf
2003-09-14 09:38:26 +02:00
import ircdb
import utils
2003-04-09 21:16:52 +02:00
import privmsgs
import callbacks
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/>'
2003-10-19 23:10:56 +02:00
class News(plugins.ChannelDBHandler, callbacks.Privmsg):
2003-04-09 21:16:52 +02:00
def __init__(self):
plugins.ChannelDBHandler.__init__(self)
2003-04-09 21:16:52 +02:00
callbacks.Privmsg.__init__(self)
2003-04-16 01:29:40 +02:00
self.removeOld = False
2003-04-09 21:16:52 +02:00
def makeDb(self, filename):
if os.path.exists(filename):
return sqlite.connect(filename)
db = sqlite.connect(filename)
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()
return db
2003-04-09 21:16:52 +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
"""
2003-09-14 09:38:26 +02:00
# Parse out the args
i = None
for i, arg in enumerate(args):
if arg.endswith(':'):
i += 1
break
if not i:
raise callbacks.ArgumentError
added_at = int(time.time())
expire_interval = int(args[0])
expires = expire_interval and (added_at + expire_interval)
subject = ' '.join(args[1:i])
text = ' '.join(args[i:])
# Set the other stuff needed for the insert.
if ircdb.users.hasUser(msg.prefix):
name = ircdb.users.getUser(msg.prefix).name
2003-09-14 09:38:26 +02:00
else:
name = msg.nick
db = self.getDb(channel)
cursor = db.cursor()
cursor.execute("INSERT INTO news VALUES (NULL, %s, %s, %s, %s, %s)",
subject[:-1], text, added_at, expires, name)
db.commit()
irc.replySuccess()
add = privmsgs.checkChannelCapability(add, 'news')
2003-09-14 09:38:26 +02:00
2003-10-05 22:33:01 +02:00
def _readnews(self, irc, msg, args):
2003-09-14 09:38:26 +02:00
"""[<channel>] <number>
Display the text for news item with id <number> from <channel>.
<channel> is only necessary if the message isn't sent in the channel
itself.
"""
channel = privmsgs.getChannel(msg, args)
id = privmsgs.getArgs(args)
db = self.getDb(channel)
cursor = db.cursor()
cursor.execute("""SELECT news.item, news.subject, news.added_at,
news.expires_at, news.added_by FROM news
2003-10-05 22:33:01 +02:00
WHERE news.id=%s""", id)
2003-09-14 09:38:26 +02:00
if cursor.rowcount == 0:
irc.error('No news item matches that id.')
2003-09-14 09:38:26 +02:00
else:
item, subject, added_at, expires_at, added_by = cursor.fetchone()
2003-10-05 22:33:01 +02:00
if int(expires_at) == 0:
s = '%s (Subject: "%s", added by %s on %s)' % \
(item, subject, added_by,
2004-01-18 08:58:26 +01:00
time.strftime(conf.supybot.humanTimestampFormat(),
2003-10-05 22:33:01 +02:00
time.localtime(int(added_at))))
2003-09-14 09:38:26 +02:00
else:
2003-10-05 22:33:01 +02:00
s = '%s (Subject: "%s", added by %s on %s, expires at %s)' % \
(item, subject, added_by,
2004-01-18 08:58:26 +01:00
time.strftime(conf.supybot.humanTimestampFormat(),
2003-10-05 22:33:01 +02:00
time.localtime(int(added_at))),
2004-01-18 08:58:26 +01:00
time.strftime(conf.supybot.humanTimestampFormat(),
2003-10-05 22:33:01 +02:00
time.localtime(int(expires_at))))
irc.reply(s)
2003-09-14 09:38:26 +02:00
2003-10-05 22:33:01 +02:00
def news(self, irc, msg, args):
"""[<channel>] [<number>]
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 <number> 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)
number = privmsgs.getArgs(args, required=0, optional=1)
2003-10-05 22:33:01 +02:00
if number:
self._readnews(irc, msg, [channel, number])
return
2003-09-14 09:38:26 +02:00
db = self.getDb(channel)
cursor = db.cursor()
cursor.execute("""SELECT news.id, news.subject FROM news
2003-10-05 22:33:01 +02:00
WHERE news.expires_at > %s
OR news.expires_at=0""", int(time.time()))
2003-09-14 09:38:26 +02:00
if cursor.rowcount == 0:
2004-01-12 19:56:10 +01:00
irc.reply('No news for %s.' % channel)
2003-09-14 09:38:26 +02:00
else:
2003-10-05 22:33:01 +02:00
items = ['(#%s) %s' % (id, s) for (id, s) in cursor.fetchall()]
s = 'News for %s: %s' % (channel, '; '.join(items))
irc.reply(s)
2003-04-16 01:29:40 +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)
db = self.getDb(channel)
cursor = db.cursor()
2003-10-15 07:25:26 +02:00
cursor.execute("""SELECT * FROM news WHERE id=%s""", id)
2003-09-14 09:38:26 +02:00
if cursor.rowcount == 0:
irc.error('No news item matches that id.')
2003-09-14 09:38:26 +02:00
else:
cursor.execute("""DELETE FROM news WHERE news.id = %s""", id)
db.commit()
irc.replySuccess()
remove = privmsgs.checkChannelCapability(remove, 'news')
2003-09-14 09:38:26 +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.
"""
(id, regexp) = privmsgs.getArgs(args, required=2)
2003-10-15 07:25:26 +02:00
try:
replacer = utils.perlReToReplacer(regexp)
except ValueError, e:
irc.error(str(e))
2003-10-15 07:25:26 +02:00
return
db = self.getDb(channel)
cursor = db.cursor()
cursor.execute("""SELECT subject, item FROM news WHERE id=%s""", id)
if cursor.rowcount == 0:
irc.error('No news item matches that id.')
2003-10-15 07:25:26 +02:00
return
(subject, item) = cursor.fetchone()
s = '%s: %s' % (subject, item)
s = replacer(s)
(newSubject, newItem) = s.split(': ')
cursor.execute("""UPDATE news SET subject=%s, item=%s WHERE id=%s""",
newSubject, newItem, id)
irc.replySuccess()
change = privmsgs.checkChannelCapability(change, 'news')
2003-04-16 01:29:40 +02:00
def old(self, irc, msg, args):
"""[<channel>] [<number>]
2003-04-16 01:29:40 +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)
id = privmsgs.getArgs(args, required=0, optional=1)
2003-10-15 07:25:26 +02:00
db = self.getDb(channel)
cursor = db.cursor()
if id:
try:
id = int(id)
except ValueError:
irc.error('%r isn\'t a valid id.' % id)
2003-10-15 07:25:26 +02:00
return
cursor.execute("""SELECT subject, item FROM news WHERE id=%s""",id)
if cursor.rowcount == 0:
irc.error('No news item matches that id.')
2003-10-15 07:25:26 +02:00
else:
(subject, item) = cursor.fetchone()
irc.reply('%s: %s' % (cursor, item))
2003-10-15 07:25:26 +02:00
else:
cursor.execute("""SELECT id, subject FROM news
WHERE expires_at <> 0 AND expires_at < %s
2003-10-15 07:25:26 +02:00
ORDER BY id DESC""", int(time.time()))
if cursor.rowcount == 0:
irc.error('I have no news for that channel.')
2003-10-15 07:25:26 +02:00
return
subjects = ['#%s: %s' % (id, s) for (id, s) in cursor.fetchall()]
irc.reply(utils.commaAndify(subjects))
2003-10-15 07:25:26 +02:00
2003-04-09 21:16:52 +02:00
2003-09-14 09:38:26 +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: