# gozerplugs/plugs/jlog.py
#
#
""" log irc channels to [hour:min] <nick> txt format """
__copyright__ = 'this file is in the public domain'
from gozerbot.commands import cmnds
from gozerbot.callbacks import callbacks, jcallbacks
from gozerbot.persistconfig import PersistConfig
from gozerbot.generic import hourmin, rlog, lockdec
from gozerbot.monitor import outmonitor, jabbermonitor
from gozerbot.plughelp import plughelp
from gozerbot.examples import examples
from gozerbot.ircevent import Ircevent
from gozerbot.fleet import fleet
from time import time
import os, thread
import sqlite3
plughelp.add('dblog', 'log irc channels to [hour:min] <nick> txt format')
outlock = thread.allocate_lock()
outlocked = lockdec(outlock)
cfg = PersistConfig()
cfg.define('channels', [])
class Log():
stopped = False
def __init__(self, dbname):
self.dbname = dbname
if not os.path.isdir(os.path.split(dbname)[0]):
os.makedirs(os.path.split(dbname)[0])
self.create_db()
self.connection = sqlite3.connect(dbname)
self.cursor = self.connection.cursor()
def create_db(self):
connection = sqlite3.connect(self.dbname)
cursor = connection.cursor()
"""
cmnds:
0 - Presence
1 - Message
"""
cursor.execute('CREATE TABLE log (id INTEGER PRIMARY KEY, conf VARCHAR(50), timestamp TIMESTAMP, nick VARCHAR(50), msg TEXT, cmnd INTEGER);')
cursor.close()
connection.close()
def close(self):
self.stopped = True
self.cursor.close()
self.connection.close()
return True
def write(self, channel, nick, msg, cmnd):
if self.stopped:
return
self.cursor.execute("INSERT INTO log VALUES (null, ?, ?, ?, ?, ?)", (channel, time(), nick, msg, cmnd))
self.connection.commit()
def jabberlog(self, bot, ievent):
if ievent.botoutput:
chan = ievent.to
else:
chan = ievent.channel
if ievent.cmnd == 'Message':
msg = ievent.txt.strip()
self.write(chan, ievent.nick, msg, 1)
elif ievent.cmnd == 'Presence':
if ievent.type == 'unavailable':
msg = "%s left" % ievent.nick
else:
msg = "%s joined" % ievent.nick
self.write(chan, ievent.nick, msg, 0)
logger = ''
def init():
jcallbacks.add('ALL', jabberdblogcb)
jabbermonitor.add('dblog', jabberdblogcb, jabberpredblogcb)
return 1
def shutdown():
return 1
def jabberpredblogcb(bot, ievent):
if ievent.type == 'Iq':
return 0
if (bot.name, ievent.channel) in cfg.get('channels') or ievent.botoutput:
return 1
def jabberdblogcb(bot, ievent):
logger = Log('logs/dblog/log.db')
logger.jabberlog(bot, ievent)
def handle_dblogon(bot, ievent):
chan = ievent.channel
if chan not in cfg.get('channels'):
cfg.get('channels').append((bot.name, chan))
cfg.save()
ievent.reply('dblog enabled on (%s,%s)' % (bot.name, chan))
else:
ievent.reply('dblog already enabled on (%s,%s)' % (bot.name, chan))
cmnds.add('dblog-on', handle_dblogon, 'OPER')
def handle_dblogoff(bot, ievent):
try:
cfg.get('channels').remove((bot.name, ievent.channel))
cfg.save()
except ValueError:
ievent.reply('dblog is not enabled in (%s,%s)' % (bot.name, ievent.channel))
return
ievent.reply('dblog disabled on (%s,%s)' % (bot.name, ievent.channel))
cmnds.add('dblog-off', handle_dblogoff, 'OPER')