From 410d72172fdf40db7969ad44828150c381e752de Mon Sep 17 00:00:00 2001 From: mutantmonkey Date: Fri, 10 Feb 2012 19:11:04 -0500 Subject: [PATCH] add logger module --- modules/logger.py | 67 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 modules/logger.py diff --git a/modules/logger.py b/modules/logger.py new file mode 100644 index 0000000..2d7cc0e --- /dev/null +++ b/modules/logger.py @@ -0,0 +1,67 @@ +#!/usr/bin/python3 +""" +logger.py - logger for privacy-protecting IRC stats +author: mutantmonkey +""" + +import os +import random +import sqlite3 + +def setup(self): + fn = self.nick + '-' + self.config.host + '.logger.db' + self.logger_db = os.path.join(os.path.expanduser('~/.phenny'), fn) + self.logger_conn = sqlite3.connect(self.logger_db) + + c = self.logger_conn.cursor() + c.execute('''create table if not exists lines_by_nick ( + channel varchar(255), + nick varchar(255), + lines unsigned big int not null default 0, + characters unsigned big int not null default 0, + last_time timestamp default CURRENT_TIMESTAMP, + quote text, + unique (channel, nick) on conflict replace + );''') + +def logger(phenny, input): + if not logger.conn: + logger.conn = sqlite3.connect(phenny.logger_db) + + sqlite_data = { + 'channel': input.sender, + 'nick': input.nick, + 'msg': input.group(1), + 'chars': len(input.group(1)), + } + + c = logger.conn.cursor() + c.execute('''insert or replace into lines_by_nick + (channel, nick, lines, characters, last_time, quote) + values( + :channel, + :nick, + coalesce((select lines from lines_by_nick where + channel=:channel and nick=:nick) + 1, 1), + coalesce((select characters from lines_by_nick where + channel=:channel and nick=:nick) + :chars, :chars), + CURRENT_TIMESTAMP, + coalesce((select quote from lines_by_nick where + channel=:channel and nick=:nick), :msg) + );''', sqlite_data) + c.close() + + if random.randint(0, 20) == 10 and not input.group(1)[:8] == '\x01ACTION ': + c = logger.conn.cursor() + c.execute('update lines_by_nick set quote=:msg where channel=:channel \ + and nick=:nick', sqlite_data) + c.close() + + logger.conn.commit() +logger.conn = None +logger.priority = 'low' +logger.rule = r'(.*)' +logger.thread = False + +if __name__ == '__main__': + print(__doc__.strip())