def execute(self, player, args): if locks.authority() is not locks.SYSTEM: # When user code is implemented, this will create an untrusted REPL # under the player's authority. raise utils.UserError("Not yet implemented: for now, sudo is " "required.") player.send("***********") player.send("* WARNING *") player.send("***********") player.send("") player.send("You are working interactively with the actual Python " "process running the game. Careless actions here can " "really permanently foul up important things.") player.send("") def check_password(line): if player.hash(line) == player.password: with locks.authority_of(locks.SYSTEM): player.enter_mode(PythonMode(player)) else: player.send("Incorrect.") d = handler.prompt(player, "To proceed, enter your password:") d.addCallback(check_password)
def __init__(self, player): if locks.authority() is not locks.SYSTEM: raise locks.LockFailedError( "PythonMode requires system authority.") self.console = code.InteractiveConsole() player.send(">>>")
def __setstate__(self, state): """ Restore self.__dict__ to the given state. See documentation for __getstate__. """ if locks.authority() is locks.SYSTEM: self.__dict__ = state
def lock_attr(self, attr, owner=None, get_lock=None, set_lock=None): if not hasattr(self, attr): raise KeyError("{} has no attribute {}.".format(self, attr)) with locks.authority_of(locks.SYSTEM): lock = self.attr_locks[attr] if (locks.authority() is not lock.owner and locks.authority() is not locks.SYSTEM): raise locks.LockFailedError("You don't own that attribute.") if owner is None and get_lock is None and set_lock is None: raise TypeError("Specify at least one of owner, get_lock, set_lock") if owner is not None: lock.owner = owner if get_lock is not None: lock.get_lock = get_lock if set_lock is not None: lock.set_lock = set_lock
def lock_attr(self, attr, owner=None, get_lock=None, set_lock=None): if not hasattr(self, attr): raise KeyError("{} has no attribute {}.".format(self, attr)) with locks.authority_of(locks.SYSTEM): lock = self.attr_locks[attr] if (locks.authority() is not lock.owner and locks.authority() is not locks.SYSTEM): raise locks.LockFailedError("You don't own that attribute.") if owner is None and get_lock is None and set_lock is None: raise TypeError( "Specify at least one of owner, get_lock, set_lock") if owner is not None: lock.owner = owner if get_lock is not None: lock.get_lock = get_lock if set_lock is not None: lock.set_lock = set_lock
def __init__(self, name, location=None, owner=None): """ Create a brand-new object and add it to the database. Args: name, location (default None), owner (defaults to current authority) as described in the class docstring """ lock = locks.AttributeLock(locks.SYSTEM, locks.Fail(), locks.Fail()) super(Object, self).__setattr__("attr_locks", {"attr_locks": lock}) if owner is not None: owner_ = owner else: if locks.authority() is not None: owner_ = locks.authority() else: raise locks.MissingAuthorityError("Object created with no " "owner and no authority.") with locks.authority_of(locks.SYSTEM): self.uid = None # This will be assigned when we call store() self.type = 'thing' self.owner = owner_ self.name = name self.lock_attr("name", set_lock=locks.Owns(self)) self.lock_attr("owner", set_lock=locks.Owns(self)) self.locks = Locks() self.lock_attr("locks", set_lock=locks.Fail()) self._location = None with locks.authority_of(self.owner): self.description = "You see nothing special." self.locks.take = locks.Pass() self.locks.drop = locks.Pass() self.locks.insert = locks.Is(self) self.locks.remove = locks.Is(self) self.locks.destroy = locks.Owns(self) if location: self.location = location
def __getattribute__(self, attr): if attr == "__dict__" and locks.authority() is locks.SYSTEM: # This comes up when we're unpickling the db, and attr_locks # doesn't exist yet return super(Object, self).__getattribute__(attr) attr_locks = super(Object, self).__getattribute__("attr_locks") if attr in attr_locks: if attr_locks[attr].get_lock(): # Lock passes; grant access return super(Object, self).__getattribute__(attr) else: # Lock fails; deny access raise locks.LockFailedError("You don't have permission to get " "{} from {}.".format(attr, self)) else: # No lock is defined; grant access return super(Object, self).__getattribute__(attr)
def location(self, destination): origin = self.location if not destination.locks.insert(): raise locks.LockFailedError("You can't put that in {}.".format( destination.name)) if origin and not origin.locks.remove(): raise locks.LockFailedError( "You can't remove that from {}.".format(origin.name)) player = locks.authority() if destination == player: if not self.locks.take(): raise locks.LockFailedError("You cannot take {}.".format( self.name)) elif origin == player: if not self.locks.drop(): raise locks.LockFailedError("You cannot drop {}.".format( self.name)) # Locks passed or non-applicable. Proceed with the move. with locks.authority_of(locks.SYSTEM): self._location = destination with locks.authority_of(self): # this gets to use self authority because it should always happen, # regardless of the reason location is being changed. # it does NOT get to use system authority because it's sometimes # (always, for players) how position gets initialized, which locks # the object out of its own position attribute. if destination is not origin: # whatever we were doing there, we're not doing it any more self.position = None # Trigger a "look" command so we see our new surroundings from muss.commands.world import Look try: Look().execute(self, {"obj": destination}) except AttributeError: pass
def location(self, destination): origin = self.location if not destination.locks.insert(): raise locks.LockFailedError("You can't put that in {}." .format(destination.name)) if origin and not origin.locks.remove(): raise locks.LockFailedError("You can't remove that from {}." .format(origin.name)) player = locks.authority() if destination == player: if not self.locks.take(): raise locks.LockFailedError("You cannot take {}." .format(self.name)) elif origin == player: if not self.locks.drop(): raise locks.LockFailedError("You cannot drop {}." .format(self.name)) # Locks passed or non-applicable. Proceed with the move. with locks.authority_of(locks.SYSTEM): self._location = destination with locks.authority_of(self): # this gets to use self authority because it should always happen, # regardless of the reason location is being changed. # it does NOT get to use system authority because it's sometimes # (always, for players) how position gets initialized, which locks # the object out of its own position attribute. if destination is not origin: # whatever we were doing there, we're not doing it any more self.position = None # Trigger a "look" command so we see our new surroundings from muss.commands.world import Look try: Look().execute(self, {"obj": destination}) except AttributeError: pass
def test_get_authority(self): self.assertIs(locks.authority(), None) with locks.authority_of(self.player): self.assertIs(locks.authority(), self.player) self.assertIs(locks.authority(), None)
def __init__(self, player): if locks.authority() is not locks.SYSTEM: raise locks.LockFailedError("PythonMode requires system authority.") self.console = code.InteractiveConsole() player.send(">>>")