beginning of log plugin

This commit is contained in:
Ryan Hitchman 2009-04-17 15:54:11 -06:00
parent 61a8706fed
commit b88b107343
3 changed files with 81 additions and 7 deletions

9
bot.py
View File

@ -88,19 +88,21 @@ class Input(object):
self.msg = msg self.msg = msg
if command == "PRIVMSG": if command == "PRIVMSG":
self.chan = paraml[0] self.chan = paraml[0]
else:
self.chan = ""
class FakeBot(object): class FakeBot(object):
def __init__(self, bot, input, func): def __init__(self, bot, input, func):
self.bot = bot self.bot = bot
self.persist_dir = bot.persist_dir self.persist_dir = bot.persist_dir
self.network = bot.network
self.input = input self.input = input
self.msg = bot.irc.msg self.msg = bot.irc.msg
self.cmd = bot.irc.cmd self.cmd = bot.irc.cmd
self.join = bot.irc.join self.join = bot.irc.join
self.func = func self.func = func
self.doreply = True self.doreply = True
if input.command == "PRIVMSG": self.chan = input.chan
self.chan = input.paraml[0]
def say(self, msg): def say(self, msg):
self.bot.irc.msg(self.input.paraml[0], msg) self.bot.irc.msg(self.input.paraml[0], msg)
@ -124,6 +126,7 @@ while True:
try: try:
out = bot.irc.out.get(timeout=1) out = bot.irc.out.get(timeout=1)
reload_plugins() reload_plugins()
printed = False
for csig, func, args in (bot.plugs['command'] + bot.plugs['event']): for csig, func, args in (bot.plugs['command'] + bot.plugs['event']):
input = Input(*out) input = Input(*out)
for fsig, sieve in bot.plugs['sieve']: for fsig, sieve in bot.plugs['sieve']:
@ -136,7 +139,9 @@ while True:
break break
if input == None: if input == None:
continue continue
if not printed:
print '<<<', input.raw print '<<<', input.raw
printed = True
thread.start_new_thread(FakeBot(bot, input, func).run, ()) thread.start_new_thread(FakeBot(bot, input, func).run, ())
except Queue.Empty: except Queue.Empty:
pass pass

View File

@ -39,8 +39,8 @@ def command(func=None, hook=None, **kwargs):
else: else:
return command_wrapper(func) return command_wrapper(func)
def event(arg): def event(arg=None, **kwargs):
args = {} args = kwargs
def event_wrapper(func): def event_wrapper(func):
if func.func_code.co_argcount != 2: if func.func_code.co_argcount != 2:
raise ValueError, \ raise ValueError, \
@ -52,7 +52,8 @@ def event(arg):
return func return func
if _isfunc(arg): if _isfunc(arg):
return event_wrapper(arg) return event_wrapper(arg, kwargs)
else: else:
if arg is not None:
args['events'] = arg.split() args['events'] = arg.split()
return event_wrapper return event_wrapper

68
plugins/log.py Normal file
View File

@ -0,0 +1,68 @@
"""
log.py: written by Scaevolus 2009
"""
from __future__ import with_statement
import os
import thread
import codecs
import time
import hook
lock = thread.allocate_lock()
log_fds = {} # '%(net)s %(chan)s' : (filename, fd)
timestamp_format = '%H:%M:%S'
def get_log_filename(dir, network, chan):
return os.path.join(dir, 'log', gmtime('%Y'), network,
gmtime('%%s.%m-%d.log') % chan)
def gmtime(format):
return time.strftime(format, time.gmtime())
def load_memory(filename, mtimes={}):
if not os.path.exists(filename):
return {}
mtime = os.stat(filename).st_mtime
if mtimes.get(filename, 0) != mtime:
mtimes[filename] = mtime
return dict((x.split(None, 1)[0].lower(), x.strip()) for x in
codecs.open(filename, 'r', 'utf-8'))
def save_memory(filename, memory):
out = codecs.open(filename, 'w', 'utf-8')
out.write('\n'.join(sorted(memory.itervalues())))
out.flush()
out.close()
def get_log_fd(dir, network, chan):
fn = get_log_filename(dir, network, chan)
cache_key = '%s %s' % (network, chan)
filename, fd = log_fds.get(cache_key, ('', 0))
if fn != filename: # we need to open a file for writing
if fd != 0: # is a valid fd
fd.flush()
fd.close()
dir = os.path.split(fn)[0]
if not os.path.exists(dir):
os.makedirs(dir)
fd = codecs.open(fn, 'wab', 'utf-8')
log_fds[cache_key] = (fn, fd)
return fd
@hook.event(ignorebots=False)
def log(bot, input):
".remember <word> <data> -- maps word to data in the memory"
with lock:
fd = get_log_fd(bot.persist_dir, bot.network, 'RAW')
fd.write(gmtime(timestamp_format) + ' ' + input.raw + '\n')
if input.chan:
fd = get_log_fd(bot.persist_dir, bot.network, input.chan)
fd.write(gmtime(timestamp_format) + ' ' + input.raw + '\n')