Mass overhaul to the decorators

This commit is contained in:
Vgr E.Barry 2015-06-01 16:37:22 -04:00
parent 416ded936d
commit ca2e901d58
3 changed files with 230 additions and 182 deletions

View File

@ -1,167 +1,222 @@
# Copyright (c) 2011, Jimmy Cao # Old, obsolete & original code by jcao219
# All rights reserved. # rewritten by Vgr
# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
# Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
# Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import fnmatch import fnmatch
from collections import defaultdict
import botconfig import botconfig
import src.settings as var
from oyoyo.parse import parse_nick from oyoyo.parse import parse_nick
from src import settings as var
from src import logger from src import logger
adminlog = logger(None) adminlog = logger(None)
def generate(fdict, permissions=True, **kwargs): COMMANDS = defaultdict(list)
"""Generates a decorator generator. Always use this""" HOOKS = defaultdict(list)
def cmd(*s, raw_nick=False, admin_only=False, owner_only=False, chan=True, pm=False,
game=False, join=False, none=False, playing=False, roles=(), hookid=-1): class cmd:
def dec(f): def __init__(self, *cmds, raw_nick=False, admin_only=False, owner_only=False,
def innerf(*args): chan=True, pm=False, join=False, none=False, game=False, playing=False, roles=()):
self.cmds = cmds
self.raw_nick = raw_nick
self.admin_only = admin_only
self.owner_only = owner_only
self.chan = chan
self.pm = pm
self.join = join
self.none = none
self.game = game
self.playing = playing
self.roles = roles
self.func = None
self.aftergame = False
self.name = cmds[0]
alias = False
self.aliases = []
for name in cmds:
for func in COMMANDS[name]:
if (func.owner_only != owner_only or
func.admin_only != admin_only):
raise ValueError("unmatching protection levels for " + func.name)
COMMANDS[name].append(self)
if alias:
self.aliases.append(self)
alias = True
def __call__(self, *args):
if self.func is None: # when function is defined; set self.func and call itself again
self.func = args[0]
self.__doc__ = self.func.__doc__
return self
largs = list(args) largs = list(args)
rawnick = largs[1]
if not permissions: cli, rawnick, chan, rest = largs
return f(*largs) nick, mode, user, cloak = parse_nick(rawnick)
if len(largs) > 1 and largs[1]:
nick, _, _, cloak = parse_nick(largs[1])
if cloak is None: if cloak is None:
cloak = "" cloak = ""
else:
nick = "" if not self.raw_nick:
cloak = ""
if not raw_nick and len(largs) > 1 and largs[1]:
largs[1] = nick largs[1] = nick
if nick == "<console>": if nick == "<console>":
return f(*largs) # special case; no questions return self.func(*largs) # special case; no questions
if not pm and largs[2] == nick: # PM command
return if not self.pm and chan == nick:
if not chan and largs[2] != nick: # channel command return # PM command, not allowed
return
if largs[2].startswith("#") and largs[2] != botconfig.CHANNEL and not admin_only and not owner_only: if not self.chan and chan != nick:
if "" in s: return # channel command, not allowed
return # Don't have empty commands triggering in other channels
allowed = False if chan.startswith("#") and chan != botconfig.CHANNEL and not (admin_only or owner_only):
for cmdname in s: if "" in self.cmds:
if cmdname in botconfig.ALLOWED_ALT_CHANNELS_COMMANDS: return # don't have empty commands triggering in other channels
allowed = True for command in self.cmds:
if command in botconfig.ALLOWED_ALT_CHANNELS_COMMANDS:
break break
if not allowed: else:
return return
if nick in var.USERS.keys() and var.USERS[nick]["account"] != "*":
if nick in var.USERS and var.USERS[nick]["account"] != "*":
acc = var.USERS[nick]["account"] acc = var.USERS[nick]["account"]
else: else:
acc = None acc = None
if "" in s:
return f(*largs) if "" in self.cmds:
if game and var.PHASE not in ("day", "night") + (("join",) if join else ()): return self.func(*largs)
largs[0].notice(nick, "No game is currently running.")
if self.game and var.PHASE not in ("day", "night") + (("join",) if self.join else ()):
if chan == nick:
pm(cli, nick, "No game is currently running.")
else:
cli.notice(nick, "No game is currently running.")
return return
if ((join and none and var.PHASE not in ("join", "none"))
or (none and not join and var.PHASE != "none")): if ((self.join and self.none and var.PHASE not in ("join", "none")) or
largs[0].notice(nick, "Sorry, but the game is already running. Try again next time.") (self.none and not self.join and var.PHASE != "none")):
if chan == nick:
pm(cli, nick, "Sorry, but the game is already running. Try again next time.")
else:
cli.notice(nick, "Sorry, but the game is already running. Try again next time.")
return return
if join and not none:
if self.join and not self.none:
if var.PHASE == "none": if var.PHASE == "none":
largs[0].notice(nick, "No game is currently running.") if chan == nick:
pm(cli, nick, "No game is currently running.")
else:
cli.notice(nick, "No game is currently running.")
return return
if var.PHASE != "join" and not game:
largs[0].notice(nick, "Werewolf is already in play.") if var.PHASE != "join" and not self.game:
if chan == nick:
pm(cli, nick, "Werewolf is already in play.")
else:
cli.notice(nick, "Werewolf is already in play.")
return return
if playing and (nick not in var.list_players() or nick in var.DISCONNECTED.keys()):
largs[0].notice(nick, "You're not currently playing.") if self.playing and (nick not in var.list_players() or nick in var.DISCONNECTED):
if chan == nick:
pm(cli, nick, "You're not currently playing.")
else:
cli.notice(nick, "You're not currently playing.")
return return
if roles:
for role in roles: if self.roles:
for role in self.roles:
if nick in var.ROLES[role]: if nick in var.ROLES[role]:
break break
else: else:
return return
return self.func(*largs) # don't check restrictions for role commands
if acc: if acc:
for pattern in var.DENY_ACCOUNTS.keys(): for pattern in var.DENY_ACCOUNTS:
if fnmatch.fnmatch(acc.lower(), pattern.lower()): if fnmatch.fnmatch(acc.lower(), pattern.lower()):
for cmdname in s: for command in self.cmds:
if cmdname in var.DENY_ACCOUNTS[pattern]: if command in var.DENY_ACCOUNTS[pattern]:
largs[0].notice(nick, "You do not have permission to use that command.") if chan == nick:
pm(cli, nick, "You do not have permission to use that command.")
else:
cli.notice(nick, "You do not have permission to use that command.")
return return
for pattern in var.ALLOW_ACCOUNTS.keys():
for pattern in var.ALLOW_ACCOUNTS:
if fnmatch.fnmatch(acc.lower(), pattern.lower()): if fnmatch.fnmatch(acc.lower(), pattern.lower()):
for cmdname in s: for command in self.cmds:
if cmdname in var.ALLOW_ACCOUNTS[pattern]: if command in var.ALLOW_ACCOUNTS[pattern]:
if admin_only or owner_only: if self.admin_only or self.owner_only:
adminlog(largs[2], rawnick, s[0], largs[3]) adminlog(chan, rawnick, self.name, rest)
return f(*largs) return self.func(*largs)
if not var.ACCOUNTS_ONLY and cloak: if not var.ACCOUNTS_ONLY and cloak:
for pattern in var.DENY.keys(): for pattern in var.DENY:
if fnmatch.fnmatch(cloak.lower(), pattern.lower()): if fnmatch.fnmatch(cloak.lower(), pattern.lower()):
for cmdname in s: for command in self.cmds:
if cmdname in var.DENY[pattern]: if command in var.DENY[pattern]:
largs[0].notice(nick, "You do not have permission to use that command.") if chan == nick:
pm(cli, nick, "You do not have permission to use that command.")
else:
cli.notice(nick, "You do not have permission to use that command.")
return return
for pattern in var.ALLOW.keys():
for pattern in var.ALLOW:
if fnmatch.fnmatch(cloak.lower(), pattern.lower()): if fnmatch.fnmatch(cloak.lower(), pattern.lower()):
for cmdname in s: for command in self.cmds:
if cmdname in var.ALLOW[pattern]: if command in var.ALLOW[pattern]:
if admin_only or owner_only: if self.admin_only or self.owner_only:
adminlog(largs[2], rawnick, s[0], largs[3]) adminlog(chan, rawnick, self.name, rest)
return f(*largs) # no questions return self.func(*largs)
if owner_only:
if self.owner_only:
if var.is_owner(nick, cloak): if var.is_owner(nick, cloak):
adminlog(largs[2], rawnick, s[0], largs[3]) adminlog(chan, rawnick, self.name, rest)
return f(*largs) return self.func(*largs)
if chan == nick:
pm(cli, nick, "You are not the owner.")
else: else:
largs[0].notice(nick, "You are not the owner.") cli.notice(nick, "You are not the owner.")
return return
if admin_only:
if self.admin_only:
if var.is_admin(nick, cloak): if var.is_admin(nick, cloak):
adminlog(largs[2], rawnick, s[0], largs[3]) adminlog(chan, rawnick, self.name, rest)
return f(*largs) return self.func(*largs)
if chan == nick:
pm(cli, nick, "You are not an admin.")
else: else:
largs[0].notice(nick, "You are not an admin.") cli.notice(nick, "You are not an admin.")
return return
return f(*largs)
alias = False
innerf.aliases = []
for x in s:
if x not in fdict.keys():
fdict[x] = []
else:
for fn in fdict[x]:
if (fn.owner_only != owner_only or
fn.admin_only != admin_only):
raise Exception("Command: "+x+" has non-matching protection levels!")
fdict[x].append(innerf)
if alias:
innerf.aliases.append(x)
alias = True
innerf.owner_only = owner_only
innerf.raw_nick = raw_nick
innerf.admin_only = admin_only
innerf.chan = chan
innerf.pm = pm
innerf.none = none
innerf.join = join
innerf.game = game
innerf.playing = playing
innerf.roles = roles
innerf.hookid = hookid
innerf.aftergame = False
innerf.__doc__ = f.__doc__
return innerf
return dec return self.func(*largs)
return lambda *args, **kwarargs: cmd(*args, **kwarargs) if kwarargs else cmd(*args, **kwargs) class hook:
def __init__(self, name, hookid=-1):
self.name = name
self.hookid = hookid
self.func = None
HOOKS[name].append(self)
def unhook(hdict, hookid): def __call__(self, *args):
for cmd in list(hdict.keys()): if self.func is None:
for x in hdict[cmd]: self.func = args[0]
if x.hookid == hookid: self.__doc__ = self.func.__doc__
hdict[cmd].remove(x) return self
if not hdict[cmd]: return self.func(*args)
del hdict[cmd]
@staticmethod
def unhook(hookid):
for each in list(HOOKS):
for inner in list(HOOKS[each]):
if inner.hookid == hookid:
HOOKS[each].remove(inner)
if not HOOKS[each]:
del HOOKS[each]

View File

@ -15,6 +15,7 @@ from src import wolfgame
log = logger("errors.log") log = logger("errors.log")
alog = logger(None) alog = logger(None)
hook = decorators.hook
def notify_error(cli, chan, target_logger): def notify_error(cli, chan, target_logger):
msg = "An error has occurred and has been logged." msg = "An error has occurred and has been logged."
@ -56,8 +57,7 @@ def on_privmsg(cli, rawnick, chan, msg, notice = False):
if chan == botconfig.NICK: if chan == botconfig.NICK:
chan = parse_nick(rawnick)[0] chan = parse_nick(rawnick)[0]
if "" in wolfgame.COMMANDS.keys(): for fn in decorators.COMMANDS[""]:
for fn in wolfgame.COMMANDS[""]:
try: try:
fn(cli, rawnick, chan, msg) fn(cli, rawnick, chan, msg)
except Exception: except Exception:
@ -67,7 +67,7 @@ def on_privmsg(cli, rawnick, chan, msg, notice = False):
notify_error(cli, chan, log) notify_error(cli, chan, log)
for x in set(list(COMMANDS.keys()) + list(wolfgame.COMMANDS.keys())): for x in decorators.COMMANDS:
if chan != parse_nick(rawnick)[0] and not msg.lower().startswith(botconfig.CMD_CHAR): if chan != parse_nick(rawnick)[0] and not msg.lower().startswith(botconfig.CMD_CHAR):
break # channel message but no prefix; ignore break # channel message but no prefix; ignore
if msg.lower().startswith(botconfig.CMD_CHAR+x): if msg.lower().startswith(botconfig.CMD_CHAR+x):
@ -77,7 +77,7 @@ def on_privmsg(cli, rawnick, chan, msg, notice = False):
else: else:
continue continue
if not h or h[0] == " ": if not h or h[0] == " ":
for fn in COMMANDS.get(x, []) + (wolfgame.COMMANDS.get(x, [])): for fn in decorators.COMMANDS.get(x, []):
try: try:
fn(cli, rawnick, chan, h.lstrip()) fn(cli, rawnick, chan, h.lstrip())
except Exception: except Exception:
@ -88,11 +88,11 @@ def on_privmsg(cli, rawnick, chan, msg, notice = False):
def unhandled(cli, prefix, cmd, *args): def unhandled(cli, prefix, cmd, *args):
if cmd in set(list(HOOKS.keys()) + list(wolfgame.HOOKS.keys())): if cmd in decorators.HOOKS:
largs = list(args) largs = list(args)
for i,arg in enumerate(largs): for i,arg in enumerate(largs):
if isinstance(arg, bytes): largs[i] = arg.decode('ascii') if isinstance(arg, bytes): largs[i] = arg.decode('ascii')
for fn in HOOKS.get(cmd, []) + wolfgame.HOOKS.get(cmd, []): for fn in decorators.HOOKS.get(cmd, []):
try: try:
fn(cli, prefix, *largs) fn(cli, prefix, *largs)
except Exception as e: except Exception as e:
@ -101,13 +101,6 @@ def unhandled(cli, prefix, cmd, *args):
else: else:
notify_error(cli, botconfig.CHANNEL, log) notify_error(cli, botconfig.CHANNEL, log)
COMMANDS = {}
HOOKS = {}
cmd = decorators.generate(COMMANDS)
hook = decorators.generate(HOOKS, raw_nick=True, permissions=False)
def connect_callback(cli): def connect_callback(cli):
def prepare_stuff(*args): def prepare_stuff(*args):
@ -147,7 +140,7 @@ def connect_callback(cli):
cli.nick(botconfig.NICK+"_") cli.nick(botconfig.NICK+"_")
cli.user(botconfig.NICK, "") cli.user(botconfig.NICK, "")
decorators.unhook(HOOKS, 239) hook.unhook(HOOKS, 239)
hook("unavailresource")(mustrelease) hook("unavailresource")(mustrelease)
hook("nicknameinuse")(mustregain) hook("nicknameinuse")(mustregain)

