gettin' there, slowly...

This commit is contained in:
Brett Kelly 2003-03-28 06:06:19 +00:00
parent e99df4442b
commit a4ff3abea7

View File

@ -41,6 +41,7 @@ import ircdb
import privmsgs import privmsgs
import callbacks import callbacks
import ircutils import ircutils
import debug
class Notes(callbacks.Privmsg): class Notes(callbacks.Privmsg):
@ -75,11 +76,11 @@ 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]
@ -87,38 +88,34 @@ class Notes(callbacks.Privmsg):
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:
results = self.cursor.fetchall() results = self.cursor.fetchall()
return results[0] return results[0]
else: else:
return "ERROR LOOKING UP USERNAME" raise KeyError
def setNoteUnread(self, irc, msg, args): # def setNoteUnread(self, irc, msg, args):
"set a note as unread" # "set a note as unread"
noteid = privmsgs.getArgs(args) # noteid = privmsgs.getArgs(args)
self.cursor.execute('UPDATE notes SET read=False where id=%s'% noteid) # self.cursor.execute('UPDATE notes SET read=False where id=%s'% noteid)
self.db.commit() # self.db.commit()
irc.reply(msg, conf.replySuccess) # irc.reply(msg, conf.replySuccess)
def sendnote(self, irc, msg, args): def sendnote(self, irc, msg, args):
"sends a new note to an IRC user" "sends a new note to an IRC user"
# sendnote <user> <text> # sendnote <user> <text>
(name, note) = privmsgs.getArgs(args, needed=2) (name, note) = privmsgs.getArgs(args, needed=2)
sender = ircdb.users.getUserName(msg.prefix) sender = ircutils.nickFromHostmask(msg.prefix)
if ircdb.users.hasUser(name): if ircdb.users.hasUser(name):
recipient = name recipient = name
else: else:
n = irc.state.nickToHostmask(name) n = irc.state.nickToHostmask(name)
recipient = ircdb.users.getUserName(n) recipient = ircdb.users.getUserName(n)
self._addUser(sender) self._addUser(sender)
print "added sender: %s" % sender
self._addUser(recipient) self._addUser(recipient)
print "added recipient: %s" % recipient
senderID = self.getUserID(sender) senderID = self.getUserID(sender)
print "senderID: %s" % senderID
recipID = self.getUserID(recipient) recipID = self.getUserID(recipient)
print "recipID: %s" % recipID
if ircutils.isChannel(msg.args[0]): public = True if ircutils.isChannel(msg.args[0]): public = True
else: public = False else: public = False
self.cursor.execute("""INSERT INTO notes VALUES self.cursor.execute("""INSERT INTO notes VALUES
@ -130,21 +127,22 @@ class Notes(callbacks.Privmsg):
def getnote(self, irc, msg, args): def getnote(self, irc, msg, args):
"retrieves a single note by unique note id" "retrieves a single note by unique note id"
# getnote 136 # 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) senderID = self.getUserID(sender)[0]
self.cursor.execute("""SELECT note, to_id, public FROM notes self.cursor.execute("""SELECT note, to_id, public FROM notes
WHERE id=%d LIMIT 1""" % int(noteid)) WHERE id=%s LIMIT 1""", noteid)
note, to_id, public = self.cursor.fetchall() note, to_id, public = self.cursor.fetchone()
debug.printf("senderID: %s" % senderID)
if senderID == to_id: if senderID == to_id:
if public == 'True': if public == 'True':
irc.reply(msg, note) irc.reply(msg, note)
else: else:
msg.args[0] = msg.nick msg.args[0] = msg.nick
irc.reply(msg, note) irc.reply(msg, note)
self.cursor.execute("""UPDATE notes SET read='True' self.cursor.execute("""UPDATE notes SET read=%s
WHERE id=%d""" % noteid) WHERE id=%s""", ('True', noteid))
self.db.commit() self.db.commit()
else: else:
irc.error(msg, 'Error getting note') irc.error(msg, 'Error getting note')
@ -155,28 +153,28 @@ class Notes(callbacks.Privmsg):
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=%d
AND read='False'""" % senderID) AND read='False'""", 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))
def deletenote(self, irc, msg, args): # def deletenote(self, irc, msg, args):
"removes single note using note id" # "removes single note using note id"
noteid = privmsgs.getArgs(args) # noteid = privmsgs.getArgs(args)
sender = ircdb.users.getUserName(msg.prefix) # sender = ircdb.users.getUserName(msg.prefix)
senderID = self.getUserID(sender) # senderID = self.getUserID(sender)
self.cursor.execute("""SELECT to_id FROM notes # self.cursor.execute("""SELECT to_id FROM notes
WHERE id=%d""" % int(noteid)) # WHERE id=%d""" % int(noteid))
to_id = self.cursor.fetchall() # to_id = self.cursor.fetchall()
if senderID == to_id: # if senderID == to_id:
self.cursor.execute("""DELETE FROM notes # self.cursor.execute("""DELETE FROM notes
WHERE id=%d""" % noteid) # WHERE id=%d""" % noteid)
self.db.commit() # self.db.commit()
irc.reply(msg, conf.replySuccess) # irc.reply(msg, conf.replySuccess)
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 gets all notes for sender" "takes no arguments gets all notes for sender"