db stuff
This commit is contained in:
parent
3fde4692d1
commit
16ddabfe2a
5 changed files with 12 additions and 147 deletions
|
@ -45,6 +45,7 @@ def get_logger():
|
|||
logger.addHandler(sh)
|
||||
return logger
|
||||
|
||||
|
||||
class Bot(threading.Thread):
|
||||
def __init__(self):
|
||||
# basic variables
|
||||
|
@ -163,4 +164,4 @@ class Bot(threading.Thread):
|
|||
def restart(self, reason=None):
|
||||
"""shuts the bot down and restarts it"""
|
||||
self.do_restart = True
|
||||
self.stop(reason)
|
||||
self.stop(reason)
|
|
@ -106,7 +106,7 @@ class SendThread(threading.Thread):
|
|||
def run(self):
|
||||
while not self.shutdown:
|
||||
line = self.output_queue.get().splitlines()[0][:500]
|
||||
#print u"{}> {}".format(self.conn_name, line)
|
||||
print u"{}> {}".format(self.conn_name, line)
|
||||
self.output_buffer += line.encode('utf-8', 'replace') + '\r\n'
|
||||
while self.output_buffer:
|
||||
sent = self.socket.send(self.output_buffer)
|
||||
|
|
12
core/main.py
12
core/main.py
|
@ -73,6 +73,10 @@ def run(bot, func, input):
|
|||
except:
|
||||
bot.logger.exception("Error in plugin {}:".format(func._filename))
|
||||
return
|
||||
finally:
|
||||
if uses_db:
|
||||
print "Close"
|
||||
input.db.close()
|
||||
else:
|
||||
kw = dict((key, input[key]) for key in args if key in input)
|
||||
try:
|
||||
|
@ -80,6 +84,10 @@ def run(bot, func, input):
|
|||
except:
|
||||
bot.logger.exception("Error in plugin {}:".format(func._filename))
|
||||
return
|
||||
finally:
|
||||
if uses_db:
|
||||
print "Close"
|
||||
input.db.close()
|
||||
else:
|
||||
try:
|
||||
out = func(input.inp)
|
||||
|
@ -89,10 +97,6 @@ def run(bot, func, input):
|
|||
if out is not None:
|
||||
input.reply(unicode(out))
|
||||
|
||||
if uses_db:
|
||||
# close SQLAlchemy session
|
||||
input.db.close()
|
||||
|
||||
|
||||
def do_sieve(sieve, bot, input, func, type, args):
|
||||
try:
|
||||
|
|
|
@ -3,18 +3,6 @@ from util import hook
|
|||
from sqlalchemy import Table, Column, Integer, String, MetaData, ForeignKey
|
||||
from sqlalchemy.orm import scoped_session, sessionmaker
|
||||
|
||||
def create_tables(metadata):
|
||||
users_table = Table('users', metadata,
|
||||
Column('id', Integer, primary_key=True),
|
||||
Column('name', String),
|
||||
Column('fullname', String),
|
||||
Column('password', String)
|
||||
)
|
||||
users_table.create()
|
||||
|
||||
|
||||
@hook.command
|
||||
def dbtest(inp, db=None):
|
||||
metadata = MetaData(db)
|
||||
create_tables(metadata)
|
||||
print metadata
|
||||
print db
|
|
@ -1,128 +0,0 @@
|
|||
from util import hook
|
||||
|
||||
|
||||
# Default value.
|
||||
# If True, all channels without a setting will have regex enabled
|
||||
# If False, all channels without a setting will have regex disabled
|
||||
default_enabled = True
|
||||
|
||||
db_already_initiated = False
|
||||
|
||||
|
||||
def db_init(db):
|
||||
global db_already_initiated
|
||||
if not db_already_initiated:
|
||||
db_already_initiated = True
|
||||
db.execute("CREATE TABLE IF NOT EXISTS regexchans(channel PRIMARY KEY, status)")
|
||||
db.commit()
|
||||
|
||||
|
||||
def get_status(db, channel):
|
||||
row = db.execute("SELECT status FROM regexchans WHERE channel = ?", [channel]).fetchone()
|
||||
if row:
|
||||
return row[0]
|
||||
else:
|
||||
return None
|
||||
|
||||
|
||||
def set_status(db, channel, status):
|
||||
row = db.execute("REPLACE INTO regexchans (channel, status) VALUES(?, ?)", [channel, status])
|
||||
db.commit()
|
||||
|
||||
|
||||
def delete_status(db, channel):
|
||||
row = db.execute("DELETE FROM regexchans WHERE channel = ?", [channel])
|
||||
db.commit()
|
||||
|
||||
|
||||
def list_status(db):
|
||||
row = db.execute("SELECT * FROM regexchans").fetchall()
|
||||
result = None
|
||||
for values in row:
|
||||
if result:
|
||||
result += u", {}: {}".format(values[0], values[1])
|
||||
else:
|
||||
result = u"{}: {}".format(values[0], values[1])
|
||||
return result
|
||||
|
||||
|
||||
@hook.sieve
|
||||
def sieve_regex(bot, inp, func, kind, args):
|
||||
db = bot.get_db_connection(inp.conn)
|
||||
db_init(db)
|
||||
if kind == 'regex' and inp.chan.startswith("#") and func.__name__ != 'factoid':
|
||||
chanstatus = get_status(db, inp.chan)
|
||||
if chanstatus != "ENABLED" and (chanstatus == "DISABLED" or not default_enabled):
|
||||
print u"Denying input.raw={}, kind={}, args={} from {}".format(inp.raw, kind, args, inp.chan)
|
||||
return None
|
||||
print u"Allowing input.raw={}, kind={}, args={} from {}".format(inp.raw, kind, args, inp.chan)
|
||||
|
||||
return inp
|
||||
|
||||
|
||||
@hook.command(permissions=["botcontrol"])
|
||||
def enableregex(inp, db=None, message=None, notice=None, chan=None, nick=None):
|
||||
db_init(db)
|
||||
inp = inp.strip().lower()
|
||||
if not inp:
|
||||
channel = chan
|
||||
elif inp.startswith("#"):
|
||||
channel = inp
|
||||
else:
|
||||
channel = u"#{}".format(inp)
|
||||
|
||||
message(u"Enabling regex matching (youtube, etc) (issued by {})".format(nick), target=channel)
|
||||
notice(u"Enabling regex matching (youtube, etc) in channel {}".format(channel))
|
||||
set_status(db, channel, "ENABLED")
|
||||
|
||||
|
||||
@hook.command(permissions=["botcontrol"])
|
||||
def disableregex(inp, db=None, message=None, notice=None, chan=None, nick=None):
|
||||
db_init(db)
|
||||
inp = inp.strip().lower()
|
||||
if not inp:
|
||||
channel = chan
|
||||
elif inp.startswith("#"):
|
||||
channel = inp
|
||||
else:
|
||||
channel = u"#{}".format(inp)
|
||||
|
||||
message(u"Disabling regex matching (youtube, etc) (issued by {})".format(nick), target=channel)
|
||||
notice(u"Disabling regex matching (youtube, etc) in channel {}".format(channel))
|
||||
set_status(db, channel, "DISABLED")
|
||||
|
||||
|
||||
@hook.command(permissions=["botcontrol"])
|
||||
def resetregex(inp, db=None, message=None, notice=None, chan=None, nick=None):
|
||||
db_init(db)
|
||||
inp = inp.strip().lower()
|
||||
if not inp:
|
||||
channel = chan
|
||||
elif inp.startswith("#"):
|
||||
channel = inp
|
||||
else:
|
||||
channel = u"#{}".format(inp)
|
||||
|
||||
message(u"Resetting regex matching setting (youtube, etc) (issued by {})".format(nick), target=channel)
|
||||
notice(u"Resetting regex matching setting (youtube, etc) in channel {}".format(channel))
|
||||
delete_status(db, channel)
|
||||
|
||||
|
||||
@hook.command(permissions=["botcontrol"])
|
||||
def regexstatus(inp, db=None, chan=None):
|
||||
db_init(db)
|
||||
inp = inp.strip().lower()
|
||||
if not inp:
|
||||
channel = chan
|
||||
elif inp.startswith("#"):
|
||||
channel = inp
|
||||
else:
|
||||
channel = u"#{}".format(inp)
|
||||
|
||||
return u"Regex status for {}: {}".format(channel, get_status(db, channel))
|
||||
|
||||
|
||||
@hook.command(permissions=["botcontrol"])
|
||||
def listregex(inp, db=None):
|
||||
db_init(db)
|
||||
return list_status(db)
|
Reference in a new issue