Added ChannelStats in the new plugin format.

This commit is contained in:
Jeremy Fincher 2005-02-02 07:18:23 +00:00
parent 16ff12a490
commit 0e3713f1b0
6 changed files with 521 additions and 0 deletions

View File

@ -0,0 +1 @@
Insert a description of your plugin here, with any notes, etc. about using it.

View File

@ -0,0 +1,62 @@
###
# Copyright (c) 2005, Jeremiah Fincher
# 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.
###
"""
Silently listens to every message received on a channel and keeps statistics
concerning joins, parts, and various other commands in addition to tracking
statistics about smileys, actions, characters, and words.
"""
import supybot
import supybot.world as world
# Use this for the version of this plugin. You may wish to put a CVS keyword
# in here if you're keeping the plugin in CVS or some similar system.
__version__ = ""
__author__ = supybot.authors.jemfinch
# This is a dictionary mapping supybot.Author instances to lists of
# contributions.
__contributors__ = {}
import config
import plugin
reload(plugin) # In case we're being reloaded.
# Add more reloads here if you add third-party modules and want them to be
# reloaded when this plugin is reloaded. Don't forget to import them as well!
if world.testing:
import test
Class = plugin.Class
configure = config.configure
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:

View File

@ -0,0 +1,69 @@
###
# Copyright (c) 2005, Jeremiah Fincher
# 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.
###
import re
import supybot.conf as conf
import supybot.registry as registry
def configure(advanced):
# This will be called by supybot to configure this module. advanced is
# a bool that specifies whether the user identified himself as an advanced
# user or not. You should effect your configuration by manipulating the
# registry as appropriate.
from supybot.questions import expect, anything, something, yn
conf.registerPlugin('ChannelStats', True)
class Smileys(registry.Value):
def set(self, s):
L = s.split()
self.setValue(L)
def setValue(self, v):
self.s = ' '.join(v)
self.value = re.compile('|'.join(map(re.escape, v)))
def __str__(self):
return self.s
ChannelStats = conf.registerPlugin('ChannelStats')
conf.registerChannelValue(ChannelStats, 'selfStats',
registry.Boolean(True, """Determines whether the bot will keep channel
statistics on itself, possibly skewing the channel stats (especially in
cases where the bot is relaying between channels on a network)."""))
conf.registerChannelValue(ChannelStats, 'smileys',
Smileys(':) ;) ;] :-) :-D :D :P :p (= =)'.split(), """Determines what
words (i.e., pieces of text with no spaces in them) are considered
'smileys' for the purposes of stats-keeping."""))
conf.registerChannelValue(ChannelStats, 'frowns',
Smileys(':| :-/ :-\\ :\\ :/ :( :-( :\'('.split(), """Determines what words
(i.e., pieces of text with no spaces in them ) are considered 'frowns' for
the purposes of stats-keeping."""))
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78

View File

