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$"
|
|
|
|
|
2004-07-24 07:18:26 +02:00
|
|
|
import supybot.plugins as plugins
|
2003-03-27 03:42:26 +01:00
|
|
|
|
2004-08-07 02:41:55 +02:00
|
|
|
import csv
|
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
|
|
|
|
2004-07-24 07:18:26 +02:00
|
|
|
import supybot.conf as conf
|
|
|
|
import supybot.utils as utils
|
|
|
|
import supybot.ircdb as ircdb
|
|
|
|
import supybot.ircmsgs as ircmsgs
|
|
|
|
import supybot.plugins as plugins
|
|
|
|
import supybot.privmsgs as privmsgs
|
|
|
|
import supybot.registry as registry
|
|
|
|
import supybot.ircutils as ircutils
|
|
|
|
import supybot.callbacks as callbacks
|
2003-04-08 09:07:54 +02:00
|
|
|
|
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-07-20 09:03:15 +02:00
|
|
|
conf.registerUserValue(conf.users.plugins.Note, 'notifyWithNotice',
|
|
|
|
registry.Boolean(False, """Determines whether the bot will notify users of
|
|
|
|
new messages with a NOTICE, rather than a PRIVMSG, so their clients won't
|
|
|
|
open a new query window."""))
|
2004-02-07 13:02:33 +01:00
|
|
|
|
2004-04-30 21:19:59 +02:00
|
|
|
class Ignores(registry.SpaceSeparatedListOfStrings):
|
|
|
|
List = ircutils.IrcSet
|
2004-07-21 21:06:49 +02:00
|
|
|
|
2004-04-30 21:19:59 +02:00
|
|
|
conf.registerUserValue(conf.users.plugins.Note, 'ignores', Ignores([], ''))
|
|
|
|
|
2004-08-07 02:41:55 +02:00
|
|
|
class FlatfileNoteDB(plugins.FlatfileDB):
|
|
|
|
class Note(object):
|
|
|
|
def __init__(self, L=None, to=None, frm=None, text=None):
|
|
|
|
if L is not None:
|
|
|
|
self.frm = int(L[0])
|
|
|
|
self.to = int(L[1])
|
|
|
|
self.at = float(L[2])
|
|
|
|
self.notified = bool(int(L[3]))
|
|
|
|
self.read = bool(int(L[4]))
|
|
|
|
self.public = bool(int(L[5]))
|
|
|
|
self.text = L[6]
|
|
|
|
else:
|
|
|
|
self.to = to
|
|
|
|
self.frm = frm
|
|
|
|
self.text = text
|
|
|
|
self.read = False
|
|
|
|
self.public = True
|
|
|
|
self.at = time.time()
|
|
|
|
self.notified = False
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return csv.join(map(str, [self.frm, self.to, self.at,
|
|
|
|
int(self.notified), int(self.read),
|
|
|
|
int(self.public), self.text]))
|
|
|
|
|
|
|
|
def serialize(self, n):
|
|
|
|
return str(n)
|
|
|
|
|
|
|
|
def deserialize(self, s):
|
|
|
|
return self.Note(csv.split(s))
|
|
|
|
|
|
|
|
def setRead(self, id):
|
|
|
|
n = self.getRecord(id)
|
|
|
|
n.read = True
|
|
|
|
n.notified = True
|
|
|
|
self.setRecord(id, n)
|
|
|
|
|
|
|
|
def setNotified(self, id):
|
|
|
|
n = self.getRecord(id)
|
|
|
|
n.notified = True
|
|
|
|
self.setRecord(id, n)
|
|
|
|
|
|
|
|
def getUnnotifiedIds(self, to):
|
|
|
|
L = []
|
|
|
|
for (id, note) in self.records():
|
|
|
|
if note.to == to and not note.notified:
|
|
|
|
L.append(id)
|
|
|
|
return L
|
|
|
|
|
|
|
|
def getUnreadIds(self, to):
|
|
|
|
L = []
|
|
|
|
for (id, note) in self.records():
|
|
|
|
if note.to == to and not note.read:
|
|
|
|
L.append(id)
|
|
|
|
return L
|
|
|
|
|
|
|
|
def send(self, frm, to, text):
|
|
|
|
n = self.Note(frm=frm, to=to, text=text)
|
|
|
|
return self.addRecord(n)
|
|
|
|
|
|
|
|
def get(self, id):
|
|
|
|
return self.getRecord(id)
|
|
|
|
|
|
|
|
def remove(self, id):
|
|
|
|
n = self.getRecord(id)
|
|
|
|
assert not n.read
|
|
|
|
self.delRecord(id)
|
|
|
|
|
|
|
|
def notes(self, p):
|
|
|
|
L = []
|
|
|
|
for (id, note) in self.records():
|
|
|
|
if p(note):
|
|
|
|
L.append((id, note))
|
|
|
|
return L
|
|
|
|
|
|
|
|
|
|
|
|
def NoteDB():
|
|
|
|
return FlatfileNoteDB(conf.supybot.directories.data.dirize('Note.db'))
|
|
|
|
|
2004-07-21 21:06:49 +02: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)
|
2004-08-07 02:41:55 +02:00
|
|
|
self.db = NoteDB()
|
2003-04-03 09:11:16 +02:00
|
|
|
|
|
|
|
def die(self):
|
2004-08-07 02:41:55 +02:00
|
|
|
self.db.close()
|
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:
|
2004-08-07 02:41:55 +02:00
|
|
|
to = ircdb.users.getUserId(msg.prefix)
|
2003-04-16 09:21:06 +02:00
|
|
|
except KeyError:
|
2003-09-08 10:58:00 +02:00
|
|
|
return
|
2004-08-07 02:41:55 +02:00
|
|
|
unnotifiedIds = ['#%s' % nid for nid in self.db.getUnnotifiedIds(to)]
|
2004-07-28 22:25:11 +02:00
|
|
|
unnotified = len(unnotifiedIds)
|
2004-08-07 02:41:55 +02:00
|
|
|
if unnotified or repeatedly:
|
|
|
|
unreadIds = ['#%s' % nid for nid in self.db.getUnreadIds(to)]
|
2004-07-28 22:25:11 +02:00
|
|
|
unread = len(unreadIds)
|
|
|
|
s = 'You have %s; %s that I haven\'t told you about before now. '\
|
|
|
|
'%s %s still unread.' % \
|
|
|
|
(utils.nItems('note', unread, 'unread'), unnotified,
|
|
|
|
utils.commaAndify(unreadIds), utils.be(unread))
|
2004-08-07 02:41:55 +02:00
|
|
|
msgmaker = ircmsgs.privmsg
|
2004-07-20 09:03:15 +02:00
|
|
|
if self.userValue('notifyWithNotice', msg.prefix):
|
2004-08-07 02:41:55 +02:00
|
|
|
msgmaker = ircmsgs.notice
|
|
|
|
irc.queueMsg(msgmaker(msg.nick, s))
|
|
|
|
for nid in unnotifiedIds:
|
|
|
|
id = int(nid[1:])
|
|
|
|
self.db.setNotified(id)
|
2003-08-15 05:00:59 +02:00
|
|
|
|
2004-08-07 02:41:55 +02:00
|
|
|
def _getUserId(self, irc, name):
|
2003-03-27 03:42:26 +01:00
|
|
|
if ircdb.users.hasUser(name):
|
2004-07-20 09:03:15 +02:00
|
|
|
return ircdb.users.getUserId(name)
|
2003-03-27 03:42:26 +01:00
|
|
|
else:
|
2003-09-14 04:43:17 +02:00
|
|
|
try:
|
2004-07-21 21:06:49 +02:00
|
|
|
hostmask = irc.state.nickToHostmask(name)
|
2004-07-20 09:03:15 +02:00
|
|
|
return ircdb.users.getUserId(hostmask)
|
2003-09-14 04:43:17 +02:00
|
|
|
except KeyError:
|
2004-07-20 09:03:15 +02:00
|
|
|
return None
|
2004-07-21 21:06:49 +02:00
|
|
|
|
2004-08-07 02:41:55 +02:00
|
|
|
def _validId(self, irc, id):
|
|
|
|
try:
|
|
|
|
id = id.lstrip('#')
|
|
|
|
return int(id)
|
|
|
|
except ValueError:
|
|
|
|
irc.error('That\'s not a valid note id.')
|
|
|
|
return None
|
|
|
|
|
2004-07-20 09:03:15 +02:00
|
|
|
def send(self, irc, msg, args):
|
|
|
|
"""<recipient>,[<recipient>,[...]] <text>
|
|
|
|
|
|
|
|
Sends a new note to the user specified. Multiple recipients may be
|
|
|
|
specified by separating their names by commas, with *no* spaces
|
|
|
|
between.
|
|
|
|
"""
|
2004-08-07 02:41:55 +02:00
|
|
|
(names, text) = privmsgs.getArgs(args, required=2)
|
2004-07-20 09:03:15 +02:00
|
|
|
# Let's get the from user.
|
2003-09-14 04:43:17 +02:00
|
|
|
try:
|
|
|
|
fromId = ircdb.users.getUserId(msg.prefix)
|
|
|
|
except KeyError:
|
2004-01-08 22:49:10 +01:00
|
|
|
irc.errorNotRegistered()
|
2003-09-14 04:43:17 +02:00
|
|
|
return
|
2004-08-07 02:41:55 +02:00
|
|
|
public = ircutils.isChannel(msg.args[0])
|
2004-07-20 09:03:15 +02:00
|
|
|
names = names.split(',')
|
2004-08-07 02:41:55 +02:00
|
|
|
ids = [self._getUserId(irc, name) for name in names]
|
2004-07-20 09:03:15 +02:00
|
|
|
badnames = []
|
2004-08-07 02:41:55 +02:00
|
|
|
# Make sure all targets are registered.
|
2004-07-20 09:03:15 +02:00
|
|
|
if None in ids:
|
|
|
|
for (id, name) in zip(ids, names):
|
|
|
|
if id is None:
|
|
|
|
badnames.append(name)
|
|
|
|
irc.errorNoUser(name=utils.commaAndify(badnames, And='or'))
|
|
|
|
return
|
2004-07-21 21:06:49 +02:00
|
|
|
|
2004-08-07 02:41:55 +02:00
|
|
|
# Make sure the sender isn't being ignored.
|
|
|
|
senderName = ircdb.users.getUser(fromId).name
|
2004-07-20 09:03:15 +02:00
|
|
|
for name in names:
|
|
|
|
if senderName in self.userValue('ignores', name):
|
|
|
|
badnames.append(name)
|
|
|
|
if badnames:
|
|
|
|
irc.error('%s %s ignoring notes from you.' % \
|
|
|
|
(utils.commaAndify(badnames), utils.be(len(badnames))))
|
|
|
|
return
|
|
|
|
sent = []
|
2004-08-07 02:41:55 +02:00
|
|
|
for toId in ids:
|
|
|
|
id = self.db.send(fromId, toId, text)
|
|
|
|
name = ircdb.users.getUser(toId).name
|
|
|
|
s = 'note #%s sent to %s' % (id, name)
|
2004-07-20 09:03:15 +02:00
|
|
|
sent.append(s)
|
|
|
|
irc.reply(utils.commaAndify(sent).capitalize() + '.')
|
2003-03-27 03:42:26 +01:00
|
|
|
|
2004-08-08 18:50:09 +02:00
|
|
|
def reply(self, irc, msg, args):
|
|
|
|
"""<id> <text>
|
|
|
|
|
|
|
|
Sends a note in reply to <id>.
|
|
|
|
"""
|
|
|
|
if not args:
|
|
|
|
raise callbacks.ArgumentError
|
|
|
|
id = self._validId(irc, args[0])
|
|
|
|
if not id:
|
|
|
|
return
|
|
|
|
args.append('(in reply to #%s)' % id)
|
|
|
|
note = self.db.get(id)
|
|
|
|
to = self.db.get(id).frm
|
|
|
|
self.db.setRead(id)
|
|
|
|
try:
|
|
|
|
args[0] = ircdb.users.getUser(to).name
|
|
|
|
except KeyError:
|
|
|
|
irc.error('Odd, the user you\'re replying to is no longer in the '
|
|
|
|
'database. You should notify my owner about this.')
|
|
|
|
return
|
|
|
|
self.send(irc, msg, args)
|
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
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
|
2004-08-07 02:41:55 +02:00
|
|
|
id = privmsgs.getArgs(args)
|
|
|
|
id = self._validId(irc, id)
|
|
|
|
if id is None:
|
2003-12-03 22:00:56 +01:00
|
|
|
return
|
2004-08-07 02:41:55 +02:00
|
|
|
note = self.db.get(id)
|
|
|
|
if note.frm == userid:
|
|
|
|
if not note.read:
|
|
|
|
self.db.remove(id)
|
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.')
|
2004-07-21 21:06:49 +02: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-04-19 23:40:04 +02:00
|
|
|
try:
|
2004-08-07 02:41:55 +02:00
|
|
|
userid = 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-08-07 02:41:55 +02:00
|
|
|
id = privmsgs.getArgs(args)
|
|
|
|
id = self._validId(irc, id)
|
|
|
|
if id is None:
|
|
|
|
return
|
2004-01-06 03:40:16 +01:00
|
|
|
try:
|
2004-08-07 02:41:55 +02:00
|
|
|
note = self.db.get(id)
|
|
|
|
except KeyError:
|
|
|
|
irc.error('That\'s not a valid note id.')
|
2004-01-06 03:40:16 +01:00
|
|
|
return
|
2004-08-07 02:41:55 +02:00
|
|
|
if userid != note.frm and userid != note.to:
|
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
|
2004-08-07 02:41:55 +02:00
|
|
|
elapsed = utils.timeElapsed(time.time() - note.at)
|
|
|
|
if note.to == userid:
|
|
|
|
author = ircdb.users.getUser(note.frm).name
|
|
|
|
newnote = '%s (Sent by %s %s ago)' % (note.text, author, elapsed)
|
|
|
|
elif note.frm == userid:
|
|
|
|
recipient = ircdb.users.getUser(note.to).name
|
|
|
|
newnote = '%s (Sent to %s %s ago)' % (note.text, recipient,elapsed)
|
|
|
|
irc.reply(newnote, private=(not note.public))
|
|
|
|
self.db.setRead(id)
|
2003-09-14 04:43:17 +02:00
|
|
|
|
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)
|
2004-07-28 08:12:02 +02:00
|
|
|
self.setUserValue('ignores', msg.prefix, L)
|
2004-04-30 21:19:59 +02:00
|
|
|
irc.replySuccess()
|
|
|
|
except KeyError:
|
|
|
|
irc.errorNoUser()
|
2004-07-21 21:06:49 +02:00
|
|
|
|
2004-08-07 02:41:55 +02:00
|
|
|
def _formatNoteId(self, msg, id, frm, public, sent=False):
|
|
|
|
if public or not ircutils.isChannel(msg.args[0]):
|
|
|
|
sender = ircdb.users.getUser(frm).name
|
|
|
|
if sent:
|
|
|
|
return '#%s to %s' % (id, sender)
|
|
|
|
else:
|
|
|
|
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):
|
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-08-07 02:41:55 +02:00
|
|
|
if option == '--old':
|
2004-02-01 22:47:11 +01:00
|
|
|
old = True
|
2004-08-07 02:41:55 +02:00
|
|
|
if option == '--sent':
|
2004-02-01 22:47:11 +01:00
|
|
|
sent = True
|
2004-08-07 02:41:55 +02:00
|
|
|
if option == '--from':
|
2004-02-01 22:47:11 +01:00
|
|
|
sender = arg
|
2004-08-07 02:41:55 +02:00
|
|
|
if option == '--to':
|
2004-02-01 22:47:11 +01:00
|
|
|
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:
|
2004-08-07 02:41:55 +02:00
|
|
|
userid = 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-08-07 02:41:55 +02:00
|
|
|
|
|
|
|
def p(note):
|
|
|
|
return not note.read and note.to == userid
|
2004-02-01 22:47:11 +01:00
|
|
|
if sender:
|
|
|
|
try:
|
|
|
|
sender = ircdb.users.getUserId(sender)
|
2004-08-07 02:41:55 +02:00
|
|
|
originalP = p
|
|
|
|
def p(note):
|
|
|
|
return originalP(note) and note.frm == sender
|
2004-02-01 22:47:11 +01:00
|
|
|
except KeyError:
|
2004-08-07 02:41:55 +02:00
|
|
|
irc.errorNoUser()
|
2004-02-01 22:47:11 +01:00
|
|
|
return
|
2004-08-07 02:41:55 +02:00
|
|
|
notesAndIds = self.db.notes(p)
|
|
|
|
notesAndIds.sort()
|
2004-08-07 02:48:03 +02:00
|
|
|
#notesAndIds.reverse() # Newer notes, higher ids.
|
2004-08-07 02:41:55 +02:00
|
|
|
if not notesAndIds:
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply('You have no unread notes.')
|
2003-04-02 10:30:12 +02:00
|
|
|
else:
|
2004-08-07 02:41:55 +02:00
|
|
|
L = [self._formatNoteId(msg, id, n.frm, n.public)
|
|
|
|
for (id, n) in notesAndIds]
|
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
|
|
|
try:
|
2004-08-07 02:41:55 +02:00
|
|
|
userid = ircdb.users.getUserId(msg.prefix)
|
2004-01-31 23:05:09 +01:00
|
|
|
except KeyError:
|
|
|
|
irc.errorNotRegistered()
|
|
|
|
return
|
2004-08-07 02:41:55 +02:00
|
|
|
def p(note):
|
|
|
|
return note.frm == userid
|
2004-02-01 22:47:11 +01:00
|
|
|
if receiver:
|
|
|
|
try:
|
|
|
|
receiver = ircdb.users.getUserId(receiver)
|
|
|
|
except KeyError:
|
|
|
|
irc.error('That user is not in my user database.')
|
|
|
|
return
|
2004-08-07 02:41:55 +02:00
|
|
|
originalP = p
|
|
|
|
def p(note):
|
|
|
|
return originalP(note) and note.to == receiver
|
|
|
|
notesAndIds = self.db.notes(p)
|
|
|
|
notesAndIds.sort()
|
2004-08-07 02:48:03 +02:00
|
|
|
notesAndIds.reverse()
|
2004-08-07 02:41:55 +02:00
|
|
|
if not notesAndIds:
|
|
|
|
irc.error('I couldn\'t find any sent notes for your user.')
|
2004-01-31 23:05:09 +01:00
|
|
|
else:
|
2004-08-07 02:41:55 +02:00
|
|
|
ids = [self._formatNoteId(msg, id, note.to, note.public,sent=True)
|
|
|
|
for (id, note) in notesAndIds]
|
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
|
|
|
try:
|
2004-08-07 02:41:55 +02:00
|
|
|
userid = 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-08-07 02:41:55 +02:00
|
|
|
def p(note):
|
|
|
|
return note.to == userid and note.read
|
2004-02-01 22:47:11 +01:00
|
|
|
if sender:
|
|
|
|
try:
|
|
|
|
sender = ircdb.users.getUserId(sender)
|
|
|
|
except KeyError:
|
|
|
|
irc.error('That user is not in my user database.')
|
|
|
|
return
|
2004-08-07 02:41:55 +02:00
|
|
|
originalP = p
|
|
|
|
def p(note):
|
|
|
|
return originalP(note) and note.frm == sender
|
|
|
|
notesAndIds = self.db.notes(p)
|
|
|
|
notesAndIds.sort()
|
2004-08-07 02:48:03 +02:00
|
|
|
notesAndIds.reverse()
|
2004-08-07 02:41:55 +02:00
|
|
|
if not notesAndIds:
|
|
|
|
irc.reply('I couldn\'t find any matching read notes for your user.')
|
2003-04-19 23:40:04 +02:00
|
|
|
else:
|
2004-08-07 02:41:55 +02:00
|
|
|
ids = [self._formatNoteId(msg, id, note.frm, note.public)
|
|
|
|
for (id, note) in notesAndIds]
|
2004-02-01 22:47:11 +01:00
|
|
|
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:
|