Limnoria/plugins/Notes.py

274 lines
10 KiB
Python
Raw Normal View History

#!/usr/bin/env python
###
# Copyright (c) 2003, Brett Kelly
# 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-08 09:07:54 +02:00
"""
A complete messaging system that allows users to leave 'notes' for other
users that can be retrieved later.
"""
from baseplugin import *
import time
2003-04-08 09:07:54 +02:00
import os.path
2003-04-14 16:49:02 +02:00
import operator
import sqlite
import conf
2003-04-08 09:07:54 +02:00
import debug
2003-04-08 11:11:33 +02:00
import utils
import ircdb
2003-04-08 09:07:54 +02:00
import ircmsgs
import privmsgs
import callbacks
2003-04-01 03:44:02 +02:00
import ircutils
2003-04-08 09:07:54 +02:00
dbfilename = os.path.join(conf.dataDir, 'Notes.db')
2003-03-28 04:57:12 +01:00
class Notes(callbacks.Privmsg):
def __init__(self):
callbacks.Privmsg.__init__(self)
2003-04-08 09:07:54 +02:00
self.makeDB(dbfilename)
2003-04-08 09:07:54 +02:00
def makeDB(self, filename):
"create Notes database and tables"
2003-04-08 09:07:54 +02:00
if os.path.exists(filename):
self.db = sqlite.connect(filename)
self.cursor = self.db.cursor()
return
self.db = sqlite.connect(filename, converters={'bool': bool})
self.cursor = self.db.cursor()
self.cursor.execute("""CREATE TABLE users (
id INTEGER PRIMARY KEY,
name TEXT UNIQUE ON CONFLICT IGNORE
)""")
self.cursor.execute("""CREATE TABLE notes (
id INTEGER PRIMARY KEY,
from_id INTEGER,
to_id INTEGER,
added_at TIMESTAMP,
2003-04-15 21:11:12 +02:00
notified BOOLEAN,
read BOOLEAN,
public BOOLEAN,
note TEXT
)""")
self.db.commit()
def _addUser(self, username):
2003-04-08 09:07:54 +02:00
"Not callable from channel, used to add users to database."
self.cursor.execute('INSERT INTO users VALUES (NULL, %s)', username)
self.db.commit()
2003-04-24 19:36:25 +02:00
def getUserId(self, username):
2003-04-08 09:07:54 +02:00
"Returns the user id matching the given username from the users table."
2003-03-28 07:06:19 +01:00
self.cursor.execute('SELECT id FROM users where name=%s', username)
if self.cursor.rowcount != 0:
2003-04-08 09:07:54 +02:00
return self.cursor.fetchone()[0]
else:
2003-04-08 09:07:54 +02:00
raise KeyError, username
def getUserName(self, userid):
2003-04-08 09:07:54 +02:00
"Returns the username matching the given user id from the users table."
2003-03-28 07:06:19 +01:00
self.cursor.execute('SELECT name FROM users WHERE id=%s', userid)
2003-03-27 07:31:32 +01:00
if self.cursor.rowcount != 0:
2003-04-08 09:07:54 +02:00
return self.cursor.fetchone()[0]
else:
2003-04-08 09:07:54 +02:00
raise KeyError, userid
def setAsRead(self, noteid):
2003-04-08 09:07:54 +02:00
"Changes a message's 'read' value to true in the notes table."
2003-04-15 21:11:12 +02:00
self.cursor.execute("""UPDATE notes
SET read=1, notified=1
WHERE id=%s""", noteid)
self.db.commit()
def die(self):
2003-04-08 09:07:54 +02:00
"Called when module is unloaded/reloaded."
self.db.close()
2003-03-28 03:56:05 +01:00
def doJoin(self, irc, msg):
try:
name = ircdb.users.getUserName(msg.prefix)
except KeyError:
return
cursor = self.db.cursor()
cursor.execute("""SELECT COUNT(*) FROM notes, users
WHERE users.name=%s AND
notes.to_id=users.id AND
notified=0""", name)
unnotified = int(cursor.fetchone()[0])
if unnotified == 0:
return
cursor.execute("""SELECT COUNT(*) FROM notes, users
WHERE users.name=%s AND
notes.to_id=users.id AND
read=0""", name)
unread = int(cursor.fetchone()[0])
s = 'You have %s unread note%s ' \
'%s that I haven\'t told you about before now..' % \
(unread, unread == 1 and ';' or 's;', unnotified)
irc.queueMsg(ircmsgs.privmsg(msg.nick, s))
cursor.execute("""UPDATE notes
SET notified=1
WHERE notes.to_id=(SELECT id
FROM users
WHERE name=%s)""", name)
2003-03-27 08:13:37 +01:00
def sendnote(self, irc, msg, args):
"""<recipient> <text>
2003-04-08 09:07:54 +02:00
Sends a new note to the user specified.
"""
(name, note) = privmsgs.getArgs(args, needed=2)
2003-04-08 09:07:54 +02:00
sender = msg.nick
if ircdb.users.hasUser(name):
2003-03-27 08:13:37 +01:00
recipient = name
else:
n = irc.state.nickToHostmask(name)
recipient = ircdb.users.getUserName(n)
2003-03-27 08:13:37 +01:00
self._addUser(sender)
self._addUser(recipient)
2003-04-24 19:36:25 +02:00
senderId = self.getUserId(sender)
recipId = self.getUserId(recipient)
2003-03-28 07:25:12 +01:00
if ircutils.isChannel(msg.args[0]):
public = 1
else:
2003-04-01 03:44:02 +02:00
public = 0
2003-03-28 03:56:05 +01:00
self.cursor.execute("""INSERT INTO notes VALUES
2003-04-15 21:11:12 +02:00
(NULL, %s, %s, %s, 0, 0, %s, %s)""",
2003-04-24 19:36:25 +02:00
senderId, recipId, int(time.time()),
2003-04-15 21:11:12 +02:00
public, note)
self.db.commit()
2003-03-27 08:13:37 +01:00
irc.reply(msg, conf.replySuccess)
def note(self, irc, msg, args):
"""<note id>
2003-04-08 09:07:54 +02:00
Retrieves a single note by unique note id.
"""
noteid = privmsgs.getArgs(args)
try:
sender = ircdb.users.getUserName(msg.prefix)
2003-04-24 19:36:25 +02:00
senderId = self.getUserId(sender)
except KeyError:
irc.error(msg, conf.replyNoUser)
return
2003-04-24 19:36:25 +02:00
self.cursor.execute("""SELECT notes.note, notes.to_id, notes.from_id,
notes.added_at, notes.public
FROM users, notes
WHERE users.name=%s AND
notes.to_id=users.id AND
notes.id=%s
LIMIT 1""", sender, noteid)
2003-04-14 17:10:38 +02:00
if self.cursor.rowcount == 0:
irc.error(msg, 'That\'s not a valid note id.')
return
2003-04-01 03:44:02 +02:00
note, to_id, from_id, added_at, public = self.cursor.fetchone()
author = self.getUserName(from_id)
2003-04-24 19:36:25 +02:00
if senderId != to_id:
2003-04-14 17:10:38 +02:00
irc.error(msg, 'You are not the recipient of note %s.' % noteid)
return
2003-04-01 03:44:02 +02:00
public = int(public)
elapsed = utils.timeElapsed(time.time() - int(added_at))
2003-04-08 11:11:33 +02:00
newnote = "%s (Sent by %s %s ago)" % (note, author, elapsed)
2003-04-14 17:10:38 +02:00
if public:
irc.reply(msg, newnote)
else:
2003-04-14 17:10:38 +02:00
irc.queueMsg(ircmsgs.privmsg(msg.nick, newnote))
self.setAsRead(noteid)
2003-04-01 03:44:02 +02:00
def notes(self, irc, msg, args):
"""takes no arguments
Retrieves all your unread notes.
2003-04-08 11:11:33 +02:00
"""
2003-04-08 09:07:54 +02:00
try:
sender = ircdb.users.getUserName(msg.prefix)
except KeyError:
irc.error(msg, conf.replyNoUser)
return
self.cursor.execute("""SELECT notes.id, notes.from_id,
notes.public, notes.read
FROM users, notes
WHERE users.name=%s AND
notes.to_id=users.id AND
notes.read=0""", sender)
2003-04-14 16:47:49 +02:00
count = self.cursor.rowcount
notes = self.cursor.fetchall()
L = []
2003-04-14 17:10:38 +02:00
more = False
if count == 0:
irc.reply(msg, 'You have no unread notes.')
else:
for (id, from_id, public, read) in notes:
if not int(read):
sender = self.getUserName(from_id)
if int(public):
L.append(r'#%s from %s' % (id, sender))
else:
L.append(r'#%s (private)' % id)
2003-04-14 17:10:38 +02:00
if more:
ircutils.shrinkList(L, ', ', 400)
2003-04-14 17:10:38 +02:00
L.append('and even more notes.')
2003-04-18 10:19:22 +02:00
else:
ircutils.shrinkList(L, ', ', 450)
irc.reply(msg, ', '.join(L))
def oldnotes(self, irc, msg, args):
"""takes no arguments
Returns a list of your most recent old notes.
"""
try:
sender = ircdb.users.getUserName(msg.prefix)
except KeyError:
irc.error(msg, conf.replyNoUser)
return
cursor = self.db.cursor()
cursor.execute("""SELECT notes.id FROM users, notes
WHERE notes.to_id=users.id AND
users.name=%s AND
notes.read=1""", sender)
if cursor.rowcount == 0:
irc.reply(msg, 'I could find no notes for your user.')
else:
ids = [str(t[0]) for t in cursor.fetchall()]
ids.reverse()
ircutils.shrinkList(ids, ', ', 425)
ids.reverse()
irc.reply(msg, ', '.join(ids))
2003-04-08 09:07:54 +02:00
2003-03-27 07:31:32 +01:00
Class = Notes
2003-04-08 09:07:54 +02:00
# vim: shiftwidth=4 tabstop=8 expandtab textwidth=78: