make persistent db name include bot nick

This commit is contained in:
Ryan Hitchman 2010-03-13 07:02:05 -07:00
parent 892f858643
commit 30baa6c49c
6 changed files with 33 additions and 31 deletions

View File

@ -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

View File

@ -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):

View File

@ -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')

View File

@ -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,

View File

@ -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)

View File

@ -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)