From c514296a80ca9fa5dbe19c317140ce04ad7267da Mon Sep 17 00:00:00 2001 From: jacob1 Date: Mon, 3 Nov 2014 13:37:24 -0500 Subject: [PATCH] save !fdeny / !fallow in database, also allow commands to be added / removed individually --- modules/wolfgame.py | 71 +++++++++++++++++++++----------------------- settings/wolfgame.py | 32 ++++++++++++++++++++ 2 files changed, 66 insertions(+), 37 deletions(-) diff --git a/modules/wolfgame.py b/modules/wolfgame.py index b92ca2b..e9703c0 100644 --- a/modules/wolfgame.py +++ b/modules/wolfgame.py @@ -5165,32 +5165,50 @@ def allow_deny(cli, nick, chan, rest, mode): else: cloak = user - if len(data) == 1: + if len(data) == 1: #list commands for a specific hostmask if cloak in variable: - msg = "\u0002{0}\u0002 ({1}) is {2} the following commands: {3}.".format( - data[0], cloak, "allowed" if mode == "allow" else "denied", ", ".join(variable[cloak])) + msg = "\u0002{0}\u0002 ({1}) is {2} the following {3}commands: {4}.".format( + data[0], cloak, "allowed" if mode == 'allow' else "denied", "special " if mode == 'allow' else "", ", ".join(variable[cloak])) 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: + if cloak not in variable: + variable[cloak] = [] commands = data[1:] - variable[cloak] = () - for command in commands: - if command.startswith(botconfig.CMD_CHAR): + for command in commands: #add or remove commands one at a time to a specific hostmask + if command[0] == '-': #starting with - removes + rem = True + command = command[1:] + else: + rem = False + if command.startswith(botconfig.CMD_CHAR): #ignore command prefix 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]): - msg = "\u0002{0}\u0002 ({1}) is now {2} the following commands: {3}{4}.".format( - data[0], cloak, "allowed" if mode == "allow" else "denied", botconfig.CMD_CHAR, ", {0}".format(botconfig.CMD_CHAR).join(variable[cloak])) + msg = "\u0002{0}\u0002 ({1}) is now {2} the following {3}commands: {4}{5}.".format( + 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: 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") - elif variable: - msg = "{0}: {1}".format("allowed" if mode == "allow" else "denied", ", ".join( + 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: #list allowed / denied commands for all hostmasks + 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)) for cloak, denied in variable.items())) - else: - msg = "Nobody is {0} commands.".format("allowed any special" if mode == "allow" else "denied any") + else: #deny / allow is empty + msg = "Nobody is {0} commands.".format("allowed any special" if mode == 'allow' else "denied any") if msg: if data: @@ -5217,19 +5235,8 @@ def deny(cli, nick, chan, rest): @cmd("fallow", admin_only=True) def fallow(cli, nick, chan, rest): """Allow someone to use an admin command.""" - 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) def fallow_pm(cli, nick, rest): @@ -5241,16 +5248,6 @@ def fdeny(cli, nick, chan, rest): """Deny someone from using a command.""" 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) def fdeny_pm(cli, nick, rest): diff --git a/settings/wolfgame.py b/settings/wolfgame.py index 8fb0da7..0cac5e2 100644 --- a/settings/wolfgame.py +++ b/settings/wolfgame.py @@ -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 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') for row in c: AWAY.append(row[0]) @@ -482,6 +486,18 @@ with conn: for row in c: 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 c.execute('DROP TABLE IF EXISTS roles') c.execute('CREATE TABLE roles (id INTEGER PRIMARY KEY AUTOINCREMENT, role TEXT)') @@ -546,6 +562,22 @@ def set_stasis(clk, games): else: 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): with conn: