2003-03-12 07:26:59 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
###
|
|
|
|
# Copyright (c) 2002, 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.
|
|
|
|
###
|
|
|
|
|
2003-03-25 07:53:51 +01:00
|
|
|
"""
|
2003-11-15 05:37:04 +01:00
|
|
|
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.
|
|
|
|
"""
|
2003-03-25 07:53:51 +01:00
|
|
|
|
2003-11-25 09:23:47 +01:00
|
|
|
__revision__ = "$Id$"
|
|
|
|
|
2003-10-05 14:56:56 +02:00
|
|
|
import plugins
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-10-04 15:53:13 +02:00
|
|
|
import os
|
2003-03-12 07:26:59 +01:00
|
|
|
import re
|
2003-08-12 21:38:23 +02:00
|
|
|
import sets
|
2003-03-12 07:26:59 +01:00
|
|
|
import time
|
2003-10-02 02:13:44 +02:00
|
|
|
import getopt
|
2003-11-05 08:59:44 +01:00
|
|
|
import string
|
2003-11-15 05:37:04 +01:00
|
|
|
from itertools import imap, ifilter
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-10-06 23:17:21 +02:00
|
|
|
import conf
|
2003-04-12 12:39:04 +02:00
|
|
|
import utils
|
2003-04-01 10:38:53 +02:00
|
|
|
import ircdb
|
|
|
|
import ircmsgs
|
2003-11-02 19:59:06 +01:00
|
|
|
import plugins
|
2003-03-12 07:26:59 +01:00
|
|
|
import privmsgs
|
|
|
|
import ircutils
|
|
|
|
import callbacks
|
2003-12-04 00:14:09 +01:00
|
|
|
import configurable
|
2003-03-12 07:26:59 +01: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/>'
|
|
|
|
|
|
|
|
|
2003-03-12 07:26:59 +01:00
|
|
|
smileys = (':)', ';)', ':]', ':-)', ':-D', ':D', ':P', ':p', '(=', '=)')
|
|
|
|
frowns = (':|', ':-/', ':-\\', ':\\', ':/', ':(', ':-(', ':\'(')
|
|
|
|
|
2003-11-15 05:37:04 +01:00
|
|
|
smileyre = re.compile('|'.join(imap(re.escape, smileys)))
|
|
|
|
frownre = re.compile('|'.join(imap(re.escape, frowns)))
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-11-11 16:58:20 +01:00
|
|
|
class ChannelDB(plugins.ChannelDBHandler,
|
2003-12-04 00:14:09 +01:00
|
|
|
configurable.Mixin,
|
2003-11-11 12:45:44 +01:00
|
|
|
callbacks.Privmsg):
|
2003-11-11 00:39:44 +01:00
|
|
|
noIgnore = True
|
2003-12-04 00:14:09 +01:00
|
|
|
configurables = configurable.Dictionary(
|
|
|
|
[('self-stats', configurable.BoolType, True,
|
2003-11-08 09:01:34 +01:00
|
|
|
"""Determines whether the bot will keep channel statistics on itself,
|
|
|
|
possibly skewing the channel stats (especially in cases where he's
|
2003-12-03 04:14:40 +01:00
|
|
|
relaying between channels on a network."""),
|
2003-12-04 00:14:09 +01:00
|
|
|
('wordstats-top-n', configurable.IntType, 3,
|
2003-12-03 04:14:40 +01:00
|
|
|
"""Determines the maximum number of top users to show for a given
|
|
|
|
wordstat when you request the wordstats for a particular word.""")]
|
2003-11-08 09:01:34 +01:00
|
|
|
)
|
2003-03-12 07:26:59 +01:00
|
|
|
def __init__(self):
|
2003-11-04 09:42:11 +01:00
|
|
|
callbacks.Privmsg.__init__(self)
|
2003-12-04 00:14:09 +01:00
|
|
|
configurable.Mixin.__init__(self)
|
2003-11-04 09:42:11 +01:00
|
|
|
plugins.ChannelDBHandler.__init__(self)
|
2003-10-16 15:34:02 +02:00
|
|
|
self.lastmsg = None
|
|
|
|
self.laststate = None
|
2003-10-25 00:29:30 +02:00
|
|
|
self.outFiltering = False
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-11-11 16:58:20 +01:00
|
|
|
def die(self):
|
|
|
|
callbacks.Privmsg.die(self)
|
2003-12-04 00:14:09 +01:00
|
|
|
configurable.Mixin.die(self)
|
2003-11-11 16:58:20 +01:00
|
|
|
plugins.ChannelDBHandler.die(self)
|
|
|
|
|
2003-03-26 11:04:26 +01:00
|
|
|
def makeDb(self, filename):
|
|
|
|
if os.path.exists(filename):
|
2003-10-09 08:43:58 +02:00
|
|
|
db = sqlite.connect(filename)
|
|
|
|
else:
|
|
|
|
db = sqlite.connect(filename)
|
|
|
|
cursor = db.cursor()
|
|
|
|
cursor.execute("""CREATE TABLE user_stats (
|
|
|
|
id INTEGER PRIMARY KEY,
|
2003-10-25 00:29:30 +02:00
|
|
|
user_id INTEGER UNIQUE ON CONFLICT IGNORE,
|
2003-10-09 08:43:58 +02:00
|
|
|
last_seen TIMESTAMP,
|
|
|
|
last_msg TEXT,
|
|
|
|
smileys INTEGER,
|
|
|
|
frowns INTEGER,
|
|
|
|
chars INTEGER,
|
|
|
|
words INTEGER,
|
|
|
|
msgs INTEGER,
|
|
|
|
actions INTEGER,
|
|
|
|
joins INTEGER,
|
|
|
|
parts INTEGER,
|
|
|
|
kicks INTEGER,
|
|
|
|
kicked INTEGER,
|
|
|
|
modes INTEGER,
|
2003-10-16 15:34:02 +02:00
|
|
|
topics INTEGER,
|
|
|
|
quits INTEGER
|
2003-10-09 08:43:58 +02:00
|
|
|
)""")
|
|
|
|
cursor.execute("""CREATE TABLE channel_stats (
|
|
|
|
smileys INTEGER,
|
|
|
|
frowns INTEGER,
|
|
|
|
chars INTEGER,
|
|
|
|
words INTEGER,
|
|
|
|
msgs INTEGER,
|
|
|
|
actions INTEGER,
|
|
|
|
joins INTEGER,
|
|
|
|
parts INTEGER,
|
|
|
|
kicks INTEGER,
|
|
|
|
modes INTEGER,
|
2003-10-16 15:34:02 +02:00
|
|
|
topics INTEGER,
|
|
|
|
quits INTEGER
|
2003-10-09 08:43:58 +02:00
|
|
|
)""")
|
|
|
|
cursor.execute("""CREATE TABLE nick_seen (
|
2003-11-11 00:39:44 +01:00
|
|
|
name TEXT,
|
|
|
|
normalized TEXT UNIQUE ON CONFLICT REPLACE,
|
2003-10-09 08:43:58 +02:00
|
|
|
last_seen TIMESTAMP,
|
|
|
|
last_msg TEXT
|
|
|
|
)""")
|
|
|
|
cursor.execute("""INSERT INTO channel_stats
|
2003-10-16 15:34:02 +02:00
|
|
|
VALUES (0, 0, 0, 0, 0, 0,
|
2003-10-09 08:43:58 +02:00
|
|
|
0, 0, 0, 0, 0, 0)""")
|
2003-11-05 07:09:17 +01:00
|
|
|
cursor.execute("""CREATE TABLE words (
|
|
|
|
id INTEGER PRIMARY KEY,
|
|
|
|
word TEXT UNIQUE ON CONFLICT IGNORE
|
|
|
|
)""")
|
|
|
|
cursor.execute("""CREATE TABLE word_stats (
|
|
|
|
id INTEGER PRIMARY KEY,
|
|
|
|
word_id INTEGER,
|
|
|
|
user_id INTEGER,
|
|
|
|
count INTEGER,
|
|
|
|
UNIQUE (word_id, user_id) ON CONFLICT IGNORE
|
|
|
|
)""")
|
|
|
|
cursor.execute("""CREATE INDEX word_stats_word_id
|
|
|
|
ON word_stats (word_id)""")
|
|
|
|
cursor.execute("""CREATE INDEX word_stats_user_id
|
|
|
|
ON word_stats (user_id)""")
|
2003-10-09 08:43:58 +02:00
|
|
|
db.commit()
|
|
|
|
def p(s1, s2):
|
|
|
|
return int(ircutils.nickEqual(s1, s2))
|
|
|
|
db.create_function('nickeq', 2, p)
|
2003-03-26 11:04:26 +01:00
|
|
|
return db
|
2003-08-20 18:26:23 +02:00
|
|
|
|
2003-10-16 15:34:02 +02:00
|
|
|
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
|
2003-11-04 09:42:11 +01:00
|
|
|
super(ChannelDB, self).__call__(irc, msg)
|
2003-10-16 15:34:02 +02:00
|
|
|
|
2003-03-12 07:26:59 +01:00
|
|
|
def doPrivmsg(self, irc, msg):
|
2003-03-26 11:04:26 +01:00
|
|
|
if ircutils.isChannel(msg.args[0]):
|
2003-10-25 00:29:30 +02:00
|
|
|
self._updatePrivmsgStats(msg)
|
2003-11-05 07:09:17 +01:00
|
|
|
self._updateWordStats(msg)
|
|
|
|
|
2003-11-05 08:59:44 +01:00
|
|
|
_alphanumeric = string.ascii_letters + string.digits
|
|
|
|
_nonAlphanumeric = string.ascii.translate(string.ascii, _alphanumeric)
|
2003-11-05 07:09:17 +01:00
|
|
|
def _updateWordStats(self, msg):
|
|
|
|
try:
|
|
|
|
if self.outFiltering:
|
|
|
|
id = 0
|
|
|
|
else:
|
|
|
|
id = ircdb.users.getUserId(msg.prefix)
|
|
|
|
except KeyError:
|
|
|
|
return
|
|
|
|
(channel, s) = msg.args
|
|
|
|
db = self.getDb(channel)
|
|
|
|
cursor = db.cursor()
|
|
|
|
words = s.lower().split()
|
2003-11-05 08:59:44 +01:00
|
|
|
words = [s.strip(self._nonAlphanumeric) for s in words]
|
2003-11-05 07:09:17 +01:00
|
|
|
criteria = ['word=%s'] * len(words)
|
|
|
|
criterion = ' OR '.join(criteria)
|
|
|
|
cursor.execute("SELECT id, word FROM words WHERE %s"%criterion, *words)
|
|
|
|
for (wordId, word) in cursor.fetchall():
|
|
|
|
cursor.execute("""INSERT INTO word_stats
|
|
|
|
VALUES(NULL, %s, %s, 0)""", wordId, id)
|
|
|
|
cursor.execute("""UPDATE word_stats SET count=count+%s
|
|
|
|
WHERE word_id=%s AND user_id=%s""",
|
|
|
|
words.count(word), wordId, id)
|
|
|
|
db.commit()
|
2003-10-25 00:29:30 +02:00
|
|
|
|
|
|
|
def _updatePrivmsgStats(self, msg):
|
|
|
|
(channel, s) = msg.args
|
|
|
|
db = self.getDb(channel)
|
|
|
|
cursor = db.cursor()
|
|
|
|
chars = len(s)
|
|
|
|
words = len(s.split())
|
|
|
|
isAction = ircmsgs.isAction(msg)
|
|
|
|
frowns = len(frownre.findall(s))
|
|
|
|
smileys = len(smileyre.findall(s))
|
|
|
|
s = ircmsgs.prettyPrint(msg)
|
|
|
|
cursor.execute("""UPDATE channel_stats
|
|
|
|
SET smileys=smileys+%s,
|
|
|
|
frowns=frowns+%s,
|
|
|
|
chars=chars+%s,
|
|
|
|
words=words+%s,
|
|
|
|
msgs=msgs+1,
|
|
|
|
actions=actions+%s""",
|
|
|
|
smileys, frowns, chars, words, int(isAction))
|
2003-11-11 00:39:44 +01:00
|
|
|
cursor.execute("""INSERT INTO nick_seen VALUES (%s, %s, %s, %s)""",
|
|
|
|
msg.nick,ircutils.toLower(msg.nick),int(time.time()),s)
|
2003-10-25 00:29:30 +02:00
|
|
|
try:
|
|
|
|
if self.outFiltering:
|
|
|
|
id = 0
|
2003-03-26 11:04:26 +01:00
|
|
|
else:
|
2003-10-25 00:29:30 +02:00
|
|
|
id = ircdb.users.getUserId(msg.prefix)
|
|
|
|
except KeyError:
|
|
|
|
return
|
|
|
|
cursor.execute("""INSERT INTO user_stats VALUES (
|
|
|
|
NULL, %s, %s, %s, %s, %s,
|
|
|
|
%s, %s, 1, %s,
|
|
|
|
0, 0, 0, 0, 0, 0, 0)""",
|
|
|
|
id, int(time.time()), s,
|
|
|
|
smileys, frowns, chars, words, int(isAction))
|
|
|
|
cursor.execute("""UPDATE user_stats SET
|
|
|
|
last_seen=%s, last_msg=%s, chars=chars+%s,
|
|
|
|
words=words+%s, msgs=msgs+1,
|
|
|
|
actions=actions+%s, smileys=smileys+%s,
|
|
|
|
frowns=frowns+%s
|
|
|
|
WHERE user_id=%s""",
|
|
|
|
int(time.time()), s, chars, words, int(isAction),
|
|
|
|
smileys, frowns, id)
|
|
|
|
db.commit()
|
|
|
|
|
|
|
|
def outFilter(self, irc, msg):
|
|
|
|
if msg.command == 'PRIVMSG':
|
|
|
|
if ircutils.isChannel(msg.args[0]):
|
2003-11-08 09:01:34 +01:00
|
|
|
if self.configurables.get('self-stats', msg.args[0]):
|
2003-11-02 19:59:06 +01:00
|
|
|
db = self.getDb(msg.args[0])
|
|
|
|
cursor = db.cursor()
|
|
|
|
try:
|
|
|
|
self.outFiltering = True
|
|
|
|
self._updatePrivmsgStats(msg)
|
|
|
|
finally:
|
|
|
|
self.outFiltering = False
|
2003-10-25 00:29:30 +02:00
|
|
|
return msg
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-03-26 11:04:26 +01:00
|
|
|
def doPart(self, irc, msg):
|
2003-04-16 08:44:30 +02:00
|
|
|
channel = msg.args[0]
|
|
|
|
db = self.getDb(channel)
|
|
|
|
cursor = db.cursor()
|
|
|
|
cursor.execute("""UPDATE channel_stats SET parts=parts+1""")
|
2003-03-26 11:04:26 +01:00
|
|
|
try:
|
2003-10-20 09:39:40 +02:00
|
|
|
id = ircdb.users.getUserId(msg.prefix)
|
2003-10-14 07:54:11 +02:00
|
|
|
cursor.execute("""UPDATE user_stats SET parts=parts+1
|
2003-10-20 09:39:40 +02:00
|
|
|
WHERE user_id=%s""", id)
|
2003-03-26 11:04:26 +01:00
|
|
|
except KeyError:
|
2003-04-16 08:44:30 +02:00
|
|
|
pass
|
|
|
|
db.commit()
|
2003-03-26 11:04:26 +01:00
|
|
|
|
|
|
|
def doTopic(self, irc, msg):
|
|
|
|
channel = msg.args[0]
|
2003-03-12 07:26:59 +01:00
|
|
|
db = self.getDb(channel)
|
2003-03-26 11:04:26 +01:00
|
|
|
cursor = db.cursor()
|
2003-04-16 08:44:30 +02:00
|
|
|
cursor.execute("""UPDATE channel_stats SET topics=topics+1""")
|
2003-03-12 07:26:59 +01:00
|
|
|
try:
|
2003-10-20 09:39:40 +02:00
|
|
|
id = ircdb.users.getUserId(msg.prefix)
|
2003-04-16 08:44:30 +02:00
|
|
|
cursor.execute("""UPDATE user_stats
|
|
|
|
SET topics=topics+1
|
2003-10-20 09:39:40 +02:00
|
|
|
WHERE user_id=%s""", id)
|
2003-03-12 07:26:59 +01:00
|
|
|
except KeyError:
|
2003-04-16 08:44:30 +02:00
|
|
|
pass
|
|
|
|
db.commit()
|
|
|
|
|
|
|
|
def doMode(self, irc, msg):
|
2003-03-26 11:04:26 +01:00
|
|
|
channel = msg.args[0]
|
|
|
|
db = self.getDb(channel)
|
|
|
|
cursor = db.cursor()
|
2003-04-16 08:44:30 +02:00
|
|
|
cursor.execute("""UPDATE channel_stats SET modes=modes+1""")
|
|
|
|
try:
|
2003-10-20 09:39:40 +02:00
|
|
|
id = ircdb.users.getUserId(msg.prefix)
|
2003-04-16 08:44:30 +02:00
|
|
|
cursor.execute("""UPDATE user_stats
|
|
|
|
SET modes=modes+1
|
2003-10-20 09:39:40 +02:00
|
|
|
WHERE user_id=%s""", id)
|
2003-04-16 08:44:30 +02:00
|
|
|
except KeyError:
|
|
|
|
pass
|
2003-03-26 11:04:26 +01:00
|
|
|
db.commit()
|
|
|
|
|
|
|
|
def doKick(self, irc, msg):
|
2003-04-16 08:44:30 +02:00
|
|
|
channel = msg.args[0]
|
|
|
|
db = self.getDb(channel)
|
2003-03-26 11:04:26 +01:00
|
|
|
cursor = db.cursor()
|
2003-04-16 08:44:30 +02:00
|
|
|
cursor.execute("""UPDATE channel_stats SET kicks=kicks+1""")
|
2003-03-26 11:04:26 +01:00
|
|
|
try:
|
2003-10-20 09:39:40 +02:00
|
|
|
id = ircdb.users.getUserId(msg.prefix)
|
2003-04-16 08:44:30 +02:00
|
|
|
cursor.execute("""UPDATE user_stats
|
|
|
|
SET kicks=kicks+1
|
2003-10-20 09:39:40 +02:00
|
|
|
WHERE user_id=%s""", id)
|
2003-03-26 11:04:26 +01:00
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
kicked = msg.args[1]
|
2003-10-20 09:39:40 +02:00
|
|
|
id = ircdb.users.getUserId(irc.state.nickToHostmask(kicked))
|
2003-04-16 08:44:30 +02:00
|
|
|
cursor.execute("""UPDATE user_stats
|
|
|
|
SET kicked=kicked+1
|
2003-10-20 09:39:40 +02:00
|
|
|
WHERE user_id=%s""", id)
|
2003-03-26 11:04:26 +01:00
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
db.commit()
|
|
|
|
|
2003-10-16 15:34:02 +02:00
|
|
|
def doJoin(self, irc, msg):
|
|
|
|
channel = msg.args[0]
|
|
|
|
db = self.getDb(channel)
|
|
|
|
cursor = db.cursor()
|
|
|
|
cursor.execute("""UPDATE channel_stats SET joins=joins+1""")
|
|
|
|
try:
|
2003-10-20 09:39:40 +02:00
|
|
|
id = ircdb.users.getUserId(msg.prefix)
|
2003-10-16 15:34:02 +02:00
|
|
|
cursor.execute("""UPDATE user_stats
|
|
|
|
SET joins=joins+1
|
2003-10-20 09:39:40 +02:00
|
|
|
WHERE user_id=%s""", id)
|
2003-10-16 15:34:02 +02:00
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
db.commit()
|
|
|
|
|
|
|
|
def doQuit(self, irc, msg):
|
|
|
|
for (channel, c) in self.laststate.channels.iteritems():
|
|
|
|
if msg.nick in c.users:
|
|
|
|
db = self.getDb(channel)
|
|
|
|
cursor = db.cursor()
|
|
|
|
cursor.execute("""UPDATE channel_stats SET quits=quits+1""")
|
|
|
|
try:
|
2003-10-20 09:39:40 +02:00
|
|
|
id = ircdb.users.getUserId(msg.prefix)
|
2003-10-16 15:34:02 +02:00
|
|
|
cursor.execute("""UPDATE user_stats SET quits=quits+1
|
2003-10-20 09:39:40 +02:00
|
|
|
WHERE user_id=%s""", id)
|
2003-10-16 15:34:02 +02:00
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
db.commit()
|
|
|
|
|
2003-03-26 11:04:26 +01:00
|
|
|
def seen(self, irc, msg, args):
|
2003-10-02 02:13:44 +02:00
|
|
|
"""[<channel>] [--user] <name>
|
2003-04-16 08:44:30 +02:00
|
|
|
|
2003-10-02 02:13:44 +02:00
|
|
|
Returns the last time <name> was seen and what <name> was last seen
|
|
|
|
saying. --user will look for user <name> instead of using <name> as
|
|
|
|
a nick (registered users, remember, can be recognized under any number
|
2003-10-02 02:24:36 +02:00
|
|
|
of nicks) <channel> is only necessary if the message isn't sent on the
|
2003-04-16 08:44:30 +02:00
|
|
|
channel itself.
|
|
|
|
"""
|
2003-03-26 11:04:26 +01:00
|
|
|
channel = privmsgs.getChannel(msg, args)
|
|
|
|
db = self.getDb(channel)
|
|
|
|
cursor = db.cursor()
|
2003-10-02 02:13:44 +02:00
|
|
|
(optlist, rest) = getopt.getopt(args, '', ['user'])
|
|
|
|
name = privmsgs.getArgs(rest)
|
2003-10-02 02:42:26 +02:00
|
|
|
if ('--user', '') in optlist:
|
|
|
|
table = 'user_stats'
|
2003-10-20 09:39:40 +02:00
|
|
|
criterion = 'user_id=%s'
|
2003-12-05 13:08:24 +01:00
|
|
|
try:
|
|
|
|
name = ircdb.users.getUserId(name)
|
|
|
|
except KeyError:
|
2003-10-02 02:13:44 +02:00
|
|
|
try:
|
|
|
|
hostmask = irc.state.nickToHostmask(name)
|
2003-12-05 13:08:24 +01:00
|
|
|
name = ircdb.users.getUserId(hostmask)
|
2003-10-02 02:13:44 +02:00
|
|
|
except KeyError:
|
|
|
|
irc.error(msg, conf.replyNoUser)
|
|
|
|
else:
|
|
|
|
table = 'nick_seen'
|
2003-11-11 00:39:44 +01:00
|
|
|
criterion = 'normalized=%s'
|
|
|
|
name = ircutils.toLower(name)
|
2003-11-10 16:21:08 +01:00
|
|
|
sql = "SELECT last_seen,last_msg FROM %s WHERE %s" % (table,criterion)
|
2003-10-02 02:13:44 +02:00
|
|
|
cursor.execute(sql, name)
|
2003-03-26 11:04:26 +01:00
|
|
|
if cursor.rowcount == 0:
|
2003-10-29 17:15:33 +01:00
|
|
|
irc.reply(msg, 'I have not seen %s.' % name)
|
2003-03-26 11:04:26 +01:00
|
|
|
else:
|
|
|
|
(seen, m) = cursor.fetchone()
|
2003-04-01 10:38:53 +02:00
|
|
|
seen = int(seen)
|
2003-11-10 17:02:17 +01:00
|
|
|
if isinstance(name, int):
|
|
|
|
name = ircdb.users.getUser(int(name)).name
|
2003-11-21 18:32:14 +01:00
|
|
|
s = '%s was last seen here %s ago saying: %s' % \
|
2003-08-15 04:56:29 +02:00
|
|
|
(name, utils.timeElapsed(time.time() - seen), m)
|
2003-04-12 12:39:04 +02:00
|
|
|
irc.reply(msg, s)
|
2003-03-26 11:04:26 +01:00
|
|
|
|
|
|
|
def stats(self, irc, msg, args):
|
2003-10-23 22:59:51 +02:00
|
|
|
"""[<channel>] [<name>]
|
2003-04-16 08:44:30 +02:00
|
|
|
|
2003-10-23 22:59:51 +02:00
|
|
|
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.
|
2003-04-16 08:44:30 +02:00
|
|
|
"""
|
2003-03-26 11:04:26 +01:00
|
|
|
channel = privmsgs.getChannel(msg, args)
|
2003-11-11 14:20:06 +01:00
|
|
|
name = privmsgs.getArgs(args, required=0, optional=1)
|
2003-10-23 22:59:51 +02:00
|
|
|
if not name:
|
|
|
|
try:
|
|
|
|
id = ircdb.users.getUserId(msg.prefix)
|
2003-10-25 00:29:30 +02:00
|
|
|
name = ircdb.users.getUser(id).name
|
2003-10-23 22:59:51 +02:00
|
|
|
except KeyError:
|
|
|
|
irc.error(msg, 'I couldn\'t find you in my user database.')
|
|
|
|
return
|
|
|
|
elif not ircdb.users.hasUser(name):
|
2003-03-26 11:04:26 +01:00
|
|
|
try:
|
2003-10-02 02:43:38 +02:00
|
|
|
hostmask = irc.state.nickToHostmask(name)
|
2003-10-20 09:39:40 +02:00
|
|
|
id = ircdb.users.getUserId(hostmask)
|
2003-03-26 11:04:26 +01:00
|
|
|
except KeyError:
|
|
|
|
irc.error(msg, conf.replyNoUser)
|
|
|
|
return
|
2003-10-20 09:39:40 +02:00
|
|
|
else:
|
|
|
|
id = ircdb.users.getUserId(name)
|
2003-03-26 11:04:26 +01:00
|
|
|
db = self.getDb(channel)
|
|
|
|
cursor = db.cursor()
|
2003-10-20 09:39:40 +02:00
|
|
|
cursor.execute("""SELECT * FROM user_stats WHERE user_id=%s""", id)
|
2003-03-26 11:04:26 +01:00
|
|
|
if cursor.rowcount == 0:
|
2003-10-14 07:54:11 +02:00
|
|
|
irc.error(msg, 'I have no stats for that user.')
|
2003-03-26 11:04:26 +01:00
|
|
|
return
|
|
|
|
values = cursor.fetchone()
|
2003-10-09 21:20:50 +02:00
|
|
|
s = '%s has sent %s; a total of %s, %s, ' \
|
|
|
|
'%s, and %s; %s of those messages %s' \
|
2003-10-16 15:34:02 +02:00
|
|
|
'%s has joined %s, parted %s, quit %s, kicked someone %s, '\
|
2003-10-09 21:20:50 +02:00
|
|
|
'been kicked %s, changed the topic %s, ' \
|
|
|
|
'and changed the mode %s.' % \
|
|
|
|
(name, utils.nItems(values.msgs, 'message'),
|
|
|
|
utils.nItems(values.chars, 'character'),
|
|
|
|
utils.nItems(values.words, 'word'),
|
|
|
|
utils.nItems(values.smileys, 'smiley'),
|
|
|
|
utils.nItems(values.frowns, 'frown'),
|
|
|
|
values.actions, values.actions == 1 and 'was an ACTION. '
|
|
|
|
or 'were ACTIONs. ',
|
|
|
|
name,
|
|
|
|
utils.nItems(values.joins, 'time'),
|
|
|
|
utils.nItems(values.parts, 'time'),
|
2003-10-16 15:34:02 +02:00
|
|
|
utils.nItems(values.quits, 'time'),
|
2003-10-09 21:20:50 +02:00
|
|
|
utils.nItems(values.kicks, 'time'),
|
|
|
|
utils.nItems(values.kicked, 'time'),
|
|
|
|
utils.nItems(values.topics, 'time'),
|
|
|
|
utils.nItems(values.modes, 'time'))
|
2003-03-26 11:04:26 +01:00
|
|
|
irc.reply(msg, s)
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-04-16 08:44:30 +02:00
|
|
|
def channelstats(self, irc, msg, args):
|
|
|
|
"""[<channel>]
|
|
|
|
|
|
|
|
Returns the statistics for <channel>. <channel> is only necessary if
|
|
|
|
the message isn't sent on the channel itself.
|
|
|
|
"""
|
|
|
|
channel = privmsgs.getChannel(msg, args)
|
|
|
|
db = self.getDb(channel)
|
|
|
|
cursor = db.cursor()
|
|
|
|
cursor.execute("""SELECT * FROM channel_stats""")
|
|
|
|
values = cursor.fetchone()
|
|
|
|
s = 'On %s there have been %s messages, containing %s characters, ' \
|
|
|
|
'%s words, %s smileys, and %s frowns; %s of those messages were ' \
|
2003-10-16 15:34:02 +02:00
|
|
|
'ACTIONs. There have been %s joins, %s parts, %s quits, ' \
|
|
|
|
'%s kicks, %s mode changes, and %s topic changes.' % \
|
2003-04-16 08:44:30 +02:00
|
|
|
(channel, values.msgs, values.chars,
|
|
|
|
values.words, values.smileys, values.frowns, values.actions,
|
2003-10-16 15:34:02 +02:00
|
|
|
values.joins, values.parts, values.quits,
|
|
|
|
values.kicks, values.modes, values.topics)
|
2003-04-16 08:44:30 +02:00
|
|
|
irc.reply(msg, s)
|
|
|
|
|
2003-11-05 07:09:17 +01:00
|
|
|
def addword(self, irc, msg, args):
|
|
|
|
"""[<channel>] <word>
|
|
|
|
|
|
|
|
Keeps stats on <word> in <channel>. <channel> is only necessary if the
|
|
|
|
message isn't sent in the channel itself.
|
|
|
|
"""
|
|
|
|
channel = privmsgs.getChannel(msg, args)
|
|
|
|
word = privmsgs.getArgs(args)
|
|
|
|
word = word.strip()
|
2003-11-05 08:59:44 +01:00
|
|
|
if word.strip(self._nonAlphanumeric) != word:
|
|
|
|
irc.error(msg, '<word> must not contain non-alphanumeric chars.')
|
2003-11-05 07:09:17 +01:00
|
|
|
return
|
|
|
|
word = word.lower()
|
|
|
|
db = self.getDb(channel)
|
|
|
|
cursor = db.cursor()
|
|
|
|
cursor.execute("""INSERT INTO words VALUES (NULL, %s)""", word)
|
|
|
|
db.commit()
|
|
|
|
irc.reply(msg, conf.replySuccess)
|
|
|
|
|
|
|
|
def wordstats(self, irc, msg, args):
|
2003-11-06 07:41:04 +01:00
|
|
|
"""[<channel>] [<user>] [<word>]
|
2003-11-05 07:09:17 +01:00
|
|
|
|
2003-11-06 07:41:04 +01:00
|
|
|
With no arguments, returns the list of words that are being monitored
|
|
|
|
for stats. With <user> alone, returns all the stats for that user.
|
|
|
|
With <word> alone, returns the top users for that word. With <user>
|
|
|
|
and <word>, returns that user's stat for that word. <channel> is only
|
2003-11-06 21:21:59 +01:00
|
|
|
needed if not said in the channel. (Note: if only one of <user> or
|
|
|
|
<word> is given, <word> is assumed first and only if no stats are
|
|
|
|
available for that word, do we assume it's <user>.)
|
2003-11-05 07:09:17 +01:00
|
|
|
"""
|
|
|
|
channel = privmsgs.getChannel(msg, args)
|
2003-11-11 14:20:06 +01:00
|
|
|
(arg1, arg2) = privmsgs.getArgs(args, required=0, optional=2)
|
2003-11-05 07:09:17 +01:00
|
|
|
db = self.getDb(channel)
|
|
|
|
cursor = db.cursor()
|
2003-11-06 07:41:04 +01:00
|
|
|
if not arg1 and not arg2:
|
|
|
|
cursor.execute("""SELECT word FROM words""")
|
2003-11-05 07:09:17 +01:00
|
|
|
if cursor.rowcount == 0:
|
2003-11-06 07:41:04 +01:00
|
|
|
irc.reply(msg, 'I am not currently keeping any word stats.')
|
|
|
|
return
|
|
|
|
l = [repr(tup[0]) for tup in cursor.fetchall()]
|
|
|
|
s = 'Currently keeping stats for: %s' % utils.commaAndify(l)
|
|
|
|
irc.reply(msg, s)
|
|
|
|
elif arg1 and arg2:
|
|
|
|
user, word = (arg1, arg2)
|
|
|
|
try:
|
|
|
|
id = ircdb.users.getUserId(user)
|
|
|
|
except KeyError: # Maybe it was a nick. Check the hostmask.
|
|
|
|
try:
|
|
|
|
hostmask = irc.state.nickToHostmask(user)
|
|
|
|
id = ircdb.users.getUserId(hostmask)
|
|
|
|
except KeyError:
|
|
|
|
irc.error(msg, conf.replyNoUser)
|
|
|
|
return
|
|
|
|
db = self.getDb(channel)
|
|
|
|
cursor = db.cursor()
|
|
|
|
word = word.lower()
|
|
|
|
cursor.execute("""SELECT word_stats.count FROM words, word_stats
|
|
|
|
WHERE words.word=%s AND
|
|
|
|
word_id=words.id AND
|
|
|
|
word_stats.user_id=%s""", word, id)
|
|
|
|
if cursor.rowcount == 0:
|
|
|
|
cursor.execute("""SELECT id FROM words WHERE word=%s""", word)
|
|
|
|
if cursor.rowcount == 0:
|
|
|
|
irc.error(msg, 'I\'m not keeping stats on %r.' % word)
|
|
|
|
else:
|
|
|
|
irc.error(msg, '%s has never said %r.' % (user, word))
|
|
|
|
return
|
|
|
|
count = int(cursor.fetchone()[0])
|
|
|
|
s = '%s has said %r %s.' % (user,word,utils.nItems(count, 'time'))
|
|
|
|
irc.reply(msg, s)
|
|
|
|
else:
|
|
|
|
# Figure out if we got a user or a word
|
2003-11-06 21:21:59 +01:00
|
|
|
cursor.execute("""SELECT word FROM words
|
|
|
|
WHERE word=%s""", arg1)
|
|
|
|
if cursor.rowcount == 0:
|
2003-12-03 03:17:10 +01:00
|
|
|
# It was a user.
|
2003-11-06 07:41:04 +01:00
|
|
|
try:
|
2003-11-06 21:21:59 +01:00
|
|
|
id = ircdb.users.getUserId(arg1)
|
|
|
|
except KeyError: # Maybe it was a nick. Check the hostmask.
|
|
|
|
try:
|
|
|
|
hostmask = irc.state.nickToHostmask(arg1)
|
|
|
|
id = ircdb.users.getUserId(hostmask)
|
|
|
|
except KeyError:
|
2003-11-06 07:41:04 +01:00
|
|
|
irc.error(msg, '%r doesn\'t look like a user I know '
|
2003-11-06 21:21:59 +01:00
|
|
|
'or a word that I\'m keeping stats '
|
|
|
|
'on' % arg1)
|
2003-11-06 07:41:04 +01:00
|
|
|
return
|
2003-11-06 21:21:59 +01:00
|
|
|
cursor.execute("""SELECT words.word, word_stats.count
|
|
|
|
FROM words, word_stats
|
|
|
|
WHERE words.id = word_stats.word_id
|
|
|
|
AND word_stats.user_id=%s
|
|
|
|
ORDER BY words.word""", id)
|
|
|
|
if cursor.rowcount == 0:
|
|
|
|
username = ircdb.users.getUser(id).name
|
|
|
|
irc.error(msg, '%r has no wordstats' % username)
|
2003-11-06 07:41:04 +01:00
|
|
|
return
|
2003-12-03 03:17:10 +01:00
|
|
|
L = [('%r: %s' % (word, count)) for \
|
2003-11-06 21:21:59 +01:00
|
|
|
(word, count) in cursor.fetchall()]
|
2003-12-03 03:17:10 +01:00
|
|
|
irc.reply(msg, utils.commaAndify(L))
|
2003-11-06 21:21:59 +01:00
|
|
|
return
|
2003-12-03 03:17:10 +01:00
|
|
|
else:
|
|
|
|
# It's a word, not a user
|
|
|
|
word = arg1
|
2003-12-03 04:14:40 +01:00
|
|
|
numUsers = self.configurables.get('wordstats-top-n',
|
|
|
|
msg.args[0])
|
2003-12-03 03:17:10 +01:00
|
|
|
cursor.execute("""SELECT word_stats.count,
|
|
|
|
word_stats.user_id
|
|
|
|
FROM words, word_stats
|
|
|
|
WHERE words.word=%s AND
|
|
|
|
words.id=word_stats.word_id
|
2003-12-03 04:14:40 +01:00
|
|
|
ORDER BY word_stats.count DESC""",
|
|
|
|
word)
|
2003-12-03 03:17:10 +01:00
|
|
|
if cursor.rowcount == 0:
|
|
|
|
irc.error(msg, 'No one has said %r' % word)
|
|
|
|
return
|
|
|
|
results = cursor.fetchall()
|
2003-12-03 04:14:40 +01:00
|
|
|
numResultsShown = min(cursor.rowcount, numUsers)
|
|
|
|
cursor.execute("""SELECT sum(word_stats.count)
|
|
|
|
FROM words, word_stats
|
|
|
|
WHERE words.word=%s AND
|
|
|
|
words.id=word_stats.word_id""",
|
|
|
|
word)
|
|
|
|
total = int(cursor.fetchone()[0])
|
2003-12-03 03:17:10 +01:00
|
|
|
ers = '%rer' % word
|
2003-12-03 04:14:40 +01:00
|
|
|
ret = 'Top %s ' % utils.nItems(numResultsShown, ers)
|
|
|
|
ret += '(out of a total of %s seen):' % \
|
|
|
|
utils.nItems(total, repr(word))
|
2003-12-03 03:17:10 +01:00
|
|
|
L = []
|
2003-12-03 04:14:40 +01:00
|
|
|
for (count, id) in results[:numResultsShown]:
|
2003-12-03 03:17:10 +01:00
|
|
|
username = ircdb.users.getUser(id).name
|
|
|
|
L.append('%s: %s' % (username, count))
|
|
|
|
try:
|
|
|
|
id = ircdb.users.getUserId(msg.prefix)
|
|
|
|
rank = 1
|
|
|
|
for (_, userId) in results:
|
|
|
|
if userId == id:
|
2003-12-03 03:26:20 +01:00
|
|
|
s = 'You are ranked %s out of %s.' % \
|
|
|
|
(rank, utils.nItems(len(results), ers))
|
2003-12-03 03:17:10 +01:00
|
|
|
break
|
|
|
|
else:
|
|
|
|
rank += 1
|
|
|
|
else:
|
|
|
|
s = 'You have not said %r' % word
|
|
|
|
ret = '%s %s. %s' % (ret, utils.commaAndify(L), s)
|
|
|
|
except KeyError:
|
2003-12-03 03:43:30 +01:00
|
|
|
ret = '%s %s.' % (ret, utils.commaAndify(L))
|
2003-12-03 03:17:10 +01:00
|
|
|
irc.reply(msg, ret)
|
2003-11-06 07:41:04 +01:00
|
|
|
|
2003-08-12 00:31:16 +02:00
|
|
|
Class = ChannelDB
|
2003-03-26 11:04:26 +01:00
|
|
|
|
2003-03-24 09:41:19 +01:00
|
|
|
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|