mirror of
https://github.com/Mikaela/Limnoria.git
synced 2024-11-27 05:09:23 +01:00
Updated for the new ircdb.users.
This commit is contained in:
parent
644fd692fa
commit
e729b430b6
175
plugins/Notes.py
175
plugins/Notes.py
@ -38,6 +38,7 @@ from baseplugin import *
|
|||||||
|
|
||||||
import time
|
import time
|
||||||
import os.path
|
import os.path
|
||||||
|
from itertools import imap
|
||||||
|
|
||||||
import sqlite
|
import sqlite
|
||||||
|
|
||||||
@ -85,87 +86,50 @@ class Notes(callbacks.Privmsg):
|
|||||||
return
|
return
|
||||||
self.db = sqlite.connect(filename, converters={'bool': bool})
|
self.db = sqlite.connect(filename, converters={'bool': bool})
|
||||||
cursor = self.db.cursor()
|
cursor = self.db.cursor()
|
||||||
cursor.execute("""CREATE TABLE users (
|
|
||||||
id INTEGER PRIMARY KEY,
|
|
||||||
name TEXT UNIQUE ON CONFLICT IGNORE
|
|
||||||
)""")
|
|
||||||
cursor.execute("""CREATE TABLE notes (
|
cursor.execute("""CREATE TABLE notes (
|
||||||
id INTEGER PRIMARY KEY,
|
id INTEGER PRIMARY KEY,
|
||||||
from_id INTEGER,
|
from_id INTEGER,
|
||||||
to_id INTEGER,
|
to_id INTEGER,
|
||||||
added_at TIMESTAMP,
|
added_at TIMESTAMP,
|
||||||
notified BOOLEAN,
|
notified BOOLEAN,
|
||||||
read BOOLEAN,
|
read BOOLEAN,
|
||||||
public BOOLEAN,
|
public BOOLEAN,
|
||||||
note TEXT
|
note TEXT
|
||||||
)""")
|
)""")
|
||||||
self.db.commit()
|
self.db.commit()
|
||||||
|
|
||||||
def _addUser(self, username):
|
def setAsRead(self, id):
|
||||||
"Not callable from channel, used to add users to database."
|
|
||||||
cursor = self.db.cursor()
|
|
||||||
cursor.execute('INSERT INTO users VALUES (NULL, %s)', username)
|
|
||||||
self.db.commit()
|
|
||||||
|
|
||||||
def getUserId(self, username):
|
|
||||||
"Returns the user id matching the given username from the users table."
|
|
||||||
cursor = self.db.cursor()
|
|
||||||
cursor.execute('SELECT id FROM users where name=%s', username)
|
|
||||||
if cursor.rowcount != 0:
|
|
||||||
return cursor.fetchone()[0]
|
|
||||||
else:
|
|
||||||
raise KeyError, username
|
|
||||||
|
|
||||||
def getUserName(self, userid):
|
|
||||||
"Returns the username matching the given user id from the users table."
|
|
||||||
cursor = self.db.cursor()
|
|
||||||
cursor.execute('SELECT name FROM users WHERE id=%s', userid)
|
|
||||||
if cursor.rowcount != 0:
|
|
||||||
return cursor.fetchone()[0]
|
|
||||||
else:
|
|
||||||
raise KeyError, userid
|
|
||||||
|
|
||||||
def setAsRead(self, noteid):
|
|
||||||
"Changes a message's 'read' value to true in the notes table."
|
|
||||||
cursor = self.db.cursor()
|
cursor = self.db.cursor()
|
||||||
cursor.execute("""UPDATE notes
|
cursor.execute("""UPDATE notes
|
||||||
SET read=1, notified=1
|
SET read=1, notified=1
|
||||||
WHERE id=%s""", noteid)
|
WHERE id=%s""", id)
|
||||||
self.db.commit()
|
self.db.commit()
|
||||||
|
|
||||||
def die(self):
|
def die(self):
|
||||||
"Called when module is unloaded/reloaded."
|
|
||||||
self.db.commit()
|
self.db.commit()
|
||||||
self.db.close()
|
self.db.close()
|
||||||
del self.db
|
del self.db
|
||||||
|
|
||||||
def doPrivmsg(self, irc, msg):
|
def doPrivmsg(self, irc, msg):
|
||||||
try:
|
try:
|
||||||
name = ircdb.users.getUserName(msg.prefix)
|
id = ircdb.users.getUserId(msg.prefix)
|
||||||
except KeyError:
|
except KeyError:
|
||||||
callbacks.Privmsg.doPrivmsg(self, irc, msg)
|
callbacks.Privmsg.doPrivmsg(self, irc, msg)
|
||||||
return
|
return
|
||||||
cursor = self.db.cursor()
|
cursor = self.db.cursor()
|
||||||
cursor.execute("""SELECT COUNT(*) FROM notes, users
|
cursor.execute("""SELECT COUNT(*) FROM notes
|
||||||
WHERE users.name=%s AND
|
WHERE notes.to_id=%s AND notified=0""", id)
|
||||||
notes.to_id=users.id AND
|
|
||||||
notified=0""", name)
|
|
||||||
unnotified = int(cursor.fetchone()[0])
|
unnotified = int(cursor.fetchone()[0])
|
||||||
if unnotified != 0:
|
if unnotified != 0:
|
||||||
cursor.execute("""SELECT COUNT(*) FROM notes, users
|
cursor.execute("""SELECT COUNT(*) FROM notes
|
||||||
WHERE users.name=%s AND
|
WHERE notes.to_id=%s AND read=0""", id)
|
||||||
notes.to_id=users.id AND
|
|
||||||
read=0""", name)
|
|
||||||
unread = int(cursor.fetchone()[0])
|
unread = int(cursor.fetchone()[0])
|
||||||
s = 'You have %s; ' \
|
s = 'You have %s; ' \
|
||||||
'%s that I haven\'t told you about before now..' % \
|
'%s that I haven\'t told you about before now..' % \
|
||||||
(utils.nItems(unread, 'note', 'unread'), unnotified)
|
(utils.nItems(unread, 'note', 'unread'), unnotified)
|
||||||
irc.queueMsg(ircmsgs.privmsg(msg.nick, s))
|
irc.queueMsg(ircmsgs.privmsg(msg.nick, s))
|
||||||
cursor.execute("""UPDATE notes
|
cursor.execute("""UPDATE notes SET notified=1
|
||||||
SET notified=1
|
WHERE notes.to_id=%s""", id)
|
||||||
WHERE notes.to_id=(SELECT id
|
|
||||||
FROM users
|
|
||||||
WHERE name=%s)""", name)
|
|
||||||
self.db.commit()
|
self.db.commit()
|
||||||
callbacks.Privmsg.doPrivmsg(self, irc, msg)
|
callbacks.Privmsg.doPrivmsg(self, irc, msg)
|
||||||
|
|
||||||
@ -175,95 +139,91 @@ class Notes(callbacks.Privmsg):
|
|||||||
Sends a new note to the user specified.
|
Sends a new note to the user specified.
|
||||||
"""
|
"""
|
||||||
(name, note) = privmsgs.getArgs(args, needed=2)
|
(name, note) = privmsgs.getArgs(args, needed=2)
|
||||||
sender = msg.nick
|
|
||||||
if ircdb.users.hasUser(name):
|
if ircdb.users.hasUser(name):
|
||||||
recipient = name
|
toId = ircdb.users.getUserId(name)
|
||||||
else:
|
else:
|
||||||
n = irc.state.nickToHostmask(name)
|
# name must be a nick, we'll try that.
|
||||||
recipient = ircdb.users.getUserName(n)
|
try:
|
||||||
self._addUser(sender)
|
hostmask = irc.state.nickToHostmask(name)
|
||||||
self._addUser(recipient)
|
toId = ircdb.users.getUserId(hostmask)
|
||||||
senderId = self.getUserId(sender)
|
except KeyError:
|
||||||
recipId = self.getUserId(recipient)
|
irc.error(msg, conf.replyNoUser)
|
||||||
|
return
|
||||||
|
try:
|
||||||
|
fromId = ircdb.users.getUserId(msg.prefix)
|
||||||
|
except KeyError:
|
||||||
|
irc.error(msg, conf.replyNotRegistered)
|
||||||
|
return
|
||||||
if ircutils.isChannel(msg.args[0]):
|
if ircutils.isChannel(msg.args[0]):
|
||||||
public = 1
|
public = 1
|
||||||
else:
|
else:
|
||||||
public = 0
|
public = 0
|
||||||
cursor = self.db.cursor()
|
cursor = self.db.cursor()
|
||||||
cursor.execute("""INSERT INTO notes VALUES
|
cursor.execute("""INSERT INTO notes VALUES
|
||||||
(NULL, %s, %s, %s, 0, 0, %s, %s)""",
|
(NULL, %s, %s, %s, 0, 0, %s, %s)""",
|
||||||
senderId, recipId, int(time.time()),
|
fromId, toId, int(time.time()), public, note)
|
||||||
public, note)
|
|
||||||
self.db.commit()
|
self.db.commit()
|
||||||
irc.reply(msg, conf.replySuccess)
|
irc.reply(msg, conf.replySuccess)
|
||||||
|
|
||||||
def note(self, irc, msg, args):
|
def note(self, irc, msg, args):
|
||||||
"""<note id>
|
"""<note id>
|
||||||
|
|
||||||
Retrieves a single note by unique note id.
|
Retrieves a single note by its unique note id.
|
||||||
"""
|
"""
|
||||||
noteid = privmsgs.getArgs(args)
|
noteid = privmsgs.getArgs(args)
|
||||||
try:
|
try:
|
||||||
sender = ircdb.users.getUserName(msg.prefix)
|
id = ircdb.users.getUserId(msg.prefix)
|
||||||
senderId = self.getUserId(sender)
|
|
||||||
except KeyError:
|
except KeyError:
|
||||||
irc.error(msg, conf.replyNotRegistered)
|
irc.error(msg, conf.replyNotRegistered)
|
||||||
return
|
return
|
||||||
cursor = self.db.cursor()
|
cursor = self.db.cursor()
|
||||||
cursor.execute("""SELECT notes.note, notes.to_id, notes.from_id,
|
cursor.execute("""SELECT note, to_id, from_id, added_at, public
|
||||||
notes.added_at, notes.public
|
FROM notes
|
||||||
FROM users, notes
|
WHERE notes.to_id=%s AND notes.id=%s""",
|
||||||
WHERE users.name=%s AND
|
id, noteid)
|
||||||
notes.to_id=users.id AND
|
|
||||||
notes.id=%s
|
|
||||||
LIMIT 1""", sender, noteid)
|
|
||||||
if cursor.rowcount == 0:
|
if cursor.rowcount == 0:
|
||||||
irc.error(msg, 'That\'s not a valid note id.')
|
irc.error(msg, 'That\'s not a valid note id for you.')
|
||||||
return
|
return
|
||||||
(note, to_id, from_id, added_at, public) = cursor.fetchone()
|
(note, toId, fromId, addedAt, public) = cursor.fetchone()
|
||||||
author = self.getUserName(from_id)
|
(toId,fromId,addedAt,public) = map(int, (toId,fromId,addedAt,public))
|
||||||
if senderId != to_id:
|
author = ircdb.users.getUser(fromId).name
|
||||||
irc.error(msg, 'You are not the recipient of note %s.' % noteid)
|
elapsed = utils.timeElapsed(time.time() - addedAt)
|
||||||
return
|
|
||||||
public = int(public)
|
|
||||||
elapsed = utils.timeElapsed(time.time() - int(added_at))
|
|
||||||
newnote = "%s (Sent by %s %s ago)" % (note, author, elapsed)
|
newnote = "%s (Sent by %s %s ago)" % (note, author, elapsed)
|
||||||
if public:
|
if public:
|
||||||
irc.reply(msg, newnote)
|
irc.reply(msg, newnote)
|
||||||
else:
|
else:
|
||||||
|
### FIXME: IrcObjectProxy should offer a private keyword arg.
|
||||||
irc.queueMsg(ircmsgs.privmsg(msg.nick, newnote))
|
irc.queueMsg(ircmsgs.privmsg(msg.nick, newnote))
|
||||||
self.setAsRead(noteid)
|
self.setAsRead(noteid)
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
def notes(self, irc, msg, args):
|
def notes(self, irc, msg, args):
|
||||||
"""takes no arguments
|
"""takes no arguments
|
||||||
|
|
||||||
Retrieves all your unread notes.
|
Retrieves the ids of all your unread notes.
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
sender = ircdb.users.getUserName(msg.prefix)
|
id = ircdb.users.getUserId(msg.prefix)
|
||||||
except KeyError:
|
except KeyError:
|
||||||
irc.error(msg, conf.replyNotRegistered)
|
irc.error(msg, conf.replyNotRegistered)
|
||||||
return
|
return
|
||||||
cursor = self.db.cursor()
|
cursor = self.db.cursor()
|
||||||
cursor.execute("""SELECT notes.id, notes.from_id,
|
cursor.execute("""SELECT id, from_id, public
|
||||||
notes.public, notes.read
|
FROM notes
|
||||||
FROM users, notes
|
WHERE notes.to_id=%s AND notes.read=0""", id)
|
||||||
WHERE users.name=%s AND
|
|
||||||
notes.to_id=users.id AND
|
|
||||||
notes.read=0""", sender)
|
|
||||||
count = cursor.rowcount
|
count = cursor.rowcount
|
||||||
notes = cursor.fetchall()
|
|
||||||
L = []
|
L = []
|
||||||
if count == 0:
|
if count == 0:
|
||||||
irc.reply(msg, 'You have no unread notes.')
|
irc.reply(msg, 'You have no unread notes.')
|
||||||
else:
|
else:
|
||||||
for (id, from_id, public, read) in notes:
|
L = [self._formatNoteData(msg, *t) for t in cursor.fetchall()]
|
||||||
if not int(read):
|
|
||||||
sender = self.getUserName(from_id)
|
|
||||||
if int(public) or not ircutils.isChannel(msg.args[0]):
|
|
||||||
L.append(r'#%s from %s' % (id, sender))
|
|
||||||
else:
|
|
||||||
L.append(r'#%s (private)' % id)
|
|
||||||
irc.reply(msg, utils.commaAndify(L))
|
irc.reply(msg, utils.commaAndify(L))
|
||||||
|
|
||||||
def oldnotes(self, irc, msg, args):
|
def oldnotes(self, irc, msg, args):
|
||||||
@ -272,19 +232,18 @@ class Notes(callbacks.Privmsg):
|
|||||||
Returns a list of your most recent old notes.
|
Returns a list of your most recent old notes.
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
sender = ircdb.users.getUserName(msg.prefix)
|
id = ircdb.users.getUserId(msg.prefix)
|
||||||
except KeyError:
|
except KeyError:
|
||||||
irc.error(msg, conf.replyNotRegistered)
|
irc.error(msg, conf.replyNotRegistered)
|
||||||
return
|
return
|
||||||
cursor = self.db.cursor()
|
cursor = self.db.cursor()
|
||||||
cursor.execute("""SELECT notes.id FROM users, notes
|
cursor.execute("""SELECT id, from_id, public
|
||||||
WHERE notes.to_id=users.id AND
|
FROM notes
|
||||||
users.name=%s AND
|
WHERE notes.to_id=%s AND notes.read=1""", id)
|
||||||
notes.read=1""", sender)
|
|
||||||
if cursor.rowcount == 0:
|
if cursor.rowcount == 0:
|
||||||
irc.reply(msg, 'I couldn\'t find any notes for your user.')
|
irc.reply(msg, 'I couldn\'t find any read notes for your user.')
|
||||||
else:
|
else:
|
||||||
ids = [str(t[0]) for t in cursor.fetchall()]
|
ids = [self._formatNoteData(msg, *t) for t in cursor.fetchall()]
|
||||||
ids.reverse()
|
ids.reverse()
|
||||||
irc.reply(msg, utils.commaAndify(ids))
|
irc.reply(msg, utils.commaAndify(ids))
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user