save !fdeny / !fallow in database, also allow commands to be added / removed individually

This commit is contained in:
jacob1 2014-11-03 13:37:24 -05:00
parent aee5bce1b0
commit c514296a80
2 changed files with 66 additions and 37 deletions

View File

@ -5165,32 +5165,50 @@ def allow_deny(cli, nick, chan, rest, mode):
else: else:
cloak = user cloak = user
if len(data) == 1: if len(data) == 1: #list commands for a specific hostmask
if cloak in variable: if cloak in variable:
msg = "\u0002{0}\u0002 ({1}) is {2} the following commands: {3}.".format( msg = "\u0002{0}\u0002 ({1}) is {2} the following {3}commands: {4}.".format(
data[0], cloak, "allowed" if mode == "allow" else "denied", ", ".join(variable[cloak])) data[0], cloak, "allowed" if mode == 'allow' else "denied", "special " if mode == 'allow' else "", ", ".join(variable[cloak]))
else: else:
msg = "\u0002{0}\u0002 ({1}) is not {2} commands.".format(data[0], cloak, "allowed any special" if mode == "allow" else "denied any") msg = "\u0002{0}\u0002 ({1}) is not {2} commands.".format(data[0], cloak, "allowed any special" if mode == 'allow' else "denied any")
else: else:
if cloak not in variable:
variable[cloak] = []
commands = data[1:] commands = data[1:]
variable[cloak] = () for command in commands: #add or remove commands one at a time to a specific hostmask
for command in commands: if command[0] == '-': #starting with - removes
if command.startswith(botconfig.CMD_CHAR): rem = True
command = command[1:]
else:
rem = False
if command.startswith(botconfig.CMD_CHAR): #ignore command prefix
command = command[len(botconfig.CMD_CHAR):] command = command[len(botconfig.CMD_CHAR):]
if (command in COMMANDS or command in PM_COMMANDS) and command not in ["fdeny", "fallow", "exec", "eval"]:
variable[cloak] += (command,) if not rem:
if (command in COMMANDS or command in PM_COMMANDS) and command not in ["fdeny", "fallow", "exec", "eval"]:
variable[cloak].append(command)
if mode == "allow":
var.add_allow(cloak, command)
else:
var.add_deny(cloak, command)
elif command in variable[cloak]:
variable[cloak].remove(command)
if mode == "allow":
var.remove_allow(cloak, command)
else:
var.remove_deny(cloak, command)
if len(variable[cloak]): if len(variable[cloak]):
msg = "\u0002{0}\u0002 ({1}) is now {2} the following commands: {3}{4}.".format( msg = "\u0002{0}\u0002 ({1}) is now {2} the following {3}commands: {4}{5}.".format(
data[0], cloak, "allowed" if mode == "allow" else "denied", botconfig.CMD_CHAR, ", {0}".format(botconfig.CMD_CHAR).join(variable[cloak])) data[0], cloak, "allowed" if mode == 'allow' else "denied", "special " if mode == 'allow' else "", botconfig.CMD_CHAR, ", {0}".format(botconfig.CMD_CHAR).join(variable[cloak]))
else: else:
del variable[cloak] del variable[cloak]
msg = "\u0002{0}\u0002 ({1}) is no longer {2} commands.".format(data[0], cloak, "allowed any special" if mode == "allow" else "denied any") msg = "\u0002{0}\u0002 ({1}) is no longer {2} commands.".format(data[0], cloak, "allowed any special" if mode == 'allow' else "denied any")
elif variable: elif variable: #list allowed / denied commands for all hostmasks
msg = "{0}: {1}".format("allowed" if mode == "allow" else "denied", ", ".join( msg = "{0}: {1}".format("Allowed" if mode == 'allow' else "Denied", ", ".join(
"\u0002{0}\u0002 ({1}{2})".format(cloak, botconfig.CMD_CHAR, ", {0}".format(botconfig.CMD_CHAR).join(denied)) "\u0002{0}\u0002 ({1}{2})".format(cloak, botconfig.CMD_CHAR, ", {0}".format(botconfig.CMD_CHAR).join(denied))
for cloak, denied in variable.items())) for cloak, denied in variable.items()))
else: else: #deny / allow is empty
msg = "Nobody is {0} commands.".format("allowed any special" if mode == "allow" else "denied any") msg = "Nobody is {0} commands.".format("allowed any special" if mode == 'allow' else "denied any")
if msg: if msg:
if data: if data:
@ -5217,19 +5235,8 @@ def deny(cli, nick, chan, rest):
@cmd("fallow", admin_only=True) @cmd("fallow", admin_only=True)
def fallow(cli, nick, chan, rest): def fallow(cli, nick, chan, rest):
"""Allow someone to use an admin command.""" """Allow someone to use an admin command."""
allow(cli, nick, chan, rest) allow(cli, nick, chan, rest)
# TODO: Remove this after code to save this to the database has been
# implemented.
msg = ("\u0002Warning:\u0002 This change has not been saved to the "
"database. It will be lost after a restart.")
if chan == nick:
pm(cli, nick, msg)
else:
cli.notice(nick, msg)
@pmcmd("fallow", admin_only=True) @pmcmd("fallow", admin_only=True)
def fallow_pm(cli, nick, rest): def fallow_pm(cli, nick, rest):
@ -5241,16 +5248,6 @@ def fdeny(cli, nick, chan, rest):
"""Deny someone from using a command.""" """Deny someone from using a command."""
deny(cli, nick, chan, rest) deny(cli, nick, chan, rest)
# TODO: Remove this after code to save this to the database has been
# implemented.
msg = ("\u0002Warning:\u0002 This change has not been saved to the "
"database. It will be lost after a restart.")
if chan == nick:
pm(cli, nick, msg)
else:
cli.notice(nick, msg)
@pmcmd("fdeny", admin_only=True) @pmcmd("fdeny", admin_only=True)
def fdeny_pm(cli, nick, rest): def fdeny_pm(cli, nick, rest):

View File

@ -466,6 +466,10 @@ with conn:
c.execute('CREATE TABLE IF NOT EXISTS stasised (cloak TEXT, games INTEGER, UNIQUE(cloak))') # stasised people c.execute('CREATE TABLE IF NOT EXISTS stasised (cloak TEXT, games INTEGER, UNIQUE(cloak))') # stasised people
c.execute('CREATE TABLE IF NOT EXISTS denied (cloak TEXT, command TEXT, UNIQUE(cloak, command))') # botconfig.DENY
c.execute('CREATE TABLE IF NOT EXISTS allowed (cloak TEXT, command TEXT, UNIQUE(cloak, command))') # botconfig.ALLOW
c.execute('SELECT * FROM away') c.execute('SELECT * FROM away')
for row in c: for row in c:
AWAY.append(row[0]) AWAY.append(row[0])
@ -482,6 +486,18 @@ with conn:
for row in c: for row in c:
STASISED[row[0]] = row[1] STASISED[row[0]] = row[1]
c.execute('SELECT * FROM denied')
for row in c:
if row[0] not in botconfig.DENY:
botconfig.DENY[row[0]] = []
botconfig.DENY[row[0]].append(row[1])
c.execute('SELECT * FROM allowed')
for row in c:
if row[0] not in botconfig.ALLOW:
botconfig.ALLOW[row[0]] = []
botconfig.ALLOW[row[0]].append(row[1])
# populate the roles table # populate the roles table
c.execute('DROP TABLE IF EXISTS roles') c.execute('DROP TABLE IF EXISTS roles')
c.execute('CREATE TABLE roles (id INTEGER PRIMARY KEY AUTOINCREMENT, role TEXT)') c.execute('CREATE TABLE roles (id INTEGER PRIMARY KEY AUTOINCREMENT, role TEXT)')
@ -546,6 +562,22 @@ def set_stasis(clk, games):
else: else:
c.execute('INSERT OR REPLACE INTO stasised VALUES (?,?)', (clk, games)) c.execute('INSERT OR REPLACE INTO stasised VALUES (?,?)', (clk, games))
def add_deny(clk, command):
with conn:
c.execute('INSERT OR REPLACE INTO denied VALUES (?,?)', (clk, command))
def remove_deny(clk, command):
with conn:
c.execute('DELETE FROM denied WHERE cloak=? AND command=?', (clk, command))
def add_allow(clk, command):
with conn:
c.execute('INSERT OR REPLACE INTO allowed VALUES (?,?)', (clk, command))
def remove_allow(clk, command):
with conn:
c.execute('DELETE FROM allowed WHERE cloak=? AND command=?', (clk, command))
def update_role_stats(acc, role, won, iwon): def update_role_stats(acc, role, won, iwon):
with conn: with conn: