diff --git a/core/db.py b/core/db.py index 1f7209f..c2400db 100644 --- a/core/db.py +++ b/core/db.py @@ -2,9 +2,13 @@ import os import sqlite3 -def get_db_connection(server, name='skybot.%s.db'): +def get_db_connection(conn, name=''): "returns an sqlite3 connection to a persistent database" - filename = os.path.join(bot.persist_dir, name % server) + + if not name: + name = '%s.%s.db' % (conn.nick, conn.server) + + filename = os.path.join(bot.persist_dir, name) return sqlite3.connect(filename, timeout=10) bot.get_db_connection = get_db_connection diff --git a/core/main.py b/core/main.py index 7eecd5c..ce60bb1 100644 --- a/core/main.py +++ b/core/main.py @@ -27,6 +27,7 @@ class Input(dict): paraml=paraml, msg=msg, server=conn.server, chan=chan, say=say, reply=reply, pm=pm, bot=bot, lastparam=paraml[-1]) + # make dict keys accessible as attributes def __getattr__(self, key): return self[key] @@ -42,18 +43,18 @@ def run(func, input): if args: if 'db' in args: - input['db'] = get_db_connection(input['server']) + input.db = get_db_connection(input.conn) if 'input' in args: - input['input'] = input + input.input = input if 0 in args: - out = func(input['inp'], **input) + out = func(input.inp, **input) else: kw = dict((key, input[key]) for key in args if key in input) - out = func(input['inp'], **kw) + out = func(input.inp, **kw) else: - out = func(input['inp']) + out = func(input.inp) if out is not None: - input['reply'](unicode(out)) + input.reply(unicode(out)) def do_sieve(sieve, bot, input, func, type, args): diff --git a/plugins/log.py b/plugins/log.py index ce1bf1f..cc6b62e 100644 --- a/plugins/log.py +++ b/plugins/log.py @@ -11,7 +11,6 @@ import re from util import hook -lock = thread.allocate_lock() log_fds = {} # '%(net)s %(chan)s' : (filename, fd) timestamp_format = '%H:%M:%S' @@ -86,24 +85,23 @@ def get_log_fd(dir, server, chan): @hook.thread @hook.event('*') def log(paraml, input=None, bot=None): - with lock: - timestamp = gmtime(timestamp_format) + timestamp = gmtime(timestamp_format) - fd = get_log_fd(bot.persist_dir, input.server, 'raw') - fd.write(timestamp + ' ' + input.raw + '\n') + fd = get_log_fd(bot.persist_dir, input.server, 'raw') + fd.write(timestamp + ' ' + input.raw + '\n') - if input.command == 'QUIT': # these are temporary fixes until proper - input.chan = 'quit' # presence tracking is implemented - if input.command == 'NICK': - input.chan = 'nick' + if input.command == 'QUIT': # these are temporary fixes until proper + input.chan = 'quit' # presence tracking is implemented + if input.command == 'NICK': + input.chan = 'nick' - beau = beautify(input) + beau = beautify(input) - if beau == '': # don't log this - return + if beau == '': # don't log this + return - if input.chan: - fd = get_log_fd(bot.persist_dir, input.server, input.chan) - fd.write(timestamp + ' ' + beau + '\n') + if input.chan: + fd = get_log_fd(bot.persist_dir, input.server, input.chan) + fd.write(timestamp + ' ' + beau + '\n') - print timestamp, input.chan, beau.encode('utf8', 'ignore') + print timestamp, input.chan, beau.encode('utf8', 'ignore') diff --git a/plugins/seen.py b/plugins/seen.py index 152c7d6..3b27d26 100644 --- a/plugins/seen.py +++ b/plugins/seen.py @@ -8,7 +8,7 @@ from util import hook, timesince @hook.thread @hook.event('PRIVMSG') def seeninput(paraml, input=None, bot=None): - db = bot.get_db_connection(input.server) + db = bot.get_db_connection(input.conn) db_init(db) db.execute("insert or replace into seen(name, time, quote, chan)" "values(?,?,?,?)", (input.nick.lower(), time.time(), input.msg, diff --git a/plugins/tell.py b/plugins/tell.py index 2951b07..350347b 100644 --- a/plugins/tell.py +++ b/plugins/tell.py @@ -18,7 +18,7 @@ def tellinput(paraml, input=None, bot=None): if 'showtells' in input.msg.lower(): return - db = bot.get_db_connection(input.server) + db = bot.get_db_connection(input.conn) db = db_init(db) tells = get_tells(db, input.nick, input.chan) diff --git a/plugins/urlhistory.py b/plugins/urlhistory.py index e6eaadf..6d6ee6c 100644 --- a/plugins/urlhistory.py +++ b/plugins/urlhistory.py @@ -11,9 +11,8 @@ expiration_period = 60 * 60 * 24 # 1 day ignored_urls = [urlnorm.normalize("http://google.com")] -def db_connect(bot, server): - "check to see that our db has the the seen table and return a dbection." - db = bot.get_db_connection(server) +def db_connect(bot, conn): + db = bot.get_db_connection(conn) db.execute("create table if not exists urlhistory" "(chan, url, nick, time)") db.commit() @@ -68,8 +67,8 @@ def format_reply(history): @hook.regex(url_re) -def urlinput(match, nick='', chan='', server='', reply=None, bot=None): - db = db_connect(bot, server) +def urlinput(match, nick='', chan='', conn=None, bot=None): + db = db_connect(bot, conn) url = urlnorm.normalize(match.group().encode('utf-8')) if url not in ignored_urls: history = get_history(db, chan, url)