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-11-25 09:23:47 +01:00
|
|
|
__revision__ = "$Id$"
|
|
|
|
|
2003-10-05 14:56:56 +02:00
|
|
|
import plugins
|
2003-03-27 03:42:26 +01:00
|
|
|
|
2004-04-30 21:19:59 +02:00
|
|
|
import sets
|
2003-03-27 03:42:26 +01:00
|
|
|
import time
|
2004-01-31 23:05:09 +01:00
|
|
|
import getopt
|
2003-04-08 09:07:54 +02:00
|
|
|
import os.path
|
2003-11-15 05:46:09 +01:00
|
|
|
from itertools import imap
|
2003-03-27 03:42:26 +01:00
|
|
|
|
|
|
|
import conf
|
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-11-15 08:56:27 +01:00
|
|
|
import plugins
|
2003-03-27 03:42:26 +01:00
|
|
|
import privmsgs
|
2004-02-07 13:02:33 +01:00
|
|
|
import registry
|
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
|
|
|
|
2003-12-04 00:48:00 +01:00
|
|
|
try:
|
|
|
|
import sqlite
|
|
|
|
except ImportError:
|
|
|
|
raise callbacks.Error, 'You need to have PySQLite installed to use this ' \
|
|
|
|
'plugin. Download it at <http://pysqlite.sf.net/>'
|
|
|
|
|
2004-02-07 13:02:33 +01:00
|
|
|
|
|
|
|
conf.registerPlugin('Note')
|
|
|
|
conf.registerGlobalValue(conf.supybot.plugins.Note, 'notifyOnJoin',
|
|
|
|
registry.Boolean(False, """Determines whether the bot will notify people of
|
|
|
|
their new messages when they join the channel. Normally it will notify
|
|
|
|
them when they send a message to the channel, since oftentimes joins are
|
|
|
|
the result of netsplits and not the actual presence of the user."""))
|
|
|
|
conf.registerGlobalValue(conf.supybot.plugins.Note, 'notifyOnJoinRepeatedly',
|
|
|
|
registry.Boolean(False, """Determines whether the bot will repeatedly
|
|
|
|
notify people of their new messages when they join the channel. That means
|
|
|
|
when they join the channel, the bot will tell them they have unread
|
|
|
|
messages, even if it's told them before."""))
|
|
|
|
|
2004-04-30 21:19:59 +02:00
|
|
|
class Ignores(registry.SpaceSeparatedListOfStrings):
|
|
|
|
List = ircutils.IrcSet
|
|
|
|
|
|
|
|
conf.registerUserValue(conf.users.plugins.Note, 'ignores', Ignores([], ''))
|
|
|
|
|
2003-11-15 08:56:27 +01:00
|
|
|
class NoteDb(plugins.DBHandler):
|
|
|
|
def makeDb(self, filename):
|
|
|
|
"create Notes database and tables"
|
|
|
|
if os.path.exists(filename):
|
|
|
|
db = sqlite.connect(filename)
|
|
|
|
else:
|
|
|
|
db = sqlite.connect(filename, converters={'bool': bool})
|
|
|
|
cursor = db.cursor()
|
|
|
|
cursor.execute("""CREATE TABLE notes (
|
|
|
|
id INTEGER PRIMARY KEY,
|
|
|
|
from_id INTEGER,
|
|
|
|
to_id INTEGER,
|
|
|
|
added_at TIMESTAMP,
|
|
|
|
notified BOOLEAN,
|
|
|
|
read BOOLEAN,
|
|
|
|
public BOOLEAN,
|
|
|
|
note TEXT
|
|
|
|
)""")
|
|
|
|
db.commit()
|
|
|
|
return db
|
|
|
|
|
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)
|
2004-01-18 08:58:26 +01:00
|
|
|
dataDir = conf.supybot.directories.data()
|
|
|
|
self.dbHandler = NoteDb(name=os.path.join(dataDir, 'Notes'))
|
2003-08-20 18:26:23 +02:00
|
|
|
|
2003-09-14 04:43:17 +02:00
|
|
|
def setAsRead(self, id):
|
2003-11-15 08:56:27 +01:00
|
|
|
db = self.dbHandler.getDb()
|
|
|
|
cursor = db.cursor()
|
2003-08-19 09:48:43 +02:00
|
|
|
cursor.execute("""UPDATE notes
|
2003-09-14 04:43:17 +02:00
|
|
|
SET read=1, notified=1
|
|
|
|
WHERE id=%s""", id)
|
2003-11-15 08:56:27 +01:00
|
|
|
db.commit()
|
2003-04-03 09:11:16 +02:00
|
|
|
|
|
|
|
def die(self):
|
2003-12-02 23:40:33 +01:00
|
|
|
self.dbHandler.die()
|
2003-08-20 18:26:23 +02:00
|
|
|
|
2003-09-03 19:43:06 +02:00
|
|
|
def doPrivmsg(self, irc, msg):
|
2004-02-07 13:02:33 +01:00
|
|
|
self._notify(irc, msg)
|
|
|
|
|
|
|
|
def doJoin(self, irc, msg):
|
|
|
|
if self.registryValue('notifyOnJoin'):
|
|
|
|
repeatedly = self.registryValue('notifyOnJoinRepeatedly')
|
|
|
|
self._notify(irc, msg, repeatedly)
|
|
|
|
|
|
|
|
def _notify(self, irc, msg, repeatedly=False):
|
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-11-15 08:56:27 +01:00
|
|
|
db = self.dbHandler.getDb()
|
|
|
|
cursor = 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])
|
2004-02-07 13:02:33 +01:00
|
|
|
if unnotified != 0 or repeatedly:
|
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..' % \
|
2003-12-12 16:41:33 +01:00
|
|
|
(utils.nItems('note', unread, 'unread'), unnotified)
|
2003-09-05 21:01:02 +02:00
|
|
|
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-11-15 08:56:27 +01:00
|
|
|
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-11-11 14:20:06 +01:00
|
|
|
(name, note) = privmsgs.getArgs(args, required=2)
|
2003-03-27 03:42:26 +01:00
|
|
|
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:
|
2004-01-08 16:47:38 +01:00
|
|
|
irc.errorNoUser()
|
2003-09-14 04:43:17 +02:00
|
|
|
return
|
|
|
|
try:
|
|
|
|
fromId = ircdb.users.getUserId(msg.prefix)
|
2004-04-30 21:19:59 +02:00
|
|
|
senderName = ircdb.users.getUser(fromId).name
|
2003-09-14 04:43:17 +02:00
|
|
|
except KeyError:
|
2004-01-08 22:49:10 +01:00
|
|
|
irc.errorNotRegistered()
|
2003-09-14 04:43:17 +02:00
|
|
|
return
|
2004-04-30 21:19:59 +02:00
|
|
|
if senderName in self.userValue('ignores', name):
|
|
|
|
irc.error('That user has ignored messages from you.')
|
|
|
|
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-11-15 08:56:27 +01:00
|
|
|
db = self.dbHandler.getDb()
|
|
|
|
cursor = 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-11-15 08:56:27 +01:00
|
|
|
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]
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply('Note #%s sent to %s.' % (id, name))
|
2003-03-27 03:42:26 +01:00
|
|
|
|
2003-12-03 22:00:56 +01:00
|
|
|
def unsend(self, irc, msg, args):
|
|
|
|
"""<id>
|
|
|
|
|
|
|
|
Unsends the note with the id given. You must be the
|
|
|
|
author of the note, and it must be unread.
|
|
|
|
"""
|
|
|
|
id = privmsgs.getArgs(args)
|
|
|
|
try:
|
|
|
|
userid = ircdb.users.getUserId(msg.prefix)
|
|
|
|
except KeyError:
|
2004-01-08 22:49:10 +01:00
|
|
|
irc.errorNotRegistered()
|
2003-12-03 22:00:56 +01:00
|
|
|
return
|
|
|
|
db = self.dbHandler.getDb()
|
|
|
|
cursor = db.cursor()
|
|
|
|
cursor.execute("""SELECT from_id, read FROM notes WHERE id=%s""", id)
|
2004-02-01 22:47:11 +01:00
|
|
|
if cursor.rowcount < 1:
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.error('That\'s not a valid note id.')
|
2003-12-03 22:00:56 +01:00
|
|
|
return
|
|
|
|
(from_id, read) = map(int, cursor.fetchone())
|
|
|
|
if from_id == userid:
|
|
|
|
if not read:
|
|
|
|
cursor.execute("""DELETE FROM notes WHERE id=%s""", id)
|
|
|
|
db.commit()
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.replySuccess()
|
2003-12-03 22:00:56 +01:00
|
|
|
else:
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.error('That note has been read already.')
|
2003-12-03 22:00:56 +01:00
|
|
|
else:
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.error('That note wasn\'t sent by you.')
|
2003-12-03 22:00:56 +01:00
|
|
|
|
|
|
|
|
2004-01-03 16:59:51 +01:00
|
|
|
def note(self, irc, msg, args):
|
2003-04-03 07:48:57 +02:00
|
|
|
"""<note id>
|
2003-08-20 18:26:23 +02:00
|
|
|
|
2004-06-25 16:14:39 +02:00
|
|
|
Retrieves a single note by its unique note id. Use the 'note list'
|
|
|
|
command to see what unread notes you have.
|
2003-04-08 09:07:54 +02:00
|
|
|
"""
|
2003-03-27 07:07:21 +01:00
|
|
|
noteid = privmsgs.getArgs(args)
|
2004-01-31 22:25:42 +01:00
|
|
|
if noteid.startswith('get'):
|
|
|
|
irc.error('The Note.get command has changed to be simply "note".')
|
|
|
|
return
|
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:
|
2004-01-08 22:49:10 +01:00
|
|
|
irc.errorNotRegistered()
|
2003-04-19 23:40:04 +02:00
|
|
|
return
|
2004-01-06 03:40:16 +01:00
|
|
|
try:
|
|
|
|
noteid = int(noteid)
|
|
|
|
except ValueError:
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.error('%r is not a valid note id.' % noteid)
|
2004-01-06 03:40:16 +01:00
|
|
|
return
|
2003-11-15 08:56:27 +01:00
|
|
|
db = self.dbHandler.getDb()
|
|
|
|
cursor = 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)
|
2004-02-01 22:47:11 +01:00
|
|
|
if cursor.rowcount < 1:
|
2003-10-26 18:49:11 +01:00
|
|
|
s = 'You may only retrieve notes you\'ve sent or received.'
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.error(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()
|
2003-11-15 05:46:09 +01:00
|
|
|
(toId,fromId,addedAt,public) = imap(int, (toId,fromId,addedAt,public))
|
2003-09-14 04:43:17 +02:00
|
|
|
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)
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply(newnote, private=(not public))
|
2003-04-14 17:10:38 +02:00
|
|
|
self.setAsRead(noteid)
|
2003-03-27 07:07:21 +01:00
|
|
|
|
2004-01-31 23:05:09 +01:00
|
|
|
def _formatNoteData(self, msg, id, fromId, public, sent=False):
|
2003-11-15 05:46:09 +01:00
|
|
|
(id, fromId, public) = imap(int, (id, fromId, public))
|
2003-09-14 04:43:17 +02:00
|
|
|
if public or not ircutils.isChannel(msg.args[0]):
|
|
|
|
sender = ircdb.users.getUser(fromId).name
|
2004-01-31 23:05:09 +01:00
|
|
|
if sent:
|
|
|
|
return '#%s to %s' % (id, sender)
|
|
|
|
else:
|
|
|
|
return '#%s from %s' % (id, sender)
|
2003-09-14 04:43:17 +02:00
|
|
|
else:
|
|
|
|
return '#%s (private)' % id
|
|
|
|
|
2004-04-30 21:19:59 +02:00
|
|
|
def ignore(self, irc, msg, args):
|
|
|
|
"""[--remove] <user>
|
|
|
|
|
|
|
|
Ignores all messages from <user>. If --remove is listed, remove <user>
|
|
|
|
from the list of users being ignored.
|
|
|
|
"""
|
|
|
|
remove = False
|
|
|
|
while '--remove' in args:
|
|
|
|
remove = True
|
|
|
|
args.remove('--remove')
|
|
|
|
user = privmsgs.getArgs(args)
|
|
|
|
try:
|
|
|
|
L = self.userValue('ignores', msg.prefix)
|
|
|
|
if remove:
|
|
|
|
try:
|
|
|
|
L.remove(user)
|
|
|
|
except (KeyError, ValueError):
|
|
|
|
irc.error('%r was not in your list of ignores.' % user)
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
L.add(user)
|
|
|
|
self.setUserValue(msg.prefix, 'ignores', L, setValue=True)
|
|
|
|
irc.replySuccess()
|
|
|
|
except KeyError:
|
|
|
|
irc.errorNoUser()
|
|
|
|
|
2003-11-11 13:59:27 +01:00
|
|
|
def list(self, irc, msg, args):
|
2004-02-01 22:47:11 +01:00
|
|
|
"""[--{old,sent}] [--{from,to} <user>]
|
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
|
2004-02-01 22:47:11 +01:00
|
|
|
read notes. If --sent is given, list notes that you have sent. If
|
|
|
|
--from is specified, only lists notes sent to you from <user>. If
|
|
|
|
--to is specified, only lists notes sent by you to <user>.
|
2003-04-08 11:11:33 +02:00
|
|
|
"""
|
2004-02-01 22:47:11 +01:00
|
|
|
options = ['old', 'sent', 'from=', 'to=']
|
2004-01-31 23:05:09 +01:00
|
|
|
(optlist, rest) = getopt.getopt(args, '', options)
|
2004-02-01 22:47:11 +01:00
|
|
|
sender, receiver, old, sent = ('', '', False, False)
|
|
|
|
for (option, arg) in optlist:
|
2004-01-31 23:05:09 +01:00
|
|
|
option = option.lstrip('-')
|
|
|
|
if option == 'old':
|
2004-02-01 22:47:11 +01:00
|
|
|
old = True
|
2004-01-31 23:05:09 +01:00
|
|
|
if option == 'sent':
|
2004-02-01 22:47:11 +01:00
|
|
|
sent = True
|
|
|
|
if option == 'from':
|
|
|
|
sender = arg
|
|
|
|
if option == 'to':
|
|
|
|
receiver = arg
|
|
|
|
sent = True
|
|
|
|
if old:
|
|
|
|
return self._oldnotes(irc, msg, sender)
|
|
|
|
if sent:
|
|
|
|
return self._sentnotes(irc, msg, receiver)
|
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:
|
2004-01-08 22:49:10 +01:00
|
|
|
irc.errorNotRegistered()
|
2003-04-08 09:07:54 +02:00
|
|
|
return
|
2004-02-01 22:47:11 +01:00
|
|
|
sql = """SELECT id, from_id, public
|
|
|
|
FROM notes
|
|
|
|
WHERE notes.to_id=%r AND notes.read=0""" % id
|
|
|
|
if sender:
|
|
|
|
try:
|
|
|
|
sender = ircdb.users.getUserId(sender)
|
|
|
|
except KeyError:
|
|
|
|
irc.error('That user is not in my user database.')
|
|
|
|
return
|
|
|
|
sql = '%s %s' % (sql, 'AND notes.from_id=%r' % sender)
|
2003-11-15 08:56:27 +01:00
|
|
|
db = self.dbHandler.getDb()
|
|
|
|
cursor = db.cursor()
|
2004-02-01 22:47:11 +01:00
|
|
|
cursor.execute(sql)
|
2003-08-19 09:48:43 +02:00
|
|
|
count = cursor.rowcount
|
2004-02-01 22:47:11 +01:00
|
|
|
if count < 1:
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply('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()]
|
2004-02-01 22:47:11 +01:00
|
|
|
L = self._condense(L)
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply(utils.commaAndify(L))
|
2003-03-27 07:07:21 +01:00
|
|
|
|
2004-02-01 22:47:11 +01:00
|
|
|
def _condense(self, notes):
|
|
|
|
temp = {}
|
|
|
|
for note in notes:
|
|
|
|
note = note.split(' ', 1)
|
|
|
|
if note[1] in temp:
|
|
|
|
temp[note[1]].append(note[0])
|
|
|
|
else:
|
|
|
|
temp[note[1]] = [note[0]]
|
|
|
|
notes = []
|
|
|
|
for (k,v) in temp.iteritems():
|
2004-02-17 03:41:09 +01:00
|
|
|
notes.append('%s %s' % (utils.commaAndify(v), k))
|
2004-02-01 22:47:11 +01:00
|
|
|
return notes
|
|
|
|
|
|
|
|
def _sentnotes(self, irc, msg, receiver):
|
2004-01-31 23:05:09 +01:00
|
|
|
"""takes no arguments
|
|
|
|
|
|
|
|
Returns a list of your most recent old notes.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
id = ircdb.users.getUserId(msg.prefix)
|
|
|
|
except KeyError:
|
|
|
|
irc.errorNotRegistered()
|
|
|
|
return
|
2004-02-01 22:47:11 +01:00
|
|
|
sql = """SELECT id, to_id, public
|
|
|
|
FROM notes
|
|
|
|
WHERE notes.from_id=%r""" % id
|
|
|
|
if receiver:
|
|
|
|
try:
|
|
|
|
receiver = ircdb.users.getUserId(receiver)
|
|
|
|
except KeyError:
|
|
|
|
irc.error('That user is not in my user database.')
|
|
|
|
return
|
|
|
|
sql = '%s %s' % (sql, 'AND notes.to_id=%r' % receiver)
|
2004-02-17 03:41:09 +01:00
|
|
|
sql = '%s ORDER BY id DESC' % sql
|
2004-01-31 23:05:09 +01:00
|
|
|
db = self.dbHandler.getDb()
|
|
|
|
cursor = db.cursor()
|
2004-02-01 22:47:11 +01:00
|
|
|
cursor.execute(sql)
|
|
|
|
if cursor.rowcount < 1:
|
2004-01-31 23:05:09 +01:00
|
|
|
irc.reply('I couldn\'t find any sent notes for your user.')
|
|
|
|
else:
|
|
|
|
ids = [self._formatNoteData(msg, sent=True, *t) for t in
|
|
|
|
cursor.fetchall()]
|
2004-02-01 22:47:11 +01:00
|
|
|
ids = self._condense(ids)
|
2004-01-31 23:05:09 +01:00
|
|
|
irc.reply(utils.commaAndify(ids))
|
|
|
|
|
2004-02-01 22:47:11 +01:00
|
|
|
def _oldnotes(self, irc, msg, sender):
|
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:
|
2004-01-08 22:49:10 +01:00
|
|
|
irc.errorNotRegistered()
|
2003-04-19 23:40:04 +02:00
|
|
|
return
|
2004-02-01 22:47:11 +01:00
|
|
|
sql = """SELECT id, from_id, public
|
|
|
|
FROM notes
|
|
|
|
WHERE notes.to_id=%r AND notes.read=1""" % id
|
|
|
|
#self.log.warning(sender)
|
|
|
|
if sender:
|
|
|
|
try:
|
|
|
|
sender = ircdb.users.getUserId(sender)
|
|
|
|
except KeyError:
|
|
|
|
irc.error('That user is not in my user database.')
|
|
|
|
return
|
|
|
|
sql = '%s %s' % (sql, 'AND notes.from_id=%r' % sender)
|
2004-02-17 03:41:09 +01:00
|
|
|
sql = '%s ORDER BY id DESC' % sql
|
2003-11-15 08:56:27 +01:00
|
|
|
db = self.dbHandler.getDb()
|
|
|
|
cursor = db.cursor()
|
2004-02-01 22:47:11 +01:00
|
|
|
cursor.execute(sql)
|
|
|
|
#self.log.warning(cursor.rowcount)
|
|
|
|
if cursor.rowcount < 1:
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply('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()]
|
2004-02-01 22:47:11 +01:00
|
|
|
#self.log.warning(ids)
|
|
|
|
ids = self._condense(ids)
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply(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:
|