@ -0,0 +1,308 @@
###
# Copyright (c) 2002-2004, Jeremiah Fincher
# 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.
###
import re
import supybot.log as log
import supybot.conf as conf
import supybot.utils as utils
import supybot.world as world
import supybot.ircdb as ircdb
from supybot.commands import *
import supybot.irclib as irclib
import supybot.ircmsgs as ircmsgs
import supybot.plugins as plugins
import supybot.ircutils as ircutils
import supybot.privmsgs as privmsgs
import supybot.registry as registry
import supybot.callbacks as callbacks
class ChannelStat(irclib.IrcCommandDispatcher):
def __init__(self, actions=0, chars=0, frowns=0, joins=0, kicks=0, modes=0,
msgs=0, parts=0, quits=0, smileys=0, topics=0, words=0):
self.actions = actions
self.chars = chars
self.frowns = frowns
self.joins = joins
self.kicks = kicks
self.modes = modes
self.msgs = msgs
self.parts = parts
self.quits = quits
self.smileys = smileys
self.topics = topics
self.words = words
self._values = ['actions', 'chars', 'frowns', 'joins', 'kicks','modes',
'msgs', 'parts', 'quits', 'smileys', 'topics', 'words']
def values(self):
return [getattr(self, s) for s in self._values]
def addMsg(self, msg):
self.msgs += 1
method = self.dispatchCommand(msg.command)
if method is not None:
method(msg)
def doPayload(self, channel, payload):
self.chars += len(payload)
self.words += len(payload.split())
fRe = conf.supybot.plugins.ChannelStats.get('frowns').get(channel)()
sRe =conf.supybot.plugins.ChannelStats.get('smileys').get(channel)()
self.frowns += len(fRe.findall(payload))
self.smileys += len(sRe.findall(payload))
def doPrivmsg(self, msg):
self.doPayload(*msg.args)
if ircmsgs.isAction(msg):
self.actions += 1
def doTopic(self, msg):
self.doPayload(*msg.args)
self.topics += 1
def doKick(self, msg):
self.kicks += 1
def doPart(self, msg):
if len(msg.args) == 2:
self.doPayload(*msg.args)
self.parts += 1
def doJoin(self, msg):
if len(msg.args) == 2:
self.doPayload(*msg.args)
self.joins += 1
def doMode(self, msg):
self.modes += 1
# doQuit is handled by the plugin.
class UserStat(ChannelStat):
def __init__(self, kicked=0, *args):
ChannelStat.__init__(self, *args)
self.kicked = kicked
self._values.insert(0, 'kicked')
def doKick(self, msg):
self.doPayload(msg.args[0], msg.args[2])
self.kicks += 1
class StatsDB(plugins.ChannelUserDB):
def __init__(self, *args, **kwargs):
plugins.ChannelUserDB.__init__(self, *args, **kwargs)
def serialize(self, v):
return v.values()
def deserialize(self, channel, id, L):
L = map(int, L)
if id == 'channelStats':
return ChannelStat(*L)
else:
return UserStat(*L)
def addMsg(self, msg, id=None):
channel = msg.args[0]
if ircutils.isChannel(channel):
if (channel, 'channelStats') not in self:
self[channel, 'channelStats'] = ChannelStat()
self[channel, 'channelStats'].addMsg(msg)
try:
if id is None:
id = ircdb.users.getUserId(msg.prefix)
except KeyError:
return
if (channel, id) not in self:
self[channel, id] = UserStat()
self[channel, id].addMsg(msg)
def getChannelStats(self, channel):
return self[channel, 'channelStats']
def getUserStats(self, channel, id):
return self[channel, id]
filename = conf.supybot.directories.data.dirize('ChannelStats.db')
class ChannelStats(callbacks.Privmsg):
noIgnore = True
def __init__(self, irc):
self.__parent = super(ChannelStats, self)
self.__parent.__init__(irc)
self.lastmsg = None
self.laststate = None
self.outFiltering = False
self.db = StatsDB(filename)
self._flush = self.db.flush
world.flushers.append(self._flush)
def die(self):
world.flushers.remove(self._flush)
self.db.close()
callbacks.Privmsg.die(self)
def __call__(self, irc, msg):
try:
if self.lastmsg:
self.laststate.addMsg(irc, self.lastmsg)
else:
self.laststate = irc.state.copy()
finally:
self.lastmsg = msg
self.db.addMsg(msg)
super(ChannelStats, self).__call__(irc, msg)
def outFilter(self, irc, msg):
if msg.command == 'PRIVMSG':
if ircutils.isChannel(msg.args[0]):
if self.registryValue('selfStats', msg.args[0]):
try:
self.outFiltering = True
self.db.addMsg(msg, 0)
finally:
self.outFiltering = False
return msg
def doQuit(self, irc, msg):
try:
id = ircdb.users.getUserId(msg.prefix)
except KeyError:
id = None
for (channel, c) in self.laststate.channels.iteritems():
if msg.nick in c.users:
if (channel, 'channelStats') not in self.db:
self.db[channel, 'channelStats'] = ChannelStat()
self.db[channel, 'channelStats'].quits += 1
if id is not None:
if (channel, id) not in self.db:
self.db[channel, id] = UserStat()
self.db[channel, id].quits += 1
def doKick(self, irc, msg):
(channel, nick, _) = msg.args
hostmask = irc.state.nickToHostmask(nick)
try:
id = ircdb.users.getUserId(hostmask)
except KeyError:
return
if channel not in self.db.channels:
self.db.channels[channel] = {}
if id not in self.db.channels[channel]:
self.db.channels[channel][id] = UserStat()
self.db.channels[channel][id].kicked += 1
def stats(self, irc, msg, args, channel, name):
"""[<channel>] [<name>]
Returns the statistics for <name> on <channel>. <channel> is only
necessary if the message isn't sent on the channel itself. If <name>
isn't given, it defaults to the user sending the command.
"""
if name and ircutils.strEqual(name, irc.nick):
id = 0
elif not name:
try:
id = ircdb.users.getUserId(msg.prefix)
name = ircdb.users.getUser(id).name
except KeyError:
irc.error('I couldn\'t find you in my user database.')
return
elif not ircdb.users.hasUser(name):
try:
hostmask = irc.state.nickToHostmask(name)
id = ircdb.users.getUserId(hostmask)
except KeyError:
irc.errorNoUser()
return
else:
id = ircdb.users.getUserId(name)
try:
stats = self.db.getUserStats(channel, id)
s = format('%s has sent %n; a total of %n, %n, '
'%n, and %n; %s of those messages %s'
'%s has joined %n, parted %n, quit %n, '
'kicked someone %n, been kicked %n, '
'changed the topic %n, and changed the '
'mode %n.',
name, (stats.msgs, 'message'),
(stats.chars, 'character'),
(stats.words, 'word'),
(stats.smileys, 'smiley'),
(stats.frowns, 'frown'),
stats.actions,
stats.actions == 1 and 'was an ACTION. '
or 'were ACTIONs. ',
name,
(stats.joins, 'time'),
(stats.parts, 'time'),
(stats.quits, 'time'),
(stats.kicks, 'time'),
(stats.kicked, 'time'),
(stats.topics, 'time'),
(stats.modes, 'time'))
irc.reply(s)
except KeyError:
irc.error(format('I have no stats for that %s in %s.',
name, channel))
stats = wrap(stats, ['channeldb', additional('something')])
def channelstats(self, irc, msg, args, channel):
"""[<channel>]
Returns the statistics for <channel>. <channel> is only necessary if
the message isn't sent on the channel itself.
"""
try:
stats = self.db.getChannelStats(channel)
s = format('On %s there have been %i messages, containing %i '
'characters, %n, %n, and %n; '
'%i of those messages %s. There have been '
'%n, %n, %n, %n, %n, and %n.',
channel, stats.msgs, stats.chars,
(stats.words, 'word'),
(stats.smileys, 'smiley'),
(stats.frowns, 'frown'),
stats.actions, stats.actions == 1 and 'was an ACTION'
or 'were ACTIONs',
(stats.joins, 'join'),
(stats.parts, 'part'),
(stats.quits, 'quit'),
(stats.kicks, 'kick'),
(stats.modes, 'mode', 'change'),
(stats.topics, 'topic', 'change'))
irc.reply(s)
except KeyError:
irc.error(format('I\'ve never been on %s.', channel))
channelstats = wrap(channelstats, ['channeldb'])
Class = ChannelStats
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:

