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.
|
|
|
|
"""
|
|
|
|
|
2003-10-05 14:56:56 +02:00
|
|
|
import plugins
|
2003-03-27 03:42:26 +01:00
|
|
|
|
|
|
|
import time
|
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-04-01 03:44:02 +02:00
|
|
|
import ircutils
|
2003-08-19 09:48:43 +02:00
|
|
|
import callbacks
|
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-11-11 13:59:27 +01:00
|
|
|
class Note(callbacks.Privmsg):
|
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)
|
|
|
|
return
|
|
|
|
self.db = sqlite.connect(filename, converters={'bool': bool})
|
2003-08-19 09:48:43 +02:00
|
|
|
cursor = self.db.cursor()
|
|
|
|
cursor.execute("""CREATE TABLE notes (
|
2003-09-14 04:43:17 +02:00
|
|
|
id INTEGER PRIMARY KEY,
|
|
|
|
from_id INTEGER,
|
|
|
|
to_id INTEGER,
|
|
|
|
added_at TIMESTAMP,
|
|
|
|
notified BOOLEAN,
|
|
|
|
read BOOLEAN,
|
|
|
|
public BOOLEAN,
|
|
|
|
note TEXT
|
|
|
|
)""")
|
2003-03-27 03:42:26 +01:00
|
|
|
self.db.commit()
|
2003-08-20 18:26:23 +02:00
|
|
|
|
2003-09-14 04:43:17 +02:00
|
|
|
def setAsRead(self, id):
|
2003-08-19 09:48:43 +02:00
|
|
|
cursor = self.db.cursor()
|
|
|
|
cursor.execute("""UPDATE notes
|
2003-09-14 04:43:17 +02:00
|
|
|
SET read=1, notified=1
|
|
|
|
WHERE id=%s""", id)
|
2003-04-03 07:48:57 +02:00
|
|
|
self.db.commit()
|
2003-04-03 09:11:16 +02:00
|
|
|
|
|
|
|
def die(self):
|
2003-08-19 09:48:43 +02:00
|
|
|
self.db.commit()
|
2003-04-03 09:11:16 +02:00
|
|
|
self.db.close()
|
2003-08-29 01:11:43 +02:00
|
|
|
del self.db
|
2003-08-20 18:26:23 +02:00
|
|
|
|
2003-09-03 19:43:06 +02:00
|
|
|
def doPrivmsg(self, irc, msg):
|
2003-04-16 09:21:06 +02:00
|
|
|
try:
|
2003-09-14 04:43:17 +02:00
|
|
|
id = ircdb.users.getUserId(msg.prefix)
|
2003-04-16 09:21:06 +02:00
|
|
|
except KeyError:
|
2003-09-08 10:58:00 +02:00
|
|
|
return
|
2003-04-16 09:21:06 +02:00
|
|
|
cursor = self.db.cursor()
|
2003-09-14 04:43:17 +02:00
|
|
|
cursor.execute("""SELECT COUNT(*) FROM notes
|
|
|
|
WHERE notes.to_id=%s AND notified=0""", id)
|
2003-04-16 09:21:06 +02:00
|
|
|
unnotified = int(cursor.fetchone()[0])
|
2003-09-05 21:01:02 +02:00
|
|
|
if unnotified != 0:
|
2003-09-14 04:43:17 +02:00
|
|
|
cursor.execute("""SELECT COUNT(*) FROM notes
|
|
|
|
WHERE notes.to_id=%s AND read=0""", id)
|
2003-09-05 21:01:02 +02:00
|
|
|
unread = int(cursor.fetchone()[0])
|
|
|
|
s = 'You have %s; ' \
|
|
|
|
'%s that I haven\'t told you about before now..' % \
|
|
|
|
(utils.nItems(unread, 'note', 'unread'), unnotified)
|
|
|
|
irc.queueMsg(ircmsgs.privmsg(msg.nick, s))
|
2003-09-14 04:43:17 +02:00
|
|
|
cursor.execute("""UPDATE notes SET notified=1
|
|
|
|
WHERE notes.to_id=%s""", id)
|
2003-09-09 19:40:47 +02:00
|
|
|
self.db.commit()
|
2003-08-15 05:00:59 +02:00
|
|
|
|
2003-11-11 13:59:27 +01:00
|
|
|
def send(self, irc, msg, args):
|
2003-04-03 07:48:57 +02:00
|
|
|
"""<recipient> <text>
|
2003-08-20 18:26:23 +02:00
|
|
|
|
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)
|
|
|
|
if ircdb.users.hasUser(name):
|
2003-09-14 04:43:17 +02:00
|
|
|
toId = ircdb.users.getUserId(name)
|
2003-03-27 03:42:26 +01:00
|
|
|
else:
|
2003-09-14 04:43:17 +02:00
|
|
|
# name must be a nick, we'll try that.
|
|
|
|
try:
|
|
|
|
hostmask = irc.state.nickToHostmask(name)
|
|
|
|
toId = ircdb.users.getUserId(hostmask)
|
|
|
|
except KeyError:
|
|
|
|
irc.error(msg, conf.replyNoUser)
|
|
|
|
return
|
|
|
|
try:
|
|
|
|
fromId = ircdb.users.getUserId(msg.prefix)
|
|
|
|
except KeyError:
|
|
|
|
irc.error(msg, conf.replyNotRegistered)
|
|
|
|
return
|
2003-08-20 18:26:23 +02:00
|
|
|
if ircutils.isChannel(msg.args[0]):
|
2003-03-28 07:25:12 +01:00
|
|
|
public = 1
|
2003-08-20 18:26:23 +02:00
|
|
|
else:
|
|
|
|
public = 0
|
2003-08-19 09:48:43 +02:00
|
|
|
cursor = self.db.cursor()
|
2003-10-23 11:16:20 +02:00
|
|
|
now = int(time.time())
|
2003-08-20 18:26:23 +02:00
|
|
|
cursor.execute("""INSERT INTO notes VALUES
|
2003-09-14 04:43:17 +02:00
|
|
|
(NULL, %s, %s, %s, 0, 0, %s, %s)""",
|
2003-10-23 11:16:20 +02:00
|
|
|
fromId, toId, now, public, note)
|
2003-03-27 03:42:26 +01:00
|
|
|
self.db.commit()
|
2003-10-23 11:16:20 +02:00
|
|
|
cursor.execute("""SELECT id FROM notes WHERE
|
|
|
|
from_id=%s AND to_id=%s AND added_at=%s""",
|
|
|
|
fromId, toId, now)
|
|
|
|
id = cursor.fetchone()[0]
|
|
|
|
irc.reply(msg, 'Note #%s sent to %s.' % (id, name))
|
2003-03-27 03:42:26 +01:00
|
|
|
|
2003-11-11 13:59:27 +01:00
|
|
|
def get(self, irc, msg, args):
|
2003-04-03 07:48:57 +02:00
|
|
|
"""<note id>
|
2003-08-20 18:26:23 +02:00
|
|
|
|
2003-09-14 04:43:17 +02:00
|
|
|
Retrieves a single note by its unique note id.
|
2003-04-08 09:07:54 +02:00
|
|
|
"""
|
2003-03-27 07:07:21 +01:00
|
|
|
noteid = privmsgs.getArgs(args)
|
2003-04-19 23:40:04 +02:00
|
|
|
try:
|
2003-09-14 04:43:17 +02:00
|
|
|
id = ircdb.users.getUserId(msg.prefix)
|
2003-04-19 23:40:04 +02:00
|
|
|
except KeyError:
|
2003-09-08 10:58:00 +02:00
|
|
|
irc.error(msg, conf.replyNotRegistered)
|
2003-04-19 23:40:04 +02:00
|
|
|
return
|
2003-08-19 09:48:43 +02:00
|
|
|
cursor = self.db.cursor()
|
2003-09-14 04:43:17 +02:00
|
|
|
cursor.execute("""SELECT note, to_id, from_id, added_at, public
|
|
|
|
FROM notes
|
2003-10-26 18:49:11 +01:00
|
|
|
WHERE (to_id=%s OR from_id=%s) AND id=%s""",
|
|
|
|
id, id, noteid)
|
2003-08-19 09:48:43 +02:00
|
|
|
if cursor.rowcount == 0:
|
2003-10-26 18:49:11 +01:00
|
|
|
s = 'You may only retrieve notes you\'ve sent or received.'
|
|
|
|
irc.error(msg, s)
|
2003-04-14 17:10:38 +02:00
|
|
|
return
|
2003-09-14 04:43:17 +02:00
|
|
|
(note, toId, fromId, addedAt, public) = cursor.fetchone()
|
|
|
|
(toId,fromId,addedAt,public) = map(int, (toId,fromId,addedAt,public))
|
|
|
|
elapsed = utils.timeElapsed(time.time() - addedAt)
|
2003-10-26 18:49:11 +01:00
|
|
|
if toId == id:
|
|
|
|
author = ircdb.users.getUser(fromId).name
|
|
|
|
newnote = '%s (Sent by %s %s ago)' % (note, author, elapsed)
|
2003-10-26 18:50:12 +01:00
|
|
|
elif fromId == id:
|
2003-10-26 18:49:11 +01:00
|
|
|
recipient = ircdb.users.getUser(toId).name
|
|
|
|
newnote = '%s (Sent to %s %s ago)' % (note, recipient, elapsed)
|
2003-10-24 10:57:02 +02:00
|
|
|
irc.reply(msg, newnote, private=(not public))
|
2003-04-14 17:10:38 +02:00
|
|
|
self.setAsRead(noteid)
|
2003-03-27 07:07:21 +01:00
|
|
|
|
2003-09-14 04:43:17 +02:00
|
|
|
def _formatNoteData(self, msg, id, fromId, public):
|
|
|
|
(id, fromId, public) = map(int, (id, fromId, public))
|
|
|
|
if public or not ircutils.isChannel(msg.args[0]):
|
|
|
|
sender = ircdb.users.getUser(fromId).name
|
|
|
|
return '#%s from %s' % (id, sender)
|
|
|
|
else:
|
|
|
|
return '#%s (private)' % id
|
|
|
|
|
2003-11-11 13:59:27 +01:00
|
|
|
def list(self, irc, msg, args):
|
|
|
|
"""[--old]
|
2003-08-20 18:26:23 +02:00
|
|
|
|
2003-11-11 13:59:27 +01:00
|
|
|
Retrieves the ids of all your unread notes. If --old is given, list
|
|
|
|
read notes.
|
2003-04-08 11:11:33 +02:00
|
|
|
"""
|
2003-11-11 13:59:27 +01:00
|
|
|
if '--old' in args:
|
|
|
|
while '--old' in args:
|
|
|
|
args.remove('--old')
|
|
|
|
self._oldnotes(irc, msg, args)
|
2003-04-08 09:07:54 +02:00
|
|
|
try:
|
2003-09-14 04:43:17 +02:00
|
|
|
id = ircdb.users.getUserId(msg.prefix)
|
2003-04-08 09:07:54 +02:00
|
|
|
except KeyError:
|
2003-09-08 10:58:00 +02:00
|
|
|
irc.error(msg, conf.replyNotRegistered)
|
2003-04-08 09:07:54 +02:00
|
|
|
return
|
2003-08-19 09:48:43 +02:00
|
|
|
cursor = self.db.cursor()
|
2003-09-14 04:43:17 +02:00
|
|
|
cursor.execute("""SELECT id, from_id, public
|
|
|
|
FROM notes
|
|
|
|
WHERE notes.to_id=%s AND notes.read=0""", id)
|
2003-08-19 09:48:43 +02:00
|
|
|
count = cursor.rowcount
|
2003-03-27 07:07:21 +01:00
|
|
|
L = []
|
2003-04-14 16:47:02 +02:00
|
|
|
if count == 0:
|
|
|
|
irc.reply(msg, 'You have no unread notes.')
|
2003-04-02 10:30:12 +02:00
|
|
|
else:
|
2003-09-14 04:43:17 +02:00
|
|
|
L = [self._formatNoteData(msg, *t) for t in cursor.fetchall()]
|
2003-09-07 08:01:25 +02:00
|
|
|
irc.reply(msg, utils.commaAndify(L))
|
2003-03-27 07:07:21 +01:00
|
|
|
|
2003-11-11 13:59:27 +01:00
|
|
|
def _oldnotes(self, irc, msg, args):
|
2003-04-19 23:40:04 +02:00
|
|
|
"""takes no arguments
|
|
|
|
|
|
|
|
Returns a list of your most recent old notes.
|
|
|
|
"""
|
|
|
|
try:
|
2003-09-14 04:43:17 +02:00
|
|
|
id = ircdb.users.getUserId(msg.prefix)
|
2003-04-19 23:40:04 +02:00
|
|
|
except KeyError:
|
2003-09-08 10:58:00 +02:00
|
|
|
irc.error(msg, conf.replyNotRegistered)
|
2003-04-19 23:40:04 +02:00
|
|
|
return
|
|
|
|
cursor = self.db.cursor()
|
2003-09-14 04:43:17 +02:00
|
|
|
cursor.execute("""SELECT id, from_id, public
|
|
|
|
FROM notes
|
|
|
|
WHERE notes.to_id=%s AND notes.read=1""", id)
|
2003-04-19 23:40:04 +02:00
|
|
|
if cursor.rowcount == 0:
|
2003-09-14 04:43:17 +02:00
|
|
|
irc.reply(msg, 'I couldn\'t find any read notes for your user.')
|
2003-04-19 23:40:04 +02:00
|
|
|
else:
|
2003-09-14 04:43:17 +02:00
|
|
|
ids = [self._formatNoteData(msg, *t) for t in cursor.fetchall()]
|
2003-04-19 23:40:04 +02:00
|
|
|
ids.reverse()
|
2003-09-07 08:01:25 +02:00
|
|
|
irc.reply(msg, utils.commaAndify(ids))
|
2003-08-20 18:26:23 +02:00
|
|
|
|
2003-04-19 23:40:04 +02:00
|
|
|
|
2003-11-11 13:59:27 +01:00
|
|
|
Class = Note
|
2003-04-08 09:07:54 +02:00
|
|
|
|
|
|
|
# vim: shiftwidth=4 tabstop=8 expandtab textwidth=78:
|