getnote (almost) works now

This commit is contained in:
Brett Kelly 2003-04-01 01:44:02 +00:00
parent 46f3012f9a
commit c618a8fd19

View File

@ -37,26 +37,26 @@ import os.path
import sqlite import sqlite
import conf import conf
import debug
import ircdb import ircdb
import privmsgs import privmsgs
import ircutils
import callbacks import callbacks
import ircutils
import debug
class Notes(callbacks.Privmsg): class Notes(callbacks.Privmsg):
def __init__(self): def __init__(self):
callbacks.Privmsg.__init__(self) callbacks.Privmsg.__init__(self)
self.filename = os.path.join(conf.dataDir, 'Notes.db') self.filename = os.path.join(conf.dataDir, 'Notes.db')
self.converters = {'bool': bool}
if os.path.exists(self.filename): if os.path.exists(self.filename):
self.db = sqlite.connect(self.filename, converters=self.converters) self.db = sqlite.connect(self.filename)
self.cursor = self.db.cursor() self.cursor = self.db.cursor()
else: else:
self.makeDB() self.makeDB()
def makeDB(self): def makeDB(self):
"create Notes database and tables" "create Notes database and tables"
self.db = sqlite.connect(self.filename, converters=self.converters) self.db = sqlite.connect(self.filename, converters={'bool': bool})
self.cursor = self.db.cursor() self.cursor = self.db.cursor()
self.cursor.execute("""CREATE TABLE users ( self.cursor.execute("""CREATE TABLE users (
id INTEGER PRIMARY KEY, id INTEGER PRIMARY KEY,
@ -76,23 +76,30 @@ class Notes(callbacks.Privmsg):
def _addUser(self, username): def _addUser(self, username):
"not callable from channel, used to add users to database" "not callable from channel, used to add users to database"
self.cursor.execute('INSERT INTO users VALUES (NULL, %s)', username) self.cursor.execute('INSERT INTO users VALUES (NULL,%s)', username)
self.db.commit() self.db.commit()
def getUserID(self, username): def getUserID(self, username):
self.cursor.execute('SELECT id FROM users where name=%s', username) self.cursor.execute('SELECT id FROM users where name=%s', username)
if self.cursor.rowcount != 0: if self.cursor.rowcount != 0:
results = self.cursor.fetchall() results = self.cursor.fetchall()
return results[0] return results[0][0]
else: # this should NEVER happen else: # this should NEVER happen
assert False assert False
def getUserName(self, userid): def getUserName(self, userid):
self.cursor.execute('SELECT name FROM users WHERE id=%s', userid) self.cursor.execute('SELECT name FROM users WHERE id=%s', userid)
if self.cursor.rowcount != 0: if self.cursor.rowcount != 0:
return self.cursor.fetchone()[0] results = self.cursor.fetchall()
return results[0][0]
else: else:
raise KeyError, userid raise KeyError
def makePrivate(self, msg):
args = list(msg.args)
args[0] = msg.nick
msg.args = tuple(args)
return msg
# def setNoteUnread(self, irc, msg, args): # def setNoteUnread(self, irc, msg, args):
# "set a note as unread" # "set a note as unread"
@ -102,10 +109,8 @@ class Notes(callbacks.Privmsg):
# irc.reply(msg, conf.replySuccess) # irc.reply(msg, conf.replySuccess)
def sendnote(self, irc, msg, args): def sendnote(self, irc, msg, args):
"""<user> <text> "sends a new note to an IRC user"
# sendnote <user> <text>
Sends a note to an IRC user.
"""
(name, note) = privmsgs.getArgs(args, needed=2) (name, note) = privmsgs.getArgs(args, needed=2)
sender = ircutils.nickFromHostmask(msg.prefix) sender = ircutils.nickFromHostmask(msg.prefix)
if ircdb.users.hasUser(name): if ircdb.users.hasUser(name):
@ -129,42 +134,46 @@ class Notes(callbacks.Privmsg):
irc.reply(msg, conf.replySuccess) irc.reply(msg, conf.replySuccess)
def getnote(self, irc, msg, args): def getnote(self, irc, msg, args):
"""<id> "retrieves a single note by unique note id"
Retrieves a single note by unique number id
"""
# BLOODY HELL, THIS ACTUALLY WORKS!!! # BLOODY HELL, THIS ACTUALLY WORKS!!!
noteid = privmsgs.getArgs(args) noteid = privmsgs.getArgs(args)
sender = ircdb.users.getUserName(msg.prefix) sender = ircdb.users.getUserName(msg.prefix)
senderID = self.getUserID(sender)[0] senderID = self.getUserID(sender)
self.cursor.execute("""SELECT note, to_id, public FROM notes self.cursor.execute("""SELECT note, to_id, from_id, added_at, public
WHERE id=%s LIMIT 1""", noteid) FROM notes WHERE id=%s LIMIT 1""", noteid)
note, to_id, public = self.cursor.fetchone() note, to_id, from_id, added_at, public = self.cursor.fetchone()
debug.printf("senderID: %s" % senderID) author = self.getUserName(from_id)
public = int(public)
added_at = int(added_at)
senttime = time.asctime(time.gmtime(added_at))
newnote = "%s (Sent by %s on %s)" % (note, author, senttime)
if senderID == to_id: if senderID == to_id:
if public: if public:
irc.reply(msg, note) irc.reply(msg, newnote)
else: else:
msg.args[0] = msg.nick "trying to change message target"
irc.reply(msg, note) msg = self.makePrivate(msg)
irc.reply(msg, newnote)
self.cursor.execute("""UPDATE notes SET read=%s self.cursor.execute("""UPDATE notes SET read=%s
WHERE id=%s""", (1, noteid)) WHERE id=%s""", (1, noteid[0]))
self.db.commit() self.db.commit()
else: else:
irc.error(msg, 'Error getting note') irc.error(msg, 'Error getting note')
def newnotes(self, irc, msg, args): def notes(self, irc, msg, args):
"takes no arguments, retrieves all unread notes for the requesting user" "takes no arguments, retrieves all unread notes for the requesting user"
sender = ircdb.users.getUserName(msg.prefix) sender = ircdb.users.getUserName(msg.prefix)
senderID = self.getUserID(sender) senderID = self.getUserID(sender)
self.cursor.execute("""SELECT id, from_id FROM notes self.cursor.execute("""SELECT id, from_id FROM notes
WHERE to_id=%d WHERE to_id=%s
AND read=0""", senderID) AND read=0""", senderID)
notes = self.cursor.fetchall() notes = self.cursor.fetchall()
L = [] L = []
for (id, from_id) in notes: for (id, from_id) in notes:
sender = self.getUserName(from_id) sender = self.getUserName(from_id)
L.append(r'#%d from %s;;' % (id, sender)) L.append(r'#%d from %s;;' % (id, sender))
debug.printf(L)
irc.reply(msg, "lookup was successful")
# def deletenote(self, irc, msg, args): # def deletenote(self, irc, msg, args):
# "removes single note using note id" # "removes single note using note id"
@ -182,18 +191,16 @@ class Notes(callbacks.Privmsg):
# else: # else:
# irc.error(msg, 'Unable to delete note') # irc.error(msg, 'Unable to delete note')
def getnotes(self, irc, msg, args): # def getnotes(self, irc, msg, args):
"""takes no arguments # "takes no arguments gets all notes for sender"
# sender = ircdb.users.getUserName(msg.prefix)
Gets all notes for sender.""" # senderID = self.getUserID(sender)
sender = ircdb.users.getUserName(msg.prefix) # self.cursor.execute("""SELECT id, from_id FROM notes
senderID = self.getUserID(sender) # WHERE to_id=%d""" % senderID)
self.cursor.execute("""SELECT id, from_id FROM notes # notes = self.cursor.fetchall()
WHERE to_id=%d""" % senderID) # L = []
notes = self.cursor.fetchall() # for (id, from_id) in notes:
L = [] # sender = self.getUserName(from_id)
for (id, from_id) in notes: # L.append(r'#%d from %s;;' % (id, sender))
sender = self.getUserName(from_id)
L.append(r'#%d from %s;;' % (id, sender))
Class = Notes Class = Notes