View File

@ -0,0 +1,80 @@
###
# Copyright (c) 2002-2004, Jeremiah Fincher
# 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.
###
from supybot.test import *
import supybot.ircdb as ircdb
class ChannelStatsTestCase(ChannelPluginTestCase):
plugins = ('ChannelStats', 'User')
def setUp(self):
ChannelPluginTestCase.setUp(self)
self.prefix = 'foo!bar@baz'
self.nick = 'foo'
self.irc.feedMsg(ircmsgs.privmsg(self.irc.nick,
'register foo bar',
prefix=self.prefix))
_ = self.irc.takeMsg()
chanop = ircdb.makeChannelCapability(self.channel, 'op')
ircdb.users.getUser(self.nick).addCapability(chanop)
def test(self):
self.assertNotError('channelstats')
self.assertNotError('channelstats')
self.assertNotError('channelstats')
def testStats(self):
self.assertError('channelstats stats %s' % self.nick)
self.assertNotError('channelstats stats %s' % self.nick)
self.assertNotError('channelstats stats %s' % self.nick.upper())
self.assertNotError('channelstats stats')
self.assertRegexp('channelstats stats', self.nick)
def testSelfStats(self):
self.assertError('channelstats stats %s' % self.irc.nick)
self.assertNotError('channelstats stats %s' % self.irc.nick)
self.assertNotError('channelstats stats %s' % self.irc.nick)
self.assertNotError('channelstats stats %s' % self.irc.nick.upper())
u = ircdb.users.getUser(self.prefix)
u.addCapability(ircdb.makeChannelCapability(self.channel, 'op'))
ircdb.users.setUser(u)
try:
conf.supybot.plugins.ChannelStats.selfStats.setValue(False)
m1 = self.getMsg('channelstats stats %s' % self.irc.nick)
m2 = self.getMsg('channelstats stats %s' % self.irc.nick)
self.assertEqual(m1.args[1], m2.args[1])
finally:
conf.supybot.plugins.ChannelStats.selfStats.setValue(True)
def testNoKeyErrorStats(self):
self.assertNotRegexp('stats sweede', 'KeyError')
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:

View File

@ -34,6 +34,7 @@ plugins = [
'AutoMode',
'Babelfish',
'Channel',
'ChannelStats',
'Config',
'Ctcp',
'Dict',