2003-03-27 03:42:26 +01:00
|
|
|
#!/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
|
2003-04-03 09:11:16 +02:00
|
|
|
users that can be retrieved later.
|
|
|
|
|
|
|
|
Commands include:
|
|
|
|
sendnote
|
|
|
|
note
|
|
|
|
notes
|
|
|
|
"""
|
|
|
|
|
2003-03-27 03:42:26 +01:00
|
|
|
from baseplugin import *
|
|
|
|
|
|
|
|
import time
|
2003-04-02 10:30:12 +02:00
|
|
|
import string
|
2003-04-08 09:07:54 +02:00
|
|
|
import os.path
|
2003-03-27 03:42:26 +01:00
|
|
|
|
|
|
|
import sqlite
|
|
|
|
|
|
|
|
import conf
|
2003-04-08 09:07:54 +02:00
|
|
|
import debug
|
2003-04-08 11:11:33 +02:00
|
|
|
import utils
|
2003-03-27 03:42:26 +01:00
|
|
|
import ircdb
|
2003-04-08 09:07:54 +02:00
|
|
|
import ircmsgs
|
2003-03-27 03:42:26 +01:00
|
|
|
import privmsgs
|
2003-03-28 08:48:16 +01:00
|
|
|
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-27 03:42:26 +01:00
|
|
|
|
2003-03-28 04:57:12 +01:00
|
|
|
class Notes(callbacks.Privmsg):
|
2003-04-01 03:44:02 +02:00
|
|
|
|
2003-03-27 03:42:26 +01:00
|
|
|
def __init__(self):
|
|
|
|
callbacks.Privmsg.__init__(self)
|
2003-04-08 09:07:54 +02:00
|
|
|
self.makeDB(dbfilename)
|
2003-03-27 03:42:26 +01:00
|
|
|
|
2003-04-08 09:07:54 +02:00
|
|
|
def makeDB(self, filename):
|
2003-03-27 03:42:26 +01:00
|
|
|
"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})
|
2003-03-27 03:42:26 +01:00
|
|
|
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,
|
|
|
|
read BOOLEAN,
|
|
|
|
public BOOLEAN,
|
|
|
|
note TEXT
|
|
|
|
)""")
|
2003-03-28 04:57:12 +01:00
|
|
|
self.cursor.execute("""CREATE INDEX users_username ON users (name)""")
|
2003-03-27 03:42:26 +01:00
|
|
|
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)
|
2003-03-27 03:42:26 +01:00
|
|
|
self.db.commit()
|
|
|
|
|
|
|
|
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)
|
2003-03-27 03:42:26 +01:00
|
|
|
if self.cursor.rowcount != 0:
|
2003-04-08 09:07:54 +02:00
|
|
|
return self.cursor.fetchone()[0]
|
2003-04-03 09:11:16 +02:00
|
|
|
else:
|
2003-04-08 09:07:54 +02:00
|
|
|
raise KeyError, username
|
2003-03-27 07:07:21 +01:00
|
|
|
|
|
|
|
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]
|
2003-03-27 07:07:21 +01:00
|
|
|
else:
|
2003-04-08 09:07:54 +02:00
|
|
|
raise KeyError, userid
|
2003-03-27 03:42:26 +01:00
|
|
|
|
2003-04-08 09:07:54 +02:00
|
|
|
def isValidNote(self, noteid):
|
|
|
|
"Checks to see if a note id represents a valid note in the table."
|
2003-04-02 10:30:12 +02:00
|
|
|
self.cursor.execute('SELECT * FROM notes WHERE id=%s', noteid)
|
|
|
|
if self.cursor.rowcount == 0:
|
2003-04-08 09:07:54 +02:00
|
|
|
return False
|
2003-04-02 10:30:12 +02:00
|
|
|
else:
|
2003-04-08 09:07:54 +02:00
|
|
|
return True
|
2003-04-02 10:30:12 +02:00
|
|
|
|
2003-04-03 07:48:57 +02:00
|
|
|
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-03 07:48:57 +02:00
|
|
|
self.cursor.execute('UPDATE notes SET read=1 WHERE id=%s', noteid)
|
|
|
|
self.db.commit()
|
2003-04-03 09:11:16 +02:00
|
|
|
|
|
|
|
def die(self):
|
2003-04-08 09:07:54 +02:00
|
|
|
"Called when module is unloaded/reloaded."
|
2003-04-03 09:11:16 +02:00
|
|
|
self.db.close()
|
2003-03-28 03:56:05 +01:00
|
|
|
|
2003-03-27 08:13:37 +01:00
|
|
|
def sendnote(self, irc, msg, args):
|
2003-04-03 07:48:57 +02:00
|
|
|
"""<recipient> <text>
|
|
|
|
|
2003-04-08 09:07:54 +02:00
|
|
|
Sends a new note to the user specified.
|
|
|
|
"""
|
2003-03-27 03:42:26 +01:00
|
|
|
(name, note) = privmsgs.getArgs(args, needed=2)
|
2003-04-08 09:07:54 +02:00
|
|
|
sender = msg.nick
|
2003-03-27 03:42:26 +01:00
|
|
|
if ircdb.users.hasUser(name):
|
2003-03-27 08:13:37 +01:00
|
|
|
recipient = name
|
2003-03-27 03:42:26 +01:00
|
|
|
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-03-27 03:42:26 +01: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-03-28 04:57:12 +01:00
|
|
|
(NULL, %s, %s, %s, %s, %s, %s)""",
|
|
|
|
senderID, recipID, int(time.time()),
|
2003-03-28 07:25:12 +01:00
|
|
|
0, public, note)
|
2003-03-27 03:42:26 +01:00
|
|
|
self.db.commit()
|
2003-03-27 08:13:37 +01:00
|
|
|
irc.reply(msg, conf.replySuccess)
|
2003-03-27 03:42:26 +01:00
|
|
|
|
2003-04-02 10:30:12 +02:00
|
|
|
def note(self, irc, msg, args):
|
2003-04-03 07:48:57 +02:00
|
|
|
"""<note id>
|
|
|
|
|
2003-04-08 09:07:54 +02:00
|
|
|
Retrieves a single note by unique note id.
|
|
|
|
"""
|
2003-03-27 07:07:21 +01:00
|
|
|
noteid = privmsgs.getArgs(args)
|
2003-04-08 09:07:54 +02:00
|
|
|
if not self.isValidNote(noteid):
|
2003-04-02 10:30:12 +02:00
|
|
|
irc.error(msg, 'Not a valid note id')
|
|
|
|
return
|
2003-03-27 07:07:21 +01:00
|
|
|
sender = ircdb.users.getUserName(msg.prefix)
|
2003-04-01 03:44:02 +02:00
|
|
|
senderID = self.getUserID(sender)
|
|
|
|
self.cursor.execute("""SELECT note, to_id, from_id, added_at, public
|
|
|
|
FROM notes WHERE id=%s LIMIT 1""", noteid)
|
|
|
|
note, to_id, from_id, added_at, public = self.cursor.fetchone()
|
|
|
|
author = self.getUserName(from_id)
|
|
|
|
public = int(public)
|
|
|
|
added_at = int(added_at)
|
2003-04-14 06:49:35 +02:00
|
|
|
elapsed = utils.timeElapsed(time.time(), added_at)
|
2003-04-08 11:11:33 +02:00
|
|
|
newnote = "%s (Sent by %s %s ago)" % (note, author, elapsed)
|
2003-03-27 07:07:21 +01:00
|
|
|
if senderID == to_id:
|
2003-03-28 07:25:12 +01:00
|
|
|
if public:
|
2003-04-01 03:44:02 +02:00
|
|
|
irc.reply(msg, newnote)
|
2003-03-27 07:07:21 +01:00
|
|
|
else:
|
2003-04-14 07:35:02 +02:00
|
|
|
irc.queueMsg(ircmsgs.privmsg(msg.nick, newnote))
|
2003-04-08 09:07:54 +02:00
|
|
|
#debug.printf("setting note to read=true")
|
2003-04-03 07:48:57 +02:00
|
|
|
self.setAsRead(noteid)
|
2003-03-27 07:07:21 +01:00
|
|
|
else:
|
2003-04-02 10:30:12 +02:00
|
|
|
irc.error(msg, 'You are not the recipient of note %s' % noteid)
|
2003-03-27 07:07:21 +01:00
|
|
|
|
2003-04-01 03:44:02 +02:00
|
|
|
def notes(self, irc, msg, args):
|
2003-04-03 07:48:57 +02:00
|
|
|
"""<takes no arguments>
|
|
|
|
|
2003-04-08 11:11:33 +02:00
|
|
|
Retrieves all unread notes for the requesting user.
|
|
|
|
"""
|
2003-04-08 09:07:54 +02:00
|
|
|
try:
|
|
|
|
sender = ircdb.users.getUserName(msg.prefix)
|
|
|
|
except KeyError:
|
|
|
|
irc.error(msg, conf.replyNoUser)
|
|
|
|
return
|
2003-03-27 07:07:21 +01:00
|
|
|
senderID = self.getUserID(sender)
|
2003-04-03 07:48:57 +02:00
|
|
|
self.cursor.execute("""SELECT id, from_id, public, read FROM notes
|
2003-04-01 03:44:02 +02:00
|
|
|
WHERE to_id=%s
|
2003-03-28 07:25:12 +01:00
|
|
|
AND read=0""", senderID)
|
2003-03-27 07:07:21 +01:00
|
|
|
notes = self.cursor.fetchall()
|
2003-04-02 10:30:12 +02:00
|
|
|
self.cursor.execute("""SELECT count(*) FROM notes
|
|
|
|
WHERE to_id=%s
|
|
|
|
AND read=0""", senderID)
|
2003-04-03 07:48:57 +02:00
|
|
|
count = int(self.cursor.fetchone()[0])
|
2003-04-14 06:49:35 +02:00
|
|
|
#debug.printf("count: %s" % count)
|
2003-03-27 07:07:21 +01:00
|
|
|
L = []
|
2003-04-03 07:48:57 +02:00
|
|
|
for (id, from_id, public, read) in notes:
|
|
|
|
if not int(read):
|
|
|
|
sender = self.getUserName(from_id)
|
|
|
|
if int(public):
|
2003-04-14 06:49:35 +02:00
|
|
|
L.append(r'#%s from %s' % (id, sender))
|
2003-04-03 07:48:57 +02:00
|
|
|
else:
|
2003-04-14 06:49:35 +02:00
|
|
|
L.append(r'#%s (private)' % id)
|
2003-04-02 10:30:12 +02:00
|
|
|
if count > 5:
|
2003-04-03 07:48:57 +02:00
|
|
|
L = string.join(L[:5], ', ')
|
2003-04-02 10:30:12 +02:00
|
|
|
reply = "you have %s unread notes, 5 shown: %s" % (count, L)
|
|
|
|
else:
|
2003-04-03 07:48:57 +02:00
|
|
|
L = string.join(L, ', ')
|
2003-04-02 10:30:12 +02:00
|
|
|
reply = "you have %s unread notes: %s" % (count, L)
|
2003-04-08 09:07:54 +02:00
|
|
|
#debug.printf(L)
|
2003-04-02 10:30:12 +02:00
|
|
|
irc.reply(msg, reply)
|
2003-03-27 07:07:21 +01:00
|
|
|
|
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:
|