View File

@ -49,14 +49,12 @@ debuglog = logger("debug.log", write=False, display=False) # will be True if in
errlog = logger("errors.log") errlog = logger("errors.log")
plog = logger(None) #use this instead of print so that logs have timestamps plog = logger(None) #use this instead of print so that logs have timestamps
COMMANDS = {}
HOOKS = {}
is_admin = var.is_admin is_admin = var.is_admin
is_owner = var.is_owner is_owner = var.is_owner
cmd = decorators.generate(COMMANDS) cmd = decorators.cmd
hook = decorators.generate(HOOKS, raw_nick=True, permissions=False) hook = decorators.hook
COMMANDS = decorators.COMMANDS
# Game Logic Begins: # Game Logic Begins:
@ -228,7 +226,7 @@ def connect_callback(cli):
notify_error(cli, botconfig.CHANNEL, errlog) notify_error(cli, botconfig.CHANNEL, errlog)
# Unhook the WHO hooks # Unhook the WHO hooks
decorators.unhook(HOOKS, 295) hook.unhook(295)
#bot can be tricked into thinking it's still opped by doing multiple modes at once #bot can be tricked into thinking it's still opped by doing multiple modes at once
@ -378,6 +376,8 @@ def pm(cli, target, message): # message either privmsg or notice, depending on
cli.msg(target, message) cli.msg(target, message)
decorators.pm = pm
def reset_settings(): def reset_settings():
var.CURRENT_GAMEMODE.teardown() var.CURRENT_GAMEMODE.teardown()
var.CURRENT_GAMEMODE = var.GAME_MODES["default"][0]() var.CURRENT_GAMEMODE = var.GAME_MODES["default"][0]()
@ -898,7 +898,7 @@ def join_timer_handler(cli):
# I was lucky to catch this in testing, as it requires precise timing # I was lucky to catch this in testing, as it requires precise timing
# it only failed if a join happened while this outer func had started # it only failed if a join happened while this outer func had started
var.PINGING_IFS = False var.PINGING_IFS = False
decorators.unhook(HOOKS, 387) hook.unhook(387)
if to_ping: if to_ping:
to_ping.sort(key=lambda x: x.lower()) to_ping.sort(key=lambda x: x.lower())
@ -6703,7 +6703,7 @@ def show_admins(cli, nick, chan, rest):
else: else:
cli.msg(chan, msg) cli.msg(chan, msg)
decorators.unhook(HOOKS, 4) hook.unhook(4)
var.ADMIN_PINGING = False var.ADMIN_PINGING = False
if nick == chan: if nick == chan: