mirror of
https://github.com/progval/Limnoria.git
synced 2025-04-27 05:21:09 -05:00
Untested, but close to functional
This commit is contained in:
parent
e112e32d71
commit
e7da39da77
@ -43,6 +43,8 @@ from baseplugin import *
|
|||||||
import re
|
import re
|
||||||
import time
|
import time
|
||||||
|
|
||||||
|
import sqlite
|
||||||
|
|
||||||
import privmsgs
|
import privmsgs
|
||||||
import ircutils
|
import ircutils
|
||||||
import callbacks
|
import callbacks
|
||||||
@ -50,40 +52,237 @@ import callbacks
|
|||||||
smileys = (':)', ';)', ':]', ':-)', ':-D', ':D', ':P', ':p', '(=', '=)')
|
smileys = (':)', ';)', ':]', ':-)', ':-D', ':D', ':P', ':p', '(=', '=)')
|
||||||
frowns = (':|', ':-/', ':-\\', ':\\', ':/', ':(', ':-(', ':\'(')
|
frowns = (':|', ':-/', ':-\\', ':\\', ':/', ':(', ':-(', ':\'(')
|
||||||
|
|
||||||
smileyRegexp = re.compile('|'.join([re.escape(s) for s in smileys]))
|
smileyre = re.compile('|'.join([re.escape(s) for s in smileys]))
|
||||||
frownRegexp = re.compile('|'.join([re.escape(s) for s in frowns]))
|
frownre = re.compile('|'.join([re.escape(s) for s in frowns]))
|
||||||
|
|
||||||
class ChannelStats(callbacks.Privmsg, DBHandler):
|
class ChannelStats(callbacks.Privmsg, DBHandler):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
callbacks.Privmsg.__init__(self)
|
callbacks.Privmsg.__init__(self)
|
||||||
DBHandler.__init__(self, '.stats')
|
DBHandler.__init__(self)
|
||||||
|
|
||||||
|
def makeDb(self, filename):
|
||||||
|
if os.path.exists(filename):
|
||||||
|
return sqlite.connect(filename)
|
||||||
|
db = sqlite.connect(filename)
|
||||||
|
cursor = db.cursor()
|
||||||
|
cursor.execute("""CREATE TABLE stats (
|
||||||
|
id INTEGER PRIMARY KEY,
|
||||||
|
username TEXT UNIQUE,
|
||||||
|
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,
|
||||||
|
topics INTEGER,
|
||||||
|
)""")
|
||||||
|
cursor.execute("""CREATE INDEX stats_username ON stats (username)""")
|
||||||
|
db.commit()
|
||||||
|
return db
|
||||||
|
|
||||||
def doPrivmsg(self, irc, msg):
|
def doPrivmsg(self, irc, msg):
|
||||||
recipient = msg.args[0]
|
if ircutils.isChannel(msg.args[0]):
|
||||||
if ircutils.isChannel(recipient):
|
(channel, s) = msg.args
|
||||||
db = self.getDb(recipient)
|
db = self.getDb(channel)
|
||||||
db[msg.nick] = (time.time(), msg.args[1])
|
try:
|
||||||
#callbacks.Privmsg.doPrivmsg(self, irc, msg)
|
name = ircdb.users.getUserName(msg.prefix)
|
||||||
super(self.__class__, self).doPrivmsg(irc, msg)
|
except KeyError:
|
||||||
#self.__class__.__bases__[0].doPrivmsg(self, irc, msg)
|
return
|
||||||
|
cursor.execute("""SELECT * FROM stats WHERE username=%s""", name)
|
||||||
|
if cursor.rowcount == 0: # User isn't in database.
|
||||||
|
cursor.execute("""INSERT INTO stats VALUES (
|
||||||
|
NULL, %s, %s, %s, %s, %s,
|
||||||
|
%s, %s, 1, %s,
|
||||||
|
0, 0, 0, 0, 0 )""",
|
||||||
|
name, int(time.time()), msg.args[1],
|
||||||
|
len(smileyre.findall(s)),
|
||||||
|
len(frownre.findall(s)), len(s), len(s.split()),
|
||||||
|
int(ircmsgs.isAction(msg)))
|
||||||
|
else:
|
||||||
|
cursor.execute("""SELECT chars, words, msgs,
|
||||||
|
actions, smileys, frowns
|
||||||
|
FROM stats WHERE username=%s""", name)
|
||||||
|
values = cursor.fetchone()
|
||||||
|
cursor.execute("""UPDATE stats SET
|
||||||
|
last_seen=%s, last_msg=%s, chars=%s,
|
||||||
|
words=%s, msgs=%s, actions=%s,
|
||||||
|
smileys=%s, frowns=%s
|
||||||
|
WHERE username=%s""",
|
||||||
|
int(time.time()),
|
||||||
|
s,
|
||||||
|
values.chars + len(s),
|
||||||
|
values.words + len(s.split()),
|
||||||
|
values.msgs + 1,
|
||||||
|
values.actions + ircmsgs.isAction(msg),
|
||||||
|
values.smileys + len(smileyre.findall(s)),
|
||||||
|
values.frowns + len(frownre.findall(s)),
|
||||||
|
name)
|
||||||
|
db.commit()
|
||||||
|
|
||||||
|
def doJoin(self, irc, msg):
|
||||||
|
try:
|
||||||
|
name = ircdb.users.getUserName(msg.prefix)
|
||||||
|
except KeyError:
|
||||||
|
return
|
||||||
|
channels = msg.args[0].split(',')
|
||||||
|
for channel in channels:
|
||||||
|
db = self.getDb(channel)
|
||||||
|
cursor = db.cursor()
|
||||||
|
cursor.execute("SELECT joins FROM stats WHERE username=%s", name)
|
||||||
|
if cursor.rowcount == 0:
|
||||||
|
return
|
||||||
|
joins = cursor.fetchone()[0]
|
||||||
|
cursor.execute("UPDATE stats SET joins=%s WHERE username=%s",
|
||||||
|
joins+1, name)
|
||||||
|
db.commit()
|
||||||
|
|
||||||
|
def doPart(self, irc, msg):
|
||||||
|
try:
|
||||||
|
name = ircdb.users.getUserName(msg.prefix)
|
||||||
|
except KeyError:
|
||||||
|
return
|
||||||
|
channels = msg.args[0].split(',')
|
||||||
|
for channel in channels:
|
||||||
|
db = self.getDb(channel)
|
||||||
|
cursor = db.cursor()
|
||||||
|
cursor.execute("SELECT parts FROM stats WHERE username=%s", name)
|
||||||
|
if cursor.rowcount == 0:
|
||||||
|
return
|
||||||
|
parts = cursor.fetchone()[0]
|
||||||
|
cursor.execute("UPDATE stats SET parts=%s WHERE username=%s",
|
||||||
|
parts+1, name)
|
||||||
|
db.commit()
|
||||||
|
|
||||||
|
def doTopic(self, irc, msg):
|
||||||
|
try:
|
||||||
|
name = ircdb.users.getUserName(msg.prefix)
|
||||||
|
except KeyError:
|
||||||
|
return
|
||||||
|
channel = msg.args[0]
|
||||||
|
db = self.getDb(channel)
|
||||||
|
cursor = db.cursor()
|
||||||
|
cursor.execute("SELECT topics FROM stats WHERE username=%s", name)
|
||||||
|
if cursor.rowcount == 0:
|
||||||
|
return
|
||||||
|
topics = cursor.fetchone()[0]
|
||||||
|
cursor.execute("UPDATE stats SET topics=%s WHERE username=%s",
|
||||||
|
topics+1, name)
|
||||||
|
db.commit()
|
||||||
|
|
||||||
|
def doMode(self, irc, msg):
|
||||||
|
try:
|
||||||
|
name = ircdb.users.getUserName(msg.prefix)
|
||||||
|
except KeyError:
|
||||||
|
return
|
||||||
|
channel = msg.args[0]
|
||||||
|
db = self.getDb(channel)
|
||||||
|
cursor = db.cursor()
|
||||||
|
cursor.execute("SELECT modes FROM stats WHERE username=%s", name)
|
||||||
|
if cursor.rowcount == 0:
|
||||||
|
return
|
||||||
|
modes = cursor.fetchone()[0]
|
||||||
|
cursor.execute("UPDATE stats SET modes=%s WHERE username=%s",
|
||||||
|
modes+1, name)
|
||||||
|
db.commit()
|
||||||
|
|
||||||
|
def doKick(self, irc, msg):
|
||||||
|
db = self.getDb(msg.args[0])
|
||||||
|
cursor = db.cursor()
|
||||||
|
try:
|
||||||
|
name = ircdb.users.getUserName(msg.prefix)
|
||||||
|
cursor.execute("SELECT kicks FROM stats WHERE username=%s", name)
|
||||||
|
if cursor.rowcount != 0:
|
||||||
|
kicks = cursor.fetchone()[0]
|
||||||
|
cursor.execute("UPDATE stats SET kicks=%s WHERE username=%s",
|
||||||
|
kicks+1, name)
|
||||||
|
except KeyError:
|
||||||
|
pass
|
||||||
|
try:
|
||||||
|
kicked = msg.args[1]
|
||||||
|
name = ircdb.users.getUserName(irc.state.nickToHostmask(kicked))
|
||||||
|
cursor.execute("SELECT kicked FROM stats WHERE username=%s", name)
|
||||||
|
if cursor.rowcount != 0:
|
||||||
|
kicked = cursor.fetchone()[0]
|
||||||
|
cursor.execute("UPDATE stats SET kicked=%s WHERE username=%s",
|
||||||
|
kicked+1, name)
|
||||||
|
except KeyError:
|
||||||
|
pass
|
||||||
|
db.commit()
|
||||||
|
|
||||||
def seen(self, irc, msg, args):
|
def seen(self, irc, msg, args):
|
||||||
"<nick>"
|
"[<channel>] (if not sent on the channel itself) <nick>"
|
||||||
channel = privmsgs.getChannel(msg, args)
|
channel = privmsgs.getChannel(msg, args)
|
||||||
nick = ircutils.nick(privmsgs.getArgs(args))
|
name = privmsgs.getArgs(args)
|
||||||
|
if not irc.users.hasUser(name):
|
||||||
|
hostmask = irc.state.nickToHostmask(name)
|
||||||
|
try:
|
||||||
|
name = irc.users.getUserName(hostmask)
|
||||||
|
except KeyError:
|
||||||
|
irc.error(msg, conf.replyNoUser)
|
||||||
|
return
|
||||||
db = self.getDb(channel)
|
db = self.getDb(channel)
|
||||||
try:
|
cursor = db.cursor()
|
||||||
(t, saying) = db[nick]
|
cursor.execute("""SELECT (last_seen, last_msg) FROM stats
|
||||||
if t == 0.0: # Default record time.
|
WHERE username=%s""", name)
|
||||||
raise KeyError
|
if cursor.rowcount == 0:
|
||||||
t = time.localtime(t)
|
irc.reply(msg, 'I have no stats for that user.')
|
||||||
ret = '%s was last seen on %s on %s at %s saying {%s}' %\
|
else:
|
||||||
(nick, channel, time.strftime('%d-%b-%Y', t),
|
(seen, m) = cursor.fetchone()
|
||||||
time.strftime('%H:%M:%S'), saying)
|
ago = int(time.time()) - seen
|
||||||
irc.reply(msg, ret)
|
days = ago / 86400
|
||||||
except KeyError:
|
ago %= 86400
|
||||||
irc.reply(msg, 'I haven\'t seen any user by that nick.')
|
hours = ago / 3600
|
||||||
|
ago %= 3600
|
||||||
|
minutes = ago / 60
|
||||||
|
seconds = ago % 60
|
||||||
|
s = '%s day%s %s hour%s %s minute%s %s second%s ago' % \
|
||||||
|
(days, days == 1 and ',' or 's,',
|
||||||
|
hours, hours == 1 and ',' or 's,',
|
||||||
|
minutes, minutes == 1 and ',' or 's,',
|
||||||
|
seconds, seconds == 1 and ',' or 's,')
|
||||||
|
irc.reply(msg, '%s was last seen here at %s saying %s' % \
|
||||||
|
(name, s, m))
|
||||||
|
|
||||||
|
def stats(self, irc, msg, args):
|
||||||
|
"[<channel>] (if not sent in the channel itself) <nick>"
|
||||||
|
channel = privmsgs.getChannel(msg, args)
|
||||||
|
name = privmsgs.getArgs(args)
|
||||||
|
if not irc.users.hasUser(name):
|
||||||
|
hostmask = irc.state.nickToHostmask(name)
|
||||||
|
try:
|
||||||
|
name = irc.users.getUserName(hostmask)
|
||||||
|
except KeyError:
|
||||||
|
irc.error(msg, conf.replyNoUser)
|
||||||
|
return
|
||||||
|
db = self.getDb(channel)
|
||||||
|
cursor = db.cursor()
|
||||||
|
cursor.execute("""SELECT smileys, frowns, chars, words, msgs, actions,
|
||||||
|
joins, parts, kicks, kicked, modes, topics
|
||||||
|
FROM stats WHERE username=%s""", name)
|
||||||
|
if cursor.rowcount == 0:
|
||||||
|
irc.reply(msg, 'I have no stats for that user.')
|
||||||
|
return
|
||||||
|
values = cursor.fetchone()
|
||||||
|
s = '%s has %s messages, with a total of %s chars, %s words, ' \
|
||||||
|
'%s smileys, and %s frowns; %s of those messages were ACTIONs. ' \
|
||||||
|
'%s has joined %s times, parted %s times, kicked someone %s times'\
|
||||||
|
' been kicked %s times, changed the topic %s times, ' \
|
||||||
|
'and changed the mode %s times.' % \
|
||||||
|
(values.msgs, values.chars, values.words,
|
||||||
|
values.smileys, values.frowns, values.actions,
|
||||||
|
values.joins, values.parts, values.kicks,
|
||||||
|
values.kicked, values.topics,
|
||||||
|
values.modes)
|
||||||
|
irc.reply(msg, s)
|
||||||
|
|
||||||
|
|
||||||
Class = ChannelStats
|
Class = ChannelStats
|
||||||
|
|
||||||
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user