gozerplugs plugs jlog py log irc channels to hour min nick txt format

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# 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')