Reshuffling the Evennia package into the new template paradigm.
This commit is contained in:
parent
2846e64833
commit
2b3a32e447
371 changed files with 17250 additions and 304 deletions
0
lib/__init__.py
Normal file
0
lib/__init__.py
Normal file
1
lib/commands/__init__.py
Normal file
1
lib/commands/__init__.py
Normal file
|
|
@ -0,0 +1 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
479
lib/commands/cmdhandler.py
Normal file
479
lib/commands/cmdhandler.py
Normal file
|
|
@ -0,0 +1,479 @@
|
|||
"""
|
||||
Command handler
|
||||
|
||||
This module contains the infrastructure for accepting commands on the
|
||||
command line. The process is as follows:
|
||||
|
||||
1) The calling object (caller) inputs a string and triggers the command parsing system.
|
||||
2) The system checks the state of the caller - loggedin or not
|
||||
3) If no command string was supplied, we search the merged cmdset for system command CMD_NOINPUT
|
||||
and branches to execute that. --> Finished
|
||||
4) Cmdsets are gathered from different sources (in order of dropping priority):
|
||||
channels - all available channel names are auto-created into a cmdset, to allow
|
||||
for giving the channel name and have the following immediately
|
||||
sent to the channel. The sending is performed by the CMD_CHANNEL
|
||||
system command.
|
||||
object cmdsets - all objects at caller's location are scanned for non-empty
|
||||
cmdsets. This includes cmdsets on exits.
|
||||
caller - the caller is searched for its own currently active cmdset.
|
||||
player - lastly the cmdsets defined on caller.player are added.
|
||||
5) All the gathered cmdsets (if more than one) are merged into one using the cmdset priority rules.
|
||||
6) If merged cmdset is empty, raise NoCmdSet exception (this should not happen, at least the
|
||||
player should have a default cmdset available at all times). --> Finished
|
||||
7) The raw input string is parsed using the parser defined by settings.COMMAND_PARSER. It
|
||||
uses the available commands from the merged cmdset to know which commands to look for and
|
||||
returns one or many matches.
|
||||
8) If match list is empty, branch to system command CMD_NOMATCH --> Finished
|
||||
9) If match list has more than one element, branch to system command CMD_MULTIMATCH --> Finished
|
||||
10) A single match was found. If this is a channel-command (i.e. the command name is that of a channel),
|
||||
branch to CMD_CHANNEL --> Finished
|
||||
11) At this point we have found a normal command. We assign useful variables to it that
|
||||
will be available to the command coder at run-time.
|
||||
12) We have a unique cmdobject, primed for use. Call all hooks:
|
||||
at_pre_cmd(), cmdobj.parse(), cmdobj.func() and finally at_post_cmd().
|
||||
|
||||
|
||||
"""
|
||||
|
||||
from weakref import WeakValueDictionary
|
||||
from copy import copy
|
||||
from traceback import format_exc
|
||||
from twisted.internet.defer import inlineCallbacks, returnValue
|
||||
from django.conf import settings
|
||||
from src.comms.channelhandler import CHANNELHANDLER
|
||||
from src.utils import logger, utils
|
||||
from src.commands.cmdparser import at_multimatch_cmd
|
||||
from src.utils.utils import string_suggestions, to_unicode
|
||||
|
||||
from django.utils.translation import ugettext as _
|
||||
|
||||
__all__ = ("cmdhandler",)
|
||||
_GA = object.__getattribute__
|
||||
_CMDSET_MERGE_CACHE = WeakValueDictionary()
|
||||
|
||||
# This decides which command parser is to be used.
|
||||
# You have to restart the server for changes to take effect.
|
||||
_COMMAND_PARSER = utils.variable_from_module(*settings.COMMAND_PARSER.rsplit('.', 1))
|
||||
|
||||
# System command names - import these variables rather than trying to
|
||||
# remember the actual string constants. If not defined, Evennia
|
||||
# hard-coded defaults are used instead.
|
||||
|
||||
# command to call if user just presses <return> with no input
|
||||
CMD_NOINPUT = "__noinput_command"
|
||||
# command to call if no command match was found
|
||||
CMD_NOMATCH = "__nomatch_command"
|
||||
# command to call if multiple command matches were found
|
||||
CMD_MULTIMATCH = "__multimatch_command"
|
||||
# command to call if found command is the name of a channel
|
||||
CMD_CHANNEL = "__send_to_channel_command"
|
||||
# command to call as the very first one when the user connects.
|
||||
# (is expected to display the login screen)
|
||||
CMD_LOGINSTART = "__unloggedin_look_command"
|
||||
|
||||
# custom Exceptions
|
||||
|
||||
|
||||
class NoCmdSets(Exception):
|
||||
"No cmdsets found. Critical error."
|
||||
pass
|
||||
|
||||
|
||||
class ExecSystemCommand(Exception):
|
||||
"Run a system command"
|
||||
def __init__(self, syscmd, sysarg):
|
||||
self.args = (syscmd, sysarg) # needed by exception error handling
|
||||
self.syscmd = syscmd
|
||||
self.sysarg = sysarg
|
||||
|
||||
# Helper function
|
||||
|
||||
|
||||
@inlineCallbacks
|
||||
def get_and_merge_cmdsets(caller, session, player, obj,
|
||||
callertype, sessid=None):
|
||||
"""
|
||||
Gather all relevant cmdsets and merge them.
|
||||
|
||||
callertype is one of "session", "player" or "object" dependin
|
||||
on which level the cmdhandler is invoked. Session includes the
|
||||
cmdsets available to Session, Player and its eventual puppeted Object.
|
||||
Player-level include cmdsets on Player and Object, while calling
|
||||
the handler on an Object only includes cmdsets on itself.
|
||||
|
||||
The cdmsets are merged in order generality, so that the Object's
|
||||
cmdset is merged last (and will thus take precedence over
|
||||
same-named and same-prio commands on Player and Session).
|
||||
|
||||
Note that this function returns a deferred!
|
||||
"""
|
||||
local_obj_cmdsets = [None]
|
||||
|
||||
@inlineCallbacks
|
||||
def _get_channel_cmdsets(player, player_cmdset):
|
||||
"Channel-cmdsets"
|
||||
# Create cmdset for all player's available channels
|
||||
channel_cmdset = None
|
||||
if not player_cmdset.no_channels:
|
||||
channel_cmdset = yield CHANNELHANDLER.get_cmdset(player)
|
||||
returnValue(channel_cmdset)
|
||||
|
||||
@inlineCallbacks
|
||||
def _get_local_obj_cmdsets(obj, obj_cmdset):
|
||||
"Object-level cmdsets"
|
||||
# Gather cmdsets from location, objects in location or carried
|
||||
local_obj_cmdsets = [None]
|
||||
try:
|
||||
location = obj.location
|
||||
except Exception:
|
||||
location = None
|
||||
if location and not obj_cmdset.no_objs:
|
||||
# Gather all cmdsets stored on objects in the room and
|
||||
# also in the caller's inventory and the location itself
|
||||
local_objlist = yield (location.contents_get(exclude=obj) +
|
||||
obj.contents +
|
||||
[location])
|
||||
for lobj in local_objlist:
|
||||
try:
|
||||
# call hook in case we need to do dynamic changing to cmdset
|
||||
_GA(lobj, "at_cmdset_get")()
|
||||
except Exception:
|
||||
logger.log_trace()
|
||||
# the call-type lock is checked here, it makes sure a player
|
||||
# is not seeing e.g. the commands on a fellow player (which is why
|
||||
# the no_superuser_bypass must be True)
|
||||
local_obj_cmdsets = \
|
||||
yield [lobj.cmdset.current for lobj in local_objlist
|
||||
if (lobj.cmdset.current and
|
||||
lobj.locks.check(caller, 'call', no_superuser_bypass=True))]
|
||||
for cset in local_obj_cmdsets:
|
||||
#This is necessary for object sets, or we won't be able to
|
||||
# separate the command sets from each other in a busy room.
|
||||
cset.old_duplicates = cset.duplicates
|
||||
cset.duplicates = True
|
||||
returnValue(local_obj_cmdsets)
|
||||
|
||||
@inlineCallbacks
|
||||
def _get_cmdset(obj):
|
||||
"Get cmdset, triggering all hooks"
|
||||
try:
|
||||
yield obj.at_cmdset_get()
|
||||
except Exception:
|
||||
logger.log_trace()
|
||||
try:
|
||||
returnValue(obj.cmdset.current)
|
||||
except AttributeError:
|
||||
returnValue(None)
|
||||
|
||||
if callertype == "session":
|
||||
# we are calling the command from the session level
|
||||
report_to = session
|
||||
session_cmdset = yield _get_cmdset(session)
|
||||
cmdsets = [session_cmdset]
|
||||
if player: # this automatically implies logged-in
|
||||
player_cmdset = yield _get_cmdset(player)
|
||||
channel_cmdset = yield _get_channel_cmdsets(player, player_cmdset)
|
||||
cmdsets.extend([player_cmdset, channel_cmdset])
|
||||
if obj:
|
||||
obj_cmdset = yield _get_cmdset(obj)
|
||||
local_obj_cmdsets = yield _get_local_obj_cmdsets(obj, obj_cmdset)
|
||||
cmdsets.extend([obj_cmdset] + local_obj_cmdsets)
|
||||
elif callertype == "player":
|
||||
# we are calling the command from the player level
|
||||
report_to = player
|
||||
player_cmdset = yield _get_cmdset(player)
|
||||
channel_cmdset = yield _get_channel_cmdsets(player, player_cmdset)
|
||||
cmdsets = [player_cmdset, channel_cmdset]
|
||||
if obj:
|
||||
obj_cmdset = yield _get_cmdset(obj)
|
||||
local_obj_cmdsets = yield _get_local_obj_cmdsets(obj, obj_cmdset)
|
||||
cmdsets.extend([obj_cmdset] + local_obj_cmdsets)
|
||||
elif callertype == "object":
|
||||
# we are calling the command from the object level
|
||||
report_to = obj
|
||||
obj_cmdset = yield _get_cmdset(obj)
|
||||
local_obj_cmdsets = yield _get_local_obj_cmdsets(obj, obj_cmdset)
|
||||
cmdsets = [obj_cmdset] + local_obj_cmdsets
|
||||
else:
|
||||
raise Exception("get_and_merge_cmdsets: callertype %s is not valid." % callertype)
|
||||
#cmdsets = yield [caller_cmdset] + [player_cmdset] +
|
||||
# [channel_cmdset] + local_obj_cmdsets
|
||||
|
||||
# weed out all non-found sets
|
||||
cmdsets = yield [cmdset for cmdset in cmdsets
|
||||
if cmdset and cmdset.key != "_EMPTY_CMDSET"]
|
||||
# report cmdset errors to user (these should already have been logged)
|
||||
yield [report_to.msg(cmdset.errmessage) for cmdset in cmdsets
|
||||
if cmdset.key == "_CMDSET_ERROR"]
|
||||
|
||||
if cmdsets:
|
||||
# faster to do tuple on list than to build tuple directly
|
||||
mergehash = tuple([id(cmdset) for cmdset in cmdsets])
|
||||
if mergehash in _CMDSET_MERGE_CACHE:
|
||||
# cached merge exist; use that
|
||||
cmdset = _CMDSET_MERGE_CACHE[mergehash]
|
||||
else:
|
||||
# we group and merge all same-prio cmdsets separately (this avoids
|
||||
# order-dependent clashes in certain cases, such as
|
||||
# when duplicates=True)
|
||||
tempmergers = {}
|
||||
for cmdset in cmdsets:
|
||||
prio = cmdset.priority
|
||||
#print cmdset.key, prio
|
||||
if prio in tempmergers:
|
||||
# merge same-prio cmdset together separately
|
||||
tempmergers[prio] = yield cmdset + tempmergers[prio]
|
||||
else:
|
||||
tempmergers[prio] = cmdset
|
||||
|
||||
# sort cmdsets after reverse priority (highest prio are merged in last)
|
||||
cmdsets = yield sorted(tempmergers.values(), key=lambda x: x.priority)
|
||||
|
||||
# Merge all command sets into one, beginning with the lowest-prio one
|
||||
cmdset = cmdsets[0]
|
||||
for merging_cmdset in cmdsets[1:]:
|
||||
#print "<%s(%s,%s)> onto <%s(%s,%s)>" % (merging_cmdset.key, merging_cmdset.priority, merging_cmdset.mergetype,
|
||||
# cmdset.key, cmdset.priority, cmdset.mergetype)
|
||||
cmdset = yield merging_cmdset + cmdset
|
||||
# store the full sets for diagnosis
|
||||
cmdset.merged_from = cmdsets
|
||||
# cache
|
||||
_CMDSET_MERGE_CACHE[mergehash] = cmdset
|
||||
else:
|
||||
cmdset = None
|
||||
|
||||
for cset in (cset for cset in local_obj_cmdsets if cset):
|
||||
cset.duplicates = cset.old_duplicates
|
||||
#print "merged set:", cmdset.key
|
||||
returnValue(cmdset)
|
||||
|
||||
|
||||
# Main command-handler function
|
||||
|
||||
@inlineCallbacks
|
||||
def cmdhandler(called_by, raw_string, _testing=False, callertype="session", sessid=None, **kwargs):
|
||||
"""
|
||||
This is the main function to handle any string sent to the engine.
|
||||
|
||||
called_by - object on which this was called from. This is either a Session, a Player or an Object.
|
||||
raw_string - the command string given on the command line
|
||||
_testing - if we should actually execute the command or not.
|
||||
if True, the command instance will be returned instead.
|
||||
callertype - this is one of "session", "player" or "object", in decending
|
||||
order. So when the Session is the caller, it will merge its
|
||||
own cmdset into cmdsets from both Player and eventual puppeted
|
||||
Object (and cmdsets in its room etc). A Player will only
|
||||
include its own cmdset and the Objects and so on. Merge order
|
||||
is the same order, so that Object cmdsets are merged in last,
|
||||
giving them precendence for same-name and same-prio commands.
|
||||
sessid - Relevant if callertype is "player" - the session id will help
|
||||
retrieve the correct cmdsets from puppeted objects.
|
||||
**kwargs - other keyword arguments will be assigned as named variables on the
|
||||
retrieved command object *before* it is executed. This is unuesed
|
||||
in default Evennia but may be used by code to set custom flags or
|
||||
special operating conditions for a command as it executes.
|
||||
|
||||
Note that this function returns a deferred!
|
||||
"""
|
||||
|
||||
raw_string = to_unicode(raw_string, force_string=True)
|
||||
|
||||
session, player, obj = None, None, None
|
||||
if callertype == "session":
|
||||
session = called_by
|
||||
player = session.player
|
||||
if player:
|
||||
obj = yield player.get_puppet(session.sessid)
|
||||
elif callertype == "player":
|
||||
player = called_by
|
||||
if sessid:
|
||||
obj = yield player.get_puppet(sessid)
|
||||
elif callertype == "object":
|
||||
obj = called_by
|
||||
else:
|
||||
raise RuntimeError("cmdhandler: callertype %s is not valid." % callertype)
|
||||
|
||||
# the caller will be the one to receive messages and excert its permissions.
|
||||
# we assign the caller with preference 'bottom up'
|
||||
caller = obj or player or session
|
||||
|
||||
try: # catch bugs in cmdhandler itself
|
||||
try: # catch special-type commands
|
||||
|
||||
cmdset = yield get_and_merge_cmdsets(caller, session, player, obj,
|
||||
callertype, sessid)
|
||||
if not cmdset:
|
||||
# this is bad and shouldn't happen.
|
||||
raise NoCmdSets
|
||||
unformatted_raw_string = raw_string
|
||||
raw_string = raw_string.strip()
|
||||
if not raw_string:
|
||||
# Empty input. Test for system command instead.
|
||||
syscmd = yield cmdset.get(CMD_NOINPUT)
|
||||
sysarg = ""
|
||||
raise ExecSystemCommand(syscmd, sysarg)
|
||||
# Parse the input string and match to available cmdset.
|
||||
# This also checks for permissions, so all commands in match
|
||||
# are commands the caller is allowed to call.
|
||||
matches = yield _COMMAND_PARSER(raw_string, cmdset, caller)
|
||||
|
||||
# Deal with matches
|
||||
|
||||
if len(matches) > 1:
|
||||
# We have a multiple-match
|
||||
syscmd = yield cmdset.get(CMD_MULTIMATCH)
|
||||
sysarg = _("There were multiple matches.")
|
||||
if syscmd:
|
||||
# use custom CMD_MULTIMATCH
|
||||
syscmd.matches = matches
|
||||
else:
|
||||
# fall back to default error handling
|
||||
sysarg = yield at_multimatch_cmd(caller, matches)
|
||||
raise ExecSystemCommand(syscmd, sysarg)
|
||||
|
||||
if len(matches) == 1:
|
||||
# We have a unique command match. But it may still be invalid.
|
||||
match = matches[0]
|
||||
cmdname, args, cmd = match[0], match[1], match[2]
|
||||
|
||||
# check if we allow this type of command
|
||||
if cmdset.no_channels and hasattr(cmd, "is_channel") and cmd.is_channel:
|
||||
matches = []
|
||||
if cmdset.no_exits and hasattr(cmd, "is_exit") and cmd.is_exit:
|
||||
matches = []
|
||||
|
||||
if not matches:
|
||||
# No commands match our entered command
|
||||
syscmd = yield cmdset.get(CMD_NOMATCH)
|
||||
if syscmd:
|
||||
# use custom CMD_NOMATH command
|
||||
sysarg = raw_string
|
||||
else:
|
||||
# fallback to default error text
|
||||
sysarg = _("Command '%s' is not available.") % raw_string
|
||||
suggestions = string_suggestions(raw_string,
|
||||
cmdset.get_all_cmd_keys_and_aliases(caller),
|
||||
cutoff=0.7, maxnum=3)
|
||||
if suggestions:
|
||||
sysarg += _(" Maybe you meant %s?") % utils.list_to_string(suggestions, _('or'), addquote=True)
|
||||
else:
|
||||
sysarg += _(" Type \"help\" for help.")
|
||||
raise ExecSystemCommand(syscmd, sysarg)
|
||||
|
||||
# Check if this is a Channel-cmd match.
|
||||
if hasattr(cmd, 'is_channel') and cmd.is_channel:
|
||||
# even if a user-defined syscmd is not defined, the
|
||||
# found cmd is already a system command in its own right.
|
||||
syscmd = yield cmdset.get(CMD_CHANNEL)
|
||||
if syscmd:
|
||||
# replace system command with custom version
|
||||
cmd = syscmd
|
||||
cmd.sessid = session.sessid if session else None
|
||||
sysarg = "%s:%s" % (cmdname, args)
|
||||
raise ExecSystemCommand(cmd, sysarg)
|
||||
|
||||
# A normal command.
|
||||
|
||||
# Assign useful variables to the instance
|
||||
cmd.caller = caller
|
||||
cmd.cmdstring = cmdname
|
||||
cmd.args = args
|
||||
cmd.cmdset = cmdset
|
||||
cmd.sessid = session.sessid if session else sessid
|
||||
cmd.session = session
|
||||
cmd.player = player
|
||||
cmd.raw_string = unformatted_raw_string
|
||||
#cmd.obj # set via on-object cmdset handler for each command,
|
||||
# since this may be different for every command when
|
||||
# merging multuple cmdsets
|
||||
|
||||
if hasattr(cmd, 'obj') and hasattr(cmd.obj, 'scripts'):
|
||||
# cmd.obj is automatically made available by the cmdhandler.
|
||||
# we make sure to validate its scripts.
|
||||
yield cmd.obj.scripts.validate()
|
||||
|
||||
if _testing:
|
||||
# only return the command instance
|
||||
returnValue(cmd)
|
||||
|
||||
# assign custom kwargs to found cmd object
|
||||
for key, val in kwargs.items():
|
||||
setattr(cmd, key, val)
|
||||
|
||||
# pre-command hook
|
||||
abort = yield cmd.at_pre_cmd()
|
||||
if abort:
|
||||
# abort sequence
|
||||
returnValue(abort)
|
||||
|
||||
# Parse and execute
|
||||
yield cmd.parse()
|
||||
# (return value is normally None)
|
||||
ret = yield cmd.func()
|
||||
|
||||
# post-command hook
|
||||
yield cmd.at_post_cmd()
|
||||
|
||||
if cmd.save_for_next:
|
||||
# store a reference to this command, possibly
|
||||
# accessible by the next command.
|
||||
caller.ndb.last_cmd = yield copy(cmd)
|
||||
else:
|
||||
caller.ndb.last_cmd = None
|
||||
|
||||
# Done! This returns a deferred. By default, Evennia does
|
||||
# not use this at all.
|
||||
returnValue(ret)
|
||||
|
||||
except ExecSystemCommand, exc:
|
||||
# Not a normal command: run a system command, if available,
|
||||
# or fall back to a return string.
|
||||
syscmd = exc.syscmd
|
||||
sysarg = exc.sysarg
|
||||
if syscmd:
|
||||
syscmd.caller = caller
|
||||
syscmd.cmdstring = syscmd.key
|
||||
syscmd.args = sysarg
|
||||
syscmd.cmdset = cmdset
|
||||
syscmd.sessid = session.sessid if session else None
|
||||
syscmd.raw_string = unformatted_raw_string
|
||||
|
||||
if hasattr(syscmd, 'obj') and hasattr(syscmd.obj, 'scripts'):
|
||||
# cmd.obj is automatically made available.
|
||||
# we make sure to validate its scripts.
|
||||
yield syscmd.obj.scripts.validate()
|
||||
|
||||
if _testing:
|
||||
# only return the command instance
|
||||
returnValue(syscmd)
|
||||
|
||||
# parse and run the command
|
||||
yield syscmd.parse()
|
||||
yield syscmd.func()
|
||||
elif sysarg:
|
||||
# return system arg
|
||||
caller.msg(exc.sysarg)
|
||||
|
||||
except NoCmdSets:
|
||||
# Critical error.
|
||||
string = "No command sets found! This is a sign of a critical bug.\n"
|
||||
string += "The error was logged.\n"
|
||||
string += "If logging out/in doesn't solve the problem, try to "
|
||||
string += "contact the server admin through some other means "
|
||||
string += "for assistance."
|
||||
caller.msg(_(string))
|
||||
logger.log_errmsg("No cmdsets found: %s" % caller)
|
||||
|
||||
except Exception:
|
||||
# We should not end up here. If we do, it's a programming bug.
|
||||
string = "%s\nAbove traceback is from an untrapped error."
|
||||
string += " Please file a bug report."
|
||||
logger.log_trace(_(string))
|
||||
caller.msg(string % format_exc())
|
||||
|
||||
except Exception:
|
||||
# This catches exceptions in cmdhandler exceptions themselves
|
||||
string = "%s\nAbove traceback is from a Command handler bug."
|
||||
string += " Please contact an admin and/or file a bug report."
|
||||
logger.log_trace(_(string))
|
||||
caller.msg(string % format_exc())
|
||||
281
lib/commands/cmdparser.py
Normal file
281
lib/commands/cmdparser.py
Normal file
|
|
@ -0,0 +1,281 @@
|
|||
"""
|
||||
The default command parser. Use your own by assigning
|
||||
settings.ALTERNATE_PARSER to a Python path to a module containing the
|
||||
replacing cmdparser function. The replacement parser must matches
|
||||
on the sme form as the default cmdparser.
|
||||
"""
|
||||
|
||||
from src.utils.logger import log_trace
|
||||
from django.utils.translation import ugettext as _
|
||||
|
||||
def cmdparser(raw_string, cmdset, caller, match_index=None):
|
||||
"""
|
||||
This function is called by the cmdhandler once it has
|
||||
gathered and merged all valid cmdsets valid for this particular parsing.
|
||||
|
||||
raw_string - the unparsed text entered by the caller.
|
||||
cmdset - the merged, currently valid cmdset
|
||||
caller - the caller triggering this parsing
|
||||
match_index - an optional integer index to pick a given match in a
|
||||
list of same-named command matches.
|
||||
|
||||
The cmdparser understand the following command combinations (where
|
||||
[] marks optional parts.
|
||||
|
||||
[cmdname[ cmdname2 cmdname3 ...] [the rest]
|
||||
|
||||
A command may consist of any number of space-separated words of any
|
||||
length, and contain any character. It may also be empty.
|
||||
|
||||
The parser makes use of the cmdset to find command candidates. The
|
||||
parser return a list of matches. Each match is a tuple with its
|
||||
first three elements being the parsed cmdname (lower case),
|
||||
the remaining arguments, and the matched cmdobject from the cmdset.
|
||||
"""
|
||||
|
||||
def create_match(cmdname, string, cmdobj):
|
||||
"""
|
||||
Evaluates the quality of a match by counting how many chars of cmdname
|
||||
matches string (counting from beginning of string). We also calculate
|
||||
a ratio from 0-1 describing how much cmdname matches string.
|
||||
We return a tuple (cmdname, count, ratio, args, cmdobj).
|
||||
|
||||
"""
|
||||
cmdlen, strlen = len(cmdname), len(string)
|
||||
mratio = 1 - (strlen - cmdlen) / (1.0 * strlen)
|
||||
args = string[cmdlen:]
|
||||
return (cmdname, args, cmdobj, cmdlen, mratio)
|
||||
|
||||
if not raw_string:
|
||||
return None
|
||||
|
||||
matches = []
|
||||
|
||||
# match everything that begins with a matching cmdname.
|
||||
l_raw_string = raw_string.lower()
|
||||
for cmd in cmdset:
|
||||
try:
|
||||
matches.extend([create_match(cmdname, raw_string, cmd)
|
||||
for cmdname in [cmd.key] + cmd.aliases
|
||||
if cmdname and l_raw_string.startswith(cmdname.lower())
|
||||
and (not cmd.arg_regex or
|
||||
cmd.arg_regex.match(l_raw_string[len(cmdname):]))])
|
||||
except Exception:
|
||||
log_trace("cmdhandler error. raw_input:%s" % raw_string)
|
||||
|
||||
if not matches:
|
||||
# no matches found.
|
||||
if '-' in raw_string:
|
||||
# This could be due to the user trying to identify the
|
||||
# command with a #num-<command> style syntax.
|
||||
mindex, new_raw_string = raw_string.split("-", 1)
|
||||
if mindex.isdigit():
|
||||
mindex = int(mindex) - 1
|
||||
# feed result back to parser iteratively
|
||||
return cmdparser(new_raw_string, cmdset,
|
||||
caller, match_index=mindex)
|
||||
|
||||
# only select command matches we are actually allowed to call.
|
||||
matches = [match for match in matches if match[2].access(caller, 'cmd')]
|
||||
|
||||
if len(matches) > 1:
|
||||
# See if it helps to analyze the match with preserved case but only if
|
||||
# it leaves at least one match.
|
||||
trimmed = [match for match in matches
|
||||
if raw_string.startswith(match[0])]
|
||||
if trimmed:
|
||||
matches = trimmed
|
||||
|
||||
if len(matches) > 1:
|
||||
# we still have multiple matches. Sort them by count quality.
|
||||
matches = sorted(matches, key=lambda m: m[3])
|
||||
# only pick the matches with highest count quality
|
||||
quality = [mat[3] for mat in matches]
|
||||
matches = matches[-quality.count(quality[-1]):]
|
||||
|
||||
if len(matches) > 1:
|
||||
# still multiple matches. Fall back to ratio-based quality.
|
||||
matches = sorted(matches, key=lambda m: m[4])
|
||||
# only pick the highest rated ratio match
|
||||
quality = [mat[4] for mat in matches]
|
||||
matches = matches[-quality.count(quality[-1]):]
|
||||
|
||||
if len(matches) > 1 and match_index != None and 0 <= match_index < len(matches):
|
||||
# We couldn't separate match by quality, but we have an
|
||||
# index argument to tell us which match to use.
|
||||
matches = [matches[match_index]]
|
||||
|
||||
# no matter what we have at this point, we have to return it.
|
||||
return matches
|
||||
|
||||
#------------------------------------------------------------
|
||||
# Search parsers and support methods
|
||||
#------------------------------------------------------------
|
||||
#
|
||||
# Default functions for formatting and processing searches.
|
||||
#
|
||||
# You can replace these from the settings file by setting the variables
|
||||
#
|
||||
# SEARCH_AT_RESULT
|
||||
# SEARCH_AT_MULTIMATCH_INPUT
|
||||
#
|
||||
# The the replacing functions must have the same inputs and outputs as
|
||||
# those in this module.
|
||||
#
|
||||
def at_search_result(msg_obj, ostring, results, global_search=False,
|
||||
nofound_string=None, multimatch_string=None, quiet=False):
|
||||
"""
|
||||
Called by search methods after a result of any type has been found.
|
||||
|
||||
Takes a search result (a list) and
|
||||
formats eventual errors.
|
||||
|
||||
msg_obj - object to receive feedback.
|
||||
ostring - original search string
|
||||
results - list of found matches (0, 1 or more)
|
||||
global_search - if this was a global_search or not
|
||||
(if it is, there might be an idea of supplying
|
||||
dbrefs instead of only numbers)
|
||||
nofound_string - optional custom string for not-found error message.
|
||||
multimatch_string - optional custom string for multimatch error header
|
||||
quiet - work normally, but don't echo to caller, just return the
|
||||
results.
|
||||
|
||||
Multiple matches are returned to the searching object
|
||||
as
|
||||
1-object
|
||||
2-object
|
||||
3-object
|
||||
etc
|
||||
|
||||
"""
|
||||
string = ""
|
||||
if not results:
|
||||
# no results.
|
||||
if nofound_string:
|
||||
# custom return string
|
||||
string = nofound_string
|
||||
else:
|
||||
string = _("Could not find '%s'." % ostring)
|
||||
results = None
|
||||
|
||||
elif len(results) > 1:
|
||||
# we have more than one match. We will display a
|
||||
# list of the form 1-objname, 2-objname etc.
|
||||
|
||||
# check if the msg_object may se dbrefs
|
||||
show_dbref = global_search
|
||||
|
||||
if multimatch_string:
|
||||
# custom header
|
||||
string = multimatch_string
|
||||
else:
|
||||
string = "More than one match for '%s'" % ostring
|
||||
string += " (please narrow target):"
|
||||
string = _(string)
|
||||
|
||||
for num, result in enumerate(results):
|
||||
invtext = ""
|
||||
dbreftext = ""
|
||||
if hasattr(result, _("location")) and result.location == msg_obj:
|
||||
invtext = _(" (carried)")
|
||||
if show_dbref:
|
||||
dbreftext = "(#%i)" % result.dbid
|
||||
string += "\n %i-%s%s%s" % (num + 1, result.name,
|
||||
dbreftext, invtext)
|
||||
results = None
|
||||
else:
|
||||
# we have exactly one match.
|
||||
results = results[0]
|
||||
|
||||
if string and not quiet:
|
||||
msg_obj.msg(string.strip())
|
||||
return results
|
||||
|
||||
|
||||
def at_multimatch_input(ostring):
|
||||
"""
|
||||
Parse number-identifiers.
|
||||
|
||||
This parser will be called by the engine when a user supplies
|
||||
a search term. The search term must be analyzed to determine
|
||||
if the user wants to differentiate between multiple matches
|
||||
(usually found during a previous search).
|
||||
|
||||
This method should separate out any identifiers from the search
|
||||
string used to differentiate between same-named objects. The
|
||||
result should be a tuple (index, search_string) where the index
|
||||
gives which match among multiple matches should be used (1 being
|
||||
the lowest number, rather than 0 as in Python).
|
||||
|
||||
This parser version will identify search strings on the following
|
||||
forms
|
||||
|
||||
2-object
|
||||
|
||||
This will be parsed to (2, "object") and, if applicable, will tell
|
||||
the engine to pick the second from a list of same-named matches of
|
||||
objects called "object".
|
||||
|
||||
Ex for use in a game session:
|
||||
|
||||
> look
|
||||
You see: ball, ball, ball and ball.
|
||||
> get ball
|
||||
There where multiple matches for ball:
|
||||
1-ball
|
||||
2-ball
|
||||
3-ball
|
||||
4-ball
|
||||
> get 3-ball
|
||||
You get the ball.
|
||||
|
||||
"""
|
||||
|
||||
if not isinstance(ostring, basestring):
|
||||
return (None, ostring)
|
||||
if not '-' in ostring:
|
||||
return (None, ostring)
|
||||
try:
|
||||
index = ostring.find('-')
|
||||
number = int(ostring[:index]) - 1
|
||||
return (number, ostring[index + 1:])
|
||||
except ValueError:
|
||||
#not a number; this is not an identifier.
|
||||
return (None, ostring)
|
||||
except IndexError:
|
||||
return (None, ostring)
|
||||
|
||||
|
||||
def at_multimatch_cmd(caller, matches):
|
||||
"""
|
||||
Format multiple command matches to a useful error.
|
||||
"""
|
||||
string = "There were multiple matches:"
|
||||
for num, match in enumerate(matches):
|
||||
# each match is a tuple (candidate, cmd)
|
||||
cmdname, arg, cmd, dum, dum = match
|
||||
|
||||
is_channel = hasattr(cmd, "is_channel") and cmd.is_channel
|
||||
if is_channel:
|
||||
is_channel = _(" (channel)")
|
||||
else:
|
||||
is_channel = ""
|
||||
if cmd.is_exit and cmd.destination:
|
||||
is_exit = (" (exit to %s)") % cmd.destination
|
||||
else:
|
||||
is_exit = ""
|
||||
|
||||
id1 = ""
|
||||
id2 = ""
|
||||
if (not (is_channel or is_exit) and
|
||||
(hasattr(cmd, 'obj') and cmd.obj != caller) and
|
||||
hasattr(cmd.obj, "key")):
|
||||
# the command is defined on some other object
|
||||
id1 = "%s-%s" % (num + 1, cmdname)
|
||||
id2 = " (%s)" % (cmd.obj.key)
|
||||
else:
|
||||
id1 = "%s-%s" % (num + 1, cmdname)
|
||||
id2 = ""
|
||||
string += "\n %s%s%s%s" % (id1, id2, is_channel, is_exit)
|
||||
return string
|
||||
462
lib/commands/cmdset.py
Normal file
462
lib/commands/cmdset.py
Normal file
|
|
@ -0,0 +1,462 @@
|
|||
"""
|
||||
A cmdset holds a set of commands available to the object or to other
|
||||
objects near it. All the commands a player can give (look, @create etc)
|
||||
are stored as the default cmdset on the player object and managed using the
|
||||
CmdHandler object (see cmdhandler.py).
|
||||
|
||||
The power of having command sets in CmdSets like this is that CmdSets
|
||||
can be merged together according to individual rules to create a new
|
||||
on-the-fly CmdSet that is some combination of the
|
||||
previous ones. Their function are borrowed to a large parts from mathematical
|
||||
Set theory, it should not be much of a problem to understand.
|
||||
|
||||
See CmdHandler for practical examples on how to apply cmdsets
|
||||
together to create interesting in-game effects.
|
||||
"""
|
||||
|
||||
from weakref import WeakKeyDictionary
|
||||
from django.utils.translation import ugettext as _
|
||||
from src.utils.utils import inherits_from, is_iter
|
||||
__all__ = ("CmdSet",)
|
||||
|
||||
|
||||
class _CmdSetMeta(type):
|
||||
"""
|
||||
This metaclass makes some minor on-the-fly convenience fixes to
|
||||
the cmdset class.
|
||||
"""
|
||||
def __init__(mcs, *args, **kwargs):
|
||||
"""
|
||||
Fixes some things in the cmdclass
|
||||
"""
|
||||
# by default we key the cmdset the same as the
|
||||
# name of its class.
|
||||
if not hasattr(mcs, 'key') or not mcs.key:
|
||||
mcs.key = mcs.__name__
|
||||
mcs.path = "%s.%s" % (mcs.__module__, mcs.__name__)
|
||||
|
||||
if not type(mcs.key_mergetypes) == dict:
|
||||
mcs.key_mergetypes = {}
|
||||
|
||||
super(_CmdSetMeta, mcs).__init__(*args, **kwargs)
|
||||
|
||||
|
||||
class CmdSet(object):
|
||||
"""
|
||||
This class describes a unique cmdset that understands priorities. CmdSets
|
||||
can be merged and made to perform various set operations on each other.
|
||||
CmdSets have priorities that affect which of their ingoing commands
|
||||
gets used.
|
||||
|
||||
In the examples, cmdset A always have higher priority than cmdset B.
|
||||
|
||||
key - the name of the cmdset. This can be used on its own for game
|
||||
operations
|
||||
|
||||
mergetype (partly from Set theory):
|
||||
|
||||
Union - The two command sets are merged so that as many
|
||||
commands as possible of each cmdset ends up in the
|
||||
merged cmdset. Same-name commands are merged by
|
||||
priority. This is the most common default.
|
||||
Ex: A1,A3 + B1,B2,B4,B5 = A1,B2,A3,B4,B5
|
||||
Intersect - Only commands found in *both* cmdsets
|
||||
(i.e. which have same names) end up in the merged
|
||||
cmdset, with the higher-priority cmdset replacing the
|
||||
lower one. Ex: A1,A3 + B1,B2,B4,B5 = A1
|
||||
Replace - The commands of this cmdset completely replaces
|
||||
the lower-priority cmdset's commands, regardless
|
||||
of if same-name commands exist.
|
||||
Ex: A1,A3 + B1,B2,B4,B5 = A1,A3
|
||||
Remove - This removes the relevant commands from the
|
||||
lower-priority cmdset completely. They are not
|
||||
replaced with anything, so this in effects uses the
|
||||
high-priority cmdset as a filter to affect the
|
||||
low-priority cmdset.
|
||||
Ex: A1,A3 + B1,B2,B4,B5 = B2,B4,B5
|
||||
|
||||
Note: Commands longer than 2 characters and starting
|
||||
with double underscrores, like '__noinput_command'
|
||||
are considered 'system commands' and are
|
||||
excempt from all merge operations - they are
|
||||
ALWAYS included across mergers and only affected
|
||||
if same-named system commands replace them.
|
||||
|
||||
priority- All cmdsets are always merged in pairs of two so that
|
||||
the higher set's mergetype is applied to the
|
||||
lower-priority cmdset. Default commands have priority 0,
|
||||
high-priority ones like Exits and Channels have 10 and 9.
|
||||
Priorities can be negative as well to give default
|
||||
commands preference.
|
||||
|
||||
duplicates - determines what happens when two sets of equal
|
||||
priority merge. Default has the first of them in the
|
||||
merger (i.e. A above) automatically taking
|
||||
precedence. But if allow_duplicates is true, the
|
||||
result will be a merger with more than one of each
|
||||
name match. This will usually lead to the player
|
||||
receiving a multiple-match error higher up the road,
|
||||
but can be good for things like cmdsets on non-player
|
||||
objects in a room, to allow the system to warn that
|
||||
more than one 'ball' in the room has the same 'kick'
|
||||
command defined on it, so it may offer a chance to
|
||||
select which ball to kick ... Allowing duplicates
|
||||
only makes sense for Union and Intersect, the setting
|
||||
is ignored for the other mergetypes.
|
||||
|
||||
key_mergetype (dict) - allows the cmdset to define a unique
|
||||
mergetype for particular cmdsets. Format is
|
||||
{CmdSetkeystring:mergetype}. Priorities still apply.
|
||||
Example: {'Myevilcmdset','Replace'} which would make
|
||||
sure for this set to always use 'Replace' on
|
||||
Myevilcmdset no matter what overall mergetype this set
|
||||
has.
|
||||
|
||||
no_objs - don't include any commands from nearby objects
|
||||
when searching for suitable commands
|
||||
no_exits - ignore the names of exits when matching against
|
||||
commands
|
||||
no_channels - ignore the name of channels when matching against
|
||||
commands (WARNING- this is dangerous since the
|
||||
player can then not even ask staff for help if
|
||||
something goes wrong)
|
||||
|
||||
|
||||
"""
|
||||
__metaclass__ = _CmdSetMeta
|
||||
|
||||
key = "Unnamed CmdSet"
|
||||
mergetype = "Union"
|
||||
priority = 0
|
||||
duplicates = False
|
||||
key_mergetypes = {}
|
||||
no_exits = False
|
||||
no_objs = False
|
||||
no_channels = False
|
||||
permanent = False
|
||||
errmessage = ""
|
||||
# pre-store properties to duplicate straight off
|
||||
to_duplicate = ("key", "cmdsetobj", "no_exits", "no_objs",
|
||||
"no_channels", "permanent", "mergetype",
|
||||
"priority", "duplicates", "errmessage")
|
||||
|
||||
def __init__(self, cmdsetobj=None, key=None):
|
||||
"""
|
||||
Creates a new CmdSet instance.
|
||||
|
||||
cmdsetobj - this is the database object to which this particular
|
||||
instance of cmdset is related. It is often a character but
|
||||
may also be a regular object.
|
||||
"""
|
||||
if key:
|
||||
self.key = key
|
||||
self.commands = []
|
||||
self.system_commands = []
|
||||
self.actual_mergetype = self.mergetype
|
||||
self.cmdsetobj = cmdsetobj
|
||||
# this is set only on merged sets, in cmdhandler.py, in order to
|
||||
# track, list and debug mergers correctly.
|
||||
self.merged_from = []
|
||||
|
||||
# initialize system
|
||||
self.at_cmdset_creation()
|
||||
self._contains_cache = WeakKeyDictionary()#{}
|
||||
|
||||
# Priority-sensitive merge operations for cmdsets
|
||||
|
||||
def _union(self, cmdset_a, cmdset_b):
|
||||
"C = A U B. CmdSet A is assumed to have higher priority"
|
||||
cmdset_c = cmdset_a._duplicate()
|
||||
# we make copies, not refs by use of [:]
|
||||
cmdset_c.commands = cmdset_a.commands[:]
|
||||
if cmdset_a.duplicates and cmdset_a.priority == cmdset_b.priority:
|
||||
cmdset_c.commands.extend(cmdset_b.commands)
|
||||
else:
|
||||
cmdset_c.commands.extend([cmd for cmd in cmdset_b
|
||||
if not cmd in cmdset_a])
|
||||
return cmdset_c
|
||||
|
||||
def _intersect(self, cmdset_a, cmdset_b):
|
||||
"C = A (intersect) B. A is assumed higher priority"
|
||||
cmdset_c = cmdset_a._duplicate()
|
||||
if cmdset_a.duplicates and cmdset_a.priority == cmdset_b.priority:
|
||||
for cmd in [cmd for cmd in cmdset_a if cmd in cmdset_b]:
|
||||
cmdset_c.add(cmd)
|
||||
cmdset_c.add(cmdset_b.get(cmd))
|
||||
else:
|
||||
cmdset_c.commands = [cmd for cmd in cmdset_a if cmd in cmdset_b]
|
||||
return cmdset_c
|
||||
|
||||
def _replace(self, cmdset_a, cmdset_b):
|
||||
"C = A + B where the result is A."
|
||||
cmdset_c = cmdset_a._duplicate()
|
||||
cmdset_c.commands = cmdset_a.commands[:]
|
||||
return cmdset_c
|
||||
|
||||
def _remove(self, cmdset_a, cmdset_b):
|
||||
"C = A + B, where B is filtered by A"
|
||||
cmdset_c = cmdset_a._duplicate()
|
||||
cmdset_c.commands = [cmd for cmd in cmdset_b if not cmd in cmdset_a]
|
||||
return cmdset_c
|
||||
|
||||
def _instantiate(self, cmd):
|
||||
"""
|
||||
checks so that object is an instantiated command
|
||||
and not, say a cmdclass. If it is, instantiate it.
|
||||
Other types, like strings, are passed through.
|
||||
"""
|
||||
try:
|
||||
return cmd()
|
||||
except TypeError:
|
||||
return cmd
|
||||
|
||||
def _duplicate(self):
|
||||
"""
|
||||
Returns a new cmdset with the same settings as this one
|
||||
(no actual commands are copied over)
|
||||
"""
|
||||
cmdset = CmdSet()
|
||||
for key, val in ((key, getattr(self, key)) for key in self.to_duplicate):
|
||||
if val != getattr(cmdset, key):
|
||||
# only copy if different from default; avoid turning
|
||||
# class-vars into instance vars
|
||||
setattr(cmdset, key, val)
|
||||
cmdset.key_mergetypes = self.key_mergetypes.copy()
|
||||
return cmdset
|
||||
#cmdset = self.__class__()
|
||||
#cmdset.__dict__.update(dict((key, val) for key, val in self.__dict__.items() if key in self.to_duplicate))
|
||||
#cmdset.key_mergetypes = self.key_mergetypes.copy() #copy.deepcopy(self.key_mergetypes)
|
||||
#return cmdset
|
||||
|
||||
def __str__(self):
|
||||
"""
|
||||
Show all commands in cmdset when printing it.
|
||||
"""
|
||||
return ", ".join([str(cmd) for cmd in sorted(self.commands, key=lambda o:o.key)])
|
||||
|
||||
def __iter__(self):
|
||||
"""
|
||||
Allows for things like 'for cmd in cmdset':
|
||||
"""
|
||||
return iter(self.commands)
|
||||
|
||||
def __contains__(self, othercmd):
|
||||
"""
|
||||
Returns True if this cmdset contains the given command (as defined
|
||||
by command name and aliases). This allows for things
|
||||
like 'if cmd in cmdset'
|
||||
"""
|
||||
ret = self._contains_cache.get(othercmd)
|
||||
if ret is None:
|
||||
ret = othercmd in self.commands
|
||||
self._contains_cache[othercmd] = ret
|
||||
return ret
|
||||
|
||||
def __add__(self, cmdset_b):
|
||||
"""
|
||||
Merge this cmdset (A) with another cmdset (B) using the + operator,
|
||||
|
||||
C = A + B
|
||||
|
||||
Here, we (by convention) say that 'A is merged onto B to form
|
||||
C'. The actual merge operation used in the 'addition' depends
|
||||
on which priorities A and B have. The one of the two with the
|
||||
highest priority will apply and give its properties to C. In
|
||||
the case of a tie, A takes priority and replaces the
|
||||
same-named commands in B unless A has the 'duplicate' variable
|
||||
set (which means both sets' commands are kept).
|
||||
"""
|
||||
|
||||
# It's okay to merge with None
|
||||
if not cmdset_b:
|
||||
return self
|
||||
|
||||
sys_commands_a = self.get_system_cmds()
|
||||
sys_commands_b = cmdset_b.get_system_cmds()
|
||||
|
||||
if self.priority >= cmdset_b.priority:
|
||||
# A higher or equal priority than B
|
||||
|
||||
# preserve system __commands
|
||||
sys_commands = sys_commands_a + [cmd for cmd in sys_commands_b
|
||||
if cmd not in sys_commands_a]
|
||||
|
||||
mergetype = self.key_mergetypes.get(cmdset_b.key, self.mergetype)
|
||||
if mergetype == "Intersect":
|
||||
cmdset_c = self._intersect(self, cmdset_b)
|
||||
elif mergetype == "Replace":
|
||||
cmdset_c = self._replace(self, cmdset_b)
|
||||
elif mergetype == "Remove":
|
||||
cmdset_c = self._remove(self, cmdset_b)
|
||||
else: # Union
|
||||
cmdset_c = self._union(self, cmdset_b)
|
||||
cmdset_c.no_channels = self.no_channels
|
||||
cmdset_c.no_exits = self.no_exits
|
||||
cmdset_c.no_objs = self.no_objs
|
||||
if self.key.startswith("_"):
|
||||
# don't rename new output if the merge set's name starts with _
|
||||
cmdset_c.key = cmdset_b.key
|
||||
|
||||
else:
|
||||
# B higher priority than A
|
||||
|
||||
# preserver system __commands
|
||||
sys_commands = sys_commands_b + [cmd for cmd in sys_commands_a
|
||||
if cmd not in sys_commands_b]
|
||||
|
||||
mergetype = cmdset_b.key_mergetypes.get(self.key, cmdset_b.mergetype)
|
||||
if mergetype == "Intersect":
|
||||
cmdset_c = self._intersect(cmdset_b, self)
|
||||
elif mergetype == "Replace":
|
||||
cmdset_c = self._replace(cmdset_b, self)
|
||||
elif mergetype == "Remove":
|
||||
cmdset_c = self._remove(self, cmdset_b)
|
||||
else: # Union
|
||||
cmdset_c = self._union(cmdset_b, self)
|
||||
cmdset_c.no_channels = cmdset_b.no_channels
|
||||
cmdset_c.no_exits = cmdset_b.no_exits
|
||||
cmdset_c.no_objs = cmdset_b.no_objs
|
||||
if cmdset_b.key.startswith("_"):
|
||||
# don't rename new output if the merge set's name starts with _
|
||||
cmdset_c.key = self.key
|
||||
|
||||
# we store actual_mergetype since key_mergetypes
|
||||
# might be different from the main mergetype.
|
||||
# This is used for diagnosis.
|
||||
cmdset_c.actual_mergetype = mergetype
|
||||
|
||||
# return the system commands to the cmdset
|
||||
cmdset_c.add(sys_commands)
|
||||
return cmdset_c
|
||||
|
||||
def add(self, cmd):
|
||||
"""
|
||||
Add a command, a list of commands or a cmdset to this cmdset.
|
||||
|
||||
Note that if cmd already exists in set,
|
||||
it will replace the old one (no priority checking etc
|
||||
at this point; this is often used to overload
|
||||
default commands).
|
||||
|
||||
If cmd is another cmdset class or -instance, the commands
|
||||
of that command set is added to this one, as if they were part
|
||||
of the original cmdset definition. No merging or priority checks
|
||||
are made, rather later added commands will simply replace
|
||||
existing ones to make a unique set.
|
||||
"""
|
||||
|
||||
if inherits_from(cmd, "src.commands.cmdset.CmdSet"):
|
||||
# cmd is a command set so merge all commands in that set
|
||||
# to this one. We raise a visible error if we created
|
||||
# an infinite loop (adding cmdset to itself somehow)
|
||||
try:
|
||||
cmd = self._instantiate(cmd)
|
||||
except RuntimeError:
|
||||
string = "Adding cmdset %(cmd)s to %(class)s lead to an "
|
||||
string += "infinite loop. When adding a cmdset to another, "
|
||||
string += "make sure they are not themself cyclically added to "
|
||||
string += "the new cmdset somewhere in the chain."
|
||||
raise RuntimeError(_(string) % {"cmd": cmd,
|
||||
"class": self.__class__})
|
||||
cmds = cmd.commands
|
||||
elif is_iter(cmd):
|
||||
cmds = [self._instantiate(c) for c in cmd]
|
||||
else:
|
||||
cmds = [self._instantiate(cmd)]
|
||||
commands = self.commands
|
||||
system_commands = self.system_commands
|
||||
for cmd in cmds:
|
||||
# add all commands
|
||||
if not hasattr(cmd, 'obj'):
|
||||
cmd.obj = self.cmdsetobj
|
||||
try:
|
||||
ic = commands.index(cmd)
|
||||
commands[ic] = cmd # replace
|
||||
except ValueError:
|
||||
commands.append(cmd)
|
||||
# extra run to make sure to avoid doublets
|
||||
self.commands = list(set(commands))
|
||||
#print "In cmdset.add(cmd):", self.key, cmd
|
||||
# add system_command to separate list as well,
|
||||
# for quick look-up
|
||||
if cmd.key.startswith("__"):
|
||||
try:
|
||||
ic = system_commands.index(cmd)
|
||||
system_commands[ic] = cmd # replace
|
||||
except ValueError:
|
||||
system_commands.append(cmd)
|
||||
|
||||
def remove(self, cmd):
|
||||
"""
|
||||
Remove a command instance from the cmdset.
|
||||
cmd can be either a cmd instance or a key string.
|
||||
"""
|
||||
cmd = self._instantiate(cmd)
|
||||
self.commands = [oldcmd for oldcmd in self.commands if oldcmd != cmd]
|
||||
|
||||
def get(self, cmd):
|
||||
"""
|
||||
Return the command in this cmdset that matches the
|
||||
given command. cmd may be either a command instance or
|
||||
a key string.
|
||||
"""
|
||||
cmd = self._instantiate(cmd)
|
||||
for thiscmd in self.commands:
|
||||
if thiscmd == cmd:
|
||||
return thiscmd
|
||||
|
||||
def count(self):
|
||||
"Return number of commands in set"
|
||||
return len(self.commands)
|
||||
|
||||
def get_system_cmds(self):
|
||||
"""
|
||||
Return system commands in the cmdset, defined as
|
||||
commands starting with double underscore __.
|
||||
These are excempt from merge operations.
|
||||
"""
|
||||
return self.system_commands
|
||||
#return [cmd for cmd in self.commands if cmd.key.startswith('__')]
|
||||
|
||||
def make_unique(self, caller):
|
||||
"""
|
||||
This is an unsafe command meant to clean out a cmdset of
|
||||
doublet commands after it has been created. It is useful
|
||||
for commands inheriting cmdsets from the cmdhandler where
|
||||
obj-based cmdsets always are added double. Doublets will
|
||||
be weeded out with preference to commands defined on caller,
|
||||
otherwise just by first-come-first-served.
|
||||
"""
|
||||
unique = {}
|
||||
for cmd in self.commands:
|
||||
if cmd.key in unique:
|
||||
ocmd = unique[cmd.key]
|
||||
if (hasattr(cmd, 'obj') and cmd.obj == caller) and not \
|
||||
(hasattr(ocmd, 'obj') and ocmd.obj == caller):
|
||||
unique[cmd.key] = cmd
|
||||
else:
|
||||
unique[cmd.key] = cmd
|
||||
self.commands = unique.values()
|
||||
|
||||
def get_all_cmd_keys_and_aliases(self, caller=None):
|
||||
"""
|
||||
Returns a list of all command keys and aliases
|
||||
available in this cmdset. If caller is given, the
|
||||
commands is checked for access on the "call" type
|
||||
before being returned.
|
||||
"""
|
||||
names = []
|
||||
if caller:
|
||||
[names.extend(cmd._keyaliases) for cmd in self.commands
|
||||
if cmd.access(caller)]
|
||||
else:
|
||||
[names.extend(cmd._keyaliases) for cmd in self.commands]
|
||||
return names
|
||||
|
||||
def at_cmdset_creation(self):
|
||||
"""
|
||||
Hook method - this should be overloaded in the inheriting
|
||||
class, and should take care of populating the cmdset
|
||||
by use of self.add().
|
||||
"""
|
||||
pass
|
||||
478
lib/commands/cmdsethandler.py
Normal file
478
lib/commands/cmdsethandler.py
Normal file
|
|
@ -0,0 +1,478 @@
|
|||
"""
|
||||
CmdSethandler
|
||||
|
||||
The Cmdsethandler tracks an object's 'Current CmdSet', which is the
|
||||
current merged sum of all CmdSets added to it.
|
||||
|
||||
A CmdSet constitues a set of commands. The CmdSet works as a special
|
||||
intelligent container that, when added to other CmdSet make sure that
|
||||
same-name commands are treated correctly (usually so there are no
|
||||
doublets). This temporary but up-to-date merger of CmdSet is jointly
|
||||
called the Current Cmset. It is this Current CmdSet that the
|
||||
commandhandler looks through whenever a player enters a command (it
|
||||
also adds CmdSets from objects in the room in real-time). All player
|
||||
objects have a 'default cmdset' containing all the normal in-game mud
|
||||
commands (look etc).
|
||||
|
||||
So what is all this cmdset complexity good for?
|
||||
|
||||
In its simplest form, a CmdSet has no commands, only a key name. In
|
||||
this case the cmdset's use is up to each individual game - it can be
|
||||
used by an AI module for example (mobs in cmdset 'roam' move from room
|
||||
to room, in cmdset 'attack' they enter combat with players).
|
||||
|
||||
Defining commands in cmdsets offer some further powerful game-design
|
||||
consequences however. Here are some examples:
|
||||
|
||||
As mentioned above, all players always have at least the Default
|
||||
CmdSet. This contains the set of all normal-use commands in-game,
|
||||
stuff like look and @desc etc. Now assume our players end up in a dark
|
||||
room. You don't want the player to be able to do much in that dark
|
||||
room unless they light a candle. You could handle this by changing all
|
||||
your normal commands to check if the player is in a dark room. This
|
||||
rapidly goes unwieldly and error prone. Instead you just define a
|
||||
cmdset with only those commands you want to be available in the 'dark'
|
||||
cmdset - maybe a modified look command and a 'light candle' command -
|
||||
and have this completely replace the default cmdset.
|
||||
|
||||
Another example: Say you want your players to be able to go
|
||||
fishing. You could implement this as a 'fish' command that fails
|
||||
whenever the player has no fishing rod. Easy enough. But what if you
|
||||
want to make fishing more complex - maybe you want four-five different
|
||||
commands for throwing your line, reeling in, etc? Most players won't
|
||||
(we assume) have fishing gear, and having all those detailed commands
|
||||
is cluttering up the command list. And what if you want to use the
|
||||
'throw' command also for throwing rocks etc instead of 'using it up'
|
||||
for a minor thing like fishing?
|
||||
|
||||
So instead you put all those detailed fishing commands into their own
|
||||
CommandSet called 'Fishing'. Whenever the player gives the command
|
||||
'fish' (presumably the code checks there is also water nearby), only
|
||||
THEN this CommandSet is added to the Cmdhandler of the player. The
|
||||
'throw' command (which normally throws rocks) is replaced by the
|
||||
custom 'fishing variant' of throw. What has happened is that the
|
||||
Fishing CommandSet was merged on top of the Default ones, and due to
|
||||
how we defined it, its command overrules the default ones.
|
||||
|
||||
When we are tired of fishing, we give the 'go home' command (or
|
||||
whatever) and the Cmdhandler simply removes the fishing CommandSet
|
||||
so that we are back at defaults (and can throw rocks again).
|
||||
|
||||
Since any number of CommandSets can be piled on top of each other, you
|
||||
can then implement separate sets for different situations. For
|
||||
example, you can have a 'On a boat' set, onto which you then tack on
|
||||
the 'Fishing' set. Fishing from a boat? No problem!
|
||||
"""
|
||||
from django.conf import settings
|
||||
from src.utils import logger, utils
|
||||
from src.commands.cmdset import CmdSet
|
||||
from src.server.models import ServerConfig
|
||||
|
||||
from django.utils.translation import ugettext as _
|
||||
__all__ = ("import_cmdset", "CmdSetHandler")
|
||||
|
||||
_CACHED_CMDSETS = {}
|
||||
_CMDSET_PATHS = utils.make_iter(settings.CMDSET_PATHS)
|
||||
|
||||
class _ErrorCmdSet(CmdSet):
|
||||
"This is a special cmdset used to report errors"
|
||||
key = "_CMDSET_ERROR"
|
||||
errmessage = "Error when loading cmdset."
|
||||
|
||||
class _EmptyCmdSet(CmdSet):
|
||||
"This cmdset represents an empty cmdset"
|
||||
key = "_EMPTY_CMDSET"
|
||||
priority = -101
|
||||
mergetype = "Union"
|
||||
|
||||
def import_cmdset(path, cmdsetobj, emit_to_obj=None, no_logging=False):
|
||||
"""
|
||||
This helper function is used by the cmdsethandler to load a cmdset
|
||||
instance from a python module, given a python_path. It's usually accessed
|
||||
through the cmdsethandler's add() and add_default() methods.
|
||||
path - This is the full path to the cmdset object on python dot-form
|
||||
cmdsetobj - the database object/typeclass on which this cmdset is to be
|
||||
assigned (this can be also channels and exits, as well as players
|
||||
but there will always be such an object)
|
||||
emit_to_obj - if given, error is emitted to this object (in addition
|
||||
to logging)
|
||||
no_logging - don't log/send error messages. This can be useful
|
||||
if import_cmdset is just used to check if this is a
|
||||
valid python path or not.
|
||||
function returns None if an error was encountered or path not found.
|
||||
"""
|
||||
|
||||
python_paths = [path] + ["%s.%s" % (prefix, path)
|
||||
for prefix in _CMDSET_PATHS if not path.startswith(prefix)]
|
||||
errstring = ""
|
||||
for python_path in python_paths:
|
||||
try:
|
||||
#print "importing %s: _CACHED_CMDSETS=%s" % (python_path, _CACHED_CMDSETS)
|
||||
wanted_cache_key = python_path
|
||||
cmdsetclass = _CACHED_CMDSETS.get(wanted_cache_key, None)
|
||||
errstring = ""
|
||||
if not cmdsetclass:
|
||||
#print "cmdset '%s' not in cache. Reloading %s on %s." % (wanted_cache_key, python_path, cmdsetobj)
|
||||
# Not in cache. Reload from disk.
|
||||
modulepath, classname = python_path.rsplit('.', 1)
|
||||
module = __import__(modulepath, fromlist=[True])
|
||||
cmdsetclass = module.__dict__[classname]
|
||||
_CACHED_CMDSETS[wanted_cache_key] = cmdsetclass
|
||||
#instantiate the cmdset (and catch its errors)
|
||||
if callable(cmdsetclass):
|
||||
cmdsetclass = cmdsetclass(cmdsetobj)
|
||||
return cmdsetclass
|
||||
except ImportError, e:
|
||||
logger.log_trace()
|
||||
errstring += _("Error loading cmdset '%s': %s.")
|
||||
errstring = errstring % (modulepath, e)
|
||||
except KeyError:
|
||||
logger.log_trace()
|
||||
errstring += _("Error in loading cmdset: No cmdset class '%(classname)s' in %(modulepath)s.")
|
||||
errstring = errstring % {"classname": classname,
|
||||
"modulepath": modulepath}
|
||||
except SyntaxError, e:
|
||||
logger.log_trace()
|
||||
errstring += _("SyntaxError encountered when loading cmdset '%s': %s.")
|
||||
errstring = errstring % (modulepath, e)
|
||||
except Exception, e:
|
||||
logger.log_trace()
|
||||
errstring += _("Compile/Run error when loading cmdset '%s': %s.")
|
||||
errstring = errstring % (python_path, e)
|
||||
|
||||
if errstring:
|
||||
# returning an empty error cmdset
|
||||
if not no_logging:
|
||||
logger.log_errmsg(errstring)
|
||||
if emit_to_obj and not ServerConfig.objects.conf("server_starting_mode"):
|
||||
emit_to_obj.msg(errstring)
|
||||
err_cmdset = _ErrorCmdSet()
|
||||
err_cmdset.errmessage = errstring + _("\n (See log for details.)")
|
||||
return err_cmdset
|
||||
|
||||
# classes
|
||||
|
||||
|
||||
class CmdSetHandler(object):
|
||||
"""
|
||||
The CmdSetHandler is always stored on an object, this object is supplied
|
||||
as an argument.
|
||||
|
||||
The 'current' cmdset is the merged set currently active for this object.
|
||||
This is the set the game engine will retrieve when determining which
|
||||
commands are available to the object. The cmdset_stack holds a history of
|
||||
all CmdSets to allow the handler to remove/add cmdsets at will. Doing so
|
||||
will re-calculate the 'current' cmdset.
|
||||
"""
|
||||
|
||||
def __init__(self, obj, init_true=True):
|
||||
"""
|
||||
This method is called whenever an object is recreated.
|
||||
|
||||
obj - this is a reference to the game object this handler
|
||||
belongs to.
|
||||
"""
|
||||
self.obj = obj
|
||||
|
||||
# the id of the "merged" current cmdset for easy access.
|
||||
self.key = None
|
||||
# this holds the "merged" current command set
|
||||
self.current = None
|
||||
# this holds a history of CommandSets
|
||||
self.cmdset_stack = [_EmptyCmdSet(cmdsetobj=self.obj)]
|
||||
# this tracks which mergetypes are actually in play in the stack
|
||||
self.mergetype_stack = ["Union"]
|
||||
|
||||
# the subset of the cmdset_paths that are to be stored in the database
|
||||
self.permanent_paths = [""]
|
||||
|
||||
if init_true:
|
||||
self.update(init_mode=True) #is then called from the object __init__.
|
||||
|
||||
def __str__(self):
|
||||
"Display current commands"
|
||||
|
||||
string = ""
|
||||
mergelist = []
|
||||
if len(self.cmdset_stack) > 1:
|
||||
# We have more than one cmdset in stack; list them all
|
||||
#print self.cmdset_stack, self.mergetype_stack
|
||||
for snum, cmdset in enumerate(self.cmdset_stack):
|
||||
mergetype = self.mergetype_stack[snum]
|
||||
permstring = "non-perm"
|
||||
if cmdset.permanent:
|
||||
permstring = "perm"
|
||||
if mergetype != cmdset.mergetype:
|
||||
mergetype = "%s^" % (mergetype)
|
||||
string += "\n %i: <%s (%s, prio %i, %s)>: %s" % \
|
||||
(snum, cmdset.key, mergetype,
|
||||
cmdset.priority, permstring, cmdset)
|
||||
mergelist.append(str(snum))
|
||||
string += "\n"
|
||||
|
||||
# Display the currently active cmdset, limited by self.obj's permissions
|
||||
mergetype = self.mergetype_stack[-1]
|
||||
if mergetype != self.current.mergetype:
|
||||
merged_on = self.cmdset_stack[-2].key
|
||||
mergetype = _("custom %(mergetype)s on cmdset '%(merged_on)s'") % \
|
||||
{"mergetype": mergetype, "merged_on":merged_on}
|
||||
if mergelist:
|
||||
string += _(" <Merged %(mergelist)s (%(mergetype)s, prio %(prio)i)>: %(current)s") % \
|
||||
{"mergelist": "+".join(mergelist),
|
||||
"mergetype": mergetype, "prio": self.current.priority,
|
||||
"current":self.current}
|
||||
else:
|
||||
permstring = "non-perm"
|
||||
if self.current.permanent:
|
||||
permstring = "perm"
|
||||
string += _(" <%(key)s (%(mergetype)s, prio %(prio)i, %(permstring)s)>: %(keylist)s") % \
|
||||
{"key": self.current.key, "mergetype": mergetype,
|
||||
"prio": self.current.priority, "permstring": permstring,
|
||||
"keylist": ", ".join(cmd.key for cmd in sorted(self.current, key=lambda o: o.key))}
|
||||
return string.strip()
|
||||
|
||||
def _import_cmdset(self, cmdset_path, emit_to_obj=None):
|
||||
"""
|
||||
Method wrapper for import_cmdset.
|
||||
load a cmdset from a module.
|
||||
cmdset_path - the python path to an cmdset object.
|
||||
emit_to_obj - object to send error messages to
|
||||
"""
|
||||
if not emit_to_obj:
|
||||
emit_to_obj = self.obj
|
||||
return import_cmdset(cmdset_path, self.obj, emit_to_obj)
|
||||
|
||||
def update(self, init_mode=False):
|
||||
"""
|
||||
Re-adds all sets in the handler to have an updated
|
||||
current set.
|
||||
|
||||
init_mode is used right after this handler was
|
||||
created; it imports all permanent cmdsets from db.
|
||||
"""
|
||||
if init_mode:
|
||||
# reimport all permanent cmdsets
|
||||
storage = self.obj.cmdset_storage
|
||||
#print "cmdset_storage:", self.obj.cmdset_storage
|
||||
if storage:
|
||||
self.cmdset_stack = []
|
||||
for pos, path in enumerate(storage):
|
||||
if pos == 0 and not path:
|
||||
self.cmdset_stack = [_EmptyCmdSet(cmdsetobj=self.obj)]
|
||||
elif path:
|
||||
cmdset = self._import_cmdset(path)
|
||||
if cmdset:
|
||||
cmdset.permanent = cmdset.key != '_CMDSET_ERROR'
|
||||
self.cmdset_stack.append(cmdset)
|
||||
|
||||
# merge the stack into a new merged cmdset
|
||||
new_current = None
|
||||
self.mergetype_stack = []
|
||||
for cmdset in self.cmdset_stack:
|
||||
try:
|
||||
# for cmdset's '+' operator, order matters.
|
||||
new_current = cmdset + new_current
|
||||
except TypeError:
|
||||
continue
|
||||
self.mergetype_stack.append(new_current.actual_mergetype)
|
||||
self.current = new_current
|
||||
|
||||
def add(self, cmdset, emit_to_obj=None, permanent=False):
|
||||
"""
|
||||
Add a cmdset to the handler, on top of the old ones.
|
||||
Default is to not make this permanent, i.e. the set
|
||||
will not survive a server reset.
|
||||
|
||||
cmdset - can be a cmdset object or the python path to
|
||||
such an object.
|
||||
emit_to_obj - an object to receive error messages.
|
||||
permanent - this cmdset will remain across a server reboot
|
||||
|
||||
Note: An interesting feature of this method is if you were to
|
||||
send it an *already instantiated cmdset* (i.e. not a class),
|
||||
the current cmdsethandler's obj attribute will then *not* be
|
||||
transferred over to this already instantiated set (this is
|
||||
because it might be used elsewhere and can cause strange effects).
|
||||
This means you could in principle have the handler
|
||||
launch command sets tied to a *different* object than the
|
||||
handler. Not sure when this would be useful, but it's a 'quirk'
|
||||
that has to be documented.
|
||||
"""
|
||||
if not (isinstance(cmdset, basestring) or utils.inherits_from(cmdset, CmdSet)):
|
||||
raise Exception(_("Only CmdSets can be added to the cmdsethandler!"))
|
||||
if callable(cmdset):
|
||||
cmdset = cmdset(self.obj)
|
||||
elif isinstance(cmdset, basestring):
|
||||
# this is (maybe) a python path. Try to import from cache.
|
||||
cmdset = self._import_cmdset(cmdset)
|
||||
if cmdset and cmdset.key != '_CMDSET_ERROR':
|
||||
if permanent and cmdset.key != '_CMDSET_ERROR':
|
||||
# store the path permanently
|
||||
cmdset.permanent = True
|
||||
storage = self.obj.cmdset_storage
|
||||
if not storage:
|
||||
storage = ["", cmdset.path]
|
||||
else:
|
||||
storage.append(cmdset.path)
|
||||
self.obj.cmdset_storage = storage
|
||||
else:
|
||||
cmdset.permanent = False
|
||||
self.cmdset_stack.append(cmdset)
|
||||
self.update()
|
||||
|
||||
def add_default(self, cmdset, emit_to_obj=None, permanent=True):
|
||||
"""
|
||||
Add a new default cmdset. If an old default existed,
|
||||
it is replaced. If permanent is set, the set will survive a reboot.
|
||||
cmdset - can be a cmdset object or the python path to
|
||||
an instance of such an object.
|
||||
emit_to_obj - an object to receive error messages.
|
||||
permanent - save cmdset across reboots
|
||||
See also the notes for self.add(), which applies here too.
|
||||
"""
|
||||
if callable(cmdset):
|
||||
if not utils.inherits_from(cmdset, CmdSet):
|
||||
raise Exception(_("Only CmdSets can be added to the cmdsethandler!"))
|
||||
cmdset = cmdset(self.obj)
|
||||
elif isinstance(cmdset, basestring):
|
||||
# this is (maybe) a python path. Try to import from cache.
|
||||
cmdset = self._import_cmdset(cmdset)
|
||||
if cmdset and cmdset.key != '_CMDSET_ERROR':
|
||||
if self.cmdset_stack:
|
||||
self.cmdset_stack[0] = cmdset
|
||||
self.mergetype_stack[0] = cmdset.mergetype
|
||||
else:
|
||||
self.cmdset_stack = [cmdset]
|
||||
self.mergetype_stack = [cmdset.mergetype]
|
||||
|
||||
if permanent and cmdset.key != '_CMDSET_ERROR':
|
||||
cmdset.permanent = True
|
||||
storage = self.obj.cmdset_storage
|
||||
if storage:
|
||||
storage[0] = cmdset.path
|
||||
else:
|
||||
storage = [cmdset.path]
|
||||
self.obj.cmdset_storage = storage
|
||||
else:
|
||||
cmdset.permanent = False
|
||||
self.update()
|
||||
|
||||
def delete(self, cmdset=None):
|
||||
"""
|
||||
Remove a cmdset from the handler.
|
||||
|
||||
cmdset can be supplied either as a cmdset-key,
|
||||
an instance of the CmdSet or a python path
|
||||
to the cmdset. If no key is given,
|
||||
the last cmdset in the stack is removed. Whenever
|
||||
the cmdset_stack changes, the cmdset is updated.
|
||||
The default cmdset (first entry in stack) is never
|
||||
removed - remove it explicitly with delete_default.
|
||||
|
||||
"""
|
||||
if len(self.cmdset_stack) < 2:
|
||||
# don't allow deleting default cmdsets here.
|
||||
return
|
||||
|
||||
if not cmdset:
|
||||
# remove the last one in the stack
|
||||
cmdset = self.cmdset_stack.pop()
|
||||
if cmdset.permanent:
|
||||
storage = self.obj.cmdset_storage
|
||||
storage.pop()
|
||||
self.obj.cmdset_storage = storage
|
||||
else:
|
||||
# try it as a callable
|
||||
if callable(cmdset) and hasattr(cmdset, 'path'):
|
||||
delcmdsets = [cset for cset in self.cmdset_stack[1:]
|
||||
if cset.path == cmdset.path]
|
||||
else:
|
||||
# try it as a path or key
|
||||
delcmdsets = [cset for cset in self.cmdset_stack[1:]
|
||||
if cset.path == cmdset or cset.key == cmdset]
|
||||
storage = []
|
||||
|
||||
if any(cset.permanent for cset in delcmdsets):
|
||||
# only hit database if there's need to
|
||||
storage = self.obj.cmdset_storage
|
||||
for cset in delcmdsets:
|
||||
if cset.permanent:
|
||||
try:
|
||||
storage.remove(cset.path)
|
||||
except ValueError:
|
||||
pass
|
||||
for cset in delcmdsets:
|
||||
# clean the in-memory stack
|
||||
try:
|
||||
self.cmdset_stack.remove(cset)
|
||||
except ValueError:
|
||||
pass
|
||||
# re-sync the cmdsethandler.
|
||||
self.update()
|
||||
|
||||
def delete_default(self):
|
||||
"""
|
||||
This explicitly deletes the default cmdset. It's the
|
||||
only command that can.
|
||||
"""
|
||||
if self.cmdset_stack:
|
||||
cmdset = self.cmdset_stack[0]
|
||||
if cmdset.permanent:
|
||||
storage = self.obj.cmdset_storage
|
||||
if storage:
|
||||
storage[0] = ""
|
||||
else:
|
||||
storage = [""]
|
||||
self.cmdset_storage = storage
|
||||
self.cmdset_stack[0] = _EmptyCmdSet(cmdsetobj=self.obj)
|
||||
else:
|
||||
self.cmdset_stack = [_EmptyCmdSet(cmdsetobj=self.obj)]
|
||||
self.update()
|
||||
|
||||
def all(self):
|
||||
"""
|
||||
Returns the list of cmdsets. Mostly useful to check
|
||||
if stack if empty or not.
|
||||
"""
|
||||
return self.cmdset_stack
|
||||
|
||||
def clear(self):
|
||||
"""
|
||||
Removes all extra Command sets from the handler, leaving only the
|
||||
default one.
|
||||
"""
|
||||
self.cmdset_stack = [self.cmdset_stack[0]]
|
||||
self.mergetype_stack = [self.cmdset_stack[0].mergetype]
|
||||
storage = self.obj.cmdset_storage
|
||||
if storage:
|
||||
storage = storage[0]
|
||||
self.obj.cmdset_storage = storage
|
||||
self.update()
|
||||
|
||||
def has_cmdset(self, cmdset_key, must_be_default=False):
|
||||
"""
|
||||
checks so the cmdsethandler contains a cmdset with the given key.
|
||||
must_be_default - only match against the default cmdset.
|
||||
"""
|
||||
if must_be_default:
|
||||
return self.cmdset_stack and self.cmdset_stack[0].key == cmdset_key
|
||||
else:
|
||||
return any([cmdset.key == cmdset_key for cmdset in self.cmdset_stack])
|
||||
|
||||
def reset(self):
|
||||
"""
|
||||
Force reload of all cmdsets in handler. This should be called
|
||||
after _CACHED_CMDSETS have been cleared (normally by @reload).
|
||||
"""
|
||||
new_cmdset_stack = []
|
||||
new_mergetype_stack = []
|
||||
for cmdset in self.cmdset_stack:
|
||||
if cmdset.key == "_EMPTY_CMDSET":
|
||||
new_cmdset_stack.append(cmdset)
|
||||
new_mergetype_stack.append("Union")
|
||||
else:
|
||||
new_cmdset_stack.append(self._import_cmdset(cmdset.path))
|
||||
new_mergetype_stack.append(cmdset.mergetype)
|
||||
self.cmdset_stack = new_cmdset_stack
|
||||
self.mergetype_stack = new_mergetype_stack
|
||||
self.update()
|
||||
312
lib/commands/command.py
Normal file
312
lib/commands/command.py
Normal file
|
|
@ -0,0 +1,312 @@
|
|||
"""
|
||||
The base Command class.
|
||||
|
||||
All commands in Evennia inherit from the 'Command' class in this module.
|
||||
|
||||
"""
|
||||
|
||||
import re
|
||||
from src.locks.lockhandler import LockHandler
|
||||
from src.utils.utils import is_iter, fill, lazy_property
|
||||
|
||||
|
||||
def _init_command(mcs, **kwargs):
|
||||
"""
|
||||
Helper command.
|
||||
Makes sure all data are stored as lowercase and
|
||||
do checking on all properties that should be in list form.
|
||||
Sets up locks to be more forgiving. This is used both by the metaclass
|
||||
and (optionally) at instantiation time.
|
||||
|
||||
If kwargs are given, these are set as instance-specific properties
|
||||
on the command.
|
||||
"""
|
||||
for i in range(len(kwargs)):
|
||||
# used for dynamic creation of commands
|
||||
key, value = kwargs.popitem()
|
||||
setattr(mcs, key, value)
|
||||
|
||||
mcs.key = mcs.key.lower()
|
||||
if mcs.aliases and not is_iter(mcs.aliases):
|
||||
try:
|
||||
mcs.aliases = [str(alias).strip().lower()
|
||||
for alias in mcs.aliases.split(',')]
|
||||
except Exception:
|
||||
mcs.aliases = []
|
||||
mcs.aliases = list(set(alias for alias in mcs.aliases
|
||||
if alias and alias != mcs.key))
|
||||
|
||||
# optimization - a set is much faster to match against than a list
|
||||
mcs._matchset = set([mcs.key] + mcs.aliases)
|
||||
# optimization for looping over keys+aliases
|
||||
mcs._keyaliases = tuple(mcs._matchset)
|
||||
|
||||
# by default we don't save the command between runs
|
||||
if not hasattr(mcs, "save_for_next"):
|
||||
mcs.save_for_next = False
|
||||
|
||||
# pre-process locks as defined in class definition
|
||||
temp = []
|
||||
if hasattr(mcs, 'permissions'):
|
||||
mcs.locks = mcs.permissions
|
||||
if not hasattr(mcs, 'locks'):
|
||||
# default if one forgets to define completely
|
||||
mcs.locks = "cmd:all()"
|
||||
if not "cmd:" in mcs.locks:
|
||||
mcs.locks = "cmd:all();" + mcs.locks
|
||||
for lockstring in mcs.locks.split(';'):
|
||||
if lockstring and not ':' in lockstring:
|
||||
lockstring = "cmd:%s" % lockstring
|
||||
temp.append(lockstring)
|
||||
mcs.lock_storage = ";".join(temp)
|
||||
|
||||
if hasattr(mcs, 'arg_regex') and isinstance(mcs.arg_regex, basestring):
|
||||
mcs.arg_regex = re.compile(r"%s" % mcs.arg_regex, re.I)
|
||||
if not hasattr(mcs, "auto_help"):
|
||||
mcs.auto_help = True
|
||||
if not hasattr(mcs, 'is_exit'):
|
||||
mcs.is_exit = False
|
||||
if not hasattr(mcs, "help_category"):
|
||||
mcs.help_category = "general"
|
||||
mcs.help_category = mcs.help_category.lower()
|
||||
|
||||
|
||||
class CommandMeta(type):
|
||||
"""
|
||||
The metaclass cleans up all properties on the class
|
||||
"""
|
||||
def __init__(mcs, *args, **kwargs):
|
||||
_init_command(mcs, **kwargs)
|
||||
super(CommandMeta, mcs).__init__(*args, **kwargs)
|
||||
|
||||
# The Command class is the basic unit of an Evennia command; when
|
||||
# defining new commands, the admin subclass this class and
|
||||
# define their own parser method to handle the input. The
|
||||
# advantage of this is inheritage; commands that have similar
|
||||
# structure can parse the input string the same way, minimizing
|
||||
# parsing errors.
|
||||
|
||||
|
||||
class Command(object):
|
||||
"""
|
||||
Base command
|
||||
|
||||
Usage:
|
||||
command [args]
|
||||
|
||||
This is the base command class. Inherit from this
|
||||
to create new commands.
|
||||
|
||||
The cmdhandler makes the following variables available to the
|
||||
command methods (so you can always assume them to be there):
|
||||
self.caller - the game object calling the command
|
||||
self.cmdstring - the command name used to trigger this command (allows
|
||||
you to know which alias was used, for example)
|
||||
cmd.args - everything supplied to the command following the cmdstring
|
||||
(this is usually what is parsed in self.parse())
|
||||
cmd.cmdset - the cmdset from which this command was matched (useful only
|
||||
seldomly, notably for help-type commands, to create dynamic
|
||||
help entries and lists)
|
||||
cmd.obj - the object on which this command is defined. If a default command,
|
||||
this is usually the same as caller.
|
||||
|
||||
The following class properties can/should be defined on your child class:
|
||||
|
||||
key - identifier for command (e.g. "look")
|
||||
aliases - (optional) list of aliases (e.g. ["l", "loo"])
|
||||
locks - lock string (default is "cmd:all()")
|
||||
help_category - how to organize this help entry in help system
|
||||
(default is "General")
|
||||
auto_help - defaults to True. Allows for turning off auto-help generation
|
||||
arg_regex - (optional) raw string regex defining how the argument part of
|
||||
the command should look in order to match for this command
|
||||
(e.g. must it be a space between cmdname and arg?)
|
||||
|
||||
(Note that if auto_help is on, this initial string is also used by the
|
||||
system to create the help entry for the command, so it's a good idea to
|
||||
format it similar to this one)
|
||||
"""
|
||||
# Tie our metaclass, for some convenience cleanup
|
||||
__metaclass__ = CommandMeta
|
||||
|
||||
# the main way to call this command (e.g. 'look')
|
||||
key = "command"
|
||||
# alternative ways to call the command (e.g. 'l', 'glance', 'examine')
|
||||
aliases = []
|
||||
# a list of lock definitions on the form
|
||||
# cmd:[NOT] func(args) [ AND|OR][ NOT] func2(args)
|
||||
locks = ""
|
||||
# used by the help system to group commands in lists.
|
||||
help_category = "general"
|
||||
# This allows to turn off auto-help entry creation for individual commands.
|
||||
auto_help = True
|
||||
# optimization for quickly separating exit-commands from normal commands
|
||||
is_exit = False
|
||||
# define the command not only by key but by the regex form of its arguments
|
||||
arg_regex = None
|
||||
|
||||
# auto-set (by Evennia on command instantiation) are:
|
||||
# obj - which object this command is defined on
|
||||
# sessid - which session-id (if any) is responsible for triggering this command
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
"""the lockhandler works the same as for objects.
|
||||
optional kwargs will be set as properties on the Command at runtime,
|
||||
overloading evential same-named class properties."""
|
||||
if kwargs:
|
||||
_init_command(self, **kwargs)
|
||||
|
||||
@lazy_property
|
||||
def lockhandler(self):
|
||||
return LockHandler(self)
|
||||
|
||||
def __str__(self):
|
||||
"Print the command"
|
||||
return self.key
|
||||
|
||||
def __eq__(self, cmd):
|
||||
"""
|
||||
Compare two command instances to each other by matching their
|
||||
key and aliases.
|
||||
input can be either a cmd object or the name of a command.
|
||||
"""
|
||||
try:
|
||||
# first assume input is a command (the most common case)
|
||||
return cmd.key in self._matchset
|
||||
except AttributeError:
|
||||
# probably got a string
|
||||
return cmd in self._matchset
|
||||
|
||||
def __ne__(self, cmd):
|
||||
"""
|
||||
The logical negation of __eq__. Since this is one of the
|
||||
most called methods in Evennia (along with __eq__) we do some
|
||||
code-duplication here rather than issuing a method-lookup to __eq__.
|
||||
"""
|
||||
try:
|
||||
return not cmd.key in self._matcheset
|
||||
except AttributeError:
|
||||
return not cmd in self._matchset
|
||||
|
||||
def __contains__(self, query):
|
||||
"""
|
||||
This implements searches like 'if query in cmd'. It's a fuzzy matching
|
||||
used by the help system, returning True if query can be found
|
||||
as a substring of the commands key or its aliases.
|
||||
|
||||
query (str) - query to match against. Should be lower case.
|
||||
|
||||
"""
|
||||
return any(query in keyalias for keyalias in self._keyaliases)
|
||||
|
||||
def match(self, cmdname):
|
||||
"""
|
||||
This is called by the system when searching the available commands,
|
||||
in order to determine if this is the one we wanted. cmdname was
|
||||
previously extracted from the raw string by the system.
|
||||
|
||||
cmdname (str) is always lowercase when reaching this point.
|
||||
|
||||
"""
|
||||
return cmdname in self._matchset
|
||||
|
||||
def access(self, srcobj, access_type="cmd", default=False):
|
||||
"""
|
||||
This hook is called by the cmdhandler to determine if srcobj
|
||||
is allowed to execute this command. It should return a boolean
|
||||
value and is not normally something that need to be changed since
|
||||
it's using the Evennia permission system directly.
|
||||
"""
|
||||
return self.lockhandler.check(srcobj, access_type, default=default)
|
||||
|
||||
def msg(self, msg="", to_obj=None, from_obj=None,
|
||||
sessid=None, all_sessions=False, **kwargs):
|
||||
"""
|
||||
This is a shortcut instad of calling msg() directly on an object - it
|
||||
will detect if caller is an Object or a Player and also appends
|
||||
self.sessid automatically.
|
||||
|
||||
msg - text string of message to send
|
||||
to_obj - target object of message. Defaults to self.caller
|
||||
from_obj - source of message. Defaults to to_obj
|
||||
data - optional dictionary of data
|
||||
sessid - supply data only to a unique sessid (normally not used -
|
||||
this is only potentially useful if to_obj is a Player object
|
||||
different from self.caller or self.caller.player)
|
||||
all_sessions (bool) - default is to send only to the session
|
||||
connected to the target object
|
||||
"""
|
||||
from_obj = from_obj or self.caller
|
||||
to_obj = to_obj or from_obj
|
||||
if not sessid:
|
||||
if hasattr(to_obj, "sessid"):
|
||||
# this is the case when to_obj is e.g. a Character
|
||||
toobj_sessions = to_obj.sessid.get()
|
||||
|
||||
# If to_obj has more than one session MULTISESSION_MODE=3
|
||||
# we need to send to every session.
|
||||
#(setting it to None, does it)
|
||||
session_tosend = None
|
||||
if len(toobj_sessions) == 1:
|
||||
session_tosend=toobj_sessions[0]
|
||||
sessid = all_sessions and None or session_tosend
|
||||
elif to_obj == self.caller:
|
||||
# this is the case if to_obj is the calling Player
|
||||
sessid = all_sessions and None or self.sessid
|
||||
else:
|
||||
# if to_obj is a different Player, all their sessions
|
||||
# will be notified unless sessid was given specifically
|
||||
sessid = None
|
||||
to_obj.msg(msg, from_obj=from_obj, sessid=sessid, **kwargs)
|
||||
|
||||
# Common Command hooks
|
||||
|
||||
def at_pre_cmd(self):
|
||||
"""
|
||||
This hook is called before self.parse() on all commands.
|
||||
If this hook returns anything but False/None, the command
|
||||
sequence is aborted.
|
||||
"""
|
||||
pass
|
||||
|
||||
def at_post_cmd(self):
|
||||
"""
|
||||
This hook is called after the command has finished executing
|
||||
(after self.func()).
|
||||
"""
|
||||
pass
|
||||
|
||||
def parse(self):
|
||||
"""
|
||||
Once the cmdhandler has identified this as the command we
|
||||
want, this function is run. If many of your commands have
|
||||
a similar syntax (for example 'cmd arg1 = arg2') you should simply
|
||||
define this once and just let other commands of the same form
|
||||
inherit from this. See the docstring of this module for
|
||||
which object properties are available to use
|
||||
(notably self.args).
|
||||
"""
|
||||
pass
|
||||
|
||||
def func(self):
|
||||
"""
|
||||
This is the actual executing part of the command.
|
||||
It is called directly after self.parse(). See the docstring
|
||||
of this module for which object properties are available
|
||||
(beyond those set in self.parse())
|
||||
"""
|
||||
# a simple test command to show the available properties
|
||||
string = "-" * 50
|
||||
string += "\n{w%s{n - Command variables from evennia:\n" % self.key
|
||||
string += "-" * 50
|
||||
string += "\nname of cmd (self.key): {w%s{n\n" % self.key
|
||||
string += "cmd aliases (self.aliases): {w%s{n\n" % self.aliases
|
||||
string += "cmd locks (self.locks): {w%s{n\n" % self.locks
|
||||
string += "help category (self.help_category): {w%s{n\n" % self.help_category.capitalize()
|
||||
string += "object calling (self.caller): {w%s{n\n" % self.caller
|
||||
string += "object storing cmdset (self.obj): {w%s{n\n" % self.obj
|
||||
string += "command string given (self.cmdstring): {w%s{n\n" % self.cmdstring
|
||||
# show cmdset.key instead of cmdset to shorten output
|
||||
string += fill("current cmdset (self.cmdset): {w%s{n\n" % (self.cmdset.key if self.cmdset.key else self.cmdset.__class__))
|
||||
|
||||
self.caller.msg(string)
|
||||
21
lib/commands/connection_screen.py
Normal file
21
lib/commands/connection_screen.py
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
#
|
||||
# This is Evennia's default connection screen. It is imported
|
||||
# and run from game/gamesrc/world/connection_screens.py.
|
||||
#
|
||||
|
||||
from django.conf import settings
|
||||
from src.utils import utils
|
||||
|
||||
DEFAULT_SCREEN = \
|
||||
"""{b=============================================================={n
|
||||
Welcome to {g%s{n, version %s!
|
||||
|
||||
If you have an existing account, connect to it by typing:
|
||||
{wconnect <username> <password>{n
|
||||
If you need to create an account, type (without the <>'s):
|
||||
{wcreate <username> <password>{n
|
||||
|
||||
If you have spaces in your username, enclose it in quotes.
|
||||
Enter {whelp{n for more info. {wlook{n will re-show this screen.
|
||||
{b=============================================================={n""" \
|
||||
% (settings.SERVERNAME, utils.get_evennia_version())
|
||||
3
lib/commands/default/__init__.py
Normal file
3
lib/commands/default/__init__.py
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
"""
|
||||
This package contains all default commands of Evennia, grouped after category.
|
||||
"""
|
||||
560
lib/commands/default/admin.py
Normal file
560
lib/commands/default/admin.py
Normal file
|
|
@ -0,0 +1,560 @@
|
|||
"""
|
||||
|
||||
Admin commands
|
||||
|
||||
"""
|
||||
|
||||
import time
|
||||
import re
|
||||
from django.conf import settings
|
||||
from src.server.sessionhandler import SESSIONS
|
||||
from src.server.models import ServerConfig
|
||||
from src.utils import prettytable, search
|
||||
from src.commands.default.muxcommand import MuxCommand
|
||||
|
||||
PERMISSION_HIERARCHY = [p.lower() for p in settings.PERMISSION_HIERARCHY]
|
||||
|
||||
# limit members for API inclusion
|
||||
__all__ = ("CmdBoot", "CmdBan", "CmdUnban", "CmdDelPlayer",
|
||||
"CmdEmit", "CmdNewPassword", "CmdPerm", "CmdWall")
|
||||
|
||||
|
||||
class CmdBoot(MuxCommand):
|
||||
"""
|
||||
kick a player from the server.
|
||||
|
||||
Usage
|
||||
@boot[/switches] <player obj> [: reason]
|
||||
|
||||
Switches:
|
||||
quiet - Silently boot without informing player
|
||||
port - boot by port number instead of name or dbref
|
||||
|
||||
Boot a player object from the server. If a reason is
|
||||
supplied it will be echoed to the user unless /quiet is set.
|
||||
"""
|
||||
|
||||
key = "@boot"
|
||||
locks = "cmd:perm(boot) or perm(Wizards)"
|
||||
help_category = "Admin"
|
||||
|
||||
def func(self):
|
||||
"Implementing the function"
|
||||
caller = self.caller
|
||||
args = self.args
|
||||
|
||||
if not args:
|
||||
caller.msg("Usage: @boot[/switches] <player> [:reason]")
|
||||
return
|
||||
|
||||
if ':' in args:
|
||||
args, reason = [a.strip() for a in args.split(':', 1)]
|
||||
else:
|
||||
args, reason = args, ""
|
||||
|
||||
boot_list = []
|
||||
|
||||
if 'port' in self.switches:
|
||||
# Boot a particular port.
|
||||
sessions = SESSIONS.get_session_list(True)
|
||||
for sess in sessions:
|
||||
# Find the session with the matching port number.
|
||||
if sess.getClientAddress()[1] == int(args):
|
||||
boot_list.append(sess)
|
||||
break
|
||||
else:
|
||||
# Boot by player object
|
||||
pobj = search.player_search(args)
|
||||
if not pobj:
|
||||
self.caller("Player %s was not found." % pobj.key)
|
||||
return
|
||||
pobj = pobj[0]
|
||||
if not pobj.access(caller, 'boot'):
|
||||
string = "You don't have the permission to boot %s."
|
||||
pobj.msg(string)
|
||||
return
|
||||
# we have a bootable object with a connected user
|
||||
matches = SESSIONS.sessions_from_player(pobj)
|
||||
for match in matches:
|
||||
boot_list.append(match)
|
||||
|
||||
if not boot_list:
|
||||
caller.msg("No matching sessions found. The Player does not seem to be online.")
|
||||
return
|
||||
|
||||
# Carry out the booting of the sessions in the boot list.
|
||||
|
||||
feedback = None
|
||||
if not 'quiet' in self.switches:
|
||||
feedback = "You have been disconnected by %s.\n" % caller.name
|
||||
if reason:
|
||||
feedback += "\nReason given: %s" % reason
|
||||
|
||||
for session in boot_list:
|
||||
session.msg(feedback)
|
||||
pobj.disconnect_session_from_player(session.sessid)
|
||||
|
||||
|
||||
# regex matching IP addresses with wildcards, eg. 233.122.4.*
|
||||
IPREGEX = re.compile(r"[0-9*]{1,3}\.[0-9*]{1,3}\.[0-9*]{1,3}\.[0-9*]{1,3}")
|
||||
|
||||
|
||||
def list_bans(banlist):
|
||||
"""
|
||||
Helper function to display a list of active bans. Input argument
|
||||
is the banlist read into the two commands @ban and @unban below.
|
||||
"""
|
||||
if not banlist:
|
||||
return "No active bans were found."
|
||||
|
||||
table = prettytable.PrettyTable(["{wid", "{wname/ip", "{wdate", "{wreason"])
|
||||
for inum, ban in enumerate(banlist):
|
||||
table.add_row([str(inum + 1),
|
||||
ban[0] and ban[0] or ban[1],
|
||||
ban[3], ban[4]])
|
||||
string = "{wActive bans:{n\n%s" % table
|
||||
return string
|
||||
|
||||
|
||||
class CmdBan(MuxCommand):
|
||||
"""
|
||||
ban a player from the server
|
||||
|
||||
Usage:
|
||||
@ban [<name or ip> [: reason]]
|
||||
|
||||
Without any arguments, shows numbered list of active bans.
|
||||
|
||||
This command bans a user from accessing the game. Supply an
|
||||
optional reason to be able to later remember why the ban was put in
|
||||
place
|
||||
|
||||
It is often to
|
||||
prefer over deleting a player with @delplayer. If banned by name,
|
||||
that player account can no longer be logged into.
|
||||
|
||||
IP (Internet Protocol) address banning allows to block all access
|
||||
from a specific address or subnet. Use the asterisk (*) as a
|
||||
wildcard.
|
||||
|
||||
Examples:
|
||||
@ban thomas - ban account 'thomas'
|
||||
@ban/ip 134.233.2.111 - ban specific ip address
|
||||
@ban/ip 134.233.2.* - ban all in a subnet
|
||||
@ban/ip 134.233.*.* - even wider ban
|
||||
|
||||
A single IP filter is easy to circumvent by changing the computer
|
||||
(also, some ISPs assign only temporary IPs to their users in the
|
||||
first placer. Widening the IP block filter with wildcards might be
|
||||
tempting, but remember that blocking too much may accidentally
|
||||
also block innocent users connecting from the same country and
|
||||
region.
|
||||
|
||||
"""
|
||||
key = "@ban"
|
||||
aliases = ["@bans"]
|
||||
locks = "cmd:perm(ban) or perm(Immortals)"
|
||||
help_category = "Admin"
|
||||
|
||||
def func(self):
|
||||
"""
|
||||
Bans are stored in a serverconf db object as a list of
|
||||
dictionaries:
|
||||
[ (name, ip, ipregex, date, reason),
|
||||
(name, ip, ipregex, date, reason),... ]
|
||||
where name and ip are set by the user and are shown in
|
||||
lists. ipregex is a converted form of ip where the * is
|
||||
replaced by an appropriate regex pattern for fast
|
||||
matching. date is the time stamp the ban was instigated and
|
||||
'reason' is any optional info given to the command. Unset
|
||||
values in each tuple is set to the empty string.
|
||||
"""
|
||||
banlist = ServerConfig.objects.conf('server_bans')
|
||||
if not banlist:
|
||||
banlist = []
|
||||
|
||||
if not self.args or (self.switches
|
||||
and not any(switch in ('ip', 'name')
|
||||
for switch in self.switches)):
|
||||
self.caller.msg(list_bans(banlist))
|
||||
return
|
||||
|
||||
now = time.ctime()
|
||||
reason = ""
|
||||
if ':' in self.args:
|
||||
ban, reason = self.args.rsplit(':', 1)
|
||||
else:
|
||||
ban = self.args
|
||||
ban = ban.lower()
|
||||
ipban = IPREGEX.findall(ban)
|
||||
if not ipban:
|
||||
# store as name
|
||||
typ = "Name"
|
||||
bantup = (ban, "", "", now, reason)
|
||||
else:
|
||||
# an ip address.
|
||||
typ = "IP"
|
||||
ban = ipban[0]
|
||||
# replace * with regex form and compile it
|
||||
ipregex = ban.replace('.', '\.')
|
||||
ipregex = ipregex.replace('*', '[0-9]{1,3}')
|
||||
#print "regex:",ipregex
|
||||
ipregex = re.compile(r"%s" % ipregex)
|
||||
bantup = ("", ban, ipregex, now, reason)
|
||||
# save updated banlist
|
||||
banlist.append(bantup)
|
||||
ServerConfig.objects.conf('server_bans', banlist)
|
||||
self.caller.msg("%s-Ban {w%s{n was added." % (typ, ban))
|
||||
|
||||
|
||||
class CmdUnban(MuxCommand):
|
||||
"""
|
||||
remove a ban from a player
|
||||
|
||||
Usage:
|
||||
@unban <banid>
|
||||
|
||||
This will clear a player name/ip ban previously set with the @ban
|
||||
command. Use this command without an argument to view a numbered
|
||||
list of bans. Use the numbers in this list to select which one to
|
||||
unban.
|
||||
|
||||
"""
|
||||
key = "@unban"
|
||||
locks = "cmd:perm(unban) or perm(Immortals)"
|
||||
help_category = "Admin"
|
||||
|
||||
def func(self):
|
||||
"Implement unbanning"
|
||||
|
||||
banlist = ServerConfig.objects.conf('server_bans')
|
||||
|
||||
if not self.args:
|
||||
self.caller.msg(list_bans(banlist))
|
||||
return
|
||||
|
||||
try:
|
||||
num = int(self.args)
|
||||
except Exception:
|
||||
self.caller.msg("You must supply a valid ban id to clear.")
|
||||
return
|
||||
|
||||
if not banlist:
|
||||
self.caller.msg("There are no bans to clear.")
|
||||
elif not (0 < num < len(banlist) + 1):
|
||||
self.caller.msg("Ban id {w%s{x was not found." % self.args)
|
||||
else:
|
||||
# all is ok, clear ban
|
||||
ban = banlist[num - 1]
|
||||
del banlist[num - 1]
|
||||
ServerConfig.objects.conf('server_bans', banlist)
|
||||
self.caller.msg("Cleared ban %s: %s" %
|
||||
(num, " ".join([s for s in ban[:2]])))
|
||||
|
||||
|
||||
class CmdDelPlayer(MuxCommand):
|
||||
"""
|
||||
delete a player from the server
|
||||
|
||||
Usage:
|
||||
@delplayer[/switch] <name> [: reason]
|
||||
|
||||
Switch:
|
||||
delobj - also delete the player's currently
|
||||
assigned in-game object.
|
||||
|
||||
Completely deletes a user from the server database,
|
||||
making their nick and e-mail again available.
|
||||
"""
|
||||
|
||||
key = "@delplayer"
|
||||
locks = "cmd:perm(delplayer) or perm(Immortals)"
|
||||
help_category = "Admin"
|
||||
|
||||
def func(self):
|
||||
"Implements the command."
|
||||
|
||||
caller = self.caller
|
||||
args = self.args
|
||||
|
||||
if hasattr(caller, 'player'):
|
||||
caller = caller.player
|
||||
|
||||
if not args:
|
||||
self.msg("Usage: @delplayer <player/user name or #id> [: reason]")
|
||||
return
|
||||
|
||||
reason = ""
|
||||
if ':' in args:
|
||||
args, reason = [arg.strip() for arg in args.split(':', 1)]
|
||||
|
||||
# We use player_search since we want to be sure to find also players
|
||||
# that lack characters.
|
||||
players = search.player_search(args)
|
||||
|
||||
if not players:
|
||||
self.msg('Could not find a player by that name.')
|
||||
return
|
||||
|
||||
if len(players) > 1:
|
||||
string = "There were multiple matches:"
|
||||
for player in players:
|
||||
string += "\n %s %s" % (player.id, player.key)
|
||||
return
|
||||
|
||||
# one single match
|
||||
|
||||
player = players.pop()
|
||||
|
||||
if not player.access(caller, 'delete'):
|
||||
string = "You don't have the permissions to delete that player."
|
||||
self.msg(string)
|
||||
return
|
||||
|
||||
uname = player.username
|
||||
# boot the player then delete
|
||||
self.msg("Informing and disconnecting player ...")
|
||||
string = "\nYour account '%s' is being *permanently* deleted.\n" % uname
|
||||
if reason:
|
||||
string += " Reason given:\n '%s'" % reason
|
||||
player.msg(string)
|
||||
player.delete()
|
||||
self.msg("Player %s was successfully deleted." % uname)
|
||||
|
||||
|
||||
class CmdEmit(MuxCommand):
|
||||
"""
|
||||
admin command for emitting message to multiple objects
|
||||
|
||||
Usage:
|
||||
@emit[/switches] [<obj>, <obj>, ... =] <message>
|
||||
@remit [<obj>, <obj>, ... =] <message>
|
||||
@pemit [<obj>, <obj>, ... =] <message>
|
||||
|
||||
Switches:
|
||||
room : limit emits to rooms only (default)
|
||||
players : limit emits to players only
|
||||
contents : send to the contents of matched objects too
|
||||
|
||||
Emits a message to the selected objects or to
|
||||
your immediate surroundings. If the object is a room,
|
||||
send to its contents. @remit and @pemit are just
|
||||
limited forms of @emit, for sending to rooms and
|
||||
to players respectively.
|
||||
"""
|
||||
key = "@emit"
|
||||
aliases = ["@pemit", "@remit"]
|
||||
locks = "cmd:perm(emit) or perm(Builders)"
|
||||
help_category = "Admin"
|
||||
|
||||
def func(self):
|
||||
"Implement the command"
|
||||
|
||||
caller = self.caller
|
||||
args = self.args
|
||||
|
||||
if not args:
|
||||
string = "Usage: "
|
||||
string += "\n@emit[/switches] [<obj>, <obj>, ... =] <message>"
|
||||
string += "\n@remit [<obj>, <obj>, ... =] <message>"
|
||||
string += "\n@pemit [<obj>, <obj>, ... =] <message>"
|
||||
caller.msg(string)
|
||||
return
|
||||
|
||||
rooms_only = 'rooms' in self.switches
|
||||
players_only = 'players' in self.switches
|
||||
send_to_contents = 'contents' in self.switches
|
||||
|
||||
# we check which command was used to force the switches
|
||||
if self.cmdstring == '@remit':
|
||||
rooms_only = True
|
||||
send_to_contents = True
|
||||
elif self.cmdstring == '@pemit':
|
||||
players_only = True
|
||||
|
||||
if not self.rhs:
|
||||
message = self.args
|
||||
objnames = [caller.location.key]
|
||||
else:
|
||||
message = self.rhs
|
||||
objnames = self.lhslist
|
||||
|
||||
# send to all objects
|
||||
for objname in objnames:
|
||||
obj = caller.search(objname, global_search=True)
|
||||
if not obj:
|
||||
return
|
||||
if rooms_only and not obj.location is None:
|
||||
caller.msg("%s is not a room. Ignored." % objname)
|
||||
continue
|
||||
if players_only and not obj.has_player:
|
||||
caller.msg("%s has no active player. Ignored." % objname)
|
||||
continue
|
||||
if obj.access(caller, 'tell'):
|
||||
obj.msg(message)
|
||||
if send_to_contents and hasattr(obj, "msg_contents"):
|
||||
obj.msg_contents(message)
|
||||
caller.msg("Emitted to %s and contents:\n%s" % (objname, message))
|
||||
else:
|
||||
caller.msg("Emitted to %s:\n%s" % (objname, message))
|
||||
else:
|
||||
caller.msg("You are not allowed to emit to %s." % objname)
|
||||
|
||||
|
||||
class CmdNewPassword(MuxCommand):
|
||||
"""
|
||||
change the password of a player
|
||||
|
||||
Usage:
|
||||
@userpassword <user obj> = <new password>
|
||||
|
||||
Set a player's password.
|
||||
"""
|
||||
|
||||
key = "@userpassword"
|
||||
locks = "cmd:perm(newpassword) or perm(Wizards)"
|
||||
help_category = "Admin"
|
||||
|
||||
def func(self):
|
||||
"Implement the function."
|
||||
|
||||
caller = self.caller
|
||||
|
||||
if not self.rhs:
|
||||
self.msg("Usage: @userpassword <user obj> = <new password>")
|
||||
return
|
||||
|
||||
# the player search also matches 'me' etc.
|
||||
player = caller.search_player(self.lhs)
|
||||
if not player:
|
||||
return
|
||||
player.set_password(self.rhs)
|
||||
player.save()
|
||||
self.msg("%s - new password set to '%s'." % (player.name, self.rhs))
|
||||
if player.character != caller:
|
||||
player.msg("%s has changed your password to '%s'." % (caller.name,
|
||||
self.rhs))
|
||||
|
||||
|
||||
class CmdPerm(MuxCommand):
|
||||
"""
|
||||
set the permissions of a player/object
|
||||
|
||||
Usage:
|
||||
@perm[/switch] <object> [= <permission>[,<permission>,...]]
|
||||
@perm[/switch] *<player> [= <permission>[,<permission>,...]]
|
||||
|
||||
Switches:
|
||||
del : delete the given permission from <object> or <player>.
|
||||
player : set permission on a player (same as adding * to name)
|
||||
|
||||
This command sets/clears individual permission strings on an object
|
||||
or player. If no permission is given, list all permissions on <object>.
|
||||
"""
|
||||
key = "@perm"
|
||||
aliases = "@setperm"
|
||||
locks = "cmd:perm(perm) or perm(Immortals)"
|
||||
help_category = "Admin"
|
||||
|
||||
def func(self):
|
||||
"Implement function"
|
||||
|
||||
caller = self.caller
|
||||
switches = self.switches
|
||||
lhs, rhs = self.lhs, self.rhs
|
||||
|
||||
if not self.args:
|
||||
string = "Usage: @perm[/switch] object [ = permission, permission, ...]"
|
||||
caller.msg(string)
|
||||
return
|
||||
|
||||
playermode = 'player' in self.switches or lhs.startswith('*')
|
||||
lhs = lhs.lstrip("*")
|
||||
|
||||
if playermode:
|
||||
obj = caller.search_player(lhs)
|
||||
else:
|
||||
obj = caller.search(lhs, global_search=True)
|
||||
if not obj:
|
||||
return
|
||||
|
||||
if not rhs:
|
||||
if not obj.access(caller, 'examine'):
|
||||
caller.msg("You are not allowed to examine this object.")
|
||||
return
|
||||
|
||||
string = "Permissions on {w%s{n: " % obj.key
|
||||
if not obj.permissions.all():
|
||||
string += "<None>"
|
||||
else:
|
||||
string += ", ".join(obj.permissions.all())
|
||||
if (hasattr(obj, 'player') and
|
||||
hasattr(obj.player, 'is_superuser') and
|
||||
obj.player.is_superuser):
|
||||
string += "\n(... but this object is currently controlled by a SUPERUSER! "
|
||||
string += "All access checks are passed automatically.)"
|
||||
caller.msg(string)
|
||||
return
|
||||
|
||||
# we supplied an argument on the form obj = perm
|
||||
|
||||
if not obj.access(caller, 'control'):
|
||||
caller.msg("You are not allowed to edit this object's permissions.")
|
||||
return
|
||||
|
||||
cstring = ""
|
||||
tstring = ""
|
||||
if 'del' in switches:
|
||||
# delete the given permission(s) from object.
|
||||
for perm in self.rhslist:
|
||||
obj.permissions.remove(perm)
|
||||
if obj.permissions.get(perm):
|
||||
cstring += "\nPermissions %s could not be removed from %s." % (perm, obj.name)
|
||||
else:
|
||||
cstring += "\nPermission %s removed from %s (if they existed)." % (perm, obj.name)
|
||||
tstring += "\n%s revokes the permission(s) %s from you." % (caller.name, perm)
|
||||
else:
|
||||
# add a new permission
|
||||
permissions = obj.permissions.all()
|
||||
|
||||
for perm in self.rhslist:
|
||||
|
||||
# don't allow to set a permission higher in the hierarchy than
|
||||
# the one the caller has (to prevent self-escalation)
|
||||
if (perm.lower() in PERMISSION_HIERARCHY and not
|
||||
obj.locks.check_lockstring(caller, "dummy:perm(%s)" % perm)):
|
||||
caller.msg("You cannot assign a permission higher than the one you have yourself.")
|
||||
return
|
||||
|
||||
if perm in permissions:
|
||||
cstring += "\nPermission '%s' is already defined on %s." % (rhs, obj.name)
|
||||
else:
|
||||
obj.permissions.add(perm)
|
||||
plystring = "the Player" if playermode else "the Object/Character"
|
||||
cstring += "\nPermission '%s' given to %s (%s)." % (rhs, obj.name, plystring)
|
||||
tstring += "\n%s gives you (%s, %s) the permission '%s'." % (caller.name, obj.name, plystring, rhs)
|
||||
caller.msg(cstring.strip())
|
||||
if tstring:
|
||||
obj.msg(tstring.strip())
|
||||
|
||||
class CmdWall(MuxCommand):
|
||||
"""
|
||||
make an announcement to all
|
||||
|
||||
Usage:
|
||||
@wall <message>
|
||||
|
||||
Announces a message to all connected players.
|
||||
"""
|
||||
key = "@wall"
|
||||
locks = "cmd:perm(wall) or perm(Wizards)"
|
||||
help_category = "Admin"
|
||||
|
||||
def func(self):
|
||||
"Implements command"
|
||||
if not self.args:
|
||||
self.caller.msg("Usage: @wall <message>")
|
||||
return
|
||||
message = "%s shouts \"%s\"" % (self.caller.name, self.args)
|
||||
self.msg("Announcing to all connected players ...")
|
||||
SESSIONS.announce_all(message)
|
||||
843
lib/commands/default/batchprocess.py
Normal file
843
lib/commands/default/batchprocess.py
Normal file
|
|
@ -0,0 +1,843 @@
|
|||
"""
|
||||
Batch processors
|
||||
|
||||
These commands implements the 'batch-command' and 'batch-code'
|
||||
processors, using the functionality in src.utils.batchprocessors.
|
||||
They allow for offline world-building.
|
||||
|
||||
Batch-command is the simpler system. This reads a file (*.ev)
|
||||
containing a list of in-game commands and executes them in sequence as
|
||||
if they had been entered in the game (including permission checks
|
||||
etc).
|
||||
|
||||
Example batch-command file: game/gamesrc/commands/examples/batch_cmds.ev
|
||||
|
||||
Batch-code is a full-fledged python code interpreter that reads blocks
|
||||
of python code (*.py) and executes them in sequence. This allows for
|
||||
much more power than Batch-command, but requires knowing Python and
|
||||
the Evennia API. It is also a severe security risk and should
|
||||
therefore always be limited to superusers only.
|
||||
|
||||
Example batch-code file: game/gamesrc/commands/examples/batch_code.py
|
||||
|
||||
"""
|
||||
from traceback import format_exc
|
||||
from django.conf import settings
|
||||
from src.utils.batchprocessors import BATCHCMD, BATCHCODE
|
||||
from src.commands.cmdset import CmdSet
|
||||
from src.commands.default.muxcommand import MuxCommand
|
||||
from src.utils import utils
|
||||
|
||||
# limit symbols for API inclusion
|
||||
__all__ = ("CmdBatchCommands", "CmdBatchCode")
|
||||
|
||||
_HEADER_WIDTH = 70
|
||||
_UTF8_ERROR = \
|
||||
"""
|
||||
{rDecode error in '%s'.{n
|
||||
|
||||
This file contains non-ascii character(s). This is common if you
|
||||
wrote some input in a language that has more letters and special
|
||||
symbols than English; such as accents or umlauts. This is usually
|
||||
fine and fully supported! But for Evennia to know how to decode such
|
||||
characters in a universal way, the batchfile must be saved with the
|
||||
international 'UTF-8' encoding. This file is not.
|
||||
|
||||
Please re-save the batchfile with the UTF-8 encoding (refer to the
|
||||
documentation of your text editor on how to do this, or switch to a
|
||||
better featured one) and try again.
|
||||
|
||||
Error reported was: '%s'
|
||||
"""
|
||||
|
||||
_PROCPOOL_BATCHCMD_SOURCE = """
|
||||
from src.commands.default.batchprocess import batch_cmd_exec, step_pointer, BatchSafeCmdSet
|
||||
caller.ndb.batch_stack = commands
|
||||
caller.ndb.batch_stackptr = 0
|
||||
caller.ndb.batch_batchmode = "batch_commands"
|
||||
caller.cmdset.add(BatchSafeCmdSet)
|
||||
for inum in range(len(commands)):
|
||||
print "command:", inum
|
||||
caller.cmdset.add(BatchSafeCmdSet)
|
||||
if not batch_cmd_exec(caller):
|
||||
break
|
||||
step_pointer(caller, 1)
|
||||
print "leaving run ..."
|
||||
"""
|
||||
_PROCPOOL_BATCHCODE_SOURCE = """
|
||||
from src.commands.default.batchprocess import batch_code_exec, step_pointer, BatchSafeCmdSet
|
||||
caller.ndb.batch_stack = codes
|
||||
caller.ndb.batch_stackptr = 0
|
||||
caller.ndb.batch_batchmode = "batch_code"
|
||||
caller.cmdset.add(BatchSafeCmdSet)
|
||||
for inum in range(len(codes)):
|
||||
print "code:", inum
|
||||
caller.cmdset.add(BatchSafeCmdSet)
|
||||
if not batch_code_exec(caller):
|
||||
break
|
||||
step_pointer(caller, 1)
|
||||
print "leaving run ..."
|
||||
"""
|
||||
|
||||
|
||||
#------------------------------------------------------------
|
||||
# Helper functions
|
||||
#------------------------------------------------------------
|
||||
|
||||
def format_header(caller, entry):
|
||||
"""
|
||||
Formats a header
|
||||
"""
|
||||
width = _HEADER_WIDTH - 10
|
||||
entry = entry.strip()
|
||||
header = utils.crop(entry, width=width)
|
||||
ptr = caller.ndb.batch_stackptr + 1
|
||||
stacklen = len(caller.ndb.batch_stack)
|
||||
header = "{w%02i/%02i{G: %s{n" % (ptr, stacklen, header)
|
||||
# add extra space to the side for padding.
|
||||
header = "%s%s" % (header, " " * (width - len(header)))
|
||||
header = header.replace('\n', '\\n')
|
||||
|
||||
return header
|
||||
|
||||
|
||||
def format_code(entry):
|
||||
"""
|
||||
Formats the viewing of code and errors
|
||||
"""
|
||||
code = ""
|
||||
for line in entry.split('\n'):
|
||||
code += "\n{G>>>{n %s" % line
|
||||
return code.strip()
|
||||
|
||||
|
||||
def batch_cmd_exec(caller):
|
||||
"""
|
||||
Helper function for executing a single batch-command entry
|
||||
"""
|
||||
ptr = caller.ndb.batch_stackptr
|
||||
stack = caller.ndb.batch_stack
|
||||
command = stack[ptr]
|
||||
caller.msg(format_header(caller, command))
|
||||
try:
|
||||
caller.execute_cmd(command)
|
||||
except Exception:
|
||||
caller.msg(format_code(format_exc()))
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def batch_code_exec(caller):
|
||||
"""
|
||||
Helper function for executing a single batch-code entry
|
||||
"""
|
||||
ptr = caller.ndb.batch_stackptr
|
||||
stack = caller.ndb.batch_stack
|
||||
debug = caller.ndb.batch_debug
|
||||
code = stack[ptr]
|
||||
|
||||
caller.msg(format_header(caller, code))
|
||||
err = BATCHCODE.code_exec(code,
|
||||
extra_environ={"caller": caller}, debug=debug)
|
||||
if err:
|
||||
caller.msg(format_code(err))
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def step_pointer(caller, step=1):
|
||||
"""
|
||||
Step in stack, returning the item located.
|
||||
|
||||
stackptr - current position in stack
|
||||
stack - the stack of units
|
||||
step - how many steps to move from stackptr
|
||||
"""
|
||||
ptr = caller.ndb.batch_stackptr
|
||||
stack = caller.ndb.batch_stack
|
||||
nstack = len(stack)
|
||||
if ptr + step <= 0:
|
||||
caller.msg("{RBeginning of batch file.")
|
||||
if ptr + step >= nstack:
|
||||
caller.msg("{REnd of batch file.")
|
||||
caller.ndb.batch_stackptr = max(0, min(nstack - 1, ptr + step))
|
||||
|
||||
|
||||
def show_curr(caller, showall=False):
|
||||
"""
|
||||
Show the current position in stack
|
||||
"""
|
||||
stackptr = caller.ndb.batch_stackptr
|
||||
stack = caller.ndb.batch_stack
|
||||
|
||||
if stackptr >= len(stack):
|
||||
caller.ndb.batch_stackptr = len(stack) - 1
|
||||
show_curr(caller, showall)
|
||||
return
|
||||
|
||||
entry = stack[stackptr]
|
||||
|
||||
string = format_header(caller, entry)
|
||||
codeall = entry.strip()
|
||||
string += "{G(hh for help)"
|
||||
if showall:
|
||||
for line in codeall.split('\n'):
|
||||
string += "\n{G|{n %s" % line
|
||||
caller.msg(string)
|
||||
|
||||
|
||||
def purge_processor(caller):
|
||||
"""
|
||||
This purges all effects running
|
||||
on the caller.
|
||||
"""
|
||||
try:
|
||||
del caller.ndb.batch_stack
|
||||
del caller.ndb.batch_stackptr
|
||||
del caller.ndb.batch_pythonpath
|
||||
del caller.ndb.batch_batchmode
|
||||
except:
|
||||
pass
|
||||
# clear everything but the default cmdset.
|
||||
caller.cmdset.delete(BatchSafeCmdSet)
|
||||
caller.cmdset.clear()
|
||||
caller.scripts.validate() # this will purge interactive mode
|
||||
|
||||
#------------------------------------------------------------
|
||||
# main access commands
|
||||
#------------------------------------------------------------
|
||||
|
||||
|
||||
class CmdBatchCommands(MuxCommand):
|
||||
"""
|
||||
build from batch-command file
|
||||
|
||||
Usage:
|
||||
@batchcommands[/interactive] <python.path.to.file>
|
||||
|
||||
Switch:
|
||||
interactive - this mode will offer more control when
|
||||
executing the batch file, like stepping,
|
||||
skipping, reloading etc.
|
||||
|
||||
Runs batches of commands from a batch-cmd text file (*.ev).
|
||||
|
||||
"""
|
||||
key = "@batchcommands"
|
||||
aliases = ["@batchcommand", "@batchcmd"]
|
||||
locks = "cmd:perm(batchcommands) or superuser()"
|
||||
help_category = "Building"
|
||||
|
||||
def func(self):
|
||||
"Starts the processor."
|
||||
|
||||
caller = self.caller
|
||||
|
||||
args = self.args
|
||||
if not args:
|
||||
caller.msg("Usage: @batchcommands[/interactive] <path.to.file>")
|
||||
return
|
||||
python_path = self.args
|
||||
|
||||
#parse indata file
|
||||
|
||||
try:
|
||||
commands = BATCHCMD.parse_file(python_path)
|
||||
except UnicodeDecodeError, err:
|
||||
caller.msg(_UTF8_ERROR % (python_path, err))
|
||||
return
|
||||
except IOError:
|
||||
string = "'%s' not found.\nYou have to supply the python path "
|
||||
string += "of the file relative to \none of your batch-file directories (%s)."
|
||||
caller.msg(string % (python_path, ", ".join(settings.BASE_BATCHPROCESS_PATHS)))
|
||||
return
|
||||
if not commands:
|
||||
caller.msg("File %s seems empty of valid commands." % python_path)
|
||||
return
|
||||
|
||||
switches = self.switches
|
||||
|
||||
# Store work data in cache
|
||||
caller.ndb.batch_stack = commands
|
||||
caller.ndb.batch_stackptr = 0
|
||||
caller.ndb.batch_pythonpath = python_path
|
||||
caller.ndb.batch_batchmode = "batch_commands"
|
||||
caller.cmdset.add(BatchSafeCmdSet)
|
||||
|
||||
if 'inter' in switches or 'interactive' in switches:
|
||||
# Allow more control over how batch file is executed
|
||||
|
||||
# Set interactive state directly
|
||||
caller.cmdset.add(BatchInteractiveCmdSet)
|
||||
|
||||
caller.msg("\nBatch-command processor - Interactive mode for %s ..." % python_path)
|
||||
show_curr(caller)
|
||||
else:
|
||||
caller.msg("Running Batch-command processor - Automatic mode for %s (this might take some time) ..." % python_path)
|
||||
|
||||
procpool = False
|
||||
if "PythonProcPool" in utils.server_services():
|
||||
if utils.uses_database("sqlite3"):
|
||||
caller.msg("Batchprocessor disabled ProcPool under SQLite3.")
|
||||
else:
|
||||
procpool = True
|
||||
|
||||
if procpool:
|
||||
# run in parallel process
|
||||
def callback(r):
|
||||
caller.msg(" {GBatchfile '%s' applied." % python_path)
|
||||
purge_processor(caller)
|
||||
|
||||
def errback(e):
|
||||
caller.msg(" {RError from processor: '%s'" % e)
|
||||
purge_processor(caller)
|
||||
|
||||
utils.run_async(_PROCPOOL_BATCHCMD_SOURCE,
|
||||
commands=commands,
|
||||
caller=caller,
|
||||
at_return=callback,
|
||||
at_err=errback)
|
||||
else:
|
||||
# run in-process (might block)
|
||||
for inum in range(len(commands)):
|
||||
# loop through the batch file
|
||||
if not batch_cmd_exec(caller):
|
||||
return
|
||||
step_pointer(caller, 1)
|
||||
# clean out the safety cmdset and clean out all other
|
||||
# temporary attrs.
|
||||
string = " Batchfile '%s' applied." % python_path
|
||||
caller.msg("{G%s" % string)
|
||||
purge_processor(caller)
|
||||
|
||||
|
||||
class CmdBatchCode(MuxCommand):
|
||||
"""
|
||||
build from batch-code file
|
||||
|
||||
Usage:
|
||||
@batchcode[/interactive] <python path to file>
|
||||
|
||||
Switch:
|
||||
interactive - this mode will offer more control when
|
||||
executing the batch file, like stepping,
|
||||
skipping, reloading etc.
|
||||
debug - auto-delete all objects that has been marked as
|
||||
deletable in the script file (see example files for
|
||||
syntax). This is useful so as to to not leave multiple
|
||||
object copies behind when testing out the script.
|
||||
|
||||
Runs batches of commands from a batch-code text file (*.py).
|
||||
|
||||
"""
|
||||
key = "@batchcode"
|
||||
aliases = ["@batchcodes"]
|
||||
locks = "cmd:superuser()"
|
||||
help_category = "Building"
|
||||
|
||||
def func(self):
|
||||
"Starts the processor."
|
||||
|
||||
caller = self.caller
|
||||
|
||||
args = self.args
|
||||
if not args:
|
||||
caller.msg("Usage: @batchcode[/interactive/debug] <path.to.file>")
|
||||
return
|
||||
python_path = self.args
|
||||
debug = 'debug' in self.switches
|
||||
|
||||
#parse indata file
|
||||
try:
|
||||
codes = BATCHCODE.parse_file(python_path, debug=debug)
|
||||
except UnicodeDecodeError, err:
|
||||
caller.msg(_UTF8_ERROR % (python_path, err))
|
||||
return
|
||||
except IOError:
|
||||
string = "'%s' not found.\nYou have to supply the python path "
|
||||
string += "of the file relative to \nyour batch-file directories (%s)."
|
||||
caller.msg(string % (python_path, ", ".join(settings.BASE_BATCHPROCESS_PATHS)))
|
||||
return
|
||||
if not codes:
|
||||
caller.msg("File %s seems empty of functional code." % python_path)
|
||||
return
|
||||
|
||||
switches = self.switches
|
||||
|
||||
# Store work data in cache
|
||||
caller.ndb.batch_stack = codes
|
||||
caller.ndb.batch_stackptr = 0
|
||||
caller.ndb.batch_pythonpath = python_path
|
||||
caller.ndb.batch_batchmode = "batch_code"
|
||||
caller.ndb.batch_debug = debug
|
||||
caller.cmdset.add(BatchSafeCmdSet)
|
||||
|
||||
if 'inter' in switches or 'interactive'in switches:
|
||||
# Allow more control over how batch file is executed
|
||||
|
||||
# Set interactive state directly
|
||||
caller.cmdset.add(BatchInteractiveCmdSet)
|
||||
|
||||
caller.msg("\nBatch-code processor - Interactive mode for %s ..." % python_path)
|
||||
show_curr(caller)
|
||||
else:
|
||||
caller.msg("Running Batch-code processor - Automatic mode for %s ..." % python_path)
|
||||
|
||||
procpool = False
|
||||
if "PythonProcPool" in utils.server_services():
|
||||
if utils.uses_database("sqlite3"):
|
||||
caller.msg("Batchprocessor disabled ProcPool under SQLite3.")
|
||||
else:
|
||||
procpool = True
|
||||
if procpool:
|
||||
# run in parallel process
|
||||
def callback(r):
|
||||
caller.msg(" {GBatchfile '%s' applied." % python_path)
|
||||
purge_processor(caller)
|
||||
|
||||
def errback(e):
|
||||
caller.msg(" {RError from processor: '%s'" % e)
|
||||
purge_processor(caller)
|
||||
utils.run_async(_PROCPOOL_BATCHCODE_SOURCE,
|
||||
codes=codes,
|
||||
caller=caller,
|
||||
at_return=callback,
|
||||
at_err=errback)
|
||||
else:
|
||||
# un in-process (will block)
|
||||
for inum in range(len(codes)):
|
||||
# loop through the batch file
|
||||
if not batch_code_exec(caller):
|
||||
return
|
||||
step_pointer(caller, 1)
|
||||
# clean out the safety cmdset and clean out all other
|
||||
# temporary attrs.
|
||||
string = " Batchfile '%s' applied." % python_path
|
||||
caller.msg("{G%s" % string)
|
||||
purge_processor(caller)
|
||||
|
||||
|
||||
#------------------------------------------------------------
|
||||
# State-commands for the interactive batch processor modes
|
||||
# (these are the same for both processors)
|
||||
#------------------------------------------------------------
|
||||
|
||||
class CmdStateAbort(MuxCommand):
|
||||
"""
|
||||
@abort
|
||||
|
||||
This is a safety feature. It force-ejects us out of the processor and to
|
||||
the default cmdset, regardless of what current cmdset the processor might
|
||||
have put us in (e.g. when testing buggy scripts etc).
|
||||
"""
|
||||
key = "@abort"
|
||||
help_category = "BatchProcess"
|
||||
locks = "cmd:perm(batchcommands)"
|
||||
|
||||
def func(self):
|
||||
"Exit back to default."
|
||||
purge_processor(self.caller)
|
||||
self.caller.msg("Exited processor and reset out active cmdset back to the default one.")
|
||||
|
||||
|
||||
class CmdStateLL(MuxCommand):
|
||||
"""
|
||||
ll
|
||||
|
||||
Look at the full source for the current
|
||||
command definition.
|
||||
"""
|
||||
key = "ll"
|
||||
help_category = "BatchProcess"
|
||||
locks = "cmd:perm(batchcommands)"
|
||||
|
||||
def func(self):
|
||||
show_curr(self.caller, showall=True)
|
||||
|
||||
class CmdStatePP(MuxCommand):
|
||||
"""
|
||||
pp
|
||||
|
||||
Process the currently shown command definition.
|
||||
"""
|
||||
key = "pp"
|
||||
help_category = "BatchProcess"
|
||||
locks = "cmd:perm(batchcommands)"
|
||||
|
||||
def func(self):
|
||||
"""
|
||||
This checks which type of processor we are running.
|
||||
"""
|
||||
caller = self.caller
|
||||
if caller.ndb.batch_batchmode == "batch_code":
|
||||
batch_code_exec(caller)
|
||||
else:
|
||||
batch_cmd_exec(caller)
|
||||
|
||||
|
||||
class CmdStateRR(MuxCommand):
|
||||
"""
|
||||
rr
|
||||
|
||||
Reload the batch file, keeping the current
|
||||
position in it.
|
||||
"""
|
||||
key = "rr"
|
||||
help_category = "BatchProcess"
|
||||
locks = "cmd:perm(batchcommands)"
|
||||
|
||||
def func(self):
|
||||
caller = self.caller
|
||||
if caller.ndb.batch_batchmode == "batch_code":
|
||||
new_data = BATCHCODE.parse_file(caller.ndb.batch_pythonpath)
|
||||
else:
|
||||
new_data = BATCHCMD.parse_file(caller.ndb.batch_pythonpath)
|
||||
caller.ndb.batch_stack = new_data
|
||||
caller.msg(format_code("File reloaded. Staying on same command."))
|
||||
show_curr(caller)
|
||||
|
||||
|
||||
class CmdStateRRR(MuxCommand):
|
||||
"""
|
||||
rrr
|
||||
|
||||
Reload the batch file, starting over
|
||||
from the beginning.
|
||||
"""
|
||||
key = "rrr"
|
||||
help_category = "BatchProcess"
|
||||
locks = "cmd:perm(batchcommands)"
|
||||
|
||||
def func(self):
|
||||
caller = self.caller
|
||||
if caller.ndb.batch_batchmode == "batch_code":
|
||||
BATCHCODE.parse_file(caller.ndb.batch_pythonpath)
|
||||
else:
|
||||
BATCHCMD.parse_file(caller.ndb.batch_pythonpath)
|
||||
caller.ndb.batch_stackptr = 0
|
||||
caller.msg(format_code("File reloaded. Restarting from top."))
|
||||
show_curr(caller)
|
||||
|
||||
|
||||
class CmdStateNN(MuxCommand):
|
||||
"""
|
||||
nn
|
||||
|
||||
Go to next command. No commands are executed.
|
||||
"""
|
||||
key = "nn"
|
||||
help_category = "BatchProcess"
|
||||
locks = "cmd:perm(batchcommands)"
|
||||
|
||||
def func(self):
|
||||
caller = self.caller
|
||||
arg = self.args
|
||||
if arg and arg.isdigit():
|
||||
step = int(self.args)
|
||||
else:
|
||||
step = 1
|
||||
step_pointer(caller, step)
|
||||
show_curr(caller)
|
||||
|
||||
|
||||
class CmdStateNL(MuxCommand):
|
||||
"""
|
||||
nl
|
||||
|
||||
Go to next command, viewing its full source.
|
||||
No commands are executed.
|
||||
"""
|
||||
key = "nl"
|
||||
help_category = "BatchProcess"
|
||||
locks = "cmd:perm(batchcommands)"
|
||||
|
||||
def func(self):
|
||||
caller = self.caller
|
||||
arg = self.args
|
||||
if arg and arg.isdigit():
|
||||
step = int(self.args)
|
||||
else:
|
||||
step = 1
|
||||
step_pointer(caller, step)
|
||||
show_curr(caller, showall=True)
|
||||
|
||||
|
||||
class CmdStateBB(MuxCommand):
|
||||
"""
|
||||
bb
|
||||
|
||||
Backwards to previous command. No commands
|
||||
are executed.
|
||||
"""
|
||||
key = "bb"
|
||||
help_category = "BatchProcess"
|
||||
locks = "cmd:perm(batchcommands)"
|
||||
|
||||
def func(self):
|
||||
caller = self.caller
|
||||
arg = self.args
|
||||
if arg and arg.isdigit():
|
||||
step = -int(self.args)
|
||||
else:
|
||||
step = -1
|
||||
step_pointer(caller, step)
|
||||
show_curr(caller)
|
||||
|
||||
|
||||
class CmdStateBL(MuxCommand):
|
||||
"""
|
||||
bl
|
||||
|
||||
Backwards to previous command, viewing its full
|
||||
source. No commands are executed.
|
||||
"""
|
||||
key = "bl"
|
||||
help_category = "BatchProcess"
|
||||
locks = "cmd:perm(batchcommands)"
|
||||
|
||||
def func(self):
|
||||
caller = self.caller
|
||||
arg = self.args
|
||||
if arg and arg.isdigit():
|
||||
step = -int(self.args)
|
||||
else:
|
||||
step = -1
|
||||
step_pointer(caller, step)
|
||||
show_curr(caller, showall=True)
|
||||
|
||||
|
||||
class CmdStateSS(MuxCommand):
|
||||
"""
|
||||
ss [steps]
|
||||
|
||||
Process current command, then step to the next
|
||||
one. If steps is given,
|
||||
process this many commands.
|
||||
"""
|
||||
key = "ss"
|
||||
help_category = "BatchProcess"
|
||||
locks = "cmd:perm(batchcommands)"
|
||||
|
||||
def func(self):
|
||||
caller = self.caller
|
||||
arg = self.args
|
||||
if arg and arg.isdigit():
|
||||
step = int(self.args)
|
||||
else:
|
||||
step = 1
|
||||
|
||||
for istep in range(step):
|
||||
if caller.ndb.batch_batchmode == "batch_code":
|
||||
batch_code_exec(caller)
|
||||
else:
|
||||
batch_cmd_exec(caller)
|
||||
step_pointer(caller, 1)
|
||||
show_curr(caller)
|
||||
|
||||
|
||||
class CmdStateSL(MuxCommand):
|
||||
"""
|
||||
sl [steps]
|
||||
|
||||
Process current command, then step to the next
|
||||
one, viewing its full source. If steps is given,
|
||||
process this many commands.
|
||||
"""
|
||||
key = "sl"
|
||||
help_category = "BatchProcess"
|
||||
locks = "cmd:perm(batchcommands)"
|
||||
|
||||
def func(self):
|
||||
caller = self.caller
|
||||
arg = self.args
|
||||
if arg and arg.isdigit():
|
||||
step = int(self.args)
|
||||
else:
|
||||
step = 1
|
||||
|
||||
for istep in range(step):
|
||||
if caller.ndb.batch_batchmode == "batch_code":
|
||||
batch_code_exec(caller)
|
||||
else:
|
||||
batch_cmd_exec(caller)
|
||||
step_pointer(caller, 1)
|
||||
show_curr(caller)
|
||||
|
||||
|
||||
class CmdStateCC(MuxCommand):
|
||||
"""
|
||||
cc
|
||||
|
||||
Continue to process all remaining
|
||||
commands.
|
||||
"""
|
||||
key = "cc"
|
||||
help_category = "BatchProcess"
|
||||
locks = "cmd:perm(batchcommands)"
|
||||
|
||||
def func(self):
|
||||
caller = self.caller
|
||||
nstack = len(caller.ndb.batch_stack)
|
||||
ptr = caller.ndb.batch_stackptr
|
||||
step = nstack - ptr
|
||||
|
||||
for istep in range(step):
|
||||
if caller.ndb.batch_batchmode == "batch_code":
|
||||
batch_code_exec(caller)
|
||||
else:
|
||||
batch_cmd_exec(caller)
|
||||
step_pointer(caller, 1)
|
||||
show_curr(caller)
|
||||
|
||||
del caller.ndb.batch_stack
|
||||
del caller.ndb.batch_stackptr
|
||||
del caller.ndb.batch_pythonpath
|
||||
del caller.ndb.batch_batchmode
|
||||
caller.msg(format_code("Finished processing batch file."))
|
||||
|
||||
|
||||
class CmdStateJJ(MuxCommand):
|
||||
"""
|
||||
j <command number>
|
||||
|
||||
Jump to specific command number
|
||||
"""
|
||||
key = "j"
|
||||
help_category = "BatchProcess"
|
||||
locks = "cmd:perm(batchcommands)"
|
||||
|
||||
def func(self):
|
||||
caller = self.caller
|
||||
arg = self.args
|
||||
if arg and arg.isdigit():
|
||||
number = int(self.args) - 1
|
||||
else:
|
||||
caller.msg(format_code("You must give a number index."))
|
||||
return
|
||||
ptr = caller.ndb.batch_stackptr
|
||||
step = number - ptr
|
||||
step_pointer(caller, step)
|
||||
show_curr(caller)
|
||||
|
||||
|
||||
class CmdStateJL(MuxCommand):
|
||||
"""
|
||||
jl <command number>
|
||||
|
||||
Jump to specific command number and view its full source.
|
||||
"""
|
||||
key = "jl"
|
||||
help_category = "BatchProcess"
|
||||
locks = "cmd:perm(batchcommands)"
|
||||
|
||||
def func(self):
|
||||
caller = self.caller
|
||||
arg = self.args
|
||||
if arg and arg.isdigit():
|
||||
number = int(self.args) - 1
|
||||
else:
|
||||
caller.msg(format_code("You must give a number index."))
|
||||
return
|
||||
ptr = caller.ndb.batch_stackptr
|
||||
step = number - ptr
|
||||
step_pointer(caller, step)
|
||||
show_curr(caller, showall=True)
|
||||
|
||||
|
||||
class CmdStateQQ(MuxCommand):
|
||||
"""
|
||||
qq
|
||||
|
||||
Quit the batchprocessor.
|
||||
"""
|
||||
key = "qq"
|
||||
help_category = "BatchProcess"
|
||||
locks = "cmd:perm(batchcommands)"
|
||||
|
||||
def func(self):
|
||||
purge_processor(self.caller)
|
||||
self.caller.msg("Aborted interactive batch mode.")
|
||||
|
||||
|
||||
class CmdStateHH(MuxCommand):
|
||||
"Help command"
|
||||
|
||||
key = "hh"
|
||||
help_category = "BatchProcess"
|
||||
locks = "cmd:perm(batchcommands)"
|
||||
|
||||
def func(self):
|
||||
string = """
|
||||
Interactive batch processing commands:
|
||||
|
||||
nn [steps] - next command (no processing)
|
||||
nl [steps] - next & look
|
||||
bb [steps] - back to previous command (no processing)
|
||||
bl [steps] - back & look
|
||||
jj <N> - jump to command nr N (no processing)
|
||||
jl <N> - jump & look
|
||||
pp - process currently shown command (no step)
|
||||
ss [steps] - process & step
|
||||
sl [steps] - process & step & look
|
||||
ll - look at full definition of current command
|
||||
rr - reload batch file (stay on current)
|
||||
rrr - reload batch file (start from first)
|
||||
hh - this help list
|
||||
|
||||
cc - continue processing to end, then quit.
|
||||
qq - quit (abort all remaining commands)
|
||||
|
||||
@abort - this is a safety command that always is available
|
||||
regardless of what cmdsets gets added to us during
|
||||
batch-command processing. It immediately shuts down
|
||||
the processor and returns us to the default cmdset.
|
||||
"""
|
||||
self.caller.msg(string)
|
||||
|
||||
|
||||
#------------------------------------------------------------
|
||||
#
|
||||
# Defining the cmdsets for the interactive batchprocessor
|
||||
# mode (same for both processors)
|
||||
#
|
||||
#------------------------------------------------------------
|
||||
|
||||
class BatchSafeCmdSet(CmdSet):
|
||||
"""
|
||||
The base cmdset for the batch processor.
|
||||
This sets a 'safe' @abort command that will
|
||||
always be available to get out of everything.
|
||||
"""
|
||||
key = "Batch_default"
|
||||
priority = 150 # override other cmdsets.
|
||||
|
||||
def at_cmdset_creation(self):
|
||||
"Init the cmdset"
|
||||
self.add(CmdStateAbort())
|
||||
|
||||
|
||||
class BatchInteractiveCmdSet(CmdSet):
|
||||
"""
|
||||
The cmdset for the interactive batch processor mode.
|
||||
"""
|
||||
key = "Batch_interactive"
|
||||
priority = 104
|
||||
|
||||
def at_cmdset_creation(self):
|
||||
"init the cmdset"
|
||||
self.add(CmdStateAbort())
|
||||
self.add(CmdStateLL())
|
||||
self.add(CmdStatePP())
|
||||
self.add(CmdStateRR())
|
||||
self.add(CmdStateRRR())
|
||||
self.add(CmdStateNN())
|
||||
self.add(CmdStateNL())
|
||||
self.add(CmdStateBB())
|
||||
self.add(CmdStateBL())
|
||||
self.add(CmdStateSS())
|
||||
self.add(CmdStateSL())
|
||||
self.add(CmdStateCC())
|
||||
self.add(CmdStateJJ())
|
||||
self.add(CmdStateJL())
|
||||
self.add(CmdStateQQ())
|
||||
self.add(CmdStateHH())
|
||||
2374
lib/commands/default/building.py
Normal file
2374
lib/commands/default/building.py
Normal file
File diff suppressed because it is too large
Load diff
87
lib/commands/default/cmdset_character.py
Normal file
87
lib/commands/default/cmdset_character.py
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
"""
|
||||
This module ties together all the commands default Character objects have
|
||||
available (i.e. IC commands). Note that some commands, such as
|
||||
communication-commands are instead put on the player level, in the
|
||||
Player cmdset. Player commands remain available also to Characters.
|
||||
"""
|
||||
from src.commands.cmdset import CmdSet
|
||||
from src.commands.default import general, help, admin, system
|
||||
from src.commands.default import building
|
||||
from src.commands.default import batchprocess
|
||||
|
||||
|
||||
class CharacterCmdSet(CmdSet):
|
||||
"""
|
||||
Implements the default command set.
|
||||
"""
|
||||
key = "DefaultCharacter"
|
||||
priority = 0
|
||||
|
||||
def at_cmdset_creation(self):
|
||||
"Populates the cmdset"
|
||||
|
||||
# The general commands
|
||||
self.add(general.CmdLook())
|
||||
self.add(general.CmdHome())
|
||||
self.add(general.CmdInventory())
|
||||
self.add(general.CmdPose())
|
||||
self.add(general.CmdNick())
|
||||
self.add(general.CmdGet())
|
||||
self.add(general.CmdDrop())
|
||||
self.add(general.CmdGive())
|
||||
self.add(general.CmdSay())
|
||||
self.add(general.CmdAccess())
|
||||
|
||||
# The help system
|
||||
self.add(help.CmdHelp())
|
||||
self.add(help.CmdSetHelp())
|
||||
|
||||
# System commands
|
||||
self.add(system.CmdPy())
|
||||
self.add(system.CmdScripts())
|
||||
self.add(system.CmdObjects())
|
||||
self.add(system.CmdPlayers())
|
||||
self.add(system.CmdService())
|
||||
self.add(system.CmdAbout())
|
||||
self.add(system.CmdTime())
|
||||
self.add(system.CmdServerLoad())
|
||||
#self.add(system.CmdPs())
|
||||
|
||||
# Admin commands
|
||||
self.add(admin.CmdBoot())
|
||||
self.add(admin.CmdBan())
|
||||
self.add(admin.CmdUnban())
|
||||
self.add(admin.CmdEmit())
|
||||
self.add(admin.CmdPerm())
|
||||
self.add(admin.CmdWall())
|
||||
|
||||
# Building and world manipulation
|
||||
self.add(building.CmdTeleport())
|
||||
self.add(building.CmdSetObjAlias())
|
||||
self.add(building.CmdListCmdSets())
|
||||
self.add(building.CmdWipe())
|
||||
self.add(building.CmdSetAttribute())
|
||||
self.add(building.CmdName())
|
||||
self.add(building.CmdDesc())
|
||||
self.add(building.CmdCpAttr())
|
||||
self.add(building.CmdMvAttr())
|
||||
self.add(building.CmdCopy())
|
||||
self.add(building.CmdFind())
|
||||
self.add(building.CmdOpen())
|
||||
self.add(building.CmdLink())
|
||||
self.add(building.CmdUnLink())
|
||||
self.add(building.CmdCreate())
|
||||
self.add(building.CmdDig())
|
||||
self.add(building.CmdTunnel())
|
||||
self.add(building.CmdDestroy())
|
||||
self.add(building.CmdExamine())
|
||||
self.add(building.CmdTypeclass())
|
||||
self.add(building.CmdLock())
|
||||
self.add(building.CmdScript())
|
||||
self.add(building.CmdSetHome())
|
||||
self.add(building.CmdTag())
|
||||
self.add(building.CmdSpawn())
|
||||
|
||||
# Batchprocessor commands
|
||||
self.add(batchprocess.CmdBatchCommands())
|
||||
self.add(batchprocess.CmdBatchCode())
|
||||
74
lib/commands/default/cmdset_player.py
Normal file
74
lib/commands/default/cmdset_player.py
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
"""
|
||||
|
||||
This is the cmdset for Player (OOC) commands. These are
|
||||
stored on the Player object and should thus be able to handle getting
|
||||
a Player object as caller rather than a Character.
|
||||
|
||||
Note - in order for session-rerouting (in MULTISESSION_MODE=2) to
|
||||
function, all commands in this cmdset should use the self.msg()
|
||||
command method rather than caller.msg().
|
||||
"""
|
||||
|
||||
from src.commands.cmdset import CmdSet
|
||||
from src.commands.default import help, comms, admin, system
|
||||
from src.commands.default import building, player
|
||||
|
||||
|
||||
class PlayerCmdSet(CmdSet):
|
||||
"""
|
||||
Implements the player command set.
|
||||
"""
|
||||
|
||||
key = "DefaultPlayer"
|
||||
priority = -10
|
||||
|
||||
def at_cmdset_creation(self):
|
||||
"Populates the cmdset"
|
||||
|
||||
# Player-specific commands
|
||||
self.add(player.CmdOOCLook())
|
||||
self.add(player.CmdIC())
|
||||
self.add(player.CmdOOC())
|
||||
self.add(player.CmdCharCreate())
|
||||
#self.add(player.CmdSessions())
|
||||
self.add(player.CmdWho())
|
||||
self.add(player.CmdEncoding())
|
||||
self.add(player.CmdQuit())
|
||||
self.add(player.CmdPassword())
|
||||
self.add(player.CmdColorTest())
|
||||
self.add(player.CmdQuell())
|
||||
|
||||
# testing
|
||||
self.add(building.CmdExamine())
|
||||
|
||||
# Help command
|
||||
self.add(help.CmdHelp())
|
||||
|
||||
# system commands
|
||||
self.add(system.CmdReload())
|
||||
self.add(system.CmdReset())
|
||||
self.add(system.CmdShutdown())
|
||||
self.add(system.CmdPy())
|
||||
|
||||
# Admin commands
|
||||
self.add(admin.CmdDelPlayer())
|
||||
self.add(admin.CmdNewPassword())
|
||||
|
||||
# Comm commands
|
||||
self.add(comms.CmdAddCom())
|
||||
self.add(comms.CmdDelCom())
|
||||
self.add(comms.CmdAllCom())
|
||||
self.add(comms.CmdChannels())
|
||||
self.add(comms.CmdCdestroy())
|
||||
self.add(comms.CmdChannelCreate())
|
||||
self.add(comms.CmdClock())
|
||||
self.add(comms.CmdCBoot())
|
||||
self.add(comms.CmdCemit())
|
||||
self.add(comms.CmdCWho())
|
||||
self.add(comms.CmdCdesc())
|
||||
self.add(comms.CmdPage())
|
||||
self.add(comms.CmdIRC2Chan())
|
||||
self.add(comms.CmdRSS2Chan())
|
||||
#self.add(comms.CmdIMC2Chan())
|
||||
#self.add(comms.CmdIMCInfo())
|
||||
#self.add(comms.CmdIMCTell())
|
||||
16
lib/commands/default/cmdset_session.py
Normal file
16
lib/commands/default/cmdset_session.py
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
"""
|
||||
This module stores session-level commands.
|
||||
"""
|
||||
from src.commands.cmdset import CmdSet
|
||||
from src.commands.default import player
|
||||
|
||||
class SessionCmdSet(CmdSet):
|
||||
"""
|
||||
Sets up the unlogged cmdset.
|
||||
"""
|
||||
key = "DefaultSession"
|
||||
priority = -20
|
||||
|
||||
def at_cmdset_creation(self):
|
||||
"Populate the cmdset"
|
||||
self.add(player.CmdSessions())
|
||||
24
lib/commands/default/cmdset_unloggedin.py
Normal file
24
lib/commands/default/cmdset_unloggedin.py
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
"""
|
||||
This module describes the unlogged state of the default game.
|
||||
The setting STATE_UNLOGGED should be set to the python path
|
||||
of the state instance in this module.
|
||||
"""
|
||||
from src.commands.cmdset import CmdSet
|
||||
from src.commands.default import unloggedin
|
||||
|
||||
|
||||
class UnloggedinCmdSet(CmdSet):
|
||||
"""
|
||||
Sets up the unlogged cmdset.
|
||||
"""
|
||||
key = "DefaultUnloggedin"
|
||||
priority = 0
|
||||
|
||||
def at_cmdset_creation(self):
|
||||
"Populate the cmdset"
|
||||
self.add(unloggedin.CmdUnconnectedConnect())
|
||||
self.add(unloggedin.CmdUnconnectedCreate())
|
||||
self.add(unloggedin.CmdUnconnectedQuit())
|
||||
self.add(unloggedin.CmdUnconnectedLook())
|
||||
self.add(unloggedin.CmdUnconnectedHelp())
|
||||
self.add(unloggedin.CmdUnconnectedEncoding())
|
||||
1169
lib/commands/default/comms.py
Normal file
1169
lib/commands/default/comms.py
Normal file
File diff suppressed because it is too large
Load diff
448
lib/commands/default/general.py
Normal file
448
lib/commands/default/general.py
Normal file
|
|
@ -0,0 +1,448 @@
|
|||
"""
|
||||
General Character commands usually availabe to all characters
|
||||
"""
|
||||
from django.conf import settings
|
||||
from src.utils import utils, prettytable
|
||||
from src.commands.default.muxcommand import MuxCommand
|
||||
|
||||
|
||||
# limit symbol import for API
|
||||
__all__ = ("CmdHome", "CmdLook", "CmdNick",
|
||||
"CmdInventory", "CmdGet", "CmdDrop", "CmdGive",
|
||||
"CmdSay", "CmdPose", "CmdAccess")
|
||||
|
||||
AT_SEARCH_RESULT = utils.variable_from_module(*settings.SEARCH_AT_RESULT.rsplit('.', 1))
|
||||
|
||||
|
||||
class CmdHome(MuxCommand):
|
||||
"""
|
||||
move to your character's home location
|
||||
|
||||
Usage:
|
||||
home
|
||||
|
||||
Teleports you to your home location.
|
||||
"""
|
||||
|
||||
key = "home"
|
||||
locks = "cmd:perm(home) or perm(Builders)"
|
||||
|
||||
def func(self):
|
||||
"Implement the command"
|
||||
caller = self.caller
|
||||
home = caller.home
|
||||
if not home:
|
||||
caller.msg("You have no home!")
|
||||
elif home == caller.location:
|
||||
caller.msg("You are already home!")
|
||||
else:
|
||||
caller.move_to(home)
|
||||
caller.msg("There's no place like home ...")
|
||||
|
||||
class CmdLook(MuxCommand):
|
||||
"""
|
||||
look at location or object
|
||||
|
||||
Usage:
|
||||
look
|
||||
look <obj>
|
||||
look *<player>
|
||||
|
||||
Observes your location or objects in your vicinity.
|
||||
"""
|
||||
key = "look"
|
||||
aliases = ["l", "ls"]
|
||||
locks = "cmd:all()"
|
||||
arg_regex = r"\s.*?|$"
|
||||
|
||||
def func(self):
|
||||
"""
|
||||
Handle the looking.
|
||||
"""
|
||||
caller = self.caller
|
||||
args = self.args
|
||||
if args:
|
||||
# Use search to handle duplicate/nonexistant results.
|
||||
looking_at_obj = caller.search(args, use_nicks=True)
|
||||
if not looking_at_obj:
|
||||
return
|
||||
else:
|
||||
looking_at_obj = caller.location
|
||||
if not looking_at_obj:
|
||||
caller.msg("You have no location to look at!")
|
||||
return
|
||||
|
||||
if not hasattr(looking_at_obj, 'return_appearance'):
|
||||
# this is likely due to us having a player instead
|
||||
looking_at_obj = looking_at_obj.character
|
||||
if not looking_at_obj.access(caller, "view"):
|
||||
caller.msg("Could not find '%s'." % args)
|
||||
return
|
||||
# get object's appearance
|
||||
caller.msg(looking_at_obj.return_appearance(caller))
|
||||
# the object's at_desc() method.
|
||||
looking_at_obj.at_desc(looker=caller)
|
||||
|
||||
|
||||
class CmdNick(MuxCommand):
|
||||
"""
|
||||
define a personal alias/nick
|
||||
|
||||
Usage:
|
||||
nick[/switches] <nickname> = [<string>]
|
||||
alias ''
|
||||
|
||||
Switches:
|
||||
object - alias an object
|
||||
player - alias a player
|
||||
clearall - clear all your aliases
|
||||
list - show all defined aliases (also "nicks" works)
|
||||
|
||||
Examples:
|
||||
nick hi = say Hello, I'm Sarah!
|
||||
nick/object tom = the tall man
|
||||
|
||||
A 'nick' is a personal shortcut you create for your own use. When
|
||||
you enter the nick, the alternative string will be sent instead.
|
||||
The switches control in which situations the substitution will
|
||||
happen. The default is that it will happen when you enter a
|
||||
command. The 'object' and 'player' nick-types kick in only when
|
||||
you use commands that requires an object or player as a target -
|
||||
you can then use the nick to refer to them.
|
||||
|
||||
Note that no objects are actually renamed or changed by this
|
||||
command - the nick is only available to you. If you want to
|
||||
permanently add keywords to an object for everyone to use, you
|
||||
need build privileges and to use the @alias command.
|
||||
"""
|
||||
key = "nick"
|
||||
aliases = ["nickname", "nicks", "@nick", "alias"]
|
||||
locks = "cmd:all()"
|
||||
|
||||
def func(self):
|
||||
"Create the nickname"
|
||||
|
||||
caller = self.caller
|
||||
switches = self.switches
|
||||
nicks = caller.nicks.get(return_obj=True)
|
||||
|
||||
if 'list' in switches:
|
||||
table = prettytable.PrettyTable(["{wNickType",
|
||||
"{wNickname",
|
||||
"{wTranslates-to"])
|
||||
for nick in utils.make_iter(nicks):
|
||||
table.add_row([nick.db_category, nick.db_key, nick.db_strvalue])
|
||||
string = "{wDefined Nicks:{n\n%s" % table
|
||||
caller.msg(string)
|
||||
return
|
||||
if 'clearall' in switches:
|
||||
caller.nicks.clear()
|
||||
caller.msg("Cleared all aliases.")
|
||||
return
|
||||
if not self.args or not self.lhs:
|
||||
caller.msg("Usage: nick[/switches] nickname = [realname]")
|
||||
return
|
||||
nick = self.lhs
|
||||
real = self.rhs
|
||||
|
||||
if real == nick:
|
||||
caller.msg("No point in setting nick same as the string to replace...")
|
||||
return
|
||||
|
||||
# check so we have a suitable nick type
|
||||
if not any(True for switch in switches if switch in ("object", "player", "inputline")):
|
||||
switches = ["inputline"]
|
||||
string = ""
|
||||
for switch in switches:
|
||||
oldnick = caller.nicks.get(key=nick, category=switch)
|
||||
if not real:
|
||||
# removal of nick
|
||||
if oldnick:
|
||||
# clear the alias
|
||||
string += "\nNick '%s' (= '%s') was cleared." % (nick, oldnick)
|
||||
caller.nicks.delete(nick, category=switch)
|
||||
else:
|
||||
string += "\nNo nick '%s' found, so it could not be removed." % nick
|
||||
else:
|
||||
# creating new nick
|
||||
if oldnick:
|
||||
string += "\nNick %s changed from '%s' to '%s'." % (nick, oldnick, real)
|
||||
else:
|
||||
string += "\nNick set: '%s' = '%s'." % (nick, real)
|
||||
caller.nicks.add(nick, real, category=switch)
|
||||
caller.msg(string)
|
||||
|
||||
|
||||
class CmdInventory(MuxCommand):
|
||||
"""
|
||||
view inventory
|
||||
|
||||
Usage:
|
||||
inventory
|
||||
inv
|
||||
|
||||
Shows your inventory.
|
||||
"""
|
||||
key = "inventory"
|
||||
aliases = ["inv", "i"]
|
||||
locks = "cmd:all()"
|
||||
|
||||
def func(self):
|
||||
"check inventory"
|
||||
items = self.caller.contents
|
||||
if not items:
|
||||
string = "You are not carrying anything."
|
||||
else:
|
||||
table = prettytable.PrettyTable(["name", "desc"])
|
||||
table.header = False
|
||||
table.border = False
|
||||
for item in items:
|
||||
table.add_row(["{C%s{n" % item.name, item.db.desc and item.db.desc or ""])
|
||||
string = "{wYou are carrying:\n%s" % table
|
||||
self.caller.msg(string)
|
||||
|
||||
|
||||
class CmdGet(MuxCommand):
|
||||
"""
|
||||
pick up something
|
||||
|
||||
Usage:
|
||||
get <obj>
|
||||
|
||||
Picks up an object from your location and puts it in
|
||||
your inventory.
|
||||
"""
|
||||
key = "get"
|
||||
aliases = "grab"
|
||||
locks = "cmd:all()"
|
||||
|
||||
def func(self):
|
||||
"implements the command."
|
||||
|
||||
caller = self.caller
|
||||
|
||||
if not self.args:
|
||||
caller.msg("Get what?")
|
||||
return
|
||||
#print "general/get:", caller, caller.location, self.args, caller.location.contents
|
||||
obj = caller.search(self.args, location=caller.location)
|
||||
if not obj:
|
||||
return
|
||||
if caller == obj:
|
||||
caller.msg("You can't get yourself.")
|
||||
return
|
||||
#print obj, obj.location, caller, caller==obj.location
|
||||
if caller == obj.location:
|
||||
caller.msg("You already hold that.")
|
||||
return
|
||||
if not obj.access(caller, 'get'):
|
||||
if obj.db.get_err_msg:
|
||||
caller.msg(obj.db.get_err_msg)
|
||||
else:
|
||||
caller.msg("You can't get that.")
|
||||
return
|
||||
|
||||
obj.move_to(caller, quiet=True)
|
||||
caller.msg("You pick up %s." % obj.name)
|
||||
caller.location.msg_contents("%s picks up %s." %
|
||||
(caller.name,
|
||||
obj.name),
|
||||
exclude=caller)
|
||||
# calling hook method
|
||||
obj.at_get(caller)
|
||||
|
||||
|
||||
class CmdDrop(MuxCommand):
|
||||
"""
|
||||
drop something
|
||||
|
||||
Usage:
|
||||
drop <obj>
|
||||
|
||||
Lets you drop an object from your inventory into the
|
||||
location you are currently in.
|
||||
"""
|
||||
|
||||
key = "drop"
|
||||
locks = "cmd:all()"
|
||||
|
||||
def func(self):
|
||||
"Implement command"
|
||||
|
||||
caller = self.caller
|
||||
if not self.args:
|
||||
caller.msg("Drop what?")
|
||||
return
|
||||
|
||||
# Because the DROP command by definition looks for items
|
||||
# in inventory, call the search function using location = caller
|
||||
results = caller.search(self.args, location=caller, quiet=True)
|
||||
|
||||
# now we send it into the error handler (this will output consistent
|
||||
# error messages if there are problems).
|
||||
obj = AT_SEARCH_RESULT(caller, self.args, results, False,
|
||||
nofound_string="You aren't carrying %s." % self.args,
|
||||
multimatch_string="You carry more than one %s:" % self.args)
|
||||
if not obj:
|
||||
return
|
||||
|
||||
obj.move_to(caller.location, quiet=True)
|
||||
caller.msg("You drop %s." % (obj.name,))
|
||||
caller.location.msg_contents("%s drops %s." %
|
||||
(caller.name, obj.name),
|
||||
exclude=caller)
|
||||
# Call the object script's at_drop() method.
|
||||
obj.at_drop(caller)
|
||||
|
||||
|
||||
class CmdGive(MuxCommand):
|
||||
"""
|
||||
give away something to someone
|
||||
|
||||
Usage:
|
||||
give <inventory obj> = <target>
|
||||
|
||||
Gives an items from your inventory to another character,
|
||||
placing it in their inventory.
|
||||
"""
|
||||
key = "give"
|
||||
locks = "cmd:all()"
|
||||
|
||||
def func(self):
|
||||
"Implement give"
|
||||
|
||||
caller = self.caller
|
||||
if not self.args or not self.rhs:
|
||||
caller.msg("Usage: give <inventory object> = <target>")
|
||||
return
|
||||
to_give = caller.search(self.lhs)
|
||||
target = caller.search(self.rhs)
|
||||
if not (to_give and target):
|
||||
return
|
||||
if target == caller:
|
||||
caller.msg("You keep %s to yourself." % to_give.key)
|
||||
return
|
||||
if not to_give.location == caller:
|
||||
caller.msg("You are not holding %s." % to_give.key)
|
||||
return
|
||||
# give object
|
||||
caller.msg("You give %s to %s." % (to_give.key, target.key))
|
||||
to_give.move_to(target, quiet=True)
|
||||
target.msg("%s gives you %s." % (caller.key, to_give.key))
|
||||
|
||||
|
||||
class CmdSay(MuxCommand):
|
||||
"""
|
||||
speak as your character
|
||||
|
||||
Usage:
|
||||
say <message>
|
||||
|
||||
Talk to those in your current location.
|
||||
"""
|
||||
|
||||
key = "say"
|
||||
aliases = ['"', "'"]
|
||||
locks = "cmd:all()"
|
||||
|
||||
def func(self):
|
||||
"Run the say command"
|
||||
|
||||
caller = self.caller
|
||||
|
||||
if not self.args:
|
||||
caller.msg("Say what?")
|
||||
return
|
||||
|
||||
speech = self.args
|
||||
|
||||
# calling the speech hook on the location
|
||||
speech = caller.location.at_say(caller, speech)
|
||||
|
||||
# Feedback for the object doing the talking.
|
||||
caller.msg('You say, "%s{n"' % speech)
|
||||
|
||||
# Build the string to emit to neighbors.
|
||||
emit_string = '%s says, "%s{n"' % (caller.name,
|
||||
speech)
|
||||
caller.location.msg_contents(emit_string,
|
||||
exclude=caller)
|
||||
|
||||
|
||||
class CmdPose(MuxCommand):
|
||||
"""
|
||||
strike a pose
|
||||
|
||||
Usage:
|
||||
pose <pose text>
|
||||
pose's <pose text>
|
||||
|
||||
Example:
|
||||
pose is standing by the wall, smiling.
|
||||
-> others will see:
|
||||
Tom is standing by the wall, smiling.
|
||||
|
||||
Describe an action being taken. The pose text will
|
||||
automatically begin with your name.
|
||||
"""
|
||||
key = "pose"
|
||||
aliases = [":", "emote"]
|
||||
locks = "cmd:all()"
|
||||
|
||||
def parse(self):
|
||||
"""
|
||||
Custom parse the cases where the emote
|
||||
starts with some special letter, such
|
||||
as 's, at which we don't want to separate
|
||||
the caller's name and the emote with a
|
||||
space.
|
||||
"""
|
||||
args = self.args
|
||||
if args and not args[0] in ["'", ",", ":"]:
|
||||
args = " %s" % args.strip()
|
||||
self.args = args
|
||||
|
||||
def func(self):
|
||||
"Hook function"
|
||||
if not self.args:
|
||||
msg = "What do you want to do?"
|
||||
self.caller.msg(msg)
|
||||
else:
|
||||
msg = "%s%s" % (self.caller.name, self.args)
|
||||
self.caller.location.msg_contents(msg)
|
||||
|
||||
|
||||
class CmdAccess(MuxCommand):
|
||||
"""
|
||||
show your current game access
|
||||
|
||||
Usage:
|
||||
access
|
||||
|
||||
This command shows you the permission hierarchy and
|
||||
which permission groups you are a member of.
|
||||
"""
|
||||
key = "access"
|
||||
aliases = ["groups", "hierarchy"]
|
||||
locks = "cmd:all()"
|
||||
|
||||
def func(self):
|
||||
"Load the permission groups"
|
||||
|
||||
caller = self.caller
|
||||
hierarchy_full = settings.PERMISSION_HIERARCHY
|
||||
string = "\n{wPermission Hierarchy{n (climbing):\n %s" % ", ".join(hierarchy_full)
|
||||
#hierarchy = [p.lower() for p in hierarchy_full]
|
||||
|
||||
if self.caller.player.is_superuser:
|
||||
cperms = "<Superuser>"
|
||||
pperms = "<Superuser>"
|
||||
else:
|
||||
cperms = ", ".join(caller.permissions.all())
|
||||
pperms = ", ".join(caller.player.permissions.all())
|
||||
|
||||
string += "\n{wYour access{n:"
|
||||
string += "\nCharacter {c%s{n: %s" % (caller.key, cperms)
|
||||
if hasattr(caller, 'player'):
|
||||
string += "\nPlayer {c%s{n: %s" % (caller.player.key, pperms)
|
||||
caller.msg(string)
|
||||
266
lib/commands/default/help.py
Normal file
266
lib/commands/default/help.py
Normal file
|
|
@ -0,0 +1,266 @@
|
|||
"""
|
||||
The help command. The basic idea is that help texts for commands
|
||||
are best written by those that write the commands - the admins. So
|
||||
command-help is all auto-loaded and searched from the current command
|
||||
set. The normal, database-tied help system is used for collaborative
|
||||
creation of other help topics such as RP help or game-world aides.
|
||||
"""
|
||||
|
||||
from collections import defaultdict
|
||||
from src.utils.utils import fill, dedent
|
||||
from src.commands.command import Command
|
||||
from src.help.models import HelpEntry
|
||||
from src.utils import create
|
||||
from src.utils.utils import string_suggestions
|
||||
from src.commands.default.muxcommand import MuxCommand
|
||||
|
||||
# limit symbol import for API
|
||||
__all__ = ("CmdHelp", "CmdSetHelp")
|
||||
|
||||
|
||||
SEP = "{C" + "-" * 78 + "{n"
|
||||
|
||||
|
||||
def format_help_entry(title, help_text, aliases=None, suggested=None):
|
||||
"""
|
||||
This visually formats the help entry.
|
||||
"""
|
||||
string = SEP + "\n"
|
||||
if title:
|
||||
string += "{CHelp topic for {w%s{n" % title
|
||||
if aliases:
|
||||
string += " {C(aliases: {w%s{n{C){n" % (", ".join(aliases))
|
||||
if help_text:
|
||||
string += "\n%s" % dedent(help_text.rstrip())
|
||||
if suggested:
|
||||
string += "\n\n{CSuggested:{n "
|
||||
string += "{w%s{n" % fill(", ".join(suggested))
|
||||
string.strip()
|
||||
string += "\n" + SEP
|
||||
return string
|
||||
|
||||
|
||||
def format_help_list(hdict_cmds, hdict_db):
|
||||
"""
|
||||
Output a category-ordered list. The input are the
|
||||
pre-loaded help files for commands and database-helpfiles
|
||||
resectively.
|
||||
"""
|
||||
string = ""
|
||||
if hdict_cmds and any(hdict_cmds.values()):
|
||||
string += "\n" + SEP + "\n {CCommand help entries{n\n" + SEP
|
||||
for category in sorted(hdict_cmds.keys()):
|
||||
string += "\n {w%s{n:\n" % (str(category).title())
|
||||
string += "{G" + fill(", ".join(sorted(hdict_cmds[category]))) + "{n"
|
||||
if hdict_db and any(hdict_db.values()):
|
||||
string += "\n\n" + SEP + "\n\r {COther help entries{n\n" + SEP
|
||||
for category in sorted(hdict_db.keys()):
|
||||
string += "\n\r {w%s{n:\n" % (str(category).title())
|
||||
string += "{G" + fill(", ".join(sorted([str(topic) for topic in hdict_db[category]]))) + "{n"
|
||||
return string
|
||||
|
||||
|
||||
class CmdHelp(Command):
|
||||
"""
|
||||
view help or a list of topics
|
||||
|
||||
Usage:
|
||||
help <topic or command>
|
||||
help list
|
||||
help all
|
||||
|
||||
This will search for help on commands and other
|
||||
topics related to the game.
|
||||
"""
|
||||
key = "help"
|
||||
locks = "cmd:all()"
|
||||
|
||||
# this is a special cmdhandler flag that makes the cmdhandler also pack
|
||||
# the current cmdset with the call to self.func().
|
||||
return_cmdset = True
|
||||
|
||||
def parse(self):
|
||||
"""
|
||||
input is a string containing the command or topic to match.
|
||||
"""
|
||||
self.original_args = self.args.strip()
|
||||
self.args = self.args.strip().lower()
|
||||
|
||||
def func(self):
|
||||
"""
|
||||
Run the dynamic help entry creator.
|
||||
"""
|
||||
query, cmdset = self.args, self.cmdset
|
||||
caller = self.caller
|
||||
|
||||
suggestion_cutoff = 0.6
|
||||
suggestion_maxnum = 5
|
||||
|
||||
if not query:
|
||||
query = "all"
|
||||
|
||||
# removing doublets in cmdset, caused by cmdhandler
|
||||
# having to allow doublet commands to manage exits etc.
|
||||
cmdset.make_unique(caller)
|
||||
|
||||
# retrieve all available commands and database topics
|
||||
all_cmds = [cmd for cmd in cmdset if cmd.auto_help and cmd.access(caller)]
|
||||
all_topics = [topic for topic in HelpEntry.objects.all() if topic.access(caller, 'view', default=True)]
|
||||
all_categories = list(set([cmd.help_category.lower() for cmd in all_cmds] + [topic.help_category.lower() for topic in all_topics]))
|
||||
|
||||
if query in ("list", "all"):
|
||||
# we want to list all available help entries, grouped by category
|
||||
hdict_cmd = defaultdict(list)
|
||||
hdict_topic = defaultdict(list)
|
||||
# create the dictionaries {category:[topic, topic ...]} required by format_help_list
|
||||
[hdict_cmd[cmd.help_category].append(cmd.key) for cmd in all_cmds]
|
||||
[hdict_topic[topic.help_category].append(topic.key) for topic in all_topics]
|
||||
# report back
|
||||
self.msg(format_help_list(hdict_cmd, hdict_topic))
|
||||
return
|
||||
|
||||
# Try to access a particular command
|
||||
|
||||
# build vocabulary of suggestions and rate them by string similarity.
|
||||
vocabulary = [cmd.key for cmd in all_cmds if cmd] + [topic.key for topic in all_topics] + all_categories
|
||||
[vocabulary.extend(cmd.aliases) for cmd in all_cmds]
|
||||
suggestions = [sugg for sugg in string_suggestions(query, set(vocabulary), cutoff=suggestion_cutoff, maxnum=suggestion_maxnum)
|
||||
if sugg != query]
|
||||
if not suggestions:
|
||||
suggestions = [sugg for sugg in vocabulary if sugg != query and sugg.startswith(query)]
|
||||
|
||||
# try an exact command auto-help match
|
||||
match = [cmd for cmd in all_cmds if cmd == query]
|
||||
if len(match) == 1:
|
||||
self.msg(format_help_entry(match[0].key,
|
||||
match[0].__doc__,
|
||||
aliases=match[0].aliases,
|
||||
suggested=suggestions))
|
||||
return
|
||||
|
||||
# try an exact database help entry match
|
||||
match = list(HelpEntry.objects.find_topicmatch(query, exact=True))
|
||||
if len(match) == 1:
|
||||
self.msg(format_help_entry(match[0].key,
|
||||
match[0].entrytext,
|
||||
suggested=suggestions))
|
||||
return
|
||||
|
||||
# try to see if a category name was entered
|
||||
if query in all_categories:
|
||||
self.msg(format_help_list({query:[cmd.key for cmd in all_cmds if cmd.help_category==query]},
|
||||
{query:[topic.key for topic in all_topics if topic.help_category==query]}))
|
||||
return
|
||||
|
||||
# no exact matches found. Just give suggestions.
|
||||
self.msg(format_help_entry("", "No help entry found for '%s'" % query, None, suggested=suggestions))
|
||||
|
||||
|
||||
class CmdSetHelp(MuxCommand):
|
||||
"""
|
||||
edit the help database
|
||||
|
||||
Usage:
|
||||
@help[/switches] <topic>[,category[,locks]] = <text>
|
||||
|
||||
Switches:
|
||||
add - add or replace a new topic with text.
|
||||
append - add text to the end of topic with a newline between.
|
||||
merge - As append, but don't add a newline between the old
|
||||
text and the appended text.
|
||||
delete - remove help topic.
|
||||
force - (used with add) create help topic also if the topic
|
||||
already exists.
|
||||
|
||||
Examples:
|
||||
@sethelp/add throw = This throws something at ...
|
||||
@sethelp/append pickpocketing,Thievery = This steals ...
|
||||
@sethelp/append pickpocketing, ,attr(is_thief) = This steals ...
|
||||
|
||||
This command manipulates the help database. A help entry can be created,
|
||||
appended/merged to and deleted. If you don't assign a category, the
|
||||
"General" category will be used. If no lockstring is specified, default
|
||||
is to let everyone read the help file.
|
||||
|
||||
"""
|
||||
key = "@help"
|
||||
aliases = "@sethelp"
|
||||
locks = "cmd:perm(PlayerHelpers)"
|
||||
help_category = "Building"
|
||||
|
||||
def func(self):
|
||||
"Implement the function"
|
||||
|
||||
switches = self.switches
|
||||
lhslist = self.lhslist
|
||||
|
||||
if not self.args:
|
||||
self.msg("Usage: @sethelp/[add|del|append|merge] <topic>[,category[,locks,..] = <text>")
|
||||
return
|
||||
|
||||
topicstr = ""
|
||||
category = "General"
|
||||
lockstring = "view:all()"
|
||||
try:
|
||||
topicstr = lhslist[0]
|
||||
category = lhslist[1]
|
||||
lockstring = ",".join(lhslist[2:])
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
if not topicstr:
|
||||
self.msg("You have to define a topic!")
|
||||
return
|
||||
# check if we have an old entry with the same name
|
||||
try:
|
||||
old_entry = HelpEntry.objects.get(db_key__iexact=topicstr)
|
||||
except Exception:
|
||||
old_entry = None
|
||||
|
||||
if 'append' in switches or "merge" in switches:
|
||||
# merge/append operations
|
||||
if not old_entry:
|
||||
self.msg("Could not find topic '%s'. You must give an exact name." % topicstr)
|
||||
return
|
||||
if not self.rhs:
|
||||
self.msg("You must supply text to append/merge.")
|
||||
return
|
||||
if 'merge' in switches:
|
||||
old_entry.entrytext += " " + self.rhs
|
||||
else:
|
||||
old_entry.entrytext += "\n\n%s" % self.rhs
|
||||
self.msg("Entry updated:\n%s" % old_entry.entrytext)
|
||||
return
|
||||
if 'delete' in switches or 'del' in switches:
|
||||
# delete the help entry
|
||||
if not old_entry:
|
||||
self.msg("Could not find topic '%s'" % topicstr)
|
||||
return
|
||||
old_entry.delete()
|
||||
self.msg("Deleted help entry '%s'." % topicstr)
|
||||
return
|
||||
|
||||
# at this point it means we want to add a new help entry.
|
||||
if not self.rhs:
|
||||
self.msg("You must supply a help text to add.")
|
||||
return
|
||||
if old_entry:
|
||||
if 'for' in switches or 'force' in switches:
|
||||
# overwrite old entry
|
||||
old_entry.key = topicstr
|
||||
old_entry.entrytext = self.rhs
|
||||
old_entry.help_category = category
|
||||
old_entry.locks.clear()
|
||||
old_entry.locks.add(lockstring)
|
||||
old_entry.save()
|
||||
self.msg("Overwrote the old topic '%s' with a new one." % topicstr)
|
||||
else:
|
||||
self.msg("Topic '%s' already exists. Use /force to overwrite or /append or /merge to add text to it." % topicstr)
|
||||
else:
|
||||
# no old entry. Create a new one.
|
||||
new_entry = create.create_help_entry(topicstr,
|
||||
self.rhs, category, lockstring)
|
||||
if new_entry:
|
||||
self.msg("Topic '%s' was successfully created." % topicstr)
|
||||
else:
|
||||
self.msg("Error when creating topic '%s'! Contact an admin." % topicstr)
|
||||
194
lib/commands/default/muxcommand.py
Normal file
194
lib/commands/default/muxcommand.py
Normal file
|
|
@ -0,0 +1,194 @@
|
|||
"""
|
||||
The command template for the default MUX-style command set. There
|
||||
is also an Player/OOC version that makes sure caller is a Player object.
|
||||
"""
|
||||
|
||||
from src.utils import utils
|
||||
from src.commands.command import Command
|
||||
|
||||
# limit symbol import for API
|
||||
__all__ = ("MuxCommand", "MuxPlayerCommand")
|
||||
|
||||
class MuxCommand(Command):
|
||||
"""
|
||||
This sets up the basis for a MUX command. The idea
|
||||
is tkhat most other Mux-related commands should just
|
||||
inherit from this and don't have to implement much
|
||||
parsing of their own unless they do something particularly
|
||||
advanced.
|
||||
|
||||
Note that the class's __doc__ string (this text) is
|
||||
used by Evennia to create the automatic help entry for
|
||||
the command, so make sure to document consistently here.
|
||||
"""
|
||||
def has_perm(self, srcobj):
|
||||
"""
|
||||
This is called by the cmdhandler to determine
|
||||
if srcobj is allowed to execute this command.
|
||||
We just show it here for completeness - we
|
||||
are satisfied using the default check in Command.
|
||||
"""
|
||||
return super(MuxCommand, self).has_perm(srcobj)
|
||||
|
||||
def at_pre_cmd(self):
|
||||
"""
|
||||
This hook is called before self.parse() on all commands
|
||||
"""
|
||||
pass
|
||||
|
||||
def at_post_cmd(self):
|
||||
"""
|
||||
This hook is called after the command has finished executing
|
||||
(after self.func()).
|
||||
"""
|
||||
pass
|
||||
|
||||
def parse(self):
|
||||
"""
|
||||
This method is called by the cmdhandler once the command name
|
||||
has been identified. It creates a new set of member variables
|
||||
that can be later accessed from self.func() (see below)
|
||||
|
||||
The following variables are available for our use when entering this
|
||||
method (from the command definition, and assigned on the fly by the
|
||||
cmdhandler):
|
||||
self.key - the name of this command ('look')
|
||||
self.aliases - the aliases of this cmd ('l')
|
||||
self.permissions - permission string for this command
|
||||
self.help_category - overall category of command
|
||||
|
||||
self.caller - the object calling this command
|
||||
self.cmdstring - the actual command name used to call this
|
||||
(this allows you to know which alias was used,
|
||||
for example)
|
||||
self.args - the raw input; everything following self.cmdstring.
|
||||
self.cmdset - the cmdset from which this command was picked. Not
|
||||
often used (useful for commands like 'help' or to
|
||||
list all available commands etc)
|
||||
self.obj - the object on which this command was defined. It is often
|
||||
the same as self.caller.
|
||||
|
||||
A MUX command has the following possible syntax:
|
||||
|
||||
name[ with several words][/switch[/switch..]] arg1[,arg2,...] [[=|,] arg[,..]]
|
||||
|
||||
The 'name[ with several words]' part is already dealt with by the
|
||||
cmdhandler at this point, and stored in self.cmdname (we don't use
|
||||
it here). The rest of the command is stored in self.args, which can
|
||||
start with the switch indicator /.
|
||||
|
||||
This parser breaks self.args into its constituents and stores them in the
|
||||
following variables:
|
||||
self.switches = [list of /switches (without the /)]
|
||||
self.raw = This is the raw argument input, including switches
|
||||
self.args = This is re-defined to be everything *except* the switches
|
||||
self.lhs = Everything to the left of = (lhs:'left-hand side'). If
|
||||
no = is found, this is identical to self.args.
|
||||
self.rhs: Everything to the right of = (rhs:'right-hand side').
|
||||
If no '=' is found, this is None.
|
||||
self.lhslist - [self.lhs split into a list by comma]
|
||||
self.rhslist - [list of self.rhs split into a list by comma]
|
||||
self.arglist = [list of space-separated args (stripped, including '=' if it exists)]
|
||||
|
||||
All args and list members are stripped of excess whitespace around the
|
||||
strings, but case is preserved.
|
||||
"""
|
||||
raw = self.args
|
||||
args = raw.strip()
|
||||
|
||||
# split out switches
|
||||
switches = []
|
||||
if args and len(args) > 1 and args[0] == "/":
|
||||
# we have a switch, or a set of switches. These end with a space.
|
||||
#print "'%s'" % args
|
||||
switches = args[1:].split(None, 1)
|
||||
if len(switches) > 1:
|
||||
switches, args = switches
|
||||
switches = switches.split('/')
|
||||
else:
|
||||
args = ""
|
||||
switches = switches[0].split('/')
|
||||
arglist = [arg.strip() for arg in args.split()]
|
||||
|
||||
# check for arg1, arg2, ... = argA, argB, ... constructs
|
||||
lhs, rhs = args, None
|
||||
lhslist, rhslist = [arg.strip() for arg in args.split(',')], []
|
||||
if args and '=' in args:
|
||||
lhs, rhs = [arg.strip() for arg in args.split('=', 1)]
|
||||
lhslist = [arg.strip() for arg in lhs.split(',')]
|
||||
rhslist = [arg.strip() for arg in rhs.split(',')]
|
||||
|
||||
# save to object properties:
|
||||
self.raw = raw
|
||||
self.switches = switches
|
||||
self.args = args.strip()
|
||||
self.arglist = arglist
|
||||
self.lhs = lhs
|
||||
self.lhslist = lhslist
|
||||
self.rhs = rhs
|
||||
self.rhslist = rhslist
|
||||
|
||||
def func(self):
|
||||
"""
|
||||
This is the hook function that actually does all the work. It is called
|
||||
by the cmdhandler right after self.parser() finishes, and so has access
|
||||
to all the variables defined therein.
|
||||
"""
|
||||
# a simple test command to show the available properties
|
||||
string = "-" * 50
|
||||
string += "\n{w%s{n - Command variables from evennia:\n" % self.key
|
||||
string += "-" * 50
|
||||
string += "\nname of cmd (self.key): {w%s{n\n" % self.key
|
||||
string += "cmd aliases (self.aliases): {w%s{n\n" % self.aliases
|
||||
string += "cmd locks (self.locks): {w%s{n\n" % self.locks
|
||||
string += "help category (self.help_category): {w%s{n\n" % self.help_category
|
||||
string += "object calling (self.caller): {w%s{n\n" % self.caller
|
||||
string += "object storing cmdset (self.obj): {w%s{n\n" % self.obj
|
||||
string += "command string given (self.cmdstring): {w%s{n\n" % self.cmdstring
|
||||
# show cmdset.key instead of cmdset to shorten output
|
||||
string += utils.fill("current cmdset (self.cmdset): {w%s{n\n" % self.cmdset)
|
||||
|
||||
|
||||
string += "\n" + "-" * 50
|
||||
string += "\nVariables from MuxCommand baseclass\n"
|
||||
string += "-" * 50
|
||||
string += "\nraw argument (self.raw): {w%s{n \n" % self.raw
|
||||
string += "cmd args (self.args): {w%s{n\n" % self.args
|
||||
string += "cmd switches (self.switches): {w%s{n\n" % self.switches
|
||||
string += "space-separated arg list (self.arglist): {w%s{n\n" % self.arglist
|
||||
string += "lhs, left-hand side of '=' (self.lhs): {w%s{n\n" % self.lhs
|
||||
string += "lhs, comma separated (self.lhslist): {w%s{n\n" % self.lhslist
|
||||
string += "rhs, right-hand side of '=' (self.rhs): {w%s{n\n" % self.rhs
|
||||
string += "rhs, comma separated (self.rhslist): {w%s{n\n" % self.rhslist
|
||||
string += "-" * 50
|
||||
self.caller.msg(string)
|
||||
|
||||
class MuxPlayerCommand(MuxCommand):
|
||||
"""
|
||||
This is an on-Player version of the MuxCommand. Since these commands sit
|
||||
on Players rather than on Characters/Objects, we need to check
|
||||
this in the parser.
|
||||
|
||||
Player commands are available also when puppeting a Character, it's
|
||||
just that they are applied with a lower priority and are always
|
||||
available, also when disconnected from a character (i.e. "ooc").
|
||||
|
||||
This class makes sure that caller is always a Player object, while
|
||||
creating a new property "character" that is set only if a
|
||||
character is actually attached to this Player and Session.
|
||||
"""
|
||||
def parse(self):
|
||||
"""
|
||||
We run the parent parser as usual, then fix the result
|
||||
"""
|
||||
super(MuxPlayerCommand, self).parse()
|
||||
|
||||
if utils.inherits_from(self.caller, "src.objects.objects.DefaultObject"):
|
||||
# caller is an Object/Character
|
||||
self.character = self.caller
|
||||
self.caller = self.caller.player
|
||||
elif utils.inherits_from(self.caller, "src.players.players.DefaultPlayer"):
|
||||
# caller was already a Player
|
||||
self.character = self.caller.get_puppet(self.sessid)
|
||||
else:
|
||||
self.character = None
|
||||
700
lib/commands/default/player.py
Normal file
700
lib/commands/default/player.py
Normal file
|
|
@ -0,0 +1,700 @@
|
|||
"""
|
||||
Player (OOC) commands. These are stored on the Player object
|
||||
and self.caller is thus always a Player, not an Object/Character.
|
||||
|
||||
These commands go in the PlayerCmdset and are accessible also
|
||||
when puppeting a Character (although with lower priority)
|
||||
|
||||
These commands use the MuxCommandOOC parent that makes sure
|
||||
to setup caller correctly. They use self.player to make sure
|
||||
to always use the player object rather than self.caller (which
|
||||
change depending on the level you are calling from)
|
||||
The property self.character can be used to
|
||||
access the character when these commands are triggered with
|
||||
a connected character (such as the case of the @ooc command), it
|
||||
is None if we are OOC.
|
||||
|
||||
Note that under MULTISESSION_MODE=2, Player- commands should use
|
||||
self.msg() and similar methods to reroute returns to the correct
|
||||
method. Otherwise all text will be returned to all connected sessions.
|
||||
|
||||
"""
|
||||
import time
|
||||
from django.conf import settings
|
||||
from src.server.sessionhandler import SESSIONS
|
||||
from src.commands.default.muxcommand import MuxPlayerCommand
|
||||
from src.utils import utils, create, search, prettytable
|
||||
|
||||
from settings import MAX_NR_CHARACTERS, MULTISESSION_MODE
|
||||
# limit symbol import for API
|
||||
__all__ = ("CmdOOCLook", "CmdIC", "CmdOOC", "CmdPassword", "CmdQuit",
|
||||
"CmdCharCreate", "CmdEncoding", "CmdSessions", "CmdWho",
|
||||
"CmdColorTest", "CmdQuell")
|
||||
|
||||
# force max nr chars to 1 if mode is 0 or 1
|
||||
MAX_NR_CHARACTERS = MULTISESSION_MODE < 2 and 1 or MAX_NR_CHARACTERS
|
||||
BASE_PLAYER_TYPECLASS = settings.BASE_PLAYER_TYPECLASS
|
||||
|
||||
PERMISSION_HIERARCHY = settings.PERMISSION_HIERARCHY
|
||||
PERMISSION_HIERARCHY_LOWER = [perm.lower() for perm in PERMISSION_HIERARCHY]
|
||||
|
||||
# Obs - these are all intended to be stored on the Player, and as such,
|
||||
# use self.player instead of self.caller, just to be sure. Also self.msg()
|
||||
# is used to make sure returns go to the right session
|
||||
|
||||
class CmdOOCLook(MuxPlayerCommand):
|
||||
"""
|
||||
look while out-of-character
|
||||
|
||||
Usage:
|
||||
look
|
||||
|
||||
Look in the ooc state.
|
||||
"""
|
||||
|
||||
#This is an OOC version of the look command. Since a
|
||||
#Player doesn't have an in-game existence, there is no
|
||||
#concept of location or "self". If we are controlling
|
||||
#a character, pass control over to normal look.
|
||||
|
||||
key = "look"
|
||||
aliases = ["l", "ls"]
|
||||
locks = "cmd:all()"
|
||||
help_category = "General"
|
||||
|
||||
def look_target(self):
|
||||
"Hook method for when an argument is given."
|
||||
player = self.player
|
||||
key = self.args.lower()
|
||||
chars = dict((utils.to_str(char.key.lower()), char)
|
||||
for char in player.db._playable_characters)
|
||||
looktarget = chars.get(key)
|
||||
if looktarget:
|
||||
self.msg(looktarget.return_appearance(player))
|
||||
else:
|
||||
self.msg("No such character.")
|
||||
return
|
||||
|
||||
def no_look_target(self):
|
||||
"Hook method for default look without a specified target"
|
||||
# caller is always a player at this point.
|
||||
player = self.player
|
||||
sessid = self.sessid
|
||||
# get all our characters and sessions
|
||||
characters = player.db._playable_characters
|
||||
if None in characters:
|
||||
# clean up list if character object was deleted in between
|
||||
characters = [character for character in characters if character]
|
||||
player.db._playable_characters = characters
|
||||
|
||||
sessions = player.get_all_sessions()
|
||||
is_su = player.is_superuser
|
||||
|
||||
# text shown when looking in the ooc area
|
||||
string = "Account {g%s{n (you are Out-of-Character)" % (player.key)
|
||||
|
||||
nsess = len(sessions)
|
||||
string += nsess == 1 and "\n\n{wConnected session:{n" or "\n\n{wConnected sessions (%i):{n" % nsess
|
||||
for isess, sess in enumerate(sessions):
|
||||
csessid = sess.sessid
|
||||
addr = "%s (%s)" % (sess.protocol_key, isinstance(sess.address, tuple) and str(sess.address[0]) or str(sess.address))
|
||||
string += "\n %s %s" % (sessid == csessid and "{w%s{n" % (isess + 1) or (isess + 1), addr)
|
||||
string += "\n\n {whelp{n - more commands"
|
||||
string += "\n {wooc <Text>{n - talk on public channel"
|
||||
|
||||
if is_su or len(characters) < MAX_NR_CHARACTERS:
|
||||
if not characters:
|
||||
string += "\n\n You don't have any characters yet. See {whelp @charcreate{n for creating one."
|
||||
else:
|
||||
string += "\n {w@charcreate <name> [=description]{n - create new character"
|
||||
|
||||
if characters:
|
||||
string_s_ending = len(characters) > 1 and "s" or ""
|
||||
string += "\n {w@ic <character>{n - enter the game ({w@ooc{n to get back here)"
|
||||
if is_su:
|
||||
string += "\n\nAvailable character%s (%i/unlimited):" % (string_s_ending, len(characters))
|
||||
else:
|
||||
string += "\n\nAvailable character%s%s:" % (string_s_ending,
|
||||
MAX_NR_CHARACTERS > 1 and " (%i/%i)" % (len(characters), MAX_NR_CHARACTERS) or "")
|
||||
|
||||
for char in characters:
|
||||
csessid = char.sessid.get()
|
||||
if csessid:
|
||||
# character is already puppeted
|
||||
sessi = player.get_session(csessid)
|
||||
for sess in utils.make_iter(sessi):
|
||||
sid = sess in sessions and sessions.index(sess) + 1
|
||||
if sess and sid:
|
||||
string += "\n - {G%s{n [%s] (played by you in session %i)" % (char.key, ", ".join(char.permissions.all()), sid)
|
||||
else:
|
||||
string += "\n - {R%s{n [%s] (played by someone else)" % (char.key, ", ".join(char.permissions.all()))
|
||||
else:
|
||||
# character is "free to puppet"
|
||||
string += "\n - %s [%s]" % (char.key, ", ".join(char.permissions.all()))
|
||||
string = ("-" * 68) + "\n" + string + "\n" + ("-" * 68)
|
||||
self.msg(string)
|
||||
|
||||
def func(self):
|
||||
"implement the ooc look command"
|
||||
if MULTISESSION_MODE < 2:
|
||||
# only one character allowed
|
||||
string = "You are out-of-character (OOC).\nUse {w@ic{n to get back into the game."
|
||||
self.msg(string)
|
||||
return
|
||||
if utils.inherits_from(self.caller, "src.objects.objects.Object"):
|
||||
# An object of some type is calling. Use default look instead.
|
||||
super(CmdOOCLook, self).func()
|
||||
elif self.args:
|
||||
self.look_target()
|
||||
else:
|
||||
self.no_look_target()
|
||||
|
||||
|
||||
class CmdCharCreate(MuxPlayerCommand):
|
||||
"""
|
||||
create a new character
|
||||
|
||||
Usage:
|
||||
@charcreate <charname> [= desc]
|
||||
|
||||
Create a new character, optionally giving it a description. You
|
||||
may use upper-case letters in the name - you will nevertheless
|
||||
always be able to access your character using lower-case letters
|
||||
if you want.
|
||||
"""
|
||||
key = "@charcreate"
|
||||
locks = "cmd:pperm(Players)"
|
||||
help_category = "General"
|
||||
|
||||
def func(self):
|
||||
"create the new character"
|
||||
player = self.player
|
||||
if not self.args:
|
||||
self.msg("Usage: @charcreate <charname> [= description]")
|
||||
return
|
||||
key = self.lhs
|
||||
desc = self.rhs
|
||||
if not player.is_superuser and \
|
||||
(player.db._playable_characters and
|
||||
len(player.db._playable_characters) >= MAX_NR_CHARACTERS):
|
||||
self.msg("You may only create a maximum of %i characters." % MAX_NR_CHARACTERS)
|
||||
return
|
||||
# create the character
|
||||
from src.objects.models import ObjectDB
|
||||
|
||||
start_location = ObjectDB.objects.get_id(settings.START_LOCATION)
|
||||
default_home = ObjectDB.objects.get_id(settings.DEFAULT_HOME)
|
||||
typeclass = settings.BASE_CHARACTER_TYPECLASS
|
||||
permissions = settings.PERMISSION_PLAYER_DEFAULT
|
||||
|
||||
new_character = create.create_object(typeclass, key=key,
|
||||
location=start_location,
|
||||
home=default_home,
|
||||
permissions=permissions)
|
||||
# only allow creator (and immortals) to puppet this char
|
||||
new_character.locks.add("puppet:id(%i) or pid(%i) or perm(Immortals) or pperm(Immortals)" %
|
||||
(new_character.id, player.id))
|
||||
player.db._playable_characters.append(new_character)
|
||||
if desc:
|
||||
new_character.db.desc = desc
|
||||
elif not new_character.db.desc:
|
||||
new_character.db.desc = "This is a Player."
|
||||
self.msg("Created new character %s. Use {w@ic %s{n to enter the game as this character." % (new_character.key, new_character.key))
|
||||
|
||||
|
||||
class CmdIC(MuxPlayerCommand):
|
||||
"""
|
||||
control an object you have permission to puppet
|
||||
|
||||
Usage:
|
||||
@ic <character>
|
||||
|
||||
Go in-character (IC) as a given Character.
|
||||
|
||||
This will attempt to "become" a different object assuming you have
|
||||
the right to do so. Note that it's the PLAYER character that puppets
|
||||
characters/objects and which needs to have the correct permission!
|
||||
|
||||
You cannot become an object that is already controlled by another
|
||||
player. In principle <character> can be any in-game object as long
|
||||
as you the player have access right to puppet it.
|
||||
"""
|
||||
|
||||
key = "@ic"
|
||||
# lockmust be all() for different puppeted objects to access it.
|
||||
locks = "cmd:all()"
|
||||
aliases = "@puppet"
|
||||
help_category = "General"
|
||||
|
||||
def func(self):
|
||||
"""
|
||||
Main puppet method
|
||||
"""
|
||||
player = self.player
|
||||
sessid = self.sessid
|
||||
|
||||
new_character = None
|
||||
if not self.args:
|
||||
new_character = player.db._last_puppet
|
||||
if not new_character:
|
||||
self.msg("Usage: @ic <character>")
|
||||
return
|
||||
if not new_character:
|
||||
# search for a matching character
|
||||
new_character = search.object_search(self.args)
|
||||
if new_character:
|
||||
new_character = new_character[0]
|
||||
else:
|
||||
self.msg("That is not a valid character choice.")
|
||||
return
|
||||
# permission checks
|
||||
if player.get_puppet(sessid) == new_character:
|
||||
self.msg("{RYou already act as {c%s{n." % new_character.name)
|
||||
return
|
||||
if new_character.player:
|
||||
# may not puppet an already puppeted character
|
||||
if new_character.sessid.count() and new_character.player == player:
|
||||
# as a safeguard we allow "taking over" chars from your own sessions.
|
||||
if MULTISESSION_MODE in (1, 3):
|
||||
txt = "{c%s{n{G is now shared from another of your sessions.{n"
|
||||
txt2 = "Sharing {c%s{n with another of your sessions."
|
||||
else:
|
||||
txt = "{c%s{n{R is now acted from another of your sessions.{n"
|
||||
txt2 = "Taking over {c%s{n from another of your sessions."
|
||||
player.unpuppet_object(new_character.sessid.get())
|
||||
player.msg(txt % (new_character.name), sessid=new_character.sessid.get())
|
||||
self.msg(txt2 % new_character.name)
|
||||
elif new_character.player != player and new_character.player.is_connected:
|
||||
self.msg("{c%s{r is already acted by another player.{n" % new_character.name)
|
||||
return
|
||||
if not new_character.access(player, "puppet"):
|
||||
# main acccess check
|
||||
self.msg("{rYou may not become %s.{n" % new_character.name)
|
||||
return
|
||||
if player.puppet_object(sessid, new_character):
|
||||
player.db._last_puppet = new_character
|
||||
else:
|
||||
self.msg("{rYou cannot become {C%s{n." % new_character.name)
|
||||
|
||||
|
||||
class CmdOOC(MuxPlayerCommand):
|
||||
"""
|
||||
stop puppeting and go ooc
|
||||
|
||||
Usage:
|
||||
@ooc
|
||||
|
||||
Go out-of-character (OOC).
|
||||
|
||||
This will leave your current character and put you in a incorporeal OOC state.
|
||||
"""
|
||||
|
||||
key = "@ooc"
|
||||
# lock must be all(), for different puppeted objects to access it.
|
||||
locks = "cmd:pperm(Players)"
|
||||
aliases = "@unpuppet"
|
||||
help_category = "General"
|
||||
|
||||
def func(self):
|
||||
"Implement function"
|
||||
|
||||
player = self.player
|
||||
sessid = self.sessid
|
||||
|
||||
old_char = player.get_puppet(sessid)
|
||||
if not old_char:
|
||||
string = "You are already OOC."
|
||||
self.msg(string)
|
||||
return
|
||||
|
||||
player.db._last_puppet = old_char
|
||||
|
||||
# disconnect
|
||||
if player.unpuppet_object(sessid):
|
||||
self.msg("\n{GYou go OOC.{n\n")
|
||||
player.execute_cmd("look", sessid=sessid)
|
||||
else:
|
||||
raise RuntimeError("Could not unpuppet!")
|
||||
|
||||
class CmdSessions(MuxPlayerCommand):
|
||||
"""
|
||||
check your connected session(s)
|
||||
|
||||
Usage:
|
||||
@sessions
|
||||
|
||||
Lists the sessions currently connected to your account.
|
||||
|
||||
"""
|
||||
key = "@sessions"
|
||||
locks = "cmd:all()"
|
||||
help_category = "General"
|
||||
|
||||
def func(self):
|
||||
"Implement function"
|
||||
player = self.player
|
||||
sessions = player.get_all_sessions()
|
||||
|
||||
table = prettytable.PrettyTable(["{wsessid",
|
||||
"{wprotocol",
|
||||
"{whost",
|
||||
"{wpuppet/character",
|
||||
"{wlocation"])
|
||||
for sess in sorted(sessions, key=lambda x: x.sessid):
|
||||
sessid = sess.sessid
|
||||
char = player.get_puppet(sessid)
|
||||
table.add_row([str(sessid), str(sess.protocol_key),
|
||||
type(sess.address) == tuple and sess.address[0] or sess.address,
|
||||
char and str(char) or "None",
|
||||
char and str(char.location) or "N/A"])
|
||||
string = "{wYour current session(s):{n\n%s" % table
|
||||
self.msg(string)
|
||||
|
||||
|
||||
class CmdWho(MuxPlayerCommand):
|
||||
"""
|
||||
list who is currently online
|
||||
|
||||
Usage:
|
||||
who
|
||||
doing
|
||||
|
||||
Shows who is currently online. Doing is an alias that limits info
|
||||
also for those with all permissions.
|
||||
"""
|
||||
|
||||
key = "who"
|
||||
aliases = "doing"
|
||||
locks = "cmd:all()"
|
||||
|
||||
def func(self):
|
||||
"""
|
||||
Get all connected players by polling session.
|
||||
"""
|
||||
|
||||
player = self.player
|
||||
session_list = SESSIONS.get_sessions()
|
||||
|
||||
session_list = sorted(session_list, key=lambda o: o.player.key)
|
||||
|
||||
if self.cmdstring == "doing":
|
||||
show_session_data = False
|
||||
else:
|
||||
show_session_data = player.check_permstring("Immortals") or player.check_permstring("Wizards")
|
||||
|
||||
nplayers = (SESSIONS.player_count())
|
||||
if show_session_data:
|
||||
# privileged info
|
||||
table = prettytable.PrettyTable(["{wPlayer Name",
|
||||
"{wOn for",
|
||||
"{wIdle",
|
||||
"{wPuppeting",
|
||||
"{wRoom",
|
||||
"{wCmds",
|
||||
"{wProtocol",
|
||||
"{wHost"])
|
||||
for session in session_list:
|
||||
if not session.logged_in: continue
|
||||
delta_cmd = time.time() - session.cmd_last_visible
|
||||
delta_conn = time.time() - session.conn_time
|
||||
player = session.get_player()
|
||||
puppet = session.get_puppet()
|
||||
location = puppet.location.key if puppet else "None"
|
||||
table.add_row([utils.crop(player.name, width=25),
|
||||
utils.time_format(delta_conn, 0),
|
||||
utils.time_format(delta_cmd, 1),
|
||||
utils.crop(puppet.key if puppet else "None", width=25),
|
||||
utils.crop(location, width=25),
|
||||
session.cmd_total,
|
||||
session.protocol_key,
|
||||
isinstance(session.address, tuple) and session.address[0] or session.address])
|
||||
else:
|
||||
# unprivileged
|
||||
table = prettytable.PrettyTable(["{wPlayer name", "{wOn for", "{wIdle"])
|
||||
for session in session_list:
|
||||
if not session.logged_in:
|
||||
continue
|
||||
delta_cmd = time.time() - session.cmd_last_visible
|
||||
delta_conn = time.time() - session.conn_time
|
||||
player = session.get_player()
|
||||
table.add_row([utils.crop(player.key, width=25),
|
||||
utils.time_format(delta_conn, 0),
|
||||
utils.time_format(delta_cmd, 1)])
|
||||
|
||||
isone = nplayers == 1
|
||||
string = "{wPlayers:{n\n%s\n%s unique account%s logged in." % (table, "One" if isone else nplayers, "" if isone else "s")
|
||||
self.msg(string)
|
||||
|
||||
|
||||
class CmdEncoding(MuxPlayerCommand):
|
||||
"""
|
||||
set which text encoding to use
|
||||
|
||||
Usage:
|
||||
@encoding/switches [<encoding>]
|
||||
|
||||
Switches:
|
||||
clear - clear your custom encoding
|
||||
|
||||
|
||||
This sets the text encoding for communicating with Evennia. This is mostly
|
||||
an issue only if you want to use non-ASCII characters (i.e. letters/symbols
|
||||
not found in English). If you see that your characters look strange (or you
|
||||
get encoding errors), you should use this command to set the server
|
||||
encoding to be the same used in your client program.
|
||||
|
||||
Common encodings are utf-8 (default), latin-1, ISO-8859-1 etc.
|
||||
|
||||
If you don't submit an encoding, the current encoding will be displayed
|
||||
instead.
|
||||
"""
|
||||
|
||||
key = "@encoding"
|
||||
aliases = "@encode"
|
||||
locks = "cmd:all()"
|
||||
|
||||
def func(self):
|
||||
"""
|
||||
Sets the encoding.
|
||||
"""
|
||||
|
||||
if self.session is None:
|
||||
return
|
||||
|
||||
if 'clear' in self.switches:
|
||||
# remove customization
|
||||
old_encoding = self.session.encoding
|
||||
if old_encoding:
|
||||
string = "Your custom text encoding ('%s') was cleared." % old_encoding
|
||||
else:
|
||||
string = "No custom encoding was set."
|
||||
self.session.encoding = "utf-8"
|
||||
elif not self.args:
|
||||
# just list the encodings supported
|
||||
pencoding = self.session.encoding
|
||||
string = ""
|
||||
if pencoding:
|
||||
string += "Default encoding: {g%s{n (change with {w@encoding <encoding>{n)" % pencoding
|
||||
encodings = settings.ENCODINGS
|
||||
if encodings:
|
||||
string += "\nServer's alternative encodings (tested in this order):\n {g%s{n" % ", ".join(encodings)
|
||||
if not string:
|
||||
string = "No encodings found."
|
||||
else:
|
||||
# change encoding
|
||||
old_encoding = self.session.encoding
|
||||
encoding = self.args
|
||||
self.session.encoding = encoding
|
||||
string = "Your custom text encoding was changed from '%s' to '%s'." % (old_encoding, encoding)
|
||||
self.msg(string.strip())
|
||||
|
||||
|
||||
class CmdPassword(MuxPlayerCommand):
|
||||
"""
|
||||
change your password
|
||||
|
||||
Usage:
|
||||
@password <old password> = <new password>
|
||||
|
||||
Changes your password. Make sure to pick a safe one.
|
||||
"""
|
||||
key = "@password"
|
||||
locks = "cmd:pperm(Players)"
|
||||
|
||||
def func(self):
|
||||
"hook function."
|
||||
|
||||
player = self.player
|
||||
if not self.rhs:
|
||||
self.msg("Usage: @password <oldpass> = <newpass>")
|
||||
return
|
||||
oldpass = self.lhslist[0] # this is already stripped by parse()
|
||||
newpass = self.rhslist[0] # ''
|
||||
if not player.check_password(oldpass):
|
||||
self.msg("The specified old password isn't correct.")
|
||||
elif len(newpass) < 3:
|
||||
self.msg("Passwords must be at least three characters long.")
|
||||
else:
|
||||
player.set_password(newpass)
|
||||
player.save()
|
||||
self.msg("Password changed.")
|
||||
|
||||
|
||||
class CmdQuit(MuxPlayerCommand):
|
||||
"""
|
||||
quit the game
|
||||
|
||||
Usage:
|
||||
@quit
|
||||
|
||||
Switch:
|
||||
all - disconnect all connected sessions
|
||||
|
||||
Gracefully disconnect your current session from the
|
||||
game. Use the /all switch to disconnect from all sessions.
|
||||
"""
|
||||
key = "@quit"
|
||||
locks = "cmd:all()"
|
||||
|
||||
def func(self):
|
||||
"hook function"
|
||||
player = self.player
|
||||
|
||||
if 'all' in self.switches:
|
||||
player.msg("{RQuitting{n all sessions. Hope to see you soon again.", sessid=self.sessid)
|
||||
for session in player.get_all_sessions():
|
||||
player.disconnect_session_from_player(session.sessid)
|
||||
else:
|
||||
nsess = len(player.get_all_sessions())
|
||||
if nsess == 2:
|
||||
player.msg("{RQuitting{n. One session is still connected.", sessid=self.sessid)
|
||||
elif nsess > 2:
|
||||
player.msg("{RQuitting{n. %i session are still connected." % (nsess-1), sessid=self.sessid)
|
||||
else:
|
||||
# we are quitting the last available session
|
||||
player.msg("{RQuitting{n. Hope to see you soon again.", sessid=self.sessid)
|
||||
player.disconnect_session_from_player(self.sessid)
|
||||
|
||||
|
||||
|
||||
class CmdColorTest(MuxPlayerCommand):
|
||||
"""
|
||||
testing which colors your client support
|
||||
|
||||
Usage:
|
||||
@color ansi|xterm256
|
||||
|
||||
Prints a color map along with in-mud color codes to use to produce
|
||||
them. It also tests what is supported in your client. Choices are
|
||||
16-color ansi (supported in most muds) or the 256-color xterm256
|
||||
standard. No checking is done to determine your client supports
|
||||
color - if not you will see rubbish appear.
|
||||
"""
|
||||
key = "@color"
|
||||
aliases = "color"
|
||||
locks = "cmd:all()"
|
||||
help_category = "General"
|
||||
|
||||
def table_format(self, table):
|
||||
"""
|
||||
Helper method to format the ansi/xterm256 tables.
|
||||
Takes a table of columns [[val,val,...],[val,val,...],...]
|
||||
"""
|
||||
if not table:
|
||||
return [[]]
|
||||
|
||||
extra_space = 1
|
||||
max_widths = [max([len(str(val)) for val in col]) for col in table]
|
||||
ftable = []
|
||||
for irow in range(len(table[0])):
|
||||
ftable.append([str(col[irow]).ljust(max_widths[icol]) + " " * extra_space
|
||||
for icol, col in enumerate(table)])
|
||||
return ftable
|
||||
|
||||
def func(self):
|
||||
"Show color tables"
|
||||
|
||||
if self.args.startswith("a"):
|
||||
# show ansi 16-color table
|
||||
from src.utils import ansi
|
||||
ap = ansi.ANSI_PARSER
|
||||
# ansi colors
|
||||
# show all ansi color-related codes
|
||||
col1 = ["%s%s{n" % (code, code.replace("{", "{{")) for code, _ in ap.ext_ansi_map[6:14]]
|
||||
col2 = ["%s%s{n" % (code, code.replace("{", "{{")) for code, _ in ap.ext_ansi_map[14:22]]
|
||||
col3 = ["%s%s{n" % (code.replace("\\",""), code.replace("{", "{{").replace("\\", "")) for code, _ in ap.ext_ansi_map[-8:]]
|
||||
col2.extend(["" for i in range(len(col1)-len(col2))])
|
||||
#hi = "%ch"
|
||||
#col2 = ["%s%s{n" % (code, code.replace("%", "%%")) for code, _ in ap.mux_ansi_map[6:]]
|
||||
#col3 = ["%s%s{n" % (hi + code, (hi + code).replace("%", "%%")) for code, _ in ap.mux_ansi_map[3:-2]]
|
||||
table = utils.format_table([col1, col2, col3])
|
||||
string = "ANSI colors:"
|
||||
for row in table:
|
||||
string += "\n " + " ".join(row)
|
||||
#print string
|
||||
self.msg(string)
|
||||
self.msg("{{X : black. {{/ : return, {{- : tab, {{_ : space, {{* : invert")
|
||||
self.msg("To combine background and foreground, add background marker last, e.g. {{r{{[b.")
|
||||
|
||||
elif self.args.startswith("x"):
|
||||
# show xterm256 table
|
||||
table = [[], [], [], [], [], [], [], [], [], [], [], []]
|
||||
for ir in range(6):
|
||||
for ig in range(6):
|
||||
for ib in range(6):
|
||||
# foreground table
|
||||
table[ir].append("{%i%i%i%s{n" % (ir, ig, ib, "{{%i%i%i" % (ir, ig, ib)))
|
||||
# background table
|
||||
table[6+ir].append("{[%i%i%i{%i%i%i%s{n" % (ir, ig, ib,
|
||||
5 - ir, 5 - ig, 5 - ib,
|
||||
"{{[%i%i%i" % (ir, ig, ib)))
|
||||
table = self.table_format(table)
|
||||
string = "Xterm256 colors (if not all hues show, your client might not report that it can handle xterm256):"
|
||||
for row in table:
|
||||
string += "\n" + "".join(row)
|
||||
self.msg(string)
|
||||
#self.msg("(e.g. %%123 and %%[123 also work)")
|
||||
else:
|
||||
# malformed input
|
||||
self.msg("Usage: @color ansi|xterm256")
|
||||
|
||||
|
||||
class CmdQuell(MuxPlayerCommand):
|
||||
"""
|
||||
use character's permissions instead of player's
|
||||
|
||||
Usage:
|
||||
quell
|
||||
unquell
|
||||
|
||||
Normally the permission level of the Player is used when puppeting a
|
||||
Character/Object to determine access. This command will switch the lock
|
||||
system to make use of the puppeted Object's permissions instead. This is
|
||||
useful mainly for testing.
|
||||
Hierarchical permission quelling only work downwards, thus a Player cannot
|
||||
use a higher-permission Character to escalate their permission level.
|
||||
Use the unquell command to revert back to normal operation.
|
||||
"""
|
||||
|
||||
key = "@quell"
|
||||
aliases = ["@unquell"]
|
||||
locks = "cmd:pperm(Players)"
|
||||
help_category = "General"
|
||||
|
||||
def _recache_locks(self, player):
|
||||
"Helper method to reset the lockhandler on an already puppeted object"
|
||||
if self.sessid:
|
||||
char = player.get_puppet(self.sessid)
|
||||
if char:
|
||||
# we are already puppeting an object. We need to reset
|
||||
# the lock caches (otherwise the superuser status change
|
||||
# won't be visible until repuppet)
|
||||
char.locks.reset()
|
||||
player.locks.reset()
|
||||
|
||||
def func(self):
|
||||
"Perform the command"
|
||||
player = self.player
|
||||
permstr = player.is_superuser and " (superuser)" or " (%s)" % (", ".join(player.permissions.all()))
|
||||
if self.cmdstring == '@unquell':
|
||||
if not player.attributes.get('_quell'):
|
||||
self.msg("Already using normal Player permissions%s." % permstr)
|
||||
else:
|
||||
player.attributes.remove('_quell')
|
||||
self.msg("Player permissions%s restored." % permstr)
|
||||
else:
|
||||
if player.attributes.get('_quell'):
|
||||
self.msg("Already quelling Player%s permissions." % permstr)
|
||||
return
|
||||
player.attributes.add('_quell', True)
|
||||
puppet = player.get_puppet(self.sessid)
|
||||
if puppet:
|
||||
cpermstr = " (%s)" % ", ".join(puppet.permissions.all())
|
||||
cpermstr = "Quelling to current puppet's permissions%s." % cpermstr
|
||||
cpermstr += "\n(Note: If this is higher than Player permissions%s, the lowest of the two will be used.)" % permstr
|
||||
cpermstr += "\nUse @unquell to return to normal permission usage."
|
||||
self.msg(cpermstr)
|
||||
else:
|
||||
self.msg("Quelling Player permissions%s. Use @unquell to get them back." % permstr)
|
||||
self._recache_locks(player)
|
||||
|
||||
173
lib/commands/default/syscommands.py
Normal file
173
lib/commands/default/syscommands.py
Normal file
|
|
@ -0,0 +1,173 @@
|
|||
"""
|
||||
System commands
|
||||
|
||||
These are the default commands called by the system commandhandler
|
||||
when various exceptions occur. If one of these commands are not
|
||||
implemented and part of the current cmdset, the engine falls back
|
||||
to a default solution instead.
|
||||
|
||||
Some system commands are shown in this module
|
||||
as a REFERENCE only (they are not all added to Evennia's
|
||||
default cmdset since they don't currently do anything differently from the
|
||||
default backup systems hard-wired in the engine).
|
||||
|
||||
Overloading these commands in a cmdset can be used to create
|
||||
interesting effects. An example is using the NoMatch system command
|
||||
to implement a line-editor where you don't have to start each
|
||||
line with a command (if there is no match to a known command,
|
||||
the line is just added to the editor buffer).
|
||||
"""
|
||||
|
||||
from src.comms.models import ChannelDB
|
||||
from src.utils import create
|
||||
|
||||
# The command keys the engine is calling
|
||||
# (the actual names all start with __)
|
||||
from src.commands.cmdhandler import CMD_NOINPUT
|
||||
from src.commands.cmdhandler import CMD_NOMATCH
|
||||
from src.commands.cmdhandler import CMD_MULTIMATCH
|
||||
from src.commands.cmdhandler import CMD_CHANNEL
|
||||
|
||||
from src.commands.default.muxcommand import MuxCommand
|
||||
|
||||
# Command called when there is no input at line
|
||||
# (i.e. an lone return key)
|
||||
|
||||
|
||||
class SystemNoInput(MuxCommand):
|
||||
"""
|
||||
This is called when there is no input given
|
||||
"""
|
||||
key = CMD_NOINPUT
|
||||
locks = "cmd:all()"
|
||||
|
||||
def func(self):
|
||||
"Do nothing."
|
||||
pass
|
||||
|
||||
|
||||
#
|
||||
# Command called when there was no match to the
|
||||
# command name
|
||||
#
|
||||
class SystemNoMatch(MuxCommand):
|
||||
"""
|
||||
No command was found matching the given input.
|
||||
"""
|
||||
key = CMD_NOMATCH
|
||||
locks = "cmd:all()"
|
||||
|
||||
def func(self):
|
||||
"""
|
||||
This is given the failed raw string as input.
|
||||
"""
|
||||
self.caller.msg("Huh?")
|
||||
|
||||
|
||||
#
|
||||
# Command called when there were mulitple matches to the command.
|
||||
#
|
||||
class SystemMultimatch(MuxCommand):
|
||||
"""
|
||||
Multiple command matches.
|
||||
|
||||
The cmdhandler adds a special attribute 'matches' to this
|
||||
system command.
|
||||
|
||||
matches = [(candidate, cmd) , (candidate, cmd), ...],
|
||||
|
||||
where candidate is an instance of src.commands.cmdparser.CommandCandidate
|
||||
and cmd is an an instantiated Command object matching the candidate.
|
||||
"""
|
||||
key = CMD_MULTIMATCH
|
||||
locks = "cmd:all()"
|
||||
|
||||
def format_multimatches(self, caller, matches):
|
||||
"""
|
||||
Format multiple command matches to a useful error.
|
||||
|
||||
This is copied directly from the default method in
|
||||
src.commands.cmdhandler.
|
||||
|
||||
"""
|
||||
string = "There were multiple matches:"
|
||||
for num, match in enumerate(matches):
|
||||
# each match is a tuple (candidate, cmd)
|
||||
candidate, cmd = match
|
||||
|
||||
is_channel = hasattr(cmd, "is_channel") and cmd.is_channel
|
||||
if is_channel:
|
||||
is_channel = " (channel)"
|
||||
else:
|
||||
is_channel = ""
|
||||
is_exit = hasattr(cmd, "is_exit") and cmd.is_exit
|
||||
if is_exit and cmd.destination:
|
||||
is_exit = " (exit to %s)" % cmd.destination
|
||||
else:
|
||||
is_exit = ""
|
||||
|
||||
id1 = ""
|
||||
id2 = ""
|
||||
if not (is_channel or is_exit) and (hasattr(cmd, 'obj') and cmd.obj != caller):
|
||||
# the command is defined on some other object
|
||||
id1 = "%s-" % cmd.obj.name
|
||||
id2 = " (%s-%s)" % (num + 1, candidate.cmdname)
|
||||
else:
|
||||
id1 = "%s-" % (num + 1)
|
||||
id2 = ""
|
||||
string += "\n %s%s%s%s%s" % (id1, candidate.cmdname, id2, is_channel, is_exit)
|
||||
return string
|
||||
|
||||
def func(self):
|
||||
"""
|
||||
argument to cmd is a comma-separated string of
|
||||
all the clashing matches.
|
||||
"""
|
||||
string = self.format_multimatches(self.caller, self.matches)
|
||||
self.caller.msg(string)
|
||||
|
||||
|
||||
# Command called when the command given at the command line
|
||||
# was identified as a channel name, like there existing a
|
||||
# channel named 'ooc' and the user wrote
|
||||
# > ooc Hello!
|
||||
|
||||
class SystemSendToChannel(MuxCommand):
|
||||
"""
|
||||
This is a special command that the cmdhandler calls
|
||||
when it detects that the command given matches
|
||||
an existing Channel object key (or alias).
|
||||
"""
|
||||
|
||||
key = CMD_CHANNEL
|
||||
locks = "cmd:all()"
|
||||
|
||||
def parse(self):
|
||||
channelname, msg = self.args.split(':', 1)
|
||||
self.args = channelname.strip(), msg.strip()
|
||||
|
||||
def func(self):
|
||||
"""
|
||||
Create a new message and send it to channel, using
|
||||
the already formatted input.
|
||||
"""
|
||||
caller = self.caller
|
||||
channelkey, msg = self.args
|
||||
if not msg:
|
||||
caller.msg("Say what?")
|
||||
return
|
||||
channel = ChannelDB.objects.get_channel(channelkey)
|
||||
if not channel:
|
||||
caller.msg("Channel '%s' not found." % channelkey)
|
||||
return
|
||||
if not channel.has_connection(caller):
|
||||
string = "You are not connected to channel '%s'."
|
||||
caller.msg(string % channelkey)
|
||||
return
|
||||
if not channel.access(caller, 'send'):
|
||||
string = "You are not permitted to send to channel '%s'."
|
||||
caller.msg(string % channelkey)
|
||||
return
|
||||
msg = "[%s] %s: %s" % (channel.key, caller.name, msg)
|
||||
msgobj = create.create_message(caller, msg, channels=[channel])
|
||||
channel.msg(msgobj)
|
||||
725
lib/commands/default/system.py
Normal file
725
lib/commands/default/system.py
Normal file
|
|
@ -0,0 +1,725 @@
|
|||
"""
|
||||
|
||||
System commands
|
||||
|
||||
"""
|
||||
|
||||
import traceback
|
||||
import os
|
||||
import datetime
|
||||
import sys
|
||||
import django
|
||||
import twisted
|
||||
from time import time as timemeasure
|
||||
|
||||
from django.conf import settings
|
||||
#from src.server.caches import get_cache_sizes
|
||||
from src.server.sessionhandler import SESSIONS
|
||||
from src.scripts.models import ScriptDB
|
||||
from src.objects.models import ObjectDB
|
||||
from src.players.models import PlayerDB
|
||||
from src.utils import logger, utils, gametime, create, is_pypy, prettytable
|
||||
from src.utils.evtable import EvTable
|
||||
from src.utils.utils import crop
|
||||
from src.commands.default.muxcommand import MuxCommand
|
||||
|
||||
# delayed imports
|
||||
_resource = None
|
||||
_idmapper = None
|
||||
_attribute_cache = None
|
||||
|
||||
# limit symbol import for API
|
||||
__all__ = ("CmdReload", "CmdReset", "CmdShutdown", "CmdPy",
|
||||
"CmdScripts", "CmdObjects", "CmdService", "CmdAbout",
|
||||
"CmdTime", "CmdServerLoad")
|
||||
|
||||
|
||||
class CmdReload(MuxCommand):
|
||||
"""
|
||||
reload the server
|
||||
|
||||
Usage:
|
||||
@reload [reason]
|
||||
|
||||
This restarts the server. The Portal is not
|
||||
affected. Non-persistent scripts will survive a @reload (use
|
||||
@reset to purge) and at_reload() hooks will be called.
|
||||
"""
|
||||
key = "@reload"
|
||||
locks = "cmd:perm(reload) or perm(Immortals)"
|
||||
help_category = "System"
|
||||
|
||||
def func(self):
|
||||
"""
|
||||
Reload the system.
|
||||
"""
|
||||
reason = ""
|
||||
if self.args:
|
||||
reason = "(Reason: %s) " % self.args.rstrip(".")
|
||||
SESSIONS.announce_all(" Server restarting %s..." % reason)
|
||||
SESSIONS.server.shutdown(mode='reload')
|
||||
|
||||
|
||||
class CmdReset(MuxCommand):
|
||||
"""
|
||||
reset and reboot the server
|
||||
|
||||
Usage:
|
||||
@reset
|
||||
|
||||
A cold reboot. This works like a mixture of @reload and @shutdown,
|
||||
- all shutdown hooks will be called and non-persistent scrips will
|
||||
be purged. But the Portal will not be affected and the server will
|
||||
automatically restart again.
|
||||
"""
|
||||
key = "@reset"
|
||||
aliases = ['@reboot']
|
||||
locks = "cmd:perm(reload) or perm(Immortals)"
|
||||
help_category = "System"
|
||||
|
||||
def func(self):
|
||||
"""
|
||||
Reload the system.
|
||||
"""
|
||||
SESSIONS.announce_all(" Server resetting/restarting ...")
|
||||
SESSIONS.server.shutdown(mode='reset')
|
||||
|
||||
|
||||
class CmdShutdown(MuxCommand):
|
||||
|
||||
"""
|
||||
stop the server completely
|
||||
|
||||
Usage:
|
||||
@shutdown [announcement]
|
||||
|
||||
Gracefully shut down both Server and Portal.
|
||||
"""
|
||||
key = "@shutdown"
|
||||
locks = "cmd:perm(shutdown) or perm(Immortals)"
|
||||
help_category = "System"
|
||||
|
||||
def func(self):
|
||||
"Define function"
|
||||
try:
|
||||
# Only allow shutdown if caller has session
|
||||
self.caller.sessions[0]
|
||||
except Exception:
|
||||
return
|
||||
self.msg('Shutting down server ...')
|
||||
announcement = "\nServer is being SHUT DOWN!\n"
|
||||
if self.args:
|
||||
announcement += "%s\n" % self.args
|
||||
logger.log_infomsg('Server shutdown by %s.' % self.caller.name)
|
||||
SESSIONS.announce_all(announcement)
|
||||
SESSIONS.portal_shutdown()
|
||||
SESSIONS.server.shutdown(mode='shutdown')
|
||||
|
||||
|
||||
class CmdPy(MuxCommand):
|
||||
"""
|
||||
execute a snippet of python code
|
||||
|
||||
Usage:
|
||||
@py <cmd>
|
||||
|
||||
Switch:
|
||||
time - output an approximate execution time for <cmd>
|
||||
|
||||
Separate multiple commands by ';'. A few variables are made
|
||||
available for convenience in order to offer access to the system
|
||||
(you can import more at execution time).
|
||||
|
||||
Available variables in @py environment:
|
||||
self, me : caller
|
||||
here : caller.location
|
||||
ev : the evennia API
|
||||
inherits_from(obj, parent) : check object inheritance
|
||||
|
||||
You can explore The evennia API from inside the game by calling
|
||||
ev.help(), ev.managers.help() etc.
|
||||
|
||||
{rNote: In the wrong hands this command is a severe security risk.
|
||||
It should only be accessible by trusted server admins/superusers.{n
|
||||
|
||||
"""
|
||||
key = "@py"
|
||||
aliases = ["!"]
|
||||
locks = "cmd:perm(py) or perm(Immortals)"
|
||||
help_category = "System"
|
||||
|
||||
def func(self):
|
||||
"hook function"
|
||||
|
||||
caller = self.caller
|
||||
pycode = self.args
|
||||
|
||||
if not pycode:
|
||||
string = "Usage: @py <code>"
|
||||
self.msg(string)
|
||||
return
|
||||
|
||||
# check if caller is a player
|
||||
|
||||
# import useful variables
|
||||
import ev
|
||||
available_vars = {'self': caller,
|
||||
'me': caller,
|
||||
'here': hasattr(caller, "location") and caller.location or None,
|
||||
'ev': ev,
|
||||
'inherits_from': utils.inherits_from}
|
||||
|
||||
try:
|
||||
self.msg(">>> %s" % pycode, raw=True, sessid=self.sessid)
|
||||
except TypeError:
|
||||
self.msg(">>> %s" % pycode, raw=True)
|
||||
|
||||
mode = "eval"
|
||||
try:
|
||||
try:
|
||||
pycode_compiled = compile(pycode, "", mode)
|
||||
except Exception:
|
||||
mode = "exec"
|
||||
pycode_compiled = compile(pycode, "", mode)
|
||||
|
||||
duration = ""
|
||||
if "time" in self.switches:
|
||||
t0 = timemeasure()
|
||||
ret = eval(pycode_compiled, {}, available_vars)
|
||||
t1 = timemeasure()
|
||||
duration = " (%.4f ms)" % ((t1 - t0) * 1000)
|
||||
else:
|
||||
ret = eval(pycode_compiled, {}, available_vars)
|
||||
if mode == "eval":
|
||||
ret = "{n<<< %s%s" % (str(ret), duration)
|
||||
else:
|
||||
ret = "{n<<< Done.%s" % duration
|
||||
except Exception:
|
||||
errlist = traceback.format_exc().split('\n')
|
||||
if len(errlist) > 4:
|
||||
errlist = errlist[4:]
|
||||
ret = "\n".join("{n<<< %s" % line for line in errlist if line)
|
||||
|
||||
try:
|
||||
self.msg(ret, sessid=self.sessid)
|
||||
except TypeError:
|
||||
self.msg(ret)
|
||||
|
||||
|
||||
# helper function. Kept outside so it can be imported and run
|
||||
# by other commands.
|
||||
|
||||
def format_script_list(scripts):
|
||||
"Takes a list of scripts and formats the output."
|
||||
if not scripts:
|
||||
return "<No scripts>"
|
||||
|
||||
table = EvTable("{wdbref{n", "{wobj{n", "{wkey{n", "{wintval{n", "{wnext{n",
|
||||
"{wrept{n", "{wdb", "{wtypeclass{n", "{wdesc{n",
|
||||
align='r', border="tablecols")
|
||||
for script in scripts:
|
||||
nextrep = script.time_until_next_repeat()
|
||||
if nextrep is None:
|
||||
nextrep = "PAUS" if script.db._paused_time else "--"
|
||||
else:
|
||||
nextrep = "%ss" % nextrep
|
||||
|
||||
maxrepeat = script.repeats
|
||||
if maxrepeat:
|
||||
rept = "%i/%i" % (maxrepeat - script.remaining_repeats(), maxrepeat)
|
||||
else:
|
||||
rept = "-/-"
|
||||
|
||||
table.add_row(script.id,
|
||||
script.obj.key if (hasattr(script, 'obj') and script.obj) else "<Global>",
|
||||
script.key,
|
||||
script.interval if script.interval > 0 else "--",
|
||||
nextrep,
|
||||
rept,
|
||||
"*" if script.persistent else "-",
|
||||
script.typeclass_path.rsplit('.', 1)[-1],
|
||||
crop(script.desc, width=20))
|
||||
return "%s" % table
|
||||
|
||||
|
||||
class CmdScripts(MuxCommand):
|
||||
"""
|
||||
list and manage all running scripts
|
||||
|
||||
Usage:
|
||||
@scripts[/switches] [#dbref, key, script.path or <obj>]
|
||||
|
||||
Switches:
|
||||
start - start a script (must supply a script path)
|
||||
stop - stops an existing script
|
||||
kill - kills a script - without running its cleanup hooks
|
||||
validate - run a validation on the script(s)
|
||||
|
||||
If no switches are given, this command just views all active
|
||||
scripts. The argument can be either an object, at which point it
|
||||
will be searched for all scripts defined on it, or an script name
|
||||
or #dbref. For using the /stop switch, a unique script #dbref is
|
||||
required since whole classes of scripts often have the same name.
|
||||
|
||||
Use @script for managing commands on objects.
|
||||
"""
|
||||
key = "@scripts"
|
||||
aliases = ["@globalscript", "@listscripts"]
|
||||
locks = "cmd:perm(listscripts) or perm(Wizards)"
|
||||
help_category = "System"
|
||||
|
||||
def func(self):
|
||||
"implement method"
|
||||
|
||||
caller = self.caller
|
||||
args = self.args
|
||||
|
||||
string = ""
|
||||
if args:
|
||||
if "start" in self.switches:
|
||||
# global script-start mode
|
||||
new_script = create.create_script(args)
|
||||
if new_script:
|
||||
caller.msg("Global script %s was started successfully." % args)
|
||||
else:
|
||||
caller.msg("Global script %s could not start correctly. See logs." % args)
|
||||
return
|
||||
|
||||
# test first if this is a script match
|
||||
scripts = ScriptDB.objects.get_all_scripts(key=args)
|
||||
if not scripts:
|
||||
# try to find an object instead.
|
||||
objects = ObjectDB.objects.object_search(args)
|
||||
if objects:
|
||||
scripts = []
|
||||
for obj in objects:
|
||||
# get all scripts on the object(s)
|
||||
scripts.extend(ScriptDB.objects.get_all_scripts_on_obj(obj))
|
||||
else:
|
||||
# we want all scripts.
|
||||
scripts = ScriptDB.objects.get_all_scripts()
|
||||
if not scripts:
|
||||
caller.msg("No scripts are running.")
|
||||
return
|
||||
|
||||
if not scripts:
|
||||
string = "No scripts found with a key '%s', or on an object named '%s'." % (args, args)
|
||||
caller.msg(string)
|
||||
return
|
||||
|
||||
if self.switches and self.switches[0] in ('stop', 'del', 'delete', 'kill'):
|
||||
# we want to delete something
|
||||
if not scripts:
|
||||
string = "No scripts/objects matching '%s'. " % args
|
||||
string += "Be more specific."
|
||||
elif len(scripts) == 1:
|
||||
# we have a unique match!
|
||||
if 'kill' in self.switches:
|
||||
string = "Killing script '%s'" % scripts[0].key
|
||||
scripts[0].stop(kill=True)
|
||||
else:
|
||||
string = "Stopping script '%s'." % scripts[0].key
|
||||
scripts[0].stop()
|
||||
#import pdb
|
||||
#pdb.set_trace()
|
||||
ScriptDB.objects.validate() #just to be sure all is synced
|
||||
else:
|
||||
# multiple matches.
|
||||
string = "Multiple script matches. Please refine your search:\n"
|
||||
string += format_script_list(scripts)
|
||||
elif self.switches and self.switches[0] in ("validate", "valid", "val"):
|
||||
# run validation on all found scripts
|
||||
nr_started, nr_stopped = ScriptDB.objects.validate(scripts=scripts)
|
||||
string = "Validated %s scripts. " % ScriptDB.objects.all().count()
|
||||
string += "Started %s and stopped %s scripts." % (nr_started, nr_stopped)
|
||||
else:
|
||||
# No stopping or validation. We just want to view things.
|
||||
string = format_script_list(scripts)
|
||||
caller.msg(string)
|
||||
|
||||
|
||||
class CmdObjects(MuxCommand):
|
||||
"""
|
||||
statistics on objects in the database
|
||||
|
||||
Usage:
|
||||
@objects [<nr>]
|
||||
|
||||
Gives statictics on objects in database as well as
|
||||
a list of <nr> latest objects in database. If not
|
||||
given, <nr> defaults to 10.
|
||||
"""
|
||||
key = "@objects"
|
||||
aliases = ["@listobjects", "@listobjs", '@stats', '@db']
|
||||
locks = "cmd:perm(listobjects) or perm(Builders)"
|
||||
help_category = "System"
|
||||
|
||||
def func(self):
|
||||
"Implement the command"
|
||||
|
||||
caller = self.caller
|
||||
|
||||
if self.args and self.args.isdigit():
|
||||
nlim = int(self.args)
|
||||
else:
|
||||
nlim = 10
|
||||
|
||||
nobjs = ObjectDB.objects.count()
|
||||
base_char_typeclass = settings.BASE_CHARACTER_TYPECLASS
|
||||
nchars = ObjectDB.objects.filter(db_typeclass_path=base_char_typeclass).count()
|
||||
nrooms = ObjectDB.objects.filter(db_location__isnull=True).exclude(db_typeclass_path=base_char_typeclass).count()
|
||||
nexits = ObjectDB.objects.filter(db_location__isnull=False, db_destination__isnull=False).count()
|
||||
nother = nobjs - nchars - nrooms - nexits
|
||||
|
||||
nobjs = nobjs or 1 # fix zero-div error with empty database
|
||||
|
||||
# total object sum table
|
||||
totaltable = EvTable("{wtype{n", "{wcomment{n", "{wcount{n", "{w%%{n", border="table", align="l")
|
||||
totaltable.align = 'l'
|
||||
totaltable.add_row("Characters", "(BASE_CHARACTER_TYPECLASS)", nchars, "%.2f" % ((float(nchars) / nobjs) * 100))
|
||||
totaltable.add_row("Rooms", "(location=None)", nrooms, "%.2f" % ((float(nrooms) / nobjs) * 100))
|
||||
totaltable.add_row("Exits", "(destination!=None)", nexits, "%.2f" % ((float(nexits) / nobjs) * 100))
|
||||
totaltable.add_row("Other", "", nother, "%.2f" % ((float(nother) / nobjs) * 100))
|
||||
|
||||
# typeclass table
|
||||
typetable = EvTable("{wtypeclass{n", "{wcount{n", "{w%%{n", border="table", align="l")
|
||||
typetable.align = 'l'
|
||||
dbtotals = ObjectDB.objects.object_totals()
|
||||
for path, count in dbtotals.items():
|
||||
typetable.add_row(path, count, "%.2f" % ((float(count) / nobjs) * 100))
|
||||
|
||||
# last N table
|
||||
objs = ObjectDB.objects.all().order_by("db_date_created")[max(0, nobjs - nlim):]
|
||||
latesttable = EvTable("{wcreated{n", "{wdbref{n", "{wname{n", "{wtypeclass{n", align="l", border="table")
|
||||
latesttable.align = 'l'
|
||||
for obj in objs:
|
||||
latesttable.add_row(utils.datetime_format(obj.date_created),
|
||||
obj.dbref, obj.key, obj.path)
|
||||
|
||||
string = "\n{wObject subtype totals (out of %i Objects):{n\n%s" % (nobjs, totaltable)
|
||||
string += "\n{wObject typeclass distribution:{n\n%s" % typetable
|
||||
string += "\n{wLast %s Objects created:{n\n%s" % (min(nobjs, nlim), latesttable)
|
||||
caller.msg(string)
|
||||
|
||||
|
||||
class CmdPlayers(MuxCommand):
|
||||
"""
|
||||
list all registered players
|
||||
|
||||
Usage:
|
||||
@players [nr]
|
||||
|
||||
Lists statistics about the Players registered with the game.
|
||||
It will list the <nr> amount of latest registered players
|
||||
If not given, <nr> defaults to 10.
|
||||
"""
|
||||
key = "@players"
|
||||
aliases = ["@listplayers"]
|
||||
locks = "cmd:perm(listplayers) or perm(Wizards)"
|
||||
|
||||
def func(self):
|
||||
"List the players"
|
||||
|
||||
caller = self.caller
|
||||
if self.args and self.args.is_digit():
|
||||
nlim = int(self.args)
|
||||
else:
|
||||
nlim = 10
|
||||
|
||||
nplayers = PlayerDB.objects.count()
|
||||
|
||||
# typeclass table
|
||||
dbtotals = PlayerDB.objects.object_totals()
|
||||
typetable = EvTable("{wtypeclass{n", "{wcount{n", "{w%%{n", border="cells", align="l")
|
||||
for path, count in dbtotals.items():
|
||||
typetable.add_row(path, count, "%.2f" % ((float(count) / nplayers) * 100))
|
||||
# last N table
|
||||
plyrs = PlayerDB.objects.all().order_by("db_date_created")[max(0, nplayers - nlim):]
|
||||
latesttable = EvTable("{wcreated{n", "{wdbref{n", "{wname{n", "{wtypeclass{n", border="cells", align="l")
|
||||
for ply in plyrs:
|
||||
latesttable.add_row(utils.datetime_format(ply.date_created), ply.dbref, ply.key, ply.path)
|
||||
|
||||
string = "\n{wPlayer typeclass distribution:{n\n%s" % typetable
|
||||
string += "\n{wLast %s Players created:{n\n%s" % (min(nplayers, nlim), latesttable)
|
||||
caller.msg(string)
|
||||
|
||||
|
||||
class CmdService(MuxCommand):
|
||||
"""
|
||||
manage system services
|
||||
|
||||
Usage:
|
||||
@service[/switch] <service>
|
||||
|
||||
Switches:
|
||||
list - shows all available services (default)
|
||||
start - activates or reactivate a service
|
||||
stop - stops/inactivate a service (can often be restarted)
|
||||
delete - tries to permanently remove a service
|
||||
|
||||
Service management system. Allows for the listing,
|
||||
starting, and stopping of services. If no switches
|
||||
are given, services will be listed. Note that to operate on the
|
||||
service you have to supply the full (green or red) name as given
|
||||
in the list.
|
||||
"""
|
||||
|
||||
key = "@service"
|
||||
aliases = ["@services"]
|
||||
locks = "cmd:perm(service) or perm(Immortals)"
|
||||
help_category = "System"
|
||||
|
||||
def func(self):
|
||||
"Implement command"
|
||||
|
||||
caller = self.caller
|
||||
switches = self.switches
|
||||
|
||||
if switches and switches[0] not in ("list", "start", "stop", "delete"):
|
||||
caller.msg("Usage: @service/<list|start|stop|delete> [servicename]")
|
||||
return
|
||||
|
||||
# get all services
|
||||
sessions = caller.sessions
|
||||
if not sessions:
|
||||
return
|
||||
service_collection = SESSIONS.server.services
|
||||
|
||||
if not switches or switches[0] == "list":
|
||||
# Just display the list of installed services and their
|
||||
# status, then exit.
|
||||
table = prettytable.PrettyTable(["{wService{n (use @services/start|stop|delete)", "{wstatus"])
|
||||
table.align = 'l'
|
||||
for service in service_collection.services:
|
||||
table.add_row([service.name, service.running and "{gRunning" or "{rNot Running"])
|
||||
caller.msg(str(table))
|
||||
return
|
||||
|
||||
# Get the service to start / stop
|
||||
|
||||
try:
|
||||
service = service_collection.getServiceNamed(self.args)
|
||||
except Exception:
|
||||
string = 'Invalid service name. This command is case-sensitive. '
|
||||
string += 'See @service/list for valid service name (enter the full name exactly).'
|
||||
caller.msg(string)
|
||||
return
|
||||
|
||||
if switches[0] in ("stop", "delete"):
|
||||
# Stopping/killing a service gracefully closes it and disconnects
|
||||
# any connections (if applicable).
|
||||
|
||||
delmode = switches[0] == "delete"
|
||||
if not service.running:
|
||||
caller.msg('That service is not currently running.')
|
||||
return
|
||||
if service.name[:7] == 'Evennia':
|
||||
if delmode:
|
||||
caller.msg("You cannot remove a core Evennia service (named 'Evennia***').")
|
||||
return
|
||||
string = "You seem to be shutting down a core Evennia service (named 'Evennia***'). Note that"
|
||||
string += "stopping some TCP port services will *not* disconnect users *already*"
|
||||
string += "connected on those ports, but *may* instead cause spurious errors for them. To "
|
||||
string += "safely and permanently remove ports, change settings file and restart the server."
|
||||
caller.msg(string)
|
||||
|
||||
if delmode:
|
||||
service.stopService()
|
||||
service_collection.removeService(service)
|
||||
caller.msg("Stopped and removed service '%s'." % self.args)
|
||||
else:
|
||||
service.stopService()
|
||||
caller.msg("Stopped service '%s'." % self.args)
|
||||
return
|
||||
|
||||
if switches[0] == "start":
|
||||
#Starts a service.
|
||||
if service.running:
|
||||
caller.msg('That service is already running.')
|
||||
return
|
||||
caller.msg("Starting service '%s'." % self.args)
|
||||
service.startService()
|
||||
|
||||
|
||||
class CmdAbout(MuxCommand):
|
||||
"""
|
||||
show Evennia info
|
||||
|
||||
Usage:
|
||||
@about
|
||||
|
||||
Display info about the game engine.
|
||||
"""
|
||||
|
||||
key = "@about"
|
||||
aliases = "@version"
|
||||
locks = "cmd:all()"
|
||||
help_category = "System"
|
||||
|
||||
def func(self):
|
||||
"Show the version"
|
||||
|
||||
string = """
|
||||
{cEvennia{n %s{n
|
||||
MUD/MUX/MU* development system
|
||||
|
||||
{wLicence{n BSD 3-Clause Licence
|
||||
{wWeb{n http://www.evennia.com
|
||||
{wIrc{n #evennia on FreeNode
|
||||
{wForum{n http://www.evennia.com/discussions
|
||||
{wMaintainer{n (2010-) Griatch (griatch AT gmail DOT com)
|
||||
{wMaintainer{n (2006-10) Greg Taylor
|
||||
|
||||
{wOS{n %s
|
||||
{wPython{n %s
|
||||
{wTwisted{n %s
|
||||
{wDjango{n %s
|
||||
""" % (utils.get_evennia_version(),
|
||||
os.name,
|
||||
sys.version.split()[0],
|
||||
twisted.version.short(),
|
||||
django.get_version())
|
||||
self.caller.msg(string)
|
||||
|
||||
|
||||
class CmdTime(MuxCommand):
|
||||
"""
|
||||
show server time statistics
|
||||
|
||||
Usage:
|
||||
@time
|
||||
|
||||
List Server time statistics such as uptime
|
||||
and the current time stamp.
|
||||
"""
|
||||
key = "@time"
|
||||
aliases = "@uptime"
|
||||
locks = "cmd:perm(time) or perm(Players)"
|
||||
help_category = "System"
|
||||
|
||||
def func(self):
|
||||
"Show server time data in a table."
|
||||
table = prettytable.PrettyTable(["{wserver time statistic","{wtime"])
|
||||
table.align = 'l'
|
||||
table.add_row(["Current server uptime", utils.time_format(gametime.uptime(), 3)])
|
||||
table.add_row(["Total server running time", utils.time_format(gametime.runtime(), 2)])
|
||||
table.add_row(["Total in-game time (realtime x %g" % (gametime.TIMEFACTOR), utils.time_format(gametime.gametime(), 2)])
|
||||
table.add_row(["Server time stamp", datetime.datetime.now()])
|
||||
self.caller.msg(str(table))
|
||||
|
||||
|
||||
class CmdServerLoad(MuxCommand):
|
||||
"""
|
||||
show server load and memory statistics
|
||||
|
||||
Usage:
|
||||
@server[/mem]
|
||||
|
||||
Switch:
|
||||
mem - return only a string of the current memory usage
|
||||
flushmem - flush the idmapper cache
|
||||
|
||||
This command shows server load statistics and dynamic memory
|
||||
usage. It also allows to flush the cache of accessed database
|
||||
objects.
|
||||
|
||||
Some Important statistics in the table:
|
||||
|
||||
{wServer load{n is an average of processor usage. It's usually
|
||||
between 0 (no usage) and 1 (100% usage), but may also be
|
||||
temporarily higher if your computer has multiple CPU cores.
|
||||
|
||||
The {wResident/Virtual memory{n displays the total memory used by
|
||||
the server process.
|
||||
|
||||
Evennia {wcaches{n all retrieved database entities when they are
|
||||
loaded by use of the idmapper functionality. This allows Evennia
|
||||
to maintain the same instances of an entity and allowing
|
||||
non-persistent storage schemes. The total amount of cached objects
|
||||
are displayed plus a breakdown of database object types.
|
||||
|
||||
The {wflushmem{n switch allows to flush the object cache. Please
|
||||
note that due to how Python's memory management works, releasing
|
||||
caches may not show you a lower Residual/Virtual memory footprint,
|
||||
the released memory will instead be re-used by the program.
|
||||
|
||||
"""
|
||||
key = "@server"
|
||||
aliases = ["@serverload", "@serverprocess"]
|
||||
locks = "cmd:perm(list) or perm(Immortals)"
|
||||
help_category = "System"
|
||||
|
||||
def func(self):
|
||||
"Show list."
|
||||
|
||||
caller = self.caller
|
||||
|
||||
# display active processes
|
||||
|
||||
if not utils.host_os_is('posix'):
|
||||
string = "Process listings are only available under Linux/Unix."
|
||||
caller.msg(string)
|
||||
return
|
||||
|
||||
global _resource, _idmapper
|
||||
if not _resource:
|
||||
import resource as _resource
|
||||
if not _idmapper:
|
||||
from src.utils.idmapper import base as _idmapper
|
||||
|
||||
import resource
|
||||
loadavg = os.getloadavg()
|
||||
psize = _resource.getpagesize()
|
||||
pid = os.getpid()
|
||||
rmem = float(os.popen('ps -p %d -o %s | tail -1' % (pid, "rss")).read()) / 1000.0 # resident memory
|
||||
vmem = float(os.popen('ps -p %d -o %s | tail -1' % (pid, "vsz")).read()) / 1000.0 # virtual memory
|
||||
pmem = float(os.popen('ps -p %d -o %s | tail -1' % (pid, "%mem")).read()) # percent of resident memory to total
|
||||
rusage = resource.getrusage(resource.RUSAGE_SELF)
|
||||
|
||||
if "mem" in self.switches:
|
||||
caller.msg("Memory usage: RMEM: {w%g{n MB (%g%%), VMEM (res+swap+cache): {w%g{n MB." % (rmem, pmem, vmem))
|
||||
return
|
||||
|
||||
if "flushmem" in self.switches:
|
||||
caller.msg("Flushed object idmapper cache. Python garbage collector recovered memory from %i objects." % _idmapper.flush_cache())
|
||||
return
|
||||
|
||||
# load table
|
||||
loadtable = prettytable.PrettyTable(["property", "statistic"])
|
||||
loadtable.align = 'l'
|
||||
loadtable.add_row(["Server load (1 min)", "%g" % loadavg[0]])
|
||||
loadtable.add_row(["Process ID", "%g" % pid]),
|
||||
loadtable.add_row(["Bytes per page", "%g " % psize])
|
||||
loadtable.add_row(["CPU time used (total)", "%s (%gs)" % (utils.time_format(rusage.ru_utime), rusage.ru_utime)])
|
||||
loadtable.add_row(["CPU time used (user)", "%s (%gs)" % (utils.time_format(rusage.ru_stime), rusage.ru_stime)])
|
||||
loadtable.add_row(["Memory usage","%g MB (%g%%)" % (rmem, pmem)])
|
||||
loadtable.add_row(["Virtual address space\n {x(resident+swap+caching){n", "%g MB" % vmem])
|
||||
loadtable.add_row(["Page faults", "%g hard, %g soft, %g swapouts" % (rusage.ru_majflt, rusage.ru_minflt, rusage.ru_nswap)])
|
||||
loadtable.add_row(["Disk I/O", "%g reads, %g writes" % (rusage.ru_inblock, rusage.ru_oublock)])
|
||||
loadtable.add_row(["Network I/O", "%g in, %g out" % (rusage.ru_msgrcv, rusage.ru_msgsnd)])
|
||||
loadtable.add_row(["Context switching", "%g vol, %g forced, %g signals" % (rusage.ru_nvcsw, rusage.ru_nivcsw, rusage.ru_nsignals)])
|
||||
|
||||
string = "{wServer CPU and Memory load:{n\n%s" % loadtable
|
||||
|
||||
if not is_pypy:
|
||||
# Cache size measurements are not available on PyPy
|
||||
# because it lacks sys.getsizeof
|
||||
|
||||
# object cache size
|
||||
total_num, cachedict = _idmapper.cache_size()
|
||||
sorted_cache = sorted([(key, num) for key, num in cachedict.items() if num > 0],
|
||||
key=lambda tup: tup[1], reverse=True)
|
||||
memtable = prettytable.PrettyTable(["entity name",
|
||||
"number",
|
||||
"idmapper %%"])
|
||||
memtable.align = 'l'
|
||||
for tup in sorted_cache:
|
||||
memtable.add_row([tup[0],
|
||||
"%i" % tup[1],
|
||||
"%.2f" % (float(tup[1]) / total_num * 100)])
|
||||
|
||||
# get sizes of other caches
|
||||
string += "\n{w Entity idmapper cache:{n %i items\n%s" % (total_num, memtable)
|
||||
|
||||
caller.msg(string)
|
||||
|
||||
287
lib/commands/default/tests.py
Normal file
287
lib/commands/default/tests.py
Normal file
|
|
@ -0,0 +1,287 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
** OBS - this is not a normal command module! **
|
||||
** You cannot import anything in this module as a command! **
|
||||
|
||||
This is part of the Evennia unittest framework, for testing the
|
||||
stability and integrity of the codebase during updates. This module
|
||||
test the default command set. It is instantiated by the
|
||||
src/objects/tests.py module, which in turn is run by as part of the
|
||||
main test suite started with
|
||||
> python game/manage.py test.
|
||||
|
||||
"""
|
||||
|
||||
import re
|
||||
from django.conf import settings
|
||||
from django.utils.unittest import TestCase
|
||||
from src.server.serversession import ServerSession
|
||||
from src.objects.objects import DefaultObject, DefaultCharacter
|
||||
from src.players.player import DefaultPlayer
|
||||
from src.utils import create, ansi
|
||||
from src.server.sessionhandler import SESSIONS
|
||||
|
||||
from django.db.models.signals import post_save
|
||||
from src.server.caches import field_post_save
|
||||
post_save.connect(field_post_save, dispatch_uid="fieldcache")
|
||||
|
||||
# set up signal here since we are not starting the server
|
||||
|
||||
_RE = re.compile(r"^\+|-+\+|\+-+|--*|\|", re.MULTILINE)
|
||||
|
||||
#------------------------------------------------------------
|
||||
# Command testing
|
||||
# ------------------------------------------------------------
|
||||
|
||||
|
||||
def dummy(self, *args, **kwargs):
|
||||
pass
|
||||
|
||||
SESSIONS.data_out = dummy
|
||||
SESSIONS.disconnect = dummy
|
||||
|
||||
|
||||
class TestObjectClass(DefaultObject):
|
||||
def msg(self, text="", **kwargs):
|
||||
"test message"
|
||||
pass
|
||||
|
||||
|
||||
class TestCharacterClass(DefaultCharacter):
|
||||
def msg(self, text="", **kwargs):
|
||||
"test message"
|
||||
if self.player:
|
||||
self.player.msg(text=text, **kwargs)
|
||||
else:
|
||||
if not self.ndb.stored_msg:
|
||||
self.ndb.stored_msg = []
|
||||
self.ndb.stored_msg.append(text)
|
||||
|
||||
|
||||
class TestPlayerClass(DefaultPlayer):
|
||||
def msg(self, text="", **kwargs):
|
||||
"test message"
|
||||
if not self.ndb.stored_msg:
|
||||
self.ndb.stored_msg = []
|
||||
self.ndb.stored_msg.append(text)
|
||||
|
||||
# not supported to overload is_superuser field with property.
|
||||
#def _get_superuser(self):
|
||||
# "test with superuser flag"
|
||||
# return self.ndb.is_superuser
|
||||
#is_superuser = property(_get_superuser)
|
||||
|
||||
|
||||
class CommandTest(TestCase):
|
||||
"""
|
||||
Tests a command
|
||||
"""
|
||||
CID = 0 # we must set a different CID in every test to avoid unique-name collisions creating the objects
|
||||
def setUp(self):
|
||||
"sets up testing environment"
|
||||
#print "creating player %i: %s" % (self.CID, self.__class__.__name__)
|
||||
self.player = create.create_player("TestPlayer%i" % self.CID, "test@test.com", "testpassword", typeclass=TestPlayerClass)
|
||||
self.player2 = create.create_player("TestPlayer%ib" % self.CID, "test@test.com", "testpassword", typeclass=TestPlayerClass)
|
||||
self.room1 = create.create_object("src.objects.objects.DefaultRoom", key="Room%i"%self.CID, nohome=True)
|
||||
self.room1.db.desc = "room_desc"
|
||||
settings.DEFAULT_HOME = "#%i" % self.room1.id # we must have a default home
|
||||
self.room2 = create.create_object("src.objects.objects.DefaultRoom", key="Room%ib" % self.CID)
|
||||
self.obj1 = create.create_object(TestObjectClass, key="Obj%i" % self.CID, location=self.room1, home=self.room1)
|
||||
self.obj2 = create.create_object(TestObjectClass, key="Obj%ib" % self.CID, location=self.room1, home=self.room1)
|
||||
self.char1 = create.create_object(TestCharacterClass, key="Char%i" % self.CID, location=self.room1, home=self.room1)
|
||||
self.char1.permissions.add("Immortals")
|
||||
self.char2 = create.create_object(TestCharacterClass, key="Char%ib" % self.CID, location=self.room1, home=self.room1)
|
||||
self.char1.player = self.player
|
||||
self.char2.player = self.player2
|
||||
self.script = create.create_script("src.scripts.scripts.Script", key="Script%i" % self.CID)
|
||||
self.player.permissions.add("Immortals")
|
||||
|
||||
# set up a fake session
|
||||
|
||||
global SESSIONS
|
||||
session = ServerSession()
|
||||
session.init_session("telnet", ("localhost", "testmode"), SESSIONS)
|
||||
session.sessid = self.CID
|
||||
SESSIONS.portal_connect(session.get_sync_data())
|
||||
SESSIONS.login(SESSIONS.session_from_sessid(self.CID), self.player, testmode=True)
|
||||
|
||||
|
||||
def call(self, cmdobj, args, msg=None, cmdset=None, noansi=True, caller=None):
|
||||
"""
|
||||
Test a command by assigning all the needed
|
||||
properties to cmdobj and running
|
||||
cmdobj.at_pre_cmd()
|
||||
cmdobj.parse()
|
||||
cmdobj.func()
|
||||
cmdobj.at_post_cmd()
|
||||
The msgreturn value is compared to eventual
|
||||
output sent to caller.msg in the game
|
||||
"""
|
||||
cmdobj.caller = caller if caller else self.char1
|
||||
#print "call:", cmdobj.key, cmdobj.caller, caller if caller else cmdobj.caller.player
|
||||
#print "perms:", cmdobj.caller.permissions.all()
|
||||
cmdobj.cmdstring = cmdobj.key
|
||||
cmdobj.args = args
|
||||
cmdobj.cmdset = cmdset
|
||||
cmdobj.sessid = self.CID
|
||||
cmdobj.session = SESSIONS.session_from_sessid(self.CID)
|
||||
cmdobj.player = self.player
|
||||
cmdobj.raw_string = cmdobj.key + " " + args
|
||||
cmdobj.obj = caller if caller else self.char1
|
||||
# test
|
||||
self.char1.player.ndb.stored_msg = []
|
||||
cmdobj.at_pre_cmd()
|
||||
cmdobj.parse()
|
||||
cmdobj.func()
|
||||
cmdobj.at_post_cmd()
|
||||
# clean out prettytable sugar
|
||||
stored_msg = self.char1.player.ndb.stored_msg if self.char1.player else self.char1.ndb.stored_msg
|
||||
returned_msg = "|".join(_RE.sub("", mess) for mess in stored_msg)
|
||||
#returned_msg = "|".join(self.char1.player.ndb.stored_msg)
|
||||
returned_msg = ansi.parse_ansi(returned_msg, strip_ansi=noansi).strip()
|
||||
if msg != None:
|
||||
if msg == "" and returned_msg or not returned_msg.startswith(msg.strip()):
|
||||
sep1 = "\n" + "="*30 + "Wanted message" + "="*34 + "\n"
|
||||
sep2 = "\n" + "="*30 + "Returned message" + "="*32 + "\n"
|
||||
sep3 = "\n" + "="*78
|
||||
retval = sep1 + msg.strip() + sep2 + returned_msg + sep3
|
||||
raise AssertionError(retval)
|
||||
|
||||
#------------------------------------------------------------
|
||||
# Individual module Tests
|
||||
#------------------------------------------------------------
|
||||
|
||||
from src.commands.default import general
|
||||
class TestGeneral(CommandTest):
|
||||
CID = 1
|
||||
|
||||
def test_cmds(self):
|
||||
self.call(general.CmdLook(), "here", "Room1\n room_desc")
|
||||
self.call(general.CmdHome(), "", "You are already home")
|
||||
self.call(general.CmdInventory(), "", "You are not carrying anything.")
|
||||
self.call(general.CmdPose(), "looks around", "") # TODO-check this
|
||||
self.call(general.CmdHome(), "", "You are already home")
|
||||
self.call(general.CmdNick(), "testalias = testaliasedstring1", "Nick set:")
|
||||
self.call(general.CmdNick(), "/player testalias = testaliasedstring2", "Nick set:")
|
||||
self.call(general.CmdNick(), "/object testalias = testaliasedstring3", "Nick set:")
|
||||
self.assertEqual(u"testaliasedstring1", self.char1.nicks.get("testalias"))
|
||||
self.assertEqual(u"testaliasedstring2", self.char1.nicks.get("testalias", category="player"))
|
||||
self.assertEqual(u"testaliasedstring3", self.char1.nicks.get("testalias", category="object"))
|
||||
self.call(general.CmdGet(), "Obj1", "You pick up Obj1.")
|
||||
self.call(general.CmdDrop(), "Obj1", "You drop Obj1.")
|
||||
self.call(general.CmdSay(), "Testing", "You say, \"Testing\"")
|
||||
self.call(general.CmdAccess(), "", "Permission Hierarchy (climbing):")
|
||||
|
||||
|
||||
from src.commands.default import help
|
||||
from src.commands.default.cmdset_character import CharacterCmdSet
|
||||
class TestHelp(CommandTest):
|
||||
CID = 2
|
||||
def test_cmds(self):
|
||||
self.call(help.CmdHelp(), "", "Command help entries", cmdset=CharacterCmdSet())
|
||||
self.call(help.CmdSetHelp(), "testhelp, General = This is a test", "Topic 'testhelp' was successfully created.")
|
||||
self.call(help.CmdHelp(), "testhelp", "Help topic for testhelp", cmdset=CharacterCmdSet())
|
||||
|
||||
|
||||
from src.commands.default import system
|
||||
class TestSystem(CommandTest):
|
||||
CID = 3
|
||||
def test_cmds(self):
|
||||
# we are not testing CmdReload, CmdReset and CmdShutdown, CmdService or CmdTime
|
||||
# since the server is not running during these tests.
|
||||
self.call(system.CmdPy(), "1+2", ">>> 1+2|<<< 3")
|
||||
self.call(system.CmdScripts(), "", "dbref ")
|
||||
self.call(system.CmdObjects(), "", "Object subtype totals")
|
||||
self.call(system.CmdAbout(), "", None)
|
||||
self.call(system.CmdServerLoad(), "", "Server CPU and Memory load:")
|
||||
|
||||
|
||||
from src.commands.default import admin
|
||||
class TestAdmin(CommandTest):
|
||||
CID = 4
|
||||
def test_cmds(self):
|
||||
# not testing CmdBoot, CmdDelPlayer, CmdNewPassword
|
||||
self.call(admin.CmdEmit(), "Char4b = Test", "Emitted to Char4b:\nTest")
|
||||
self.call(admin.CmdPerm(), "Obj4 = Builders", "Permission 'Builders' given to Obj4 (the Object/Character).")
|
||||
self.call(admin.CmdWall(), "Test", "Announcing to all connected players ...")
|
||||
self.call(admin.CmdPerm(), "Char4b = Builders","Permission 'Builders' given to Char4b (the Object/Character).")
|
||||
self.call(admin.CmdBan(), "Char4", "NameBan char4 was added.")
|
||||
|
||||
|
||||
from src.commands.default import player
|
||||
class TestPlayer(CommandTest):
|
||||
CID = 5
|
||||
def test_cmds(self):
|
||||
if settings.MULTISESSION_MODE < 2:
|
||||
self.call(player.CmdOOCLook(), "", "You are outofcharacter (OOC).", caller=self.player)
|
||||
if settings.MULTISESSION_MODE == 2:
|
||||
self.call(player.CmdOOCLook(), "", "Account TestPlayer5 (you are OutofCharacter)", caller=self.player)
|
||||
self.call(player.CmdOOC(), "", "You are already", caller=self.player)
|
||||
self.call(player.CmdIC(), "Char5","You become Char5.", caller=self.player)
|
||||
self.call(player.CmdPassword(), "testpassword = testpassword", "Password changed.", caller=self.player)
|
||||
self.call(player.CmdEncoding(), "", "Default encoding:", caller=self.player)
|
||||
self.call(player.CmdWho(), "", "Players:", caller=self.player)
|
||||
self.call(player.CmdQuit(), "", "Quitting. Hope to see you soon again.", caller=self.player)
|
||||
self.call(player.CmdSessions(), "", "Your current session(s):", caller=self.player)
|
||||
self.call(player.CmdColorTest(), "ansi", "ANSI colors:", caller=self.player)
|
||||
self.call(player.CmdCharCreate(), "Test1=Test char","Created new character Test1. Use @ic Test1 to enter the game", caller=self.player)
|
||||
self.call(player.CmdQuell(), "", "Quelling to current puppet's permissions (immortals).", caller=self.player)
|
||||
|
||||
|
||||
from src.commands.default import building
|
||||
class TestBuilding(CommandTest):
|
||||
CID = 6
|
||||
def test_cmds(self):
|
||||
self.call(building.CmdCreate(), "/drop TestObj1", "You create a new DefaultObject: TestObj1.")
|
||||
self.call(building.CmdExamine(), "TestObj1", "Name/key: TestObj1")
|
||||
self.call(building.CmdSetObjAlias(), "TestObj1 = TestObj1b","Alias(es) for 'TestObj1' set to testobj1b.")
|
||||
self.call(building.CmdCopy(), "TestObj1 = TestObj2;TestObj2b, TestObj3;TestObj3b", "Copied TestObj1 to 'TestObj3' (aliases: ['TestObj3b']")
|
||||
self.call(building.CmdSetAttribute(), "Obj6/test1=\"value1\"", "Created attribute Obj6/test1 = \"value1\"")
|
||||
self.call(building.CmdSetAttribute(), "Obj6b/test2=\"value2\"", "Created attribute Obj6b/test2 = \"value2\"")
|
||||
self.call(building.CmdMvAttr(), "Obj6b/test2 = Obj6/test3", "Moving Obj6b/test2 (with value value2) ...\nMoved Obj6b.test2")
|
||||
self.call(building.CmdCpAttr(), "Obj6/test1 = Obj6b/test3", "Copying Obj6/test1 (with value value1) ...\nCopied Obj6.test1")
|
||||
self.call(building.CmdName(), "Obj6b=Obj6c", "Object's name changed to 'Obj6c'.")
|
||||
self.call(building.CmdDesc(), "Obj6c=TestDesc", "The description was set on Obj6c.")
|
||||
self.call(building.CmdWipe(), "Obj6c/test2/test3", "Wiped attributes test2,test3 on Obj6c.")
|
||||
self.call(building.CmdDestroy(), "TestObj1","TestObj1 was destroyed.")
|
||||
self.call(building.CmdDig(), "TestRoom1=testroom;tr,back;b", "Created room TestRoom1")
|
||||
self.call(building.CmdTunnel(), "n = TestRoom2;test2", "Created room TestRoom2")
|
||||
self.call(building.CmdOpen(), "TestExit1=Room6b", "Created new Exit 'TestExit1' from Room6 to Room6b")
|
||||
self.call(building.CmdLink(),"TestExit1 = TestRoom1","Link created TestExit1 > TestRoom1 (one way).")
|
||||
self.call(building.CmdUnLink(), "TestExit1", "Former exit TestExit1 no longer links anywhere.")
|
||||
self.call(building.CmdSetHome(), "Obj6 = Room6b", "Obj6's home location was changed from Room6")
|
||||
self.call(building.CmdListCmdSets(), "", "<DefaultCharacter (Union, prio 0, perm)>:")
|
||||
self.call(building.CmdTypeclass(), "Obj6 = src.objects.objects.DefaultExit",
|
||||
"Obj6 changed typeclass from src.commands.default.tests.TestObjectClass to src.objects.objects.DefaultExit")
|
||||
self.call(building.CmdLock(), "Obj6 = test:perm(Immortals)", "Added lock 'test:perm(Immortals)' to Obj6.")
|
||||
self.call(building.CmdFind(), "TestRoom1", "One Match")
|
||||
self.call(building.CmdScript(), "Obj6 = src.scripts.scripts.Script", "Script src.scripts.scripts.Script successfully added")
|
||||
self.call(building.CmdTeleport(), "TestRoom1", "TestRoom1\nExits: back|Teleported to TestRoom1.")
|
||||
|
||||
|
||||
from src.commands.default import comms
|
||||
class TestComms(CommandTest):
|
||||
CID = 7
|
||||
def test_cmds(self):
|
||||
# not testing the irc/imc2/rss commands here since testing happens offline
|
||||
self.call(comms.CmdChannelCreate(), "testchan;test=Test Channel", "Created channel testchan and connected to it.")
|
||||
self.call(comms.CmdAddCom(), "tc = testchan", "You are already connected to channel testchan. You can now")
|
||||
self.call(comms.CmdDelCom(), "tc", "Your alias 'tc' for channel testchan was cleared.")
|
||||
self.call(comms.CmdChannels(), "" ,"Available channels (use comlist,addcom and delcom to manage")
|
||||
self.call(comms.CmdAllCom(), "", "Available channels (use comlist,addcom and delcom to manage")
|
||||
self.call(comms.CmdClock(), "testchan=send:all()", "Lock(s) applied. Current locks on testchan:")
|
||||
self.call(comms.CmdCdesc(), "testchan = Test Channel", "Description of channel 'testchan' set to 'Test Channel'.")
|
||||
self.call(comms.CmdCemit(), "testchan = Test Message", "Sent to channel testchan: Test Message")
|
||||
self.call(comms.CmdCWho(), "testchan", "Channel subscriptions\ntestchan:\n TestPlayer7")
|
||||
self.call(comms.CmdPage(), "TestPlayer7b = Test", "TestPlayer7b is offline. They will see your message if they list their pages later.|You paged TestPlayer7b with: 'Test'.")
|
||||
self.call(comms.CmdCBoot(), "", "Usage: @cboot[/quiet] <channel> = <player> [:reason]") # noone else connected to boot
|
||||
self.call(comms.CmdCdestroy(), "testchan" ,"Channel 'testchan' was destroyed.")
|
||||
|
||||
|
||||
from src.commands.default import batchprocess
|
||||
class TestBatchProcess(CommandTest):
|
||||
CID = 8
|
||||
def test_cmds(self):
|
||||
# cannot test batchcode here, it must run inside the server process
|
||||
self.call(batchprocess.CmdBatchCommands(), "examples.batch_cmds", "Running Batchcommand processor Automatic mode for examples.batch_cmds")
|
||||
#self.call(batchprocess.CmdBatchCode(), "examples.batch_code", "")
|
||||
459
lib/commands/default/unloggedin.py
Normal file
459
lib/commands/default/unloggedin.py
Normal file
|
|
@ -0,0 +1,459 @@
|
|||
"""
|
||||
Commands that are available from the connect screen.
|
||||
"""
|
||||
import re
|
||||
from random import getrandbits
|
||||
import traceback
|
||||
from django.conf import settings
|
||||
from src.players.models import PlayerDB
|
||||
from src.objects.models import ObjectDB
|
||||
from src.server.models import ServerConfig
|
||||
from src.comms.models import ChannelDB
|
||||
|
||||
from src.utils import create, logger, utils, ansi
|
||||
from src.commands.default.muxcommand import MuxCommand
|
||||
from src.commands.cmdhandler import CMD_LOGINSTART
|
||||
|
||||
# limit symbol import for API
|
||||
__all__ = ("CmdUnconnectedConnect", "CmdUnconnectedCreate",
|
||||
"CmdUnconnectedQuit", "CmdUnconnectedLook", "CmdUnconnectedHelp")
|
||||
|
||||
MULTISESSION_MODE = settings.MULTISESSION_MODE
|
||||
CONNECTION_SCREEN_MODULE = settings.CONNECTION_SCREEN_MODULE
|
||||
CONNECTION_SCREEN = ""
|
||||
try:
|
||||
CONNECTION_SCREEN = ansi.parse_ansi(utils.random_string_from_module(CONNECTION_SCREEN_MODULE))
|
||||
except Exception:
|
||||
pass
|
||||
if not CONNECTION_SCREEN:
|
||||
CONNECTION_SCREEN = "\nEvennia: Error in CONNECTION_SCREEN MODULE (randomly picked connection screen variable is not a string). \nEnter 'help' for aid."
|
||||
|
||||
|
||||
class CmdUnconnectedConnect(MuxCommand):
|
||||
"""
|
||||
connect to the game
|
||||
|
||||
Usage (at login screen):
|
||||
connect playername password
|
||||
connect "player name" "pass word"
|
||||
|
||||
Use the create command to first create an account before logging in.
|
||||
|
||||
If you have spaces in your name, enclose it in quotes.
|
||||
"""
|
||||
key = "connect"
|
||||
aliases = ["conn", "con", "co"]
|
||||
locks = "cmd:all()" # not really needed
|
||||
arg_regex = r"\s.*?|$"
|
||||
|
||||
def func(self):
|
||||
"""
|
||||
Uses the Django admin api. Note that unlogged-in commands
|
||||
have a unique position in that their func() receives
|
||||
a session object instead of a source_object like all
|
||||
other types of logged-in commands (this is because
|
||||
there is no object yet before the player has logged in)
|
||||
"""
|
||||
|
||||
session = self.caller
|
||||
args = self.args
|
||||
# extract quoted parts
|
||||
parts = [part.strip() for part in re.split(r"\"|\'", args) if part.strip()]
|
||||
if len(parts) == 1:
|
||||
# this was (hopefully) due to no quotes being found, or a guest login
|
||||
parts = parts[0].split(None, 1)
|
||||
# Guest login
|
||||
if len(parts) == 1 and parts[0].lower() == "guest" and settings.GUEST_ENABLED:
|
||||
try:
|
||||
# Find an available guest name.
|
||||
for playername in settings.GUEST_LIST:
|
||||
if not PlayerDB.objects.filter(username__iexact=playername):
|
||||
break
|
||||
playername = None
|
||||
if playername == None:
|
||||
session.msg("All guest accounts are in use. Please try again later.")
|
||||
return
|
||||
|
||||
password = "%016x" % getrandbits(64)
|
||||
home = ObjectDB.objects.get_id(settings.GUEST_HOME)
|
||||
permissions = settings.PERMISSION_GUEST_DEFAULT
|
||||
typeclass = settings.BASE_CHARACTER_TYPECLASS
|
||||
ptypeclass = settings.BASE_GUEST_TYPECLASS
|
||||
start_location = ObjectDB.objects.get_id(settings.GUEST_START_LOCATION)
|
||||
|
||||
new_player = _create_player(session, playername, password,
|
||||
home, permissions, ptypeclass)
|
||||
if new_player:
|
||||
_create_character(session, new_player, typeclass, start_location,
|
||||
home, permissions)
|
||||
session.sessionhandler.login(session, new_player)
|
||||
|
||||
except Exception:
|
||||
# We are in the middle between logged in and -not, so we have
|
||||
# to handle tracebacks ourselves at this point. If we don't,
|
||||
# we won't see any errors at all.
|
||||
string = "%s\nThis is a bug. Please e-mail an admin if the problem persists."
|
||||
session.msg(string % (traceback.format_exc()))
|
||||
logger.log_errmsg(traceback.format_exc())
|
||||
finally:
|
||||
return
|
||||
|
||||
if len(parts) != 2:
|
||||
session.msg("\n\r Usage (without <>): connect <name> <password>")
|
||||
return
|
||||
playername, password = parts
|
||||
|
||||
# Match account name and check password
|
||||
player = PlayerDB.objects.get_player_from_name(playername)
|
||||
pswd = None
|
||||
if player:
|
||||
pswd = player.check_password(password)
|
||||
|
||||
if not (player and pswd):
|
||||
# No playername or password match
|
||||
string = "Wrong login information given.\nIf you have spaces in your name or "
|
||||
string += "password, don't forget to enclose it in quotes. Also capitalization matters."
|
||||
string += "\nIf you are new you should first create a new account "
|
||||
string += "using the 'create' command."
|
||||
session.msg(string)
|
||||
return
|
||||
|
||||
# Check IP and/or name bans
|
||||
bans = ServerConfig.objects.conf("server_bans")
|
||||
if bans and (any(tup[0]==player.name.lower() for tup in bans)
|
||||
or
|
||||
any(tup[2].match(session.address) for tup in bans if tup[2])):
|
||||
# this is a banned IP or name!
|
||||
string = "{rYou have been banned and cannot continue from here."
|
||||
string += "\nIf you feel this ban is in error, please email an admin.{x"
|
||||
session.msg(string)
|
||||
session.execute_cmd("quit")
|
||||
return
|
||||
|
||||
# actually do the login. This will call all other hooks:
|
||||
# session.at_login()
|
||||
# player.at_init() # always called when object is loaded from disk
|
||||
# player.at_pre_login()
|
||||
# player.at_first_login() # only once
|
||||
# player.at_post_login(sessid=sessid)
|
||||
session.sessionhandler.login(session, player)
|
||||
|
||||
|
||||
class CmdUnconnectedCreate(MuxCommand):
|
||||
"""
|
||||
create a new player account
|
||||
|
||||
Usage (at login screen):
|
||||
create <playername> <password>
|
||||
create "player name" "pass word"
|
||||
|
||||
This creates a new player account.
|
||||
|
||||
If you have spaces in your name, enclose it in quotes.
|
||||
"""
|
||||
key = "create"
|
||||
aliases = ["cre", "cr"]
|
||||
locks = "cmd:all()"
|
||||
arg_regex = r"\s.*?|$"
|
||||
|
||||
def func(self):
|
||||
"Do checks and create account"
|
||||
|
||||
session = self.caller
|
||||
args = self.args.strip()
|
||||
|
||||
# extract quoted parts
|
||||
parts = [part.strip() for part in re.split(r"\"|\'", args) if part.strip()]
|
||||
if len(parts) == 1:
|
||||
# this was (hopefully) due to no quotes being found
|
||||
parts = parts[0].split(None, 1)
|
||||
if len(parts) != 2:
|
||||
string = "\n Usage (without <>): create <name> <password>"
|
||||
string += "\nIf <name> or <password> contains spaces, enclose it in quotes."
|
||||
session.msg(string)
|
||||
return
|
||||
playername, password = parts
|
||||
|
||||
# sanity checks
|
||||
if not re.findall('^[\w. @+-]+$', playername) or not (0 < len(playername) <= 30):
|
||||
# this echoes the restrictions made by django's auth
|
||||
# module (except not allowing spaces, for convenience of
|
||||
# logging in).
|
||||
string = "\n\r Playername can max be 30 characters or fewer. Letters, spaces, digits and @/./+/-/_ only."
|
||||
session.msg(string)
|
||||
return
|
||||
# strip excessive spaces in playername
|
||||
playername = re.sub(r"\s+", " ", playername).strip()
|
||||
if PlayerDB.objects.filter(username__iexact=playername):
|
||||
# player already exists (we also ignore capitalization here)
|
||||
session.msg("Sorry, there is already a player with the name '%s'." % playername)
|
||||
return
|
||||
# Reserve playernames found in GUEST_LIST
|
||||
if settings.GUEST_LIST and playername.lower() in map(str.lower, settings.GUEST_LIST):
|
||||
string = "\n\r That name is reserved. Please choose another Playername."
|
||||
session.msg(string)
|
||||
return
|
||||
if not re.findall('^[\w. @+-]+$', password) or not (3 < len(password)):
|
||||
string = "\n\r Password should be longer than 3 characers. Letters, spaces, digits and @\.\+\-\_ only."
|
||||
string += "\nFor best security, make it longer than 8 characters. You can also use a phrase of"
|
||||
string += "\nmany words if you enclose the password in quotes."
|
||||
session.msg(string)
|
||||
return
|
||||
|
||||
# Check IP and/or name bans
|
||||
bans = ServerConfig.objects.conf("server_bans")
|
||||
if bans and (any(tup[0]==playername.lower() for tup in bans)
|
||||
or
|
||||
any(tup[2].match(session.address) for tup in bans if tup[2])):
|
||||
# this is a banned IP or name!
|
||||
string = "{rYou have been banned and cannot continue from here."
|
||||
string += "\nIf you feel this ban is in error, please email an admin.{x"
|
||||
session.msg(string)
|
||||
session.execute_cmd("quit")
|
||||
return
|
||||
|
||||
# everything's ok. Create the new player account.
|
||||
try:
|
||||
default_home = ObjectDB.objects.get_id(settings.DEFAULT_HOME)
|
||||
permissions = settings.PERMISSION_PLAYER_DEFAULT
|
||||
typeclass = settings.BASE_CHARACTER_TYPECLASS
|
||||
new_player = _create_player(session, playername, password, default_home, permissions)
|
||||
start_location = ObjectDB.objects.get_id(settings.START_LOCATION)
|
||||
if new_player:
|
||||
if MULTISESSION_MODE < 2:
|
||||
_create_character(session, new_player, typeclass, start_location,
|
||||
default_home, permissions)
|
||||
# tell the caller everything went well.
|
||||
string = "A new account '%s' was created. Welcome!"
|
||||
if " " in playername:
|
||||
string += "\n\nYou can now log in with the command 'connect \"%s\" <your password>'."
|
||||
else:
|
||||
string += "\n\nYou can now log with the command 'connect %s <your password>'."
|
||||
session.msg(string % (playername, playername))
|
||||
|
||||
except Exception:
|
||||
# We are in the middle between logged in and -not, so we have
|
||||
# to handle tracebacks ourselves at this point. If we don't,
|
||||
# we won't see any errors at all.
|
||||
string = "%s\nThis is a bug. Please e-mail an admin if the problem persists."
|
||||
session.msg(string % (traceback.format_exc()))
|
||||
logger.log_errmsg(traceback.format_exc())
|
||||
|
||||
|
||||
class CmdUnconnectedQuit(MuxCommand):
|
||||
"""
|
||||
quit when in unlogged-in state
|
||||
|
||||
Usage:
|
||||
quit
|
||||
|
||||
We maintain a different version of the quit command
|
||||
here for unconnected players for the sake of simplicity. The logged in
|
||||
version is a bit more complicated.
|
||||
"""
|
||||
key = "quit"
|
||||
aliases = ["q", "qu"]
|
||||
locks = "cmd:all()"
|
||||
|
||||
def func(self):
|
||||
"Simply close the connection."
|
||||
session = self.caller
|
||||
#session.msg("Good bye! Disconnecting ...")
|
||||
session.sessionhandler.disconnect(session, "Good bye! Disconnecting.")
|
||||
|
||||
|
||||
class CmdUnconnectedLook(MuxCommand):
|
||||
"""
|
||||
look when in unlogged-in state
|
||||
|
||||
Usage:
|
||||
look
|
||||
|
||||
This is an unconnected version of the look command for simplicity.
|
||||
|
||||
This is called by the server and kicks everything in gear.
|
||||
All it does is display the connect screen.
|
||||
"""
|
||||
key = CMD_LOGINSTART
|
||||
aliases = ["look", "l"]
|
||||
locks = "cmd:all()"
|
||||
|
||||
def func(self):
|
||||
"Show the connect screen."
|
||||
self.caller.msg(CONNECTION_SCREEN)
|
||||
|
||||
|
||||
class CmdUnconnectedHelp(MuxCommand):
|
||||
"""
|
||||
get help when in unconnected-in state
|
||||
|
||||
Usage:
|
||||
help
|
||||
|
||||
This is an unconnected version of the help command,
|
||||
for simplicity. It shows a pane of info.
|
||||
"""
|
||||
key = "help"
|
||||
aliases = ["h", "?"]
|
||||
locks = "cmd:all()"
|
||||
|
||||
def func(self):
|
||||
"Shows help"
|
||||
|
||||
string = \
|
||||
"""
|
||||
You are not yet logged into the game. Commands available at this point:
|
||||
|
||||
{wcreate{n - create a new account
|
||||
{wconnect{n - connect with an existing account
|
||||
{wlook{n - re-show the connection screen
|
||||
{whelp{n - show this help
|
||||
{wencoding{n - change the text encoding to match your client
|
||||
{wquit{n - abort the connection
|
||||
|
||||
To login, first create an account
|
||||
|
||||
{wcreate Anna c67jHL8p{n
|
||||
|
||||
Note that if you use spaces in your name, you have to enclose in quotes:
|
||||
|
||||
{wcreate "Anna the Barbarian" c67jHL8p{n
|
||||
|
||||
It's always a good idea (not only here, but everywhere on the net)
|
||||
to not use a regular word for your password. Make it longer than
|
||||
6 characters or write a full passphrase.
|
||||
|
||||
Once you have an account, connect using your password
|
||||
|
||||
{wconnect Anna c67jHL8p{n
|
||||
|
||||
(Again, if there are spaces in the name you have to enclose it in quotes).
|
||||
This should log you in. Run {whelp{n again once you're logged in
|
||||
to get more aid. Hope you enjoy your stay!
|
||||
|
||||
You can use the {wlook{n command if you want to see the connect screen again.
|
||||
|
||||
"""
|
||||
self.caller.msg(string)
|
||||
|
||||
|
||||
class CmdUnconnectedEncoding(MuxCommand):
|
||||
"""
|
||||
set which text encoding to use in unconnected-in state
|
||||
|
||||
Usage:
|
||||
encoding/switches [<encoding>]
|
||||
|
||||
Switches:
|
||||
clear - clear your custom encoding
|
||||
|
||||
|
||||
This sets the text encoding for communicating with Evennia. This is mostly
|
||||
an issue only if you want to use non-ASCII characters (i.e. letters/symbols
|
||||
not found in English). If you see that your characters look strange (or you
|
||||
get encoding errors), you should use this command to set the server
|
||||
encoding to be the same used in your client program.
|
||||
|
||||
Common encodings are utf-8 (default), latin-1, ISO-8859-1 etc.
|
||||
|
||||
If you don't submit an encoding, the current encoding will be displayed
|
||||
instead.
|
||||
"""
|
||||
|
||||
key = "encoding"
|
||||
aliases = "@encoding, @encode"
|
||||
locks = "cmd:all()"
|
||||
|
||||
def func(self):
|
||||
"""
|
||||
Sets the encoding.
|
||||
"""
|
||||
|
||||
if self.session is None:
|
||||
return
|
||||
|
||||
if 'clear' in self.switches:
|
||||
# remove customization
|
||||
old_encoding = self.session.encoding
|
||||
if old_encoding:
|
||||
string = "Your custom text encoding ('%s') was cleared." % old_encoding
|
||||
else:
|
||||
string = "No custom encoding was set."
|
||||
self.session.encoding = "utf-8"
|
||||
elif not self.args:
|
||||
# just list the encodings supported
|
||||
pencoding = self.session.encoding
|
||||
string = ""
|
||||
if pencoding:
|
||||
string += "Default encoding: {g%s{n (change with {w@encoding <encoding>{n)" % pencoding
|
||||
encodings = settings.ENCODINGS
|
||||
if encodings:
|
||||
string += "\nServer's alternative encodings (tested in this order):\n {g%s{n" % ", ".join(encodings)
|
||||
if not string:
|
||||
string = "No encodings found."
|
||||
else:
|
||||
# change encoding
|
||||
old_encoding = self.session.encoding
|
||||
encoding = self.args
|
||||
self.session.encoding = encoding
|
||||
string = "Your custom text encoding was changed from '%s' to '%s'." % (old_encoding, encoding)
|
||||
self.caller.msg(string.strip())
|
||||
|
||||
|
||||
def _create_player(session, playername, password,
|
||||
default_home, permissions, typeclass=None):
|
||||
"""
|
||||
Helper function, creates a player of the specified typeclass.
|
||||
"""
|
||||
try:
|
||||
new_player = create.create_player(playername, None, password,
|
||||
permissions=permissions, typeclass=typeclass)
|
||||
|
||||
except Exception, e:
|
||||
session.msg("There was an error creating the Player:\n%s\n If this problem persists, contact an admin." % e)
|
||||
logger.log_trace()
|
||||
return False
|
||||
|
||||
# This needs to be called so the engine knows this player is
|
||||
# logging in for the first time. (so it knows to call the right
|
||||
# hooks during login later)
|
||||
utils.init_new_player(new_player)
|
||||
|
||||
# join the new player to the public channel
|
||||
pchanneldef = settings.CHANNEL_PUBLIC
|
||||
if pchanneldef:
|
||||
pchannel = ChannelDB.objects.get_channel(pchanneldef[0])
|
||||
if not pchannel.connect(new_player):
|
||||
string = "New player '%s' could not connect to public channel!" % new_player.key
|
||||
logger.log_errmsg(string)
|
||||
return new_player
|
||||
|
||||
|
||||
def _create_character(session, new_player, typeclass, start_location, home, permissions):
|
||||
"""
|
||||
Helper function, creates a character based on a player's name.
|
||||
This is meant for Guest and MULTISESSION_MODE < 2 situations.
|
||||
"""
|
||||
try:
|
||||
if not start_location:
|
||||
start_location = home # fallback
|
||||
new_character = create.create_object(typeclass, key=new_player.key,
|
||||
location=start_location, home=home,
|
||||
permissions=permissions)
|
||||
# set playable character list
|
||||
new_player.db._playable_characters.append(new_character)
|
||||
|
||||
# allow only the character itself and the player to puppet this character (and Immortals).
|
||||
new_character.locks.add("puppet:id(%i) or pid(%i) or perm(Immortals) or pperm(Immortals)" %
|
||||
(new_character.id, new_player.id))
|
||||
|
||||
# If no description is set, set a default description
|
||||
if not new_character.db.desc:
|
||||
new_character.db.desc = "This is a Player."
|
||||
# We need to set this to have @ic auto-connect to this character
|
||||
new_player.db._last_puppet = new_character
|
||||
except Exception, e:
|
||||
session.msg("There was an error creating the Character:\n%s\n If this problem persists, contact an admin." % e)
|
||||
logger.log_trace()
|
||||
return False
|
||||
|
||||
14
lib/comms/__init__.py
Normal file
14
lib/comms/__init__.py
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
"""
|
||||
Makes it easier to import by grouping all relevant things already at this
|
||||
level.
|
||||
|
||||
You can henceforth import most things directly from src.comms
|
||||
Also, the initiated object manager is available as src.comms.msgmanager and
|
||||
src.comms.channelmanager.
|
||||
|
||||
"""
|
||||
|
||||
#from src.comms.models import *
|
||||
|
||||
#msgmanager = Msg.objects
|
||||
#channelmanager = ChannelDB.objects
|
||||
49
lib/comms/admin.py
Normal file
49
lib/comms/admin.py
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
#
|
||||
# This sets up how models are displayed
|
||||
# in the web admin interface.
|
||||
#
|
||||
|
||||
from django.contrib import admin
|
||||
from src.comms.models import ChannelDB
|
||||
from src.typeclasses.admin import AttributeInline, TagInline
|
||||
|
||||
|
||||
class ChannelAttributeInline(AttributeInline):
|
||||
model = ChannelDB.db_attributes.through
|
||||
|
||||
|
||||
class ChannelTagInline(TagInline):
|
||||
model = ChannelDB.db_tags.through
|
||||
|
||||
|
||||
class MsgAdmin(admin.ModelAdmin):
|
||||
list_display = ('id', 'db_date_sent', 'db_sender', 'db_receivers',
|
||||
'db_channels', 'db_message', 'db_lock_storage')
|
||||
list_display_links = ("id",)
|
||||
ordering = ["db_date_sent", 'db_sender', 'db_receivers', 'db_channels']
|
||||
#readonly_fields = ['db_message', 'db_sender', 'db_receivers', 'db_channels']
|
||||
search_fields = ['id', '^db_date_sent', '^db_message']
|
||||
save_as = True
|
||||
save_on_top = True
|
||||
list_select_related = True
|
||||
#admin.site.register(Msg, MsgAdmin)
|
||||
|
||||
|
||||
class ChannelAdmin(admin.ModelAdmin):
|
||||
inlines = [ChannelTagInline, ChannelAttributeInline]
|
||||
list_display = ('id', 'db_key', 'db_lock_storage', "subscriptions")
|
||||
list_display_links = ("id", 'db_key')
|
||||
ordering = ["db_key"]
|
||||
search_fields = ['id', 'db_key', 'db_aliases']
|
||||
save_as = True
|
||||
save_on_top = True
|
||||
list_select_related = True
|
||||
fieldsets = (
|
||||
(None, {'fields': (('db_key',), 'db_lock_storage', 'db_subscriptions')}),
|
||||
)
|
||||
|
||||
def subscriptions(self, obj):
|
||||
"Helper method to get subs from a channel"
|
||||
return ", ".join([str(sub) for sub in obj.db_subscriptions.all()])
|
||||
|
||||
admin.site.register(ChannelDB, ChannelAdmin)
|
||||
161
lib/comms/channelhandler.py
Normal file
161
lib/comms/channelhandler.py
Normal file
|
|
@ -0,0 +1,161 @@
|
|||
"""
|
||||
The channel handler handles the stored set of channels
|
||||
and how they are represented against the cmdhandler.
|
||||
|
||||
If there is a channel named 'newbie', we want to be able
|
||||
to just write
|
||||
|
||||
> newbie Hello!
|
||||
|
||||
For this to work, 'newbie', the name of the channel, must
|
||||
be identified by the cmdhandler as a command name. The
|
||||
channelhandler stores all channels as custom 'commands'
|
||||
that the cmdhandler can import and look through.
|
||||
|
||||
Warning - channel names take precedence over command names,
|
||||
so make sure to not pick clashing channel names.
|
||||
|
||||
Unless deleting a channel you normally don't need to bother about
|
||||
the channelhandler at all - the create_channel method handles the update.
|
||||
|
||||
To delete a channel cleanly, delete the channel object, then call
|
||||
update() on the channelhandler. Or use Channel.objects.delete() which
|
||||
does this for you.
|
||||
|
||||
"""
|
||||
from src.comms.models import ChannelDB
|
||||
from src.commands import cmdset, command
|
||||
|
||||
|
||||
class ChannelCommand(command.Command):
|
||||
"""
|
||||
Channel
|
||||
|
||||
Usage:
|
||||
<channel name or alias> <message>
|
||||
|
||||
This is a channel. If you have subscribed to it, you can send to
|
||||
it by entering its name or alias, followed by the text you want to
|
||||
send.
|
||||
"""
|
||||
# this flag is what identifies this cmd as a channel cmd
|
||||
# and branches off to the system send-to-channel command
|
||||
# (which is customizable by admin)
|
||||
is_channel = True
|
||||
key = "general"
|
||||
help_category = "Channel Names"
|
||||
obj = None
|
||||
|
||||
def parse(self):
|
||||
"""
|
||||
Simple parser
|
||||
"""
|
||||
# cmdhandler sends channame:msg here.
|
||||
channelname, msg = self.args.split(":", 1)
|
||||
self.args = (channelname.strip(), msg.strip())
|
||||
|
||||
def func(self):
|
||||
"""
|
||||
Create a new message and send it to channel, using
|
||||
the already formatted input.
|
||||
"""
|
||||
channelkey, msg = self.args
|
||||
caller = self.caller
|
||||
if not msg:
|
||||
self.msg("Say what?")
|
||||
return
|
||||
channel = ChannelDB.objects.get_channel(channelkey)
|
||||
|
||||
if not channel:
|
||||
self.msg("Channel '%s' not found." % channelkey)
|
||||
return
|
||||
if not channel.has_connection(caller):
|
||||
string = "You are not connected to channel '%s'."
|
||||
self.msg(string % channelkey)
|
||||
return
|
||||
if not channel.access(caller, 'send'):
|
||||
string = "You are not permitted to send to channel '%s'."
|
||||
self.msg(string % channelkey)
|
||||
return
|
||||
channel.msg(msg, senders=self.caller, online=True)
|
||||
|
||||
|
||||
class ChannelHandler(object):
|
||||
"""
|
||||
Handles the set of commands related to channels.
|
||||
"""
|
||||
def __init__(self):
|
||||
self.cached_channel_cmds = []
|
||||
self.cached_cmdsets = {}
|
||||
|
||||
def __str__(self):
|
||||
return ", ".join(str(cmd) for cmd in self.cached_channel_cmds)
|
||||
|
||||
def clear(self):
|
||||
"""
|
||||
Reset the cache storage.
|
||||
"""
|
||||
self.cached_channel_cmds = []
|
||||
|
||||
def _format_help(self, channel):
|
||||
"builds a doc string"
|
||||
key = channel.key
|
||||
aliases = channel.aliases.all()
|
||||
ustring = "%s <message>" % key.lower() + "".join(["\n %s <message>" % alias.lower() for alias in aliases])
|
||||
desc = channel.db.desc
|
||||
string = \
|
||||
"""
|
||||
Channel '%s'
|
||||
|
||||
Usage (not including your personal aliases):
|
||||
%s
|
||||
|
||||
%s
|
||||
""" % (key, ustring, desc)
|
||||
return string
|
||||
|
||||
def add_channel(self, channel):
|
||||
"""
|
||||
Add an individual channel to the handler. This should be
|
||||
called whenever a new channel is created. To
|
||||
remove a channel, simply delete the channel object
|
||||
and run self.update on the handler.
|
||||
"""
|
||||
# map the channel to a searchable command
|
||||
cmd = ChannelCommand(key=channel.key.strip().lower(),
|
||||
aliases=channel.aliases.all(),
|
||||
locks="cmd:all();%s" % channel.locks,
|
||||
help_category="Channel names",
|
||||
obj=channel,
|
||||
arg_regex=r"\s.*?",
|
||||
is_channel=True)
|
||||
self.cached_channel_cmds.append(cmd)
|
||||
self.cached_cmdsets = {}
|
||||
|
||||
def update(self):
|
||||
"Updates the handler completely."
|
||||
self.cached_channel_cmds = []
|
||||
self.cached_cmdsets = {}
|
||||
for channel in ChannelDB.objects.get_all_channels():
|
||||
self.add_channel(channel)
|
||||
|
||||
def get_cmdset(self, source_object):
|
||||
"""
|
||||
Retrieve cmdset for channels this source_object has
|
||||
access to send to.
|
||||
"""
|
||||
if source_object in self.cached_cmdsets:
|
||||
return self.cached_cmdsets[source_object]
|
||||
else:
|
||||
# create a new cmdset holding all channels
|
||||
chan_cmdset = cmdset.CmdSet()
|
||||
chan_cmdset.key = '_channelset'
|
||||
chan_cmdset.priority = 120
|
||||
chan_cmdset.duplicates = True
|
||||
for cmd in [cmd for cmd in self.cached_channel_cmds
|
||||
if cmd.access(source_object, 'send')]:
|
||||
chan_cmdset.add(cmd)
|
||||
self.cached_cmdsets[source_object] = chan_cmdset
|
||||
return chan_cmdset
|
||||
|
||||
CHANNELHANDLER = ChannelHandler()
|
||||
330
lib/comms/comms.py
Normal file
330
lib/comms/comms.py
Normal file
|
|
@ -0,0 +1,330 @@
|
|||
"""
|
||||
Default Typeclass for Comms.
|
||||
|
||||
See objects.objects for more information on Typeclassing.
|
||||
"""
|
||||
from src.typeclasses.models import TypeclassBase
|
||||
from src.comms.models import Msg, TempMsg, ChannelDB
|
||||
from src.comms.managers import ChannelManager
|
||||
from src.utils import logger
|
||||
from src.utils.utils import make_iter
|
||||
|
||||
|
||||
class Channel(ChannelDB):
|
||||
"""
|
||||
This is the base class for all Comms. Inherit from this to create different
|
||||
types of communication channels.
|
||||
"""
|
||||
__metaclass__ = TypeclassBase
|
||||
objects = ChannelManager()
|
||||
|
||||
def at_first_save(self):
|
||||
"""
|
||||
Called by the typeclass system the very first time the channel
|
||||
is saved to the database. Generally, don't overload this but
|
||||
the hooks called by this method.
|
||||
"""
|
||||
self.at_channel_creation()
|
||||
|
||||
if hasattr(self, "_createdict"):
|
||||
# this is only set if the channel was created
|
||||
# with the utils.create.create_channel function.
|
||||
cdict = self._createdict
|
||||
if not cdict.get("key"):
|
||||
if not self.db_key:
|
||||
self.db_key = "#i" % self.dbid
|
||||
elif cdict["key"] and self.key != cdict["key"]:
|
||||
self.key = cdict["key"]
|
||||
if cdict.get("keep_log"):
|
||||
self.db_keep_log = cdict["keep_log"]
|
||||
if cdict.get("aliases"):
|
||||
self.aliases.add(cdict["aliases"])
|
||||
if cdict.get("locks"):
|
||||
self.locks.add(cdict["locks"])
|
||||
if cdict.get("keep_log"):
|
||||
self.attributes.add("keep_log", cdict["keep_log"])
|
||||
if cdict.get("desc"):
|
||||
self.attributes.add("desc", cdict["desc"])
|
||||
|
||||
def at_channel_creation(self):
|
||||
"""
|
||||
Called once, when the channel is first created.
|
||||
"""
|
||||
pass
|
||||
|
||||
# helper methods, for easy overloading
|
||||
|
||||
def has_connection(self, player):
|
||||
"""
|
||||
Checks so this player is actually listening
|
||||
to this channel.
|
||||
"""
|
||||
if hasattr(player, "player"):
|
||||
player = player.player
|
||||
return player in self.db_subscriptions.all()
|
||||
|
||||
def connect(self, player):
|
||||
"Connect the user to this channel. This checks access."
|
||||
if hasattr(player, "player"):
|
||||
player = player.player
|
||||
# check access
|
||||
if not self.access(player, 'listen'):
|
||||
return False
|
||||
# pre-join hook
|
||||
connect = self.pre_join_channel(player)
|
||||
if not connect:
|
||||
return False
|
||||
# subscribe
|
||||
self.db_subscriptions.add(player)
|
||||
# post-join hook
|
||||
self.post_join_channel(player)
|
||||
return True
|
||||
|
||||
def disconnect(self, player):
|
||||
"Disconnect user from this channel."
|
||||
if hasattr(player, "player"):
|
||||
player = player.player
|
||||
# pre-disconnect hook
|
||||
disconnect = self.pre_leave_channel(player)
|
||||
if not disconnect:
|
||||
return False
|
||||
# disconnect
|
||||
self.db_subscriptions.remove(player)
|
||||
# post-disconnect hook
|
||||
self.post_leave_channel(player)
|
||||
return True
|
||||
|
||||
def access(self, accessing_obj, access_type='listen', default=False):
|
||||
"""
|
||||
Determines if another object has permission to access.
|
||||
accessing_obj - object trying to access this one
|
||||
access_type - type of access sought
|
||||
default - what to return if no lock of access_type was found
|
||||
"""
|
||||
return self.locks.check(accessing_obj, access_type=access_type, default=default)
|
||||
|
||||
def delete(self):
|
||||
"""
|
||||
Deletes channel while also cleaning up channelhandler
|
||||
"""
|
||||
self.attributes.clear()
|
||||
self.aliases.clear()
|
||||
super(Channel, self).delete()
|
||||
from src.comms.channelhandler import CHANNELHANDLER
|
||||
CHANNELHANDLER.update()
|
||||
|
||||
def channel_prefix(self, msg=None, emit=False):
|
||||
"""
|
||||
How the channel should prefix itself for users. Return a string.
|
||||
"""
|
||||
return '[%s] ' % self.key
|
||||
|
||||
def format_senders(self, senders=None):
|
||||
"""
|
||||
Function used to format a list of sender names.
|
||||
|
||||
This function exists separately so that external sources can use
|
||||
it to format source names in the same manner as normal object/player
|
||||
names.
|
||||
"""
|
||||
if not senders:
|
||||
return ''
|
||||
return ', '.join(senders)
|
||||
|
||||
def pose_transform(self, msg, sender_string):
|
||||
"""
|
||||
Detects if the sender is posing, and modifies the message accordingly.
|
||||
"""
|
||||
pose = False
|
||||
message = msg.message
|
||||
message_start = message.lstrip()
|
||||
if message_start.startswith((':', ';')):
|
||||
pose = True
|
||||
message = message[1:]
|
||||
if not message.startswith((':', "'", ',')):
|
||||
if not message.startswith(' '):
|
||||
message = ' ' + message
|
||||
if pose:
|
||||
return '%s%s' % (sender_string, message)
|
||||
else:
|
||||
return '%s: %s' % (sender_string, message)
|
||||
|
||||
def format_external(self, msg, senders, emit=False):
|
||||
"""
|
||||
Used for formatting external messages. This is needed as a separate
|
||||
operation because the senders of external messages may not be in-game
|
||||
objects/players, and so cannot have things like custom user
|
||||
preferences.
|
||||
|
||||
senders should be a list of strings, each containing a sender.
|
||||
msg should contain the body of the message to be sent.
|
||||
"""
|
||||
if not senders:
|
||||
emit = True
|
||||
if emit:
|
||||
return msg.message
|
||||
senders = ', '.join(senders)
|
||||
return self.pose_transform(msg, senders)
|
||||
|
||||
def format_message(self, msg, emit=False):
|
||||
"""
|
||||
Formats a message body for display.
|
||||
|
||||
If emit is True, it means the message is intended to be posted detached
|
||||
from an identity.
|
||||
"""
|
||||
# We don't want to count things like external sources as senders for
|
||||
# the purpose of constructing the message string.
|
||||
senders = [sender for sender in msg.senders if hasattr(sender, 'key')]
|
||||
if not senders:
|
||||
emit = True
|
||||
if emit:
|
||||
return msg.message
|
||||
else:
|
||||
senders = [sender.key for sender in msg.senders]
|
||||
senders = ', '.join(senders)
|
||||
return self.pose_transform(msg, senders)
|
||||
|
||||
def message_transform(self, msg, emit=False, prefix=True,
|
||||
sender_strings=None, external=False):
|
||||
"""
|
||||
Generates the formatted string sent to listeners on a channel.
|
||||
"""
|
||||
if sender_strings or external:
|
||||
body = self.format_external(msg, sender_strings, emit=emit)
|
||||
else:
|
||||
body = self.format_message(msg, emit=emit)
|
||||
if prefix:
|
||||
body = "%s%s" % (self.channel_prefix(msg, emit=emit), body)
|
||||
msg.message = body
|
||||
return msg
|
||||
|
||||
def pre_join_channel(self, joiner):
|
||||
"""
|
||||
Run right before a channel is joined. If this returns a false value,
|
||||
channel joining is aborted.
|
||||
"""
|
||||
return True
|
||||
|
||||
def post_join_channel(self, joiner):
|
||||
"""
|
||||
Run right after an object or player joins a channel.
|
||||
"""
|
||||
return True
|
||||
|
||||
def pre_leave_channel(self, leaver):
|
||||
"""
|
||||
Run right before a user leaves a channel. If this returns a false
|
||||
value, leaving the channel will be aborted.
|
||||
"""
|
||||
return True
|
||||
|
||||
def post_leave_channel(self, leaver):
|
||||
"""
|
||||
Run right after an object or player leaves a channel.
|
||||
"""
|
||||
pass
|
||||
|
||||
def pre_send_message(self, msg):
|
||||
"""
|
||||
Run before a message is sent to the channel.
|
||||
|
||||
This should return the message object, after any transformations.
|
||||
If the message is to be discarded, return a false value.
|
||||
"""
|
||||
return msg
|
||||
|
||||
def post_send_message(self, msg):
|
||||
"""
|
||||
Run after a message is sent to the channel.
|
||||
"""
|
||||
pass
|
||||
|
||||
def at_init(self):
|
||||
"""
|
||||
This is always called whenever this channel is initiated --
|
||||
that is, whenever it its typeclass is cached from memory. This
|
||||
happens on-demand first time the channel is used or activated
|
||||
in some way after being created but also after each server
|
||||
restart or reload.
|
||||
"""
|
||||
pass
|
||||
|
||||
def distribute_message(self, msg, online=False):
|
||||
"""
|
||||
Method for grabbing all listeners that a message should be sent to on
|
||||
this channel, and sending them a message.
|
||||
"""
|
||||
# get all players connected to this channel and send to them
|
||||
for player in self.db_subscriptions.all():
|
||||
try:
|
||||
# note our addition of the from_channel keyword here. This could be checked
|
||||
# by a custom player.msg() to treat channel-receives differently.
|
||||
player.msg(msg.message, from_obj=msg.senders, from_channel=self.id)
|
||||
except AttributeError, e:
|
||||
logger.log_trace("%s\nCannot send msg to player '%s'." % (e, player))
|
||||
|
||||
def msg(self, msgobj, header=None, senders=None, sender_strings=None,
|
||||
persistent=False, online=False, emit=False, external=False):
|
||||
"""
|
||||
Send the given message to all players connected to channel. Note that
|
||||
no permission-checking is done here; it is assumed to have been
|
||||
done before calling this method. The optional keywords are not used if
|
||||
persistent is False.
|
||||
|
||||
msgobj - a Msg/TempMsg instance or a message string. If one of the
|
||||
former, the remaining keywords will be ignored. If a string,
|
||||
this will either be sent as-is (if persistent=False) or it
|
||||
will be used together with header and senders keywords to
|
||||
create a Msg instance on the fly.
|
||||
senders - an object, player or a list of objects or players.
|
||||
Optional if persistent=False.
|
||||
sender_strings - Name strings of senders. Used for external
|
||||
connections where the sender is not a player or object. When
|
||||
this is defined, external will be assumed.
|
||||
external - Treat this message agnostic of its sender.
|
||||
persistent (default False) - ignored if msgobj is a Msg or TempMsg.
|
||||
If True, a Msg will be created, using header and senders
|
||||
keywords. If False, other keywords will be ignored.
|
||||
online (bool) - If this is set true, only messages people who are
|
||||
online. Otherwise, messages all players connected. This can
|
||||
make things faster, but may not trigger listeners on players
|
||||
that are offline.
|
||||
emit (bool) - Signals to the message formatter that this message is
|
||||
not to be directly associated with a name.
|
||||
"""
|
||||
if senders:
|
||||
senders = make_iter(senders)
|
||||
else:
|
||||
senders = []
|
||||
if isinstance(msgobj, basestring):
|
||||
# given msgobj is a string
|
||||
msg = msgobj
|
||||
if persistent and self.db.keep_log:
|
||||
msgobj = Msg()
|
||||
msgobj.save()
|
||||
else:
|
||||
# Use TempMsg, so this message is not stored.
|
||||
msgobj = TempMsg()
|
||||
msgobj.header = header
|
||||
msgobj.message = msg
|
||||
msgobj.channels = [self] # add this channel
|
||||
|
||||
if not msgobj.senders:
|
||||
msgobj.senders = senders
|
||||
msgobj = self.pre_send_message(msgobj)
|
||||
if not msgobj:
|
||||
return False
|
||||
msgobj = self.message_transform(msgobj, emit=emit,
|
||||
sender_strings=sender_strings,
|
||||
external=external)
|
||||
self.distribute_message(msgobj, online=online)
|
||||
self.post_send_message(msgobj)
|
||||
return True
|
||||
|
||||
def tempmsg(self, message, header=None, senders=None):
|
||||
"""
|
||||
A wrapper for sending non-persistent messages.
|
||||
"""
|
||||
self.msg(message, senders=senders, header=header, persistent=False)
|
||||
|
||||
332
lib/comms/managers.py
Normal file
332
lib/comms/managers.py
Normal file
|
|
@ -0,0 +1,332 @@
|
|||
"""
|
||||
These managers handles the
|
||||
"""
|
||||
|
||||
from django.db import models
|
||||
from django.db.models import Q
|
||||
from src.typeclasses.managers import (TypedObjectManager, TypeclassManager,
|
||||
returns_typeclass_list, returns_typeclass)
|
||||
|
||||
_GA = object.__getattribute__
|
||||
_PlayerDB = None
|
||||
_ObjectDB = None
|
||||
_ChannelDB = None
|
||||
_SESSIONS = None
|
||||
|
||||
# error class
|
||||
|
||||
|
||||
class CommError(Exception):
|
||||
"Raise by comm system, to allow feedback to player when caught."
|
||||
pass
|
||||
|
||||
|
||||
#
|
||||
# helper functions
|
||||
#
|
||||
|
||||
def dbref(dbref, reqhash=True):
|
||||
"""
|
||||
Valid forms of dbref (database reference number)
|
||||
are either a string '#N' or an integer N.
|
||||
Output is the integer part.
|
||||
"""
|
||||
if reqhash and not (isinstance(dbref, basestring) and dbref.startswith("#")):
|
||||
return None
|
||||
if isinstance(dbref, basestring):
|
||||
dbref = dbref.lstrip('#')
|
||||
try:
|
||||
if int(dbref) < 0:
|
||||
return None
|
||||
except Exception:
|
||||
return None
|
||||
return dbref
|
||||
|
||||
|
||||
def identify_object(inp):
|
||||
"identify if an object is a player or an object; return its database model"
|
||||
# load global stores
|
||||
global _PlayerDB, _ObjectDB, _ChannelDB
|
||||
if not _PlayerDB:
|
||||
from src.players.models import PlayerDB as _PlayerDB
|
||||
if not _ObjectDB:
|
||||
from src.objects.models import ObjectDB as _ObjectDB
|
||||
if not _ChannelDB:
|
||||
from src.comms.models import ChannelDB as _ChannelDB
|
||||
|
||||
if not inp:
|
||||
return inp, None
|
||||
if isinstance(inp, basestring):
|
||||
return inp, "string"
|
||||
elif inp.is_typeclass(_PlayerDB, exact=False):
|
||||
return inp, "player"
|
||||
elif inp.is_typeclass(_ObjectDB, exact=False):
|
||||
return inp, "object"
|
||||
elif inp.is_typeclass(_ChannelDB, exact=False):
|
||||
return inp, "channel"
|
||||
elif dbref(inp):
|
||||
return dbref(inp), "dbref"
|
||||
return inp, None # something else
|
||||
|
||||
|
||||
def to_object(inp, objtype='player'):
|
||||
"""
|
||||
Locates the object related to the given
|
||||
playername or channel key. If input was already
|
||||
the correct object, return it.
|
||||
inp - the input object/string
|
||||
objtype - 'player' or 'channel'
|
||||
"""
|
||||
obj, typ = identify_object(inp)
|
||||
if typ == objtype:
|
||||
return obj
|
||||
if objtype == 'player':
|
||||
if typ == 'object':
|
||||
return obj.player
|
||||
if typ == 'string':
|
||||
return _PlayerDB.objects.get(user_username__iexact=obj)
|
||||
if typ == 'dbref':
|
||||
return _PlayerDB.objects.get(id=obj)
|
||||
print objtype, inp, obj, typ, type(inp)
|
||||
raise CommError()
|
||||
elif objtype == 'object':
|
||||
if typ == 'player':
|
||||
return obj.obj
|
||||
if typ == 'string':
|
||||
return _ObjectDB.objects.get(db_key__iexact=obj)
|
||||
if typ == 'dbref':
|
||||
return _ObjectDB.objects.get(id=obj)
|
||||
print objtype, inp, obj, typ, type(inp)
|
||||
raise CommError()
|
||||
elif objtype == 'channel':
|
||||
if typ == 'string':
|
||||
return _ChannelDB.objects.get(db_key__iexact=obj)
|
||||
if typ == 'dbref':
|
||||
return _ChannelDB.objects.get(id=obj)
|
||||
print objtype, inp, obj, typ, type(inp)
|
||||
raise CommError()
|
||||
|
||||
#
|
||||
# Msg manager
|
||||
#
|
||||
|
||||
class MsgManager(models.Manager):
|
||||
"""
|
||||
This MsgManager implements methods for searching
|
||||
and manipulating Messages directly from the database.
|
||||
|
||||
These methods will all return database objects
|
||||
(or QuerySets) directly.
|
||||
|
||||
A Message represents one unit of communication, be it over a
|
||||
Channel or via some form of in-game mail system. Like an e-mail,
|
||||
it always has a sender and can have any number of receivers (some
|
||||
of which may be Channels).
|
||||
|
||||
Evennia-specific:
|
||||
get_message_by_id
|
||||
get_messages_by_sender
|
||||
get_messages_by_receiver
|
||||
get_messages_by_channel
|
||||
text_search
|
||||
message_search (equivalent to ev.search_messages)
|
||||
"""
|
||||
|
||||
def identify_object(self, obj):
|
||||
"method version for easy access"
|
||||
return identify_object(obj)
|
||||
|
||||
def get_message_by_id(self, idnum):
|
||||
"Retrieve message by its id."
|
||||
try:
|
||||
return self.get(id=self.dbref(idnum, reqhash=False))
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
def get_messages_by_sender(self, obj, exclude_channel_messages=False):
|
||||
"""
|
||||
Get all messages sent by one entity - this could be either a
|
||||
player or an object
|
||||
|
||||
only_non_channel: only return messages -not- aimed at a channel
|
||||
(e.g. private tells)
|
||||
"""
|
||||
obj, typ = identify_object(obj)
|
||||
if exclude_channel_messages:
|
||||
# explicitly exclude channel recipients
|
||||
if typ == 'player':
|
||||
return list(self.filter(db_sender_players=obj,
|
||||
db_receivers_channels__isnull=True).exclude(db_hide_from_players=obj))
|
||||
elif typ == 'object':
|
||||
return list(self.filter(db_sender_objects=obj,
|
||||
db_receivers_channels__isnull=True).exclude(db_hide_from_objects=obj))
|
||||
else:
|
||||
raise CommError
|
||||
else:
|
||||
# get everything, channel or not
|
||||
if typ == 'player':
|
||||
return list(self.filter(db_sender_players=obj).exclude(db_hide_from_players=obj))
|
||||
elif typ == 'object':
|
||||
return list(self.filter(db_sender_objects=obj).exclude(db_hide_from_objects=obj))
|
||||
else:
|
||||
raise CommError
|
||||
|
||||
def get_messages_by_receiver(self, obj):
|
||||
"""
|
||||
Get all messages sent to one give recipient
|
||||
"""
|
||||
obj, typ = identify_object(obj)
|
||||
if typ == 'player':
|
||||
return list(self.filter(db_receivers_players=obj).exclude(db_hide_from_players=obj))
|
||||
elif typ == 'object':
|
||||
return list(self.filter(db_receivers_objects=obj).exclude(db_hide_from_objects=obj))
|
||||
elif typ == 'channel':
|
||||
return list(self.filter(db_receivers_channels=obj).exclude(db_hide_from_channels=obj))
|
||||
else:
|
||||
raise CommError
|
||||
|
||||
def get_messages_by_channel(self, channel):
|
||||
"""
|
||||
Get all messages sent to one channel
|
||||
"""
|
||||
return self.filter(db_receivers_channels=channel).exclude(db_hide_from_channels=channel)
|
||||
|
||||
def message_search(self, sender=None, receiver=None, freetext=None, dbref=None):
|
||||
"""
|
||||
Search the message database for particular messages. At least one
|
||||
of the arguments must be given to do a search.
|
||||
|
||||
sender - get messages sent by a particular player or object
|
||||
receiver - get messages received by a certain player,object or channel
|
||||
freetext - Search for a text string in a message.
|
||||
NOTE: This can potentially be slow, so make sure to supply
|
||||
one of the other arguments to limit the search.
|
||||
dbref - (int) the exact database id of the message. This will override
|
||||
all other search criteria since it's unique and
|
||||
always gives a list with only one match.
|
||||
"""
|
||||
# unique msg id
|
||||
if dbref:
|
||||
msg = self.objects.filter(id=dbref)
|
||||
if msg:
|
||||
return msg[0]
|
||||
|
||||
# We use Q objects to gradually build up the query - this way we only
|
||||
# need to do one database lookup at the end rather than gradually
|
||||
# refining with multiple filter:s. Django Note: Q objects can be
|
||||
# combined with & and | (=AND,OR). ~ negates the queryset
|
||||
|
||||
# filter by sender
|
||||
sender, styp = identify_object(sender)
|
||||
if styp == 'player':
|
||||
sender_restrict = Q(db_sender_players=sender) & ~Q(db_hide_from_players=sender)
|
||||
elif styp == 'object':
|
||||
sender_restrict = Q(db_sender_objects=sender) & ~Q(db_hide_from_objects=sender)
|
||||
else:
|
||||
sender_restrict = Q()
|
||||
# filter by receiver
|
||||
receiver, rtyp = identify_object(receiver)
|
||||
if rtyp == 'player':
|
||||
receiver_restrict = Q(db_receivers_players=receiver) & ~Q(db_hide_from_players=receiver)
|
||||
elif rtyp == 'object':
|
||||
receiver_restrict = Q(db_receivers_objects=receiver) & ~Q(db_hide_from_objects=receiver)
|
||||
elif rtyp == 'channel':
|
||||
receiver_restrict = Q(db_receivers_channels=receiver) & ~Q(db_hide_from_channels=receiver)
|
||||
else:
|
||||
receiver_restrict = Q()
|
||||
# filter by full text
|
||||
if freetext:
|
||||
fulltext_restrict = Q(db_header__icontains=freetext) | Q(db_message__icontains=freetext)
|
||||
else:
|
||||
fulltext_restrict = Q()
|
||||
# execute the query
|
||||
return list(self.filter(sender_restrict & receiver_restrict & fulltext_restrict))
|
||||
|
||||
|
||||
#
|
||||
# Channel manager
|
||||
#
|
||||
|
||||
class ChannelDBManager(TypedObjectManager):
|
||||
"""
|
||||
This ChannelManager implements methods for searching
|
||||
and manipulating Channels directly from the database.
|
||||
|
||||
These methods will all return database objects
|
||||
(or QuerySets) directly.
|
||||
|
||||
A Channel is an in-game venue for communication. It's
|
||||
essentially representation of a re-sender: Users sends
|
||||
Messages to the Channel, and the Channel re-sends those
|
||||
messages to all users subscribed to the Channel.
|
||||
|
||||
Evennia-specific:
|
||||
get_all_channels
|
||||
get_channel(channel)
|
||||
get_subscriptions(player)
|
||||
channel_search (equivalent to ev.search_channel)
|
||||
|
||||
"""
|
||||
@returns_typeclass_list
|
||||
def get_all_channels(self):
|
||||
"""
|
||||
Returns all channels in game.
|
||||
"""
|
||||
return self.all()
|
||||
|
||||
@returns_typeclass
|
||||
def get_channel(self, channelkey):
|
||||
"""
|
||||
Return the channel object if given its key.
|
||||
Also searches its aliases.
|
||||
"""
|
||||
# first check the channel key
|
||||
channels = self.filter(db_key__iexact=channelkey)
|
||||
if not channels:
|
||||
# also check aliases
|
||||
channels = [channel for channel in self.all()
|
||||
if channelkey in channel.aliases.all()]
|
||||
if channels:
|
||||
return channels[0]
|
||||
return None
|
||||
|
||||
@returns_typeclass_list
|
||||
def get_subscriptions(self, player):
|
||||
"""
|
||||
Return all channels a given player is subscribed to
|
||||
"""
|
||||
return player.subscription_set.all()
|
||||
|
||||
@returns_typeclass_list
|
||||
def channel_search(self, ostring, exact=True):
|
||||
"""
|
||||
Search the channel database for a particular channel.
|
||||
|
||||
ostring - the key or database id of the channel.
|
||||
exact - require an exact key match (still not case sensitive)
|
||||
"""
|
||||
channels = []
|
||||
if not ostring: return channels
|
||||
try:
|
||||
# try an id match first
|
||||
dbref = int(ostring.strip('#'))
|
||||
channels = self.filter(id=dbref)
|
||||
except Exception:
|
||||
pass
|
||||
if not channels:
|
||||
# no id match. Search on the key.
|
||||
if exact:
|
||||
channels = self.filter(db_key__iexact=ostring)
|
||||
else:
|
||||
channels = self.filter(db_key__icontains=ostring)
|
||||
if not channels:
|
||||
# still no match. Search by alias.
|
||||
channels = [channel for channel in self.all()
|
||||
if ostring.lower() in [a.lower
|
||||
for a in channel.aliases.all()]]
|
||||
return channels
|
||||
|
||||
class ChannelManager(ChannelDBManager, TypeclassManager):
|
||||
pass
|
||||
|
||||
|
||||
44
lib/comms/migrations/0001_initial.py
Normal file
44
lib/comms/migrations/0001_initial.py
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import models, migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='ChannelDB',
|
||||
fields=[
|
||||
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
|
||||
('db_key', models.CharField(max_length=255, verbose_name=b'key', db_index=True)),
|
||||
('db_typeclass_path', models.CharField(help_text=b"this defines what 'type' of entity this is. This variable holds a Python path to a module with a valid Evennia Typeclass.", max_length=255, null=True, verbose_name=b'typeclass')),
|
||||
('db_date_created', models.DateTimeField(auto_now_add=True, verbose_name=b'creation date')),
|
||||
('db_lock_storage', models.TextField(help_text=b"locks limit access to an entity. A lock is defined as a 'lock string' on the form 'type:lockfunctions', defining what functionality is locked and how to determine access. Not defining a lock means no access is granted.", verbose_name=b'locks', blank=True)),
|
||||
],
|
||||
options={
|
||||
'verbose_name': 'Channel',
|
||||
'verbose_name_plural': 'Channels',
|
||||
},
|
||||
bases=(models.Model,),
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='Msg',
|
||||
fields=[
|
||||
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
|
||||
('db_sender_external', models.CharField(help_text=b"identifier for external sender, for example a sender over an IRC connection (i.e. someone who doesn't have an exixtence in-game).", max_length=255, null=True, verbose_name=b'external sender', db_index=True)),
|
||||
('db_header', models.TextField(null=True, verbose_name=b'header', blank=True)),
|
||||
('db_message', models.TextField(verbose_name=b'messsage')),
|
||||
('db_date_sent', models.DateTimeField(auto_now_add=True, verbose_name=b'date sent', db_index=True)),
|
||||
('db_lock_storage', models.TextField(help_text=b'access locks on this message.', verbose_name=b'locks', blank=True)),
|
||||
('db_hide_from_channels', models.ManyToManyField(related_name=b'hide_from_channels_set', null=True, to='comms.ChannelDB')),
|
||||
],
|
||||
options={
|
||||
'verbose_name': 'Message',
|
||||
},
|
||||
bases=(models.Model,),
|
||||
),
|
||||
]
|
||||
21
lib/comms/migrations/0002_msg_db_hide_from_objects.py
Normal file
21
lib/comms/migrations/0002_msg_db_hide_from_objects.py
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import models, migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('objects', '0001_initial'),
|
||||
('comms', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='msg',
|
||||
name='db_hide_from_objects',
|
||||
field=models.ManyToManyField(related_name=b'hide_from_objects_set', null=True, to='objects.ObjectDB'),
|
||||
preserve_default=True,
|
||||
),
|
||||
]
|
||||
72
lib/comms/migrations/0003_auto_20140917_0756.py
Normal file
72
lib/comms/migrations/0003_auto_20140917_0756.py
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import models, migrations
|
||||
from django.conf import settings
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('objects', '0001_initial'),
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
('typeclasses', '0001_initial'),
|
||||
('comms', '0002_msg_db_hide_from_objects'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='msg',
|
||||
name='db_hide_from_players',
|
||||
field=models.ManyToManyField(related_name=b'hide_from_players_set', null=True, to=settings.AUTH_USER_MODEL),
|
||||
preserve_default=True,
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='msg',
|
||||
name='db_receivers_channels',
|
||||
field=models.ManyToManyField(help_text=b'channel recievers', related_name=b'channel_set', null=True, to='comms.ChannelDB'),
|
||||
preserve_default=True,
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='msg',
|
||||
name='db_receivers_objects',
|
||||
field=models.ManyToManyField(help_text=b'object receivers', related_name=b'receiver_object_set', null=True, to='objects.ObjectDB'),
|
||||
preserve_default=True,
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='msg',
|
||||
name='db_receivers_players',
|
||||
field=models.ManyToManyField(help_text=b'player receivers', related_name=b'receiver_player_set', null=True, to=settings.AUTH_USER_MODEL),
|
||||
preserve_default=True,
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='msg',
|
||||
name='db_sender_objects',
|
||||
field=models.ManyToManyField(related_name=b'sender_object_set', null=True, verbose_name=b'sender(object)', to='objects.ObjectDB', db_index=True),
|
||||
preserve_default=True,
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='msg',
|
||||
name='db_sender_players',
|
||||
field=models.ManyToManyField(related_name=b'sender_player_set', null=True, verbose_name=b'sender(player)', to=settings.AUTH_USER_MODEL, db_index=True),
|
||||
preserve_default=True,
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='channeldb',
|
||||
name='db_attributes',
|
||||
field=models.ManyToManyField(help_text=b'attributes on this object. An attribute can hold any pickle-able python object (see docs for special cases).', to='typeclasses.Attribute', null=True),
|
||||
preserve_default=True,
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='channeldb',
|
||||
name='db_subscriptions',
|
||||
field=models.ManyToManyField(related_name=b'subscription_set', null=True, verbose_name=b'subscriptions', to=settings.AUTH_USER_MODEL, db_index=True),
|
||||
preserve_default=True,
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='channeldb',
|
||||
name='db_tags',
|
||||
field=models.ManyToManyField(help_text=b'tags on this object. Tags are simple string markers to identify, group and alias objects.', to='typeclasses.Tag', null=True),
|
||||
preserve_default=True,
|
||||
),
|
||||
]
|
||||
1
lib/comms/migrations/__init__.py
Normal file
1
lib/comms/migrations/__init__.py
Normal file
|
|
@ -0,0 +1 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
368
lib/comms/models.py
Normal file
368
lib/comms/models.py
Normal file
|
|
@ -0,0 +1,368 @@
|
|||
"""
|
||||
Models for the comsystem. The Commsystem is intended to be
|
||||
used by Players (thematic IC communication is probably
|
||||
best handled by custom commands instead).
|
||||
|
||||
The comm system could take the form of channels, but can also
|
||||
be adopted for storing tells or in-game mail.
|
||||
|
||||
The comsystem's main component is the Message (Msg), which
|
||||
carries the actual information between two parties.
|
||||
Msgs are stored in the database and usually not
|
||||
deleted.
|
||||
A Msg always have one sender (a user), but can have
|
||||
any number targets, both users and channels.
|
||||
|
||||
Channels are central objects that act as targets for
|
||||
Msgs. Players can connect to channels by use of a
|
||||
ChannelConnect object (this object is necessary to easily
|
||||
be able to delete connections on the fly).
|
||||
"""
|
||||
|
||||
from datetime import datetime
|
||||
from django.conf import settings
|
||||
from django.db import models
|
||||
from src.typeclasses.models import TypedObject
|
||||
from src.utils.idmapper.models import SharedMemoryModel
|
||||
from src.comms import managers
|
||||
from src.comms.managers import identify_object
|
||||
from src.locks.lockhandler import LockHandler
|
||||
from src.utils.utils import crop, make_iter, lazy_property
|
||||
|
||||
__all__ = ("Msg", "TempMsg", "ChannelDB")
|
||||
|
||||
|
||||
_GA = object.__getattribute__
|
||||
_SA = object.__setattr__
|
||||
_DA = object.__delattr__
|
||||
|
||||
|
||||
#------------------------------------------------------------
|
||||
#
|
||||
# Msg
|
||||
#
|
||||
#------------------------------------------------------------
|
||||
|
||||
class Msg(SharedMemoryModel):
|
||||
"""
|
||||
A single message. This model describes all ooc messages
|
||||
sent in-game, both to channels and between players.
|
||||
|
||||
The Msg class defines the following properties:
|
||||
sender - sender of message
|
||||
receivers - list of target objects for message
|
||||
channels - list of channels message was sent to
|
||||
message - the text being sent
|
||||
date_sent - time message was sent
|
||||
hide_from_sender - bool if message should be hidden from sender
|
||||
hide_from_receivers - list of receiver objects to hide message from
|
||||
hide_from_channels - list of channels objects to hide message from
|
||||
permissions - perm strings
|
||||
|
||||
"""
|
||||
#
|
||||
# Msg database model setup
|
||||
#
|
||||
#
|
||||
# These databse fields are all set using their corresponding properties,
|
||||
# named same as the field, but withtout the db_* prefix.
|
||||
|
||||
# Sender is either a player, an object or an external sender, like
|
||||
# an IRC channel; normally there is only one, but if co-modification of
|
||||
# a message is allowed, there may be more than one "author"
|
||||
db_sender_players = models.ManyToManyField("players.PlayerDB", related_name='sender_player_set', null=True, verbose_name='sender(player)', db_index=True)
|
||||
db_sender_objects = models.ManyToManyField("objects.ObjectDB", related_name='sender_object_set', null=True, verbose_name='sender(object)', db_index=True)
|
||||
db_sender_external = models.CharField('external sender', max_length=255, null=True, db_index=True,
|
||||
help_text="identifier for external sender, for example a sender over an IRC connection (i.e. someone who doesn't have an exixtence in-game).")
|
||||
# The destination objects of this message. Stored as a
|
||||
# comma-separated string of object dbrefs. Can be defined along
|
||||
# with channels below.
|
||||
db_receivers_players = models.ManyToManyField('players.PlayerDB', related_name='receiver_player_set', null=True, help_text="player receivers")
|
||||
db_receivers_objects = models.ManyToManyField('objects.ObjectDB', related_name='receiver_object_set', null=True, help_text="object receivers")
|
||||
db_receivers_channels = models.ManyToManyField("ChannelDB", related_name='channel_set', null=True, help_text="channel recievers")
|
||||
|
||||
# header could be used for meta-info about the message if your system needs
|
||||
# it, or as a separate store for the mail subject line maybe.
|
||||
db_header = models.TextField('header', null=True, blank=True)
|
||||
# the message body itself
|
||||
db_message = models.TextField('messsage')
|
||||
# send date
|
||||
db_date_sent = models.DateTimeField('date sent', editable=False, auto_now_add=True, db_index=True)
|
||||
# lock storage
|
||||
db_lock_storage = models.TextField('locks', blank=True,
|
||||
help_text='access locks on this message.')
|
||||
|
||||
# these can be used to filter/hide a given message from supplied objects/players/channels
|
||||
db_hide_from_players = models.ManyToManyField("players.PlayerDB", related_name='hide_from_players_set', null=True)
|
||||
db_hide_from_objects = models.ManyToManyField("objects.ObjectDB", related_name='hide_from_objects_set', null=True)
|
||||
db_hide_from_channels = models.ManyToManyField("ChannelDB", related_name='hide_from_channels_set', null=True)
|
||||
|
||||
# Database manager
|
||||
objects = managers.MsgManager()
|
||||
_is_deleted = False
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
SharedMemoryModel.__init__(self, *args, **kwargs)
|
||||
self.extra_senders = []
|
||||
|
||||
class Meta:
|
||||
"Define Django meta options"
|
||||
verbose_name = "Message"
|
||||
|
||||
# Wrapper properties to easily set database fields. These are
|
||||
# @property decorators that allows to access these fields using
|
||||
# normal python operations (without having to remember to save()
|
||||
# etc). So e.g. a property 'attr' has a get/set/del decorator
|
||||
# defined that allows the user to do self.attr = value,
|
||||
# value = self.attr and del self.attr respectively (where self
|
||||
# is the object in question).
|
||||
|
||||
# sender property (wraps db_sender_*)
|
||||
#@property
|
||||
def __senders_get(self):
|
||||
"Getter. Allows for value = self.sender"
|
||||
return list(self.db_sender_players.all()) + \
|
||||
list(self.db_sender_objects.all()) + \
|
||||
self.extra_senders
|
||||
|
||||
#@sender.setter
|
||||
def __senders_set(self, value):
|
||||
"Setter. Allows for self.sender = value"
|
||||
for val in (v for v in make_iter(value) if v):
|
||||
obj, typ = identify_object(val)
|
||||
if typ == 'player':
|
||||
self.db_sender_players.add(obj)
|
||||
elif typ == 'object':
|
||||
self.db_sender_objects.add(obj)
|
||||
elif isinstance(typ, basestring):
|
||||
self.db_sender_external = obj
|
||||
elif not obj:
|
||||
return
|
||||
else:
|
||||
raise ValueError(obj)
|
||||
self.save()
|
||||
|
||||
#@sender.deleter
|
||||
def __senders_del(self):
|
||||
"Deleter. Clears all senders"
|
||||
self.db_sender_players.clear()
|
||||
self.db_sender_objects.clear()
|
||||
self.db_sender_external = ""
|
||||
self.extra_senders = []
|
||||
self.save()
|
||||
senders = property(__senders_get, __senders_set, __senders_del)
|
||||
|
||||
def remove_sender(self, value):
|
||||
"Remove a single sender or a list of senders"
|
||||
for val in make_iter(value):
|
||||
obj, typ = identify_object(val)
|
||||
if typ == 'player':
|
||||
self.db_sender_players.remove(obj)
|
||||
elif typ == 'object':
|
||||
self.db_sender_objects.remove(obj)
|
||||
elif isinstance(obj, basestring):
|
||||
self.db_sender_external = obj
|
||||
else:
|
||||
raise ValueError(obj)
|
||||
self.save()
|
||||
|
||||
# receivers property
|
||||
#@property
|
||||
def __receivers_get(self):
|
||||
"""
|
||||
Getter. Allows for value = self.receivers.
|
||||
Returns three lists of receivers: players, objects and channels.
|
||||
"""
|
||||
return list(self.db_receivers_players.all()) + list(self.db_receivers_objects.all())
|
||||
|
||||
#@receivers.setter
|
||||
def __receivers_set(self, value):
|
||||
"""
|
||||
Setter. Allows for self.receivers = value.
|
||||
This appends a new receiver to the message.
|
||||
"""
|
||||
for val in (v for v in make_iter(value) if v):
|
||||
obj, typ = identify_object(val)
|
||||
if typ == 'player':
|
||||
self.db_receivers_players.add(obj)
|
||||
elif typ == 'object':
|
||||
self.db_receivers_objects.add(obj)
|
||||
elif not obj:
|
||||
return
|
||||
else:
|
||||
raise ValueError
|
||||
self.save()
|
||||
|
||||
#@receivers.deleter
|
||||
def __receivers_del(self):
|
||||
"Deleter. Clears all receivers"
|
||||
self.db_receivers_players.clear()
|
||||
self.db_receivers_objects.clear()
|
||||
self.extra_senders = []
|
||||
self.save()
|
||||
receivers = property(__receivers_get, __receivers_set, __receivers_del)
|
||||
|
||||
def remove_receiver(self, obj):
|
||||
"Remove a single recevier"
|
||||
obj, typ = identify_object(obj)
|
||||
if typ == 'player':
|
||||
self.db_receivers_players.remove(obj)
|
||||
elif typ == 'object':
|
||||
self.db_receivers_objects.remove(obj)
|
||||
else:
|
||||
raise ValueError
|
||||
self.save()
|
||||
|
||||
# channels property
|
||||
#@property
|
||||
def __channels_get(self):
|
||||
"Getter. Allows for value = self.channels. Returns a list of channels."
|
||||
return self.db_receivers_channels.all()
|
||||
|
||||
#@channels.setter
|
||||
def __channels_set(self, value):
|
||||
"""
|
||||
Setter. Allows for self.channels = value.
|
||||
Requires a channel to be added.
|
||||
"""
|
||||
for val in (v for v in make_iter(value) if v):
|
||||
self.db_receivers_channels.add(val)
|
||||
|
||||
#@channels.deleter
|
||||
def __channels_del(self):
|
||||
"Deleter. Allows for del self.channels"
|
||||
self.db_receivers_channels.clear()
|
||||
self.save()
|
||||
channels = property(__channels_get, __channels_set, __channels_del)
|
||||
|
||||
def __hide_from_get(self):
|
||||
"""
|
||||
Getter. Allows for value = self.hide_from.
|
||||
Returns 3 lists of players, objects and channels
|
||||
"""
|
||||
return self.db_hide_from_players.all(), self.db_hide_from_objects.all(), self.db_hide_from_channels.all()
|
||||
|
||||
#@hide_from_sender.setter
|
||||
def __hide_from_set(self, value):
|
||||
"Setter. Allows for self.hide_from = value. Will append to hiders"
|
||||
obj, typ = identify_object(value)
|
||||
if typ == "player":
|
||||
self.db_hide_from_players.add(obj)
|
||||
elif typ == "object":
|
||||
self.db_hide_from_objects.add(obj)
|
||||
elif typ == "channel":
|
||||
self.db_hide_from_channels.add(obj)
|
||||
else:
|
||||
raise ValueError
|
||||
self.save()
|
||||
|
||||
#@hide_from_sender.deleter
|
||||
def __hide_from_del(self):
|
||||
"Deleter. Allows for del self.hide_from_senders"
|
||||
self.db_hide_from_players.clear()
|
||||
self.db_hide_from_objects.clear()
|
||||
self.db_hide_from_channels.clear()
|
||||
self.save()
|
||||
hide_from = property(__hide_from_get, __hide_from_set, __hide_from_del)
|
||||
|
||||
#
|
||||
# Msg class methods
|
||||
#
|
||||
|
||||
def __str__(self):
|
||||
"This handles what is shown when e.g. printing the message"
|
||||
senders = ",".join(obj.key for obj in self.senders)
|
||||
receivers = ",".join(["[%s]" % obj.key for obj in self.channels] + [obj.key for obj in self.receivers])
|
||||
return "%s->%s: %s" % (senders, receivers, crop(self.message, width=40))
|
||||
|
||||
|
||||
#------------------------------------------------------------
|
||||
#
|
||||
# TempMsg
|
||||
#
|
||||
#------------------------------------------------------------
|
||||
|
||||
class TempMsg(object):
|
||||
"""
|
||||
This is a non-persistent object for sending
|
||||
temporary messages that will not be stored.
|
||||
It mimics the "real" Msg object, but don't require
|
||||
sender to be given.
|
||||
"""
|
||||
def __init__(self, senders=None, receivers=None, channels=None, message="", header="", type="", lockstring="", hide_from=None):
|
||||
self.senders = senders and make_iter(senders) or []
|
||||
self.receivers = receivers and make_iter(receivers) or []
|
||||
self.channels = channels and make_iter(channels) or []
|
||||
self.type = type
|
||||
self.header = header
|
||||
self.message = message
|
||||
self.lock_storage = lockstring
|
||||
self.hide_from = hide_from and make_iter(hide_from) or []
|
||||
self.date_sent = datetime.now()
|
||||
|
||||
@lazy_property
|
||||
def locks(self):
|
||||
return LockHandler(self)
|
||||
|
||||
def __str__(self):
|
||||
"This handles what is shown when e.g. printing the message"
|
||||
senders = ",".join(obj.key for obj in self.senders)
|
||||
receivers = ",".join(["[%s]" % obj.key for obj in self.channels] + [obj.key for obj in self.receivers])
|
||||
return "%s->%s: %s" % (senders, receivers, crop(self.message, width=40))
|
||||
|
||||
def remove_sender(self, obj):
|
||||
"Remove a sender or a list of senders"
|
||||
for o in make_iter(obj):
|
||||
try:
|
||||
self.senders.remove(o)
|
||||
except ValueError:
|
||||
pass # nothing to remove
|
||||
|
||||
def remove_receiver(self, obj):
|
||||
"Remove a sender or a list of senders"
|
||||
for o in make_iter(obj):
|
||||
try:
|
||||
self.senders.remove(o)
|
||||
except ValueError:
|
||||
pass # nothing to remove
|
||||
|
||||
def access(self, accessing_obj, access_type='read', default=False):
|
||||
"checks lock access"
|
||||
return self.locks.check(accessing_obj,
|
||||
access_type=access_type, default=default)
|
||||
|
||||
|
||||
#------------------------------------------------------------
|
||||
#
|
||||
# Channel
|
||||
#
|
||||
#------------------------------------------------------------
|
||||
|
||||
class ChannelDB(TypedObject):
|
||||
"""
|
||||
This is the basis of a comm channel, only implementing
|
||||
the very basics of distributing messages.
|
||||
|
||||
The Channel class defines the following properties:
|
||||
key - main name for channel
|
||||
desc - optional description of channel
|
||||
aliases - alternative names for the channel
|
||||
permissions - perm strings
|
||||
|
||||
"""
|
||||
db_subscriptions = models.ManyToManyField("players.PlayerDB",
|
||||
related_name="subscription_set", null=True, verbose_name='subscriptions', db_index=True)
|
||||
|
||||
# Database manager
|
||||
objects = managers.ChannelDBManager()
|
||||
|
||||
_typeclass_paths = settings.CHANNEL_TYPECLASS_PATHS
|
||||
_default_typeclass_path = settings.BASE_CHANNEL_TYPECLASS or "src.comms.comms.Channel"
|
||||
|
||||
class Meta:
|
||||
"Define Django meta options"
|
||||
verbose_name = "Channel"
|
||||
verbose_name_plural = "Channels"
|
||||
|
||||
def __str__(self):
|
||||
return "Channel '%s' (%s)" % (self.key, self.db.desc)
|
||||
11
lib/help/__init__.py
Normal file
11
lib/help/__init__.py
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
"""
|
||||
Makes it easier to import by grouping all relevant things already at this level.
|
||||
|
||||
You can henceforth import most things directly from src.help
|
||||
Also, the initiated object manager is available as src.help.manager.
|
||||
|
||||
"""
|
||||
|
||||
#from src.help.models import *
|
||||
#
|
||||
#manager = HelpEntry.objects
|
||||
39
lib/help/admin.py
Normal file
39
lib/help/admin.py
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
"""
|
||||
This defines how to edit help entries in Admin.
|
||||
"""
|
||||
from django import forms
|
||||
from django.contrib import admin
|
||||
from src.help.models import HelpEntry
|
||||
|
||||
|
||||
|
||||
class HelpEntryForm(forms.ModelForm):
|
||||
"Defines how to display the help entry"
|
||||
class Meta:
|
||||
model = HelpEntry
|
||||
fields = '__all__'
|
||||
|
||||
db_help_category = forms.CharField(label="Help category", initial='General',
|
||||
help_text="organizes help entries in lists")
|
||||
db_lock_storage = forms.CharField(label="Locks", initial='view:all()',required=False,
|
||||
widget=forms.TextInput(attrs={'size':'40'}),)
|
||||
|
||||
class HelpEntryAdmin(admin.ModelAdmin):
|
||||
"Sets up the admin manaager for help entries"
|
||||
|
||||
list_display = ('id', 'db_key', 'db_help_category', 'db_lock_storage')
|
||||
list_display_links = ('id', 'db_key')
|
||||
search_fields = ['^db_key', 'db_entrytext']
|
||||
ordering = ['db_help_category', 'db_key']
|
||||
save_as = True
|
||||
save_on_top = True
|
||||
list_select_related = True
|
||||
|
||||
form = HelpEntryForm
|
||||
fieldsets = (
|
||||
(None, {'fields':(('db_key', 'db_help_category'),
|
||||
'db_entrytext', 'db_lock_storage'),
|
||||
'description':"Sets a Help entry. Set lock to <i>view:all()</I> unless you want to restrict it."}),)
|
||||
|
||||
|
||||
admin.site.register(HelpEntry, HelpEntryAdmin)
|
||||
99
lib/help/manager.py
Normal file
99
lib/help/manager.py
Normal file
|
|
@ -0,0 +1,99 @@
|
|||
"""
|
||||
Custom manager for HelpEntry objects.
|
||||
"""
|
||||
from django.db import models
|
||||
from src.utils import logger, utils
|
||||
__all__ = ("HelpEntryManager",)
|
||||
|
||||
|
||||
class HelpEntryManager(models.Manager):
|
||||
"""
|
||||
This HelpEntryManager implements methods for searching
|
||||
and manipulating HelpEntries directly from the database.
|
||||
|
||||
These methods will all return database objects
|
||||
(or QuerySets) directly.
|
||||
|
||||
Evennia-specific:
|
||||
find_topicmatch
|
||||
find_apropos
|
||||
find_topicsuggestions
|
||||
find_topics_with_category
|
||||
all_to_category
|
||||
search_help (equivalent to ev.search_helpentry)
|
||||
|
||||
"""
|
||||
def find_topicmatch(self, topicstr, exact=False):
|
||||
"""
|
||||
Searches for matching topics based on player's input.
|
||||
"""
|
||||
dbref = utils.dbref(topicstr)
|
||||
if dbref:
|
||||
return self.filter(id=dbref)
|
||||
topics = self.filter(db_key__iexact=topicstr)
|
||||
if not topics and not exact:
|
||||
topics = self.filter(db_key__istartswith=topicstr)
|
||||
if not topics:
|
||||
topics = self.filter(db_key__icontains=topicstr)
|
||||
return topics
|
||||
|
||||
def find_apropos(self, topicstr):
|
||||
"""
|
||||
Do a very loose search, returning all help entries containing
|
||||
the search criterion in their titles.
|
||||
"""
|
||||
return self.filter(db_key__icontains=topicstr)
|
||||
|
||||
def find_topicsuggestions(self, topicstr):
|
||||
"""
|
||||
Do a fuzzy match, preferably within the category of the
|
||||
current topic.
|
||||
"""
|
||||
return self.filter(db_key__icontains=topicstr).exclude(db_key__iexact=topicstr)
|
||||
|
||||
def find_topics_with_category(self, help_category):
|
||||
"""
|
||||
Search topics having a particular category
|
||||
"""
|
||||
return self.filter(db_help_category__iexact=help_category)
|
||||
|
||||
def get_all_topics(self):
|
||||
"""
|
||||
Return all topics.
|
||||
"""
|
||||
return self.all()
|
||||
|
||||
def get_all_categories(self, pobject):
|
||||
"""
|
||||
Return all defined category names with at least one
|
||||
topic in them.
|
||||
"""
|
||||
return list(set(topic.help_category for topic in self.all()))
|
||||
|
||||
def all_to_category(self, default_category):
|
||||
"""
|
||||
Shifts all help entries in database to default_category.
|
||||
This action cannot be reverted. It is used primarily by
|
||||
the engine when importing a default help database, making
|
||||
sure this ends up in one easily separated category.
|
||||
"""
|
||||
topics = self.all()
|
||||
for topic in topics:
|
||||
topic.help_category = default_category
|
||||
topic.save()
|
||||
string = "Help database moved to category %s" % default_category
|
||||
logger.log_infomsg(string)
|
||||
|
||||
def search_help(self, ostring, help_category=None):
|
||||
"""
|
||||
Retrieve a search entry object.
|
||||
|
||||
ostring - the help topic to look for
|
||||
category - limit the search to a particular help topic
|
||||
"""
|
||||
ostring = ostring.strip().lower()
|
||||
if help_category:
|
||||
return self.filter(db_key__iexact=ostring,
|
||||
db_help_category__iexact=help_category)
|
||||
else:
|
||||
return self.filter(db_key__iexact=ostring)
|
||||
31
lib/help/migrations/0001_initial.py
Normal file
31
lib/help/migrations/0001_initial.py
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import models, migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('typeclasses', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='HelpEntry',
|
||||
fields=[
|
||||
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
|
||||
('db_key', models.CharField(help_text=b'key to search for', unique=True, max_length=255, verbose_name=b'help key')),
|
||||
('db_help_category', models.CharField(default=b'General', help_text=b'organizes help entries in lists', max_length=255, verbose_name=b'help category')),
|
||||
('db_entrytext', models.TextField(help_text=b'the main body of help text', verbose_name=b'help entry', blank=True)),
|
||||
('db_lock_storage', models.TextField(help_text=b'normally view:all().', verbose_name=b'locks', blank=True)),
|
||||
('db_staff_only', models.BooleanField(default=False)),
|
||||
('db_tags', models.ManyToManyField(help_text=b'tags on this object. Tags are simple string markers to identify, group and alias objects.', to='typeclasses.Tag', null=True)),
|
||||
],
|
||||
options={
|
||||
'verbose_name': 'Help Entry',
|
||||
'verbose_name_plural': 'Help Entries',
|
||||
},
|
||||
bases=(models.Model,),
|
||||
),
|
||||
]
|
||||
1
lib/help/migrations/__init__.py
Normal file
1
lib/help/migrations/__init__.py
Normal file
|
|
@ -0,0 +1 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
104
lib/help/models.py
Normal file
104
lib/help/models.py
Normal file
|
|
@ -0,0 +1,104 @@
|
|||
"""
|
||||
Models for the help system.
|
||||
|
||||
The database-tied help system is only half of Evennia's help
|
||||
functionality, the other one being the auto-generated command help
|
||||
that is created on the fly from each command's __doc__ string. The
|
||||
persistent database system defined here is intended for all other
|
||||
forms of help that do not concern commands, like information about the
|
||||
game world, policy info, rules and similar.
|
||||
|
||||
"""
|
||||
from django.db import models
|
||||
from src.utils.idmapper.models import SharedMemoryModel
|
||||
from src.help.manager import HelpEntryManager
|
||||
from src.typeclasses.models import Tag, TagHandler
|
||||
from src.locks.lockhandler import LockHandler
|
||||
from src.utils.utils import lazy_property
|
||||
__all__ = ("HelpEntry",)
|
||||
|
||||
|
||||
#------------------------------------------------------------
|
||||
#
|
||||
# HelpEntry
|
||||
#
|
||||
#------------------------------------------------------------
|
||||
|
||||
class HelpEntry(SharedMemoryModel):
|
||||
"""
|
||||
A generic help entry.
|
||||
|
||||
An HelpEntry object has the following properties defined:
|
||||
key - main name of entry
|
||||
help_category - which category entry belongs to (defaults to General)
|
||||
entrytext - the actual help text
|
||||
permissions - perm strings
|
||||
|
||||
Method:
|
||||
access
|
||||
|
||||
"""
|
||||
|
||||
#
|
||||
# HelpEntry Database Model setup
|
||||
#
|
||||
#
|
||||
# These database fields are all set using their corresponding properties,
|
||||
# named same as the field, but withtout the db_* prefix.
|
||||
|
||||
# title of the help entry
|
||||
db_key = models.CharField('help key', max_length=255, unique=True, help_text='key to search for')
|
||||
# help category
|
||||
db_help_category = models.CharField("help category", max_length=255, default="General",
|
||||
help_text='organizes help entries in lists')
|
||||
# the actual help entry text, in any formatting.
|
||||
db_entrytext = models.TextField('help entry', blank=True, help_text='the main body of help text')
|
||||
# lock string storage
|
||||
db_lock_storage = models.TextField('locks', blank=True, help_text='normally view:all().')
|
||||
# tags are primarily used for permissions
|
||||
db_tags = models.ManyToManyField(Tag, null=True,
|
||||
help_text='tags on this object. Tags are simple string markers to identify, group and alias objects.')
|
||||
# (deprecated, only here to allow MUX helpfile load (don't use otherwise)).
|
||||
# TODO: remove this when not needed anymore.
|
||||
db_staff_only = models.BooleanField(default=False)
|
||||
|
||||
# Database manager
|
||||
objects = HelpEntryManager()
|
||||
_is_deleted = False
|
||||
|
||||
# lazy-loaded handlers
|
||||
|
||||
@lazy_property
|
||||
def locks(self):
|
||||
return LockHandler(self)
|
||||
|
||||
@lazy_property
|
||||
def tags(self):
|
||||
return TagHandler(self)
|
||||
|
||||
|
||||
class Meta:
|
||||
"Define Django meta options"
|
||||
verbose_name = "Help Entry"
|
||||
verbose_name_plural = "Help Entries"
|
||||
|
||||
#
|
||||
#
|
||||
# HelpEntry main class methods
|
||||
#
|
||||
#
|
||||
|
||||
def __str__(self):
|
||||
return self.key
|
||||
|
||||
def __unicode__(self):
|
||||
return u'%s' % self.key
|
||||
|
||||
def access(self, accessing_obj, access_type='read', default=False):
|
||||
"""
|
||||
Determines if another object has permission to access.
|
||||
accessing_obj - object trying to access this one
|
||||
access_type - type of access sought
|
||||
default - what to return if no lock of access_type was found
|
||||
"""
|
||||
return self.locks.check(accessing_obj, access_type=access_type, default=default)
|
||||
1
lib/locks/__init__.py
Normal file
1
lib/locks/__init__.py
Normal file
|
|
@ -0,0 +1 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
526
lib/locks/lockfuncs.py
Normal file
526
lib/locks/lockfuncs.py
Normal file
|
|
@ -0,0 +1,526 @@
|
|||
"""
|
||||
This module provides a set of permission lock functions for use
|
||||
with Evennia's permissions system.
|
||||
|
||||
To call these locks, make sure this module is included in the
|
||||
settings tuple PERMISSION_FUNC_MODULES then define a lock on the form
|
||||
'<access_type>:func(args)' and add it to the object's lockhandler.
|
||||
Run the access() method of the handler to execute the lock check.
|
||||
|
||||
Note that accessing_obj and accessed_obj can be any object type
|
||||
with a lock variable/field, so be careful to not expect
|
||||
a certain object type.
|
||||
|
||||
|
||||
Appendix: MUX locks
|
||||
|
||||
Below is a list nicked from the MUX help file on the locks available
|
||||
in standard MUX. Most of these are not relevant to core Evennia since
|
||||
locks in Evennia are considerably more flexible and can be implemented
|
||||
on an individual command/typeclass basis rather than as globally
|
||||
available like the MUX ones. So many of these are not available in
|
||||
basic Evennia, but could all be implemented easily if needed for the
|
||||
individual game.
|
||||
|
||||
MUX Name: Affects: Effect:
|
||||
-------------------------------------------------------------------------------
|
||||
DefaultLock: Exits: controls who may traverse the exit to
|
||||
its destination.
|
||||
Evennia: "traverse:<lockfunc()>"
|
||||
Rooms: controls whether the player sees the SUCC
|
||||
or FAIL message for the room following the
|
||||
room description when looking at the room.
|
||||
Evennia: Custom typeclass
|
||||
Players/Things: controls who may GET the object.
|
||||
Evennia: "get:<lockfunc()"
|
||||
EnterLock: Players/Things: controls who may ENTER the object
|
||||
Evennia:
|
||||
GetFromLock: All but Exits: controls who may gets things from a given
|
||||
location.
|
||||
Evennia:
|
||||
GiveLock: Players/Things: controls who may give the object.
|
||||
Evennia:
|
||||
LeaveLock: Players/Things: controls who may LEAVE the object.
|
||||
Evennia:
|
||||
LinkLock: All but Exits: controls who may link to the location if the
|
||||
location is LINK_OK (for linking exits or
|
||||
setting drop-tos) or ABODE (for setting
|
||||
homes)
|
||||
Evennia:
|
||||
MailLock: Players: controls who may @mail the player.
|
||||
Evennia:
|
||||
OpenLock: All but Exits: controls who may open an exit.
|
||||
Evennia:
|
||||
PageLock: Players: controls who may page the player.
|
||||
Evennia: "send:<lockfunc()>"
|
||||
ParentLock: All: controls who may make @parent links to the
|
||||
object.
|
||||
Evennia: Typeclasses and "puppet:<lockstring()>"
|
||||
ReceiveLock: Players/Things: controls who may give things to the object.
|
||||
Evennia:
|
||||
SpeechLock: All but Exits: controls who may speak in that location
|
||||
Evennia:
|
||||
TeloutLock: All but Exits: controls who may teleport out of the
|
||||
location.
|
||||
Evennia:
|
||||
TportLock: Rooms/Things: controls who may teleport there
|
||||
Evennia:
|
||||
UseLock: All but Exits: controls who may USE the object, GIVE the
|
||||
object money and have the PAY attributes
|
||||
run, have their messages heard and possibly
|
||||
acted on by LISTEN and AxHEAR, and invoke
|
||||
$-commands stored on the object.
|
||||
Evennia: Commands and Cmdsets.
|
||||
DropLock: All but rooms: controls who may drop that object.
|
||||
Evennia:
|
||||
VisibleLock: All: Controls object visibility when the object
|
||||
is not dark and the looker passes the lock.
|
||||
In DARK locations, the object must also be
|
||||
set LIGHT and the viewer must pass the
|
||||
VisibleLock.
|
||||
Evennia: Room typeclass with Dark/light script
|
||||
"""
|
||||
|
||||
from django.conf import settings
|
||||
from src.utils import utils
|
||||
|
||||
_PERMISSION_HIERARCHY = [p.lower() for p in settings.PERMISSION_HIERARCHY]
|
||||
|
||||
|
||||
def _to_player(accessing_obj):
|
||||
"Helper function. Makes sure an accessing object is a player object"
|
||||
if utils.inherits_from(accessing_obj, "src.objects.objects.Object"):
|
||||
# an object. Convert to player.
|
||||
accessing_obj = accessing_obj.player
|
||||
return accessing_obj
|
||||
|
||||
|
||||
# lock functions
|
||||
|
||||
def true(*args, **kwargs):
|
||||
"Always returns True."
|
||||
return True
|
||||
|
||||
|
||||
def all(*args, **kwargs):
|
||||
return True
|
||||
|
||||
|
||||
def false(*args, **kwargs):
|
||||
"Always returns False"
|
||||
return False
|
||||
|
||||
|
||||
def none(*args, **kwargs):
|
||||
return False
|
||||
|
||||
|
||||
def self(accessing_obj, accessed_obj, *args, **kwargs):
|
||||
"""
|
||||
Check if accessing_obj is the same as accessed_obj
|
||||
|
||||
Usage:
|
||||
self()
|
||||
|
||||
This can be used to lock specifically only to
|
||||
the same object that the lock is defined on.
|
||||
"""
|
||||
return accessing_obj == accessed_obj
|
||||
|
||||
|
||||
def perm(accessing_obj, accessed_obj, *args, **kwargs):
|
||||
"""
|
||||
The basic permission-checker. Ignores case.
|
||||
|
||||
Usage:
|
||||
perm(<permission>)
|
||||
|
||||
where <permission> is the permission accessing_obj must
|
||||
have in order to pass the lock.
|
||||
|
||||
If the given permission is part of settings.PERMISSION_HIERARCHY,
|
||||
permission is also granted to all ranks higher up in the hierarchy.
|
||||
|
||||
If accessing_object is an Object controlled by a Player, the
|
||||
permissions of the Player is used unless the Attribute _quell
|
||||
is set to True on the Object. In this case however, the
|
||||
LOWEST hieararcy-permission of the Player/Object-pair will be used
|
||||
(this is order to avoid Players potentially escalating their own permissions
|
||||
by use of a higher-level Object)
|
||||
|
||||
"""
|
||||
# this allows the perm_above lockfunc to make use of this function too
|
||||
gtmode = kwargs.pop("_greater_than", False)
|
||||
|
||||
try:
|
||||
perm = args[0].lower()
|
||||
perms_object = [p.lower() for p in accessing_obj.permissions.all()]
|
||||
except (AttributeError, IndexError):
|
||||
return False
|
||||
|
||||
if utils.inherits_from(accessing_obj, "src.objects.objects.Object") and accessing_obj.player:
|
||||
player = accessing_obj.player
|
||||
perms_player = [p.lower() for p in player.permissions.all()]
|
||||
is_quell = player.attributes.get("_quell")
|
||||
|
||||
if perm in _PERMISSION_HIERARCHY:
|
||||
# check hierarchy without allowing escalation obj->player
|
||||
hpos_target = _PERMISSION_HIERARCHY.index(perm)
|
||||
hpos_player = [hpos for hpos, hperm in enumerate(_PERMISSION_HIERARCHY) if hperm in perms_player]
|
||||
hpos_player = hpos_player and hpos_player[-1] or -1
|
||||
if is_quell:
|
||||
hpos_object = [hpos for hpos, hperm in enumerate(_PERMISSION_HIERARCHY) if hperm in perms_object]
|
||||
hpos_object = hpos_object and hpos_object[-1] or -1
|
||||
if gtmode:
|
||||
return hpos_target < min(hpos_player, hpos_object)
|
||||
else:
|
||||
return hpos_target <= min(hpos_player, hpos_object)
|
||||
elif gtmode:
|
||||
return gtmode and hpos_target < hpos_player
|
||||
else:
|
||||
return hpos_target <= hpos_player
|
||||
elif not is_quell and perm in perms_player:
|
||||
# if we get here, check player perms first, otherwise
|
||||
# continue as normal
|
||||
return True
|
||||
|
||||
if perm in perms_object:
|
||||
# simplest case - we have direct match
|
||||
return True
|
||||
if perm in _PERMISSION_HIERARCHY:
|
||||
# check if we have a higher hierarchy position
|
||||
hpos_target = _PERMISSION_HIERARCHY.index(perm)
|
||||
return any(1 for hpos, hperm in enumerate(_PERMISSION_HIERARCHY)
|
||||
if hperm in perms_object and hpos_target < hpos)
|
||||
return False
|
||||
|
||||
|
||||
def perm_above(accessing_obj, accessed_obj, *args, **kwargs):
|
||||
"""
|
||||
Only allow objects with a permission *higher* in the permission
|
||||
hierarchy than the one given. If there is no such higher rank,
|
||||
it's assumed we refer to superuser. If no hierarchy is defined,
|
||||
this function has no meaning and returns False.
|
||||
"""
|
||||
kwargs["_greater_than"] = True
|
||||
return perm(accessing_obj, accessed_obj, *args, **kwargs)
|
||||
|
||||
|
||||
def pperm(accessing_obj, accessed_obj, *args, **kwargs):
|
||||
"""
|
||||
The basic permission-checker only for Player objects. Ignores case.
|
||||
|
||||
Usage:
|
||||
pperm(<permission>)
|
||||
|
||||
where <permission> is the permission accessing_obj must
|
||||
have in order to pass the lock. If the given permission
|
||||
is part of _PERMISSION_HIERARCHY, permission is also granted
|
||||
to all ranks higher up in the hierarchy.
|
||||
"""
|
||||
return perm(_to_player(accessing_obj), accessed_obj, *args, **kwargs)
|
||||
|
||||
|
||||
def pperm_above(accessing_obj, accessed_obj, *args, **kwargs):
|
||||
"""
|
||||
Only allow Player objects with a permission *higher* in the permission
|
||||
hierarchy than the one given. If there is no such higher rank,
|
||||
it's assumed we refer to superuser. If no hierarchy is defined,
|
||||
this function has no meaning and returns False.
|
||||
"""
|
||||
return perm_above(_to_player(accessing_obj), accessed_obj, *args, **kwargs)
|
||||
|
||||
|
||||
def dbref(accessing_obj, accessed_obj, *args, **kwargs):
|
||||
"""
|
||||
Usage:
|
||||
dbref(3)
|
||||
|
||||
This lock type checks if the checking object
|
||||
has a particular dbref. Note that this only
|
||||
works for checking objects that are stored
|
||||
in the database (e.g. not for commands)
|
||||
"""
|
||||
if not args:
|
||||
return False
|
||||
try:
|
||||
dbref = int(args[0].strip().strip('#'))
|
||||
except ValueError:
|
||||
return False
|
||||
if hasattr(accessing_obj, 'dbid'):
|
||||
return dbref == accessing_obj.dbid
|
||||
return False
|
||||
|
||||
|
||||
def pdbref(accessing_obj, accessed_obj, *args, **kwargs):
|
||||
"""
|
||||
Same as dbref, but making sure accessing_obj is a player.
|
||||
"""
|
||||
return dbref(_to_player(accessing_obj), accessed_obj, *args, **kwargs)
|
||||
|
||||
|
||||
def id(accessing_obj, accessed_obj, *args, **kwargs):
|
||||
"Alias to dbref"
|
||||
return dbref(accessing_obj, accessed_obj, *args, **kwargs)
|
||||
|
||||
|
||||
def pid(accessing_obj, accessed_obj, *args, **kwargs):
|
||||
"Alias to dbref, for Players"
|
||||
return dbref(_to_player(accessing_obj), accessed_obj, *args, **kwargs)
|
||||
|
||||
|
||||
# this is more efficient than multiple if ... elif statments
|
||||
CF_MAPPING = {'eq': lambda val1, val2: val1 == val2 or int(val1) == int(val2),
|
||||
'gt': lambda val1, val2: int(val1) > int(val2),
|
||||
'lt': lambda val1, val2: int(val1) < int(val2),
|
||||
'ge': lambda val1, val2: int(val1) >= int(val2),
|
||||
'le': lambda val1, val2: int(val1) <= int(val2),
|
||||
'ne': lambda val1, val2: int(val1) != int(val2),
|
||||
'default': lambda val1, val2: False}
|
||||
|
||||
|
||||
def attr(accessing_obj, accessed_obj, *args, **kwargs):
|
||||
"""
|
||||
Usage:
|
||||
attr(attrname)
|
||||
attr(attrname, value)
|
||||
attr(attrname, value, compare=type)
|
||||
|
||||
where compare's type is one of (eq,gt,lt,ge,le,ne) and signifies
|
||||
how the value should be compared with one on accessing_obj (so
|
||||
compare=gt means the accessing_obj must have a value greater than
|
||||
the one given).
|
||||
|
||||
Searches attributes *and* properties stored on the checking
|
||||
object. The first form works like a flag - if the
|
||||
attribute/property exists on the object, the value is checked for
|
||||
True/False. The second form also requires that the value of the
|
||||
attribute/property matches. Note that all retrieved values will be
|
||||
converted to strings before doing the comparison.
|
||||
"""
|
||||
# deal with arguments
|
||||
if not args:
|
||||
return False
|
||||
attrname = args[0].strip()
|
||||
value = None
|
||||
if len(args) > 1:
|
||||
value = args[1].strip()
|
||||
compare = 'eq'
|
||||
if kwargs:
|
||||
compare = kwargs.get('compare', 'eq')
|
||||
|
||||
def valcompare(val1, val2, typ='eq'):
|
||||
"compare based on type"
|
||||
try:
|
||||
return CF_MAPPING.get(typ, 'default')(val1, val2)
|
||||
except Exception:
|
||||
# this might happen if we try to compare two things
|
||||
# that cannot be compared
|
||||
return False
|
||||
|
||||
# first, look for normal properties on the object trying to gain access
|
||||
if hasattr(accessing_obj, attrname):
|
||||
if value:
|
||||
return valcompare(str(getattr(accessing_obj, attrname)), value, compare)
|
||||
# will return Fail on False value etc
|
||||
return bool(getattr(accessing_obj, attrname))
|
||||
# check attributes, if they exist
|
||||
if (hasattr(accessing_obj, 'attributes') and accessing_obj.attributes.has(attrname)):
|
||||
if value:
|
||||
return (hasattr(accessing_obj, 'attributes')
|
||||
and valcompare(accessing_obj.attributes.get(attrname), value, compare))
|
||||
# fails on False/None values
|
||||
return bool(accessing_obj.attributes.get(attrname))
|
||||
return False
|
||||
|
||||
|
||||
def objattr(accessing_obj, accessed_obj, *args, **kwargs):
|
||||
"""
|
||||
Usage:
|
||||
objattr(attrname)
|
||||
objattr(attrname, value)
|
||||
objattr(attrname, value, compare=type)
|
||||
|
||||
Works like attr, except it looks for an attribute on
|
||||
accessing_obj.obj, if such an entity exists. Suitable
|
||||
for commands.
|
||||
|
||||
"""
|
||||
if hasattr(accessing_obj, "obj"):
|
||||
return attr(accessing_obj.obj, accessed_obj, *args, **kwargs)
|
||||
|
||||
|
||||
def locattr(accessing_obj, accessed_obj, *args, **kwargs):
|
||||
"""
|
||||
Usage:
|
||||
locattr(attrname)
|
||||
locattr(attrname, value)
|
||||
locattr(attrname, value, compare=type)
|
||||
|
||||
Works like attr, except it looks for an attribute on
|
||||
accessing_obj.location, if such an entity exists.
|
||||
|
||||
"""
|
||||
if hasattr(accessing_obj, "location"):
|
||||
return attr(accessing_obj.location, accessed_obj, *args, **kwargs)
|
||||
|
||||
|
||||
def attr_eq(accessing_obj, accessed_obj, *args, **kwargs):
|
||||
"""
|
||||
Usage:
|
||||
attr_gt(attrname, 54)
|
||||
"""
|
||||
return attr(accessing_obj, accessed_obj, *args, **kwargs)
|
||||
|
||||
|
||||
def attr_gt(accessing_obj, accessed_obj, *args, **kwargs):
|
||||
"""
|
||||
Usage:
|
||||
attr_gt(attrname, 54)
|
||||
|
||||
Only true if access_obj's attribute > the value given.
|
||||
"""
|
||||
return attr(accessing_obj, accessed_obj, *args, **{'compare': 'gt'})
|
||||
|
||||
|
||||
def attr_ge(accessing_obj, accessed_obj, *args, **kwargs):
|
||||
"""
|
||||
Usage:
|
||||
attr_gt(attrname, 54)
|
||||
|
||||
Only true if access_obj's attribute >= the value given.
|
||||
"""
|
||||
return attr(accessing_obj, accessed_obj, *args, **{'compare': 'ge'})
|
||||
|
||||
|
||||
def attr_lt(accessing_obj, accessed_obj, *args, **kwargs):
|
||||
"""
|
||||
Usage:
|
||||
attr_gt(attrname, 54)
|
||||
|
||||
Only true if access_obj's attribute < the value given.
|
||||
"""
|
||||
return attr(accessing_obj, accessed_obj, *args, **{'compare': 'lt'})
|
||||
|
||||
|
||||
def attr_le(accessing_obj, accessed_obj, *args, **kwargs):
|
||||
"""
|
||||
Usage:
|
||||
attr_gt(attrname, 54)
|
||||
|
||||
Only true if access_obj's attribute <= the value given.
|
||||
"""
|
||||
return attr(accessing_obj, accessed_obj, *args, **{'compare': 'le'})
|
||||
|
||||
|
||||
def attr_ne(accessing_obj, accessed_obj, *args, **kwargs):
|
||||
"""
|
||||
Usage:
|
||||
attr_gt(attrname, 54)
|
||||
|
||||
Only true if access_obj's attribute != the value given.
|
||||
"""
|
||||
return attr(accessing_obj, accessed_obj, *args, **{'compare': 'ne'})
|
||||
|
||||
|
||||
def inside(accessing_obj, accessed_obj, *args, **kwargs):
|
||||
"""
|
||||
Usage:
|
||||
inside()
|
||||
|
||||
Only true if accessing_obj is "inside" accessed_obj
|
||||
"""
|
||||
return accessing_obj.location == accessed_obj
|
||||
|
||||
|
||||
def holds(accessing_obj, accessed_obj, *args, **kwargs):
|
||||
"""
|
||||
Usage:
|
||||
holds() checks if accessed_obj or accessed_obj.obj
|
||||
is held by accessing_obj
|
||||
holds(key/dbref) checks if accessing_obj holds an object
|
||||
with given key/dbref
|
||||
holds(attrname, value) checks if accessing_obj holds an
|
||||
object with the given attrname and value
|
||||
|
||||
This is passed if accessed_obj is carried by accessing_obj (that is,
|
||||
accessed_obj.location == accessing_obj), or if accessing_obj itself holds
|
||||
an object matching the given key.
|
||||
"""
|
||||
try:
|
||||
# commands and scripts don't have contents, so we are usually looking
|
||||
# for the contents of their .obj property instead (i.e. the object the
|
||||
# command/script is attached to).
|
||||
contents = accessing_obj.contents
|
||||
except AttributeError:
|
||||
try:
|
||||
contents = accessing_obj.obj.contents
|
||||
except AttributeError:
|
||||
return False
|
||||
|
||||
def check_holds(objid):
|
||||
# helper function. Compares both dbrefs and keys/aliases.
|
||||
objid = str(objid)
|
||||
dbref = utils.dbref(objid, reqhash=False)
|
||||
if dbref and any((True for obj in contents if obj.dbid == dbref)):
|
||||
return True
|
||||
objid = objid.lower()
|
||||
return any((True for obj in contents
|
||||
if obj.key.lower() == objid or objid in [al.lower() for al in obj.aliases.all()]))
|
||||
if not args:
|
||||
# holds() - check if accessed_obj or accessed_obj.obj is held by accessing_obj
|
||||
try:
|
||||
if check_holds(accessed_obj.dbid):
|
||||
return True
|
||||
except Exception:
|
||||
pass
|
||||
return hasattr(accessed_obj, "obj") and check_holds(accessed_obj.obj.dbid)
|
||||
if len(args) == 1:
|
||||
# command is holds(dbref/key) - check if given objname/dbref is held by accessing_ob
|
||||
return check_holds(args[0])
|
||||
elif len(args = 2):
|
||||
# command is holds(attrname, value) check if any held object has the given attribute and value
|
||||
for obj in contents:
|
||||
if obj.attributes.get(args[0]) == args[1]:
|
||||
return True
|
||||
|
||||
|
||||
def superuser(*args, **kwargs):
|
||||
"""
|
||||
Only accepts an accesing_obj that is superuser (e.g. user #1)
|
||||
|
||||
Since a superuser would not ever reach this check (superusers
|
||||
bypass the lock entirely), any user who gets this far cannot be a
|
||||
superuser, hence we just return False. :)
|
||||
"""
|
||||
return False
|
||||
|
||||
|
||||
def serversetting(accessing_obj, accessed_obj, *args, **kwargs):
|
||||
"""
|
||||
Only returns true if the Evennia settings exists, alternatively has
|
||||
a certain value.
|
||||
|
||||
Usage:
|
||||
serversetting(IRC_ENABLED)
|
||||
serversetting(BASE_SCRIPT_PATH, [game.gamesrc.scripts])
|
||||
|
||||
A given True/False or integers will be converted properly.
|
||||
"""
|
||||
if not args or not args[0]:
|
||||
return False
|
||||
if len(args) < 2:
|
||||
setting = args[0]
|
||||
val = "True"
|
||||
else:
|
||||
setting, val = args[0], args[1]
|
||||
# convert
|
||||
if val == 'True':
|
||||
val = True
|
||||
elif val == 'False':
|
||||
val = False
|
||||
elif val.isdigit():
|
||||
val = int(val)
|
||||
if setting in settings._wrapped.__dict__:
|
||||
return settings._wrapped.__dict__[setting] == val
|
||||
return False
|
||||
439
lib/locks/lockhandler.py
Normal file
439
lib/locks/lockhandler.py
Normal file
|
|
@ -0,0 +1,439 @@
|
|||
"""
|
||||
Locks
|
||||
|
||||
A lock defines access to a particular subsystem or property of
|
||||
Evennia. For example, the "owner" property can be impmemented as a
|
||||
lock. Or the disability to lift an object or to ban users.
|
||||
|
||||
A lock consists of three parts:
|
||||
|
||||
- access_type - this defines what kind of access this lock regulates. This
|
||||
just a string.
|
||||
- function call - this is one or many calls to functions that will determine
|
||||
if the lock is passed or not.
|
||||
- lock function(s). These are regular python functions with a special
|
||||
set of allowed arguments. They should always return a boolean depending
|
||||
on if they allow access or not.
|
||||
|
||||
# Lock function
|
||||
|
||||
A lock function is defined by existing in one of the modules
|
||||
listed by settings.LOCK_FUNC_MODULES. It should also always
|
||||
take four arguments looking like this:
|
||||
|
||||
funcname(accessing_obj, accessed_obj, *args, **kwargs):
|
||||
[...]
|
||||
|
||||
The accessing object is the object wanting to gain access.
|
||||
The accessed object is the object this lock resides on
|
||||
args and kwargs will hold optional arguments and/or keyword arguments
|
||||
to the function as a list and a dictionary respectively.
|
||||
|
||||
Example:
|
||||
|
||||
perm(accessing_obj, accessed_obj, *args, **kwargs):
|
||||
"Checking if the object has a particular, desired permission"
|
||||
if args:
|
||||
desired_perm = args[0]
|
||||
return desired_perm in accessing_obj.permissions.all()
|
||||
return False
|
||||
|
||||
Lock functions should most often be pretty general and ideally possible to
|
||||
re-use and combine in various ways to build clever locks.
|
||||
|
||||
|
||||
# Lock definition ("Lock string")
|
||||
|
||||
A lock definition is a string with a special syntax. It is added to
|
||||
each object's lockhandler, making that lock available from then on.
|
||||
|
||||
The lock definition looks like this:
|
||||
|
||||
'access_type:[NOT] func1(args)[ AND|OR][NOT] func2() ...'
|
||||
|
||||
That is, the access_type, a colon followed by calls to lock functions
|
||||
combined with AND or OR. NOT negates the result of the following call.
|
||||
|
||||
Example:
|
||||
|
||||
We want to limit who may edit a particular object (let's call this access_type
|
||||
for 'edit', it depends on what the command is looking for). We want this to
|
||||
only work for those with the Permission 'Builders'. So we use our lock
|
||||
function above and define it like this:
|
||||
|
||||
'edit:perm(Builders)'
|
||||
|
||||
Here, the lock-function perm() will be called with the string
|
||||
'Builders' (accessing_obj and accessed_obj are added automatically,
|
||||
you only need to add the args/kwargs, if any).
|
||||
|
||||
If we wanted to make sure the accessing object was BOTH a Builders and a
|
||||
GoodGuy, we could use AND:
|
||||
|
||||
'edit:perm(Builders) AND perm(GoodGuy)'
|
||||
|
||||
To allow EITHER Builders and GoodGuys, we replace AND with OR. perm() is just
|
||||
one example, the lock function can do anything and compare any properties of
|
||||
the calling object to decide if the lock is passed or not.
|
||||
|
||||
'lift:attrib(very_strong) AND NOT attrib(bad_back)'
|
||||
|
||||
To make these work, add the string to the lockhandler of the object you want
|
||||
to apply the lock to:
|
||||
|
||||
obj.lockhandler.add('edit:perm(Builders)')
|
||||
|
||||
From then on, a command that wants to check for 'edit' access on this
|
||||
object would do something like this:
|
||||
|
||||
if not target_obj.lockhandler.has_perm(caller, 'edit'):
|
||||
caller.msg("Sorry, you cannot edit that.")
|
||||
|
||||
All objects also has a shortcut called 'access' that is recommended to
|
||||
use instead:
|
||||
|
||||
if not target_obj.access(caller, 'edit'):
|
||||
caller.msg("Sorry, you cannot edit that.")
|
||||
|
||||
# Permissions
|
||||
|
||||
Permissions are just text strings stored in a comma-separated list on
|
||||
typeclassed objects. The default perm() lock function uses them,
|
||||
taking into account settings.PERMISSION_HIERARCHY. Also, the
|
||||
restricted @perm command sets them, but otherwise they are identical
|
||||
to any other identifier you can use.
|
||||
|
||||
"""
|
||||
|
||||
import re
|
||||
import inspect
|
||||
from django.conf import settings
|
||||
from src.utils import logger, utils
|
||||
from django.utils.translation import ugettext as _
|
||||
|
||||
__all__ = ("LockHandler", "LockException")
|
||||
|
||||
WARNING_LOG = "lockwarnings.log"
|
||||
|
||||
#
|
||||
# Exception class. This will be raised
|
||||
# by errors in lock definitions.
|
||||
#
|
||||
|
||||
class LockException(Exception):
|
||||
"raised during an error in a lock."
|
||||
pass
|
||||
|
||||
|
||||
#
|
||||
# Cached lock functions
|
||||
#
|
||||
|
||||
_LOCKFUNCS = {}
|
||||
def _cache_lockfuncs():
|
||||
"Updates the cache."
|
||||
global _LOCKFUNCS
|
||||
_LOCKFUNCS = {}
|
||||
for modulepath in settings.LOCK_FUNC_MODULES:
|
||||
modulepath = utils.pypath_to_realpath(modulepath)
|
||||
mod = utils.mod_import(modulepath)
|
||||
if mod:
|
||||
for tup in (tup for tup in inspect.getmembers(mod) if callable(tup[1])):
|
||||
_LOCKFUNCS[tup[0]] = tup[1]
|
||||
else:
|
||||
logger.log_errmsg("Couldn't load %s from PERMISSION_FUNC_MODULES." % modulepath)
|
||||
|
||||
#
|
||||
# pre-compiled regular expressions
|
||||
#
|
||||
|
||||
_RE_FUNCS = re.compile(r"\w+\([^)]*\)")
|
||||
_RE_SEPS = re.compile(r"(?<=[ )])AND(?=\s)|(?<=[ )])OR(?=\s)|(?<=[ )])NOT(?=\s)")
|
||||
_RE_OK = re.compile(r"%s|and|or|not")
|
||||
|
||||
|
||||
#
|
||||
#
|
||||
# Lock handler
|
||||
#
|
||||
#
|
||||
|
||||
class LockHandler(object):
|
||||
"""
|
||||
This handler should be attached to all objects implementing
|
||||
permission checks, under the property 'lockhandler'.
|
||||
"""
|
||||
|
||||
def __init__(self, obj):
|
||||
"""
|
||||
Loads and pre-caches all relevant locks and their
|
||||
functions.
|
||||
"""
|
||||
if not _LOCKFUNCS:
|
||||
_cache_lockfuncs()
|
||||
self.obj = obj
|
||||
self.locks = {}
|
||||
self.reset()
|
||||
|
||||
def __str__(self):
|
||||
return ";".join(self.locks[key][2] for key in sorted(self.locks))
|
||||
|
||||
def _log_error(self, message):
|
||||
"Try to log errors back to object"
|
||||
raise LockException(message)
|
||||
|
||||
def _parse_lockstring(self, storage_lockstring):
|
||||
"""
|
||||
Helper function. This is normally only called when the
|
||||
lockstring is cached and does preliminary checking. locks are
|
||||
stored as a string
|
||||
'atype:[NOT] lock()[[ AND|OR [NOT] lock()[...]];atype...
|
||||
|
||||
"""
|
||||
locks = {}
|
||||
if not storage_lockstring:
|
||||
return locks
|
||||
duplicates = 0
|
||||
elist = [] # errors
|
||||
wlist = [] # warnings
|
||||
for raw_lockstring in storage_lockstring.split(';'):
|
||||
if not raw_lockstring:
|
||||
continue
|
||||
lock_funcs = []
|
||||
try:
|
||||
access_type, rhs = (part.strip() for part in raw_lockstring.split(':', 1))
|
||||
except ValueError:
|
||||
logger.log_trace()
|
||||
return locks
|
||||
|
||||
# parse the lock functions and separators
|
||||
funclist = _RE_FUNCS.findall(rhs)
|
||||
evalstring = rhs
|
||||
for pattern in ('AND', 'OR', 'NOT'):
|
||||
evalstring = re.sub(r"\b%s\b" % pattern, pattern.lower(), evalstring)
|
||||
nfuncs = len(funclist)
|
||||
for funcstring in funclist:
|
||||
funcname, rest = (part.strip().strip(')') for part in funcstring.split('(', 1))
|
||||
func = _LOCKFUNCS.get(funcname, None)
|
||||
if not callable(func):
|
||||
elist.append(_("Lock: lock-function '%s' is not available.") % funcstring)
|
||||
continue
|
||||
args = list(arg.strip() for arg in rest.split(',') if arg and not '=' in arg)
|
||||
kwargs = dict([arg.split('=', 1) for arg in rest.split(',') if arg and '=' in arg])
|
||||
lock_funcs.append((func, args, kwargs))
|
||||
evalstring = evalstring.replace(funcstring, '%s')
|
||||
if len(lock_funcs) < nfuncs:
|
||||
continue
|
||||
try:
|
||||
# purge the eval string of any superfluous items, then test it
|
||||
evalstring = " ".join(_RE_OK.findall(evalstring))
|
||||
eval(evalstring % tuple(True for func in funclist), {}, {})
|
||||
except Exception:
|
||||
elist.append(_("Lock: definition '%s' has syntax errors.") % raw_lockstring)
|
||||
continue
|
||||
if access_type in locks:
|
||||
duplicates += 1
|
||||
wlist.append(_("LockHandler on %(obj)s: access type '%(access_type)s' changed from '%(source)s' to '%(goal)s' " % \
|
||||
{"obj":self.obj, "access_type":access_type, "source":locks[access_type][2], "goal":raw_lockstring}))
|
||||
locks[access_type] = (evalstring, tuple(lock_funcs), raw_lockstring)
|
||||
if wlist:
|
||||
# a warning text was set, it's not an error, so only report
|
||||
logger.log_file("\n".join(wlist), WARNING_LOG)
|
||||
if elist:
|
||||
# an error text was set, raise exception.
|
||||
raise LockException("\n".join(elist))
|
||||
# return the gathered locks in an easily executable form
|
||||
return locks
|
||||
|
||||
def _cache_locks(self, storage_lockstring):
|
||||
"""Store data"""
|
||||
self.locks = self._parse_lockstring(storage_lockstring)
|
||||
|
||||
def _save_locks(self):
|
||||
"Store locks to obj"
|
||||
self.obj.lock_storage = ";".join([tup[2] for tup in self.locks.values()])
|
||||
|
||||
def cache_lock_bypass(self, obj):
|
||||
"""
|
||||
We cache superuser bypass checks here for efficiency. This needs to
|
||||
be re-run when a player is assigned to a character.
|
||||
We need to grant access to superusers. We need to check both directly
|
||||
on the object (players), through obj.player and using the get_player()
|
||||
method (this sits on serversessions, in some rare cases where a
|
||||
check is done before the login process has yet been fully finalized)
|
||||
"""
|
||||
self.lock_bypass = hasattr(obj, "is_superuser") and obj.is_superuser
|
||||
|
||||
def add(self, lockstring):
|
||||
"""
|
||||
Add a new lockstring on the form '<access_type>:<functions>'. Multiple
|
||||
access types should be separated by semicolon (;).
|
||||
|
||||
"""
|
||||
# sanity checks
|
||||
for lockdef in lockstring.split(';'):
|
||||
if not ':' in lockstring:
|
||||
self._log_error(_("Lock: '%s' contains no colon (:).") % lockdef)
|
||||
return False
|
||||
access_type, rhs = [part.strip() for part in lockdef.split(':', 1)]
|
||||
if not access_type:
|
||||
self._log_error(_("Lock: '%s' has no access_type (left-side of colon is empty).") % lockdef)
|
||||
return False
|
||||
if rhs.count('(') != rhs.count(')'):
|
||||
self._log_error(_("Lock: '%s' has mismatched parentheses.") % lockdef)
|
||||
return False
|
||||
if not _RE_FUNCS.findall(rhs):
|
||||
self._log_error(_("Lock: '%s' has no valid lock functions.") % lockdef)
|
||||
return False
|
||||
# get the lock string
|
||||
storage_lockstring = self.obj.lock_storage
|
||||
if storage_lockstring:
|
||||
storage_lockstring = storage_lockstring + ";" + lockstring
|
||||
else:
|
||||
storage_lockstring = lockstring
|
||||
# cache the locks will get rid of eventual doublets
|
||||
self._cache_locks(storage_lockstring)
|
||||
self._save_locks()
|
||||
return True
|
||||
|
||||
def replace(self, lockstring):
|
||||
"Replaces the lockstring entirely."
|
||||
old_lockstring = str(self)
|
||||
self.clear()
|
||||
try:
|
||||
return self.add(lockstring)
|
||||
except LockException:
|
||||
self.add(old_lockstring)
|
||||
raise
|
||||
|
||||
def get(self, access_type=None):
|
||||
"get the full lockstring or the lockstring of a particular access type."
|
||||
if access_type:
|
||||
return self.locks.get(access_type, ["", "", ""])[2]
|
||||
return str(self)
|
||||
|
||||
def delete(self, access_type):
|
||||
"Remove a lock from the handler"
|
||||
if access_type in self.locks:
|
||||
del self.locks[access_type]
|
||||
self._save_locks()
|
||||
return True
|
||||
return False
|
||||
|
||||
def clear(self):
|
||||
"Remove all locks"
|
||||
self.locks = {}
|
||||
self.lock_storage = ""
|
||||
self._save_locks()
|
||||
|
||||
def reset(self):
|
||||
"""
|
||||
Set the reset flag, so the the lock will be re-cached at next checking.
|
||||
This is usually set by @reload.
|
||||
"""
|
||||
self._cache_locks(self.obj.lock_storage)
|
||||
self.cache_lock_bypass(self.obj)
|
||||
|
||||
def check(self, accessing_obj, access_type, default=False, no_superuser_bypass=False):
|
||||
"""
|
||||
Checks a lock of the correct type by passing execution
|
||||
off to the lock function(s).
|
||||
|
||||
accessing_obj - the object seeking access
|
||||
access_type - the type of access wanted
|
||||
default - if no suitable lock type is found, use this
|
||||
no_superuser_bypass - don't use this unless you really, really need to,
|
||||
it makes supersusers susceptible to the lock check.
|
||||
|
||||
A lock is executed in the follwoing way:
|
||||
|
||||
Parsing the lockstring, we (during cache) extract the valid
|
||||
lock functions and store their function objects in the right
|
||||
order along with their args/kwargs. These are now executed in
|
||||
sequence, creating a list of True/False values. This is put
|
||||
into the evalstring, which is a string of AND/OR/NOT entries
|
||||
separated by placeholders where each function result should
|
||||
go. We just put those results in and evaluate the string to
|
||||
get a final, combined True/False value for the lockstring.
|
||||
|
||||
The important bit with this solution is that the full
|
||||
lockstring is never blindly evaluated, and thus there (should
|
||||
be) no way to sneak in malign code in it. Only "safe" lock
|
||||
functions (as defined by your settings) are executed.
|
||||
|
||||
"""
|
||||
try:
|
||||
# check if the lock should be bypassed (e.g. superuser status)
|
||||
if accessing_obj.locks.lock_bypass and not no_superuser_bypass:
|
||||
return True
|
||||
except AttributeError:
|
||||
# happens before session is initiated.
|
||||
if not no_superuser_bypass and ((hasattr(accessing_obj, 'is_superuser') and accessing_obj.is_superuser)
|
||||
or (hasattr(accessing_obj, 'player') and hasattr(accessing_obj.player, 'is_superuser') and accessing_obj.player.is_superuser)
|
||||
or (hasattr(accessing_obj, 'get_player') and (not accessing_obj.get_player() or accessing_obj.get_player().is_superuser))):
|
||||
return True
|
||||
|
||||
# no superuser or bypass -> normal lock operation
|
||||
if access_type in self.locks:
|
||||
# we have a lock, test it.
|
||||
evalstring, func_tup, raw_string = self.locks[access_type]
|
||||
# execute all lock funcs in the correct order, producing a tuple of True/False results.
|
||||
true_false = tuple(bool(tup[0](accessing_obj, self.obj, *tup[1], **tup[2])) for tup in func_tup)
|
||||
# the True/False tuple goes into evalstring, which combines them
|
||||
# with AND/OR/NOT in order to get the final result.
|
||||
return eval(evalstring % true_false)
|
||||
else:
|
||||
return default
|
||||
|
||||
def check_lockstring(self, accessing_obj, lockstring, no_superuser_bypass=False):
|
||||
"""
|
||||
Do a direct check against a lockstring ('atype:func()..'), without any
|
||||
intermediary storage on the accessed object (this can be left
|
||||
to None if the lock functions called don't access it). atype can also be
|
||||
put to a dummy value since no lock selection is made.
|
||||
"""
|
||||
try:
|
||||
if accessing_obj.locks.lock_bypass and not no_superuser_bypass:
|
||||
return True
|
||||
except AttributeError:
|
||||
if no_superuser_bypass and ((hasattr(accessing_obj, 'is_superuser') and accessing_obj.is_superuser)
|
||||
or (hasattr(accessing_obj, 'player') and hasattr(accessing_obj.player, 'is_superuser') and accessing_obj.player.is_superuser)
|
||||
or (hasattr(accessing_obj, 'get_player') and (not accessing_obj.get_player() or accessing_obj.get_player().is_superuser))):
|
||||
return True
|
||||
|
||||
locks = self._parse_lockstring(lockstring)
|
||||
for access_type in locks:
|
||||
evalstring, func_tup, raw_string = locks[access_type]
|
||||
true_false = tuple(tup[0](accessing_obj, self.obj, *tup[1],**tup[2])
|
||||
for tup in func_tup)
|
||||
return eval(evalstring % true_false)
|
||||
|
||||
|
||||
def _test():
|
||||
# testing
|
||||
|
||||
class TestObj(object):
|
||||
pass
|
||||
|
||||
import pdb
|
||||
obj1 = TestObj()
|
||||
obj2 = TestObj()
|
||||
|
||||
#obj1.lock_storage = "owner:dbref(#4);edit:dbref(#5) or perm(Wizards);examine:perm(Builders);delete:perm(Wizards);get:all()"
|
||||
#obj1.lock_storage = "cmd:all();admin:id(1);listen:all();send:all()"
|
||||
obj1.lock_storage = "listen:perm(Immortals)"
|
||||
|
||||
pdb.set_trace()
|
||||
obj1.locks = LockHandler(obj1)
|
||||
obj2.permissions.add("Immortals")
|
||||
obj2.id = 4
|
||||
|
||||
#obj1.locks.add("edit:attr(test)")
|
||||
|
||||
print "comparing obj2.permissions (%s) vs obj1.locks (%s)" % (obj2.permissions, obj1.locks)
|
||||
print obj1.locks.check(obj2, 'owner')
|
||||
print obj1.locks.check(obj2, 'edit')
|
||||
print obj1.locks.check(obj2, 'examine')
|
||||
print obj1.locks.check(obj2, 'delete')
|
||||
print obj1.locks.check(obj2, 'get')
|
||||
print obj1.locks.check(obj2, 'listen')
|
||||
63
lib/locks/tests.py
Normal file
63
lib/locks/tests.py
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
This is part of Evennia's unittest framework, for testing
|
||||
the stability and integrrity of the codebase during updates.
|
||||
|
||||
This module tests the lock functionality of Evennia.
|
||||
|
||||
"""
|
||||
|
||||
try:
|
||||
# this is a special optimized Django version, only available in current Django devel
|
||||
from django.utils.unittest import TestCase
|
||||
except ImportError:
|
||||
from django.test import TestCase
|
||||
|
||||
from django.conf import settings
|
||||
from src.locks import lockfuncs
|
||||
from src.utils import create
|
||||
|
||||
#------------------------------------------------------------
|
||||
#
|
||||
# Lock testing
|
||||
#
|
||||
#------------------------------------------------------------
|
||||
|
||||
class LockTest(TestCase):
|
||||
"Defines the lock test base"
|
||||
def setUp(self):
|
||||
"sets up the testing environment"
|
||||
|
||||
self.obj1 = create.create_object(settings.BASE_OBJECT_TYPECLASS, key="obj1")
|
||||
self.obj2 = create.create_object(settings.BASE_OBJECT_TYPECLASS, key="obj2")
|
||||
|
||||
class TestLockCheck(LockTest):
|
||||
def testrun(self):
|
||||
dbref = self.obj2.dbref
|
||||
self.obj1.locks.add("owner:dbref(%s);edit:dbref(%s) or perm(Wizards);examine:perm(Builders) and id(%s);delete:perm(Wizards);get:all()" % (dbref, dbref, dbref))
|
||||
self.obj2.permissions.add('Wizards')
|
||||
self.assertEquals(True, self.obj1.locks.check(self.obj2, 'owner'))
|
||||
self.assertEquals(True, self.obj1.locks.check(self.obj2, 'edit'))
|
||||
self.assertEquals(True, self.obj1.locks.check(self.obj2, 'examine'))
|
||||
self.assertEquals(True, self.obj1.locks.check(self.obj2, 'delete'))
|
||||
self.assertEquals(True, self.obj1.locks.check(self.obj2, 'get'))
|
||||
self.obj1.locks.add("get:false()")
|
||||
self.assertEquals(False, self.obj1.locks.check(self.obj2, 'get'))
|
||||
self.assertEquals(True, self.obj1.locks.check(self.obj2, 'not_exist', default=True))
|
||||
class TestLockfuncs(LockTest):
|
||||
def testrun(self):
|
||||
self.obj2.permissions.add('Wizards')
|
||||
self.assertEquals(True, lockfuncs.true(self.obj2, self.obj1))
|
||||
self.assertEquals(False, lockfuncs.false(self.obj2, self.obj1))
|
||||
self.assertEquals(True, lockfuncs.perm(self.obj2, self.obj1, 'Wizards'))
|
||||
self.assertEquals(True, lockfuncs.perm_above(self.obj2, self.obj1, 'Builders'))
|
||||
dbref = self.obj2.dbref
|
||||
self.assertEquals(True, lockfuncs.dbref(self.obj2, self.obj1, '%s' % dbref))
|
||||
self.obj2.db.testattr = 45
|
||||
self.assertEquals(True, lockfuncs.attr(self.obj2, self.obj1, 'testattr', '45'))
|
||||
self.assertEquals(False, lockfuncs.attr_gt(self.obj2, self.obj1, 'testattr', '45'))
|
||||
self.assertEquals(True, lockfuncs.attr_ge(self.obj2, self.obj1, 'testattr', '45'))
|
||||
self.assertEquals(False, lockfuncs.attr_lt(self.obj2, self.obj1, 'testattr', '45'))
|
||||
self.assertEquals(True, lockfuncs.attr_le(self.obj2, self.obj1, 'testattr', '45'))
|
||||
self.assertEquals(False, lockfuncs.attr_ne(self.obj2, self.obj1, 'testattr', '45'))
|
||||
11
lib/objects/__init__.py
Normal file
11
lib/objects/__init__.py
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
"""
|
||||
Makes it easier to import by grouping all relevant things already at this level.
|
||||
|
||||
You can henceforth import most things directly from src.objects
|
||||
Also, the initiated object manager is available as src.objects.manager.
|
||||
|
||||
"""
|
||||
|
||||
#from src.objects.objects import *
|
||||
#from src.objects.models import ObjectDB
|
||||
#manager = ObjectDB.objects
|
||||
128
lib/objects/admin.py
Normal file
128
lib/objects/admin.py
Normal file
|
|
@ -0,0 +1,128 @@
|
|||
#
|
||||
# This sets up how models are displayed
|
||||
# in the web admin interface.
|
||||
#
|
||||
|
||||
from django import forms
|
||||
from django.conf import settings
|
||||
from django.contrib import admin
|
||||
from src.typeclasses.admin import AttributeInline, TagInline
|
||||
from src.objects.models import ObjectDB
|
||||
|
||||
|
||||
class ObjectAttributeInline(AttributeInline):
|
||||
model = ObjectDB.db_attributes.through
|
||||
|
||||
|
||||
class ObjectTagInline(TagInline):
|
||||
model = ObjectDB.db_tags.through
|
||||
|
||||
|
||||
class ObjectCreateForm(forms.ModelForm):
|
||||
"This form details the look of the fields"
|
||||
class Meta:
|
||||
model = ObjectDB
|
||||
fields = '__all__'
|
||||
db_key = forms.CharField(label="Name/Key",
|
||||
widget=forms.TextInput(attrs={'size': '78'}),
|
||||
help_text="Main identifier, like 'apple', 'strong guy', 'Elizabeth' etc. If creating a Character, check so the name is unique among characters!",)
|
||||
db_typeclass_path = forms.CharField(label="Typeclass",
|
||||
initial=settings.BASE_OBJECT_TYPECLASS,
|
||||
widget=forms.TextInput(attrs={'size': '78'}),
|
||||
help_text="This defines what 'type' of entity this is. This variable holds a Python path to a module with a valid Evennia Typeclass. If you are creating a Character you should use the typeclass defined by settings.BASE_CHARACTER_TYPECLASS or one derived from that.")
|
||||
db_cmdset_storage = forms.CharField(label="CmdSet",
|
||||
initial="",
|
||||
required=False,
|
||||
widget=forms.TextInput(attrs={'size': '78'}),
|
||||
help_text="Most non-character objects don't need a cmdset and can leave this field blank.")
|
||||
raw_id_fields = ('db_destination', 'db_location', 'db_home')
|
||||
|
||||
|
||||
class ObjectEditForm(ObjectCreateForm):
|
||||
"Form used for editing. Extends the create one with more fields"
|
||||
|
||||
class Meta:
|
||||
fields = '__all__'
|
||||
db_lock_storage = forms.CharField(label="Locks",
|
||||
required=False,
|
||||
widget=forms.Textarea(attrs={'cols':'100', 'rows':'2'}),
|
||||
help_text="In-game lock definition string. If not given, defaults will be used. This string should be on the form <i>type:lockfunction(args);type2:lockfunction2(args);...")
|
||||
|
||||
|
||||
class ObjectDBAdmin(admin.ModelAdmin):
|
||||
|
||||
inlines = [ObjectTagInline, ObjectAttributeInline]
|
||||
list_display = ('id', 'db_key', 'db_player', 'db_typeclass_path')
|
||||
list_display_links = ('id', 'db_key')
|
||||
ordering = ['db_player', 'db_typeclass_path', 'id']
|
||||
search_fields = ['^db_key', 'db_typeclass_path']
|
||||
raw_id_fields = ('db_destination', 'db_location', 'db_home')
|
||||
|
||||
save_as = True
|
||||
save_on_top = True
|
||||
list_select_related = True
|
||||
list_filter = ('db_typeclass_path',)
|
||||
#list_filter = ('db_permissions', 'db_typeclass_path')
|
||||
|
||||
# editing fields setup
|
||||
|
||||
form = ObjectEditForm
|
||||
fieldsets = (
|
||||
(None, {
|
||||
'fields': (('db_key','db_typeclass_path'), ('db_lock_storage', ),
|
||||
('db_location', 'db_home'), 'db_destination','db_cmdset_storage'
|
||||
)}),
|
||||
)
|
||||
#fieldsets = (
|
||||
# (None, {
|
||||
# 'fields': (('db_key','db_typeclass_path'), ('db_permissions', 'db_lock_storage'),
|
||||
# ('db_location', 'db_home'), 'db_destination','db_cmdset_storage'
|
||||
# )}),
|
||||
# )
|
||||
|
||||
#deactivated temporarily, they cause empty objects to be created in admin
|
||||
|
||||
# Custom modification to give two different forms wether adding or not.
|
||||
add_form = ObjectCreateForm
|
||||
add_fieldsets = (
|
||||
(None, {
|
||||
'fields': (('db_key','db_typeclass_path'),
|
||||
('db_location', 'db_home'), 'db_destination', 'db_cmdset_storage'
|
||||
)}),
|
||||
)
|
||||
|
||||
#add_fieldsets = (
|
||||
# (None, {
|
||||
# 'fields': (('db_key','db_typeclass_path'), 'db_permissions',
|
||||
# ('db_location', 'db_home'), 'db_destination', 'db_cmdset_storage'
|
||||
# )}),
|
||||
# )
|
||||
def get_fieldsets(self, request, obj=None):
|
||||
if not obj:
|
||||
return self.add_fieldsets
|
||||
return super(ObjectDBAdmin, self).get_fieldsets(request, obj)
|
||||
|
||||
def get_form(self, request, obj=None, **kwargs):
|
||||
"""
|
||||
Use special form during creation
|
||||
"""
|
||||
defaults = {}
|
||||
if obj is None:
|
||||
defaults.update({
|
||||
'form': self.add_form,
|
||||
'fields': admin.util.flatten_fieldsets(self.add_fieldsets),
|
||||
})
|
||||
defaults.update(kwargs)
|
||||
return super(ObjectDBAdmin, self).get_form(request, obj, **defaults)
|
||||
|
||||
def save_model(self, request, obj, form, change):
|
||||
obj.save()
|
||||
if not change:
|
||||
# adding a new object
|
||||
obj.basetype_setup()
|
||||
obj.basetype_posthook_setup()
|
||||
obj.at_object_creation()
|
||||
obj.at_init()
|
||||
|
||||
|
||||
admin.site.register(ObjectDB, ObjectDBAdmin)
|
||||
416
lib/objects/manager.py
Normal file
416
lib/objects/manager.py
Normal file
|
|
@ -0,0 +1,416 @@
|
|||
"""
|
||||
Custom manager for Objects.
|
||||
"""
|
||||
from itertools import chain
|
||||
from django.db.models import Q
|
||||
from django.conf import settings
|
||||
from django.db.models.fields import exceptions
|
||||
from src.typeclasses.managers import TypedObjectManager, TypeclassManager
|
||||
from src.typeclasses.managers import returns_typeclass, returns_typeclass_list
|
||||
from src.utils import utils
|
||||
from src.utils.utils import to_unicode, is_iter, make_iter, string_partial_matching
|
||||
|
||||
__all__ = ("ObjectManager",)
|
||||
_GA = object.__getattribute__
|
||||
|
||||
# delayed import
|
||||
_ATTR = None
|
||||
|
||||
|
||||
# Try to use a custom way to parse id-tagged multimatches.
|
||||
|
||||
_AT_MULTIMATCH_INPUT = utils.variable_from_module(*settings.SEARCH_AT_MULTIMATCH_INPUT.rsplit('.', 1))
|
||||
|
||||
|
||||
class ObjectDBManager(TypedObjectManager):
|
||||
"""
|
||||
This ObjectManager implementes methods for searching
|
||||
and manipulating Objects directly from the database.
|
||||
|
||||
Evennia-specific search methods (will return Typeclasses or
|
||||
lists of Typeclasses, whereas Django-general methods will return
|
||||
Querysets or database objects).
|
||||
|
||||
dbref (converter)
|
||||
get_id (alias: dbref_search)
|
||||
get_dbref_range
|
||||
object_totals
|
||||
typeclass_search
|
||||
get_object_with_player
|
||||
get_objs_with_key_and_typeclass
|
||||
get_objs_with_attr
|
||||
get_objs_with_attr_match
|
||||
get_objs_with_db_property
|
||||
get_objs_with_db_property_match
|
||||
get_objs_with_key_or_alias
|
||||
get_contents
|
||||
object_search (interface to many of the above methods,
|
||||
equivalent to ev.search_object)
|
||||
copy_object
|
||||
|
||||
"""
|
||||
|
||||
#
|
||||
# ObjectManager Get methods
|
||||
#
|
||||
|
||||
# player related
|
||||
|
||||
@returns_typeclass
|
||||
def get_object_with_player(self, ostring, exact=True, candidates=None):
|
||||
"""
|
||||
Search for an object based on its player's name or dbref.
|
||||
This search
|
||||
is sometimes initiated by appending a * to the beginning of
|
||||
the search criterion (e.g. in local_and_global_search).
|
||||
search_string: (string) The name or dbref to search for.
|
||||
"""
|
||||
ostring = to_unicode(ostring).lstrip('*')
|
||||
# simplest case - search by dbref
|
||||
dbref = self.dbref(ostring)
|
||||
if dbref:
|
||||
return dbref
|
||||
# not a dbref. Search by name.
|
||||
cand_restriction = candidates != None and Q(pk__in=[_GA(obj, "id") for obj in make_iter(candidates) if obj]) or Q()
|
||||
if exact:
|
||||
return self.filter(cand_restriction & Q(db_player__username__iexact=ostring))
|
||||
else: # fuzzy matching
|
||||
ply_cands = self.filter(cand_restriction & Q(playerdb__username__istartswith=ostring)).values_list("db_key", flat=True)
|
||||
if candidates:
|
||||
index_matches = string_partial_matching(ply_cands, ostring, ret_index=True)
|
||||
return [obj for ind, obj in enumerate(make_iter(candidates)) if ind in index_matches]
|
||||
else:
|
||||
return string_partial_matching(ply_cands, ostring, ret_index=False)
|
||||
|
||||
@returns_typeclass_list
|
||||
def get_objs_with_key_and_typeclass(self, oname, otypeclass_path, candidates=None):
|
||||
"""
|
||||
Returns objects based on simultaneous key and typeclass match.
|
||||
"""
|
||||
cand_restriction = candidates != None and Q(pk__in=[_GA(obj, "id") for obj in make_iter(candidates) if obj]) or Q()
|
||||
return self.filter(cand_restriction & Q(db_key__iexact=oname, db_typeclass_path__exact=otypeclass_path))
|
||||
|
||||
# attr/property related
|
||||
|
||||
@returns_typeclass_list
|
||||
def get_objs_with_attr(self, attribute_name, candidates=None):
|
||||
"""
|
||||
Returns all objects having the given attribute_name defined at all.
|
||||
Location should be a valid location object.
|
||||
"""
|
||||
cand_restriction = candidates != None and Q(db_attributes__db_obj__pk__in=[_GA(obj, "id") for obj in make_iter(candidates) if obj]) or Q()
|
||||
return list(self.filter(cand_restriction & Q(db_attributes__db_key=attribute_name)))
|
||||
|
||||
@returns_typeclass_list
|
||||
def get_objs_with_attr_value(self, attribute_name, attribute_value, candidates=None, typeclasses=None):
|
||||
"""
|
||||
Returns all objects having the valid attrname set to the given value.
|
||||
|
||||
candidates - list of candidate objects to search
|
||||
typeclasses - list of typeclass-path strings to restrict matches with
|
||||
|
||||
This uses the Attribute's PickledField to transparently search the database by matching
|
||||
the internal representation. This is reasonably effective but since Attribute values
|
||||
cannot be indexed, searching by Attribute key is to be preferred whenever possible.
|
||||
"""
|
||||
cand_restriction = candidates != None and Q(pk__in=[_GA(obj, "id") for obj in make_iter(candidates) if obj]) or Q()
|
||||
type_restriction = typeclasses and Q(db_typeclass_path__in=make_iter(typeclasses)) or Q()
|
||||
|
||||
## This doesn't work if attribute_value is an object. Workaround below
|
||||
|
||||
if isinstance(attribute_value, (basestring, int, float, bool, long)):
|
||||
return self.filter(cand_restriction & type_restriction & Q(db_attributes__db_key=attribute_name, db_attributes__db_value=attribute_value))
|
||||
else:
|
||||
# We have to loop for safety since the referenced lookup gives deepcopy error if attribute value is an object.
|
||||
global _ATTR
|
||||
if not _ATTR:
|
||||
from src.typeclasses.models import Attribute as _ATTR
|
||||
cands = list(self.filter(cand_restriction & type_restriction & Q(db_attributes__db_key=attribute_name)))
|
||||
results = [attr.objectdb_set.all() for attr in _ATTR.objects.filter(objectdb__in=cands, db_value=attribute_value)]
|
||||
return chain(*results)
|
||||
|
||||
@returns_typeclass_list
|
||||
def get_objs_with_db_property(self, property_name, candidates=None):
|
||||
"""
|
||||
Returns all objects having a given db field property.
|
||||
property_name = search string
|
||||
candidates - list of candidate objects to search
|
||||
"""
|
||||
property_name = "db_%s" % property_name.lstrip('db_')
|
||||
cand_restriction = candidates != None and Q(pk__in=[_GA(obj, "id") for obj in make_iter(candidates) if obj]) or Q()
|
||||
querykwargs = {property_name:None}
|
||||
try:
|
||||
return list(self.filter(cand_restriction).exclude(Q(**querykwargs)))
|
||||
except exceptions.FieldError:
|
||||
return []
|
||||
|
||||
@returns_typeclass_list
|
||||
def get_objs_with_db_property_value(self, property_name, property_value, candidates=None, typeclasses=None):
|
||||
"""
|
||||
Returns all objects having a given db field property.
|
||||
candidates - list of objects to search
|
||||
typeclasses - list of typeclass-path strings to restrict matches with
|
||||
"""
|
||||
if isinstance(property_value, basestring):
|
||||
property_value = to_unicode(property_value)
|
||||
if isinstance(property_name, basestring):
|
||||
if not property_name.startswith('db_'):
|
||||
property_name = "db_%s" % property_name
|
||||
querykwargs = {property_name:property_value}
|
||||
cand_restriction = candidates != None and Q(pk__in=[_GA(obj, "id") for obj in make_iter(candidates) if obj]) or Q()
|
||||
type_restriction = typeclasses and Q(db_typeclass_path__in=make_iter(typeclasses)) or Q()
|
||||
try:
|
||||
return list(self.filter(cand_restriction & type_restriction & Q(**querykwargs)))
|
||||
except exceptions.FieldError:
|
||||
return []
|
||||
except ValueError:
|
||||
from src.utils import logger
|
||||
logger.log_errmsg("The property '%s' does not support search criteria of the type %s." % (property_name, type(property_value)))
|
||||
return []
|
||||
|
||||
@returns_typeclass_list
|
||||
def get_contents(self, location, excludeobj=None):
|
||||
"""
|
||||
Get all objects that has a location
|
||||
set to this one.
|
||||
|
||||
excludeobj - one or more object keys to exclude from the match
|
||||
"""
|
||||
exclude_restriction = Q(pk__in=[_GA(obj, "id") for obj in make_iter(excludeobj)]) if excludeobj else Q()
|
||||
return self.filter(db_location=location).exclude(exclude_restriction)
|
||||
|
||||
@returns_typeclass_list
|
||||
def get_objs_with_key_or_alias(self, ostring, exact=True,
|
||||
candidates=None, typeclasses=None):
|
||||
"""
|
||||
Returns objects based on key or alias match. Will also do fuzzy
|
||||
matching based on the utils.string_partial_matching function.
|
||||
candidates - list of candidate objects to restrict on
|
||||
typeclasses - list of typeclass path strings to restrict on
|
||||
"""
|
||||
if not isinstance(ostring, basestring):
|
||||
if hasattr(ostring, "key"):
|
||||
ostring = ostring.key
|
||||
else:
|
||||
return []
|
||||
if is_iter(candidates) and not len(candidates):
|
||||
# if candidates is an empty iterable there can be no matches
|
||||
# Exit early.
|
||||
return []
|
||||
|
||||
# build query objects
|
||||
candidates_id = [_GA(obj, "id") for obj in make_iter(candidates) if obj]
|
||||
cand_restriction = candidates != None and Q(pk__in=make_iter(candidates_id)) or Q()
|
||||
type_restriction = typeclasses and Q(db_typeclass_path__in=make_iter(typeclasses)) or Q()
|
||||
if exact:
|
||||
# exact match - do direct search
|
||||
return self.filter(cand_restriction & type_restriction & (Q(db_key__iexact=ostring) |
|
||||
Q(db_tags__db_key__iexact=ostring) & Q(db_tags__db_tagtype__iexact="alias"))).distinct()
|
||||
elif candidates:
|
||||
# fuzzy with candidates
|
||||
key_candidates = self.filter(cand_restriction & type_restriction)
|
||||
else:
|
||||
# fuzzy without supplied candidates - we select our own candidates
|
||||
key_candidates = self.filter(type_restriction & (Q(db_key__istartswith=ostring) | Q(db_tags__db_key__istartswith=ostring))).distinct()
|
||||
candidates_id = [_GA(obj, "id") for obj in key_candidates]
|
||||
# fuzzy matching
|
||||
key_strings = key_candidates.values_list("db_key", flat=True).order_by("id")
|
||||
|
||||
index_matches = string_partial_matching(key_strings, ostring, ret_index=True)
|
||||
if index_matches:
|
||||
return [obj for ind, obj in enumerate(key_candidates) if ind in index_matches]
|
||||
else:
|
||||
alias_candidates = self.filter(id__in=candidates_id, db_tags__db_tagtype__iexact="alias")
|
||||
alias_strings = alias_candidates.values_list("db_key", flat=True)
|
||||
index_matches = string_partial_matching(alias_strings, ostring, ret_index=True)
|
||||
if index_matches:
|
||||
return [alias.db_obj for ind, alias in enumerate(alias_candidates) if ind in index_matches]
|
||||
return []
|
||||
|
||||
# main search methods and helper functions
|
||||
|
||||
@returns_typeclass_list
|
||||
def object_search(self, searchdata,
|
||||
attribute_name=None,
|
||||
typeclass=None,
|
||||
candidates=None,
|
||||
exact=True):
|
||||
"""
|
||||
Search as an object globally or in a list of candidates and return
|
||||
results. The result is always an Object. Always returns a list.
|
||||
|
||||
Arguments:
|
||||
searchdata: (str or obj) The entity to match for. This is usually a
|
||||
key string but may also be an object itself. By default (if
|
||||
not attribute_name is set), this will search object.key and
|
||||
object.aliases in order. Can also be on the form #dbref,
|
||||
which will, if exact=True be matched against primary key.
|
||||
attribute_name: (str): Use this named ObjectAttribute to match
|
||||
searchdata against, instead of the defaults. If this is
|
||||
the name of a database field (with or without the db_ prefix),
|
||||
that will be matched too.
|
||||
typeclass (str or TypeClass): restrict matches to objects having this
|
||||
typeclass. This will help speed up global searches.
|
||||
candidates (list obj ObjectDBs): If supplied, search will only be
|
||||
performed among the candidates in this list. A common list
|
||||
of candidates is the contents of the current location
|
||||
searched.
|
||||
exact (bool): Match names/aliases exactly or partially. Partial
|
||||
matching matches the beginning of words in the names/aliases,
|
||||
using a matching routine to separate multiple matches in
|
||||
names with multiple components (so "bi sw" will match
|
||||
"Big sword"). Since this is more expensive than exact
|
||||
matching, it is recommended to be used together with the
|
||||
objlist keyword to limit the number of possibilities. This
|
||||
value has no meaning if searching for attributes/properties.
|
||||
|
||||
Returns:
|
||||
A list of matching objects (or a list with one unique match)
|
||||
"""
|
||||
def _searcher(searchdata, candidates, typeclass, exact=False):
|
||||
"""
|
||||
Helper method for searching objects. typeclass is only used
|
||||
for global searching (no candidates)
|
||||
"""
|
||||
if attribute_name:
|
||||
# attribute/property search (always exact).
|
||||
matches = self.get_objs_with_db_property_value(attribute_name, searchdata, candidates=candidates, typeclasses=typeclass)
|
||||
if matches:
|
||||
return matches
|
||||
return self.get_objs_with_attr_value(attribute_name, searchdata, candidates=candidates, typeclasses=typeclass)
|
||||
else:
|
||||
# normal key/alias search
|
||||
return self.get_objs_with_key_or_alias(searchdata, exact=exact, candidates=candidates, typeclasses=typeclass)
|
||||
|
||||
if not searchdata and searchdata != 0:
|
||||
return []
|
||||
|
||||
if typeclass:
|
||||
# typeclass may also be a list
|
||||
typeclasses = make_iter(typeclass)
|
||||
for i, typeclass in enumerate(make_iter(typeclasses)):
|
||||
if callable(typeclass):
|
||||
typeclasses[i] = u"%s.%s" % (typeclass.__module__, typeclass.__name__)
|
||||
else:
|
||||
typeclasses[i] = u"%s" % typeclass
|
||||
typeclass = typeclasses
|
||||
|
||||
if candidates:
|
||||
# Convenience check to make sure candidates are really dbobjs
|
||||
candidates = [cand for cand in make_iter(candidates) if cand]
|
||||
if typeclass:
|
||||
candidates = [cand for cand in candidates
|
||||
if _GA(cand, "db_typeclass_path") in typeclass]
|
||||
|
||||
dbref = not attribute_name and exact and self.dbref(searchdata)
|
||||
if dbref is not None:
|
||||
# Easiest case - dbref matching (always exact)
|
||||
dbref_match = self.dbref_search(dbref)
|
||||
if dbref_match:
|
||||
if not candidates or dbref_match in candidates:
|
||||
return [dbref_match]
|
||||
else:
|
||||
return []
|
||||
|
||||
# Search through all possibilities.
|
||||
match_number = None
|
||||
# always run first check exact - we don't want partial matches
|
||||
# if on the form of 1-keyword etc.
|
||||
matches = _searcher(searchdata, candidates, typeclass, exact=True)
|
||||
if not matches:
|
||||
# no matches found - check if we are dealing with N-keyword
|
||||
# query - if so, strip it.
|
||||
match_number, searchdata = _AT_MULTIMATCH_INPUT(searchdata)
|
||||
# run search again, with the exactness set by call
|
||||
if match_number is not None or not exact:
|
||||
matches = _searcher(searchdata, candidates, typeclass, exact=exact)
|
||||
|
||||
# deal with result
|
||||
if len(matches) > 1 and match_number is not None:
|
||||
# multiple matches, but a number was given to separate them
|
||||
try:
|
||||
matches = [matches[match_number]]
|
||||
except IndexError:
|
||||
pass
|
||||
# return a list (possibly empty)
|
||||
return matches
|
||||
|
||||
#
|
||||
# ObjectManager Copy method
|
||||
#
|
||||
|
||||
def copy_object(self, original_object, new_key=None,
|
||||
new_location=None, new_home=None,
|
||||
new_permissions=None, new_locks=None,
|
||||
new_aliases=None, new_destination=None):
|
||||
"""
|
||||
Create and return a new object as a copy of the original object. All
|
||||
will be identical to the original except for the arguments given
|
||||
specifically to this method.
|
||||
|
||||
original_object (obj) - the object to make a copy from
|
||||
new_key (str) - name the copy differently from the original.
|
||||
new_location (obj) - if not None, change the location
|
||||
new_home (obj) - if not None, change the Home
|
||||
new_aliases (list of strings) - if not None, change object aliases.
|
||||
new_destination (obj) - if not None, change destination
|
||||
"""
|
||||
|
||||
# get all the object's stats
|
||||
typeclass_path = original_object.typeclass_path
|
||||
if not new_key:
|
||||
new_key = original_object.key
|
||||
if not new_location:
|
||||
new_location = original_object.location
|
||||
if not new_home:
|
||||
new_home = original_object.home
|
||||
if not new_aliases:
|
||||
new_aliases = original_object.aliases.all()
|
||||
if not new_locks:
|
||||
new_locks = original_object.db_lock_storage
|
||||
if not new_permissions:
|
||||
new_permissions = original_object.permissions.all()
|
||||
if not new_destination:
|
||||
new_destination = original_object.destination
|
||||
|
||||
# create new object
|
||||
from src.utils import create
|
||||
from src.scripts.models import ScriptDB
|
||||
new_object = create.create_object(typeclass_path,
|
||||
key=new_key,
|
||||
location=new_location,
|
||||
home=new_home,
|
||||
permissions=new_permissions,
|
||||
locks=new_locks,
|
||||
aliases=new_aliases,
|
||||
destination=new_destination)
|
||||
if not new_object:
|
||||
return None
|
||||
|
||||
# copy over all attributes from old to new.
|
||||
for attr in original_object.attributes.all():
|
||||
new_object.attributes.add(attr.key, attr.value)
|
||||
|
||||
# copy over all cmdsets, if any
|
||||
for icmdset, cmdset in enumerate(original_object.cmdset.all()):
|
||||
if icmdset == 0:
|
||||
new_object.cmdset.add_default(cmdset)
|
||||
else:
|
||||
new_object.cmdset.add(cmdset)
|
||||
|
||||
# copy over all scripts, if any
|
||||
for script in original_object.scripts.all():
|
||||
ScriptDB.objects.copy_script(script, new_obj=new_object)
|
||||
|
||||
return new_object
|
||||
|
||||
def clear_all_sessids(self):
|
||||
"""
|
||||
Clear the db_sessid field of all objects having also the db_player field
|
||||
set.
|
||||
"""
|
||||
self.filter(db_sessid__isnull=False).update(db_sessid=None)
|
||||
|
||||
|
||||
class ObjectManager(ObjectDBManager, TypeclassManager):
|
||||
pass
|
||||
36
lib/objects/migrations/0001_initial.py
Normal file
36
lib/objects/migrations/0001_initial.py
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import models, migrations
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('typeclasses', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='ObjectDB',
|
||||
fields=[
|
||||
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
|
||||
('db_key', models.CharField(max_length=255, verbose_name=b'key', db_index=True)),
|
||||
('db_typeclass_path', models.CharField(help_text=b"this defines what 'type' of entity this is. This variable holds a Python path to a module with a valid Evennia Typeclass.", max_length=255, null=True, verbose_name=b'typeclass')),
|
||||
('db_date_created', models.DateTimeField(auto_now_add=True, verbose_name=b'creation date')),
|
||||
('db_lock_storage', models.TextField(help_text=b"locks limit access to an entity. A lock is defined as a 'lock string' on the form 'type:lockfunctions', defining what functionality is locked and how to determine access. Not defining a lock means no access is granted.", verbose_name=b'locks', blank=True)),
|
||||
('db_sessid', models.CommaSeparatedIntegerField(help_text=b'csv list of session ids of connected Player, if any.', max_length=32, null=True, verbose_name=b'session id')),
|
||||
('db_cmdset_storage', models.CharField(help_text=b'optional python path to a cmdset class.', max_length=255, null=True, verbose_name=b'cmdset', blank=True)),
|
||||
('db_attributes', models.ManyToManyField(help_text=b'attributes on this object. An attribute can hold any pickle-able python object (see docs for special cases).', to='typeclasses.Attribute', null=True)),
|
||||
('db_destination', models.ForeignKey(related_name=b'destinations_set', on_delete=django.db.models.deletion.SET_NULL, blank=True, to='objects.ObjectDB', help_text=b'a destination, used only by exit objects.', null=True, verbose_name=b'destination')),
|
||||
('db_home', models.ForeignKey(related_name=b'homes_set', on_delete=django.db.models.deletion.SET_NULL, verbose_name=b'home location', blank=True, to='objects.ObjectDB', null=True)),
|
||||
('db_location', models.ForeignKey(related_name=b'locations_set', on_delete=django.db.models.deletion.SET_NULL, verbose_name=b'game location', blank=True, to='objects.ObjectDB', null=True)),
|
||||
],
|
||||
options={
|
||||
'verbose_name': 'Object',
|
||||
'verbose_name_plural': 'Objects',
|
||||
},
|
||||
bases=(models.Model,),
|
||||
),
|
||||
]
|
||||
30
lib/objects/migrations/0002_auto_20140917_0756.py
Normal file
30
lib/objects/migrations/0002_auto_20140917_0756.py
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import models, migrations
|
||||
import django.db.models.deletion
|
||||
from django.conf import settings
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('objects', '0001_initial'),
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
('typeclasses', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='objectdb',
|
||||
name='db_player',
|
||||
field=models.ForeignKey(on_delete=django.db.models.deletion.SET_NULL, verbose_name=b'player', to=settings.AUTH_USER_MODEL, help_text=b'a Player connected to this object, if any.', null=True),
|
||||
preserve_default=True,
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='objectdb',
|
||||
name='db_tags',
|
||||
field=models.ManyToManyField(help_text=b'tags on this object. Tags are simple string markers to identify, group and alias objects.', to='typeclasses.Tag', null=True),
|
||||
preserve_default=True,
|
||||
),
|
||||
]
|
||||
0
lib/objects/migrations/__init__.py
Normal file
0
lib/objects/migrations/__init__.py
Normal file
178
lib/objects/models.py
Normal file
178
lib/objects/models.py
Normal file
|
|
@ -0,0 +1,178 @@
|
|||
"""
|
||||
This module defines the database models for all in-game objects, that
|
||||
is, all objects that has an actual existence in-game.
|
||||
|
||||
Each database object is 'decorated' with a 'typeclass', a normal
|
||||
python class that implements all the various logics needed by the game
|
||||
in question. Objects created of this class transparently communicate
|
||||
with its related database object for storing all attributes. The
|
||||
admin should usually not have to deal directly with this database
|
||||
object layer.
|
||||
|
||||
Attributes are separate objects that store values persistently onto
|
||||
the database object. Like everything else, they can be accessed
|
||||
transparently through the decorating TypeClass.
|
||||
"""
|
||||
|
||||
from django.db import models
|
||||
from django.conf import settings
|
||||
from django.core.exceptions import ObjectDoesNotExist
|
||||
|
||||
from src.typeclasses.models import TypedObject
|
||||
from src.objects.manager import ObjectDBManager
|
||||
from src.utils import logger
|
||||
from src.utils.utils import (make_iter, dbref)
|
||||
|
||||
|
||||
#------------------------------------------------------------
|
||||
#
|
||||
# ObjectDB
|
||||
#
|
||||
#------------------------------------------------------------
|
||||
|
||||
class ObjectDB(TypedObject):
|
||||
"""
|
||||
All objects in the game use the ObjectDB model to store
|
||||
data in the database. This is handled transparently through
|
||||
the typeclass system.
|
||||
|
||||
Note that the base objectdb is very simple, with
|
||||
few defined fields. Use attributes to extend your
|
||||
type class with new database-stored variables.
|
||||
|
||||
The TypedObject supplies the following (inherited) properties:
|
||||
key - main name
|
||||
name - alias for key
|
||||
typeclass_path - the path to the decorating typeclass
|
||||
typeclass - auto-linked typeclass
|
||||
date_created - time stamp of object creation
|
||||
permissions - perm strings
|
||||
locks - lock definitions (handler)
|
||||
dbref - #id of object
|
||||
db - persistent attribute storage
|
||||
ndb - non-persistent attribute storage
|
||||
|
||||
The ObjectDB adds the following properties:
|
||||
player - optional connected player (always together with sessid)
|
||||
sessid - optional connection session id (always together with player)
|
||||
location - in-game location of object
|
||||
home - safety location for object (handler)
|
||||
|
||||
scripts - scripts assigned to object (handler from typeclass)
|
||||
cmdset - active cmdset on object (handler from typeclass)
|
||||
aliases - aliases for this object (property)
|
||||
nicks - nicknames for *other* things in Evennia (handler)
|
||||
sessions - sessions connected to this object (see also player)
|
||||
has_player - bool if an active player is currently connected
|
||||
contents - other objects having this object as location
|
||||
exits - exits from this object
|
||||
"""
|
||||
#
|
||||
# ObjectDB Database model setup
|
||||
#
|
||||
#
|
||||
# inherited fields (from TypedObject):
|
||||
# db_key (also 'name' works), db_typeclass_path, db_date_created,
|
||||
# db_permissions
|
||||
#
|
||||
# These databse fields (including the inherited ones) should normally be
|
||||
# managed by their corresponding wrapper properties, named same as the
|
||||
# field, but without the db_* prefix (e.g. the db_key field is set with
|
||||
# self.key instead). The wrappers are created at the metaclass level and
|
||||
# will automatically save and cache the data more efficiently.
|
||||
|
||||
# If this is a character object, the player is connected here.
|
||||
db_player = models.ForeignKey("players.PlayerDB", null=True, verbose_name='player', on_delete=models.SET_NULL,
|
||||
help_text='a Player connected to this object, if any.')
|
||||
# the session id associated with this player, if any
|
||||
db_sessid = models.CommaSeparatedIntegerField(null=True, max_length=32, verbose_name="session id",
|
||||
help_text="csv list of session ids of connected Player, if any.")
|
||||
# The location in the game world. Since this one is likely
|
||||
# to change often, we set this with the 'location' property
|
||||
# to transparently handle Typeclassing.
|
||||
db_location = models.ForeignKey('self', related_name="locations_set", db_index=True, on_delete=models.SET_NULL,
|
||||
blank=True, null=True, verbose_name='game location')
|
||||
# a safety location, this usually don't change much.
|
||||
db_home = models.ForeignKey('self', related_name="homes_set", on_delete=models.SET_NULL,
|
||||
blank=True, null=True, verbose_name='home location')
|
||||
# destination of this object - primarily used by exits.
|
||||
db_destination = models.ForeignKey('self', related_name="destinations_set", db_index=True, on_delete=models.SET_NULL,
|
||||
blank=True, null=True, verbose_name='destination',
|
||||
help_text='a destination, used only by exit objects.')
|
||||
# database storage of persistant cmdsets.
|
||||
db_cmdset_storage = models.CharField('cmdset', max_length=255, null=True, blank=True,
|
||||
help_text="optional python path to a cmdset class.")
|
||||
|
||||
# Database manager
|
||||
objects = ObjectDBManager()
|
||||
|
||||
# cmdset_storage property handling
|
||||
def __cmdset_storage_get(self):
|
||||
"getter"
|
||||
storage = self.db_cmdset_storage
|
||||
return [path.strip() for path in storage.split(',')] if storage else []
|
||||
|
||||
def __cmdset_storage_set(self, value):
|
||||
"setter"
|
||||
self.db_cmdset_storage = ",".join(str(val).strip() for val in make_iter(value))
|
||||
self.save(update_fields=["db_cmdset_storage"])
|
||||
|
||||
def __cmdset_storage_del(self):
|
||||
"deleter"
|
||||
self.db_cmdset_storage = None
|
||||
self.save(update_fields=["db_cmdset_storage"])
|
||||
cmdset_storage = property(__cmdset_storage_get, __cmdset_storage_set, __cmdset_storage_del)
|
||||
|
||||
# location getsetter
|
||||
def __location_get(self):
|
||||
"Get location"
|
||||
return self.db_location
|
||||
|
||||
def __location_set(self, location):
|
||||
"Set location, checking for loops and allowing dbref"
|
||||
if isinstance(location, (basestring, int)):
|
||||
# allow setting of #dbref
|
||||
dbid = dbref(location, reqhash=False)
|
||||
if dbid:
|
||||
try:
|
||||
location = ObjectDB.objects.get(id=dbid)
|
||||
except ObjectDoesNotExist:
|
||||
# maybe it is just a name that happens to look like a dbid
|
||||
pass
|
||||
try:
|
||||
def is_loc_loop(loc, depth=0):
|
||||
"Recursively traverse target location, trying to catch a loop."
|
||||
if depth > 10:
|
||||
return
|
||||
elif loc == self:
|
||||
raise RuntimeError
|
||||
elif loc == None:
|
||||
raise RuntimeWarning
|
||||
return is_loc_loop(loc.db_location, depth + 1)
|
||||
try:
|
||||
is_loc_loop(location)
|
||||
except RuntimeWarning:
|
||||
pass
|
||||
# actually set the field
|
||||
self.db_location = location
|
||||
self.save(update_fields=["db_location"])
|
||||
except RuntimeError:
|
||||
errmsg = "Error: %s.location = %s creates a location loop." % (self.key, location)
|
||||
logger.log_errmsg(errmsg)
|
||||
raise RuntimeError(errmsg)
|
||||
except Exception, e:
|
||||
errmsg = "Error (%s): %s is not a valid location." % (str(e), location)
|
||||
logger.log_errmsg(errmsg)
|
||||
raise Exception(errmsg)
|
||||
|
||||
def __location_del(self):
|
||||
"Cleanly delete the location reference"
|
||||
self.db_location = None
|
||||
self.save(update_fields=["db_location"])
|
||||
location = property(__location_get, __location_set, __location_del)
|
||||
|
||||
class Meta:
|
||||
"Define Django meta options"
|
||||
verbose_name = "Object"
|
||||
verbose_name_plural = "Objects"
|
||||
|
||||
1500
lib/objects/objects.py
Normal file
1500
lib/objects/objects.py
Normal file
File diff suppressed because it is too large
Load diff
13
lib/players/__init__.py
Normal file
13
lib/players/__init__.py
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
"""
|
||||
Makes it easier to import by grouping all relevant things already at this
|
||||
level.
|
||||
|
||||
You can henceforth import most things directly from src.player
|
||||
Also, the initiated object manager is available as src.players.manager.
|
||||
|
||||
"""
|
||||
|
||||
#from src.players.player import *
|
||||
#from src.players.models import PlayerDB
|
||||
#
|
||||
#manager = PlayerDB.objects
|
||||
230
lib/players/admin.py
Normal file
230
lib/players/admin.py
Normal file
|
|
@ -0,0 +1,230 @@
|
|||
#
|
||||
# This sets up how models are displayed
|
||||
# in the web admin interface.
|
||||
#
|
||||
|
||||
from django import forms
|
||||
from django.conf import settings
|
||||
from django.contrib import admin
|
||||
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
|
||||
from django.contrib.auth.forms import UserChangeForm, UserCreationForm
|
||||
from src.players.models import PlayerDB
|
||||
from src.typeclasses.admin import AttributeInline, TagInline
|
||||
from src.utils import create
|
||||
|
||||
|
||||
# handle the custom User editor
|
||||
class PlayerDBChangeForm(UserChangeForm):
|
||||
|
||||
class Meta:
|
||||
model = PlayerDB
|
||||
fields = '__all__'
|
||||
|
||||
username = forms.RegexField(
|
||||
label="Username",
|
||||
max_length=30,
|
||||
regex=r'^[\w. @+-]+$',
|
||||
widget=forms.TextInput(
|
||||
attrs={'size': '30'}),
|
||||
error_messages={
|
||||
'invalid': "This value may contain only letters, spaces, numbers "
|
||||
"and @/./+/-/_ characters."},
|
||||
help_text="30 characters or fewer. Letters, spaces, digits and "
|
||||
"@/./+/-/_ only.")
|
||||
|
||||
def clean_username(self):
|
||||
username = self.cleaned_data['username']
|
||||
if username.upper() == self.instance.username.upper():
|
||||
return username
|
||||
elif PlayerDB.objects.filter(username__iexact=username):
|
||||
raise forms.ValidationError('A player with that name '
|
||||
'already exists.')
|
||||
return self.cleaned_data['username']
|
||||
|
||||
|
||||
class PlayerDBCreationForm(UserCreationForm):
|
||||
|
||||
class Meta:
|
||||
model = PlayerDB
|
||||
fields = '__all__'
|
||||
|
||||
username = forms.RegexField(
|
||||
label="Username",
|
||||
max_length=30,
|
||||
regex=r'^[\w. @+-]+$',
|
||||
widget=forms.TextInput(
|
||||
attrs={'size': '30'}),
|
||||
error_messages={
|
||||
'invalid': "This value may contain only letters, spaces, numbers "
|
||||
"and @/./+/-/_ characters."},
|
||||
help_text="30 characters or fewer. Letters, spaces, digits and "
|
||||
"@/./+/-/_ only.")
|
||||
|
||||
def clean_username(self):
|
||||
username = self.cleaned_data['username']
|
||||
if PlayerDB.objects.filter(username__iexact=username):
|
||||
raise forms.ValidationError('A player with that name already '
|
||||
'exists.')
|
||||
return username
|
||||
|
||||
|
||||
class PlayerForm(forms.ModelForm):
|
||||
"""
|
||||
Defines how to display Players
|
||||
"""
|
||||
class Meta:
|
||||
model = PlayerDB
|
||||
fields = '__all__'
|
||||
|
||||
db_key = forms.RegexField(
|
||||
label="Username",
|
||||
initial="PlayerDummy",
|
||||
max_length=30,
|
||||
regex=r'^[\w. @+-]+$',
|
||||
required=False,
|
||||
widget=forms.TextInput(attrs={'size': '30'}),
|
||||
error_messages={
|
||||
'invalid': "This value may contain only letters, spaces, numbers"
|
||||
" and @/./+/-/_ characters."},
|
||||
help_text="This should be the same as the connected Player's key "
|
||||
"name. 30 characters or fewer. Letters, spaces, digits and "
|
||||
"@/./+/-/_ only.")
|
||||
|
||||
db_typeclass_path = forms.CharField(
|
||||
label="Typeclass",
|
||||
initial=settings.BASE_PLAYER_TYPECLASS,
|
||||
widget=forms.TextInput(
|
||||
attrs={'size': '78'}),
|
||||
help_text="Required. Defines what 'type' of entity this is. This "
|
||||
"variable holds a Python path to a module with a valid "
|
||||
"Evennia Typeclass. Defaults to "
|
||||
"settings.BASE_PLAYER_TYPECLASS.")
|
||||
|
||||
db_permissions = forms.CharField(
|
||||
label="Permissions",
|
||||
initial=settings.PERMISSION_PLAYER_DEFAULT,
|
||||
required=False,
|
||||
widget=forms.TextInput(
|
||||
attrs={'size': '78'}),
|
||||
help_text="In-game permissions. A comma-separated list of text "
|
||||
"strings checked by certain locks. They are often used for "
|
||||
"hierarchies, such as letting a Player have permission "
|
||||
"'Wizards', 'Builders' etc. A Player permission can be "
|
||||
"overloaded by the permissions of a controlled Character. "
|
||||
"Normal players use 'Players' by default.")
|
||||
|
||||
db_lock_storage = forms.CharField(
|
||||
label="Locks",
|
||||
widget=forms.Textarea(attrs={'cols': '100', 'rows': '2'}),
|
||||
required=False,
|
||||
help_text="In-game lock definition string. If not given, defaults "
|
||||
"will be used. This string should be on the form "
|
||||
"<i>type:lockfunction(args);type2:lockfunction2(args);...")
|
||||
db_cmdset_storage = forms.CharField(
|
||||
label="cmdset",
|
||||
initial=settings.CMDSET_PLAYER,
|
||||
widget=forms.TextInput(attrs={'size': '78'}),
|
||||
required=False,
|
||||
help_text="python path to player cmdset class (set in "
|
||||
"settings.CMDSET_PLAYER by default)")
|
||||
|
||||
|
||||
class PlayerInline(admin.StackedInline):
|
||||
"""
|
||||
Inline creation of Player
|
||||
"""
|
||||
model = PlayerDB
|
||||
template = "admin/players/stacked.html"
|
||||
form = PlayerForm
|
||||
fieldsets = (
|
||||
("In-game Permissions and Locks",
|
||||
{'fields': ('db_lock_storage',),
|
||||
#{'fields': ('db_permissions', 'db_lock_storage'),
|
||||
'description': "<i>These are permissions/locks for in-game use. "
|
||||
"They are unrelated to website access rights.</i>"}),
|
||||
("In-game Player data",
|
||||
{'fields': ('db_typeclass_path', 'db_cmdset_storage'),
|
||||
'description': "<i>These fields define in-game-specific properties "
|
||||
"for the Player object in-game.</i>"}))
|
||||
|
||||
extra = 1
|
||||
max_num = 1
|
||||
|
||||
|
||||
class PlayerTagInline(TagInline):
|
||||
model = PlayerDB.db_tags.through
|
||||
|
||||
|
||||
class PlayerAttributeInline(AttributeInline):
|
||||
model = PlayerDB.db_attributes.through
|
||||
|
||||
|
||||
class PlayerDBAdmin(BaseUserAdmin):
|
||||
"""
|
||||
This is the main creation screen for Users/players
|
||||
"""
|
||||
|
||||
list_display = ('username', 'email', 'is_staff', 'is_superuser')
|
||||
form = PlayerDBChangeForm
|
||||
add_form = PlayerDBCreationForm
|
||||
inlines = [PlayerTagInline, PlayerAttributeInline]
|
||||
fieldsets = (
|
||||
(None, {'fields': ('username', 'password', 'email')}),
|
||||
('Website profile', {
|
||||
'fields': ('first_name', 'last_name'),
|
||||
'description': "<i>These are not used "
|
||||
"in the default system.</i>"}),
|
||||
('Website dates', {
|
||||
'fields': ('last_login', 'date_joined'),
|
||||
'description': '<i>Relevant only to the website.</i>'}),
|
||||
('Website Permissions', {
|
||||
'fields': ('is_active', 'is_staff', 'is_superuser',
|
||||
'user_permissions', 'groups'),
|
||||
'description': "<i>These are permissions/permission groups for "
|
||||
"accessing the admin site. They are unrelated to "
|
||||
"in-game access rights.</i>"}),
|
||||
('Game Options', {
|
||||
'fields': ('db_typeclass_path', 'db_cmdset_storage',
|
||||
'db_lock_storage'),
|
||||
'description': '<i>These are attributes that are more relevant '
|
||||
'to gameplay.</i>'}))
|
||||
# ('Game Options', {'fields': (
|
||||
# 'db_typeclass_path', 'db_cmdset_storage',
|
||||
# 'db_permissions', 'db_lock_storage'),
|
||||
# 'description': '<i>These are attributes that are '
|
||||
# 'more relevant to gameplay.</i>'}))
|
||||
|
||||
add_fieldsets = (
|
||||
(None,
|
||||
{'fields': ('username', 'password1', 'password2', 'email'),
|
||||
'description': "<i>These account details are shared by the admin "
|
||||
"system and the game.</i>"},),)
|
||||
|
||||
def save_model(self, request, obj, form, change):
|
||||
obj.save()
|
||||
if not change:
|
||||
#calling hooks for new player
|
||||
ply = obj
|
||||
ply.basetype_setup()
|
||||
ply.at_player_creation()
|
||||
|
||||
## TODO! Remove User reference!
|
||||
#def save_formset(self, request, form, formset, change):
|
||||
# """
|
||||
# Run all hooks on the player object
|
||||
# """
|
||||
# super(PlayerDBAdmin, self).save_formset(request, form, formset, change)
|
||||
# userobj = form.instance
|
||||
# userobj.name = userobj.username
|
||||
# if not change:
|
||||
# # uname, passwd, email = str(request.POST.get(u"username")), \
|
||||
# # str(request.POST.get(u"password1")), \
|
||||
# # str(request.POST.get(u"email"))
|
||||
# typeclass = str(request.POST.get(
|
||||
# u"playerdb_set-0-db_typeclass_path"))
|
||||
# create.create_player("", "", "",
|
||||
# user=userobj,
|
||||
# typeclass=typeclass,
|
||||
# player_dbobj=userobj)
|
||||
|
||||
admin.site.register(PlayerDB, PlayerDBAdmin)
|
||||
334
lib/players/bots.py
Normal file
334
lib/players/bots.py
Normal file
|
|
@ -0,0 +1,334 @@
|
|||
"""
|
||||
Bots are a special child typeclasses of
|
||||
Player that are controlled by the server.
|
||||
|
||||
"""
|
||||
|
||||
from django.conf import settings
|
||||
from src.players.player import DefaultPlayer
|
||||
from src.scripts.scripts import Script
|
||||
from src.commands.command import Command
|
||||
from src.commands.cmdset import CmdSet
|
||||
from src.utils import search
|
||||
|
||||
_IDLE_TIMEOUT = settings.IDLE_TIMEOUT
|
||||
|
||||
_SESSIONS = None
|
||||
|
||||
|
||||
# Bot helper utilities
|
||||
|
||||
class BotStarter(Script):
|
||||
"""
|
||||
This non-repeating script has the
|
||||
sole purpose of kicking its bot
|
||||
into gear when it is initialized.
|
||||
"""
|
||||
def at_script_creation(self):
|
||||
self.key = "botstarter"
|
||||
self.desc = "bot start/keepalive"
|
||||
self.persistent = True
|
||||
self.db.started = False
|
||||
if _IDLE_TIMEOUT > 0:
|
||||
# call before idle_timeout triggers
|
||||
self.interval = int(max(60, _IDLE_TIMEOUT * 0.90))
|
||||
self.start_delay = True
|
||||
|
||||
def at_start(self):
|
||||
"Kick bot into gear"
|
||||
if not self.db.started:
|
||||
self.player.start()
|
||||
self.db.started = True
|
||||
|
||||
def at_repeat(self):
|
||||
"""
|
||||
Called self.interval seconds to keep connection. We cannot use
|
||||
the IDLE command from inside the game since the system will
|
||||
not catch it (commands executed from the server side usually
|
||||
has no sessions). So we update the idle counter manually here
|
||||
instead. This keeps the bot getting hit by IDLE_TIMEOUT.
|
||||
"""
|
||||
global _SESSIONS
|
||||
if not _SESSIONS:
|
||||
from src.server.sessionhandler import SESSIONS as _SESSIONS
|
||||
for session in _SESSIONS.sessions_from_player(self.player):
|
||||
session.update_session_counters(idle=True)
|
||||
|
||||
def at_server_reload(self):
|
||||
"""
|
||||
If server reloads we don't need to reconnect the protocol
|
||||
again, this is handled by the portal reconnect mechanism.
|
||||
"""
|
||||
self.db.started = True
|
||||
|
||||
def at_server_shutdown(self):
|
||||
"Make sure we are shutdown"
|
||||
self.db.started = False
|
||||
|
||||
|
||||
class CmdBotListen(Command):
|
||||
"""
|
||||
This is a command that absorbs input
|
||||
aimed specifically at the bot. The session
|
||||
must prepend its data with bot_data_in for
|
||||
this to trigger.
|
||||
"""
|
||||
key = "bot_data_in"
|
||||
def func(self):
|
||||
"Relay to typeclass"
|
||||
self.obj.execute_cmd(self.args.strip(), sessid=self.sessid)
|
||||
|
||||
class BotCmdSet(CmdSet):
|
||||
"Holds the BotListen command"
|
||||
key = "botcmdset"
|
||||
def at_cmdset_creation(self):
|
||||
self.add(CmdBotListen())
|
||||
|
||||
|
||||
# Bot base class
|
||||
|
||||
class Bot(DefaultPlayer):
|
||||
"""
|
||||
A Bot will start itself when the server
|
||||
starts (it will generally not do so
|
||||
on a reload - that will be handled by the
|
||||
normal Portal session resync)
|
||||
"""
|
||||
|
||||
def basetype_setup(self):
|
||||
"""
|
||||
This sets up the basic properties for the bot.
|
||||
"""
|
||||
# the text encoding to use.
|
||||
self.db.encoding = "utf-8"
|
||||
# A basic security setup
|
||||
lockstring = "examine:perm(Wizards);edit:perm(Wizards);delete:perm(Wizards);boot:perm(Wizards);msg:false()"
|
||||
self.locks.add(lockstring)
|
||||
# set the basics of being a bot
|
||||
self.cmdset.add_default(BotCmdSet)
|
||||
script_key = "%s" % self.key
|
||||
self.scripts.add(BotStarter, key=script_key)
|
||||
self.is_bot = True
|
||||
|
||||
def start(self, **kwargs):
|
||||
"""
|
||||
This starts the bot, whatever that may mean.
|
||||
"""
|
||||
pass
|
||||
|
||||
def msg(self, text=None, from_obj=None, sessid=None, **kwargs):
|
||||
"""
|
||||
Evennia -> outgoing protocol
|
||||
"""
|
||||
pass
|
||||
|
||||
def execute_cmd(self, raw_string, sessid=None):
|
||||
"""
|
||||
Incoming protocol -> Evennia
|
||||
"""
|
||||
pass
|
||||
|
||||
def at_server_shutdown(self):
|
||||
"We need to handle this case manually since the shutdown may be a reset"
|
||||
print "bots at_server_shutdown called"
|
||||
for session in self.get_all_sessions():
|
||||
session.sessionhandler.disconnect(session)
|
||||
|
||||
|
||||
# Bot implementations
|
||||
|
||||
# IRC
|
||||
|
||||
class IRCBot(Bot):
|
||||
"""
|
||||
Bot for handling IRC connections.
|
||||
"""
|
||||
def start(self, ev_channel=None, irc_botname=None, irc_channel=None, irc_network=None, irc_port=None):
|
||||
"""
|
||||
Start by telling the portal to start a new session.
|
||||
|
||||
ev_channel - key of the Evennia channel to connect to
|
||||
irc_botname - name of bot to connect to irc channel. If not set, use self.key
|
||||
irc_channel - name of channel on the form #channelname
|
||||
irc_network - url of network, like irc.freenode.net
|
||||
irc_port - port number of irc network, like 6667
|
||||
"""
|
||||
global _SESSIONS
|
||||
if not _SESSIONS:
|
||||
from src.server.sessionhandler import SESSIONS as _SESSIONS
|
||||
|
||||
# if keywords are given, store (the BotStarter script
|
||||
# will not give any keywords, so this should normally only
|
||||
# happen at initialization)
|
||||
if irc_botname:
|
||||
self.db.irc_botname = irc_botname
|
||||
elif not self.db.irc_botname:
|
||||
self.db.irc_botname = self.key
|
||||
if ev_channel:
|
||||
# connect to Evennia channel
|
||||
channel = search.channel_search(ev_channel)
|
||||
if not channel:
|
||||
raise RuntimeError("Evennia Channel '%s' not found." % ev_channel)
|
||||
channel = channel[0]
|
||||
channel.connect(self)
|
||||
self.db.ev_channel = channel
|
||||
if irc_channel:
|
||||
self.db.irc_channel = irc_channel
|
||||
if irc_network:
|
||||
self.db.irc_network = irc_network
|
||||
if irc_port:
|
||||
self.db.irc_port = irc_port
|
||||
|
||||
# instruct the server and portal to create a new session with
|
||||
# the stored configuration
|
||||
configdict = {"uid":self.dbid,
|
||||
"botname": self.db.irc_botname,
|
||||
"channel": self.db.irc_channel ,
|
||||
"network": self.db.irc_network,
|
||||
"port": self.db.irc_port}
|
||||
_SESSIONS.start_bot_session("src.server.portal.irc.IRCBotFactory", configdict)
|
||||
|
||||
def msg(self, text=None, **kwargs):
|
||||
"""
|
||||
Takes text from connected channel (only)
|
||||
"""
|
||||
if not self.ndb.ev_channel and self.db.ev_channel:
|
||||
# cache channel lookup
|
||||
self.ndb.ev_channel = self.db.ev_channel
|
||||
if "from_channel" in kwargs and text and self.ndb.ev_channel.dbid == kwargs["from_channel"]:
|
||||
if "from_obj" not in kwargs or kwargs["from_obj"] != [self.id]:
|
||||
text = "bot_data_out %s" % text
|
||||
self.msg(text=text)
|
||||
|
||||
def execute_cmd(self, text=None, sessid=None):
|
||||
"""
|
||||
Take incoming data and send it to connected channel. This is triggered
|
||||
by the CmdListen command in the BotCmdSet.
|
||||
"""
|
||||
if not self.ndb.ev_channel and self.db.ev_channel:
|
||||
# cache channel lookup
|
||||
self.ndb.ev_channel = self.db.ev_channel
|
||||
if self.ndb.ev_channel:
|
||||
self.ndb.ev_channel.msg(text, senders=self.id)
|
||||
|
||||
|
||||
# RSS
|
||||
|
||||
class RSSBot(Bot):
|
||||
"""
|
||||
An RSS relayer. The RSS protocol itself runs a ticker to update its feed at regular
|
||||
intervals.
|
||||
"""
|
||||
def start(self, ev_channel=None, rss_url=None, rss_rate=None):
|
||||
"""
|
||||
Start by telling the portal to start a new RSS session
|
||||
|
||||
ev_channel - key of the Evennia channel to connect to
|
||||
rss_url - full URL to the RSS feed to subscribe to
|
||||
rss_update_rate - how often for the feedreader to update
|
||||
"""
|
||||
global _SESSIONS
|
||||
if not _SESSIONS:
|
||||
from src.server.sessionhandler import SESSIONS as _SESSIONS
|
||||
|
||||
if ev_channel:
|
||||
# connect to Evennia channel
|
||||
channel = search.channel_search(ev_channel)
|
||||
if not channel:
|
||||
raise RuntimeError("Evennia Channel '%s' not found." % ev_channel)
|
||||
channel = channel[0]
|
||||
self.db.ev_channel = channel
|
||||
if rss_url:
|
||||
self.db.rss_url = rss_url
|
||||
if rss_rate:
|
||||
self.db.rss_rate = rss_rate
|
||||
# instruct the server and portal to create a new session with
|
||||
# the stored configuration
|
||||
configdict = {"uid": self.dbid,
|
||||
"url": self.db.rss_url,
|
||||
"rate": self.db.rss_rate}
|
||||
_SESSIONS.start_bot_session("src.server.portal.rss.RSSBotFactory", configdict)
|
||||
|
||||
def execute_cmd(self, text=None, sessid=None):
|
||||
"""
|
||||
Echo RSS input to connected channel
|
||||
"""
|
||||
print "execute_cmd rss:", text
|
||||
if not self.ndb.ev_channel and self.db.ev_channel:
|
||||
# cache channel lookup
|
||||
self.ndb.ev_channel = self.db.ev_channel
|
||||
if self.ndb.ev_channel:
|
||||
self.ndb.ev_channel.msg(text, senders=self.id)
|
||||
|
||||
class IMC2Bot(Bot):
|
||||
"""
|
||||
IMC2 Bot
|
||||
"""
|
||||
def start(self, ev_channel=None, imc2_network=None, imc2_mudname=None,
|
||||
imc2_port=None, imc2_client_pwd=None, imc2_server_pwd=None):
|
||||
"""
|
||||
Start by telling the portal to start a new session
|
||||
ev_channel - key of the Evennia channel to connect to
|
||||
imc2_network - IMC2 network name
|
||||
imc2_mudname - registered mudname (if not given, use settings.SERVERNAME)
|
||||
imc2_port - port number of IMC2 network
|
||||
imc2_client_pwd - client password registered with IMC2 network
|
||||
imc2_server_pwd - server password registered with IMC2 network
|
||||
"""
|
||||
global _SESSIONS
|
||||
if not _SESSIONS:
|
||||
from src.server.sessionhandler import SESSIONS as _SESSIONS
|
||||
if ev_channel:
|
||||
# connect to Evennia channel
|
||||
channel = search.channel_search(ev_channel)
|
||||
if not channel:
|
||||
raise RuntimeError("Evennia Channel '%s' not found." % ev_channel)
|
||||
channel = channel[0]
|
||||
channel.connect(self)
|
||||
self.db.ev_channel = channel
|
||||
if imc2_network:
|
||||
self.db.imc2_network = imc2_network
|
||||
if imc2_port:
|
||||
self.db.imc2_port = imc2_port
|
||||
if imc2_mudname:
|
||||
self.db.imc2_mudname = imc2_mudname
|
||||
elif not self.db.imc2_mudname:
|
||||
self.db.imc2_mudname = settings.SERVERNAME
|
||||
# storing imc2 passwords in attributes - a possible
|
||||
# security issue?
|
||||
if imc2_server_pwd:
|
||||
self.db.imc2_server_pwd = imc2_server_pwd
|
||||
if imc2_client_pwd:
|
||||
self.db.imc2_client_pwd = imc2_client_pwd
|
||||
|
||||
configdict = {"uid": self.dbid,
|
||||
"mudname": self.db.imc2_mudname,
|
||||
"network": self.db.imc2_network,
|
||||
"port": self.db.imc2_port,
|
||||
"client_pwd": self.db.client_pwd,
|
||||
"server_pwd": self.db.server_pwd}
|
||||
|
||||
_SESSIONS.start_bot_session("src.server.portal.imc2.IMC2BotFactory", configdict)
|
||||
|
||||
def msg(self, text=None, **kwargs):
|
||||
"""
|
||||
Takes text from connected channel (only)
|
||||
"""
|
||||
if not self.ndb.ev_channel and self.db.ev_channel:
|
||||
# cache channel lookup
|
||||
self.ndb.ev_channel = self.db.ev_channel
|
||||
if "from_channel" in kwargs and text and self.ndb.ev_channel.dbid == kwargs["from_channel"]:
|
||||
if "from_obj" not in kwargs or kwargs["from_obj"] != [self.id]:
|
||||
text = "bot_data_out %s" % text
|
||||
self.msg(text=text)
|
||||
|
||||
def execute_cmd(self, text=None, sessid=None):
|
||||
"""
|
||||
Relay incoming data to connected channel.
|
||||
"""
|
||||
if not self.ndb.ev_channel and self.db.ev_channel:
|
||||
# cache channel lookup
|
||||
self.ndb.ev_channel = self.db.ev_channel
|
||||
if self.ndb.ev_channel:
|
||||
self.ndb.ev_channel.msg(text, senders=self.id)
|
||||
|
||||
155
lib/players/manager.py
Normal file
155
lib/players/manager.py
Normal file
|
|
@ -0,0 +1,155 @@
|
|||
"""
|
||||
The managers for the custom Player object and permissions.
|
||||
"""
|
||||
|
||||
import datetime
|
||||
from django.contrib.auth.models import UserManager
|
||||
#from functools import update_wrapper
|
||||
from src.typeclasses.managers import (returns_typeclass_list, returns_typeclass,
|
||||
TypedObjectManager, TypeclassManager)
|
||||
#from src.utils import logger
|
||||
__all__ = ("PlayerManager",)
|
||||
|
||||
|
||||
#
|
||||
# Player Manager
|
||||
#
|
||||
|
||||
class PlayerDBManager(TypedObjectManager, UserManager):
|
||||
"""
|
||||
This PlayerManager implements methods for searching
|
||||
and manipulating Players directly from the database.
|
||||
|
||||
Evennia-specific search methods (will return Characters if
|
||||
possible or a Typeclass/list of Typeclassed objects, whereas
|
||||
Django-general methods will return Querysets or database objects):
|
||||
|
||||
dbref (converter)
|
||||
dbref_search
|
||||
get_dbref_range
|
||||
object_totals
|
||||
typeclass_search
|
||||
num_total_players
|
||||
get_connected_players
|
||||
get_recently_created_players
|
||||
get_recently_connected_players
|
||||
get_player_from_email
|
||||
get_player_from_uid
|
||||
get_player_from_name
|
||||
player_search (equivalent to ev.search_player)
|
||||
#swap_character
|
||||
|
||||
"""
|
||||
def num_total_players(self):
|
||||
"""
|
||||
Returns the total number of registered players.
|
||||
"""
|
||||
return self.count()
|
||||
|
||||
@returns_typeclass_list
|
||||
def get_connected_players(self):
|
||||
"""
|
||||
Returns a list of player objects with currently connected users/players.
|
||||
"""
|
||||
return self.filter(db_is_connected=True)
|
||||
|
||||
@returns_typeclass_list
|
||||
def get_recently_created_players(self, days=7):
|
||||
"""
|
||||
Returns a QuerySet containing the player User accounts that have been
|
||||
connected within the last <days> days.
|
||||
"""
|
||||
end_date = datetime.datetime.now()
|
||||
tdelta = datetime.timedelta(days)
|
||||
start_date = end_date - tdelta
|
||||
return self.filter(date_joined__range=(start_date, end_date))
|
||||
|
||||
@returns_typeclass_list
|
||||
def get_recently_connected_players(self, days=7):
|
||||
"""
|
||||
Returns a QuerySet containing the player accounts that have been
|
||||
connected within the last <days> days.
|
||||
|
||||
days - number of days backwards to check
|
||||
"""
|
||||
end_date = datetime.datetime.now()
|
||||
tdelta = datetime.timedelta(days)
|
||||
start_date = end_date - tdelta
|
||||
return self.filter(last_login__range=(
|
||||
start_date, end_date)).order_by('-last_login')
|
||||
|
||||
@returns_typeclass
|
||||
def get_player_from_email(self, uemail):
|
||||
"""
|
||||
Returns a player object when given an email address.
|
||||
"""
|
||||
return self.filter(email__iexact=uemail)
|
||||
|
||||
@returns_typeclass
|
||||
def get_player_from_uid(self, uid):
|
||||
"""
|
||||
Returns a player object based on User id.
|
||||
"""
|
||||
try:
|
||||
return self.get(id=uid)
|
||||
except self.model.DoesNotExist:
|
||||
return None
|
||||
|
||||
@returns_typeclass
|
||||
def get_player_from_name(self, uname):
|
||||
"Get player object based on name"
|
||||
try:
|
||||
return self.get(username__iexact=uname)
|
||||
except self.model.DoesNotExist:
|
||||
return None
|
||||
|
||||
@returns_typeclass_list
|
||||
def player_search(self, ostring, exact=True):
|
||||
"""
|
||||
Searches for a particular player by name or
|
||||
database id.
|
||||
|
||||
ostring - a string or database id.
|
||||
exact - allow for a partial match
|
||||
"""
|
||||
dbref = self.dbref(ostring)
|
||||
if dbref or dbref == 0:
|
||||
# bref search is always exact
|
||||
matches = self.filter(id=dbref)
|
||||
if matches:
|
||||
return matches
|
||||
if exact:
|
||||
return self.filter(username__iexact=ostring)
|
||||
else:
|
||||
return self.filter(username__icontains=ostring)
|
||||
|
||||
# def swap_character(self, player, new_character, delete_old_character=False):
|
||||
# """
|
||||
# This disconnects a player from the current character (if any) and
|
||||
# connects to a new character object.
|
||||
#
|
||||
# """
|
||||
#
|
||||
# if new_character.player:
|
||||
# # the new character is already linked to a player!
|
||||
# return False
|
||||
#
|
||||
# # do the swap
|
||||
# old_character = player.character
|
||||
# if old_character:
|
||||
# old_character.player = None
|
||||
# try:
|
||||
# player.character = new_character
|
||||
# new_character.player = player
|
||||
# except Exception:
|
||||
# # recover old setup
|
||||
# if old_character:
|
||||
# old_character.player = player
|
||||
# player.character = old_character
|
||||
# return False
|
||||
# if old_character and delete_old_character:
|
||||
# old_character.delete()
|
||||
# return True
|
||||
|
||||
class PlayerManager(PlayerDBManager, TypeclassManager):
|
||||
pass
|
||||
49
lib/players/migrations/0001_initial.py
Normal file
49
lib/players/migrations/0001_initial.py
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import models, migrations
|
||||
import django.utils.timezone
|
||||
import django.core.validators
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('auth', '0001_initial'),
|
||||
('typeclasses', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='PlayerDB',
|
||||
fields=[
|
||||
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
|
||||
('password', models.CharField(max_length=128, verbose_name='password')),
|
||||
('last_login', models.DateTimeField(default=django.utils.timezone.now, verbose_name='last login')),
|
||||
('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')),
|
||||
('username', models.CharField(help_text='Required. 30 characters or fewer. Letters, digits and @/./+/-/_ only.', unique=True, max_length=30, verbose_name='username', validators=[django.core.validators.RegexValidator('^[\\w.@+-]+$', 'Enter a valid username.', 'invalid')])),
|
||||
('first_name', models.CharField(max_length=30, verbose_name='first name', blank=True)),
|
||||
('last_name', models.CharField(max_length=30, verbose_name='last name', blank=True)),
|
||||
('email', models.EmailField(max_length=75, verbose_name='email address', blank=True)),
|
||||
('is_staff', models.BooleanField(default=False, help_text='Designates whether the user can log into this admin site.', verbose_name='staff status')),
|
||||
('is_active', models.BooleanField(default=True, help_text='Designates whether this user should be treated as active. Unselect this instead of deleting accounts.', verbose_name='active')),
|
||||
('date_joined', models.DateTimeField(default=django.utils.timezone.now, verbose_name='date joined')),
|
||||
('db_key', models.CharField(max_length=255, verbose_name=b'key', db_index=True)),
|
||||
('db_typeclass_path', models.CharField(help_text=b"this defines what 'type' of entity this is. This variable holds a Python path to a module with a valid Evennia Typeclass.", max_length=255, null=True, verbose_name=b'typeclass')),
|
||||
('db_date_created', models.DateTimeField(auto_now_add=True, verbose_name=b'creation date')),
|
||||
('db_lock_storage', models.TextField(help_text=b"locks limit access to an entity. A lock is defined as a 'lock string' on the form 'type:lockfunctions', defining what functionality is locked and how to determine access. Not defining a lock means no access is granted.", verbose_name=b'locks', blank=True)),
|
||||
('db_is_connected', models.BooleanField(default=False, help_text=b'If player is connected to game or not', verbose_name=b'is_connected')),
|
||||
('db_cmdset_storage', models.CharField(help_text=b'optional python path to a cmdset class. If creating a Character, this will default to settings.CMDSET_CHARACTER.', max_length=255, null=True, verbose_name=b'cmdset')),
|
||||
('db_is_bot', models.BooleanField(default=False, help_text=b'Used to identify irc/imc2/rss bots', verbose_name=b'is_bot')),
|
||||
('db_attributes', models.ManyToManyField(help_text=b'attributes on this object. An attribute can hold any pickle-able python object (see docs for special cases).', to='typeclasses.Attribute', null=True)),
|
||||
('db_tags', models.ManyToManyField(help_text=b'tags on this object. Tags are simple string markers to identify, group and alias objects.', to='typeclasses.Tag', null=True)),
|
||||
('groups', models.ManyToManyField(related_query_name='user', related_name='user_set', to='auth.Group', blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of his/her group.', verbose_name='groups')),
|
||||
('user_permissions', models.ManyToManyField(related_query_name='user', related_name='user_set', to='auth.Permission', blank=True, help_text='Specific permissions for this user.', verbose_name='user permissions')),
|
||||
],
|
||||
options={
|
||||
'verbose_name': 'Player',
|
||||
'verbose_name_plural': 'Players',
|
||||
},
|
||||
bases=(models.Model,),
|
||||
),
|
||||
]
|
||||
1
lib/players/migrations/__init__.py
Normal file
1
lib/players/migrations/__init__.py
Normal file
|
|
@ -0,0 +1 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
173
lib/players/models.py
Normal file
173
lib/players/models.py
Normal file
|
|
@ -0,0 +1,173 @@
|
|||
"""
|
||||
Player
|
||||
|
||||
The player class is an extension of the default Django user class,
|
||||
and is customized for the needs of Evennia.
|
||||
|
||||
We use the Player to store a more mud-friendly style of permission
|
||||
system as well as to allow the admin more flexibility by storing
|
||||
attributes on the Player. Within the game we should normally use the
|
||||
Player manager's methods to create users so that permissions are set
|
||||
correctly.
|
||||
|
||||
To make the Player model more flexible for your own game, it can also
|
||||
persistently store attributes of its own. This is ideal for extra
|
||||
account info and OOC account configuration variables etc.
|
||||
|
||||
"""
|
||||
|
||||
from django.conf import settings
|
||||
from django.db import models
|
||||
from django.contrib.auth.models import AbstractUser
|
||||
from django.utils.encoding import smart_str
|
||||
|
||||
from src.players.manager import PlayerDBManager
|
||||
from src.typeclasses.models import TypedObject
|
||||
from src.utils.utils import make_iter
|
||||
|
||||
__all__ = ("PlayerDB",)
|
||||
|
||||
#_ME = _("me")
|
||||
#_SELF = _("self")
|
||||
|
||||
_MULTISESSION_MODE = settings.MULTISESSION_MODE
|
||||
|
||||
_GA = object.__getattribute__
|
||||
_SA = object.__setattr__
|
||||
_DA = object.__delattr__
|
||||
|
||||
_TYPECLASS = None
|
||||
|
||||
|
||||
#------------------------------------------------------------
|
||||
#
|
||||
# PlayerDB
|
||||
#
|
||||
#------------------------------------------------------------
|
||||
|
||||
class PlayerDB(TypedObject, AbstractUser):
|
||||
"""
|
||||
This is a special model using Django's 'profile' functionality
|
||||
and extends the default Django User model. It is defined as such
|
||||
by use of the variable AUTH_PROFILE_MODULE in the settings.
|
||||
One accesses the fields/methods. We try use this model as much
|
||||
as possible rather than User, since we can customize this to
|
||||
our liking.
|
||||
|
||||
The TypedObject supplies the following (inherited) properties:
|
||||
key - main name
|
||||
typeclass_path - the path to the decorating typeclass
|
||||
typeclass - auto-linked typeclass
|
||||
date_created - time stamp of object creation
|
||||
permissions - perm strings
|
||||
dbref - #id of object
|
||||
db - persistent attribute storage
|
||||
ndb - non-persistent attribute storage
|
||||
|
||||
The PlayerDB adds the following properties:
|
||||
user - Connected User object. django field, needs to be save():d.
|
||||
name - alias for user.username
|
||||
sessions - sessions connected to this player
|
||||
is_superuser - bool if this player is a superuser
|
||||
is_bot - bool if this player is a bot and not a real player
|
||||
|
||||
"""
|
||||
|
||||
#
|
||||
# PlayerDB Database model setup
|
||||
#
|
||||
# inherited fields (from TypedObject):
|
||||
# db_key, db_typeclass_path, db_date_created, db_permissions
|
||||
|
||||
# store a connected flag here too, not just in sessionhandler.
|
||||
# This makes it easier to track from various out-of-process locations
|
||||
db_is_connected = models.BooleanField(default=False,
|
||||
verbose_name="is_connected",
|
||||
help_text="If player is connected to game or not")
|
||||
# database storage of persistant cmdsets.
|
||||
db_cmdset_storage = models.CharField('cmdset', max_length=255, null=True,
|
||||
help_text="optional python path to a cmdset class. If creating a Character, this will default to settings.CMDSET_CHARACTER.")
|
||||
# marks if this is a "virtual" bot player object
|
||||
db_is_bot = models.BooleanField(default=False, verbose_name="is_bot", help_text="Used to identify irc/imc2/rss bots")
|
||||
|
||||
# Database manager
|
||||
objects = PlayerDBManager()
|
||||
|
||||
class Meta:
|
||||
app_label = 'players'
|
||||
verbose_name = 'Player'
|
||||
|
||||
# alias to the objs property
|
||||
def __characters_get(self):
|
||||
return self.objs
|
||||
|
||||
def __characters_set(self, value):
|
||||
self.objs = value
|
||||
|
||||
def __characters_del(self):
|
||||
raise Exception("Cannot delete name")
|
||||
characters = property(__characters_get, __characters_set, __characters_del)
|
||||
|
||||
# cmdset_storage property
|
||||
# This seems very sensitive to caching, so leaving it be for now /Griatch
|
||||
#@property
|
||||
def cmdset_storage_get(self):
|
||||
"""
|
||||
Getter. Allows for value = self.name. Returns a list of cmdset_storage.
|
||||
"""
|
||||
storage = self.db_cmdset_storage
|
||||
# we need to check so storage is not None
|
||||
return [path.strip() for path in storage.split(',')] if storage else []
|
||||
|
||||
#@cmdset_storage.setter
|
||||
def cmdset_storage_set(self, value):
|
||||
"""
|
||||
Setter. Allows for self.name = value. Stores as a comma-separated
|
||||
string.
|
||||
"""
|
||||
_SA(self, "db_cmdset_storage", ",".join(str(val).strip() for val in make_iter(value)))
|
||||
_GA(self, "save")()
|
||||
|
||||
#@cmdset_storage.deleter
|
||||
def cmdset_storage_del(self):
|
||||
"Deleter. Allows for del self.name"
|
||||
_SA(self, "db_cmdset_storage", None)
|
||||
_GA(self, "save")()
|
||||
cmdset_storage = property(cmdset_storage_get, cmdset_storage_set, cmdset_storage_del)
|
||||
|
||||
#
|
||||
# property/field access
|
||||
#
|
||||
|
||||
def __str__(self):
|
||||
return smart_str("%s(player %s)" % (self.name, self.dbid))
|
||||
|
||||
def __unicode__(self):
|
||||
return u"%s(player#%s)" % (self.name, self.dbid)
|
||||
|
||||
#@property
|
||||
def __username_get(self):
|
||||
return self.username
|
||||
|
||||
def __username_set(self, value):
|
||||
self.username = value
|
||||
self.save(update_fields=["username"])
|
||||
|
||||
def __username_del(self):
|
||||
del self.username
|
||||
|
||||
# aliases
|
||||
name = property(__username_get, __username_set, __username_del)
|
||||
key = property(__username_get, __username_set, __username_del)
|
||||
|
||||
#@property
|
||||
def __uid_get(self):
|
||||
"Getter. Retrieves the user id"
|
||||
return self.id
|
||||
|
||||
def __uid_set(self, value):
|
||||
raise Exception("User id cannot be set!")
|
||||
|
||||
def __uid_del(self):
|
||||
raise Exception("User id cannot be deleted!")
|
||||
uid = property(__uid_get, __uid_set, __uid_del)
|
||||
704
lib/players/player.py
Normal file
704
lib/players/player.py
Normal file
|
|
@ -0,0 +1,704 @@
|
|||
"""
|
||||
Typeclass for Player objects
|
||||
|
||||
Note that this object is primarily intended to
|
||||
store OOC information, not game info! This
|
||||
object represents the actual user (not their
|
||||
character) and has NO actual precence in the
|
||||
game world (this is handled by the associated
|
||||
character object, so you should customize that
|
||||
instead for most things).
|
||||
|
||||
"""
|
||||
|
||||
import datetime
|
||||
from django.conf import settings
|
||||
from src.typeclasses.models import TypeclassBase
|
||||
from src.players.manager import PlayerManager
|
||||
from src.players.models import PlayerDB
|
||||
from src.comms.models import ChannelDB
|
||||
from src.commands import cmdhandler
|
||||
from src.scripts.models import ScriptDB
|
||||
from src.utils import logger
|
||||
from src.utils.utils import (lazy_property, to_str,
|
||||
make_iter, to_unicode,
|
||||
variable_from_module)
|
||||
from src.typeclasses.attributes import NickHandler
|
||||
from src.scripts.scripthandler import ScriptHandler
|
||||
from src.commands.cmdsethandler import CmdSetHandler
|
||||
|
||||
from django.utils.translation import ugettext as _
|
||||
|
||||
__all__ = ("DefaultPlayer",)
|
||||
|
||||
_SESSIONS = None
|
||||
|
||||
_AT_SEARCH_RESULT = variable_from_module(*settings.SEARCH_AT_RESULT.rsplit('.', 1))
|
||||
_MULTISESSION_MODE = settings.MULTISESSION_MODE
|
||||
_CMDSET_PLAYER = settings.CMDSET_PLAYER
|
||||
_CONNECT_CHANNEL = None
|
||||
|
||||
class DefaultPlayer(PlayerDB):
|
||||
"""
|
||||
This is the base Typeclass for all Players. Players represent
|
||||
the person playing the game and tracks account info, password
|
||||
etc. They are OOC entities without presence in-game. A Player
|
||||
can connect to a Character Object in order to "enter" the
|
||||
game.
|
||||
|
||||
Player Typeclass API:
|
||||
|
||||
* Available properties (only available on initiated typeclass objects)
|
||||
|
||||
key (string) - name of player
|
||||
name (string)- wrapper for user.username
|
||||
aliases (list of strings) - aliases to the object. Will be saved to
|
||||
database as AliasDB entries but returned as strings.
|
||||
dbref (int, read-only) - unique #id-number. Also "id" can be used.
|
||||
date_created (string) - time stamp of object creation
|
||||
permissions (list of strings) - list of permission strings
|
||||
|
||||
user (User, read-only) - django User authorization object
|
||||
obj (Object) - game object controlled by player. 'character' can also
|
||||
be used.
|
||||
sessions (list of Sessions) - sessions connected to this player
|
||||
is_superuser (bool, read-only) - if the connected user is a superuser
|
||||
|
||||
* Handlers
|
||||
|
||||
locks - lock-handler: use locks.add() to add new lock strings
|
||||
db - attribute-handler: store/retrieve database attributes on this
|
||||
self.db.myattr=val, val=self.db.myattr
|
||||
ndb - non-persistent attribute handler: same as db but does not
|
||||
create a database entry when storing data
|
||||
scripts - script-handler. Add new scripts to object with scripts.add()
|
||||
cmdset - cmdset-handler. Use cmdset.add() to add new cmdsets to object
|
||||
nicks - nick-handler. New nicks with nicks.add().
|
||||
|
||||
* Helper methods
|
||||
|
||||
msg(outgoing_string, from_obj=None, **kwargs)
|
||||
#swap_character(new_character, delete_old_character=False)
|
||||
execute_cmd(raw_string)
|
||||
search(ostring, global_search=False, attribute_name=None,
|
||||
use_nicks=False, location=None,
|
||||
ignore_errors=False, player=False)
|
||||
is_typeclass(typeclass, exact=False)
|
||||
swap_typeclass(new_typeclass, clean_attributes=False, no_default=True)
|
||||
access(accessing_obj, access_type='read', default=False)
|
||||
check_permstring(permstring)
|
||||
|
||||
* Hook methods
|
||||
|
||||
basetype_setup()
|
||||
at_player_creation()
|
||||
|
||||
- note that the following hooks are also found on Objects and are
|
||||
usually handled on the character level:
|
||||
|
||||
at_init()
|
||||
at_access()
|
||||
at_cmdset_get(**kwargs)
|
||||
at_first_login()
|
||||
at_post_login(sessid=None)
|
||||
at_disconnect()
|
||||
at_message_receive()
|
||||
at_message_send()
|
||||
at_server_reload()
|
||||
at_server_shutdown()
|
||||
|
||||
"""
|
||||
|
||||
__metaclass__ = TypeclassBase
|
||||
objects = PlayerManager()
|
||||
|
||||
# properties
|
||||
@lazy_property
|
||||
def cmdset(self):
|
||||
return CmdSetHandler(self, True)
|
||||
|
||||
@lazy_property
|
||||
def scripts(self):
|
||||
return ScriptHandler(self)
|
||||
|
||||
@lazy_property
|
||||
def nicks(self):
|
||||
return NickHandler(self)
|
||||
|
||||
|
||||
# session-related methods
|
||||
|
||||
def get_session(self, sessid):
|
||||
"""
|
||||
Return session with given sessid connected to this player.
|
||||
note that the sessionhandler also accepts sessid as an iterable.
|
||||
"""
|
||||
global _SESSIONS
|
||||
if not _SESSIONS:
|
||||
from src.server.sessionhandler import SESSIONS as _SESSIONS
|
||||
return _SESSIONS.session_from_player(self, sessid)
|
||||
|
||||
def get_all_sessions(self):
|
||||
"Return all sessions connected to this player"
|
||||
global _SESSIONS
|
||||
if not _SESSIONS:
|
||||
from src.server.sessionhandler import SESSIONS as _SESSIONS
|
||||
return _SESSIONS.sessions_from_player(self)
|
||||
sessions = property(get_all_sessions) # alias shortcut
|
||||
|
||||
def disconnect_session_from_player(self, sessid):
|
||||
"""
|
||||
Access method for disconnecting a given session from the player
|
||||
(connection happens automatically in the sessionhandler)
|
||||
"""
|
||||
# this should only be one value, loop just to make sure to
|
||||
# clean everything
|
||||
sessions = (session for session in self.get_all_sessions()
|
||||
if session.sessid == sessid)
|
||||
for session in sessions:
|
||||
# this will also trigger unpuppeting
|
||||
session.sessionhandler.disconnect(session)
|
||||
|
||||
# puppeting operations
|
||||
|
||||
def puppet_object(self, sessid, obj, normal_mode=True):
|
||||
"""
|
||||
Use the given session to control (puppet) the given object (usually
|
||||
a Character type). Note that we make no puppet checks here, that must
|
||||
have been done before calling this method.
|
||||
|
||||
sessid - session id of session to connect
|
||||
obj - the object to connect to
|
||||
normal_mode - trigger hooks and extra checks - this is turned off when
|
||||
the server reloads, to quickly re-connect puppets.
|
||||
|
||||
returns True if successful, False otherwise
|
||||
"""
|
||||
session = self.get_session(sessid)
|
||||
if not session:
|
||||
return False
|
||||
if normal_mode and session.puppet:
|
||||
# cleanly unpuppet eventual previous object puppeted by this session
|
||||
self.unpuppet_object(sessid)
|
||||
if obj.player and obj.player.is_connected and obj.player != self:
|
||||
# we don't allow to puppet an object already controlled by an active
|
||||
# player. To kick a player, call unpuppet_object on them explicitly.
|
||||
return
|
||||
# if we get to this point the character is ready to puppet or it
|
||||
# was left with a lingering player/sessid reference from an unclean
|
||||
# server kill or similar
|
||||
|
||||
if normal_mode:
|
||||
obj.at_pre_puppet(self, sessid=sessid)
|
||||
# do the connection
|
||||
obj.sessid.add(sessid)
|
||||
obj.player = self
|
||||
session.puid = obj.id
|
||||
session.puppet = obj
|
||||
# validate/start persistent scripts on object
|
||||
ScriptDB.objects.validate(obj=obj)
|
||||
if normal_mode:
|
||||
obj.at_post_puppet()
|
||||
# re-cache locks to make sure superuser bypass is updated
|
||||
obj.locks.cache_lock_bypass(obj)
|
||||
return True
|
||||
|
||||
def unpuppet_object(self, sessid):
|
||||
"""
|
||||
Disengage control over an object
|
||||
|
||||
sessid - the session id to disengage
|
||||
|
||||
returns True if successful
|
||||
"""
|
||||
session = self.get_session(sessid)
|
||||
if not session:
|
||||
return False
|
||||
obj = hasattr(session, "puppet") and session.puppet or None
|
||||
if not obj:
|
||||
return False
|
||||
# do the disconnect, but only if we are the last session to puppet
|
||||
obj.at_pre_unpuppet()
|
||||
obj.sessid.remove(sessid)
|
||||
if not obj.sessid.count():
|
||||
del obj.player
|
||||
obj.at_post_unpuppet(self, sessid=sessid)
|
||||
session.puppet = None
|
||||
session.puid = None
|
||||
return True
|
||||
|
||||
def unpuppet_all(self):
|
||||
"""
|
||||
Disconnect all puppets. This is called by server
|
||||
before a reset/shutdown.
|
||||
"""
|
||||
for session in self.get_all_sessions():
|
||||
self.unpuppet_object(session.sessid)
|
||||
|
||||
def get_puppet(self, sessid, return_dbobj=False):
|
||||
"""
|
||||
Get an object puppeted by this session through this player. This is
|
||||
the main method for retrieving the puppeted object from the
|
||||
player's end.
|
||||
|
||||
sessid - return character connected to this sessid,
|
||||
|
||||
"""
|
||||
session = self.get_session(sessid)
|
||||
if not session:
|
||||
return None
|
||||
if return_dbobj:
|
||||
return session.puppet
|
||||
return session.puppet and session.puppet or None
|
||||
|
||||
def get_all_puppets(self, return_dbobj=False):
|
||||
"""
|
||||
Get all currently puppeted objects as a list
|
||||
"""
|
||||
puppets = [session.puppet for session in self.get_all_sessions()
|
||||
if session.puppet]
|
||||
if return_dbobj:
|
||||
return puppets
|
||||
return [puppet for puppet in puppets]
|
||||
|
||||
def __get_single_puppet(self):
|
||||
"""
|
||||
This is a legacy convenience link for users of
|
||||
MULTISESSION_MODE 0 or 1. It will return
|
||||
only the first puppet. For mode 2, this returns
|
||||
a list of all characters.
|
||||
"""
|
||||
puppets = self.get_all_puppets()
|
||||
if _MULTISESSION_MODE in (0, 1):
|
||||
return puppets and puppets[0] or None
|
||||
return puppets
|
||||
character = property(__get_single_puppet)
|
||||
puppet = property(__get_single_puppet)
|
||||
|
||||
# utility methods
|
||||
|
||||
def delete(self, *args, **kwargs):
|
||||
"""
|
||||
Deletes the player permanently.
|
||||
"""
|
||||
for session in self.get_all_sessions():
|
||||
# unpuppeting all objects and disconnecting the user, if any
|
||||
# sessions remain (should usually be handled from the
|
||||
# deleting command)
|
||||
self.unpuppet_object(session.sessid)
|
||||
session.sessionhandler.disconnect(session, reason=_("Player being deleted."))
|
||||
self.scripts.stop()
|
||||
self.attributes.clear()
|
||||
self.nicks.clear()
|
||||
self.aliases.clear()
|
||||
super(PlayerDB, self).delete(*args, **kwargs)
|
||||
## methods inherited from database model
|
||||
|
||||
def msg(self, text=None, from_obj=None, sessid=None, **kwargs):
|
||||
"""
|
||||
Evennia -> User
|
||||
This is the main route for sending data back to the user from the
|
||||
server.
|
||||
|
||||
outgoing_string (string) - text data to send
|
||||
from_obj (Object/Player) - source object of message to send. Its
|
||||
at_msg_send() hook will be called.
|
||||
sessid - the session id of the session to send to. If not given, return
|
||||
to all sessions connected to this player. This is usually only
|
||||
relevant when using msg() directly from a player-command (from
|
||||
a command on a Character, the character automatically stores
|
||||
and handles the sessid). Can also be a list of sessids.
|
||||
kwargs (dict) - All other keywords are parsed as extra data.
|
||||
"""
|
||||
if "data" in kwargs:
|
||||
# deprecation warning
|
||||
logger.log_depmsg("PlayerDB:msg() 'data'-dict keyword is deprecated. Use **kwargs instead.")
|
||||
data = kwargs.pop("data")
|
||||
if isinstance(data, dict):
|
||||
kwargs.update(data)
|
||||
|
||||
text = to_str(text, force_string=True) if text else ""
|
||||
if from_obj:
|
||||
# call hook
|
||||
try:
|
||||
from_obj.at_msg_send(text=text, to_obj=self, **kwargs)
|
||||
except Exception:
|
||||
pass
|
||||
sessions = _MULTISESSION_MODE > 1 and sessid and self.get_session(sessid) or None
|
||||
if sessions:
|
||||
for session in make_iter(sessions):
|
||||
obj = session.puppet
|
||||
if obj and not obj.at_msg_receive(text=text, **kwargs):
|
||||
# if hook returns false, cancel send
|
||||
continue
|
||||
session.msg(text=text, **kwargs)
|
||||
else:
|
||||
# if no session was specified, send to them all
|
||||
for sess in self.get_all_sessions():
|
||||
sess.msg(text=text, **kwargs)
|
||||
|
||||
def execute_cmd(self, raw_string, sessid=None, **kwargs):
|
||||
"""
|
||||
Do something as this player. This method is never called normally,
|
||||
but only when the player object itself is supposed to execute the
|
||||
command. It takes player nicks into account, but not nicks of
|
||||
eventual puppets.
|
||||
|
||||
raw_string - raw command input coming from the command line.
|
||||
sessid - the optional session id to be responsible for the command-send
|
||||
**kwargs - other keyword arguments will be added to the found command
|
||||
object instace as variables before it executes. This is
|
||||
unused by default Evennia but may be used to set flags and
|
||||
change operating paramaters for commands at run-time.
|
||||
"""
|
||||
raw_string = to_unicode(raw_string)
|
||||
raw_string = self.nicks.nickreplace(raw_string,
|
||||
categories=("inputline", "channel"), include_player=False)
|
||||
if not sessid and _MULTISESSION_MODE in (0, 1):
|
||||
# in this case, we should either have only one sessid, or the sessid
|
||||
# should not matter (since the return goes to all of them we can
|
||||
# just use the first one as the source)
|
||||
try:
|
||||
sessid = self.get_all_sessions()[0].sessid
|
||||
except IndexError:
|
||||
# this can happen for bots
|
||||
sessid = None
|
||||
return cmdhandler.cmdhandler(self, raw_string,
|
||||
callertype="player", sessid=sessid, **kwargs)
|
||||
|
||||
def search(self, searchdata, return_puppet=False, **kwargs):
|
||||
"""
|
||||
This is similar to the ObjectDB search method but will search for
|
||||
Players only. Errors will be echoed, and None returned if no Player
|
||||
is found.
|
||||
searchdata - search criterion, the Player's key or dbref to search for
|
||||
return_puppet - will try to return the object the player controls
|
||||
instead of the Player object itself. If no
|
||||
puppeted object exists (since Player is OOC), None will
|
||||
be returned.
|
||||
Extra keywords are ignored, but are allowed in call in order to make
|
||||
API more consistent with objects.models.TypedObject.search.
|
||||
"""
|
||||
# handle me, self and *me, *self
|
||||
if isinstance(searchdata, basestring):
|
||||
# handle wrapping of common terms
|
||||
if searchdata.lower() in ("me", "*me", "self", "*self",):
|
||||
return self
|
||||
matches = self.__class__.objects.player_search(searchdata)
|
||||
matches = _AT_SEARCH_RESULT(self, searchdata, matches, global_search=True)
|
||||
if matches and return_puppet:
|
||||
try:
|
||||
return matches.puppet
|
||||
except AttributeError:
|
||||
return None
|
||||
return matches
|
||||
|
||||
def is_typeclass(self, typeclass, exact=False):
|
||||
"""
|
||||
Returns true if this object has this type
|
||||
OR has a typeclass which is an subclass of
|
||||
the given typeclass.
|
||||
|
||||
typeclass - can be a class object or the
|
||||
python path to such an object to match against.
|
||||
|
||||
exact - returns true only if the object's
|
||||
type is exactly this typeclass, ignoring
|
||||
parents.
|
||||
|
||||
Returns: Boolean
|
||||
"""
|
||||
return super(DefaultPlayer, self).is_typeclass(typeclass, exact=exact)
|
||||
|
||||
def swap_typeclass(self, new_typeclass, clean_attributes=False, no_default=True):
|
||||
"""
|
||||
This performs an in-situ swap of the typeclass. This means
|
||||
that in-game, this object will suddenly be something else.
|
||||
Player will not be affected. To 'move' a player to a different
|
||||
object entirely (while retaining this object's type), use
|
||||
self.player.swap_object().
|
||||
|
||||
Note that this might be an error prone operation if the
|
||||
old/new typeclass was heavily customized - your code
|
||||
might expect one and not the other, so be careful to
|
||||
bug test your code if using this feature! Often its easiest
|
||||
to create a new object and just swap the player over to
|
||||
that one instead.
|
||||
|
||||
Arguments:
|
||||
new_typeclass (path/classobj) - type to switch to
|
||||
clean_attributes (bool/list) - will delete all attributes
|
||||
stored on this object (but not any
|
||||
of the database fields such as name or
|
||||
location). You can't get attributes back,
|
||||
but this is often the safest bet to make
|
||||
sure nothing in the new typeclass clashes
|
||||
with the old one. If you supply a list,
|
||||
only those named attributes will be cleared.
|
||||
no_default - if this is active, the swapper will not allow for
|
||||
swapping to a default typeclass in case the given
|
||||
one fails for some reason. Instead the old one
|
||||
will be preserved.
|
||||
Returns:
|
||||
boolean True/False depending on if the swap worked or not.
|
||||
|
||||
"""
|
||||
super(DefaultPlayer, self).swap_typeclass(new_typeclass,
|
||||
clean_attributes=clean_attributes, no_default=no_default)
|
||||
|
||||
def access(self, accessing_obj, access_type='read', default=False, **kwargs):
|
||||
"""
|
||||
Determines if another object has permission to access this object
|
||||
in whatever way.
|
||||
|
||||
accessing_obj (Object)- object trying to access this one
|
||||
access_type (string) - type of access sought
|
||||
default (bool) - what to return if no lock of access_type was found
|
||||
**kwargs - passed to the at_access hook along with the result.
|
||||
"""
|
||||
result = super(DefaultPlayer, self).access(accessing_obj, access_type=access_type, default=default)
|
||||
self.at_access(result, accessing_obj, access_type, **kwargs)
|
||||
return result
|
||||
|
||||
def check_permstring(self, permstring):
|
||||
"""
|
||||
This explicitly checks the given string against this object's
|
||||
'permissions' property without involving any locks.
|
||||
|
||||
permstring (string) - permission string that need to match a permission
|
||||
on the object. (example: 'Builders')
|
||||
Note that this method does -not- call the at_access hook.
|
||||
"""
|
||||
return super(DefaultPlayer, self).check_permstring(permstring)
|
||||
|
||||
## player hooks
|
||||
|
||||
def basetype_setup(self):
|
||||
"""
|
||||
This sets up the basic properties for a player.
|
||||
Overload this with at_player_creation rather than
|
||||
changing this method.
|
||||
|
||||
"""
|
||||
# A basic security setup
|
||||
lockstring = "examine:perm(Wizards);edit:perm(Wizards);delete:perm(Wizards);boot:perm(Wizards);msg:all()"
|
||||
self.locks.add(lockstring)
|
||||
|
||||
# The ooc player cmdset
|
||||
self.cmdset.add_default(_CMDSET_PLAYER, permanent=True)
|
||||
|
||||
def at_player_creation(self):
|
||||
"""
|
||||
This is called once, the very first time
|
||||
the player is created (i.e. first time they
|
||||
register with the game). It's a good place
|
||||
to store attributes all players should have,
|
||||
like configuration values etc.
|
||||
"""
|
||||
# set an (empty) attribute holding the characters this player has
|
||||
lockstring = "attrread:perm(Admins);attredit:perm(Admins);attrcreate:perm(Admins)"
|
||||
self.attributes.add("_playable_characters", [], lockstring=lockstring)
|
||||
|
||||
# TODO - handle this in __init__ instead.
|
||||
def at_init(self):
|
||||
"""
|
||||
This is always called whenever this object is initiated --
|
||||
that is, whenever it its typeclass is cached from memory. This
|
||||
happens on-demand first time the object is used or activated
|
||||
in some way after being created but also after each server
|
||||
restart or reload. In the case of player objects, this usually
|
||||
happens the moment the player logs in or reconnects after a
|
||||
reload.
|
||||
"""
|
||||
pass
|
||||
|
||||
|
||||
# Note that the hooks below also exist in the character object's
|
||||
# typeclass. You can often ignore these and rely on the character
|
||||
# ones instead, unless you are implementing a multi-character game
|
||||
# and have some things that should be done regardless of which
|
||||
# character is currently connected to this player.
|
||||
|
||||
def at_first_save(self):
|
||||
"""
|
||||
This is a generic hook called by Evennia when this object is
|
||||
saved to the database the very first time. You generally
|
||||
don't override this method but the hooks called by it.
|
||||
"""
|
||||
self.basetype_setup()
|
||||
self.at_player_creation()
|
||||
|
||||
permissions = settings.PERMISSION_PLAYER_DEFAULT
|
||||
if hasattr(self, "_createdict"):
|
||||
# this will only be set if the utils.create_player
|
||||
# function was used to create the object.
|
||||
cdict = self._createdict
|
||||
if cdict.get("locks"):
|
||||
self.locks.add(cdict["locks"])
|
||||
if cdict.get("permissions"):
|
||||
permissions = cdict["permissions"]
|
||||
del self._createdict
|
||||
|
||||
self.permissions.add(permissions)
|
||||
|
||||
def at_access(self, result, accessing_obj, access_type, **kwargs):
|
||||
"""
|
||||
This is called with the result of an access call, along with
|
||||
any kwargs used for that call. The return of this method does
|
||||
not affect the result of the lock check. It can be used e.g. to
|
||||
customize error messages in a central location or other effects
|
||||
based on the access result.
|
||||
"""
|
||||
pass
|
||||
|
||||
def at_cmdset_get(self, **kwargs):
|
||||
"""
|
||||
Called just before cmdsets on this player are requested by the
|
||||
command handler. If changes need to be done on the fly to the
|
||||
cmdset before passing them on to the cmdhandler, this is the
|
||||
place to do it. This is called also if the player currently
|
||||
have no cmdsets. kwargs are usually not used unless the
|
||||
cmdset is generated dynamically.
|
||||
"""
|
||||
pass
|
||||
|
||||
def at_first_login(self):
|
||||
"""
|
||||
Called the very first time this player logs into the game.
|
||||
"""
|
||||
pass
|
||||
|
||||
def at_pre_login(self):
|
||||
"""
|
||||
Called every time the user logs in, just before the actual
|
||||
login-state is set.
|
||||
"""
|
||||
pass
|
||||
|
||||
def _send_to_connect_channel(self, message):
|
||||
"Helper method for loading the default comm channel"
|
||||
global _CONNECT_CHANNEL
|
||||
if not _CONNECT_CHANNEL:
|
||||
try:
|
||||
_CONNECT_CHANNEL = ChannelDB.objects.filter(db_key=settings.CHANNEL_CONNECTINFO[0])[0]
|
||||
except Exception:
|
||||
logger.log_trace()
|
||||
now = datetime.datetime.now()
|
||||
now = "%02i-%02i-%02i(%02i:%02i)" % (now.year, now.month,
|
||||
now.day, now.hour, now.minute)
|
||||
if _CONNECT_CHANNEL:
|
||||
_CONNECT_CHANNEL.tempmsg("[%s, %s]: %s" % (_CONNECT_CHANNEL.key, now, message))
|
||||
else:
|
||||
logger.log_infomsg("[%s]: %s" % (now, message))
|
||||
|
||||
def at_post_login(self, sessid=None):
|
||||
"""
|
||||
Called at the end of the login process, just before letting
|
||||
them loose. This is called before an eventual Character's
|
||||
at_post_login hook.
|
||||
"""
|
||||
self._send_to_connect_channel("{G%s connected{n" % self.key)
|
||||
if _MULTISESSION_MODE == 0:
|
||||
# in this mode we should have only one character available. We
|
||||
# try to auto-connect to it by calling the @ic command
|
||||
# (this relies on player.db._last_puppet being set)
|
||||
self.execute_cmd("@ic", sessid=sessid)
|
||||
elif _MULTISESSION_MODE == 1:
|
||||
# in this mode the first session to connect acts like mode 0,
|
||||
# the following sessions "share" the same view and should
|
||||
# not perform any actions
|
||||
if not self.get_all_puppets():
|
||||
self.execute_cmd("@ic", sessid=sessid)
|
||||
elif _MULTISESSION_MODE in (2, 3):
|
||||
# In this mode we by default end up at a character selection
|
||||
# screen. We execute look on the player.
|
||||
self.execute_cmd("look", sessid=sessid)
|
||||
|
||||
def at_disconnect(self, reason=None):
|
||||
"""
|
||||
Called just before user is disconnected.
|
||||
"""
|
||||
reason = reason and "(%s)" % reason or ""
|
||||
self._send_to_connect_channel("{R%s disconnected %s{n" % (self.key, reason))
|
||||
|
||||
def at_post_disconnect(self):
|
||||
"""
|
||||
This is called after disconnection is complete. No messages
|
||||
can be relayed to the player from here. After this call, the
|
||||
player should not be accessed any more, making this a good
|
||||
spot for deleting it (in the case of a guest player account,
|
||||
for example).
|
||||
"""
|
||||
pass
|
||||
|
||||
def at_message_receive(self, message, from_obj=None):
|
||||
"""
|
||||
Called when any text is emitted to this
|
||||
object. If it returns False, no text
|
||||
will be sent automatically.
|
||||
"""
|
||||
return True
|
||||
|
||||
def at_message_send(self, message, to_object):
|
||||
"""
|
||||
Called whenever this object tries to send text
|
||||
to another object. Only called if the object supplied
|
||||
itself as a sender in the msg() call.
|
||||
"""
|
||||
pass
|
||||
|
||||
def at_server_reload(self):
|
||||
"""
|
||||
This hook is called whenever the server is shutting down for
|
||||
restart/reboot. If you want to, for example, save non-persistent
|
||||
properties across a restart, this is the place to do it.
|
||||
"""
|
||||
pass
|
||||
|
||||
def at_server_shutdown(self):
|
||||
"""
|
||||
This hook is called whenever the server is shutting down fully
|
||||
(i.e. not for a restart).
|
||||
"""
|
||||
pass
|
||||
|
||||
|
||||
class Guest(DefaultPlayer):
|
||||
"""
|
||||
This class is used for guest logins. Unlike Players, Guests and their
|
||||
characters are deleted after disconnection.
|
||||
"""
|
||||
def at_post_login(self, sessid=None):
|
||||
"""
|
||||
In theory, guests only have one character regardless of which
|
||||
MULTISESSION_MODE we're in. They don't get a choice.
|
||||
"""
|
||||
self._send_to_connect_channel("{G%s connected{n" % self.key)
|
||||
self.execute_cmd("@ic", sessid=sessid)
|
||||
|
||||
def at_disconnect(self):
|
||||
"""
|
||||
A Guest's characters aren't meant to linger on the server. When a
|
||||
Guest disconnects, we remove its character.
|
||||
"""
|
||||
super(Guest, self).at_disconnect()
|
||||
characters = self.db._playable_characters
|
||||
for character in filter(None, characters):
|
||||
character.delete()
|
||||
|
||||
def at_server_shutdown(self):
|
||||
"""
|
||||
We repeat at_disconnect() here just to be on the safe side.
|
||||
"""
|
||||
super(Guest, self).at_server_shutdown()
|
||||
characters = self.db._playable_characters
|
||||
for character in filter(None, characters):
|
||||
character.delete()
|
||||
|
||||
def at_post_disconnect(self):
|
||||
"""
|
||||
Guests aren't meant to linger on the server, either. We need to wait
|
||||
until after the Guest disconnects to delete it, though.
|
||||
"""
|
||||
super(Guest, self).at_post_disconnect()
|
||||
self.delete()
|
||||
15
lib/scripts/__init__.py
Normal file
15
lib/scripts/__init__.py
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
"""
|
||||
Makes it easier to import by grouping all relevant things already at this
|
||||
level.
|
||||
|
||||
You can henceforth import most things directly from src.scripts
|
||||
Also, the initiated object manager is available as src.scripts.manager.
|
||||
|
||||
"""
|
||||
|
||||
# Note - we MUST NOT import src.scripts.scripts here, or
|
||||
# proxy models will fall under Django migrations.
|
||||
#from src.scripts.scripts import *
|
||||
#from src.scripts.models import ScriptDB
|
||||
|
||||
#manager = ScriptDB.objects
|
||||
40
lib/scripts/admin.py
Normal file
40
lib/scripts/admin.py
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
#
|
||||
# This sets up how models are displayed
|
||||
# in the web admin interface.
|
||||
#
|
||||
from src.typeclasses.admin import AttributeInline, TagInline
|
||||
|
||||
from src.scripts.models import ScriptDB
|
||||
from django.contrib import admin
|
||||
|
||||
|
||||
class ScriptTagInline(TagInline):
|
||||
model = ScriptDB.db_tags.through
|
||||
|
||||
|
||||
class ScriptAttributeInline(AttributeInline):
|
||||
model = ScriptDB.db_attributes.through
|
||||
|
||||
|
||||
class ScriptDBAdmin(admin.ModelAdmin):
|
||||
|
||||
list_display = ('id', 'db_key', 'db_typeclass_path',
|
||||
'db_obj', 'db_interval', 'db_repeats', 'db_persistent')
|
||||
list_display_links = ('id', 'db_key')
|
||||
ordering = ['db_obj', 'db_typeclass_path']
|
||||
search_fields = ['^db_key', 'db_typeclass_path']
|
||||
save_as = True
|
||||
save_on_top = True
|
||||
list_select_related = True
|
||||
raw_id_fields = ('db_obj',)
|
||||
|
||||
fieldsets = (
|
||||
(None, {
|
||||
'fields': (('db_key', 'db_typeclass_path'), 'db_interval',
|
||||
'db_repeats', 'db_start_delay', 'db_persistent',
|
||||
'db_obj')}),
|
||||
)
|
||||
inlines = [ScriptTagInline, ScriptAttributeInline]
|
||||
|
||||
|
||||
admin.site.register(ScriptDB, ScriptDBAdmin)
|
||||
234
lib/scripts/manager.py
Normal file
234
lib/scripts/manager.py
Normal file
|
|
@ -0,0 +1,234 @@
|
|||
"""
|
||||
The custom manager for Scripts.
|
||||
"""
|
||||
|
||||
from django.db.models import Q
|
||||
from src.typeclasses.managers import TypedObjectManager, TypeclassManager
|
||||
from src.typeclasses.managers import returns_typeclass_list
|
||||
from src.utils.utils import make_iter
|
||||
__all__ = ("ScriptManager",)
|
||||
_GA = object.__getattribute__
|
||||
|
||||
VALIDATE_ITERATION = 0
|
||||
|
||||
|
||||
class ScriptDBManager(TypedObjectManager):
|
||||
"""
|
||||
This Scriptmanager implements methods for searching
|
||||
and manipulating Scripts directly from the database.
|
||||
|
||||
Evennia-specific search methods (will return Typeclasses or
|
||||
lists of Typeclasses, whereas Django-general methods will return
|
||||
Querysets or database objects).
|
||||
|
||||
dbref (converter)
|
||||
get_id (or dbref_search)
|
||||
get_dbref_range
|
||||
object_totals
|
||||
typeclass_search
|
||||
get_all_scripts_on_obj
|
||||
get_all_scripts
|
||||
delete_script
|
||||
remove_non_persistent
|
||||
validate
|
||||
script_search (equivalent to ev.search_script)
|
||||
copy_script
|
||||
|
||||
"""
|
||||
@returns_typeclass_list
|
||||
def get_all_scripts_on_obj(self, obj, key=None):
|
||||
"""
|
||||
Returns as result all the Scripts related to a particular object.
|
||||
key can be given as a dbref or name string. If given, only scripts
|
||||
matching the key on the object will be returned.
|
||||
"""
|
||||
if not obj:
|
||||
return []
|
||||
player = _GA(_GA(obj, "__class__"), "__name__") == "PlayerDB"
|
||||
if key:
|
||||
dbref = self.dbref(key)
|
||||
if dbref or dbref == 0:
|
||||
if player:
|
||||
return self.filter(db_player=obj, id=dbref)
|
||||
else:
|
||||
return self.filter(db_obj=obj, id=dbref)
|
||||
elif player:
|
||||
return self.filter(db_player=obj, db_key=key)
|
||||
else:
|
||||
return self.filter(db_obj=obj, db_key=key)
|
||||
elif player:
|
||||
return self.filter(db_player=obj)
|
||||
else:
|
||||
return self.filter(db_obj=obj)
|
||||
|
||||
@returns_typeclass_list
|
||||
def get_all_scripts(self, key=None):
|
||||
"""
|
||||
Return all scripts, alternative only
|
||||
scripts with a certain key/dbref
|
||||
"""
|
||||
if key:
|
||||
script = []
|
||||
dbref = self.dbref(key)
|
||||
if dbref or dbref == 0:
|
||||
script = self.dbref_search(dbref)
|
||||
if not script:
|
||||
script = self.filter(db_key=key)
|
||||
return script
|
||||
return self.all()
|
||||
|
||||
def delete_script(self, dbref):
|
||||
"""
|
||||
This stops and deletes a specific script directly
|
||||
from the script database. This might be
|
||||
needed for global scripts not tied to
|
||||
a specific game object.
|
||||
"""
|
||||
scripts = self.get_id(dbref)
|
||||
for script in make_iter(scripts):
|
||||
script.stop()
|
||||
|
||||
def remove_non_persistent(self, obj=None):
|
||||
"""
|
||||
This cleans up the script database of all non-persistent
|
||||
scripts, or only those on obj. It is called every time the server
|
||||
restarts.
|
||||
"""
|
||||
if obj:
|
||||
to_stop = self.filter(db_obj=obj, db_persistent=False, db_is_active=True)
|
||||
to_delete = self.filter(db_obj=obj, db_persistent=False, db_is_active=False)
|
||||
else:
|
||||
to_stop = self.filter(db_persistent=False, db_is_active=True)
|
||||
to_delete = self.filter(db_persistent=False, db_is_active=False)
|
||||
nr_deleted = to_stop.count() + to_delete.count()
|
||||
for script in to_stop:
|
||||
script.stop()
|
||||
for script in to_delete:
|
||||
script.delete()
|
||||
return nr_deleted
|
||||
|
||||
def validate(self, scripts=None, obj=None, key=None, dbref=None,
|
||||
init_mode=False):
|
||||
"""
|
||||
This will step through the script database and make sure
|
||||
all objects run scripts that are still valid in the context
|
||||
they are in. This is called by the game engine at regular
|
||||
intervals but can also be initiated by player scripts.
|
||||
If key and/or obj is given, only update the related
|
||||
script/object.
|
||||
|
||||
Only one of the arguments are supposed to be supplied
|
||||
at a time, since they are exclusive to each other.
|
||||
|
||||
scripts = a list of scripts objects obtained somewhere.
|
||||
obj = validate only scripts defined on a special object.
|
||||
key = validate only scripts with a particular key
|
||||
dbref = validate only the single script with this particular id.
|
||||
|
||||
init_mode - This is used during server upstart and can have
|
||||
three values:
|
||||
False (no init mode). Called during run.
|
||||
"reset" - server reboot. Kill non-persistent scripts
|
||||
"reload" - server reload. Keep non-persistent scripts.
|
||||
|
||||
This method also makes sure start any scripts it validates,
|
||||
this should be harmless, since already-active scripts
|
||||
have the property 'is_running' set and will be skipped.
|
||||
"""
|
||||
|
||||
# we store a variable that tracks if we are calling a
|
||||
# validation from within another validation (avoids
|
||||
# loops).
|
||||
|
||||
global VALIDATE_ITERATION
|
||||
if VALIDATE_ITERATION > 0:
|
||||
# we are in a nested validation. Exit.
|
||||
VALIDATE_ITERATION -= 1
|
||||
return None, None
|
||||
VALIDATE_ITERATION += 1
|
||||
|
||||
# not in a validation - loop. Validate as normal.
|
||||
|
||||
nr_started = 0
|
||||
nr_stopped = 0
|
||||
|
||||
if init_mode:
|
||||
if init_mode == 'reset':
|
||||
# special mode when server starts or object logs in.
|
||||
# This deletes all non-persistent scripts from database
|
||||
nr_stopped += self.remove_non_persistent(obj=obj)
|
||||
# turn off the activity flag for all remaining scripts
|
||||
scripts = self.get_all_scripts()
|
||||
for script in scripts:
|
||||
script.is_active = False
|
||||
|
||||
elif not scripts:
|
||||
# normal operation
|
||||
if dbref and self.dbref(dbref, reqhash=False):
|
||||
scripts = self.get_id(dbref)
|
||||
elif obj:
|
||||
#print "calling get_all_scripts_on_obj", obj, key, VALIDATE_ITERATION
|
||||
scripts = self.get_all_scripts_on_obj(obj, key=key)
|
||||
else:
|
||||
scripts = self.get_all_scripts(key=key) #self.model.get_all_cached_instances()
|
||||
|
||||
if not scripts:
|
||||
# no scripts available to validate
|
||||
VALIDATE_ITERATION -= 1
|
||||
return None, None
|
||||
|
||||
#print "scripts to validate: [%s]" % (", ".join(script.key for script in scripts))
|
||||
for script in scripts:
|
||||
#print "validating %s (%i) (init_mode=%s)" % (script.key, id(script), init_mode)
|
||||
if script.is_valid():
|
||||
nr_started += script.start(force_restart=init_mode)
|
||||
#print "back from start. nr_started=", nr_started
|
||||
else:
|
||||
script.stop()
|
||||
nr_stopped += 1
|
||||
VALIDATE_ITERATION -= 1
|
||||
return nr_started, nr_stopped
|
||||
|
||||
@returns_typeclass_list
|
||||
def script_search(self, ostring, obj=None, only_timed=False):
|
||||
"""
|
||||
Search for a particular script.
|
||||
|
||||
ostring - search criterion - a script ID or key
|
||||
obj - limit search to scripts defined on this object
|
||||
only_timed - limit search only to scripts that run
|
||||
on a timer.
|
||||
"""
|
||||
|
||||
ostring = ostring.strip()
|
||||
|
||||
dbref = self.dbref(ostring)
|
||||
if dbref or dbref == 0:
|
||||
# this is a dbref, try to find the script directly
|
||||
dbref_match = self.dbref_search(dbref)
|
||||
if dbref_match and not ((obj and obj != dbref_match.obj)
|
||||
or (only_timed and dbref_match.interval)):
|
||||
return [dbref_match]
|
||||
|
||||
# not a dbref; normal search
|
||||
obj_restriction = obj and Q(db_obj=obj) or Q()
|
||||
timed_restriction = only_timed and Q(interval__gt=0) or Q()
|
||||
scripts = self.filter(timed_restriction & obj_restriction & Q(db_key__iexact=ostring))
|
||||
return scripts
|
||||
|
||||
def copy_script(self, original_script, new_key=None, new_obj=None, new_locks=None):
|
||||
"""
|
||||
Make an identical copy of the original_script
|
||||
"""
|
||||
typeclass = original_script.typeclass_path
|
||||
new_key = new_key if new_key is not None else original_script.key
|
||||
new_obj = new_obj if new_obj is not None else original_script.obj
|
||||
new_locks = new_locks if new_locks is not None else original_script.db_lock_storage
|
||||
|
||||
from src.utils import create
|
||||
new_script = create.create_script(typeclass, key=new_key, obj=new_obj,
|
||||
locks=new_locks, autostart=True)
|
||||
return new_script
|
||||
|
||||
class ScriptManager(ScriptDBManager, TypeclassManager):
|
||||
pass
|
||||
41
lib/scripts/migrations/0001_initial.py
Normal file
41
lib/scripts/migrations/0001_initial.py
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import models, migrations
|
||||
from django.conf import settings
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('objects', '0001_initial'),
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
('typeclasses', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='ScriptDB',
|
||||
fields=[
|
||||
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
|
||||
('db_key', models.CharField(max_length=255, verbose_name=b'key', db_index=True)),
|
||||
('db_typeclass_path', models.CharField(help_text=b"this defines what 'type' of entity this is. This variable holds a Python path to a module with a valid Evennia Typeclass.", max_length=255, null=True, verbose_name=b'typeclass')),
|
||||
('db_date_created', models.DateTimeField(auto_now_add=True, verbose_name=b'creation date')),
|
||||
('db_lock_storage', models.TextField(help_text=b"locks limit access to an entity. A lock is defined as a 'lock string' on the form 'type:lockfunctions', defining what functionality is locked and how to determine access. Not defining a lock means no access is granted.", verbose_name=b'locks', blank=True)),
|
||||
('db_desc', models.CharField(max_length=255, verbose_name=b'desc', blank=True)),
|
||||
('db_interval', models.IntegerField(default=-1, help_text=b'how often to repeat script, in seconds. -1 means off.', verbose_name=b'interval')),
|
||||
('db_start_delay', models.BooleanField(default=False, help_text=b'pause interval seconds before starting.', verbose_name=b'start delay')),
|
||||
('db_repeats', models.IntegerField(default=0, help_text=b'0 means off.', verbose_name=b'number of repeats')),
|
||||
('db_persistent', models.BooleanField(default=False, verbose_name=b'survive server reboot')),
|
||||
('db_is_active', models.BooleanField(default=False, verbose_name=b'script active')),
|
||||
('db_attributes', models.ManyToManyField(help_text=b'attributes on this object. An attribute can hold any pickle-able python object (see docs for special cases).', to='typeclasses.Attribute', null=True)),
|
||||
('db_obj', models.ForeignKey(blank=True, to='objects.ObjectDB', help_text=b'the object to store this script on, if not a global script.', null=True, verbose_name=b'scripted object')),
|
||||
('db_player', models.ForeignKey(blank=True, to=settings.AUTH_USER_MODEL, help_text=b'the player to store this script on (should not be set if obj is set)', null=True, verbose_name=b'scripted player')),
|
||||
('db_tags', models.ManyToManyField(help_text=b'tags on this object. Tags are simple string markers to identify, group and alias objects.', to='typeclasses.Tag', null=True)),
|
||||
],
|
||||
options={
|
||||
'verbose_name': 'Script',
|
||||
},
|
||||
bases=(models.Model,),
|
||||
),
|
||||
]
|
||||
1
lib/scripts/migrations/__init__.py
Normal file
1
lib/scripts/migrations/__init__.py
Normal file
|
|
@ -0,0 +1 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
176
lib/scripts/models.py
Normal file
176
lib/scripts/models.py
Normal file
|
|
@ -0,0 +1,176 @@
|
|||
"""
|
||||
Scripts are entities that perform some sort of action, either only
|
||||
once or repeatedly. They can be directly linked to a particular
|
||||
Evennia Object or be stand-alonw (in the latter case it is considered
|
||||
a 'global' script). Scripts can indicate both actions related to the
|
||||
game world as well as pure behind-the-scenes events and
|
||||
effects. Everything that has a time component in the game (i.e. is not
|
||||
hard-coded at startup or directly created/controlled by players) is
|
||||
handled by Scripts.
|
||||
|
||||
Scripts have to check for themselves that they should be applied at a
|
||||
particular moment of time; this is handled by the is_valid() hook.
|
||||
Scripts can also implement at_start and at_end hooks for preparing and
|
||||
cleaning whatever effect they have had on the game object.
|
||||
|
||||
Common examples of uses of Scripts:
|
||||
- load the default cmdset to the player object's cmdhandler
|
||||
when logging in.
|
||||
- switch to a different state, such as entering a text editor,
|
||||
start combat or enter a dark room.
|
||||
- Weather patterns in-game
|
||||
- merge a new cmdset with the default one for changing which
|
||||
commands are available at a particular time
|
||||
- give the player/object a time-limited bonus/effect
|
||||
|
||||
"""
|
||||
from django.conf import settings
|
||||
from django.db import models
|
||||
from django.core.exceptions import ObjectDoesNotExist
|
||||
from src.typeclasses.models import TypedObject
|
||||
from src.scripts.manager import ScriptDBManager
|
||||
from src.utils.utils import dbref, to_str
|
||||
|
||||
__all__ = ("ScriptDB",)
|
||||
_GA = object.__getattribute__
|
||||
_SA = object.__setattr__
|
||||
|
||||
|
||||
#------------------------------------------------------------
|
||||
#
|
||||
# ScriptDB
|
||||
#
|
||||
#------------------------------------------------------------
|
||||
|
||||
class ScriptDB(TypedObject):
|
||||
"""
|
||||
The Script database representation.
|
||||
|
||||
The TypedObject supplies the following (inherited) properties:
|
||||
key - main name
|
||||
name - alias for key
|
||||
typeclass_path - the path to the decorating typeclass
|
||||
typeclass - auto-linked typeclass
|
||||
date_created - time stamp of object creation
|
||||
permissions - perm strings
|
||||
dbref - #id of object
|
||||
db - persistent attribute storage
|
||||
ndb - non-persistent attribute storage
|
||||
|
||||
The ScriptDB adds the following properties:
|
||||
desc - optional description of script
|
||||
obj - the object the script is linked to, if any
|
||||
player - the player the script is linked to (exclusive with obj)
|
||||
interval - how often script should run
|
||||
start_delay - if the script should start repeating right away
|
||||
repeats - how many times the script should repeat
|
||||
persistent - if script should survive a server reboot
|
||||
is_active - bool if script is currently running
|
||||
|
||||
"""
|
||||
|
||||
|
||||
#
|
||||
# ScriptDB Database Model setup
|
||||
#
|
||||
# These database fields are all set using their corresponding properties,
|
||||
# named same as the field, but withtou the db_* prefix.
|
||||
|
||||
# inherited fields (from TypedObject):
|
||||
# db_key, db_typeclass_path, db_date_created, db_permissions
|
||||
|
||||
# optional description.
|
||||
db_desc = models.CharField('desc', max_length=255, blank=True)
|
||||
# A reference to the database object affected by this Script, if any.
|
||||
db_obj = models.ForeignKey("objects.ObjectDB", null=True, blank=True, verbose_name='scripted object',
|
||||
help_text='the object to store this script on, if not a global script.')
|
||||
db_player = models.ForeignKey("players.PlayerDB", null=True, blank=True, verbose_name="scripted player",
|
||||
help_text='the player to store this script on (should not be set if obj is set)')
|
||||
# how often to run Script (secs). -1 means there is no timer
|
||||
db_interval = models.IntegerField('interval', default=-1, help_text='how often to repeat script, in seconds. -1 means off.')
|
||||
# start script right away or wait interval seconds first
|
||||
db_start_delay = models.BooleanField('start delay', default=False, help_text='pause interval seconds before starting.')
|
||||
# how many times this script is to be repeated, if interval!=0.
|
||||
db_repeats = models.IntegerField('number of repeats', default=0, help_text='0 means off.')
|
||||
# defines if this script should survive a reboot or not
|
||||
db_persistent = models.BooleanField('survive server reboot', default=False)
|
||||
# defines if this script has already been started in this session
|
||||
db_is_active = models.BooleanField('script active', default=False)
|
||||
|
||||
# Database manager
|
||||
objects = ScriptDBManager()
|
||||
|
||||
class Meta:
|
||||
"Define Django meta options"
|
||||
verbose_name = "Script"
|
||||
|
||||
#
|
||||
#
|
||||
# ScriptDB class properties
|
||||
#
|
||||
#
|
||||
|
||||
# obj property
|
||||
def __get_obj(self):
|
||||
"""
|
||||
property wrapper that homogenizes access to either
|
||||
the db_player or db_obj field, using the same obj
|
||||
property name
|
||||
"""
|
||||
obj = _GA(self, "db_player")
|
||||
if not obj:
|
||||
obj = _GA(self, "db_obj")
|
||||
return obj
|
||||
|
||||
def __set_obj(self, value):
|
||||
"""
|
||||
Set player or obj to their right database field. If
|
||||
a dbref is given, assume ObjectDB.
|
||||
"""
|
||||
try:
|
||||
value = _GA(value, "dbobj")
|
||||
except AttributeError:
|
||||
pass
|
||||
if isinstance(value, (basestring, int)):
|
||||
from src.objects.models import ObjectDB
|
||||
value = to_str(value, force_string=True)
|
||||
if (value.isdigit() or value.startswith("#")):
|
||||
dbid = dbref(value, reqhash=False)
|
||||
if dbid:
|
||||
try:
|
||||
value = ObjectDB.objects.get(id=dbid)
|
||||
except ObjectDoesNotExist:
|
||||
# maybe it is just a name that happens to look like a dbid
|
||||
pass
|
||||
if value.__class__.__name__ == "PlayerDB":
|
||||
fname = "db_player"
|
||||
_SA(self, fname, value)
|
||||
else:
|
||||
fname = "db_obj"
|
||||
_SA(self, fname, value)
|
||||
# saving the field
|
||||
_GA(self, "save")(update_fields=[fname])
|
||||
obj = property(__get_obj, __set_obj)
|
||||
object = property(__get_obj, __set_obj)
|
||||
|
||||
|
||||
# def at_typeclass_error(self):
|
||||
# """
|
||||
# If this is called, it means the typeclass has a critical
|
||||
# error and cannot even be loaded. We don't allow a script
|
||||
# to be created under those circumstances. Already created,
|
||||
# permanent scripts are set to already be active so they
|
||||
# won't get activated now (next reboot the bug might be fixed)
|
||||
# """
|
||||
# # By setting is_active=True, we trick the script not to run "again".
|
||||
# self.is_active = True
|
||||
# return super(ScriptDB, self).at_typeclass_error()
|
||||
#
|
||||
# delete_iter = 0
|
||||
# def delete(self):
|
||||
# "Delete script"
|
||||
# if self.delete_iter > 0:
|
||||
# return
|
||||
# self.delete_iter += 1
|
||||
# _GA(self, "attributes").clear()
|
||||
# super(ScriptDB, self).delete()
|
||||
126
lib/scripts/scripthandler.py
Normal file
126
lib/scripts/scripthandler.py
Normal file
|
|
@ -0,0 +1,126 @@
|
|||
"""
|
||||
The script handler makes sure to check through all stored scripts
|
||||
to make sure they are still relevant.
|
||||
An scripthandler is automatically added to all game objects. You
|
||||
access it through the property 'scripts' on the game object.
|
||||
"""
|
||||
|
||||
from src.scripts.models import ScriptDB
|
||||
from src.utils import create
|
||||
from src.utils import logger
|
||||
|
||||
from django.utils.translation import ugettext as _
|
||||
|
||||
class ScriptHandler(object):
|
||||
"""
|
||||
Implements the handler. This sits on each game object.
|
||||
"""
|
||||
def __init__(self, obj):
|
||||
"""
|
||||
Set up internal state.
|
||||
obj - a reference to the object this handler is attached to.
|
||||
|
||||
We retrieve all scripts attached to this object and check
|
||||
if they are all peristent. If they are not, they are just
|
||||
cruft left over from a server shutdown.
|
||||
"""
|
||||
self.obj = obj
|
||||
|
||||
def __str__(self):
|
||||
"List the scripts tied to this object"
|
||||
scripts = ScriptDB.objects.get_all_scripts_on_obj(self.obj)
|
||||
string = ""
|
||||
for script in scripts:
|
||||
interval = "inf"
|
||||
next_repeat = "inf"
|
||||
repeats = "inf"
|
||||
if script.interval > 0:
|
||||
interval = script.interval
|
||||
if script.repeats:
|
||||
repeats = script.repeats
|
||||
try:
|
||||
next_repeat = script.time_until_next_repeat()
|
||||
except:
|
||||
next_repeat = "?"
|
||||
string += _("\n '%(key)s' (%(next_repeat)s/%(interval)s, %(repeats)s repeats): %(desc)s") % \
|
||||
{"key": script.key, "next_repeat": next_repeat,
|
||||
"interval": interval, "repeats": repeats, "desc": script.desc}
|
||||
return string.strip()
|
||||
|
||||
def add(self, scriptclass, key=None, autostart=True):
|
||||
"""
|
||||
Add an script to this object.
|
||||
|
||||
scriptclass - either a class object
|
||||
inheriting from Script, an instantiated script object
|
||||
or a python path to such a class object.
|
||||
key - optional identifier for the script (often set in script
|
||||
definition)
|
||||
autostart - start the script upon adding it
|
||||
"""
|
||||
if self.obj.__class__.__name__ == "PlayerDB":
|
||||
# we add to a Player, not an Object
|
||||
script = create.create_script(scriptclass, key=key, player=self.obj,
|
||||
autostart=autostart)
|
||||
else:
|
||||
# the normal - adding to an Object
|
||||
script = create.create_script(scriptclass, key=key, obj=self.obj,
|
||||
autostart=autostart)
|
||||
if not script:
|
||||
logger.log_errmsg("Script %s could not be created and/or started." % scriptclass)
|
||||
return False
|
||||
return True
|
||||
|
||||
def start(self, scriptid):
|
||||
"""
|
||||
Find an already added script and force-start it
|
||||
"""
|
||||
scripts = ScriptDB.objects.get_all_scripts_on_obj(self.obj, key=scriptid)
|
||||
num = 0
|
||||
for script in scripts:
|
||||
num += script.start()
|
||||
return num
|
||||
|
||||
def get(self, scriptid):
|
||||
"""
|
||||
Return one or all scripts on this object matching scriptid. Will return
|
||||
a list.
|
||||
"""
|
||||
return ScriptDB.objects.get_all_scripts_on_obj(self.obj, key=scriptid)
|
||||
|
||||
def delete(self, scriptid=None):
|
||||
"""
|
||||
Forcibly delete a script from this object.
|
||||
|
||||
scriptid can be a script key or the path to a script (in the
|
||||
latter case all scripts with this path will be deleted!)
|
||||
If no scriptid is set, delete all scripts on the object.
|
||||
|
||||
"""
|
||||
delscripts = ScriptDB.objects.get_all_scripts_on_obj(self.obj, key=scriptid)
|
||||
if not delscripts:
|
||||
delscripts = [script for script in ScriptDB.objects.get_all_scripts_on_obj(self.obj) if script.path == scriptid]
|
||||
num = 0
|
||||
for script in delscripts:
|
||||
num += script.stop()
|
||||
return num
|
||||
|
||||
def stop(self, scriptid=None):
|
||||
"""
|
||||
Alias for delete. scriptid can be a script key or a script path string.
|
||||
"""
|
||||
return self.delete(scriptid)
|
||||
|
||||
def all(self, scriptid=None):
|
||||
"""
|
||||
Get all scripts stored in the handler, alternatively all matching a key.
|
||||
"""
|
||||
return ScriptDB.objects.get_all_scripts_on_obj(self.obj, key=scriptid)
|
||||
|
||||
def validate(self, init_mode=False):
|
||||
"""
|
||||
Runs a validation on this object's scripts only.
|
||||
This should be called regularly to crank the wheels.
|
||||
"""
|
||||
ScriptDB.objects.validate(obj=self.obj, init_mode=init_mode)
|
||||
|
||||
610
lib/scripts/scripts.py
Normal file
610
lib/scripts/scripts.py
Normal file
|
|
@ -0,0 +1,610 @@
|
|||
"""
|
||||
This module contains the base Script class that all
|
||||
scripts are inheriting from.
|
||||
|
||||
It also defines a few common scripts.
|
||||
"""
|
||||
|
||||
from twisted.internet.defer import Deferred, maybeDeferred
|
||||
from twisted.internet.task import LoopingCall
|
||||
from django.conf import settings
|
||||
from src.typeclasses.models import TypeclassBase
|
||||
from django.utils.translation import ugettext as _
|
||||
from src.scripts.models import ScriptDB
|
||||
from src.scripts.manager import ScriptManager
|
||||
from src.comms import channelhandler
|
||||
from src.utils import logger
|
||||
|
||||
__all__ = ["Script", "DoNothing", "CheckSessions",
|
||||
"ValidateScripts", "ValidateChannelHandler"]
|
||||
|
||||
_GA = object.__getattribute__
|
||||
_SESSIONS = None
|
||||
|
||||
class ExtendedLoopingCall(LoopingCall):
|
||||
"""
|
||||
LoopingCall that can start at a delay different
|
||||
than self.interval.
|
||||
"""
|
||||
start_delay = None
|
||||
callcount = 0
|
||||
|
||||
def start(self, interval, now=True, start_delay=None, count_start=0):
|
||||
"""
|
||||
Start running function every interval seconds.
|
||||
|
||||
This overloads the LoopingCall default by offering
|
||||
the start_delay keyword and ability to repeat.
|
||||
|
||||
start_delay: The number of seconds before starting.
|
||||
If None, wait interval seconds. Only
|
||||
valid is now is False.
|
||||
repeat_start: the task will track how many times it has run.
|
||||
this will change where it starts counting from.
|
||||
Note that as opposed to Twisted's inbuild
|
||||
counter, this will count also if force_repeat()
|
||||
was called (so it will not just count the number
|
||||
of interval seconds since start).
|
||||
"""
|
||||
assert not self.running, ("Tried to start an already running "
|
||||
"ExtendedLoopingCall.")
|
||||
if interval < 0:
|
||||
raise ValueError, "interval must be >= 0"
|
||||
|
||||
self.running = True
|
||||
d = self.deferred = Deferred()
|
||||
self.starttime = self.clock.seconds()
|
||||
self._expectNextCallAt = self.starttime
|
||||
self.interval = interval
|
||||
self._runAtStart = now
|
||||
self.callcount = max(0, count_start)
|
||||
|
||||
if now:
|
||||
self()
|
||||
else:
|
||||
if start_delay is not None and start_delay >= 0:
|
||||
# we set start_delay after the _reshedule call to make
|
||||
# next_call_time() find it until next reshedule.
|
||||
self.interval = start_delay
|
||||
self._reschedule()
|
||||
self.interval = interval
|
||||
self.start_delay = start_delay
|
||||
else:
|
||||
self._reschedule()
|
||||
return d
|
||||
|
||||
def __call__(self):
|
||||
"tick one step"
|
||||
self.callcount += 1
|
||||
super(ExtendedLoopingCall, self).__call__()
|
||||
|
||||
def _reschedule(self):
|
||||
"""
|
||||
Handle call rescheduling including
|
||||
nulling start_delay and stopping if
|
||||
number of repeats is reached.
|
||||
"""
|
||||
self.start_delay = None
|
||||
super(ExtendedLoopingCall, self)._reschedule()
|
||||
|
||||
def force_repeat(self):
|
||||
"Force-fire the callback"
|
||||
assert self.running, ("Tried to fire an ExtendedLoopingCall "
|
||||
"that was not running.")
|
||||
if self.call is not None:
|
||||
self.call.cancel()
|
||||
self._expectNextCallAt = self.clock.seconds()
|
||||
self()
|
||||
|
||||
def next_call_time(self):
|
||||
"""
|
||||
Return the time in seconds until the next call. This takes
|
||||
start_delay into account.
|
||||
"""
|
||||
if self.running:
|
||||
currentTime = self.clock.seconds()
|
||||
return self._expectNextCallAt - currentTime
|
||||
return None
|
||||
|
||||
#
|
||||
# Base script, inherit from Script below instead.
|
||||
#
|
||||
class ScriptBase(ScriptDB):
|
||||
"""
|
||||
Base class for scripts. Don't inherit from this, inherit
|
||||
from the class 'Script' instead.
|
||||
"""
|
||||
__metaclass__ = TypeclassBase
|
||||
objects = ScriptManager()
|
||||
|
||||
def __eq__(self, other):
|
||||
"""
|
||||
This has to be located at this level, having it in the
|
||||
parent doesn't work.
|
||||
"""
|
||||
try:
|
||||
return other.dbid == self.dbid
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
def _start_task(self):
|
||||
"start task runner"
|
||||
|
||||
self.ndb._task = ExtendedLoopingCall(self._step_task)
|
||||
|
||||
if self.db._paused_time:
|
||||
# the script was paused; restarting
|
||||
callcount = self.db._paused_callcount or 0
|
||||
self.ndb._task.start(self.db_interval,
|
||||
now=False,
|
||||
start_delay=self.db._paused_time,
|
||||
count_start=callcount)
|
||||
del self.db._paused_time
|
||||
del self.db._paused_repeats
|
||||
else:
|
||||
# starting script anew
|
||||
self.ndb._task.start(self.db_interval,
|
||||
now=not self.db_start_delay)
|
||||
|
||||
def _stop_task(self):
|
||||
"stop task runner"
|
||||
task = self.ndb._task
|
||||
if task and task.running:
|
||||
task.stop()
|
||||
|
||||
def _step_errback(self, e):
|
||||
"callback for runner errors"
|
||||
cname = self.__class__.__name__
|
||||
estring = _("Script %(key)s(#%(dbid)s) of type '%(cname)s': at_repeat() error '%(err)s'.") % \
|
||||
{"key": self.key, "dbid": self.dbid, "cname": cname,
|
||||
"err": e.getErrorMessage()}
|
||||
try:
|
||||
self.db_obj.msg(estring)
|
||||
except Exception:
|
||||
pass
|
||||
logger.log_errmsg(estring)
|
||||
|
||||
def _step_callback(self):
|
||||
"step task runner. No try..except needed due to defer wrap."
|
||||
|
||||
if not self.is_valid():
|
||||
self.stop()
|
||||
return
|
||||
|
||||
# call hook
|
||||
self.at_repeat()
|
||||
|
||||
# check repeats
|
||||
callcount = self.ndb._task.callcount
|
||||
maxcount = self.db_repeats
|
||||
if maxcount > 0 and maxcount <= callcount:
|
||||
#print "stopping script!"
|
||||
self.stop()
|
||||
|
||||
def _step_task(self):
|
||||
"Step task. This groups error handling."
|
||||
try:
|
||||
return maybeDeferred(self._step_callback).addErrback(self._step_errback)
|
||||
except Exception:
|
||||
logger.log_trace()
|
||||
|
||||
# Public methods
|
||||
|
||||
def time_until_next_repeat(self):
|
||||
"""
|
||||
Returns the time in seconds until the script will be
|
||||
run again. If this is not a stepping script, returns None.
|
||||
This is not used in any way by the script's stepping
|
||||
system; it's only here for the user to be able to
|
||||
check in on their scripts and when they will next be run.
|
||||
"""
|
||||
task = self.ndb._task
|
||||
if task:
|
||||
try:
|
||||
return int(round(task.next_call_time()))
|
||||
except TypeError:
|
||||
pass
|
||||
return None
|
||||
|
||||
def remaining_repeats(self):
|
||||
"Get the number of returning repeats. Returns None if unlimited repeats."
|
||||
task = self.ndb._task
|
||||
if task:
|
||||
return max(0, self.db_repeats - task.callcount)
|
||||
|
||||
def start(self, force_restart=False):
|
||||
"""
|
||||
Called every time the script is started (for
|
||||
persistent scripts, this is usually once every server start)
|
||||
|
||||
force_restart - if True, will always restart the script, regardless
|
||||
of if it has started before.
|
||||
|
||||
returns 0 or 1 to indicated the script has been started or not.
|
||||
Used in counting.
|
||||
"""
|
||||
|
||||
if self.is_active and not force_restart:
|
||||
# script already runs and should not be restarted.
|
||||
return 0
|
||||
|
||||
obj = self.obj
|
||||
if obj:
|
||||
# check so the scripted object is valid and initalized
|
||||
try:
|
||||
obj.cmdset
|
||||
except AttributeError:
|
||||
# this means the object is not initialized.
|
||||
logger.log_trace()
|
||||
self.is_active = False
|
||||
return 0
|
||||
|
||||
# try to restart a paused script
|
||||
if self.unpause():
|
||||
return 1
|
||||
|
||||
# start the script from scratch
|
||||
self.is_active = True
|
||||
try:
|
||||
self.at_start()
|
||||
except Exception:
|
||||
logger.log_trace()
|
||||
|
||||
if self.db_interval > 0:
|
||||
self._start_task()
|
||||
return 1
|
||||
|
||||
def stop(self, kill=False):
|
||||
"""
|
||||
Called to stop the script from running.
|
||||
This also deletes the script.
|
||||
|
||||
kill - don't call finishing hooks.
|
||||
"""
|
||||
#print "stopping script %s" % self.key
|
||||
#import pdb
|
||||
#pdb.set_trace()
|
||||
if not kill:
|
||||
try:
|
||||
self.at_stop()
|
||||
except Exception:
|
||||
logger.log_trace()
|
||||
self._stop_task()
|
||||
try:
|
||||
self.delete()
|
||||
except AssertionError:
|
||||
logger.log_trace()
|
||||
return 0
|
||||
return 1
|
||||
|
||||
def pause(self):
|
||||
"""
|
||||
This stops a running script and stores its active state.
|
||||
It WILL NOT call that at_stop() hook.
|
||||
"""
|
||||
if not self.db._paused_time:
|
||||
# only allow pause if not already paused
|
||||
task = self.ndb._task
|
||||
if task:
|
||||
self.db._paused_time = task.next_call_time()
|
||||
self.db._paused_callcount = task.callcount
|
||||
self._stop_task()
|
||||
self.is_active = False
|
||||
|
||||
def unpause(self):
|
||||
"""
|
||||
Restart a paused script. This WILL call the at_start() hook.
|
||||
"""
|
||||
if self.db._paused_time:
|
||||
# only unpause if previously paused
|
||||
self.is_active = True
|
||||
|
||||
try:
|
||||
self.at_start()
|
||||
except Exception:
|
||||
logger.log_trace()
|
||||
|
||||
self._start_task()
|
||||
return True
|
||||
|
||||
def force_repeat(self):
|
||||
"""
|
||||
Fire a premature triggering of the script callback. This
|
||||
will reset the timer and count down repeats as if the script
|
||||
had fired normally.
|
||||
"""
|
||||
task = self.ndb._task
|
||||
if task:
|
||||
task.force_repeat()
|
||||
|
||||
# hooks
|
||||
def at_script_creation(self):
|
||||
"placeholder"
|
||||
pass
|
||||
|
||||
def is_valid(self):
|
||||
"placeholder"
|
||||
pass
|
||||
|
||||
def at_start(self):
|
||||
"placeholder."
|
||||
pass
|
||||
|
||||
def at_stop(self):
|
||||
"placeholder"
|
||||
pass
|
||||
|
||||
def at_repeat(self):
|
||||
"placeholder"
|
||||
pass
|
||||
|
||||
def at_init(self):
|
||||
"called when typeclass re-caches. Usually not used for scripts."
|
||||
pass
|
||||
|
||||
#
|
||||
# Base Script - inherit from this
|
||||
#
|
||||
|
||||
class Script(ScriptBase):
|
||||
"""
|
||||
This is the base TypeClass for all Scripts. Scripts describe events,
|
||||
timers and states in game, they can have a time component or describe
|
||||
a state that changes under certain conditions.
|
||||
|
||||
Script API:
|
||||
|
||||
* Available properties (only available on initiated Typeclass objects)
|
||||
|
||||
key (string) - name of object
|
||||
name (string)- same as key
|
||||
aliases (list of strings) - aliases to the object. Will be saved to
|
||||
database as AliasDB entries but returned as strings.
|
||||
dbref (int, read-only) - unique #id-number. Also "id" can be used.
|
||||
date_created (string) - time stamp of object creation
|
||||
permissions (list of strings) - list of permission strings
|
||||
|
||||
desc (string) - optional description of script, shown in listings
|
||||
obj (Object) - optional object that this script is connected to
|
||||
and acts on (set automatically
|
||||
by obj.scripts.add())
|
||||
interval (int) - how often script should run, in seconds.
|
||||
<=0 turns off ticker
|
||||
start_delay (bool) - if the script should start repeating right
|
||||
away or wait self.interval seconds
|
||||
repeats (int) - how many times the script should repeat before
|
||||
stopping. <=0 means infinite repeats
|
||||
persistent (bool) - if script should survive a server shutdown or not
|
||||
is_active (bool) - if script is currently running
|
||||
|
||||
* Handlers
|
||||
|
||||
locks - lock-handler: use locks.add() to add new lock strings
|
||||
db - attribute-handler: store/retrieve database attributes on this
|
||||
self.db.myattr=val, val=self.db.myattr
|
||||
ndb - non-persistent attribute handler: same as db but does not
|
||||
create a database entry when storing data
|
||||
|
||||
* Helper methods
|
||||
|
||||
start() - start script (this usually happens automatically at creation
|
||||
and obj.script.add() etc)
|
||||
stop() - stop script, and delete it
|
||||
pause() - put the script on hold, until unpause() is called. If script
|
||||
is persistent, the pause state will survive a shutdown.
|
||||
unpause() - restart a previously paused script. The script will
|
||||
continue as if it was never paused.
|
||||
force_repeat() - force-step the script, regardless of how much remains
|
||||
until next step. This counts like a normal firing in all ways.
|
||||
time_until_next_repeat() - if a timed script (interval>0), returns
|
||||
time until next tick
|
||||
remaining_repeats() - number of repeats remaining, if limited
|
||||
|
||||
* Hook methods
|
||||
|
||||
at_script_creation() - called only once, when an object of this
|
||||
class is first created.
|
||||
is_valid() - is called to check if the script is valid to be running
|
||||
at the current time. If is_valid() returns False, the
|
||||
running script is stopped and removed from the game. You
|
||||
can use this to check state changes (i.e. an script
|
||||
tracking some combat stats at regular intervals is only
|
||||
valid to run while there is actual combat going on).
|
||||
at_start() - Called every time the script is started, which for
|
||||
persistent scripts is at least once every server start.
|
||||
Note that this is unaffected by self.delay_start, which
|
||||
only delays the first call to at_repeat(). It will also
|
||||
be called after a pause, to allow for setting up the script.
|
||||
at_repeat() - Called every self.interval seconds. It will be called
|
||||
immediately upon launch unless self.delay_start is True,
|
||||
which will delay the first call of this method by
|
||||
self.interval seconds. If self.interval<=0, this method
|
||||
will never be called.
|
||||
at_stop() - Called as the script object is stopped and is about to
|
||||
be removed from the game, e.g. because is_valid()
|
||||
returned False or self.stop() was called manually.
|
||||
at_server_reload() - Called when server reloads. Can be used to save
|
||||
temporary variables you want should survive a reload.
|
||||
at_server_shutdown() - called at a full server shutdown.
|
||||
|
||||
"""
|
||||
def at_first_save(self):
|
||||
"""
|
||||
This is called after very first time this object is saved.
|
||||
Generally, you don't need to overload this, but only the hooks
|
||||
called by this method.
|
||||
"""
|
||||
self.at_script_creation()
|
||||
|
||||
if hasattr(self, "_createdict"):
|
||||
# this will only be set if the utils.create_script
|
||||
# function was used to create the object. We want
|
||||
# the create call's kwargs to override the values
|
||||
# set by hooks.
|
||||
cdict = self._createdict
|
||||
updates = []
|
||||
if not cdict.get("key"):
|
||||
if not self.db_key:
|
||||
self.db_key = "#%i" % self.dbid
|
||||
updates.append("db_key")
|
||||
elif self.db_key != cdict["key"]:
|
||||
self.db_key = cdict["key"]
|
||||
updates.append("db_key")
|
||||
if cdict.get("interval") and self.interval != cdict["interval"]:
|
||||
self.db_interval = cdict["interval"]
|
||||
updates.append("db_interval")
|
||||
if cdict.get("start_delay") and self.start_delay != cdict["start_delay"]:
|
||||
self.db_start_delay = cdict["start_delay"]
|
||||
updates.append("db_start_delay")
|
||||
if cdict.get("repeats") and self.repeats != cdict["repeats"]:
|
||||
self.db_repeats = cdict["repeats"]
|
||||
updates.append("db_repeats")
|
||||
if cdict.get("persistent") and self.persistent != cdict["persistent"]:
|
||||
self.db_persistent = cdict["persistent"]
|
||||
updates.append("db_persistent")
|
||||
if updates:
|
||||
self.save(update_fields=updates)
|
||||
if not cdict.get("autostart"):
|
||||
# don't auto-start the script
|
||||
return
|
||||
|
||||
# auto-start script (default)
|
||||
self.start()
|
||||
|
||||
|
||||
def at_script_creation(self):
|
||||
"""
|
||||
Only called once, by the create function.
|
||||
"""
|
||||
pass
|
||||
|
||||
def is_valid(self):
|
||||
"""
|
||||
Is called to check if the script is valid to run at this time.
|
||||
Should return a boolean. The method is assumed to collect all needed
|
||||
information from its related self.obj.
|
||||
"""
|
||||
return not self._is_deleted
|
||||
|
||||
def at_start(self):
|
||||
"""
|
||||
Called whenever the script is started, which for persistent
|
||||
scripts is at least once every server start. It will also be called
|
||||
when starting again after a pause (such as after a server reload)
|
||||
"""
|
||||
pass
|
||||
|
||||
def at_repeat(self):
|
||||
"""
|
||||
Called repeatedly if this Script is set to repeat
|
||||
regularly.
|
||||
"""
|
||||
pass
|
||||
|
||||
def at_stop(self):
|
||||
"""
|
||||
Called whenever when it's time for this script to stop
|
||||
(either because is_valid returned False or it runs out of iterations)
|
||||
"""
|
||||
pass
|
||||
|
||||
def at_server_reload(self):
|
||||
"""
|
||||
This hook is called whenever the server is shutting down for
|
||||
restart/reboot. If you want to, for example, save non-persistent
|
||||
properties across a restart, this is the place to do it.
|
||||
"""
|
||||
pass
|
||||
|
||||
def at_server_shutdown(self):
|
||||
"""
|
||||
This hook is called whenever the server is shutting down fully
|
||||
(i.e. not for a restart).
|
||||
"""
|
||||
pass
|
||||
|
||||
|
||||
# Some useful default Script types used by Evennia.
|
||||
|
||||
class DoNothing(Script):
|
||||
"An script that does nothing. Used as default fallback."
|
||||
def at_script_creation(self):
|
||||
"Setup the script"
|
||||
self.key = "sys_do_nothing"
|
||||
self.desc = _("This is an empty placeholder script.")
|
||||
|
||||
|
||||
class Store(Script):
|
||||
"Simple storage script"
|
||||
def at_script_creation(self):
|
||||
"Setup the script"
|
||||
self.key = "sys_storage"
|
||||
self.desc = _("This is a generic storage container.")
|
||||
|
||||
|
||||
class CheckSessions(Script):
|
||||
"Check sessions regularly."
|
||||
def at_script_creation(self):
|
||||
"Setup the script"
|
||||
self.key = "sys_session_check"
|
||||
self.desc = _("Checks sessions so they are live.")
|
||||
self.interval = 60 # repeat every 60 seconds
|
||||
self.persistent = True
|
||||
|
||||
def at_repeat(self):
|
||||
"called every 60 seconds"
|
||||
global _SESSIONS
|
||||
if not _SESSIONS:
|
||||
from src.server.sessionhandler import SESSIONS as _SESSIONS
|
||||
#print "session check!"
|
||||
#print "ValidateSessions run"
|
||||
_SESSIONS.validate_sessions()
|
||||
|
||||
_FLUSH_CACHE = None
|
||||
_IDMAPPER_CACHE_MAX_MEMORY = settings.IDMAPPER_CACHE_MAXSIZE
|
||||
class ValidateIdmapperCache(Script):
|
||||
"""
|
||||
Check memory use of idmapper cache
|
||||
"""
|
||||
def at_script_creation(self):
|
||||
self.key = "sys_cache_validate"
|
||||
self.desc = _("Restrains size of idmapper cache.")
|
||||
self.interval = 61 * 5 # staggered compared to session check
|
||||
self.persistent = True
|
||||
|
||||
def at_repeat(self):
|
||||
"Called every ~5 mins"
|
||||
global _FLUSH_CACHE
|
||||
if not _FLUSH_CACHE:
|
||||
from src.utils.idmapper.base import conditional_flush as _FLUSH_CACHE
|
||||
_FLUSH_CACHE(_IDMAPPER_CACHE_MAX_MEMORY)
|
||||
|
||||
class ValidateScripts(Script):
|
||||
"Check script validation regularly"
|
||||
def at_script_creation(self):
|
||||
"Setup the script"
|
||||
self.key = "sys_scripts_validate"
|
||||
self.desc = _("Validates all scripts regularly.")
|
||||
self.interval = 3600 # validate every hour.
|
||||
self.persistent = True
|
||||
|
||||
def at_repeat(self):
|
||||
"called every hour"
|
||||
#print "ValidateScripts run."
|
||||
ScriptDB.objects.validate()
|
||||
|
||||
|
||||
class ValidateChannelHandler(Script):
|
||||
"Update the channelhandler to make sure it's in sync."
|
||||
def at_script_creation(self):
|
||||
"Setup the script"
|
||||
self.key = "sys_channels_validate"
|
||||
self.desc = _("Updates the channel handler")
|
||||
self.interval = 3700 # validate a little later than ValidateScripts
|
||||
self.persistent = True
|
||||
|
||||
def at_repeat(self):
|
||||
"called every hour+"
|
||||
#print "ValidateChannelHandler run."
|
||||
channelhandler.CHANNELHANDLER.update()
|
||||
|
||||
329
lib/scripts/tickerhandler.py
Normal file
329
lib/scripts/tickerhandler.py
Normal file
|
|
@ -0,0 +1,329 @@
|
|||
"""
|
||||
TickerHandler
|
||||
|
||||
This implements an efficient Ticker which uses a subscription
|
||||
model to 'tick' subscribed objects at regular intervals.
|
||||
|
||||
The ticker mechanism is used by importing and accessing
|
||||
the instantiated TICKER_HANDLER instance in this module. This
|
||||
instance is run by the server; it will save its status across
|
||||
server reloads and be started automaticall on boot.
|
||||
|
||||
Example:
|
||||
|
||||
from src.scripts.tickerhandler import TICKER_HANDLER
|
||||
|
||||
# tick myobj every 15 seconds
|
||||
TICKER_HANDLER.add(myobj, 15)
|
||||
|
||||
The handler will by default try to call a hook "at_tick()"
|
||||
on the subscribing object. The hook's name can be changed
|
||||
if the "hook_key" keyword is given to the add() method (only
|
||||
one such alternate name per interval though). The
|
||||
handler will transparently set up and add new timers behind
|
||||
the scenes to tick at given intervals, using a TickerPool.
|
||||
|
||||
To remove:
|
||||
|
||||
TICKER_HANDLER.remove(myobj, 15)
|
||||
|
||||
The interval must be given since a single object can be subcribed
|
||||
to many different tickers at the same time.
|
||||
|
||||
|
||||
The TickerHandler's functionality can be overloaded by modifying the
|
||||
Ticker class and then changing TickerPool and TickerHandler to use the
|
||||
custom classes
|
||||
|
||||
class MyTicker(Ticker):
|
||||
# [doing custom stuff]
|
||||
|
||||
class MyTickerPool(TickerPool):
|
||||
ticker_class = MyTicker
|
||||
class MyTickerHandler(TickerHandler):
|
||||
ticker_pool_class = MyTickerPool
|
||||
|
||||
If one wants to duplicate TICKER_HANDLER's auto-saving feature in
|
||||
a custom handler one can make a custom AT_STARTSTOP_MODULE entry to
|
||||
call the handler's save() and restore() methods when the server reboots.
|
||||
|
||||
"""
|
||||
from twisted.internet.defer import inlineCallbacks
|
||||
from src.scripts.scripts import ExtendedLoopingCall
|
||||
from src.server.models import ServerConfig
|
||||
from src.utils.logger import log_trace
|
||||
from src.utils.dbserialize import dbserialize, dbunserialize, pack_dbobj, unpack_dbobj
|
||||
|
||||
_GA = object.__getattribute__
|
||||
_SA = object.__setattr__
|
||||
|
||||
|
||||
class Ticker(object):
|
||||
"""
|
||||
Represents a repeatedly running task that calls
|
||||
hooks repeatedly. Overload _callback to change the
|
||||
way it operates.
|
||||
"""
|
||||
|
||||
@inlineCallbacks
|
||||
def _callback(self):
|
||||
"""
|
||||
This will be called repeatedly every self.interval seconds.
|
||||
self.subscriptions contain tuples of (obj, args, kwargs) for
|
||||
each subscribing object.
|
||||
|
||||
If overloading, this callback is expected to handle all
|
||||
subscriptions when it is triggered. It should not return
|
||||
anything and should not traceback on poorly designed hooks.
|
||||
The callback should ideally work under @inlineCallbacks so it can yield
|
||||
appropriately.
|
||||
"""
|
||||
for key, (obj, args, kwargs) in self.subscriptions.items():
|
||||
hook_key = yield kwargs.get("hook_key", "at_tick")
|
||||
if not obj:
|
||||
# object was deleted between calls
|
||||
self.validate()
|
||||
continue
|
||||
try:
|
||||
yield _GA(obj, hook_key)(*args, **kwargs)
|
||||
except Exception:
|
||||
log_trace()
|
||||
|
||||
def __init__(self, interval):
|
||||
"""
|
||||
Set up the ticker
|
||||
"""
|
||||
self.interval = interval
|
||||
self.subscriptions = {}
|
||||
# set up a twisted asynchronous repeat call
|
||||
self.task = ExtendedLoopingCall(self._callback)
|
||||
|
||||
def validate(self, start_delay=None):
|
||||
"""
|
||||
Start/stop the task depending on how many
|
||||
subscribers we have using it.
|
||||
"""
|
||||
subs = self.subscriptions
|
||||
if None in subs.values():
|
||||
# clean out objects that may have been deleted
|
||||
subs = dict((store_key, obj) for store_key, obj in subs if obj)
|
||||
self.subscriptions = subs
|
||||
if self.task.running:
|
||||
if not subs:
|
||||
self.task.stop()
|
||||
elif subs:
|
||||
#print "starting with start_delay=", start_delay
|
||||
self.task.start(self.interval, now=False, start_delay=start_delay)
|
||||
|
||||
def add(self, store_key, obj, *args, **kwargs):
|
||||
"""
|
||||
Sign up a subscriber to this ticker. If kwargs contains
|
||||
a keyword _start_delay, this will be used to delay the start
|
||||
of the trigger instead of interval.
|
||||
"""
|
||||
start_delay = kwargs.pop("_start_delay", None)
|
||||
self.subscriptions[store_key] = (obj, args, kwargs)
|
||||
self.validate(start_delay=start_delay)
|
||||
|
||||
def remove(self, store_key):
|
||||
"""
|
||||
Unsubscribe object from this ticker
|
||||
"""
|
||||
self.subscriptions.pop(store_key, False)
|
||||
self.validate()
|
||||
|
||||
def stop(self):
|
||||
"""
|
||||
Kill the Task, regardless of subscriptions
|
||||
"""
|
||||
self.subscriptions = {}
|
||||
self.validate()
|
||||
|
||||
|
||||
class TickerPool(object):
|
||||
"""
|
||||
This maintains a pool of Twisted LoopingCall tasks
|
||||
for calling subscribed objects at given times.
|
||||
"""
|
||||
ticker_class = Ticker
|
||||
|
||||
def __init__(self):
|
||||
"Initialize the pool"
|
||||
self.tickers = {}
|
||||
|
||||
def add(self, store_key, obj, interval, *args, **kwargs):
|
||||
"""
|
||||
Add new ticker subscriber
|
||||
"""
|
||||
if interval not in self.tickers:
|
||||
self.tickers[interval] = self.ticker_class(interval)
|
||||
self.tickers[interval].add(store_key, obj, *args, **kwargs)
|
||||
|
||||
def remove(self, store_key, interval):
|
||||
"""
|
||||
Remove subscription from pool
|
||||
"""
|
||||
if interval in self.tickers:
|
||||
self.tickers[interval].remove(store_key)
|
||||
|
||||
def stop(self, interval=None):
|
||||
"""
|
||||
Stop all scripts in pool. This is done at server reload since
|
||||
restoring the pool will automatically re-populate the pool.
|
||||
If interval is given, only stop tickers with that interval.
|
||||
"""
|
||||
if interval and interval in self.tickers:
|
||||
self.tickers[interval].stop()
|
||||
else:
|
||||
for ticker in self.tickers.values():
|
||||
ticker.stop()
|
||||
|
||||
|
||||
class TickerHandler(object):
|
||||
"""
|
||||
The Tickerhandler maintains a pool of tasks for subscribing
|
||||
objects to various tick rates. The pool maintains creation
|
||||
instructions and and re-applies them at a server restart.
|
||||
"""
|
||||
ticker_pool_class = TickerPool
|
||||
|
||||
def __init__(self, save_name="ticker_storage"):
|
||||
"""
|
||||
Initialize handler
|
||||
"""
|
||||
self.ticker_storage = {}
|
||||
self.save_name = save_name
|
||||
self.ticker_pool = self.ticker_pool_class()
|
||||
|
||||
def _store_key(self, obj, interval, idstring=""):
|
||||
"""
|
||||
Tries to create a store_key for the object.
|
||||
Returns a tuple (isdb, store_key) where isdb
|
||||
is a boolean True if obj was a database object,
|
||||
False otherwise.
|
||||
"""
|
||||
if hasattr(obj, "db_key"):
|
||||
# create a store_key using the database representation
|
||||
objkey = pack_dbobj(obj)
|
||||
isdb = True
|
||||
else:
|
||||
# non-db object, look for a property "key" on it, otherwise
|
||||
# use its memory location.
|
||||
try:
|
||||
objkey = _GA(obj, "key")
|
||||
except AttributeError:
|
||||
objkey = id(obj)
|
||||
isdb = False
|
||||
# return sidb and store_key
|
||||
return isdb, (objkey, interval, idstring)
|
||||
|
||||
def save(self):
|
||||
"""
|
||||
Save ticker_storage as a serialized string into a temporary
|
||||
ServerConf field. Whereas saving is done on the fly, if called by
|
||||
server when it shuts down, the current timer of each ticker will be
|
||||
saved so it can start over from that point.
|
||||
"""
|
||||
if self.ticker_storage:
|
||||
start_delays = dict((interval, ticker.task.next_call_time())
|
||||
for interval, ticker in self.ticker_pool.tickers.items())
|
||||
# update the timers for the tickers
|
||||
#for (obj, interval, idstring), (args, kwargs) in self.ticker_storage.items():
|
||||
for store_key, (args, kwargs) in self.ticker_storage.items():
|
||||
interval = store_key[1]
|
||||
# this is a mutable, so it's updated in-place in ticker_storage
|
||||
kwargs["_start_delay"] = start_delays.get(interval, None)
|
||||
ServerConfig.objects.conf(key=self.save_name,
|
||||
value=dbserialize(self.ticker_storage))
|
||||
else:
|
||||
ServerConfig.objects.conf(key=self.save_name, delete=True)
|
||||
|
||||
def restore(self):
|
||||
"""
|
||||
Restore ticker_storage from database and re-initialize the handler from storage. This is triggered by the server at restart.
|
||||
"""
|
||||
# load stored command instructions and use them to re-initialize handler
|
||||
ticker_storage = ServerConfig.objects.conf(key=self.save_name)
|
||||
if ticker_storage:
|
||||
self.ticker_storage = dbunserialize(ticker_storage)
|
||||
#print "restore:", self.ticker_storage
|
||||
for store_key, (args, kwargs) in self.ticker_storage.items():
|
||||
if len(store_key) == 2:
|
||||
# old form of store_key - update it
|
||||
store_key = (store_key[0], store_key[1], "")
|
||||
obj, interval, idstring = store_key
|
||||
obj = unpack_dbobj(obj)
|
||||
_, store_key = self._store_key(obj, interval, idstring)
|
||||
self.ticker_pool.add(store_key, obj, interval, *args, **kwargs)
|
||||
|
||||
def add(self, obj, interval, idstring="", *args, **kwargs):
|
||||
"""
|
||||
Add object to tickerhandler. The object must have an at_tick
|
||||
method. This will be called every interval seconds until the
|
||||
object is unsubscribed from the ticker.
|
||||
"""
|
||||
isdb, store_key = self._store_key(obj, interval, idstring)
|
||||
if isdb:
|
||||
self.ticker_storage[store_key] = (args, kwargs)
|
||||
self.save()
|
||||
self.ticker_pool.add(store_key, obj, interval, *args, **kwargs)
|
||||
|
||||
def remove(self, obj, interval=None, idstring=""):
|
||||
"""
|
||||
Remove object from ticker, or only this object ticking
|
||||
at a given interval.
|
||||
"""
|
||||
if interval:
|
||||
isdb, store_key = self._store_key(obj, interval, idstring)
|
||||
if isdb:
|
||||
self.ticker_storage.pop(store_key, None)
|
||||
self.save()
|
||||
self.ticker_pool.remove(store_key, interval)
|
||||
else:
|
||||
# remove all objects with any intervals
|
||||
intervals = self.ticker_pool.tickers.keys()
|
||||
should_save = False
|
||||
for interval in intervals:
|
||||
isdb, store_key = self._store_key(obj, interval, idstring)
|
||||
if isdb:
|
||||
self.ticker_storage.pop(store_key, None)
|
||||
should_save = True
|
||||
self.ticker_pool.remove(store_key, interval)
|
||||
if should_save:
|
||||
self.save()
|
||||
|
||||
|
||||
|
||||
def clear(self, interval=None):
|
||||
"""
|
||||
Stop/remove all tickers from handler, or the ones
|
||||
with a given interval. This is the only supported
|
||||
way to kill tickers for non-db objects. If interval
|
||||
is given, only stop tickers with this interval.
|
||||
"""
|
||||
self.ticker_pool.stop(interval)
|
||||
if interval:
|
||||
self.ticker_storage = dict((store_key, store_key) for store_key in self.ticker_storage if store_key[1] != interval)
|
||||
else:
|
||||
self.ticker_storage = {}
|
||||
self.save()
|
||||
|
||||
def all(self, interval=None):
|
||||
"""
|
||||
Get the subsciptions for a given interval. If interval
|
||||
is not given, return a dictionary with lists for every
|
||||
interval in the tickerhandler.
|
||||
"""
|
||||
if interval is None:
|
||||
# return dict of all, ordered by interval
|
||||
return dict((interval, ticker.subscriptions.values())
|
||||
for interval, ticker in self.ticker_pool.tickers.items())
|
||||
else:
|
||||
# get individual interval
|
||||
ticker = self.ticker_pool.tickers.get(interval, None)
|
||||
if ticker:
|
||||
return ticker.subscriptions.values()
|
||||
|
||||
|
||||
# main tickerhandler
|
||||
TICKER_HANDLER = TickerHandler()
|
||||
10
lib/server/__init__.py
Normal file
10
lib/server/__init__.py
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
"""
|
||||
Makes it easier to import by grouping all relevant things already at this level.
|
||||
|
||||
You can henceforth import most things directly from src.server
|
||||
Also, the initiated object manager is available as src.server.manager.
|
||||
|
||||
"""
|
||||
|
||||
from src.server.models import *
|
||||
manager = ServerConfig.objects
|
||||
19
lib/server/admin.py
Normal file
19
lib/server/admin.py
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
#
|
||||
# This sets up how models are displayed
|
||||
# in the web admin interface.
|
||||
#
|
||||
|
||||
from django.contrib import admin
|
||||
from src.server.models import ServerConfig
|
||||
|
||||
|
||||
class ServerConfigAdmin(admin.ModelAdmin):
|
||||
"Custom admin for server configs"
|
||||
list_display = ('db_key', 'db_value')
|
||||
list_display_links = ('db_key',)
|
||||
ordering = ['db_key', 'db_value']
|
||||
search_fields = ['db_key']
|
||||
save_as = True
|
||||
save_on_top = True
|
||||
list_select_related = True
|
||||
admin.site.register(ServerConfig, ServerConfigAdmin)
|
||||
535
lib/server/amp.py
Normal file
535
lib/server/amp.py
Normal file
|
|
@ -0,0 +1,535 @@
|
|||
"""
|
||||
Contains the protocols, commands, and client factory needed for the Server
|
||||
and Portal to communicate with each other, letting Portal work as a proxy.
|
||||
Both sides use this same protocol.
|
||||
|
||||
The separation works like this:
|
||||
|
||||
Portal - (AMP client) handles protocols. It contains a list of connected
|
||||
sessions in a dictionary for identifying the respective player
|
||||
connected. If it looses the AMP connection it will automatically
|
||||
try to reconnect.
|
||||
|
||||
Server - (AMP server) Handles all mud operations. The server holds its own list
|
||||
of sessions tied to player objects. This is synced against the portal
|
||||
at startup and when a session connects/disconnects
|
||||
|
||||
"""
|
||||
|
||||
# imports needed on both server and portal side
|
||||
import os
|
||||
from collections import defaultdict
|
||||
try:
|
||||
import cPickle as pickle
|
||||
except ImportError:
|
||||
import pickle
|
||||
from twisted.protocols import amp
|
||||
from twisted.internet import protocol
|
||||
from twisted.internet.defer import Deferred
|
||||
from src.utils.utils import to_str, variable_from_module
|
||||
|
||||
# communication bits
|
||||
|
||||
PCONN = chr(1) # portal session connect
|
||||
PDISCONN = chr(2) # portal session disconnect
|
||||
PSYNC = chr(3) # portal session sync
|
||||
SLOGIN = chr(4) # server session login
|
||||
SDISCONN = chr(5) # server session disconnect
|
||||
SDISCONNALL = chr(6) # server session disconnect all
|
||||
SSHUTD = chr(7) # server shutdown
|
||||
SSYNC = chr(8) # server session sync
|
||||
SCONN = chr(9) # server creating new connectiong (for irc/imc2 bots etc)
|
||||
PCONNSYNC = chr(10) # portal post-syncing a session
|
||||
|
||||
MAXLEN = 65535 # max allowed data length in AMP protocol
|
||||
_MSGBUFFER = defaultdict(list)
|
||||
|
||||
def get_restart_mode(restart_file):
|
||||
"""
|
||||
Parse the server/portal restart status
|
||||
"""
|
||||
if os.path.exists(restart_file):
|
||||
flag = open(restart_file, 'r').read()
|
||||
return flag == "True"
|
||||
return False
|
||||
|
||||
|
||||
class AmpServerFactory(protocol.ServerFactory):
|
||||
"""
|
||||
This factory creates the Server as a new AMPProtocol instance for accepting
|
||||
connections from the Portal.
|
||||
"""
|
||||
def __init__(self, server):
|
||||
"""
|
||||
server: The Evennia server service instance
|
||||
protocol: The protocol the factory creates instances of.
|
||||
"""
|
||||
self.server = server
|
||||
self.protocol = AMPProtocol
|
||||
|
||||
def buildProtocol(self, addr):
|
||||
"""
|
||||
Start a new connection, and store it on the service object
|
||||
"""
|
||||
#print "Evennia Server connected to Portal at %s." % addr
|
||||
self.server.amp_protocol = AMPProtocol()
|
||||
self.server.amp_protocol.factory = self
|
||||
return self.server.amp_protocol
|
||||
|
||||
|
||||
class AmpClientFactory(protocol.ReconnectingClientFactory):
|
||||
"""
|
||||
This factory creates an instance of the Portal, an AMPProtocol
|
||||
instances to use to connect
|
||||
"""
|
||||
# Initial reconnect delay in seconds.
|
||||
initialDelay = 1
|
||||
factor = 1.5
|
||||
maxDelay = 1
|
||||
|
||||
def __init__(self, portal):
|
||||
self.portal = portal
|
||||
self.protocol = AMPProtocol
|
||||
|
||||
def startedConnecting(self, connector):
|
||||
"""
|
||||
Called when starting to try to connect to the MUD server.
|
||||
"""
|
||||
pass
|
||||
#print 'AMP started to connect:', connector
|
||||
|
||||
def buildProtocol(self, addr):
|
||||
"""
|
||||
Creates an AMPProtocol instance when connecting to the server.
|
||||
"""
|
||||
#print "Portal connected to Evennia server at %s." % addr
|
||||
self.resetDelay()
|
||||
self.portal.amp_protocol = AMPProtocol()
|
||||
self.portal.amp_protocol.factory = self
|
||||
return self.portal.amp_protocol
|
||||
|
||||
def clientConnectionLost(self, connector, reason):
|
||||
"""
|
||||
Called when the AMP connection to the MUD server is lost.
|
||||
"""
|
||||
if hasattr(self, "server_restart_mode"):
|
||||
self.maxDelay = 1
|
||||
else:
|
||||
# Don't translate this; avoid loading django on portal side.
|
||||
self.maxDelay = 10
|
||||
self.portal.sessions.announce_all(" ... Portal lost connection to Server.")
|
||||
protocol.ReconnectingClientFactory.clientConnectionLost(self, connector, reason)
|
||||
|
||||
def clientConnectionFailed(self, connector, reason):
|
||||
"""
|
||||
Called when an AMP connection attempt to the MUD server fails.
|
||||
"""
|
||||
if hasattr(self, "server_restart_mode"):
|
||||
self.maxDelay = 1
|
||||
else:
|
||||
self.maxDelay = 10
|
||||
self.portal.sessions.announce_all(" ...")
|
||||
protocol.ReconnectingClientFactory.clientConnectionFailed(self, connector, reason)
|
||||
|
||||
|
||||
# AMP Communication Command types
|
||||
|
||||
class MsgPortal2Server(amp.Command):
|
||||
"""
|
||||
Message portal -> server
|
||||
"""
|
||||
key = "MsgPortal2Server"
|
||||
arguments = [('sessid', amp.Integer()),
|
||||
('ipart', amp.Integer()),
|
||||
('nparts', amp.Integer()),
|
||||
('msg', amp.String()),
|
||||
('data', amp.String())]
|
||||
errors = [(Exception, 'EXCEPTION')]
|
||||
response = []
|
||||
|
||||
|
||||
class MsgServer2Portal(amp.Command):
|
||||
"""
|
||||
Message server -> portal
|
||||
"""
|
||||
key = "MsgServer2Portal"
|
||||
arguments = [('sessid', amp.Integer()),
|
||||
('ipart', amp.Integer()),
|
||||
('nparts', amp.Integer()),
|
||||
('msg', amp.String()),
|
||||
('data', amp.String())]
|
||||
errors = [(Exception, 'EXCEPTION')]
|
||||
response = []
|
||||
|
||||
|
||||
class ServerAdmin(amp.Command):
|
||||
"""
|
||||
Portal -> Server
|
||||
|
||||
Sent when the portal needs to perform admin
|
||||
operations on the server, such as when a new
|
||||
session connects or resyncs
|
||||
"""
|
||||
key = "ServerAdmin"
|
||||
arguments = [('sessid', amp.Integer()),
|
||||
('ipart', amp.Integer()),
|
||||
('nparts', amp.Integer()),
|
||||
('operation', amp.String()),
|
||||
('data', amp.String())]
|
||||
errors = [(Exception, 'EXCEPTION')]
|
||||
response = []
|
||||
|
||||
|
||||
class PortalAdmin(amp.Command):
|
||||
"""
|
||||
Server -> Portal
|
||||
|
||||
Sent when the server needs to perform admin
|
||||
operations on the portal.
|
||||
"""
|
||||
key = "PortalAdmin"
|
||||
arguments = [('sessid', amp.Integer()),
|
||||
('ipart', amp.Integer()),
|
||||
('nparts', amp.Integer()),
|
||||
('operation', amp.String()),
|
||||
('data', amp.String())]
|
||||
errors = [(Exception, 'EXCEPTION')]
|
||||
response = []
|
||||
|
||||
|
||||
class FunctionCall(amp.Command):
|
||||
"""
|
||||
Bidirectional
|
||||
|
||||
Sent when either process needs to call an
|
||||
arbitrary function in the other.
|
||||
"""
|
||||
key = "FunctionCall"
|
||||
arguments = [('module', amp.String()),
|
||||
('function', amp.String()),
|
||||
('args', amp.String()),
|
||||
('kwargs', amp.String())]
|
||||
errors = [(Exception, 'EXCEPTION')]
|
||||
response = [('result', amp.String())]
|
||||
|
||||
|
||||
# Helper functions
|
||||
|
||||
dumps = lambda data: to_str(pickle.dumps(data, pickle.HIGHEST_PROTOCOL))
|
||||
loads = lambda data: pickle.loads(to_str(data))
|
||||
|
||||
# multipart message store
|
||||
|
||||
|
||||
|
||||
#------------------------------------------------------------
|
||||
# Core AMP protocol for communication Server <-> Portal
|
||||
#------------------------------------------------------------
|
||||
|
||||
class AMPProtocol(amp.AMP):
|
||||
"""
|
||||
This is the protocol that the MUD server and the proxy server
|
||||
communicate to each other with. AMP is a bi-directional protocol, so
|
||||
both the proxy and the MUD use the same commands and protocol.
|
||||
|
||||
AMP specifies responder methods here and connect them to amp.Command
|
||||
subclasses that specify the datatypes of the input/output of these methods.
|
||||
"""
|
||||
|
||||
# helper methods
|
||||
|
||||
def connectionMade(self):
|
||||
"""
|
||||
This is called when a connection is established
|
||||
between server and portal. AMP calls it on both sides,
|
||||
so we need to make sure to only trigger resync from the
|
||||
portal side.
|
||||
"""
|
||||
if hasattr(self.factory, "portal"):
|
||||
# only the portal has the 'portal' property, so we know we are
|
||||
# on the portal side and can initialize the connection.
|
||||
sessdata = self.factory.portal.sessions.get_all_sync_data()
|
||||
self.call_remote_ServerAdmin(0,
|
||||
PSYNC,
|
||||
data=sessdata)
|
||||
self.factory.portal.sessions.at_server_connection()
|
||||
if hasattr(self.factory, "server_restart_mode"):
|
||||
del self.factory.server_restart_mode
|
||||
|
||||
# Error handling
|
||||
|
||||
def errback(self, e, info):
|
||||
"error handler, to avoid dropping connections on server tracebacks."
|
||||
f = e.trap(Exception)
|
||||
print "AMP Error for %(info)s: %(e)s" % {'info': info,
|
||||
'e': e.getErrorMessage()}
|
||||
|
||||
def safe_send(self, command, sessid, **kwargs):
|
||||
"""
|
||||
This helper method splits the sending of a message into
|
||||
multiple parts with a maxlength of MAXLEN. This is to avoid
|
||||
repetition in two sending commands. when calling this the
|
||||
maximum length has already been exceeded. The max-length will
|
||||
be checked for all kwargs and these will be used as argument
|
||||
to the command. The command type must have keywords ipart and
|
||||
nparts to track the parts and put them back together on the
|
||||
other side.
|
||||
|
||||
Returns a deferred or a list of such
|
||||
"""
|
||||
to_send = [(key, [string[i:i+MAXLEN] for i in range(0, len(string), MAXLEN)])
|
||||
for key, string in kwargs.items()]
|
||||
nparts_max = max(len(part[1]) for part in to_send)
|
||||
if nparts_max == 1:
|
||||
# first try to send directly
|
||||
return self.callRemote(command,
|
||||
sessid=sessid,
|
||||
ipart=0,
|
||||
nparts=1,
|
||||
**kwargs).addErrback(self.errback, command.key)
|
||||
else:
|
||||
# one or more parts were too long for MAXLEN.
|
||||
#print "TooLong triggered!"
|
||||
deferreds = []
|
||||
for ipart in range(nparts_max):
|
||||
part_kwargs = {}
|
||||
for key, str_part in to_send:
|
||||
try:
|
||||
part_kwargs[key] = str_part[ipart]
|
||||
except IndexError:
|
||||
# means this kwarg needed fewer splits
|
||||
part_kwargs[key] = ""
|
||||
# send this part
|
||||
#print "amp safe sending:", ipart, nparts_max, str_part
|
||||
deferreds.append(self.callRemote(
|
||||
command,
|
||||
sessid=sessid,
|
||||
ipart=ipart,
|
||||
nparts=nparts_max,
|
||||
**part_kwargs).addErrback(self.errback, command.key))
|
||||
return deferreds
|
||||
|
||||
def safe_recv(self, command, sessid, ipart, nparts, **kwargs):
|
||||
"""
|
||||
Safely decode potentially split data coming over the wire. No
|
||||
decoding or parsing is done here, only merging of data split
|
||||
with safe_send().
|
||||
If the data stream is not yet complete, this method will return
|
||||
None, otherwise it will return a dictionary of the (possibly
|
||||
merged) properties.
|
||||
"""
|
||||
global _MSGBUFFER
|
||||
if nparts == 1:
|
||||
# the most common case
|
||||
return kwargs
|
||||
else:
|
||||
# part of a multi-part send
|
||||
hashid = "%s_%s" % (command.key, sessid)
|
||||
#print "amp safe receive:", ipart, nparts-1, kwargs
|
||||
if ipart < nparts-1:
|
||||
# not yet complete
|
||||
_MSGBUFFER[hashid].append(kwargs)
|
||||
return
|
||||
else:
|
||||
# all parts in place, put them back together
|
||||
buf = _MSGBUFFER.pop(hashid) + [kwargs]
|
||||
recv_kwargs = dict((key, "".join(kw[key] for kw in buf)) for key in kwargs)
|
||||
return recv_kwargs
|
||||
|
||||
# Message definition + helper methods to call/create each message type
|
||||
|
||||
# Portal -> Server Msg
|
||||
|
||||
def amp_msg_portal2server(self, sessid, ipart, nparts, msg, data):
|
||||
"""
|
||||
Relays message to server. This method is executed on the Server.
|
||||
|
||||
Since AMP has a limit of 65355 bytes per message, it's possible the
|
||||
data comes in multiple chunks; if so (nparts>1) we buffer the data
|
||||
and wait for the remaining parts to arrive before continuing.
|
||||
"""
|
||||
#print "msg portal -> server (server side):", sessid, msg, data
|
||||
ret = self.safe_recv(MsgPortal2Server, sessid, ipart, nparts,
|
||||
text=msg, data=data)
|
||||
if ret is not None:
|
||||
self.factory.server.sessions.data_in(sessid,
|
||||
text=ret["text"],
|
||||
**loads(ret["data"]))
|
||||
return {}
|
||||
MsgPortal2Server.responder(amp_msg_portal2server)
|
||||
|
||||
def call_remote_MsgPortal2Server(self, sessid, msg, data=""):
|
||||
"""
|
||||
Access method called by the Portal and executed on the Portal.
|
||||
"""
|
||||
#print "msg portal->server (portal side):", sessid, msg, data
|
||||
return self.safe_send(MsgPortal2Server, sessid,
|
||||
msg=msg if msg is not None else "",
|
||||
data=dumps(data))
|
||||
|
||||
# Server -> Portal message
|
||||
|
||||
def amp_msg_server2portal(self, sessid, ipart, nparts, msg, data):
|
||||
"""
|
||||
Relays message to Portal. This method is executed on the Portal.
|
||||
"""
|
||||
#print "msg server->portal (portal side):", sessid, msg
|
||||
ret = self.safe_recv(MsgServer2Portal, sessid,
|
||||
ipart, nparts, text=msg, data=data)
|
||||
if ret is not None:
|
||||
self.factory.portal.sessions.data_out(sessid,
|
||||
text=ret["text"],
|
||||
**loads(ret["data"]))
|
||||
return {}
|
||||
MsgServer2Portal.responder(amp_msg_server2portal)
|
||||
|
||||
def call_remote_MsgServer2Portal(self, sessid, msg, data=""):
|
||||
"""
|
||||
Access method called by the Server and executed on the Server.
|
||||
"""
|
||||
#print "msg server->portal (server side):", sessid, msg, data
|
||||
return self.safe_send(MsgServer2Portal, sessid,
|
||||
msg=msg if msg is not None else "",
|
||||
data=dumps(data))
|
||||
|
||||
# Server administration from the Portal side
|
||||
def amp_server_admin(self, sessid, ipart, nparts, operation, data):
|
||||
"""
|
||||
This allows the portal to perform admin
|
||||
operations on the server. This is executed on the Server.
|
||||
|
||||
"""
|
||||
ret = self.safe_recv(ServerAdmin, sessid, ipart, nparts,
|
||||
operation=operation, data=data)
|
||||
|
||||
if ret is not None:
|
||||
data = loads(ret["data"])
|
||||
operation = ret["operation"]
|
||||
server_sessionhandler = self.factory.server.sessions
|
||||
|
||||
#print "serveradmin (server side):", sessid, ord(operation), data
|
||||
|
||||
if operation == PCONN: # portal_session_connect
|
||||
# create a new session and sync it
|
||||
server_sessionhandler.portal_connect(data)
|
||||
|
||||
elif operation == PCONNSYNC: #portal_session_sync
|
||||
server_sessionhandler.portal_session_sync(data)
|
||||
|
||||
elif operation == PDISCONN: # portal_session_disconnect
|
||||
# session closed from portal side
|
||||
self.factory.server.sessions.portal_disconnect(sessid)
|
||||
|
||||
elif operation == PSYNC: # portal_session_sync
|
||||
# force a resync of sessions when portal reconnects to
|
||||
# server (e.g. after a server reboot) the data kwarg
|
||||
# contains a dict {sessid: {arg1:val1,...}}
|
||||
# representing the attributes to sync for each
|
||||
# session.
|
||||
server_sessionhandler.portal_sessions_sync(data)
|
||||
else:
|
||||
raise Exception("operation %(op)s not recognized." % {'op': operation})
|
||||
return {}
|
||||
ServerAdmin.responder(amp_server_admin)
|
||||
|
||||
def call_remote_ServerAdmin(self, sessid, operation="", data=""):
|
||||
"""
|
||||
Access method called by the Portal and Executed on the Portal.
|
||||
"""
|
||||
#print "serveradmin (portal side):", sessid, ord(operation), data
|
||||
data = dumps(data)
|
||||
return self.safe_send(ServerAdmin, sessid, operation=operation, data=data)
|
||||
|
||||
# Portal administraton from the Server side
|
||||
|
||||
def amp_portal_admin(self, sessid, ipart, nparts, operation, data):
|
||||
"""
|
||||
This allows the server to perform admin
|
||||
operations on the portal. This is executed on the Portal.
|
||||
"""
|
||||
#print "portaladmin (portal side):", sessid, ord(operation), data
|
||||
ret = self.safe_recv(PortalAdmin, sessid, ipart, nparts,
|
||||
operation=operation, data=data)
|
||||
if ret is not None:
|
||||
data = loads(data)
|
||||
portal_sessionhandler = self.factory.portal.sessions
|
||||
|
||||
if operation == SLOGIN: # server_session_login
|
||||
# a session has authenticated; sync it.
|
||||
portal_sessionhandler.server_logged_in(sessid, data)
|
||||
|
||||
elif operation == SDISCONN: # server_session_disconnect
|
||||
# the server is ordering to disconnect the session
|
||||
portal_sessionhandler.server_disconnect(sessid, reason=data)
|
||||
|
||||
elif operation == SDISCONNALL: # server_session_disconnect_all
|
||||
# server orders all sessions to disconnect
|
||||
portal_sessionhandler.server_disconnect_all(reason=data)
|
||||
|
||||
elif operation == SSHUTD: # server_shutdown
|
||||
# the server orders the portal to shut down
|
||||
self.factory.portal.shutdown(restart=False)
|
||||
|
||||
elif operation == SSYNC: # server_session_sync
|
||||
# server wants to save session data to the portal,
|
||||
# maybe because it's about to shut down.
|
||||
portal_sessionhandler.server_session_sync(data)
|
||||
# set a flag in case we are about to shut down soon
|
||||
self.factory.server_restart_mode = True
|
||||
|
||||
elif operation == SCONN: # server_force_connection (for irc/imc2 etc)
|
||||
portal_sessionhandler.server_connect(**data)
|
||||
|
||||
else:
|
||||
raise Exception("operation %(op)s not recognized." % {'op': operation})
|
||||
return {}
|
||||
PortalAdmin.responder(amp_portal_admin)
|
||||
|
||||
def call_remote_PortalAdmin(self, sessid, operation="", data=""):
|
||||
"""
|
||||
Access method called by the server side.
|
||||
"""
|
||||
self.safe_send(PortalAdmin, sessid, operation=operation, data=dumps(data))
|
||||
|
||||
# Extra functions
|
||||
|
||||
def amp_function_call(self, module, function, args, **kwargs):
|
||||
"""
|
||||
This allows Portal- and Server-process to call an arbitrary function
|
||||
in the other process. It is intended for use by plugin modules.
|
||||
"""
|
||||
args = loads(args)
|
||||
kwargs = loads(kwargs)
|
||||
|
||||
# call the function (don't catch tracebacks here)
|
||||
result = variable_from_module(module, function)(*args, **kwargs)
|
||||
|
||||
if isinstance(result, Deferred):
|
||||
# if result is a deferred, attach handler to properly
|
||||
# wrap the return value
|
||||
result.addCallback(lambda r: {"result": dumps(r)})
|
||||
return result
|
||||
else:
|
||||
return {'result': dumps(result)}
|
||||
FunctionCall.responder(amp_function_call)
|
||||
|
||||
def call_remote_FunctionCall(self, modulepath, functionname, *args, **kwargs):
|
||||
"""
|
||||
Access method called by either process. This will call an arbitrary
|
||||
function on the other process (On Portal if calling from Server and
|
||||
vice versa).
|
||||
|
||||
Inputs:
|
||||
modulepath (str) - python path to module holding function to call
|
||||
functionname (str) - name of function in given module
|
||||
*args, **kwargs will be used as arguments/keyword args for the
|
||||
remote function call
|
||||
Returns:
|
||||
A deferred that fires with the return value of the remote
|
||||
function call
|
||||
"""
|
||||
return self.callRemote(FunctionCall,
|
||||
module=modulepath,
|
||||
function=functionname,
|
||||
args=dumps(args),
|
||||
kwargs=dumps(kwargs)).addCallback(lambda r: loads(r["result"])).addErrback(self.errback, "FunctionCall")
|
||||
193
lib/server/caches.py
Normal file
193
lib/server/caches.py
Normal file
|
|
@ -0,0 +1,193 @@
|
|||
"""
|
||||
Central caching module.
|
||||
|
||||
"""
|
||||
|
||||
from sys import getsizeof
|
||||
import os
|
||||
import threading
|
||||
from collections import defaultdict
|
||||
|
||||
from src.server.models import ServerConfig
|
||||
from src.utils.utils import uses_database, to_str, get_evennia_pids
|
||||
|
||||
_GA = object.__getattribute__
|
||||
_SA = object.__setattr__
|
||||
_DA = object.__delattr__
|
||||
|
||||
_IS_SUBPROCESS = os.getpid() in get_evennia_pids()
|
||||
_IS_MAIN_THREAD = threading.currentThread().getName() == "MainThread"
|
||||
|
||||
#
|
||||
# Set up the cache stores
|
||||
#
|
||||
|
||||
_ATTR_CACHE = {}
|
||||
_PROP_CACHE = defaultdict(dict)
|
||||
|
||||
#------------------------------------------------------------
|
||||
# Cache key hash generation
|
||||
#------------------------------------------------------------
|
||||
|
||||
if uses_database("mysql") and ServerConfig.objects.get_mysql_db_version() < '5.6.4':
|
||||
# mysql <5.6.4 don't support millisecond precision
|
||||
_DATESTRING = "%Y:%m:%d-%H:%M:%S:000000"
|
||||
else:
|
||||
_DATESTRING = "%Y:%m:%d-%H:%M:%S:%f"
|
||||
|
||||
|
||||
def hashid(obj, suffix=""):
|
||||
"""
|
||||
Returns a per-class unique hash that combines the object's
|
||||
class name with its idnum and creation time. This makes this id unique also
|
||||
between different typeclassed entities such as scripts and
|
||||
objects (which may still have the same id).
|
||||
"""
|
||||
if not obj:
|
||||
return obj
|
||||
try:
|
||||
hid = _GA(obj, "_hashid")
|
||||
except AttributeError:
|
||||
try:
|
||||
date, idnum = _GA(obj, "db_date_created").strftime(_DATESTRING), _GA(obj, "id")
|
||||
except AttributeError:
|
||||
try:
|
||||
# maybe a typeclass, try to go to dbobj
|
||||
obj = _GA(obj, "dbobj")
|
||||
date, idnum = _GA(obj, "db_date_created").strftime(_DATESTRING), _GA(obj, "id")
|
||||
except AttributeError:
|
||||
# this happens if hashing something like ndb. We have to
|
||||
# rely on memory adressing in this case.
|
||||
date, idnum = "InMemory", id(obj)
|
||||
if not idnum or not date:
|
||||
# this will happen if setting properties on an object which
|
||||
# is not yet saved
|
||||
return None
|
||||
# we have to remove the class-name's space, for eventual use
|
||||
# of memcached
|
||||
hid = "%s-%s-#%s" % (_GA(obj, "__class__"), date, idnum)
|
||||
hid = hid.replace(" ", "")
|
||||
# we cache the object part of the hashid to avoid too many
|
||||
# object lookups
|
||||
_SA(obj, "_hashid", hid)
|
||||
# build the complete hashid
|
||||
hid = "%s%s" % (hid, suffix)
|
||||
return to_str(hid)
|
||||
|
||||
|
||||
#------------------------------------------------------------
|
||||
# Cache callback handlers
|
||||
#------------------------------------------------------------
|
||||
|
||||
# callback to field pre_save signal (connected in src.server.server)
|
||||
#def field_pre_save(sender, instance=None, update_fields=None, raw=False, **kwargs):
|
||||
# """
|
||||
# Called at the beginning of the field save operation. The save method
|
||||
# must be called with the update_fields keyword in order to be most efficient.
|
||||
# This method should NOT save; rather it is the save() that triggers this
|
||||
# function. Its main purpose is to allow to plug-in a save handler and oob
|
||||
# handlers.
|
||||
# """
|
||||
# if raw:
|
||||
# return
|
||||
# if update_fields:
|
||||
# # this is a list of strings at this point. We want field objects
|
||||
# update_fields = (_GA(_GA(instance, "_meta"), "get_field_by_name")(field)[0] for field in update_fields)
|
||||
# else:
|
||||
# # meta.fields are already field objects; get them all
|
||||
# update_fields = _GA(_GA(instance, "_meta"), "fields")
|
||||
# for field in update_fields:
|
||||
# fieldname = field.name
|
||||
# handlername = "_at_%s_presave" % fieldname
|
||||
# handler = _GA(instance, handlername) if handlername in _GA(sender, '__dict__') else None
|
||||
# if callable(handler):
|
||||
# handler()
|
||||
|
||||
|
||||
def field_post_save(sender, instance=None, update_fields=None, raw=False, **kwargs):
|
||||
"""
|
||||
Called at the beginning of the field save operation. The save method
|
||||
must be called with the update_fields keyword in order to be most efficient.
|
||||
This method should NOT save; rather it is the save() that triggers this
|
||||
function. Its main purpose is to allow to plug-in a save handler and oob
|
||||
handlers.
|
||||
"""
|
||||
if raw:
|
||||
return
|
||||
if update_fields:
|
||||
# this is a list of strings at this point. We want field objects
|
||||
update_fields = (_GA(_GA(instance, "_meta"), "get_field_by_name")(field)[0] for field in update_fields)
|
||||
else:
|
||||
# meta.fields are already field objects; get them all
|
||||
update_fields = _GA(_GA(instance, "_meta"), "fields")
|
||||
for field in update_fields:
|
||||
fieldname = field.name
|
||||
handlername = "_at_%s_postsave" % fieldname
|
||||
handler = _GA(instance, handlername) if handlername in _GA(sender, '__dict__') else None
|
||||
if callable(handler):
|
||||
handler()
|
||||
trackerhandler = _GA(instance, "_trackerhandler") if "_trackerhandler" in _GA(instance, '__dict__') else None
|
||||
if trackerhandler:
|
||||
trackerhandler.update(fieldname, _GA(instance, fieldname))
|
||||
|
||||
#------------------------------------------------------------
|
||||
# Attribute lookup cache
|
||||
#------------------------------------------------------------
|
||||
|
||||
def get_attr_cache(obj):
|
||||
"Retrieve lookup cache"
|
||||
hid = hashid(obj)
|
||||
return _ATTR_CACHE.get(hid, None)
|
||||
|
||||
|
||||
def set_attr_cache(obj, store):
|
||||
"Set lookup cache"
|
||||
global _ATTR_CACHE
|
||||
hid = hashid(obj)
|
||||
_ATTR_CACHE[hid] = store
|
||||
|
||||
#------------------------------------------------------------
|
||||
# Property cache - this is a generic cache for properties stored on models.
|
||||
#------------------------------------------------------------
|
||||
|
||||
# access methods
|
||||
|
||||
def get_prop_cache(obj, propname):
|
||||
"retrieve data from cache"
|
||||
hid = hashid(obj, "-%s" % propname)
|
||||
return _PROP_CACHE[hid].get(propname, None) if hid else None
|
||||
|
||||
|
||||
def set_prop_cache(obj, propname, propvalue):
|
||||
"Set property cache"
|
||||
hid = hashid(obj, "-%s" % propname)
|
||||
if hid:
|
||||
_PROP_CACHE[hid][propname] = propvalue
|
||||
|
||||
|
||||
def del_prop_cache(obj, propname):
|
||||
"Delete element from property cache"
|
||||
hid = hashid(obj, "-%s" % propname)
|
||||
if hid:
|
||||
if propname in _PROP_CACHE[hid]:
|
||||
del _PROP_CACHE[hid][propname]
|
||||
|
||||
|
||||
def flush_prop_cache():
|
||||
"Clear property cache"
|
||||
global _PROP_CACHE
|
||||
_PROP_CACHE = defaultdict(dict)
|
||||
|
||||
|
||||
def get_cache_sizes():
|
||||
"""
|
||||
Get cache sizes, expressed in number of objects and memory size in MB
|
||||
"""
|
||||
global _ATTR_CACHE, _PROP_CACHE
|
||||
attr_n = len(_ATTR_CACHE)
|
||||
attr_mb = sum(getsizeof(obj) for obj in _ATTR_CACHE) / 1024.0
|
||||
prop_n = sum(len(dic) for dic in _PROP_CACHE.values())
|
||||
prop_mb = sum(sum([getsizeof(obj) for obj in dic.values()]) for dic in _PROP_CACHE.values()) / 1024.0
|
||||
return (attr_n, attr_mb), (prop_n, prop_mb)
|
||||
|
||||
|
||||
267
lib/server/initial_setup.py
Normal file
267
lib/server/initial_setup.py
Normal file
|
|
@ -0,0 +1,267 @@
|
|||
"""
|
||||
This module handles initial database propagation, which is only run the first
|
||||
time the game starts. It will create some default channels, objects, and
|
||||
other things.
|
||||
|
||||
Everything starts at handle_setup()
|
||||
"""
|
||||
|
||||
import django
|
||||
from django.conf import settings
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.utils.translation import ugettext as _
|
||||
from src.players.models import PlayerDB
|
||||
from src.server.models import ServerConfig
|
||||
from src.utils import create
|
||||
from src.utils.utils import class_from_module
|
||||
|
||||
def create_config_values():
|
||||
"""
|
||||
Creates the initial config values.
|
||||
"""
|
||||
ServerConfig.objects.conf("site_name", settings.SERVERNAME)
|
||||
ServerConfig.objects.conf("idle_timeout", settings.IDLE_TIMEOUT)
|
||||
|
||||
|
||||
def get_god_player():
|
||||
"""
|
||||
Creates the god user.
|
||||
"""
|
||||
try:
|
||||
god_player = PlayerDB.objects.get(id=1)
|
||||
except PlayerDB.DoesNotExist:
|
||||
txt = "\n\nNo superuser exists yet. The superuser is the 'owner'\n" \
|
||||
"account on the Evennia server. Create a new superuser using\n" \
|
||||
"the command\n\n" \
|
||||
" python manage.py createsuperuser\n\n" \
|
||||
"Follow the prompts, then restart the server."
|
||||
raise Exception(txt)
|
||||
return god_player
|
||||
|
||||
|
||||
def create_objects():
|
||||
"""
|
||||
Creates the #1 player and Limbo room.
|
||||
"""
|
||||
|
||||
print " Creating objects (Player #1 and Limbo room) ..."
|
||||
|
||||
# Set the initial User's account object's username on the #1 object.
|
||||
# This object is pure django and only holds name, email and password.
|
||||
god_player = get_god_player()
|
||||
|
||||
# Create a Player 'user profile' object to hold eventual
|
||||
# mud-specific settings for the PlayerDB object.
|
||||
player_typeclass = settings.BASE_PLAYER_TYPECLASS
|
||||
|
||||
# run all creation hooks on god_player (we must do so manually
|
||||
# since the manage.py command does not)
|
||||
god_player.swap_typeclass(player_typeclass, clean_attributes=True)
|
||||
god_player.basetype_setup()
|
||||
god_player.at_player_creation()
|
||||
god_player.locks.add("examine:perm(Immortals);edit:false();delete:false();boot:false();msg:all()")
|
||||
# this is necessary for quelling to work correctly.
|
||||
god_player.permissions.add("Immortals")
|
||||
|
||||
# Limbo is the default "nowhere" starting room
|
||||
|
||||
# Create the in-game god-character for player #1 and set
|
||||
# it to exist in Limbo.
|
||||
character_typeclass = settings.BASE_CHARACTER_TYPECLASS
|
||||
god_character = create.create_object(character_typeclass,
|
||||
key=god_player.username,
|
||||
nohome=True)
|
||||
|
||||
god_character.id = 1
|
||||
god_character.db.desc = _('This is User #1.')
|
||||
god_character.locks.add("examine:perm(Immortals);edit:false();delete:false();boot:false();msg:all();puppet:false()")
|
||||
god_character.permissions.add("Immortals")
|
||||
|
||||
god_character.save()
|
||||
god_player.attributes.add("_first_login", True)
|
||||
god_player.attributes.add("_last_puppet", god_character)
|
||||
god_player.db._playable_characters.append(god_character)
|
||||
|
||||
room_typeclass = settings.BASE_ROOM_TYPECLASS
|
||||
limbo_obj = create.create_object(room_typeclass, _('Limbo'), nohome=True)
|
||||
limbo_obj.id = 2
|
||||
string = \
|
||||
"Welcome to your new {wEvennia{n-based game. From here you are ready " \
|
||||
"to begin development. Visit http://evennia.com if you should need " \
|
||||
"help or would like to participate in community discussions. If you " \
|
||||
"are logged in as user #1 you can create a demo/tutorial area with " \
|
||||
"{w@batchcommand contrib.tutorial_world.build{n. Use {w@quell{n or login " \
|
||||
"as normal player to play the demo properly."
|
||||
string = _(string)
|
||||
limbo_obj.db.desc = string
|
||||
limbo_obj.save()
|
||||
|
||||
# Now that Limbo exists, try to set the user up in Limbo (unless
|
||||
# the creation hooks already fixed this).
|
||||
if not god_character.location:
|
||||
god_character.location = limbo_obj
|
||||
if not god_character.home:
|
||||
god_character.home = limbo_obj
|
||||
|
||||
|
||||
def create_channels():
|
||||
"""
|
||||
Creates some sensible default channels.
|
||||
"""
|
||||
print " Creating default channels ..."
|
||||
|
||||
# public channel
|
||||
key1, aliases, desc, locks = settings.CHANNEL_PUBLIC
|
||||
pchan = create.create_channel(key1, aliases, desc, locks=locks)
|
||||
# mudinfo channel
|
||||
key2, aliases, desc, locks = settings.CHANNEL_MUDINFO
|
||||
ichan = create.create_channel(key2, aliases, desc, locks=locks)
|
||||
# connectinfo channel
|
||||
key3, aliases, desc, locks = settings.CHANNEL_CONNECTINFO
|
||||
cchan = create.create_channel(key3, aliases, desc, locks=locks)
|
||||
|
||||
# TODO: postgresql-psycopg2 has a strange error when trying to
|
||||
# connect the user to the default channels. It works fine from inside
|
||||
# the game, but not from the initial startup. We are temporarily bypassing
|
||||
# the problem with the following fix. See Evennia Issue 151.
|
||||
if ((".".join(str(i) for i in django.VERSION) < "1.2"
|
||||
and settings.DATABASE_ENGINE == "postgresql_psycopg2")
|
||||
or (hasattr(settings, 'DATABASES')
|
||||
and settings.DATABASES.get("default", {}).get('ENGINE', None)
|
||||
== 'django.db.backends.postgresql_psycopg2')):
|
||||
warning = """
|
||||
PostgreSQL-psycopg2 compatability fix:
|
||||
The in-game channels %s, %s and %s were created,
|
||||
but the superuser was not yet connected to them. Please use in
|
||||
game commands to onnect Player #1 to those channels when first
|
||||
logging in.
|
||||
""" % (key1, key2, key3)
|
||||
print warning
|
||||
return
|
||||
|
||||
# connect the god user to all these channels by default.
|
||||
goduser = get_god_player()
|
||||
pchan.connect(goduser)
|
||||
ichan.connect(goduser)
|
||||
cchan.connect(goduser)
|
||||
|
||||
|
||||
def create_system_scripts():
|
||||
"""
|
||||
Setup the system repeat scripts. They are automatically started
|
||||
by the create_script function.
|
||||
"""
|
||||
from src.scripts import scripts
|
||||
|
||||
print " Creating and starting global scripts ..."
|
||||
|
||||
# check so that all sessions are alive.
|
||||
script1 = create.create_script(scripts.CheckSessions)
|
||||
# validate all scripts in script table.
|
||||
script2 = create.create_script(scripts.ValidateScripts)
|
||||
# update the channel handler to make sure it's in sync
|
||||
script3 = create.create_script(scripts.ValidateChannelHandler)
|
||||
# flush the idmapper cache
|
||||
script4 = create.create_script(scripts.ValidateIdmapperCache)
|
||||
|
||||
if not script1 or not script2 or not script3 or not script4:
|
||||
print " Error creating system scripts."
|
||||
|
||||
|
||||
def start_game_time():
|
||||
"""
|
||||
This starts a persistent script that keeps track of the
|
||||
in-game time (in whatever accelerated reference frame), but also
|
||||
the total run time of the server as well as its current uptime
|
||||
(the uptime can also be found directly from the server though).
|
||||
"""
|
||||
print " Starting in-game time ..."
|
||||
from src.utils import gametime
|
||||
gametime.init_gametime()
|
||||
|
||||
|
||||
def at_initial_setup():
|
||||
"""
|
||||
Custom hook for users to overload some or all parts of the initial
|
||||
setup. Called very last in the sequence. It tries to import and
|
||||
srun a module settings.AT_INITIAL_SETUP_HOOK_MODULE and will fail
|
||||
silently if this does not exist or fails to load.
|
||||
"""
|
||||
modname = settings.AT_INITIAL_SETUP_HOOK_MODULE
|
||||
if not modname:
|
||||
return
|
||||
try:
|
||||
mod = __import__(modname, fromlist=[None])
|
||||
except (ImportError, ValueError):
|
||||
return
|
||||
print " Running at_initial_setup() hook."
|
||||
if mod.__dict__.get("at_initial_setup", None):
|
||||
mod.at_initial_setup()
|
||||
|
||||
|
||||
def reset_server():
|
||||
"""
|
||||
We end the initialization by resetting the server. This
|
||||
makes sure the first login is the same as all the following
|
||||
ones, particularly it cleans all caches for the special objects.
|
||||
It also checks so the warm-reset mechanism works as it should.
|
||||
"""
|
||||
from src.server.sessionhandler import SESSIONS
|
||||
print " Initial setup complete. Restarting Server once."
|
||||
SESSIONS.server.shutdown(mode='reset')
|
||||
|
||||
|
||||
def handle_setup(last_step):
|
||||
"""
|
||||
Main logic for the module. It allows for restarting
|
||||
the initialization at any point if one of the modules
|
||||
should crash.
|
||||
"""
|
||||
|
||||
if last_step < 0:
|
||||
# this means we don't need to handle setup since
|
||||
# it already ran sucessfully once.
|
||||
return
|
||||
elif last_step is None:
|
||||
# config doesn't exist yet. First start of server
|
||||
last_step = 0
|
||||
|
||||
# setting up the list of functions to run
|
||||
setup_queue = [
|
||||
create_config_values,
|
||||
create_objects,
|
||||
create_channels,
|
||||
create_system_scripts,
|
||||
start_game_time,
|
||||
at_initial_setup,
|
||||
reset_server
|
||||
]
|
||||
|
||||
#print " Initial setup: %s steps." % (len(setup_queue))
|
||||
|
||||
# step through queue, from last completed function
|
||||
for num, setup_func in enumerate(setup_queue[last_step:]):
|
||||
# run the setup function. Note that if there is a
|
||||
# traceback we let it stop the system so the config
|
||||
# step is not saved.
|
||||
#print "%s..." % num
|
||||
|
||||
try:
|
||||
setup_func()
|
||||
except Exception:
|
||||
if last_step + num == 2:
|
||||
from src.players.models import PlayerDB
|
||||
from src.objects.models import ObjectDB
|
||||
|
||||
for obj in ObjectDB.objects.all():
|
||||
obj.delete()
|
||||
for profile in PlayerDB.objects.all():
|
||||
profile.delete()
|
||||
elif last_step + num == 3:
|
||||
from src.comms.models import ChannelDB
|
||||
ChannelDB.objects.all().delete()
|
||||
raise
|
||||
ServerConfig.objects.conf("last_initial_setup_step", last_step + num + 1)
|
||||
# We got through the entire list. Set last_step to -1 so we don't
|
||||
# have to run this again.
|
||||
ServerConfig.objects.conf("last_initial_setup_step", -1)
|
||||
53
lib/server/manager.py
Normal file
53
lib/server/manager.py
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
"""
|
||||
Custom manager for ServerConfig objects.
|
||||
"""
|
||||
from django.db import models
|
||||
|
||||
|
||||
class ServerConfigManager(models.Manager):
|
||||
"""
|
||||
This ServerConfigManager implements methods for searching
|
||||
and manipulating ServerConfigs directly from the database.
|
||||
|
||||
These methods will all return database objects
|
||||
(or QuerySets) directly.
|
||||
|
||||
ServerConfigs are used to store certain persistent settings for the
|
||||
server at run-time.
|
||||
|
||||
Evennia-specific:
|
||||
conf
|
||||
|
||||
"""
|
||||
def conf(self, key=None, value=None, delete=False, default=None):
|
||||
"""
|
||||
Access and manipulate config values
|
||||
"""
|
||||
if not key:
|
||||
return self.all()
|
||||
elif delete is True:
|
||||
for conf in self.filter(db_key=key):
|
||||
conf.delete()
|
||||
elif value is not None:
|
||||
conf = self.filter(db_key=key)
|
||||
if conf:
|
||||
conf = conf[0]
|
||||
else:
|
||||
conf = self.model(db_key=key)
|
||||
conf.value = value # this will pickle
|
||||
else:
|
||||
conf = self.filter(db_key=key)
|
||||
if not conf:
|
||||
return default
|
||||
return conf[0].value
|
||||
|
||||
def get_mysql_db_version(self):
|
||||
"""
|
||||
This is a helper method for getting the version string
|
||||
of a mysql database.
|
||||
"""
|
||||
from django.db import connection
|
||||
conn = connection.cursor()
|
||||
conn.execute("SELECT VERSION()")
|
||||
version = conn.fetchone()
|
||||
return version and str(version[0]) or ""
|
||||
26
lib/server/migrations/0001_initial.py
Normal file
26
lib/server/migrations/0001_initial.py
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import models, migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='ServerConfig',
|
||||
fields=[
|
||||
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
|
||||
('db_key', models.CharField(unique=True, max_length=64)),
|
||||
('db_value', models.TextField(blank=True)),
|
||||
],
|
||||
options={
|
||||
'verbose_name': 'Server Config value',
|
||||
'verbose_name_plural': 'Server Config values',
|
||||
},
|
||||
bases=(models.Model,),
|
||||
),
|
||||
]
|
||||
1
lib/server/migrations/__init__.py
Normal file
1
lib/server/migrations/__init__.py
Normal file
|
|
@ -0,0 +1 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
118
lib/server/models.py
Normal file
118
lib/server/models.py
Normal file
|
|
@ -0,0 +1,118 @@
|
|||
"""
|
||||
|
||||
Server Configuration flags
|
||||
|
||||
This holds persistent server configuration flags.
|
||||
|
||||
Config values should usually be set through the
|
||||
manager's conf() method.
|
||||
|
||||
"""
|
||||
try:
|
||||
import cPickle as pickle
|
||||
except ImportError:
|
||||
import pickle
|
||||
|
||||
from django.db import models
|
||||
from src.utils.idmapper.models import WeakSharedMemoryModel
|
||||
from src.utils import logger, utils
|
||||
from src.server.manager import ServerConfigManager
|
||||
|
||||
|
||||
#------------------------------------------------------------
|
||||
#
|
||||
# ServerConfig
|
||||
#
|
||||
#------------------------------------------------------------
|
||||
|
||||
class ServerConfig(WeakSharedMemoryModel):
|
||||
"""
|
||||
On-the fly storage of global settings.
|
||||
|
||||
Properties defined on ServerConfig:
|
||||
key - main identifier
|
||||
value - value stored in key. This is a pickled storage.
|
||||
|
||||
"""
|
||||
|
||||
#
|
||||
# ServerConfig database model setup
|
||||
#
|
||||
#
|
||||
# These database fields are all set using their corresponding properties,
|
||||
# named same as the field, but withtout the db_* prefix.
|
||||
|
||||
# main name of the database entry
|
||||
db_key = models.CharField(max_length=64, unique=True)
|
||||
# config value
|
||||
db_value = models.TextField(blank=True)
|
||||
|
||||
objects = ServerConfigManager()
|
||||
_is_deleted = False
|
||||
|
||||
# Wrapper properties to easily set database fields. These are
|
||||
# @property decorators that allows to access these fields using
|
||||
# normal python operations (without having to remember to save()
|
||||
# etc). So e.g. a property 'attr' has a get/set/del decorator
|
||||
# defined that allows the user to do self.attr = value,
|
||||
# value = self.attr and del self.attr respectively (where self
|
||||
# is the object in question).
|
||||
|
||||
# key property (wraps db_key)
|
||||
#@property
|
||||
def __key_get(self):
|
||||
"Getter. Allows for value = self.key"
|
||||
return self.db_key
|
||||
|
||||
#@key.setter
|
||||
def __key_set(self, value):
|
||||
"Setter. Allows for self.key = value"
|
||||
self.db_key = value
|
||||
self.save()
|
||||
|
||||
#@key.deleter
|
||||
def __key_del(self):
|
||||
"Deleter. Allows for del self.key. Deletes entry."
|
||||
self.delete()
|
||||
key = property(__key_get, __key_set, __key_del)
|
||||
|
||||
# value property (wraps db_value)
|
||||
#@property
|
||||
def __value_get(self):
|
||||
"Getter. Allows for value = self.value"
|
||||
return pickle.loads(str(self.db_value))
|
||||
|
||||
#@value.setter
|
||||
def __value_set(self, value):
|
||||
"Setter. Allows for self.value = value"
|
||||
if utils.has_parent('django.db.models.base.Model', value):
|
||||
# we have to protect against storing db objects.
|
||||
logger.log_errmsg("ServerConfig cannot store db objects! (%s)" % value)
|
||||
return
|
||||
self.db_value = pickle.dumps(value)
|
||||
self.save()
|
||||
|
||||
#@value.deleter
|
||||
def __value_del(self):
|
||||
"Deleter. Allows for del self.value. Deletes entry."
|
||||
self.delete()
|
||||
value = property(__value_get, __value_set, __value_del)
|
||||
|
||||
class Meta:
|
||||
"Define Django meta options"
|
||||
verbose_name = "Server Config value"
|
||||
verbose_name_plural = "Server Config values"
|
||||
|
||||
#
|
||||
# ServerConfig other methods
|
||||
#
|
||||
|
||||
def __unicode__(self):
|
||||
return "%s : %s" % (self.key, self.value)
|
||||
|
||||
def store(self, key, value):
|
||||
"""
|
||||
Wrap the storage (handles pickling)
|
||||
"""
|
||||
self.key = key
|
||||
self.value = value
|
||||
221
lib/server/oob_cmds.py
Normal file
221
lib/server/oob_cmds.py
Normal file
|
|
@ -0,0 +1,221 @@
|
|||
"""
|
||||
Out-of-band default plugin commands available for OOB handler.
|
||||
|
||||
This module implements commands as defined by the MSDP standard
|
||||
(http://tintin.sourceforge.net/msdp/), but is independent of the
|
||||
actual transfer protocol (webclient, MSDP, GMCP etc).
|
||||
|
||||
This module is pointed to by settings.OOB_PLUGIN_MODULES. All functions
|
||||
(not classes) defined globally in this module will be made available
|
||||
to the oob mechanism.
|
||||
|
||||
oob functions have the following call signature:
|
||||
function(oobhandler, session, *args, **kwargs)
|
||||
|
||||
where oobhandler is a back-reference to the central OOB_HANDLER
|
||||
instance and session is the active session to get return data.
|
||||
|
||||
The function names are not case-sensitive (this allows for names
|
||||
like "LIST" which would otherwise collide with Python builtins).
|
||||
|
||||
A function named OOB_ERROR will retrieve error strings if it is
|
||||
defined. It will get the error message as its 3rd argument.
|
||||
|
||||
Data is usually returned via
|
||||
session.msg(oob=(cmdname, (args,), {kwargs}))
|
||||
Note that args, kwargs must be iterable/dict, non-iterables will
|
||||
be interpreted as a new command name.
|
||||
|
||||
"""
|
||||
|
||||
from django.conf import settings
|
||||
_GA = object.__getattribute__
|
||||
_SA = object.__setattr__
|
||||
_NA_SEND = lambda o: "N/A"
|
||||
|
||||
#------------------------------------------------------------
|
||||
# All OOB commands must be on the form
|
||||
# cmdname(oobhandler, session, *args, **kwargs)
|
||||
#------------------------------------------------------------
|
||||
|
||||
def OOB_ERROR(oobhandler, session, errmsg, *args, **kwargs):
|
||||
"""
|
||||
A function with this name is special and is called by the oobhandler when an error
|
||||
occurs already at the execution stage (such as the oob function
|
||||
not being recognized or having the wrong args etc).
|
||||
"""
|
||||
session.msg(oob=("err", ("ERROR " + errmsg,)))
|
||||
|
||||
|
||||
def ECHO(oobhandler, session, *args, **kwargs):
|
||||
"Test/debug function, simply returning the args and kwargs"
|
||||
session.msg(oob=("echo", args, kwargs))
|
||||
|
||||
##OOB{"SEND":"CHARACTER_NAME"}
|
||||
def SEND(oobhandler, session, *args, **kwargs):
|
||||
"""
|
||||
This function directly returns the value of the given variable to the
|
||||
session.
|
||||
"""
|
||||
obj = session.get_puppet_or_player()
|
||||
ret = {}
|
||||
if obj:
|
||||
for name in (a.upper() for a in args if a):
|
||||
try:
|
||||
value = OOB_SENDABLE.get(name, _NA_SEND)(obj)
|
||||
ret[name] = value
|
||||
except Exception, e:
|
||||
ret[name] = str(e)
|
||||
session.msg(oob=("send", ret))
|
||||
else:
|
||||
session.msg(oob=("err", ("You must log in first.",)))
|
||||
|
||||
##OOB{"REPORT":"TEST"}
|
||||
def REPORT(oobhandler, session, *args, **kwargs):
|
||||
"""
|
||||
This creates a tracker instance to track the data given in *args.
|
||||
|
||||
The tracker will return with a oob structure
|
||||
oob={"report":["attrfieldname", (args,), {kwargs}}
|
||||
|
||||
Note that the data name is assumed to be a field is it starts with db_*
|
||||
and an Attribute otherwise.
|
||||
|
||||
"Example of tracking changes to the db_key field and the desc" Attribite:
|
||||
REPORT(oobhandler, session, "CHARACTER_NAME", )
|
||||
"""
|
||||
obj = session.get_puppet_or_player()
|
||||
if obj:
|
||||
for name in (a.upper() for a in args if a):
|
||||
trackname = OOB_REPORTABLE.get(name, None)
|
||||
if not trackname:
|
||||
session.msg(oob=("err", ("No Reportable property '%s'. Use LIST REPORTABLE_VARIABLES." % trackname,)))
|
||||
elif trackname.startswith("db_"):
|
||||
oobhandler.track_field(obj, session.sessid, trackname)
|
||||
else:
|
||||
oobhandler.track_attribute(obj, session.sessid, trackname)
|
||||
else:
|
||||
session.msg(oob=("err", ("You must log in first.",)))
|
||||
|
||||
|
||||
##OOB{"UNREPORT": "TEST"}
|
||||
def UNREPORT(oobhandler, session, *args, **kwargs):
|
||||
"""
|
||||
This removes tracking for the given data given in *args.
|
||||
"""
|
||||
obj = session.get_puppet_or_player()
|
||||
if obj:
|
||||
for name in (a.upper() for a in args if a):
|
||||
trackname = OOB_REPORTABLE.get(name, None)
|
||||
if not trackname:
|
||||
session.msg(oob=("err", ("No Un-Reportable property '%s'. Use LIST REPORTED_VALUES." % name,)))
|
||||
elif trackname.startswith("db_"):
|
||||
oobhandler.untrack_field(obj, session.sessid, trackname)
|
||||
else: # assume attribute
|
||||
oobhandler.untrack_attribute(obj, session.sessid, trackname)
|
||||
else:
|
||||
session.msg(oob=("err", ("You must log in first.",)))
|
||||
|
||||
|
||||
##OOB{"LIST":"COMMANDS"}
|
||||
def LIST(oobhandler, session, mode, *args, **kwargs):
|
||||
"""
|
||||
List available properties. Mode is the type of information
|
||||
desired:
|
||||
"COMMANDS" Request an array of commands supported
|
||||
by the server.
|
||||
"LISTS" Request an array of lists supported
|
||||
by the server.
|
||||
"CONFIGURABLE_VARIABLES" Request an array of variables the client
|
||||
can configure.
|
||||
"REPORTABLE_VARIABLES" Request an array of variables the server
|
||||
will report.
|
||||
"REPORTED_VARIABLES" Request an array of variables currently
|
||||
being reported.
|
||||
"SENDABLE_VARIABLES" Request an array of variables the server
|
||||
will send.
|
||||
"""
|
||||
mode = mode.upper()
|
||||
if mode == "COMMANDS":
|
||||
session.msg(oob=("list", ("COMMANDS",
|
||||
"LIST",
|
||||
"REPORT",
|
||||
"UNREPORT",
|
||||
# "RESET",
|
||||
"SEND")))
|
||||
elif mode == "LISTS":
|
||||
session.msg(oob=("list", ("LISTS",
|
||||
"REPORTABLE_VARIABLES",
|
||||
"REPORTED_VARIABLES",
|
||||
# "CONFIGURABLE_VARIABLES",
|
||||
"SENDABLE_VARIABLES")))
|
||||
elif mode == "REPORTABLE_VARIABLES":
|
||||
session.msg(oob=("list", ("REPORTABLE_VARIABLES",) +
|
||||
tuple(key for key in OOB_REPORTABLE.keys())))
|
||||
elif mode == "REPORTED_VARIABLES":
|
||||
# we need to check so as to use the right return value depending on if it is
|
||||
# an Attribute (identified by tracking the db_value field) or a normal database field
|
||||
reported = oobhandler.get_all_tracked(session)
|
||||
reported = [stored[2] if stored[2] != "db_value" else stored[4][0] for stored in reported]
|
||||
session.msg(oob=("list", ["REPORTED_VARIABLES"] + reported))
|
||||
elif mode == "SENDABLE_VARIABLES":
|
||||
session.msg(oob=("list", ("SENDABLE_VARIABLES",) +
|
||||
tuple(key for key in OOB_REPORTABLE.keys())))
|
||||
elif mode == "CONFIGURABLE_VARIABLES":
|
||||
# Not implemented (game specific)
|
||||
pass
|
||||
else:
|
||||
session.msg(oob=("err", ("LIST", "Unsupported mode",)))
|
||||
|
||||
def _repeat_callback(oobhandler, session, *args, **kwargs):
|
||||
"Set up by REPEAT"
|
||||
session.msg(oob=("repeat", ("Repeat!",)))
|
||||
|
||||
##OOB{"REPEAT":10}
|
||||
def REPEAT(oobhandler, session, interval, *args, **kwargs):
|
||||
"""
|
||||
Test command for the repeat functionality. Note that the args/kwargs
|
||||
must not be db objects (or anything else non-picklable), rather use
|
||||
dbrefs if so needed. The callback must be defined globally and
|
||||
will be called as
|
||||
callback(oobhandler, session, *args, **kwargs)
|
||||
"""
|
||||
oobhandler.repeat(None, session.sessid, interval, _repeat_callback, *args, **kwargs)
|
||||
|
||||
|
||||
##OOB{"UNREPEAT":10}
|
||||
def UNREPEAT(oobhandler, session, interval):
|
||||
"""
|
||||
Disable repeating callback
|
||||
"""
|
||||
oobhandler.unrepeat(None, session.sessid, interval)
|
||||
|
||||
|
||||
# Mapping for how to retrieve each property name.
|
||||
# Each entry should point to a callable that gets the interesting object as
|
||||
# input and returns the relevant value.
|
||||
|
||||
# MSDP recommends the following standard name mappings for general compliance:
|
||||
# "CHARACTER_NAME", "SERVER_ID", "SERVER_TIME", "AFFECTS", "ALIGNMENT", "EXPERIENCE", "EXPERIENCE_MAX", "EXPERIENCE_TNL",
|
||||
# "HEALTH", "HEALTH_MAX", "LEVEL", "RACE", "CLASS", "MANA", "MANA_MAX", "WIMPY", "PRACTICE", "MONEY", "MOVEMENT",
|
||||
# "MOVEMENT_MAX", "HITROLL", "DAMROLL", "AC", "STR", "INT", "WIS", "DEX", "CON", "OPPONENT_HEALTH", "OPPONENT_HEALTH_MAX",
|
||||
# "OPPONENT_LEVEL", "OPPONENT_NAME", "AREA_NAME", "ROOM_EXITS", "ROOM_VNUM", "ROOM_NAME", "WORLD_TIME", "CLIENT_ID",
|
||||
# "CLIENT_VERSION", "PLUGIN_ID", "ANSI_COLORS", "XTERM_256_COLORS", "UTF_8", "SOUND", "MXP", "BUTTON_1", "BUTTON_2",
|
||||
# "BUTTON_3", "BUTTON_4", "BUTTON_5", "GAUGE_1", "GAUGE_2","GAUGE_3", "GAUGE_4", "GAUGE_5"
|
||||
|
||||
OOB_SENDABLE = {
|
||||
"CHARACTER_NAME": lambda o: o.key,
|
||||
"SERVER_ID": lambda o: settings.SERVERNAME,
|
||||
"ROOM_NAME": lambda o: o.db_location.key,
|
||||
"ANSI_COLORS": lambda o: True,
|
||||
"XTERM_256_COLORS": lambda o: True,
|
||||
"UTF_8": lambda o: True
|
||||
}
|
||||
|
||||
# mapping for which properties may be tracked. Each value points either to a database field
|
||||
# (starting with db_*) or an Attribute name.
|
||||
OOB_REPORTABLE = {
|
||||
"CHARACTER_NAME": "db_key",
|
||||
"ROOM_NAME": "db_location",
|
||||
"TEST" : "test"
|
||||
}
|
||||
401
lib/server/oobhandler.py
Normal file
401
lib/server/oobhandler.py
Normal file
|
|
@ -0,0 +1,401 @@
|
|||
"""
|
||||
OOBHandler - Out Of Band Handler
|
||||
|
||||
The OOBHandler.execute_cmd is called by the sessionhandler when it detects
|
||||
an OOB instruction (exactly how this looked depends on the protocol; at this
|
||||
point all oob calls should look the same)
|
||||
|
||||
The handler pieces of functionality:
|
||||
|
||||
function execution - the oob protocol can execute a function directly on
|
||||
the server. The available functions must be defined
|
||||
as global functions in settings.OOB_PLUGIN_MODULES.
|
||||
repeat func execution - the oob protocol can request a given function be
|
||||
executed repeatedly at a regular interval. This
|
||||
uses an internal script pool.
|
||||
tracking - the oob protocol can request Evennia to track changes to
|
||||
fields on objects, as well as changes in Attributes. This is
|
||||
done by dynamically adding tracker-objects on entities. The
|
||||
behaviour of those objects can be customized by adding new
|
||||
tracker classes in settings.OOB_PLUGIN_MODULES.
|
||||
|
||||
What goes into the OOB_PLUGIN_MODULES is a (list of) modules that contains
|
||||
the working server-side code available to the OOB system: oob functions and
|
||||
tracker classes.
|
||||
|
||||
oob functions have the following call signature:
|
||||
function(caller, session, *args, **kwargs)
|
||||
|
||||
oob trackers should inherit from the OOBTracker class (in this
|
||||
module) and implement a minimum of the same functionality.
|
||||
|
||||
If a function named "oob_error" is given, this will be called with error
|
||||
messages.
|
||||
|
||||
"""
|
||||
|
||||
from inspect import isfunction
|
||||
from twisted.internet.defer import inlineCallbacks
|
||||
from django.conf import settings
|
||||
from src.server.models import ServerConfig
|
||||
from src.server.sessionhandler import SESSIONS
|
||||
#from src.scripts.scripts import Script
|
||||
#from src.utils.create import create_script
|
||||
from src.scripts.tickerhandler import Ticker, TickerPool, TickerHandler
|
||||
from src.utils.dbserialize import dbserialize, dbunserialize, pack_dbobj, unpack_dbobj
|
||||
from src.utils import logger
|
||||
from src.utils.utils import all_from_module, make_iter, to_str
|
||||
|
||||
_SA = object.__setattr__
|
||||
_GA = object.__getattribute__
|
||||
_DA = object.__delattr__
|
||||
|
||||
# load resources from plugin module
|
||||
_OOB_FUNCS = {}
|
||||
for mod in make_iter(settings.OOB_PLUGIN_MODULES):
|
||||
_OOB_FUNCS.update(dict((key.lower(), func) for key, func in all_from_module(mod).items() if isfunction(func)))
|
||||
|
||||
# get custom error method or use the default
|
||||
_OOB_ERROR = _OOB_FUNCS.get("oob_error", None)
|
||||
if not _OOB_ERROR:
|
||||
# create default oob error message function
|
||||
def oob_error(oobhandler, session, errmsg, *args, **kwargs):
|
||||
"Error wrapper"
|
||||
session.msg(oob=("err", ("ERROR ", errmsg)))
|
||||
_OOB_ERROR = oob_error
|
||||
|
||||
|
||||
#
|
||||
# TrackerHandler is assigned to objects that should notify themselves to
|
||||
# the OOB system when some property changes. This is never assigned manually
|
||||
# but automatically through the OOBHandler.
|
||||
#
|
||||
|
||||
class TrackerHandler(object):
|
||||
"""
|
||||
This object is dynamically assigned to objects whenever one of its fields
|
||||
are to be tracked. It holds an internal dictionary mapping to the fields
|
||||
on that object. Each field can be tracked by any number of trackers (each
|
||||
tied to a different callback).
|
||||
"""
|
||||
def __init__(self, obj):
|
||||
"""
|
||||
This is initiated and stored on the object as a
|
||||
property _trackerhandler.
|
||||
"""
|
||||
self.obj = obj
|
||||
self.ntrackers = 0
|
||||
# initiate store only with valid on-object fieldnames
|
||||
self.tracktargets = dict((key, {})
|
||||
for key in _GA(_GA(self.obj, "_meta"), "get_all_field_names")())
|
||||
|
||||
def add(self, fieldname, tracker):
|
||||
"""
|
||||
Add tracker to the handler. Raises KeyError if fieldname
|
||||
does not exist.
|
||||
"""
|
||||
trackerkey = tracker.__class__.__name__
|
||||
self.tracktargets[fieldname][trackerkey] = tracker
|
||||
self.ntrackers += 1
|
||||
|
||||
def remove(self, fieldname, trackerclass, *args, **kwargs):
|
||||
"""
|
||||
Remove identified tracker from TrackerHandler.
|
||||
Raises KeyError if tracker is not found.
|
||||
"""
|
||||
trackerkey = trackerclass.__name__
|
||||
tracker = self.tracktargets[fieldname][trackerkey]
|
||||
try:
|
||||
tracker.at_remove(*args, **kwargs)
|
||||
except Exception:
|
||||
logger.log_trace()
|
||||
del self.tracktargets[fieldname][trackerkey]
|
||||
self.ntrackers -= 1
|
||||
if self.ntrackers <= 0:
|
||||
# if there are no more trackers, clean this handler
|
||||
del self
|
||||
|
||||
def update(self, fieldname, new_value):
|
||||
"""
|
||||
Called by the field when it updates to a new value
|
||||
"""
|
||||
for tracker in self.tracktargets[fieldname].values():
|
||||
try:
|
||||
tracker.update(new_value)
|
||||
except Exception:
|
||||
logger.log_trace()
|
||||
|
||||
|
||||
# On-object Trackers to load with TrackerHandler
|
||||
|
||||
class TrackerBase(object):
|
||||
"""
|
||||
Base class for OOB Tracker objects. Inherit from this
|
||||
to define custom trackers.
|
||||
"""
|
||||
def __init__(self, *args, **kwargs):
|
||||
pass
|
||||
|
||||
def update(self, *args, **kwargs):
|
||||
"Called by tracked objects"
|
||||
pass
|
||||
|
||||
def at_remove(self, *args, **kwargs):
|
||||
"Called when tracker is removed"
|
||||
pass
|
||||
|
||||
|
||||
class ReportFieldTracker(TrackerBase):
|
||||
"""
|
||||
Tracker that passively sends data to a stored sessid whenever
|
||||
a named database field changes. The TrackerHandler calls this with
|
||||
the correct arguments.
|
||||
"""
|
||||
def __init__(self, oobhandler, fieldname, sessid, *args, **kwargs):
|
||||
"""
|
||||
name - name of entity to track, such as "db_key"
|
||||
sessid - sessid of session to report to
|
||||
"""
|
||||
self.oobhandler = oobhandler
|
||||
self.fieldname = fieldname
|
||||
self.sessid = sessid
|
||||
|
||||
def update(self, new_value, *args, **kwargs):
|
||||
"Called by cache when updating the tracked entitiy"
|
||||
# use oobhandler to relay data
|
||||
try:
|
||||
# we must never relay objects across the amp, only text data.
|
||||
new_value = new_value.key
|
||||
except AttributeError:
|
||||
new_value = to_str(new_value, force_string=True)
|
||||
kwargs[self.fieldname] = new_value
|
||||
# this is a wrapper call for sending oob data back to session
|
||||
self.oobhandler.msg(self.sessid, "report", *args, **kwargs)
|
||||
|
||||
|
||||
class ReportAttributeTracker(TrackerBase):
|
||||
"""
|
||||
Tracker that passively sends data to a stored sessid whenever
|
||||
the Attribute updates. Since the field here is always "db_key",
|
||||
we instead store the name of the attribute to return.
|
||||
"""
|
||||
def __init__(self, oobhandler, fieldname, sessid, attrname, *args, **kwargs):
|
||||
"""
|
||||
attrname - name of attribute to track
|
||||
sessid - sessid of session to report to
|
||||
"""
|
||||
self.oobhandler = oobhandler
|
||||
self.attrname = attrname
|
||||
self.sessid = sessid
|
||||
|
||||
def update(self, new_value, *args, **kwargs):
|
||||
"Called by cache when attribute's db_value field updates"
|
||||
kwargs[self.attrname] = new_value
|
||||
# this is a wrapper call for sending oob data back to session
|
||||
self.oobhandler.msg(self.sessid, "report", *args, **kwargs)
|
||||
|
||||
|
||||
|
||||
# Ticker of auto-updating objects
|
||||
|
||||
class OOBTicker(Ticker):
|
||||
"""
|
||||
Version of Ticker that executes an executable rather than trying to call
|
||||
a hook method.
|
||||
"""
|
||||
@inlineCallbacks
|
||||
def _callback(self):
|
||||
"See original for more info"
|
||||
for key, (_, args, kwargs) in self.subscriptions.items():
|
||||
# args = (sessid, callback_function)
|
||||
session = SESSIONS.session_from_sessid(args[0])
|
||||
try:
|
||||
# execute the oob callback
|
||||
yield args[1](OOB_HANDLER, session, *args[2:], **kwargs)
|
||||
except Exception:
|
||||
logger.log_trace()
|
||||
|
||||
class OOBTickerPool(TickerPool):
|
||||
ticker_class = OOBTicker
|
||||
|
||||
class OOBTickerHandler(TickerHandler):
|
||||
ticker_pool_class = OOBTickerPool
|
||||
|
||||
|
||||
# Main OOB Handler
|
||||
|
||||
class OOBHandler(object):
|
||||
"""
|
||||
The OOBHandler maintains all dynamic on-object oob hooks. It will store the
|
||||
creation instructions and and re-apply them at a server reload (but
|
||||
not after a server shutdown)
|
||||
"""
|
||||
def __init__(self):
|
||||
"""
|
||||
Initialize handler
|
||||
"""
|
||||
self.sessionhandler = SESSIONS
|
||||
self.oob_tracker_storage = {}
|
||||
self.tickerhandler = OOBTickerHandler("oob_ticker_storage")
|
||||
|
||||
def save(self):
|
||||
"""
|
||||
Save the command_storage as a serialized string into a temporary
|
||||
ServerConf field
|
||||
"""
|
||||
if self.oob_tracker_storage:
|
||||
#print "saved tracker_storage:", self.oob_tracker_storage
|
||||
ServerConfig.objects.conf(key="oob_tracker_storage",
|
||||
value=dbserialize(self.oob_tracker_storage))
|
||||
self.tickerhandler.save()
|
||||
|
||||
def restore(self):
|
||||
"""
|
||||
Restore the command_storage from database and re-initialize the handler from storage.. This is
|
||||
only triggered after a server reload, not after a shutdown-restart
|
||||
"""
|
||||
# load stored command instructions and use them to re-initialize handler
|
||||
tracker_storage = ServerConfig.objects.conf(key="oob_tracker_storage")
|
||||
if tracker_storage:
|
||||
self.oob_tracker_storage = dbunserialize(tracker_storage)
|
||||
for (obj, sessid, fieldname, trackerclass, args, kwargs) in self.oob_tracker_storage.values():
|
||||
#print "restoring tracking:",obj, sessid, fieldname, trackerclass
|
||||
self._track(unpack_dbobj(obj), sessid, fieldname, trackerclass, *args, **kwargs)
|
||||
# make sure to purge the storage
|
||||
ServerConfig.objects.conf(key="oob_tracker_storage", delete=True)
|
||||
self.tickerhandler.restore()
|
||||
|
||||
def _track(self, obj, sessid, propname, trackerclass, *args, **kwargs):
|
||||
"""
|
||||
Create an OOB obj of class _oob_MAPPING[tracker_key] on obj. args,
|
||||
kwargs will be used to initialize the OOB hook before adding
|
||||
it to obj.
|
||||
If propname is not given, but the OOB has a class property
|
||||
named as propname, this will be used as the property name when assigning
|
||||
the OOB to obj, otherwise tracker_key is used as the property name.
|
||||
"""
|
||||
if not "_trackerhandler" in _GA(obj, "__dict__"):
|
||||
# assign trackerhandler to object
|
||||
_SA(obj, "_trackerhandler", TrackerHandler(obj))
|
||||
# initialize object
|
||||
tracker = trackerclass(self, propname, sessid, *args, **kwargs)
|
||||
_GA(obj, "_trackerhandler").add(propname, tracker)
|
||||
# store calling arguments as a pickle for retrieval later
|
||||
obj_packed = pack_dbobj(obj)
|
||||
storekey = (obj_packed, sessid, propname)
|
||||
stored = (obj_packed, sessid, propname, trackerclass, args, kwargs)
|
||||
self.oob_tracker_storage[storekey] = stored
|
||||
#print "_track:", obj, id(obj), obj.__dict__
|
||||
|
||||
def _untrack(self, obj, sessid, propname, trackerclass, *args, **kwargs):
|
||||
"""
|
||||
Remove the OOB from obj. If oob implements an
|
||||
at_delete hook, this will be called with args, kwargs
|
||||
"""
|
||||
try:
|
||||
# call at_remove hook on the trackerclass
|
||||
_GA(obj, "_trackerhandler").remove(propname, trackerclass, *args, **kwargs)
|
||||
except AttributeError:
|
||||
pass
|
||||
# remove the pickle from storage
|
||||
store_key = (pack_dbobj(obj), sessid, propname)
|
||||
self.oob_tracker_storage.pop(store_key, None)
|
||||
|
||||
def get_all_tracked(self, session):
|
||||
"""
|
||||
Get the names of all variables this session is tracking.
|
||||
"""
|
||||
sessid = session.sessid
|
||||
return [stored for key, stored in self.oob_tracker_storage.items() if key[1] == sessid]
|
||||
|
||||
def track_field(self, obj, sessid, field_name, trackerclass=ReportFieldTracker):
|
||||
"""
|
||||
Shortcut wrapper method for specifically tracking a database field.
|
||||
Takes the tracker class as argument.
|
||||
"""
|
||||
# all database field names starts with db_*
|
||||
field_name = field_name if field_name.startswith("db_") else "db_%s" % field_name
|
||||
self._track(obj, sessid, field_name, trackerclass, field_name)
|
||||
|
||||
def untrack_field(self, obj, sessid, field_name, trackerclass=ReportFieldTracker):
|
||||
"""
|
||||
Shortcut for untracking a database field. Uses OOBTracker by defualt
|
||||
"""
|
||||
field_name = field_name if field_name.startswith("db_") else "db_%s" % field_name
|
||||
self._untrack(obj, sessid, field_name, trackerclass)
|
||||
|
||||
def track_attribute(self, obj, sessid, attr_name, trackerclass=ReportAttributeTracker):
|
||||
"""
|
||||
Shortcut wrapper method for specifically tracking the changes of an
|
||||
Attribute on an object. Will create a tracker on the Attribute
|
||||
Object and name in a way the Attribute expects.
|
||||
"""
|
||||
# get the attribute object if we can
|
||||
attrobj = obj.attributes.get(attr_name, return_obj=True)
|
||||
#print "track_attribute attrobj:", attrobj, id(attrobj)
|
||||
if attrobj:
|
||||
self._track(attrobj, sessid, "db_value", trackerclass, attr_name)
|
||||
|
||||
def untrack_attribute(self, obj, sessid, attr_name, trackerclass=ReportAttributeTracker):
|
||||
"""
|
||||
Shortcut for deactivating tracking for a given attribute.
|
||||
"""
|
||||
attrobj = obj.attributes.get(attr_name, return_obj=True)
|
||||
if attrobj:
|
||||
self._untrack(attrobj, sessid, "db_value", trackerclass, attr_name)
|
||||
|
||||
def repeat(self, obj, sessid, interval=20, callback=None, *args, **kwargs):
|
||||
"""
|
||||
Start a repeating action. Every interval seconds, trigger
|
||||
callback(*args, **kwargs). The callback is called with
|
||||
args and kwargs; note that *args and **kwargs may not contain
|
||||
anything un-picklable (use dbrefs if wanting to use objects).
|
||||
"""
|
||||
self.tickerhandler.add(obj, interval, sessid, callback, *args, **kwargs)
|
||||
|
||||
def unrepeat(self, obj, sessid, interval=20):
|
||||
"""
|
||||
Stop a repeating action
|
||||
"""
|
||||
self.tickerhandler.remove(obj, interval)
|
||||
|
||||
|
||||
# access method - called from session.msg()
|
||||
|
||||
def execute_cmd(self, session, func_key, *args, **kwargs):
|
||||
"""
|
||||
Retrieve oobfunc from OOB_FUNCS and execute it immediately
|
||||
using *args and **kwargs
|
||||
"""
|
||||
oobfunc = _OOB_FUNCS.get(func_key, None)
|
||||
if not oobfunc:
|
||||
# function not found
|
||||
errmsg = "OOB Error: function '%s' not recognized." % func_key
|
||||
if _OOB_ERROR:
|
||||
_OOB_ERROR(self, session, errmsg, *args, **kwargs)
|
||||
logger.log_trace()
|
||||
else:
|
||||
logger.log_trace(errmsg)
|
||||
return
|
||||
|
||||
# execute the found function
|
||||
try:
|
||||
#print "OOB execute_cmd:", session, func_key, args, kwargs, _OOB_FUNCS.keys()
|
||||
oobfunc(self, session, *args, **kwargs)
|
||||
except Exception, err:
|
||||
errmsg = "OOB Error: Exception in '%s'(%s, %s):\n%s" % (func_key, args, kwargs, err)
|
||||
if _OOB_ERROR:
|
||||
_OOB_ERROR(self, session, errmsg, *args, **kwargs)
|
||||
logger.log_trace(errmsg)
|
||||
raise Exception(errmsg)
|
||||
|
||||
def msg(self, sessid, funcname, *args, **kwargs):
|
||||
"Shortcut to force-send an OOB message through the oobhandler to a session"
|
||||
session = self.sessionhandler.session_from_sessid(sessid)
|
||||
#print "oobhandler msg:", sessid, session, funcname, args, kwargs
|
||||
if session:
|
||||
session.msg(oob=(funcname, args, kwargs))
|
||||
|
||||
|
||||
# access object
|
||||
OOB_HANDLER = OOBHandler()
|
||||
1
lib/server/portal/__init__.py
Normal file
1
lib/server/portal/__init__.py
Normal file
|
|
@ -0,0 +1 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
399
lib/server/portal/imc2.py
Normal file
399
lib/server/portal/imc2.py
Normal file
|
|
@ -0,0 +1,399 @@
|
|||
"""
|
||||
IMC2 client module. Handles connecting to and communicating with an IMC2 server.
|
||||
"""
|
||||
|
||||
from time import time
|
||||
from twisted.internet import task
|
||||
from twisted.application import internet
|
||||
from twisted.internet import protocol
|
||||
from twisted.conch import telnet
|
||||
|
||||
from src.server.session import Session
|
||||
from src.utils import logger, utils
|
||||
from src.server.portal.imc2lib import imc2_ansi
|
||||
from src.server.portal.imc2lib import imc2_packets as pck
|
||||
|
||||
from django.utils.translation import ugettext as _
|
||||
|
||||
|
||||
# storage containers for IMC2 muds and channels
|
||||
|
||||
class IMC2Mud(object):
|
||||
"""
|
||||
Stores information about other games connected to our current IMC2 network.
|
||||
"""
|
||||
def __init__(self, packet):
|
||||
self.name = packet.origin
|
||||
self.versionid = packet.optional_data.get('versionid', None)
|
||||
self.networkname = packet.optional_data.get('networkname', None)
|
||||
self.url = packet.optional_data.get('url', None)
|
||||
self.host = packet.optional_data.get('host', None)
|
||||
self.port = packet.optional_data.get('port', None)
|
||||
self.sha256 = packet.optional_data.get('sha256', None)
|
||||
# This is used to determine when a Mud has fallen into inactive status.
|
||||
self.last_updated = time()
|
||||
|
||||
|
||||
class IMC2MudList(dict):
|
||||
"""
|
||||
Keeps track of other MUDs connected to the IMC network.
|
||||
"""
|
||||
def get_mud_list(self):
|
||||
"""
|
||||
Returns a sorted list of connected Muds.
|
||||
"""
|
||||
muds = self.items()
|
||||
muds.sort()
|
||||
return [value for key, value in muds]
|
||||
|
||||
def update_mud_from_packet(self, packet):
|
||||
"""
|
||||
This grabs relevant info from the packet and stuffs it in the
|
||||
Mud list for later retrieval.
|
||||
"""
|
||||
mud = IMC2Mud(packet)
|
||||
self[mud.name] = mud
|
||||
|
||||
def remove_mud_from_packet(self, packet):
|
||||
"""
|
||||
Removes a mud from the Mud list when given a packet.
|
||||
"""
|
||||
mud = IMC2Mud(packet)
|
||||
try:
|
||||
del self[mud.name]
|
||||
except KeyError:
|
||||
# No matching entry, no big deal.
|
||||
pass
|
||||
|
||||
|
||||
class IMC2Channel(object):
|
||||
"""
|
||||
Stores information about channels available on the network.
|
||||
"""
|
||||
def __init__(self, packet):
|
||||
self.localname = packet.optional_data.get('localname', None)
|
||||
self.name = packet.optional_data.get('channel', None)
|
||||
self.level = packet.optional_data.get('level', None)
|
||||
self.owner = packet.optional_data.get('owner', None)
|
||||
self.policy = packet.optional_data.get('policy', None)
|
||||
self.last_updated = time()
|
||||
|
||||
|
||||
class IMC2ChanList(dict):
|
||||
"""
|
||||
Keeps track of Channels on the IMC network.
|
||||
"""
|
||||
|
||||
def get_channel_list(self):
|
||||
"""
|
||||
Returns a sorted list of cached channels.
|
||||
"""
|
||||
channels = self.items()
|
||||
channels.sort()
|
||||
return [value for key, value in channels]
|
||||
|
||||
def update_channel_from_packet(self, packet):
|
||||
"""
|
||||
This grabs relevant info from the packet and stuffs it in the
|
||||
channel list for later retrieval.
|
||||
"""
|
||||
channel = IMC2Channel(packet)
|
||||
self[channel.name] = channel
|
||||
|
||||
def remove_channel_from_packet(self, packet):
|
||||
"""
|
||||
Removes a channel from the Channel list when given a packet.
|
||||
"""
|
||||
channel = IMC2Channel(packet)
|
||||
try:
|
||||
del self[channel.name]
|
||||
except KeyError:
|
||||
# No matching entry, no big deal.
|
||||
pass
|
||||
|
||||
|
||||
#
|
||||
# IMC2 protocol
|
||||
#
|
||||
|
||||
class IMC2Bot(telnet.StatefulTelnetProtocol, Session):
|
||||
"""
|
||||
Provides the abstraction for the IMC2 protocol. Handles connection,
|
||||
authentication, and all necessary packets.
|
||||
"""
|
||||
def __init__(self):
|
||||
self.is_authenticated = False
|
||||
# only support plaintext passwords
|
||||
self.auth_type = "plaintext"
|
||||
self.sequence = None
|
||||
self.imc2_mudlist = IMC2MudList()
|
||||
self.imc2_chanlist = IMC2ChanList()
|
||||
|
||||
def _send_packet(self, packet):
|
||||
"Helper function to send packets across the wire"
|
||||
packet.imc2_protocol = self
|
||||
packet_str = utils.to_str(packet.assemble(self.factory.mudname,
|
||||
self.factory.client_pwd, self.factory.server_pwd))
|
||||
self.sendLine(packet_str)
|
||||
|
||||
def _isalive(self):
|
||||
"Send an isalive packet"
|
||||
self._send_packet(pck.IMC2PacketIsAlive())
|
||||
|
||||
def _keepalive(self):
|
||||
"Send a keepalive packet"
|
||||
# send to channel?
|
||||
self._send_packet(pck.IMC2PacketKeepAliveRequest())
|
||||
|
||||
def _channellist(self):
|
||||
"Sync the network channel list"
|
||||
checked_networks = []
|
||||
if not self.network in checked_networks:
|
||||
self._send_packet(pck.IMC2PacketIceRefresh())
|
||||
checked_networks.append(self.network)
|
||||
|
||||
def _prune(self):
|
||||
"Prune active channel list"
|
||||
t0 = time()
|
||||
for name, mudinfo in self.imc2_mudlist.items():
|
||||
if t0 - mudinfo.last_updated > 3599:
|
||||
del self.imc2_mudlist[name]
|
||||
|
||||
def _whois_reply(self, packet):
|
||||
"handle reply from server from an imcwhois request"
|
||||
# packet.target potentially contains the id of an character to target
|
||||
# not using that here
|
||||
response_text = imc2_ansi.parse_ansi(packet.optional_data.get('text', 'Unknown'))
|
||||
string = _('Whois reply from %(origin)s: %(msg)s') % {"origin":packet.origin, "msg":response_text}
|
||||
# somehow pass reply on to a given player, for now we just send to channel
|
||||
self.data_in(string)
|
||||
|
||||
def _format_tell(self, packet):
|
||||
"""
|
||||
Handle tells over IMC2 by formatting the text properly
|
||||
"""
|
||||
return _("{c%(sender)s@%(origin)s{n {wpages (over IMC):{n %(msg)s") % {"sender": packet.sender,
|
||||
"origin": packet.origin,
|
||||
"msg": packet.optional_data.get('text', 'ERROR: No text provided.')}
|
||||
|
||||
def _imc_login(self, line):
|
||||
"Connect and identify to imc network"
|
||||
|
||||
if self.auth_type == "plaintext":
|
||||
# Only support Plain text passwords.
|
||||
# SERVER Sends: PW <servername> <serverpw> version=<version#> <networkname>
|
||||
|
||||
logger.log_infomsg("IMC2: AUTH< %s" % line)
|
||||
|
||||
line_split = line.split(' ')
|
||||
pw_present = line_split[0] == 'PW'
|
||||
autosetup_present = line_split[0] == 'autosetup'
|
||||
|
||||
if "reject" in line_split:
|
||||
auth_message = _("IMC2 server rejected connection.")
|
||||
logger.log_infomsg(auth_message)
|
||||
return
|
||||
|
||||
if pw_present:
|
||||
self.server_name = line_split[1]
|
||||
self.network_name = line_split[4]
|
||||
elif autosetup_present:
|
||||
logger.log_infomsg(_("IMC2: Autosetup response found."))
|
||||
self.server_name = line_split[1]
|
||||
self.network_name = line_split[3]
|
||||
self.is_authenticated = True
|
||||
self.sequence = int(time())
|
||||
|
||||
# Log to stdout and notify over MUDInfo.
|
||||
logger.log_infomsg('IMC2: Authenticated to %s' % self.factory.network)
|
||||
|
||||
# Ask to see what other MUDs are connected.
|
||||
self._send_packet(pck.IMC2PacketKeepAliveRequest())
|
||||
# IMC2 protocol states that KeepAliveRequests should be followed
|
||||
# up by the requester sending an IsAlive packet.
|
||||
self._send_packet(pck.IMC2PacketIsAlive())
|
||||
# Get a listing of channels.
|
||||
self._send_packet(pck.IMC2PacketIceRefresh())
|
||||
|
||||
def connectionMade(self):
|
||||
"""
|
||||
Triggered after connecting to the IMC2 network.
|
||||
"""
|
||||
|
||||
self.stopping = False
|
||||
self.factory.bot = self
|
||||
address = "%s@%s" % (self.mudname, self.network)
|
||||
self.init_session("ircbot", address, self.factory.sessionhandler)
|
||||
# link back and log in
|
||||
self.uid = int(self.factory.uid)
|
||||
self.logged_in = True
|
||||
self.factory.sessionhandler.connect(self)
|
||||
logger.log_infomsg("IMC2 bot connected to %s." % self.network)
|
||||
# Send authentication packet. The reply will be caught by lineReceived
|
||||
self._send_packet(pck.IMC2PacketAuthPlaintext())
|
||||
|
||||
def lineReceived(self, line):
|
||||
"""
|
||||
IMC2 -> Evennia
|
||||
|
||||
Triggered when text is received from the IMC2 network. Figures out
|
||||
what to do with the packet. This deals with the following
|
||||
|
||||
"""
|
||||
line = line.strip()
|
||||
|
||||
if not self.is_authenticated:
|
||||
# we are not authenticated yet. Deal with this.
|
||||
self._imc_login(line)
|
||||
return
|
||||
|
||||
#logger.log_infomsg("IMC2: RECV> %s" % line)
|
||||
|
||||
# Parse the packet and encapsulate it for easy access
|
||||
packet = pck.IMC2Packet(self.mudname, packet_str=line)
|
||||
|
||||
# Figure out what kind of packet we're dealing with and hand it
|
||||
# off to the correct handler.
|
||||
|
||||
if packet.packet_type == 'is-alive':
|
||||
self.imc2_mudlist.update_mud_from_packet(packet)
|
||||
elif packet.packet_type == 'keepalive-request':
|
||||
# Don't need to check the destination, we only receive these
|
||||
# packets when they are intended for us.
|
||||
self.send_packet(pck.IMC2PacketIsAlive())
|
||||
elif packet.packet_type == 'ice-msg-b':
|
||||
self.data_out(text=line, packettype="broadcast")
|
||||
elif packet.packet_type == 'whois-reply':
|
||||
# handle eventual whois reply
|
||||
self._whois_reply(packet)
|
||||
elif packet.packet_type == 'close-notify':
|
||||
self.imc2_mudlist.remove_mud_from_packet(packet)
|
||||
elif packet.packet_type == 'ice-update':
|
||||
self.imc2_chanlist.update_channel_from_packet(packet)
|
||||
elif packet.packet_type == 'ice-destroy':
|
||||
self.imc2_chanlist.remove_channel_from_packet(packet)
|
||||
elif packet.packet_type == 'tell':
|
||||
# send message to identified player
|
||||
pass
|
||||
|
||||
def data_in(self, text=None, **kwargs):
|
||||
"""
|
||||
Data IMC2 -> Evennia
|
||||
"""
|
||||
text = "bot_data_in " + text
|
||||
self.sessionhandler.data_in(self, text=text, **kwargs)
|
||||
|
||||
def data_out(self, text=None, **kwargs):
|
||||
"""
|
||||
Evennia -> IMC2
|
||||
|
||||
Keywords
|
||||
packet_type:
|
||||
broadcast - send to everyone on IMC channel
|
||||
tell - send a tell (see target keyword)
|
||||
whois - get whois information (see target keyword)
|
||||
sender - used by tell to identify the sender
|
||||
target - key identifier of target to tells or whois. If not
|
||||
given "Unknown" will be used.
|
||||
destination - used by tell to specify mud destination to send to
|
||||
|
||||
"""
|
||||
|
||||
if self.sequence:
|
||||
# This gets incremented with every command.
|
||||
self.sequence += 1
|
||||
|
||||
packet_type = kwargs.get("packet_type", "imcbroadcast")
|
||||
|
||||
if packet_type == "broadcast":
|
||||
# broadcast to everyone on IMC channel
|
||||
|
||||
if text.startswith("bot_data_out"):
|
||||
text = text.split(" ", 1)[1]
|
||||
else:
|
||||
return
|
||||
|
||||
# we remove the extra channel info since imc2 supplies this anyway
|
||||
if ":" in text:
|
||||
header, message = [part.strip() for part in text.split(":", 1)]
|
||||
# Create imc2packet and send it
|
||||
self._send_packet(pck.IMC2PacketIceMsgBroadcasted(self.servername,
|
||||
self.channel,
|
||||
header, text))
|
||||
elif packet_type == "tell":
|
||||
# send an IMC2 tell
|
||||
sender = kwargs.get("sender", self.mudname)
|
||||
target = kwargs.get("target", "Unknown")
|
||||
destination = kwargs.get("destination", "Unknown")
|
||||
self._send_packet(pck.IMC2PacketTell(sender, target, destination, text))
|
||||
|
||||
elif packet_type == "whois":
|
||||
# send a whois request
|
||||
sender = kwargs.get("sender", self.mudname)
|
||||
target = kwargs.get("target", "Unknown")
|
||||
self._send_packet(pck.IMC2PacketWhois(sender, target))
|
||||
|
||||
|
||||
class IMC2BotFactory(protocol.ReconnectingClientFactory):
|
||||
"""
|
||||
Creates instances of the IMC2Protocol. Should really only ever
|
||||
need to create one connection. Tied in via src/server.py.
|
||||
"""
|
||||
initialDelay = 1
|
||||
factor = 1.5
|
||||
maxDelay = 60
|
||||
|
||||
def __init__(self, sessionhandler, uid=None, network=None, channel=None,
|
||||
port=None, mudname=None, client_pwd=None, server_pwd=None):
|
||||
self.uid = uid
|
||||
self.network = network
|
||||
sname, host = network.split(".", 1)
|
||||
self.servername = sname.strip()
|
||||
self.channel = channel
|
||||
self.port = port
|
||||
self.mudname = mudname
|
||||
self.protocol_version = '2'
|
||||
self.client_pwd = client_pwd
|
||||
self.server_pwd = server_pwd
|
||||
self.bot = None
|
||||
self.task_isalive = None
|
||||
self.task_keepalive = None
|
||||
self.task_prune = None
|
||||
self.task_channellist = None
|
||||
|
||||
def buildProtocol(self, addr):
|
||||
"Build the protocol"
|
||||
protocol = IMC2Bot()
|
||||
protocol.factory = self
|
||||
protocol.network = self.network
|
||||
protocol.servername = self.servername
|
||||
protocol.channel = self.channel
|
||||
protocol.mudname = self.mudname
|
||||
protocol.port = self.port
|
||||
return protocol
|
||||
|
||||
def clientConnectionFailed(self, connector, reason):
|
||||
self.retry(connector)
|
||||
|
||||
def clientConnectionLost(self, connector, reason):
|
||||
if not self.bot.stopping:
|
||||
self.retry(connector)
|
||||
|
||||
def start(self):
|
||||
"Connect session to sessionhandler"
|
||||
def errback(fail):
|
||||
logger.log_errmsg(fail.value)
|
||||
|
||||
if self.port:
|
||||
service = internet.TCPClient(self.network, int(self.port), self)
|
||||
self.sessionhandler.portal.services.addService(service)
|
||||
# start tasks
|
||||
self.task_isalive = task.LoopingCall(self.bot._isalive)
|
||||
self.task_keepalive = task.LoopingCall(self.bot._keepalive)
|
||||
self.task_prune = task.LoopingCall(self.bot._prune)
|
||||
self.task_channellist = task.LoopingCall(self.bot._channellist)
|
||||
self.task_isalive.start(900, now=False)
|
||||
self.task_keepalive.start(3500, now=False)
|
||||
self.task_prune.start(1800, now=False)
|
||||
self.task_channellist.start(3600 * 24, now=False)
|
||||
|
||||
1
lib/server/portal/imc2lib/__init__.py
Normal file
1
lib/server/portal/imc2lib/__init__.py
Normal file
|
|
@ -0,0 +1 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
60
lib/server/portal/imc2lib/imc2_ansi.py
Normal file
60
lib/server/portal/imc2lib/imc2_ansi.py
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
"""
|
||||
ANSI parser - this adds colour to text according to
|
||||
special markup strings.
|
||||
|
||||
This is a IMC2 complacent version.
|
||||
"""
|
||||
|
||||
import re
|
||||
from src.utils import ansi
|
||||
|
||||
|
||||
class IMCANSIParser(ansi.ANSIParser):
|
||||
"""
|
||||
This parser is per the IMC2 specification.
|
||||
"""
|
||||
def __init__(self):
|
||||
normal = ansi.ANSI_NORMAL
|
||||
hilite = ansi.ANSI_HILITE
|
||||
self.ansi_map = [
|
||||
(r'~Z', normal), # Random
|
||||
(r'~x', normal + ansi.ANSI_BLACK), # Black
|
||||
(r'~D', hilite + ansi.ANSI_BLACK), # Dark Grey
|
||||
(r'~z', hilite + ansi.ANSI_BLACK),
|
||||
(r'~w', normal + ansi.ANSI_WHITE), # Grey
|
||||
(r'~W', hilite + ansi.ANSI_WHITE), # White
|
||||
(r'~g', normal + ansi.ANSI_GREEN), # Dark Green
|
||||
(r'~G', hilite + ansi.ANSI_GREEN), # Green
|
||||
(r'~p', normal + ansi.ANSI_MAGENTA), # Dark magenta
|
||||
(r'~m', normal + ansi.ANSI_MAGENTA),
|
||||
(r'~M', hilite + ansi.ANSI_MAGENTA), # Magenta
|
||||
(r'~P', hilite + ansi.ANSI_MAGENTA),
|
||||
(r'~c', normal + ansi.ANSI_CYAN), # Cyan
|
||||
(r'~y', normal + ansi.ANSI_YELLOW), # Dark Yellow (brown)
|
||||
(r'~Y', hilite + ansi.ANSI_YELLOW), # Yellow
|
||||
(r'~b', normal + ansi.ANSI_BLUE), # Dark Blue
|
||||
(r'~B', hilite + ansi.ANSI_BLUE), # Blue
|
||||
(r'~C', hilite + ansi.ANSI_BLUE),
|
||||
(r'~r', normal + ansi.ANSI_RED), # Dark Red
|
||||
(r'~R', hilite + ansi.ANSI_RED), # Red
|
||||
|
||||
## Formatting
|
||||
(r'~L', hilite), # Bold/hilite
|
||||
(r'~!', normal), # reset
|
||||
(r'\\r', normal),
|
||||
(r'\\n', ansi.ANSI_RETURN),
|
||||
]
|
||||
# prepare regex matching
|
||||
self.ansi_sub = [(re.compile(sub[0], re.DOTALL), sub[1])
|
||||
for sub in self.ansi_map]
|
||||
# prepare matching ansi codes overall
|
||||
self.ansi_regex = re.compile("\033\[[0-9;]+m")
|
||||
|
||||
ANSI_PARSER = IMCANSIParser()
|
||||
|
||||
|
||||
def parse_ansi(string, strip_ansi=False, parser=ANSI_PARSER):
|
||||
"""
|
||||
Shortcut to use the IMC2 ANSI parser.
|
||||
"""
|
||||
return parser.parse_ansi(string, strip_ansi=strip_ansi)
|
||||
795
lib/server/portal/imc2lib/imc2_packets.py
Normal file
795
lib/server/portal/imc2lib/imc2_packets.py
Normal file
|
|
@ -0,0 +1,795 @@
|
|||
"""
|
||||
IMC2 packets. These are pretty well documented at:
|
||||
http://www.mudbytes.net/index.php?a=articles&s=imc2_protocol
|
||||
|
||||
"""
|
||||
import shlex
|
||||
from django.conf import settings
|
||||
|
||||
class Lexxer(shlex.shlex):
|
||||
"""
|
||||
A lexical parser for interpreting IMC2 packets.
|
||||
"""
|
||||
def __init__(self, packet_str, posix=True):
|
||||
shlex.shlex.__init__(self, packet_str, posix=True)
|
||||
# Single-quotes are notably not present. This is important!
|
||||
self.quotes = '"'
|
||||
self.commenters = ''
|
||||
# This helps denote what constitutes a continuous token.
|
||||
self.wordchars += "~`!@#$%^&*()-_+=[{]}|\\;:',<.>/?"
|
||||
|
||||
class IMC2Packet(object):
|
||||
"""
|
||||
Base IMC2 packet class. This is generally sub-classed, aside from using it
|
||||
to parse incoming packets from the IMC2 network server.
|
||||
"""
|
||||
def __init__(self, mudname=None, packet_str=None):
|
||||
"""
|
||||
Optionally, parse a packet and load it up.
|
||||
"""
|
||||
# The following fields are all according to the basic packet format of:
|
||||
# <sender>@<origin> <sequence> <route> <packet-type> <target>@<destination> <data...>
|
||||
self.sender = None
|
||||
if not mudname:
|
||||
mudname = settings.SERVERNAME
|
||||
self.origin = mudname
|
||||
self.sequence = None
|
||||
self.route = mudname
|
||||
self.packet_type = None
|
||||
self.target = None
|
||||
self.destination = None
|
||||
# Optional data.
|
||||
self.optional_data = {}
|
||||
# Reference to the IMC2Protocol object doing the sending.
|
||||
self.imc2_protocol = None
|
||||
|
||||
if packet_str:
|
||||
# The lexxer handles the double quotes correctly, unlike just
|
||||
# splitting. Spaces throw things off, so shlex handles it
|
||||
# gracefully, ala POSIX shell-style parsing.
|
||||
lex = Lexxer(packet_str)
|
||||
|
||||
# Token counter.
|
||||
counter = 0
|
||||
for token in lex:
|
||||
if counter == 0:
|
||||
# This is the sender@origin token.
|
||||
sender_origin = token
|
||||
split_sender_origin = sender_origin.split('@')
|
||||
self.sender = split_sender_origin[0].strip()
|
||||
self.origin = split_sender_origin[1]
|
||||
elif counter == 1:
|
||||
# Numeric time-based sequence.
|
||||
self.sequence = token
|
||||
elif counter == 2:
|
||||
# Packet routing info.
|
||||
self.route = token
|
||||
elif counter == 3:
|
||||
# Packet type string.
|
||||
self.packet_type = token
|
||||
elif counter == 4:
|
||||
# Get values for the target and destination attributes.
|
||||
target_destination = token
|
||||
split_target_destination = target_destination.split('@')
|
||||
self.target = split_target_destination[0]
|
||||
try:
|
||||
self.destination = split_target_destination[1]
|
||||
except IndexError:
|
||||
# There is only one element to the target@dest segment
|
||||
# of the packet. Wipe the target and move the captured
|
||||
# value to the destination attrib.
|
||||
self.target = '*'
|
||||
self.destination = split_target_destination[0]
|
||||
elif counter > 4:
|
||||
# Populate optional data.
|
||||
try:
|
||||
key, value = token.split('=', 1)
|
||||
self.optional_data[key] = value
|
||||
except ValueError:
|
||||
# Failed to split on equal sign, disregard.
|
||||
pass
|
||||
# Increment and continue to the next token (if applicable)
|
||||
counter += 1
|
||||
|
||||
def __str__(self):
|
||||
retval = """
|
||||
--IMC2 package (%s)
|
||||
Sender: %s
|
||||
Origin: %s
|
||||
Sequence: %s
|
||||
Route: %s
|
||||
Type: %s
|
||||
Target: %s
|
||||
Dest.: %s
|
||||
Data:
|
||||
%s
|
||||
------------------------""" % (self.packet_type, self.sender,
|
||||
self.origin, self.sequence,
|
||||
self.route, self.packet_type,
|
||||
self.target, self.destination,
|
||||
"\n ".join(["%s: %s" % items for items in self.optional_data.items()]))
|
||||
return retval.strip()
|
||||
|
||||
def _get_optional_data_string(self):
|
||||
"""
|
||||
Generates the optional data string to tack on to the end of the packet.
|
||||
"""
|
||||
if self.optional_data:
|
||||
data_string = ''
|
||||
for key, value in self.optional_data.items():
|
||||
# Determine the number of words in this value.
|
||||
words = len(str(value).split(' '))
|
||||
# Anything over 1 word needs double quotes.
|
||||
if words > 1:
|
||||
value = '"%s"' % (value,)
|
||||
data_string += '%s=%s ' % (key, value)
|
||||
return data_string.strip()
|
||||
else:
|
||||
return ''
|
||||
|
||||
def _get_sender_name(self):
|
||||
"""
|
||||
Calculates the sender name to be sent with the packet.
|
||||
"""
|
||||
if self.sender == '*':
|
||||
# Some packets have no sender.
|
||||
return '*'
|
||||
elif str(self.sender).isdigit():
|
||||
return self.sender
|
||||
elif type(self.sender) in [type(u""),type(str())]:
|
||||
#this is used by e.g. IRC where no user object is present.
|
||||
return self.sender.strip().replace(' ', '_')
|
||||
elif self.sender:
|
||||
# Player object.
|
||||
name = self.sender.get_name(fullname=False, show_dbref=False,
|
||||
show_flags=False,
|
||||
no_ansi=True)
|
||||
# IMC2 does not allow for spaces.
|
||||
return name.strip().replace(' ', '_')
|
||||
else:
|
||||
# None value. Do something or other.
|
||||
return 'Unknown'
|
||||
|
||||
def assemble(self, mudname=None, client_pwd=None, server_pwd=None):
|
||||
"""
|
||||
Assembles the packet and returns the ready-to-send string.
|
||||
Note that the arguments are not used, they are there for
|
||||
consistency across all packets.
|
||||
"""
|
||||
self.sequence = self.imc2_protocol.sequence
|
||||
packet = "%s@%s %s %s %s %s@%s %s\n" % (
|
||||
self._get_sender_name(),
|
||||
self.origin,
|
||||
self.sequence,
|
||||
self.route,
|
||||
self.packet_type,
|
||||
self.target,
|
||||
self.destination,
|
||||
self._get_optional_data_string())
|
||||
return packet.strip()
|
||||
|
||||
|
||||
class IMC2PacketAuthPlaintext(object):
|
||||
"""
|
||||
IMC2 plain-text authentication packet. Auth packets are strangely
|
||||
formatted, so this does not sub-class IMC2Packet. The SHA and plain text
|
||||
auth packets are the two only non-conformers.
|
||||
|
||||
CLIENT Sends:
|
||||
PW <mudname> <clientpw> version=<version#> autosetup <serverpw> (SHA256)
|
||||
|
||||
Optional Arguments( required if using the specified authentication method:
|
||||
(SHA256) The literal string: SHA256. This is sent to notify the server
|
||||
that the MUD is SHA256-Enabled. All future logins from this
|
||||
client will be expected in SHA256-AUTH format if the server
|
||||
supports it.
|
||||
"""
|
||||
def assemble(self, mudname=None, client_pwd=None, server_pwd=None):
|
||||
"""
|
||||
This is one of two strange packets, just assemble the packet manually
|
||||
and go.
|
||||
"""
|
||||
return 'PW %s %s version=2 autosetup %s\n' %(mudname, client_pwd, server_pwd)
|
||||
|
||||
|
||||
class IMC2PacketKeepAliveRequest(IMC2Packet):
|
||||
"""
|
||||
Description:
|
||||
This packet is sent by a MUD to trigger is-alive packets from other MUDs.
|
||||
This packet is usually followed by the sending MUD's own is-alive packet.
|
||||
It is used in the filling of a client's MUD list, thus any MUD that doesn't
|
||||
respond with an is-alive isn't marked as online on the sending MUD's
|
||||
mudlist.
|
||||
|
||||
Data:
|
||||
(none)
|
||||
|
||||
Example of a received keepalive-request:
|
||||
*@YourMUD 1234567890 YourMUD!Hub1 keepalive-request *@*
|
||||
|
||||
Example of a sent keepalive-request:
|
||||
*@YourMUD 1234567890 YourMUD keepalive-request *@*
|
||||
"""
|
||||
def __init__(self):
|
||||
super(IMC2PacketKeepAliveRequest, self).__init__()
|
||||
self.sender = '*'
|
||||
self.packet_type = 'keepalive-request'
|
||||
self.target = '*'
|
||||
self.destination = '*'
|
||||
|
||||
|
||||
class IMC2PacketIsAlive(IMC2Packet):
|
||||
"""
|
||||
Description:
|
||||
This packet is the reply to a keepalive-request packet. It is responsible
|
||||
for filling a client's mudlist with the information about other MUDs on the
|
||||
network.
|
||||
|
||||
Data:
|
||||
versionid=<string>
|
||||
Where <string> is the text version ID of the client. ("IMC2 4.5 MUD-Net")
|
||||
|
||||
url=<string>
|
||||
Where <string> is the proper URL of the client. (http://www.domain.com)
|
||||
|
||||
host=<string>
|
||||
Where <string> is the telnet address of the MUD. (telnet://domain.com)
|
||||
|
||||
port=<int>
|
||||
Where <int> is the telnet port of the MUD.
|
||||
|
||||
(These data fields are not sent by the MUD, they are added by the server.)
|
||||
networkname=<string>
|
||||
Where <string> is the network name that the MUD/server is on. ("MyNetwork")
|
||||
|
||||
sha256=<int>
|
||||
This is an optional tag that denotes the SHA-256 capabilities of a
|
||||
MUD or server.
|
||||
|
||||
Example of a received is-alive:
|
||||
*@SomeMUD 1234567890 SomeMUD!Hub2 is-alive *@YourMUD versionid="IMC2 4.5 MUD-Net" url="http://www.domain.com" networkname="MyNetwork" sha256=1 host=domain.com port=5500
|
||||
|
||||
Example of a sent is-alive:
|
||||
*@YourMUD 1234567890 YourMUD is-alive *@* versionid="IMC2 4.5 MUD-Net" url="http://www.domain.com" host=domain.com port=5500
|
||||
"""
|
||||
def __init__(self):
|
||||
super(IMC2PacketIsAlive, self).__init__()
|
||||
self.sender = '*'
|
||||
self.packet_type = 'is-alive'
|
||||
self.target = '*'
|
||||
self.destination = '*'
|
||||
self.optional_data = {'versionid': 'Evennia IMC2',
|
||||
'url': '"http://www.evennia.com"',
|
||||
'host': 'test.com',
|
||||
'port': '5555'}
|
||||
|
||||
|
||||
class IMC2PacketIceRefresh(IMC2Packet):
|
||||
"""
|
||||
Description:
|
||||
This packet is sent by the MUD to request data about the channels on the
|
||||
network. Servers with channels reply with an ice-update packet for each
|
||||
channel they control. The usual target for this packet is IMC@$.
|
||||
|
||||
Data:
|
||||
(none)
|
||||
|
||||
Example:
|
||||
*@YourMUD 1234567890 YourMUD!Hub1 ice-refresh IMC@$
|
||||
"""
|
||||
def __init__(self):
|
||||
super(IMC2PacketIceRefresh, self).__init__()
|
||||
self.sender = '*'
|
||||
self.packet_type = 'ice-refresh'
|
||||
self.target = 'IMC'
|
||||
self.destination = '$'
|
||||
|
||||
|
||||
class IMC2PacketIceUpdate(IMC2Packet):
|
||||
"""
|
||||
Description:
|
||||
A server returns this packet with the data of a channel when prompted with
|
||||
an ice-refresh request.
|
||||
|
||||
Data:
|
||||
channel=<string>
|
||||
The channel's network name in the format of ServerName:ChannelName
|
||||
|
||||
owner=<string>
|
||||
The Name@MUD of the channel's owner
|
||||
|
||||
operators=<string>
|
||||
A space-seperated list of the Channel's operators, (format: Person@MUD)
|
||||
|
||||
policy=<string>
|
||||
The policy is either "open" or "private" with no quotes.
|
||||
|
||||
invited=<string>
|
||||
The space-seperated list of invited User@MUDs, only valid for a
|
||||
"private" channel.
|
||||
|
||||
excluded=<string>
|
||||
The space-seperated list of banned User@MUDs, only valid for "open"
|
||||
channels.
|
||||
|
||||
level=<string> The default level of the channel: Admin, Imp, Imm,
|
||||
Mort, or None
|
||||
|
||||
localname=<string> The suggested local name of the channel.
|
||||
|
||||
Examples:
|
||||
|
||||
Open Policy:
|
||||
ICE@Hub1 1234567890 Hub1!Hub2 ice-update *@YourMUD channel=Hub1:ichat owner=Imm@SomeMUD operators=Other@SomeMUD policy=open excluded="Flamer@badMUD Jerk@dirtyMUD" level=Imm localname=ichat
|
||||
|
||||
Private Policy:
|
||||
ICE@Hub1 1234567890 Hub1!Hub2 ice-update *@YourMUD channel=Hub1:secretchat owner=Imm@SomeMUD operators=Other@SomeMUD policy=private invited="SpecialDude@OtherMUD CoolDude@WeirdMUD" level=Mort localname=schat
|
||||
"""
|
||||
pass
|
||||
|
||||
|
||||
class IMC2PacketIceMsgRelayed(IMC2Packet):
|
||||
"""
|
||||
Description:
|
||||
The -r in this ice-msg packet means it was relayed. This, along with the
|
||||
ice-msg-p packet, are used with private policy channels. The 'r' stands
|
||||
for 'relay'. All incoming channel messages are from ICE@<server>, where
|
||||
<server> is the server hosting the channel.
|
||||
|
||||
Data:
|
||||
realfrom=<string>
|
||||
The User@MUD the message came from.
|
||||
|
||||
channel=<string>
|
||||
The Server:Channel the message is intended to be displayed on.
|
||||
|
||||
text=<string>
|
||||
The message text.
|
||||
|
||||
emote=<int>
|
||||
An integer value designating emotes. 0 for no emote, 1 for an emote,
|
||||
and 2 for a social.
|
||||
|
||||
Examples:
|
||||
ICE@Hub1 1234567890 Hub1!Hub2 ice-msg-r *@YourMUD realfrom=You@YourMUD channel=hub1:secret text="Aha! I got it!" emote=0
|
||||
|
||||
ICE@Hub1 1234567890 Hub1!Hub2 ice-msg-r *@YourMUD realfrom=You@YourMUD channel=hub1:secret text=Ahh emote=0
|
||||
|
||||
ICE@Hub1 1234567890 Hub1!Hub2 ice-msg-r *@YourMUD realfrom=You@YourMUD channel=hub1:secret text="grins evilly." emote=1
|
||||
|
||||
ICE@Hub1 1234567890 Hub1!Hub2 ice-msg-r *@YourMUD realfrom=You@YourMUD channel=hub1:secret text="You@YourMUD grins evilly!" emote=2
|
||||
"""
|
||||
pass
|
||||
|
||||
|
||||
class IMC2PacketIceMsgPrivate(IMC2Packet):
|
||||
"""
|
||||
Description:
|
||||
This packet is sent when a player sends a message to a private channel.
|
||||
This packet should never be seen as incoming to a client. The target of
|
||||
this packet should be IMC@<server> of the server hosting the channel.
|
||||
|
||||
Data:
|
||||
channel=<string>
|
||||
The Server:Channel the message is intended to be displayed on.
|
||||
|
||||
text=<string>
|
||||
The message text.
|
||||
|
||||
emote=<int>
|
||||
An integer value designating emotes. 0 for no emote, 1 for an emote,
|
||||
and 2 for a social.
|
||||
|
||||
echo=<int>
|
||||
Tells the server to echo the message back to the sending MUD. This is only
|
||||
seen on out-going messages.
|
||||
|
||||
Examples:
|
||||
You@YourMUD 1234567890 YourMUD ice-msg-p IMC@Hub1 channel=Hub1:secret text="Ahh! I got it!" emote=0 echo=1
|
||||
You@YourMUD 1234567890 YourMUD ice-msg-p IMC@Hub1 channel=Hub1:secret text=Ahh! emote=0 echo=1
|
||||
You@YourMUD 1234567890 YourMUD ice-msg-p IMC@Hub1 channel=Hub1:secret text="grins evilly." emote=1 echo=1
|
||||
You@YourMUD 1234567890 YourMUD ice-msg-p IMC@Hub1 channel=Hub1:secret text="You@YourMUD grins evilly." emote=2 echo=1
|
||||
"""
|
||||
pass
|
||||
|
||||
|
||||
class IMC2PacketIceMsgBroadcasted(IMC2Packet):
|
||||
"""
|
||||
Description:
|
||||
This is the packet used to chat on open policy channels. When sent from a
|
||||
MUD, it is broadcasted across the network. Other MUDs receive it in-tact
|
||||
as it was sent by the originating MUD. The server that hosts the channel
|
||||
sends the packet back to the originating MUD as an 'echo' by removing the
|
||||
"echo=1" and attaching the "sender=Person@MUD" data field.
|
||||
|
||||
Data:
|
||||
channel=<string>
|
||||
The Server:Channel the message is intended to be displayed on.
|
||||
|
||||
text=<string>
|
||||
The message text.
|
||||
|
||||
emote=<int>
|
||||
An integer value designating emotes. 0 for no emote, 1 for an emote,
|
||||
and 2 for a social.
|
||||
|
||||
*echo=<int>
|
||||
This stays on broadcasted messages. It tells the channel's server to
|
||||
relay an echo back.
|
||||
|
||||
*sender=<string>
|
||||
The hosting server replaces "echo=1" with this when sending the echo back
|
||||
to the originating MUD.
|
||||
|
||||
Examples:
|
||||
(See above for emote/social examples as they are pretty much the same)
|
||||
|
||||
Return Echo Packet:
|
||||
You-YourMUD@Hub1 1234567890 Hub1 ice-msg-b *@YourMUD text=Hi! channel=Hub1:ichat sender=You@YourMUD emote=0
|
||||
|
||||
Broadcasted Packet:
|
||||
You@YourMUD 1234567890 YourMUD!Hub1 ice-msg-b *@* channel=Hub1:ichat text=Hi! emote=0 echo=1
|
||||
"""
|
||||
def __init__(self, server, channel, pobject, message):
|
||||
"""
|
||||
Args:
|
||||
server: (String) Server name the channel resides on (obs - this is
|
||||
e.g. Server01, not the full network name!)
|
||||
channel: (String) Name of the IMC2 channel.
|
||||
pobject: (Object) Object sending the message.
|
||||
message: (String) Message to send.
|
||||
"""
|
||||
super(IMC2PacketIceMsgBroadcasted, self).__init__()
|
||||
self.sender = pobject
|
||||
self.packet_type = 'ice-msg-b'
|
||||
self.target = '*'
|
||||
self.destination = '*'
|
||||
self.optional_data = {'channel': '%s:%s' % (server, channel),
|
||||
'text': message,
|
||||
'emote': 0,
|
||||
'echo': 1}
|
||||
|
||||
|
||||
class IMC2PacketUserCache(IMC2Packet):
|
||||
"""
|
||||
Description:
|
||||
Sent by a MUD with a new IMC2-able player or when a player's gender changes,
|
||||
this packet contains only the gender for data. The packet's origination
|
||||
should be the Player@MUD.
|
||||
|
||||
Data:
|
||||
gender=<int> 0 is male, 1 is female, 2 is anything else such as neuter.
|
||||
Will be referred to as "it".
|
||||
|
||||
Example:
|
||||
Dude@someMUD 1234567890 SomeMUD!Hub2!Hub1 user-cache *@* gender=0
|
||||
"""
|
||||
pass
|
||||
|
||||
|
||||
class IMC2PacketUserCacheRequest(IMC2Packet):
|
||||
"""
|
||||
Description:
|
||||
The MUD sends this packet out when making a request for the user-cache
|
||||
information of the user included in the data part of the packet.
|
||||
|
||||
Data:
|
||||
user=<string> The Person@MUD whose data the MUD is seeking.
|
||||
|
||||
Example:
|
||||
*@YourMUD 1234567890 YourMUD user-cache-request *@SomeMUD user=Dude@SomeMUD
|
||||
"""
|
||||
pass
|
||||
|
||||
|
||||
class IMC2PacketUserCacheReply(IMC2Packet):
|
||||
"""
|
||||
Description:
|
||||
A reply to the user-cache-request packet. It contains the user and gender
|
||||
for the user.
|
||||
|
||||
Data:
|
||||
user=<string>
|
||||
The Person@MUD whose data the MUD requested.
|
||||
|
||||
gender=<int>
|
||||
The gender of the Person@MUD in the 'user' field.
|
||||
|
||||
Example:
|
||||
*@someMUD 1234567890 SomeMUD!Hub2!Hub1 user-cache-reply *@YourMUD user=Dude@SomeMUD gender=0
|
||||
"""
|
||||
pass
|
||||
|
||||
|
||||
class IMC2PacketTell(IMC2Packet):
|
||||
"""
|
||||
Description:
|
||||
This packet is used to communicate private messages between users on MUDs
|
||||
across the network.
|
||||
|
||||
Data:
|
||||
text=<string> Message text
|
||||
isreply=<int> Two settings: 1 denotes a reply, 2 denotes a tell social.
|
||||
|
||||
Example:
|
||||
|
||||
Originating:
|
||||
You@YourMUD 1234567890 YourMUD tell Dude@SomeMUD text="Having fun?"
|
||||
|
||||
Reply from Dude:
|
||||
Dude@SomeMUD 1234567890 SomeMUD!Hub1 tell You@YourMUD text="Yeah, this is cool!" isreply=1
|
||||
"""
|
||||
def __init__(self, pobject, target, destination, message):
|
||||
super(IMC2PacketTell, self).__init__()
|
||||
self.sender = pobject
|
||||
self.packet_type = "tell"
|
||||
self.target = target
|
||||
self.destination = destination
|
||||
self.optional_data = {"text": message,
|
||||
"isreply":None}
|
||||
|
||||
def assemble(self, mudname=None, client_pwd=None, server_pwd=None):
|
||||
self.sequence = self.imc2_protocol.sequence
|
||||
#self.route = "%s!%s" % (self.origin, self.imc2_protocol.factory.servername.capitalize())
|
||||
return '''"%s@%s %s %s tell %s@%s text="%s"''' % (self.sender, self.origin, self.sequence,
|
||||
self.route, self.target, self.destination,
|
||||
self.optional_data.get("text","NO TEXT GIVEN"))
|
||||
|
||||
|
||||
class IMC2PacketEmote(IMC2Packet):
|
||||
"""
|
||||
Description:
|
||||
This packet seems to be sent by servers when notifying the network of a new
|
||||
channel or the destruction of a channel.
|
||||
|
||||
Data:
|
||||
channel=<int>
|
||||
Unsure of what this means. The channel seen in both creation and
|
||||
destruction packets is 15.
|
||||
|
||||
level=<int>
|
||||
I am assuming this is the permission level of the sender. In both
|
||||
creation and destruction messages, this is -1.
|
||||
|
||||
text=<string>
|
||||
This is the message to be sent to the users.
|
||||
|
||||
Examples:
|
||||
ICE@Hub1 1234567890 Hub1 emote *@* channel=15 level=-1 text="the
|
||||
channel called hub1:test has been destroyed by You@YourMUD."
|
||||
"""
|
||||
pass
|
||||
|
||||
|
||||
class IMC2PacketRemoteAdmin(IMC2Packet):
|
||||
"""
|
||||
Description:
|
||||
This packet is used in remote server administration. Please note that
|
||||
SHA-256 Support is *required* for a client to use this feature. The command
|
||||
can vary, in fact this very packet is highly dependant on the server it's
|
||||
being directed to. In most cases, sending the 'list' command will have a
|
||||
remote-admin enabled server send you the list of commands it will accept.
|
||||
|
||||
Data:
|
||||
command=<string>
|
||||
The command being sent to the server for processing.
|
||||
|
||||
data=<string>
|
||||
Data associated with the command. This is not always required.
|
||||
|
||||
hash=<string>
|
||||
The SHA-256 hash that is verified by the server. This hash is generated in
|
||||
the same manner as an authentication packet.
|
||||
|
||||
Example:
|
||||
You@YourMUD 1234567890 YourMUD remote-admin IMC@Hub1 command=list hash=<hash goes here>
|
||||
"""
|
||||
pass
|
||||
|
||||
|
||||
class IMC2PacketIceCmd(IMC2Packet):
|
||||
"""
|
||||
Description:
|
||||
Used for remote channel administration. In most cases, one must be listed
|
||||
as a channel creator on the target server in order to do much with this
|
||||
packet. Other cases include channel operators.
|
||||
|
||||
Data:
|
||||
channel=<string>
|
||||
The target server:channel for the command.
|
||||
|
||||
command=<string>
|
||||
The command to be processed.
|
||||
|
||||
data=<string>
|
||||
Data associated with the command. This is not always required.
|
||||
|
||||
Example:
|
||||
You@YourMUD 1234567890 YourMUD ice-cmd IMC@hub1 channel=hub1:ichat command=list
|
||||
"""
|
||||
pass
|
||||
|
||||
|
||||
class IMC2PacketDestroy(IMC2Packet):
|
||||
"""
|
||||
Description:
|
||||
Sent by a server to indicate the destruction of a channel it hosted.
|
||||
The mud should remove this channel from its local configuration.
|
||||
|
||||
Data:
|
||||
channel=<string> The server:channel being destroyed.
|
||||
"""
|
||||
pass
|
||||
|
||||
|
||||
class IMC2PacketWho(IMC2Packet):
|
||||
"""
|
||||
Description:
|
||||
A seemingly mutli-purpose information-requesting packet. The istats
|
||||
packet currently only works on servers, or at least that's the case on
|
||||
MUD-Net servers. The 'finger' type takes a player name in addition to the
|
||||
type name.
|
||||
|
||||
Example: "finger Dude". The 'who' and 'info' types take no argument.
|
||||
The MUD is responsible for building the reply text sent in the who-reply
|
||||
packet.
|
||||
|
||||
Data:
|
||||
type=<string> Types: who, info, "finger <name>", istats (server only)
|
||||
|
||||
Example:
|
||||
Dude@SomeMUD 1234567890 SomeMUD!Hub1 who *@YourMUD type=who
|
||||
"""
|
||||
pass
|
||||
|
||||
|
||||
class IMC2PacketWhoReply(IMC2Packet):
|
||||
"""
|
||||
Description:
|
||||
The multi-purpose reply to the multi-purpose information-requesting 'who'
|
||||
packet. The MUD is responsible for building the return data, including the
|
||||
format of it. The mud can use the permission level sent in the original who
|
||||
packet to filter the output. The example below is the MUD-Net format.
|
||||
|
||||
Data:
|
||||
text=<string> The formatted reply to a 'who' packet.
|
||||
|
||||
Additional Notes:
|
||||
The example below is for the who list packet. The same construction would
|
||||
go into formatting the other types of who packets.
|
||||
|
||||
Example:
|
||||
*@YourMUD 1234567890 YourMUD who-reply Dude@SomeMUD text="\n\r~R-=< ~WPlayers on YourMUD ~R>=-\n\r ~Y-=< ~Wtelnet://yourmud.domain.com:1234 ~Y>=-\n\r\n\r~B--------------------------------=< ~WPlayers ~B>=---------------------------------\n\r\n\r ~BPlayer ~z<--->~G Mortal the Toy\n\r\n\r~R-------------------------------=< ~WImmortals ~R>=--------------------------------\n\r\n\r ~YStaff ~z<--->~G You the Immortal\n\r\n\r~Y<~W2 Players~Y> ~Y<~WHomepage: http://www.yourmud.com~Y> <~W 2 Max Since Reboot~Y>\n\r~Y<~W3 logins since last reboot on Tue Feb 24, 2004 6:55:59 PM EST~Y>"
|
||||
"""
|
||||
pass
|
||||
|
||||
|
||||
class IMC2PacketWhois(IMC2Packet):
|
||||
"""
|
||||
Description:
|
||||
Sends a request to the network for the location of the specified player.
|
||||
|
||||
Data:
|
||||
level=<int> The permission level of the person making the request.
|
||||
|
||||
Example:
|
||||
You@YourMUD 1234567890 YourMUD whois dude@* level=5
|
||||
"""
|
||||
def __init__(self, pobject_id, whois_target):
|
||||
super(IMC2PacketWhois, self).__init__()
|
||||
# Use the dbref, it's easier to trace back for the whois-reply.
|
||||
self.sender = pobject_id
|
||||
self.packet_type = 'whois'
|
||||
self.target = whois_target
|
||||
self.destination = '*'
|
||||
self.optional_data = {'level': '5'}
|
||||
|
||||
|
||||
class IMC2PacketWhoisReply(IMC2Packet):
|
||||
"""
|
||||
Description:
|
||||
The reply to a whois packet. The MUD is responsible for building and formatting
|
||||
the text sent back to the requesting player, and can use the permission level
|
||||
sent in the original whois packet to filter or block the response.
|
||||
|
||||
Data:
|
||||
text=<string> The whois text.
|
||||
|
||||
Example:
|
||||
*@SomeMUD 1234567890 SomeMUD!Hub1 whois-reply You@YourMUD text="~RIMC Locate: ~YDude@SomeMUD: ~cOnline.\n\r"
|
||||
"""
|
||||
pass
|
||||
|
||||
|
||||
class IMC2PacketBeep(IMC2Packet):
|
||||
"""
|
||||
Description:
|
||||
Sends out a beep packet to the Player@MUD. The client receiving this should
|
||||
then send a bell-character to the target player to 'beep' them.
|
||||
|
||||
Example:
|
||||
You@YourMUD 1234567890 YourMUD beep dude@somemud
|
||||
"""
|
||||
pass
|
||||
|
||||
|
||||
class IMC2PacketIceChanWho(IMC2Packet):
|
||||
"""
|
||||
Description:
|
||||
Sends a request to the specified MUD or * to list all the users listening
|
||||
to the specified channel.
|
||||
|
||||
Data:
|
||||
level=<int>
|
||||
Sender's permission level.
|
||||
|
||||
channel=<string>
|
||||
The server:chan name of the channel.
|
||||
|
||||
lname=<string>
|
||||
The localname of the channel.
|
||||
|
||||
Example:
|
||||
You@YourMUD 1234567890 YourMUD ice-chan-who somemud level=5 channel=Hub1:ichat lname=ichat
|
||||
"""
|
||||
pass
|
||||
|
||||
|
||||
class IMC2PacketIceChanWhoReply(IMC2Packet):
|
||||
"""
|
||||
Description:
|
||||
This is the reply packet for an ice-chan-who. The MUD is responsible for
|
||||
creating and formatting the list sent back in the 'list' field. The
|
||||
permission level sent in the original ice-chan-who packet can be used to
|
||||
filter or block the response.
|
||||
|
||||
Data:
|
||||
channel=<string>
|
||||
The server:chan of the requested channel.
|
||||
|
||||
list=<string>
|
||||
The formatted list of local listeners for that MUD.
|
||||
|
||||
Example:
|
||||
*@SomeMUD 1234567890 SomeMUD!Hub1 ice-chan-whoreply You@YourMUD channel=Hub1:ichat list="The following people are listening to ichat on SomeMUD:\n\r\n\rDude\n\r"
|
||||
"""
|
||||
pass
|
||||
|
||||
|
||||
class IMC2PacketLaston(IMC2Packet):
|
||||
"""
|
||||
Description:
|
||||
This packet queries the server the mud is connected to to find out when a
|
||||
specified user was last seen by the network on a public channel.
|
||||
|
||||
Data:
|
||||
username=<string> The user, user@mud, or "all" being queried. Responses
|
||||
to this packet will be sent by the server in the form of a series of tells.
|
||||
|
||||
Example: User@MUD 1234567890 MUD imc-laston SERVER username=somenamehere
|
||||
"""
|
||||
pass
|
||||
|
||||
|
||||
class IMC2PacketCloseNotify(IMC2Packet):
|
||||
"""
|
||||
Description:
|
||||
This packet alerts the network when a server or MUD has disconnected. The
|
||||
server hosting the server or MUD is responsible for sending this packet
|
||||
out across the network. Clients need only process the packet to remove the
|
||||
disconnected MUD from their MUD list (or mark it as Disconnected).
|
||||
|
||||
Data:
|
||||
host=<string>
|
||||
The MUD or server that has disconnected from the network.
|
||||
|
||||
Example:
|
||||
*@Hub2 1234567890 Hub2!Hub1 close-notify *@* host=DisconnMUD
|
||||
"""
|
||||
pass
|
||||
|
||||
if __name__ == "__main__":
|
||||
packstr = "Kayle@MW 1234567 MW!Server02!Server01 ice-msg-b *@* channel=Server01:ichat text=\"*they're going woot\" emote=0 echo=1"
|
||||
packstr = "*@Lythelian 1234567 Lythelian!Server01 is-alive *@* versionid=\"Tim's LPC IMC2 client 30-Jan-05 / Dead Souls integrated\" networkname=Mudbytes url=http://dead-souls.net host=70.32.76.142 port=6666 sha256=0"
|
||||
print IMC2Packet(packstr)
|
||||
|
||||
125
lib/server/portal/irc.py
Normal file
125
lib/server/portal/irc.py
Normal file
|
|
@ -0,0 +1,125 @@
|
|||
"""
|
||||
This connects to an IRC network/channel and launches an 'bot' onto it.
|
||||
The bot then pipes what is being said between the IRC channel and one or
|
||||
more Evennia channels.
|
||||
"""
|
||||
|
||||
from twisted.application import internet
|
||||
from twisted.words.protocols import irc
|
||||
from twisted.internet import protocol
|
||||
from src.server.session import Session
|
||||
from src.utils import logger
|
||||
|
||||
|
||||
# IRC bot
|
||||
|
||||
class IRCBot(irc.IRCClient, Session):
|
||||
"""
|
||||
An IRC bot that tracks actitivity in a channel as well
|
||||
as sends text to it when prompted
|
||||
"""
|
||||
lineRate = 1
|
||||
|
||||
# assigned by factory at creation
|
||||
|
||||
nickname = None
|
||||
logger = None
|
||||
factory = None
|
||||
channel = None
|
||||
|
||||
def signedOn(self):
|
||||
"""
|
||||
This is called when we successfully connect to
|
||||
the network. We make sure to now register with
|
||||
the game as a full session.
|
||||
"""
|
||||
self.join(self.channel)
|
||||
self.stopping = False
|
||||
self.factory.bot = self
|
||||
address = "%s@%s" % (self.channel, self.network)
|
||||
self.init_session("ircbot", address, self.factory.sessionhandler)
|
||||
# we link back to our bot and log in
|
||||
self.uid = int(self.factory.uid)
|
||||
self.logged_in = True
|
||||
self.factory.sessionhandler.connect(self)
|
||||
logger.log_infomsg("IRC bot '%s' connected to %s at %s:%s." % (self.nickname, self.channel,
|
||||
self.network, self.port))
|
||||
|
||||
def disconnect(self, reason=None):
|
||||
"""
|
||||
Called by sessionhandler to disconnect this protocol
|
||||
"""
|
||||
print "irc disconnect called!"
|
||||
self.sessionhandler.disconnect(self)
|
||||
self.stopping = True
|
||||
self.transport.loseConnection()
|
||||
|
||||
def privmsg(self, user, channel, msg):
|
||||
"A message was sent to channel"
|
||||
if not msg.startswith('***'):
|
||||
user = user.split('!', 1)[0]
|
||||
self.data_in("bot_data_in %s@%s: %s" % (user, channel, msg))
|
||||
|
||||
def action(self, user, channel, msg):
|
||||
"An action was done in channel"
|
||||
if not msg.startswith('**'):
|
||||
user = user.split('!', 1)[0]
|
||||
self.data_in("bot_data_in %s@%s %s" % (user, channel, msg))
|
||||
|
||||
def data_in(self, text=None, **kwargs):
|
||||
"Data IRC -> Server"
|
||||
self.sessionhandler.data_in(self, text=text, **kwargs)
|
||||
|
||||
def data_out(self, text=None, **kwargs):
|
||||
"Data from server-> IRC"
|
||||
if text.startswith("bot_data_out"):
|
||||
text = text.split(" ", 1)[1]
|
||||
self.say(self.channel, text)
|
||||
|
||||
|
||||
class IRCBotFactory(protocol.ReconnectingClientFactory):
|
||||
"""
|
||||
Creates instances of AnnounceBot, connecting with
|
||||
a staggered increase in delay
|
||||
"""
|
||||
# scaling reconnect time
|
||||
initialDelay = 1
|
||||
factor = 1.5
|
||||
maxDelay = 60
|
||||
|
||||
def __init__(self, sessionhandler, uid=None, botname=None, channel=None, network=None, port=None):
|
||||
"Storing some important protocol properties"
|
||||
self.sessionhandler = sessionhandler
|
||||
self.uid = uid
|
||||
self.nickname = str(botname)
|
||||
self.channel = str(channel)
|
||||
self.network = str(network)
|
||||
self.port = port
|
||||
self.bot = None
|
||||
|
||||
def buildProtocol(self, addr):
|
||||
"Build the protocol and assign it some properties"
|
||||
protocol = IRCBot()
|
||||
protocol.factory = self
|
||||
protocol.nickname = self.nickname
|
||||
protocol.channel = self.channel
|
||||
protocol.network = self.network
|
||||
protocol.port = self.port
|
||||
return protocol
|
||||
|
||||
def startedConnecting(self, connector):
|
||||
"Tracks reconnections for debugging"
|
||||
logger.log_infomsg("(re)connecting to %s" % self.channel)
|
||||
|
||||
def clientConnectionFailed(self, connector, reason):
|
||||
self.retry(connector)
|
||||
|
||||
def clientConnectionLost(self, connector, reason):
|
||||
if not self.bot.stopping:
|
||||
self.retry(connector)
|
||||
|
||||
def start(self):
|
||||
"Connect session to sessionhandler"
|
||||
if self.port:
|
||||
service = internet.TCPClient(self.network, int(self.port), self)
|
||||
self.sessionhandler.portal.services.addService(service)
|
||||
67
lib/server/portal/mccp.py
Normal file
67
lib/server/portal/mccp.py
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
"""
|
||||
|
||||
MCCP - Mud Client Compression Protocol
|
||||
|
||||
This implements the MCCP v2 telnet protocol as per
|
||||
http://tintin.sourceforge.net/mccp/. MCCP allows for the server to
|
||||
compress data when sending to supporting clients, reducing bandwidth
|
||||
by 70-90%.. The compression is done using Python's builtin zlib
|
||||
library. If the client doesn't support MCCP, server sends uncompressed
|
||||
as normal. Note: On modern hardware you are not likely to notice the
|
||||
effect of MCCP unless you have extremely heavy traffic or sits on a
|
||||
terribly slow connection.
|
||||
|
||||
This protocol is implemented by the telnet protocol importing
|
||||
mccp_compress and calling it from its write methods.
|
||||
"""
|
||||
import zlib
|
||||
|
||||
# negotiations for v1 and v2 of the protocol
|
||||
MCCP = chr(86)
|
||||
FLUSH = zlib.Z_SYNC_FLUSH
|
||||
|
||||
|
||||
def mccp_compress(protocol, data):
|
||||
"Handles zlib compression, if applicable"
|
||||
if hasattr(protocol, 'zlib'):
|
||||
return protocol.zlib.compress(data) + protocol.zlib.flush(FLUSH)
|
||||
return data
|
||||
|
||||
|
||||
class Mccp(object):
|
||||
"""
|
||||
Implements the MCCP protocol. Add this to a
|
||||
variable on the telnet protocol to set it up.
|
||||
"""
|
||||
|
||||
def __init__(self, protocol):
|
||||
"""
|
||||
initialize MCCP by storing protocol on
|
||||
ourselves and calling the client to see if
|
||||
it supports MCCP. Sets callbacks to
|
||||
start zlib compression in that case.
|
||||
"""
|
||||
|
||||
self.protocol = protocol
|
||||
self.protocol.protocol_flags['MCCP'] = False
|
||||
# ask if client will mccp, connect callbacks to handle answer
|
||||
self.protocol.will(MCCP).addCallbacks(self.do_mccp, self.no_mccp)
|
||||
|
||||
def no_mccp(self, option):
|
||||
"""
|
||||
Called if client doesn't support mccp or chooses to turn it off
|
||||
"""
|
||||
if hasattr(self.protocol, 'zlib'):
|
||||
del self.protocol.zlib
|
||||
self.protocol.protocol_flags['MCCP'] = False
|
||||
self.protocol.handshake_done()
|
||||
|
||||
def do_mccp(self, option):
|
||||
"""
|
||||
The client supports MCCP. Set things up by
|
||||
creating a zlib compression stream.
|
||||
"""
|
||||
self.protocol.protocol_flags['MCCP'] = True
|
||||
self.protocol.requestNegotiation(MCCP, '')
|
||||
self.protocol.zlib = zlib.compressobj(9)
|
||||
self.protocol.handshake_done()
|
||||
243
lib/server/portal/msdp.py
Normal file
243
lib/server/portal/msdp.py
Normal file
|
|
@ -0,0 +1,243 @@
|
|||
"""
|
||||
|
||||
MSDP (Mud Server Data Protocol)
|
||||
|
||||
This implements the MSDP protocol as per
|
||||
http://tintin.sourceforge.net/msdp/. MSDP manages out-of-band
|
||||
communication between the client and server, for updating health bars
|
||||
etc.
|
||||
|
||||
"""
|
||||
import re
|
||||
from src.utils.utils import to_str
|
||||
|
||||
# MSDP-relevant telnet cmd/opt-codes
|
||||
MSDP = chr(69)
|
||||
MSDP_VAR = chr(1)
|
||||
MSDP_VAL = chr(2)
|
||||
MSDP_TABLE_OPEN = chr(3)
|
||||
MSDP_TABLE_CLOSE = chr(4)
|
||||
MSDP_ARRAY_OPEN = chr(5)
|
||||
MSDP_ARRAY_CLOSE = chr(6)
|
||||
|
||||
IAC = chr(255)
|
||||
SB = chr(250)
|
||||
SE = chr(240)
|
||||
|
||||
force_str = lambda inp: to_str(inp, force_string=True)
|
||||
|
||||
# pre-compiled regexes
|
||||
# returns 2-tuple
|
||||
regex_array = re.compile(r"%s(.*?)%s%s(.*?)%s" % (MSDP_VAR, MSDP_VAL,
|
||||
MSDP_ARRAY_OPEN,
|
||||
MSDP_ARRAY_CLOSE))
|
||||
# returns 2-tuple (may be nested)
|
||||
regex_table = re.compile(r"%s(.*?)%s%s(.*?)%s" % (MSDP_VAR, MSDP_VAL,
|
||||
MSDP_TABLE_OPEN,
|
||||
MSDP_TABLE_CLOSE))
|
||||
regex_var = re.compile(MSDP_VAR)
|
||||
regex_val = re.compile(MSDP_VAL)
|
||||
|
||||
|
||||
# Msdp object handler
|
||||
|
||||
class Msdp(object):
|
||||
"""
|
||||
Implements the MSDP protocol.
|
||||
"""
|
||||
|
||||
def __init__(self, protocol):
|
||||
"""
|
||||
Initiates by storing the protocol
|
||||
on itself and trying to determine
|
||||
if the client supports MSDP.
|
||||
"""
|
||||
self.protocol = protocol
|
||||
self.protocol.protocol_flags['MSDP'] = False
|
||||
self.protocol.negotiationMap[MSDP] = self.msdp_to_evennia
|
||||
self.protocol.will(MSDP).addCallbacks(self.do_msdp, self.no_msdp)
|
||||
self.msdp_reported = {}
|
||||
|
||||
def no_msdp(self, option):
|
||||
"No msdp supported or wanted"
|
||||
self.protocol.handshake_done()
|
||||
|
||||
def do_msdp(self, option):
|
||||
"""
|
||||
Called when client confirms that it can do MSDP.
|
||||
"""
|
||||
self.protocol.protocol_flags['MSDP'] = True
|
||||
self.protocol.handshake_done()
|
||||
|
||||
def evennia_to_msdp(self, cmdname, *args, **kwargs):
|
||||
"""
|
||||
handle return data from cmdname by converting it to
|
||||
a proper msdp structure. data can either be a single value (will be
|
||||
converted to a string), a list (will be converted to an MSDP_ARRAY),
|
||||
or a dictionary (will be converted to MSDP_TABLE).
|
||||
|
||||
OBS - there is no actual use of arrays and tables in the MSDP
|
||||
specification or default commands -- are returns are implemented
|
||||
as simple lists or named lists (our name for them here, these
|
||||
un-bounded structures are not named in the specification). So for
|
||||
now, this routine will not explicitly create arrays nor tables,
|
||||
although there are helper methods ready should it be needed in
|
||||
the future.
|
||||
"""
|
||||
|
||||
def make_table(name, **kwargs):
|
||||
"build a table that may be nested with other tables or arrays."
|
||||
string = MSDP_VAR + force_str(name) + MSDP_VAL + MSDP_TABLE_OPEN
|
||||
for key, val in kwargs.items():
|
||||
if isinstance(val, dict):
|
||||
string += make_table(string, key, **val)
|
||||
elif hasattr(val, '__iter__'):
|
||||
string += make_array(string, key, *val)
|
||||
else:
|
||||
string += MSDP_VAR + force_str(key) + MSDP_VAL + force_str(val)
|
||||
string += MSDP_TABLE_CLOSE
|
||||
return string
|
||||
|
||||
def make_array(name, *args):
|
||||
"build a array. Arrays may not nest tables by definition."
|
||||
string = MSDP_VAR + force_str(name) + MSDP_ARRAY_OPEN
|
||||
string += MSDP_VAL.join(force_str(arg) for arg in args)
|
||||
string += MSDP_ARRAY_CLOSE
|
||||
return string
|
||||
|
||||
def make_list(name, *args):
|
||||
"build a simple list - an array without start/end markers"
|
||||
string = MSDP_VAR + force_str(name)
|
||||
string += MSDP_VAL.join(force_str(arg) for arg in args)
|
||||
return string
|
||||
|
||||
def make_named_list(name, **kwargs):
|
||||
"build a named list - a table without start/end markers"
|
||||
string = MSDP_VAR + force_str(name)
|
||||
for key, val in kwargs.items():
|
||||
string += MSDP_VAR + force_str(key) + MSDP_VAL + force_str(val)
|
||||
return string
|
||||
|
||||
# Default MSDP commands
|
||||
|
||||
print "MSDP outgoing:", cmdname, args, kwargs
|
||||
|
||||
cupper = cmdname.upper()
|
||||
if cupper == "LIST":
|
||||
if args:
|
||||
args = list(args)
|
||||
mode = args.pop(0).upper()
|
||||
self.data_out(make_array(mode, *args))
|
||||
elif cupper == "REPORT":
|
||||
self.data_out(make_list("REPORT", *args))
|
||||
elif cupper == "UNREPORT":
|
||||
self.data_out(make_list("UNREPORT", *args))
|
||||
elif cupper == "RESET":
|
||||
self.data_out(make_list("RESET", *args))
|
||||
elif cupper == "SEND":
|
||||
self.data_out(make_named_list("SEND", **kwargs))
|
||||
else:
|
||||
# return list or named lists.
|
||||
msdp_string = ""
|
||||
if args:
|
||||
msdp_string += make_list(cupper, *args)
|
||||
if kwargs:
|
||||
msdp_string += make_named_list(cupper, **kwargs)
|
||||
self.data_out(msdp_string)
|
||||
|
||||
def msdp_to_evennia(self, data):
|
||||
"""
|
||||
Handle a client's requested negotiation, converting
|
||||
it into a function mapping - either one of the MSDP
|
||||
default functions (LIST, SEND etc) or a custom one
|
||||
in OOB_FUNCS dictionary. command names are case-insensitive.
|
||||
|
||||
varname, var --> mapped to function varname(var)
|
||||
arrayname, array --> mapped to function arrayname(*array)
|
||||
tablename, table --> mapped to function tablename(**table)
|
||||
|
||||
Note: Combinations of args/kwargs to one function is not supported
|
||||
in this implementation (it complicates the code for limited
|
||||
gain - arrayname(*array) is usually as complex as anyone should
|
||||
ever need to go anyway (I hope!).
|
||||
|
||||
"""
|
||||
tables = {}
|
||||
arrays = {}
|
||||
variables = {}
|
||||
|
||||
if hasattr(data, "__iter__"):
|
||||
data = "".join(data)
|
||||
|
||||
#logger.log_infomsg("MSDP SUBNEGOTIATION: %s" % data)
|
||||
|
||||
for key, table in regex_table.findall(data):
|
||||
tables[key] = {}
|
||||
for varval in regex_var.split(table):
|
||||
parts = regex_val.split(varval)
|
||||
tables[key].expand({parts[0]: tuple(parts[1:]) if len(parts) > 1 else ("",)})
|
||||
for key, array in regex_array.findall(data):
|
||||
arrays[key] = []
|
||||
for val in regex_val.split(array):
|
||||
arrays[key].append(val)
|
||||
arrays[key] = tuple(arrays[key])
|
||||
for varval in regex_var.split(regex_array.sub("", regex_table.sub("", data))):
|
||||
# get remaining varvals after cleaning away tables/arrays
|
||||
parts = regex_val.split(varval)
|
||||
variables[parts[0].upper()] = tuple(parts[1:]) if len(parts) > 1 else ("", )
|
||||
|
||||
#print "MSDP: table, array, variables:", tables, arrays, variables
|
||||
|
||||
# all variables sent through msdp to Evennia are considered commands
|
||||
# with arguments. There are three forms of commands possible
|
||||
# through msdp:
|
||||
#
|
||||
# VARNAME VAR -> varname(var)
|
||||
# ARRAYNAME VAR VAL VAR VAL VAR VAL ENDARRAY -> arrayname(val,val,val)
|
||||
# TABLENAME TABLE VARNAME VAL VARNAME VAL ENDTABLE ->
|
||||
# tablename(varname=val, varname=val)
|
||||
#
|
||||
|
||||
# default MSDP functions
|
||||
if "LIST" in variables:
|
||||
self.data_in("list", *variables.pop("LIST"))
|
||||
if "REPORT" in variables:
|
||||
self.data_in("report", *variables.pop("REPORT"))
|
||||
if "REPORT" in arrays:
|
||||
self.data_in("report", *(arrays.pop("REPORT")))
|
||||
if "UNREPORT" in variables:
|
||||
self.data_in("unreport", *(arrays.pop("UNREPORT")))
|
||||
if "RESET" in variables:
|
||||
self.data_in("reset", *variables.pop("RESET"))
|
||||
if "RESET" in arrays:
|
||||
self.data_in("reset", *(arrays.pop("RESET")))
|
||||
if "SEND" in variables:
|
||||
self.data_in("send", *variables.pop("SEND"))
|
||||
if "SEND" in arrays:
|
||||
self.data_in("send", *(arrays.pop("SEND")))
|
||||
|
||||
# if there are anything left consider it a call to a custom function
|
||||
|
||||
for varname, var in variables.items():
|
||||
# a simple function + argument
|
||||
self.data_in(varname, (var,))
|
||||
for arrayname, array in arrays.items():
|
||||
# we assume the array are multiple arguments to the function
|
||||
self.data_in(arrayname, *array)
|
||||
for tablename, table in tables.items():
|
||||
# we assume tables are keyword arguments to the function
|
||||
self.data_in(tablename, **table)
|
||||
|
||||
def data_out(self, msdp_string):
|
||||
"""
|
||||
Return a msdp-valid subnegotiation across the protocol.
|
||||
"""
|
||||
#print "msdp data_out (without IAC SE):", msdp_string
|
||||
self.protocol ._write(IAC + SB + MSDP + force_str(msdp_string) + IAC + SE)
|
||||
|
||||
def data_in(self, funcname, *args, **kwargs):
|
||||
"""
|
||||
Send oob data to Evennia
|
||||
"""
|
||||
#print "msdp data_in:", funcname, args, kwargs
|
||||
self.protocol.data_in(text=None, oob=(funcname, args, kwargs))
|
||||
185
lib/server/portal/mssp.py
Normal file
185
lib/server/portal/mssp.py
Normal file
|
|
@ -0,0 +1,185 @@
|
|||
"""
|
||||
|
||||
MSSP - Mud Server Status Protocol
|
||||
|
||||
This implements the MSSP telnet protocol as per
|
||||
http://tintin.sourceforge.net/mssp/. MSSP allows web portals and
|
||||
listings to have their crawlers find the mud and automatically
|
||||
extract relevant information about it, such as genre, how many
|
||||
active players and so on.
|
||||
|
||||
|
||||
"""
|
||||
from django.conf import settings
|
||||
from src.utils import utils
|
||||
|
||||
MSSP = chr(70)
|
||||
MSSP_VAR = chr(1)
|
||||
MSSP_VAL = chr(2)
|
||||
|
||||
|
||||
# try to get the customized mssp info, if it exists.
|
||||
MSSPTable_CUSTOM = utils.variable_from_module(settings.MSSP_META_MODULE, "MSSPTable", default={})
|
||||
|
||||
class Mssp(object):
|
||||
"""
|
||||
Implements the MSSP protocol. Add this to a
|
||||
variable on the telnet protocol to set it up.
|
||||
"""
|
||||
def __init__(self, protocol):
|
||||
"""
|
||||
initialize MSSP by storing protocol on ourselves
|
||||
and calling the client to see if it supports
|
||||
MSSP.
|
||||
"""
|
||||
self.protocol = protocol
|
||||
self.protocol.will(MSSP).addCallbacks(self.do_mssp, self.no_mssp)
|
||||
|
||||
def get_player_count(self):
|
||||
"Get number of logged-in players"
|
||||
return str(self.protocol.sessionhandler.count_loggedin())
|
||||
|
||||
def get_uptime(self):
|
||||
"Get how long the portal has been online (reloads are not counted)"
|
||||
return str(self.protocol.sessionhandler.uptime)
|
||||
|
||||
def no_mssp(self, option):
|
||||
"""
|
||||
This is the normal operation.
|
||||
"""
|
||||
self.protocol.handshake_done()
|
||||
pass
|
||||
|
||||
def do_mssp(self, option):
|
||||
"""
|
||||
Negotiate all the information.
|
||||
"""
|
||||
|
||||
self.mssp_table = {
|
||||
|
||||
# Required fields
|
||||
|
||||
"NAME": "Evennia",
|
||||
"PLAYERS": self.get_player_count,
|
||||
"UPTIME" : self.get_uptime,
|
||||
|
||||
# Generic
|
||||
|
||||
"CRAWL DELAY": "-1",
|
||||
|
||||
"HOSTNAME": "", # current or new hostname
|
||||
"PORT": ["4000"], # most important port should be last in list
|
||||
"CODEBASE": "Evennia",
|
||||
"CONTACT": "", # email for contacting the mud
|
||||
"CREATED": "", # year MUD was created
|
||||
"ICON": "", # url to icon 32x32 or larger; <32kb.
|
||||
"IP": "", # current or new IP address
|
||||
"LANGUAGE": "", # name of language used, e.g. English
|
||||
"LOCATION": "", # full English name of server country
|
||||
"MINIMUM AGE": "0", # set to 0 if not applicable
|
||||
"WEBSITE": "www.evennia.com",
|
||||
|
||||
# Categorisation
|
||||
|
||||
"FAMILY": "Custom", # evennia goes under 'Custom'
|
||||
"GENRE": "None", # Adult, Fantasy, Historical, Horror, Modern, None, or Science Fiction
|
||||
"GAMEPLAY": "None", # Adventure, Educational, Hack and Slash, None,
|
||||
# Player versus Player, Player versus Environment,
|
||||
# Roleplaying, Simulation, Social or Strategy
|
||||
"STATUS": "Open Beta", # Alpha, Closed Beta, Open Beta, Live
|
||||
"GAMESYSTEM": "Custom", # D&D, d20 System, World of Darkness, etc. Use Custom if homebrew
|
||||
"INTERMUD": "IMC2", # evennia supports IMC2.
|
||||
"SUBGENRE": "None", # LASG, Medieval Fantasy, World War II, Frankenstein,
|
||||
# Cyberpunk, Dragonlance, etc. Or None if not available.
|
||||
|
||||
# World
|
||||
|
||||
"AREAS": "0",
|
||||
"HELPFILES": "0",
|
||||
"MOBILES": "0",
|
||||
"OBJECTS": "0",
|
||||
"ROOMS": "0", # use 0 if room-less
|
||||
"CLASSES": "0", # use 0 if class-less
|
||||
"LEVELS": "0", # use 0 if level-less
|
||||
"RACES": "0", # use 0 if race-less
|
||||
"SKILLS": "0", # use 0 if skill-less
|
||||
|
||||
# Protocols set to 1 or 0)
|
||||
|
||||
"ANSI": "1",
|
||||
"GMCP": "0",
|
||||
"MCCP": "0",
|
||||
"MCP": "0",
|
||||
"MSDP": "0",
|
||||
"MSP": "0",
|
||||
"MXP": "0",
|
||||
"PUEBLO": "0",
|
||||
"UTF-8": "1",
|
||||
"VT100": "0",
|
||||
"XTERM 256 COLORS": "0",
|
||||
|
||||
# Commercial set to 1 or 0)
|
||||
|
||||
"PAY TO PLAY": "0",
|
||||
"PAY FOR PERKS": "0",
|
||||
|
||||
# Hiring set to 1 or 0)
|
||||
|
||||
"HIRING BUILDERS": "0",
|
||||
"HIRING CODERS": "0",
|
||||
|
||||
# Extended variables
|
||||
|
||||
# World
|
||||
|
||||
"DBSIZE": "0",
|
||||
"EXITS": "0",
|
||||
"EXTRA DESCRIPTIONS": "0",
|
||||
"MUDPROGS": "0",
|
||||
"MUDTRIGS": "0",
|
||||
"RESETS": "0",
|
||||
|
||||
# Game (set to 1, 0 or one of the given alternatives)
|
||||
|
||||
"ADULT MATERIAL": "0",
|
||||
"MULTICLASSING": "0",
|
||||
"NEWBIE FRIENDLY": "0",
|
||||
"PLAYER CITIES": "0",
|
||||
"PLAYER CLANS": "0",
|
||||
"PLAYER CRAFTING": "0",
|
||||
"PLAYER GUILDS": "0",
|
||||
"EQUIPMENT SYSTEM": "None", # "None", "Level", "Skill", "Both"
|
||||
"MULTIPLAYING": "None", # "None", "Restricted", "Full"
|
||||
"PLAYERKILLING": "None", # "None", "Restricted", "Full"
|
||||
"QUEST SYSTEM": "None", # "None", "Immortal Run", "Automated", "Integrated"
|
||||
"ROLEPLAYING": "None", # "None", "Accepted", "Encouraged", "Enforced"
|
||||
"TRAINING SYSTEM": "None", # "None", "Level", "Skill", "Both"
|
||||
"WORLD ORIGINALITY": "None", # "All Stock", "Mostly Stock", "Mostly Original", "All Original"
|
||||
|
||||
# Protocols (only change if you added/removed something manually)
|
||||
|
||||
"ATCP": "0",
|
||||
"MSDP": "0",
|
||||
"MCCP": "1",
|
||||
"SSL": "1",
|
||||
"UTF-8": "1",
|
||||
"ZMP": "0",
|
||||
"XTERM 256 COLORS": "0"}
|
||||
|
||||
# update the static table with the custom one
|
||||
if MSSPTable_CUSTOM:
|
||||
self.mssp_table.update(MSSPTable_CUSTOM)
|
||||
|
||||
varlist = ''
|
||||
for variable, value in self.mssp_table.items():
|
||||
if callable(value):
|
||||
value = value()
|
||||
if utils.is_iter(value):
|
||||
for partval in value:
|
||||
varlist += MSSP_VAR + str(variable) + MSSP_VAL + str(partval)
|
||||
else:
|
||||
varlist += MSSP_VAR + str(variable) + MSSP_VAL + str(value)
|
||||
|
||||
# send to crawler by subnegotiation
|
||||
self.protocol.requestNegotiation(MSSP, varlist)
|
||||
self.protocol.handshake_done()
|
||||
62
lib/server/portal/mxp.py
Normal file
62
lib/server/portal/mxp.py
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
"""
|
||||
MXP - Mud eXtension Protocol.
|
||||
|
||||
Partial implementation of the MXP protocol.
|
||||
The MXP protocol allows more advanced formatting options for telnet clients
|
||||
that supports it (mudlet, zmud, mushclient are a few)
|
||||
|
||||
This only implements the SEND tag.
|
||||
|
||||
More information can be found on the following links:
|
||||
http://www.zuggsoft.com/zmud/mxp.htm
|
||||
http://www.mushclient.com/mushclient/mxp.htm
|
||||
http://www.gammon.com.au/mushclient/addingservermxp.htm
|
||||
"""
|
||||
import re
|
||||
|
||||
LINKS_SUB = re.compile(r'\{lc(.*?)\{lt(.*?)\{le', re.DOTALL)
|
||||
|
||||
MXP = "\x5B"
|
||||
MXP_TEMPSECURE = "\x1B[4z"
|
||||
MXP_SEND = MXP_TEMPSECURE + \
|
||||
"<SEND HREF='\\1'>" + \
|
||||
"\\2" + \
|
||||
MXP_TEMPSECURE + \
|
||||
"</SEND>"
|
||||
|
||||
def mxp_parse(text):
|
||||
"""
|
||||
Replaces links to the correct format for MXP.
|
||||
"""
|
||||
text = text.replace("&", "&") \
|
||||
.replace("<", "<") \
|
||||
.replace(">", ">")
|
||||
|
||||
text = LINKS_SUB.sub(MXP_SEND, text)
|
||||
return text
|
||||
|
||||
class Mxp(object):
|
||||
"""
|
||||
Implements the MXP protocol.
|
||||
"""
|
||||
|
||||
def __init__(self, protocol):
|
||||
"""Initializes the protocol by checking if the client supports it."""
|
||||
self.protocol = protocol
|
||||
self.protocol.protocol_flags["MXP"] = False
|
||||
self.protocol.will(MXP).addCallbacks(self.do_mxp, self.no_mxp)
|
||||
|
||||
def no_mxp(self, option):
|
||||
"""
|
||||
Client does not support MXP.
|
||||
"""
|
||||
self.protocol.protocol_flags["MXP"] = False
|
||||
self.protocol.handshake_done()
|
||||
|
||||
def do_mxp(self, option):
|
||||
"""
|
||||
Client does support MXP.
|
||||
"""
|
||||
self.protocol.protocol_flags["MXP"] = True
|
||||
self.protocol.handshake_done()
|
||||
self.protocol.requestNegotiation(MXP, '')
|
||||
61
lib/server/portal/naws.py
Normal file
61
lib/server/portal/naws.py
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
"""
|
||||
|
||||
NAWS - Negotiate About Window Size
|
||||
|
||||
This implements the NAWS telnet option as per
|
||||
https://www.ietf.org/rfc/rfc1073.txt
|
||||
|
||||
NAWS allows telnet clients to report their
|
||||
current window size to the client and update
|
||||
it when the size changes
|
||||
|
||||
"""
|
||||
from django.conf import settings
|
||||
from src.utils import utils
|
||||
|
||||
NAWS = chr(31)
|
||||
IS = chr(0)
|
||||
# default taken from telnet specification
|
||||
DEFAULT_WIDTH = settings.CLIENT_DEFAULT_WIDTH
|
||||
DEFAULT_HEIGHT = settings.CLIENT_DEFAULT_HEIGHT
|
||||
|
||||
# try to get the customized mssp info, if it exists.
|
||||
|
||||
class Naws(object):
|
||||
"""
|
||||
Implements the MSSP protocol. Add this to a
|
||||
variable on the telnet protocol to set it up.
|
||||
"""
|
||||
def __init__(self, protocol):
|
||||
"""
|
||||
initialize NAWS by storing protocol on ourselves
|
||||
and calling the client to see if it supports
|
||||
NAWS.
|
||||
"""
|
||||
self.naws_step = 0
|
||||
self.protocol = protocol
|
||||
self.protocol.protocol_flags['SCREENWIDTH'] = {0: DEFAULT_WIDTH} # windowID (0 is root):width
|
||||
self.protocol.protocol_flags['SCREENHEIGHT'] = {0: DEFAULT_HEIGHT} # windowID:width
|
||||
self.protocol.negotiationMap[NAWS] = self.negotiate_sizes
|
||||
self.protocol.do(NAWS).addCallbacks(self.do_naws, self.no_naws)
|
||||
|
||||
def no_naws(self, option):
|
||||
"""
|
||||
This is the normal operation.
|
||||
"""
|
||||
self.protocol.handshake_done()
|
||||
|
||||
def do_naws(self, option):
|
||||
"""
|
||||
Negotiate all the information.
|
||||
"""
|
||||
self.protocol.handshake_done()
|
||||
|
||||
def negotiate_sizes(self, options):
|
||||
if len(options) == 4:
|
||||
# NAWS is negotiated with 16bit words
|
||||
width = options[0] + options[1]
|
||||
self.protocol.protocol_flags['SCREENWIDTH'][0] = int(width.encode('hex'), 16)
|
||||
height = options[2] + options[3]
|
||||
self.protocol.protocol_flags['SCREENHEIGHT'][0] = int(height.encode('hex'), 16)
|
||||
|
||||
312
lib/server/portal/portal.py
Normal file
312
lib/server/portal/portal.py
Normal file
|
|
@ -0,0 +1,312 @@
|
|||
"""
|
||||
This module implements the main Evennia server process, the core of
|
||||
the game engine.
|
||||
|
||||
This module should be started with the 'twistd' executable since it
|
||||
sets up all the networking features. (this is done automatically
|
||||
by game/evennia.py).
|
||||
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
if os.name == 'nt':
|
||||
# For Windows batchfile we need an extra path insertion here.
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(
|
||||
os.path.dirname(os.path.abspath(__file__))))))
|
||||
from src.server.webserver import EvenniaReverseProxyResource
|
||||
from twisted.application import internet, service
|
||||
from twisted.internet import protocol, reactor
|
||||
from twisted.web import server
|
||||
import django
|
||||
|
||||
django.setup()
|
||||
|
||||
from django.conf import settings
|
||||
from src.utils.utils import get_evennia_version, mod_import, make_iter
|
||||
from src.server.portal.portalsessionhandler import PORTAL_SESSIONS
|
||||
|
||||
PORTAL_SERVICES_PLUGIN_MODULES = [mod_import(module) for module in make_iter(settings.PORTAL_SERVICES_PLUGIN_MODULES)]
|
||||
|
||||
if os.name == 'nt':
|
||||
# For Windows we need to handle pid files manually.
|
||||
PORTAL_PIDFILE = os.path.join(settings.GAME_DIR, 'portal.pid')
|
||||
|
||||
#------------------------------------------------------------
|
||||
# Evennia Portal settings
|
||||
#------------------------------------------------------------
|
||||
|
||||
VERSION = get_evennia_version()
|
||||
|
||||
SERVERNAME = settings.SERVERNAME
|
||||
|
||||
PORTAL_RESTART = os.path.join(settings.GAME_DIR, 'portal.restart')
|
||||
|
||||
TELNET_PORTS = settings.TELNET_PORTS
|
||||
SSL_PORTS = settings.SSL_PORTS
|
||||
SSH_PORTS = settings.SSH_PORTS
|
||||
WEBSERVER_PORTS = settings.WEBSERVER_PORTS
|
||||
WEBSOCKET_CLIENT_PORT = settings.WEBSOCKET_CLIENT_PORT
|
||||
|
||||
TELNET_INTERFACES = settings.TELNET_INTERFACES
|
||||
SSL_INTERFACES = settings.SSL_INTERFACES
|
||||
SSH_INTERFACES = settings.SSH_INTERFACES
|
||||
WEBSERVER_INTERFACES = settings.WEBSERVER_INTERFACES
|
||||
WEBSOCKET_CLIENT_INTERFACE = settings.WEBSOCKET_CLIENT_INTERFACE
|
||||
WEBSOCKET_CLIENT_URL = settings.WEBSOCKET_CLIENT_URL
|
||||
|
||||
TELNET_ENABLED = settings.TELNET_ENABLED and TELNET_PORTS and TELNET_INTERFACES
|
||||
SSL_ENABLED = settings.SSL_ENABLED and SSL_PORTS and SSL_INTERFACES
|
||||
SSH_ENABLED = settings.SSH_ENABLED and SSH_PORTS and SSH_INTERFACES
|
||||
WEBSERVER_ENABLED = settings.WEBSERVER_ENABLED and WEBSERVER_PORTS and WEBSERVER_INTERFACES
|
||||
WEBCLIENT_ENABLED = settings.WEBCLIENT_ENABLED
|
||||
WEBSOCKET_CLIENT_ENABLED = settings.WEBSOCKET_CLIENT_ENABLED and WEBSOCKET_CLIENT_PORT and WEBSOCKET_CLIENT_INTERFACE
|
||||
|
||||
AMP_HOST = settings.AMP_HOST
|
||||
AMP_PORT = settings.AMP_PORT
|
||||
AMP_INTERFACE = settings.AMP_INTERFACE
|
||||
AMP_ENABLED = AMP_HOST and AMP_PORT and AMP_INTERFACE
|
||||
|
||||
|
||||
#------------------------------------------------------------
|
||||
# Portal Service object
|
||||
#------------------------------------------------------------
|
||||
class Portal(object):
|
||||
|
||||
"""
|
||||
The main Portal server handler. This object sets up the database and
|
||||
tracks and interlinks all the twisted network services that make up
|
||||
Portal.
|
||||
"""
|
||||
|
||||
def __init__(self, application):
|
||||
"""
|
||||
Setup the server.
|
||||
|
||||
application - an instantiated Twisted application
|
||||
|
||||
"""
|
||||
sys.path.append('.')
|
||||
|
||||
# create a store of services
|
||||
self.services = service.IServiceCollection(application)
|
||||
self.amp_protocol = None # set by amp factory
|
||||
self.sessions = PORTAL_SESSIONS
|
||||
self.sessions.portal = self
|
||||
|
||||
# set a callback if the server is killed abruptly,
|
||||
# by Ctrl-C, reboot etc.
|
||||
reactor.addSystemEventTrigger('before', 'shutdown', self.shutdown, _reactor_stopping=True)
|
||||
|
||||
self.game_running = False
|
||||
|
||||
def set_restart_mode(self, mode=None):
|
||||
"""
|
||||
This manages the flag file that tells the runner if the server should
|
||||
be restarted or is shutting down. Valid modes are True/False and None.
|
||||
If mode is None, no change will be done to the flag file.
|
||||
"""
|
||||
if mode is None:
|
||||
return
|
||||
f = open(PORTAL_RESTART, 'w')
|
||||
print "writing mode=%(mode)s to %(portal_restart)s" % {'mode': mode, 'portal_restart': PORTAL_RESTART}
|
||||
f.write(str(mode))
|
||||
f.close()
|
||||
|
||||
def shutdown(self, restart=None, _reactor_stopping=False):
|
||||
"""
|
||||
Shuts down the server from inside it.
|
||||
|
||||
restart - True/False sets the flags so the server will be
|
||||
restarted or not. If None, the current flag setting
|
||||
(set at initialization or previous runs) is used.
|
||||
_reactor_stopping - this is set if server is already in the process of
|
||||
shutting down; in this case we don't need to stop it again.
|
||||
|
||||
Note that restarting (regardless of the setting) will not work
|
||||
if the Portal is currently running in daemon mode. In that
|
||||
case it always needs to be restarted manually.
|
||||
"""
|
||||
if _reactor_stopping and hasattr(self, "shutdown_complete"):
|
||||
# we get here due to us calling reactor.stop below. No need
|
||||
# to do the shutdown procedure again.
|
||||
return
|
||||
self.set_restart_mode(restart)
|
||||
if os.name == 'nt' and os.path.exists(PORTAL_PIDFILE):
|
||||
# for Windows we need to remove pid files manually
|
||||
os.remove(PORTAL_PIDFILE)
|
||||
if not _reactor_stopping:
|
||||
# shutting down the reactor will trigger another signal. We set
|
||||
# a flag to avoid loops.
|
||||
self.shutdown_complete = True
|
||||
reactor.callLater(0, reactor.stop)
|
||||
|
||||
#------------------------------------------------------------
|
||||
#
|
||||
# Start the Portal proxy server and add all active services
|
||||
#
|
||||
#------------------------------------------------------------
|
||||
|
||||
# twistd requires us to define the variable 'application' so it knows
|
||||
# what to execute from.
|
||||
application = service.Application('Portal')
|
||||
|
||||
# The main Portal server program. This sets up the database
|
||||
# and is where we store all the other services.
|
||||
PORTAL = Portal(application)
|
||||
|
||||
print '-' * 50
|
||||
print ' %(servername)s Portal (%(version)s) started.' % {'servername': SERVERNAME, 'version': VERSION}
|
||||
|
||||
if AMP_ENABLED:
|
||||
|
||||
# The AMP protocol handles the communication between
|
||||
# the portal and the mud server. Only reason to ever deactivate
|
||||
# it would be during testing and debugging.
|
||||
|
||||
from src.server import amp
|
||||
|
||||
print ' amp (to Server): %s' % AMP_PORT
|
||||
|
||||
factory = amp.AmpClientFactory(PORTAL)
|
||||
amp_client = internet.TCPClient(AMP_HOST, AMP_PORT, factory)
|
||||
amp_client.setName('evennia_amp')
|
||||
PORTAL.services.addService(amp_client)
|
||||
|
||||
|
||||
# We group all the various services under the same twisted app.
|
||||
# These will gradually be started as they are initialized below.
|
||||
|
||||
if TELNET_ENABLED:
|
||||
|
||||
# Start telnet game connections
|
||||
|
||||
from src.server.portal import telnet
|
||||
|
||||
for interface in TELNET_INTERFACES:
|
||||
ifacestr = ""
|
||||
if interface not in ('0.0.0.0', '::') or len(TELNET_INTERFACES) > 1:
|
||||
ifacestr = "-%s" % interface
|
||||
for port in TELNET_PORTS:
|
||||
pstring = "%s:%s" % (ifacestr, port)
|
||||
factory = protocol.ServerFactory()
|
||||
factory.protocol = telnet.TelnetProtocol
|
||||
factory.sessionhandler = PORTAL_SESSIONS
|
||||
telnet_service = internet.TCPServer(port, factory, interface=interface)
|
||||
telnet_service.setName('EvenniaTelnet%s' % pstring)
|
||||
PORTAL.services.addService(telnet_service)
|
||||
|
||||
print ' telnet%s: %s' % (ifacestr, port)
|
||||
|
||||
|
||||
if SSL_ENABLED:
|
||||
|
||||
# Start SSL game connection (requires PyOpenSSL).
|
||||
|
||||
from src.server.portal import ssl
|
||||
|
||||
for interface in SSL_INTERFACES:
|
||||
ifacestr = ""
|
||||
if interface not in ('0.0.0.0', '::') or len(SSL_INTERFACES) > 1:
|
||||
ifacestr = "-%s" % interface
|
||||
for port in SSL_PORTS:
|
||||
pstring = "%s:%s" % (ifacestr, port)
|
||||
factory = protocol.ServerFactory()
|
||||
factory.sessionhandler = PORTAL_SESSIONS
|
||||
factory.protocol = ssl.SSLProtocol
|
||||
ssl_service = internet.SSLServer(port,
|
||||
factory,
|
||||
ssl.getSSLContext(),
|
||||
interface=interface)
|
||||
ssl_service.setName('EvenniaSSL%s' % pstring)
|
||||
PORTAL.services.addService(ssl_service)
|
||||
|
||||
print " ssl%s: %s" % (ifacestr, port)
|
||||
|
||||
|
||||
if SSH_ENABLED:
|
||||
|
||||
# Start SSH game connections. Will create a keypair in
|
||||
# evennia/game if necessary.
|
||||
|
||||
from src.server.portal import ssh
|
||||
|
||||
for interface in SSH_INTERFACES:
|
||||
ifacestr = ""
|
||||
if interface not in ('0.0.0.0', '::') or len(SSH_INTERFACES) > 1:
|
||||
ifacestr = "-%s" % interface
|
||||
for port in SSH_PORTS:
|
||||
pstring = "%s:%s" % (ifacestr, port)
|
||||
factory = ssh.makeFactory({'protocolFactory': ssh.SshProtocol,
|
||||
'protocolArgs': (),
|
||||
'sessions': PORTAL_SESSIONS})
|
||||
ssh_service = internet.TCPServer(port, factory, interface=interface)
|
||||
ssh_service.setName('EvenniaSSH%s' % pstring)
|
||||
PORTAL.services.addService(ssh_service)
|
||||
|
||||
print " ssl%s: %s" % (ifacestr, port)
|
||||
|
||||
|
||||
if WEBSERVER_ENABLED:
|
||||
|
||||
# Start a reverse proxy to relay data to the Server-side webserver
|
||||
|
||||
websocket_started = False
|
||||
for interface in WEBSERVER_INTERFACES:
|
||||
ifacestr = ""
|
||||
if interface not in ('0.0.0.0', '::') or len(WEBSERVER_INTERFACES) > 1:
|
||||
ifacestr = "-%s" % interface
|
||||
for proxyport, serverport in WEBSERVER_PORTS:
|
||||
pstring = "%s:%s<->%s" % (ifacestr, proxyport, serverport)
|
||||
web_root = EvenniaReverseProxyResource('127.0.0.1', serverport, '')
|
||||
webclientstr = ""
|
||||
if WEBCLIENT_ENABLED:
|
||||
# create ajax client processes at /webclientdata
|
||||
from src.server.portal.webclient import WebClient
|
||||
|
||||
webclient = WebClient()
|
||||
webclient.sessionhandler = PORTAL_SESSIONS
|
||||
web_root.putChild("webclientdata", webclient)
|
||||
webclientstr = "\n + client (ajax only)"
|
||||
|
||||
if WEBSOCKET_CLIENT_ENABLED and not websocket_started:
|
||||
# start websocket client port for the webclient
|
||||
# we only support one websocket client
|
||||
from src.server.portal import websocket_client
|
||||
from src.utils.txws import WebSocketFactory
|
||||
|
||||
interface = WEBSOCKET_CLIENT_INTERFACE
|
||||
port = WEBSOCKET_CLIENT_PORT
|
||||
ifacestr = ""
|
||||
if interface not in ('0.0.0.0', '::'):
|
||||
ifacestr = "-%s" % interface
|
||||
pstring = "%s:%s" % (ifacestr, port)
|
||||
factory = protocol.ServerFactory()
|
||||
factory.protocol = websocket_client.WebSocketClient
|
||||
factory.sessionhandler = PORTAL_SESSIONS
|
||||
websocket_service = internet.TCPServer(port, WebSocketFactory(factory), interface=interface)
|
||||
websocket_service.setName('EvenniaWebSocket%s' % pstring)
|
||||
PORTAL.services.addService(websocket_service)
|
||||
websocket_started = True
|
||||
webclientstr = webclientstr[:-11] + "(%s:%s)" % (WEBSOCKET_CLIENT_URL, port)
|
||||
|
||||
web_root = server.Site(web_root, logPath=settings.HTTP_LOG_FILE)
|
||||
proxy_service = internet.TCPServer(proxyport,
|
||||
web_root,
|
||||
interface=interface)
|
||||
proxy_service.setName('EvenniaWebProxy%s' % pstring)
|
||||
PORTAL.services.addService(proxy_service)
|
||||
print " webproxy%s:%s (<-> %s)%s" % (ifacestr, proxyport, serverport, webclientstr)
|
||||
|
||||
|
||||
for plugin_module in PORTAL_SERVICES_PLUGIN_MODULES:
|
||||
# external plugin services to start
|
||||
plugin_module.start_plugin_services(PORTAL)
|
||||
|
||||
print '-' * 50 # end of terminal output
|
||||
|
||||
if os.name == 'nt':
|
||||
# Windows only: Set PID file manually
|
||||
f = open(os.path.join(settings.GAME_DIR, 'portal.pid'), 'w')
|
||||
f.write(str(os.getpid()))
|
||||
f.close()
|
||||
208
lib/server/portal/portalsessionhandler.py
Normal file
208
lib/server/portal/portalsessionhandler.py
Normal file
|
|
@ -0,0 +1,208 @@
|
|||
"""
|
||||
Sessionhandler for portal sessions
|
||||
"""
|
||||
import time
|
||||
from src.server.sessionhandler import SessionHandler, PCONN, PDISCONN, PSYNC, PCONNSYNC
|
||||
|
||||
_MOD_IMPORT = None
|
||||
|
||||
#------------------------------------------------------------
|
||||
# Portal-SessionHandler class
|
||||
#------------------------------------------------------------
|
||||
class PortalSessionHandler(SessionHandler):
|
||||
"""
|
||||
This object holds the sessions connected to the portal at any time.
|
||||
It is synced with the server's equivalent SessionHandler over the AMP
|
||||
connection.
|
||||
|
||||
Sessions register with the handler using the connect() method. This
|
||||
will assign a new unique sessionid to the session and send that sessid
|
||||
to the server using the AMP connection.
|
||||
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
"""
|
||||
Init the handler
|
||||
"""
|
||||
self.portal = None
|
||||
self.sessions = {}
|
||||
self.latest_sessid = 0
|
||||
self.uptime = time.time()
|
||||
self.connection_time = 0
|
||||
|
||||
def at_server_connection(self):
|
||||
"""
|
||||
Called when the Portal establishes connection with the
|
||||
Server. At this point, the AMP connection is already
|
||||
established.
|
||||
"""
|
||||
self.connection_time = time.time()
|
||||
|
||||
def connect(self, session):
|
||||
"""
|
||||
Called by protocol at first connect. This adds a not-yet
|
||||
authenticated session using an ever-increasing counter for sessid.
|
||||
"""
|
||||
self.latest_sessid += 1
|
||||
sessid = self.latest_sessid
|
||||
session.sessid = sessid
|
||||
sessdata = session.get_sync_data()
|
||||
self.sessions[sessid] = session
|
||||
# sync with server-side
|
||||
if self.portal.amp_protocol: # this is a timing issue
|
||||
self.portal.amp_protocol.call_remote_ServerAdmin(sessid,
|
||||
operation=PCONN,
|
||||
data=sessdata)
|
||||
def sync(self, session):
|
||||
"""
|
||||
Called by the protocol of an already connected session. This
|
||||
can be used to sync the session info in a delayed manner,
|
||||
such as when negotiation and handshakes are delayed.
|
||||
"""
|
||||
if session.sessid:
|
||||
# only use if session already has sessid (i.e. has already connected)
|
||||
sessdata = session.get_sync_data()
|
||||
if self.portal.amp_protocol:
|
||||
# we only send sessdata that should not have changed
|
||||
# at the server level at this point
|
||||
sessdata = dict((key, val) for key, val in sessdata.items() if key in ("protocol_key",
|
||||
"address",
|
||||
"sessid",
|
||||
"suid",
|
||||
"conn_time",
|
||||
"protocol_flags",
|
||||
"server_data",))
|
||||
self.portal.amp_protocol.call_remote_ServerAdmin(session.sessid,
|
||||
operation=PCONNSYNC,
|
||||
data=sessdata)
|
||||
|
||||
def disconnect(self, session):
|
||||
"""
|
||||
Called from portal side when the connection is closed
|
||||
from the portal side.
|
||||
"""
|
||||
sessid = session.sessid
|
||||
if sessid in self.sessions:
|
||||
del self.sessions[sessid]
|
||||
del session
|
||||
# tell server to also delete this session
|
||||
self.portal.amp_protocol.call_remote_ServerAdmin(sessid,
|
||||
operation=PDISCONN)
|
||||
|
||||
|
||||
def server_connect(self, protocol_path="", config=dict()):
|
||||
"""
|
||||
Called by server to force the initialization of a new
|
||||
protocol instance. Server wants this instance to get
|
||||
a unique sessid and to be connected back as normal. This
|
||||
is used to initiate irc/imc2/rss etc connections.
|
||||
|
||||
protocol_path - full python path to the class factory
|
||||
for the protocol used, eg
|
||||
'src.server.portal.irc.IRCClientFactory'
|
||||
config - dictionary of configuration options, fed as **kwarg
|
||||
to protocol class' __init__ method.
|
||||
|
||||
The called protocol class must have a method start()
|
||||
that calls the portalsession.connect() as a normal protocol.
|
||||
"""
|
||||
global _MOD_IMPORT
|
||||
if not _MOD_IMPORT:
|
||||
from src.utils.utils import variable_from_module as _MOD_IMPORT
|
||||
path, clsname = protocol_path.rsplit(".", 1)
|
||||
cls = _MOD_IMPORT(path, clsname)
|
||||
if not cls:
|
||||
raise RuntimeError("ServerConnect: protocol factory '%s' not found." % protocol_path)
|
||||
protocol = cls(self, **config)
|
||||
protocol.start()
|
||||
|
||||
def server_disconnect(self, sessid, reason=""):
|
||||
"""
|
||||
Called by server to force a disconnect by sessid
|
||||
"""
|
||||
session = self.sessions.get(sessid, None)
|
||||
if session:
|
||||
session.disconnect(reason)
|
||||
if sessid in self.sessions:
|
||||
# in case sess.disconnect doesn't delete it
|
||||
del self.sessions[sessid]
|
||||
del session
|
||||
|
||||
def server_disconnect_all(self, reason=""):
|
||||
"""
|
||||
Called by server when forcing a clean disconnect for everyone.
|
||||
"""
|
||||
for session in self.sessions.values():
|
||||
session.disconnect(reason)
|
||||
del session
|
||||
self.sessions = {}
|
||||
|
||||
def server_logged_in(self, sessid, data):
|
||||
"""
|
||||
The server tells us that the session has been
|
||||
authenticated. Updated it.
|
||||
"""
|
||||
sess = self.get_session(sessid)
|
||||
sess.load_sync_data(data)
|
||||
|
||||
def server_session_sync(self, serversessions):
|
||||
"""
|
||||
Server wants to save data to the portal, maybe because it's about
|
||||
to shut down. We don't overwrite any sessions here, just update
|
||||
them in-place and remove any that are out of sync (which should
|
||||
normally not be the case)
|
||||
|
||||
serversessions - dictionary {sessid:{property:value},...} describing
|
||||
the properties to sync on all sessions
|
||||
"""
|
||||
to_save = [sessid for sessid in serversessions if sessid in self.sessions]
|
||||
to_delete = [sessid for sessid in self.sessions if sessid not in to_save]
|
||||
# save protocols
|
||||
for sessid in to_save:
|
||||
self.sessions[sessid].load_sync_data(serversessions[sessid])
|
||||
# disconnect out-of-sync missing protocols
|
||||
for sessid in to_delete:
|
||||
self.server_disconnect(sessid)
|
||||
|
||||
def count_loggedin(self, include_unloggedin=False):
|
||||
"""
|
||||
Count loggedin connections, alternatively count all connections.
|
||||
"""
|
||||
return len(self.get_sessions(include_unloggedin=include_unloggedin))
|
||||
|
||||
def session_from_suid(self, suid):
|
||||
"""
|
||||
Given a session id, retrieve the session (this is primarily
|
||||
intended to be called by web clients)
|
||||
"""
|
||||
return [sess for sess in self.get_sessions(include_unloggedin=True)
|
||||
if hasattr(sess, 'suid') and sess.suid == suid]
|
||||
|
||||
def data_in(self, session, text="", **kwargs):
|
||||
"""
|
||||
Called by portal sessions for relaying data coming
|
||||
in from the protocol to the server. data is
|
||||
serialized before passed on.
|
||||
"""
|
||||
self.portal.amp_protocol.call_remote_MsgPortal2Server(session.sessid,
|
||||
msg=text,
|
||||
data=kwargs)
|
||||
|
||||
def announce_all(self, message):
|
||||
"""
|
||||
Send message to all connection sessions
|
||||
"""
|
||||
for session in self.sessions.values():
|
||||
session.data_out(message)
|
||||
|
||||
def data_out(self, sessid, text=None, **kwargs):
|
||||
"""
|
||||
Called by server for having the portal relay messages and data
|
||||
to the correct session protocol.
|
||||
"""
|
||||
session = self.sessions.get(sessid, None)
|
||||
if session:
|
||||
session.data_out(text=text, **kwargs)
|
||||
|
||||
PORTAL_SESSIONS = PortalSessionHandler()
|
||||
100
lib/server/portal/rss.py
Normal file
100
lib/server/portal/rss.py
Normal file
|
|
@ -0,0 +1,100 @@
|
|||
"""
|
||||
RSS parser for Evennia
|
||||
|
||||
This connects an RSS feed to an in-game Evennia channel, sending messages
|
||||
to the channel whenever the feed updates.
|
||||
|
||||
"""
|
||||
|
||||
from twisted.internet import task, threads
|
||||
from django.conf import settings
|
||||
from src.server.session import Session
|
||||
from src.utils import logger
|
||||
|
||||
RSS_ENABLED = settings.RSS_ENABLED
|
||||
#RETAG = re.compile(r'<[^>]*?>')
|
||||
|
||||
if RSS_ENABLED:
|
||||
try:
|
||||
import feedparser
|
||||
except ImportError:
|
||||
raise ImportError("RSS requires python-feedparser to be installed. Install or set RSS_ENABLED=False.")
|
||||
|
||||
class RSSReader(Session):
|
||||
"""
|
||||
A simple RSS reader using universal feedparser
|
||||
"""
|
||||
def __init__(self, factory, url, rate):
|
||||
self.url = url
|
||||
self.rate = rate
|
||||
self.factory = factory
|
||||
self.old_entries = {}
|
||||
|
||||
def get_new(self):
|
||||
"""Returns list of new items."""
|
||||
feed = feedparser.parse(self.url)
|
||||
new_entries = []
|
||||
for entry in feed['entries']:
|
||||
idval = entry['id'] + entry.get("updated", "")
|
||||
if idval not in self.old_entries:
|
||||
self.old_entries[idval] = entry
|
||||
new_entries.append(entry)
|
||||
return new_entries
|
||||
|
||||
def disconnect(self, reason=None):
|
||||
"Disconnect from feed"
|
||||
if self.factory.task and self.factory.task.running:
|
||||
self.factory.task.stop()
|
||||
self.sessionhandler.disconnect(self)
|
||||
|
||||
def _callback(self, new_entries, init):
|
||||
"Called when RSS returns (threaded)"
|
||||
if not init:
|
||||
# for initialization we just ignore old entries
|
||||
for entry in reversed(new_entries):
|
||||
self.data_in("bot_data_in " + entry)
|
||||
|
||||
def data_in(self, text=None, **kwargs):
|
||||
"Data RSS -> Server"
|
||||
self.sessionhandler.data_in(self, text=text, **kwargs)
|
||||
|
||||
def _errback(self, fail):
|
||||
"Report error"
|
||||
logger.log_errmsg("RSS feed error: %s" % fail.value)
|
||||
|
||||
def update(self, init=False):
|
||||
"Request feed"
|
||||
return threads.deferToThread(self.get_new).addCallback(self._callback, init).addErrback(self._errback)
|
||||
|
||||
class RSSBotFactory(object):
|
||||
"""
|
||||
Initializes new bots
|
||||
"""
|
||||
|
||||
def __init__(self, sessionhandler, uid=None, url=None, rate=None):
|
||||
"Initialize"
|
||||
self.sessionhandler = sessionhandler
|
||||
self.url = url
|
||||
self.rate = rate
|
||||
self.uid = uid
|
||||
self.bot = RSSReader(self, url, rate)
|
||||
self.task = None
|
||||
|
||||
def start(self):
|
||||
"""
|
||||
Called by portalsessionhandler
|
||||
"""
|
||||
def errback(fail):
|
||||
logger.log_errmsg(fail.value)
|
||||
|
||||
# set up session and connect it to sessionhandler
|
||||
self.bot.init_session("rssbot", self.url, self.sessionhandler)
|
||||
self.bot.uid = self.uid
|
||||
self.bot.logged_in = True
|
||||
self.sessionhandler.connect(self.bot)
|
||||
|
||||
# start repeater task
|
||||
self.bot.update(init=True)
|
||||
self.task = task.LoopingCall(self.bot.update)
|
||||
if self.rate:
|
||||
self.task.start(self.rate, now=False).addErrback(errback)
|
||||
346
lib/server/portal/ssh.py
Normal file
346
lib/server/portal/ssh.py
Normal file
|
|
@ -0,0 +1,346 @@
|
|||
"""
|
||||
This module implements the ssh (Secure SHell) protocol for encrypted
|
||||
connections.
|
||||
|
||||
This depends on a generic session module that implements
|
||||
the actual login procedure of the game, tracks
|
||||
sessions etc.
|
||||
|
||||
Using standard ssh client,
|
||||
|
||||
"""
|
||||
import os
|
||||
|
||||
from twisted.cred.checkers import credentials
|
||||
from twisted.cred.portal import Portal
|
||||
from twisted.conch.ssh.keys import Key
|
||||
from twisted.conch.interfaces import IConchUser
|
||||
from twisted.conch.ssh.userauth import SSHUserAuthServer
|
||||
from twisted.conch.ssh import common
|
||||
from twisted.conch.insults import insults
|
||||
from twisted.conch.manhole_ssh import TerminalRealm, _Glue, ConchFactory
|
||||
from twisted.conch.manhole import Manhole, recvline
|
||||
from twisted.internet import defer
|
||||
from twisted.conch import interfaces as iconch
|
||||
from twisted.python import components
|
||||
from django.conf import settings
|
||||
from src.server import session
|
||||
from src.players.models import PlayerDB
|
||||
from src.utils import ansi, utils
|
||||
|
||||
ENCODINGS = settings.ENCODINGS
|
||||
|
||||
CTRL_C = '\x03'
|
||||
CTRL_D = '\x04'
|
||||
CTRL_BACKSLASH = '\x1c'
|
||||
CTRL_L = '\x0c'
|
||||
|
||||
|
||||
class SshProtocol(Manhole, session.Session):
|
||||
"""
|
||||
Each player connecting over ssh gets this protocol assigned to
|
||||
them. All communication between game and player goes through
|
||||
here.
|
||||
"""
|
||||
def __init__(self, starttuple):
|
||||
"""
|
||||
For setting up the player. If player is not None then we'll
|
||||
login automatically.
|
||||
"""
|
||||
self.authenticated_player = starttuple[0]
|
||||
# obs must not be called self.factory, that gets overwritten!
|
||||
self.cfactory = starttuple[1]
|
||||
|
||||
def terminalSize(self, width, height):
|
||||
"""
|
||||
Initialize the terminal and connect to the new session.
|
||||
"""
|
||||
# Clear the previous input line, redraw it at the new
|
||||
# cursor position
|
||||
self.terminal.eraseDisplay()
|
||||
self.terminal.cursorHome()
|
||||
self.width = width
|
||||
self.height = height
|
||||
|
||||
# initialize the session
|
||||
client_address = self.getClientAddress()
|
||||
self.init_session("ssh", client_address, self.cfactory.sessionhandler)
|
||||
|
||||
# since we might have authenticated already, we might set this here.
|
||||
if self.authenticated_player:
|
||||
self.logged_in = True
|
||||
self.uid = self.authenticated_player.user.id
|
||||
self.sessionhandler.connect(self)
|
||||
|
||||
def connectionMade(self):
|
||||
"""
|
||||
This is called when the connection is first
|
||||
established.
|
||||
"""
|
||||
recvline.HistoricRecvLine.connectionMade(self)
|
||||
self.keyHandlers[CTRL_C] = self.handle_INT
|
||||
self.keyHandlers[CTRL_D] = self.handle_EOF
|
||||
self.keyHandlers[CTRL_L] = self.handle_FF
|
||||
self.keyHandlers[CTRL_BACKSLASH] = self.handle_QUIT
|
||||
|
||||
# initalize
|
||||
|
||||
def handle_INT(self):
|
||||
"""
|
||||
Handle ^C as an interrupt keystroke by resetting the current input
|
||||
variables to their initial state.
|
||||
"""
|
||||
self.lineBuffer = []
|
||||
self.lineBufferIndex = 0
|
||||
|
||||
self.terminal.nextLine()
|
||||
self.terminal.write("KeyboardInterrupt")
|
||||
self.terminal.nextLine()
|
||||
|
||||
def handle_EOF(self):
|
||||
"""
|
||||
Handles EOF generally used to exit.
|
||||
"""
|
||||
if self.lineBuffer:
|
||||
self.terminal.write('\a')
|
||||
else:
|
||||
self.handle_QUIT()
|
||||
|
||||
def handle_FF(self):
|
||||
"""
|
||||
Handle a 'form feed' byte - generally used to request a screen
|
||||
refresh/redraw.
|
||||
"""
|
||||
self.terminal.eraseDisplay()
|
||||
self.terminal.cursorHome()
|
||||
|
||||
def handle_QUIT(self):
|
||||
"""
|
||||
Quit, end, and lose the connection.
|
||||
"""
|
||||
self.terminal.loseConnection()
|
||||
|
||||
def connectionLost(self, reason=None):
|
||||
"""
|
||||
This is executed when the connection is lost for
|
||||
whatever reason. It can also be called directly,
|
||||
from the disconnect method.
|
||||
|
||||
"""
|
||||
insults.TerminalProtocol.connectionLost(self, reason)
|
||||
self.sessionhandler.disconnect(self)
|
||||
self.terminal.loseConnection()
|
||||
|
||||
def getClientAddress(self):
|
||||
"""
|
||||
Returns the client's address and port in a tuple. For example
|
||||
('127.0.0.1', 41917)
|
||||
"""
|
||||
return self.terminal.transport.getPeer()
|
||||
|
||||
def lineReceived(self, string):
|
||||
"""
|
||||
Communication Player -> Evennia. Any line return indicates a
|
||||
command for the purpose of the MUD. So we take the user input
|
||||
and pass it on to the game engine.
|
||||
"""
|
||||
self.sessionhandler.data_in(self, string)
|
||||
|
||||
def lineSend(self, string):
|
||||
"""
|
||||
Communication Evennia -> Player
|
||||
Any string sent should already have been
|
||||
properly formatted and processed
|
||||
before reaching this point.
|
||||
|
||||
"""
|
||||
for line in string.split('\n'):
|
||||
#this is the telnet-specific method for sending
|
||||
self.terminal.write(line)
|
||||
self.terminal.nextLine()
|
||||
|
||||
# session-general method hooks
|
||||
|
||||
def disconnect(self, reason="Connection closed. Goodbye for now."):
|
||||
"""
|
||||
Disconnect from server
|
||||
"""
|
||||
if reason:
|
||||
self.data_out(reason)
|
||||
self.connectionLost(reason)
|
||||
|
||||
def data_out(self, text=None, **kwargs):
|
||||
"""
|
||||
Data Evennia -> Player access hook. 'data' argument is a dict
|
||||
parsed for string settings.
|
||||
|
||||
ssh flags:
|
||||
raw=True - leave all ansi markup and tokens unparsed
|
||||
nomarkup=True - remove all ansi markup
|
||||
|
||||
"""
|
||||
try:
|
||||
text = utils.to_str(text if text else "", encoding=self.encoding)
|
||||
except Exception, e:
|
||||
self.lineSend(str(e))
|
||||
return
|
||||
raw = kwargs.get("raw", False)
|
||||
nomarkup = kwargs.get("nomarkup", False)
|
||||
if raw:
|
||||
self.lineSend(text)
|
||||
else:
|
||||
self.lineSend(ansi.parse_ansi(text.strip("{r") + "{r", strip_ansi=nomarkup))
|
||||
|
||||
|
||||
class ExtraInfoAuthServer(SSHUserAuthServer):
|
||||
def auth_password(self, packet):
|
||||
"""
|
||||
Password authentication.
|
||||
|
||||
Used mostly for setting up the transport so we can query
|
||||
username and password later.
|
||||
"""
|
||||
password = common.getNS(packet[1:])[0]
|
||||
c = credentials.UsernamePassword(self.user, password)
|
||||
c.transport = self.transport
|
||||
return self.portal.login(c, None, IConchUser).addErrback(
|
||||
self._ebPassword)
|
||||
|
||||
|
||||
class PlayerDBPasswordChecker(object):
|
||||
"""
|
||||
Checks the django db for the correct credentials for
|
||||
username/password otherwise it returns the player or None which is
|
||||
useful for the Realm.
|
||||
"""
|
||||
credentialInterfaces = (credentials.IUsernamePassword,)
|
||||
|
||||
def __init__(self, factory):
|
||||
self.factory = factory
|
||||
super(PlayerDBPasswordChecker, self).__init__()
|
||||
|
||||
def requestAvatarId(self, c):
|
||||
"Generic credentials"
|
||||
up = credentials.IUsernamePassword(c, None)
|
||||
username = up.username
|
||||
password = up.password
|
||||
player = PlayerDB.objects.get_player_from_name(username)
|
||||
res = (None, self.factory)
|
||||
if player and player.user.check_password(password):
|
||||
res = (player, self.factory)
|
||||
return defer.succeed(res)
|
||||
|
||||
|
||||
class PassAvatarIdTerminalRealm(TerminalRealm):
|
||||
"""
|
||||
Returns an avatar that passes the avatarId through to the
|
||||
protocol. This is probably not the best way to do it.
|
||||
"""
|
||||
|
||||
def _getAvatar(self, avatarId):
|
||||
comp = components.Componentized()
|
||||
user = self.userFactory(comp, avatarId)
|
||||
sess = self.sessionFactory(comp)
|
||||
|
||||
sess.transportFactory = self.transportFactory
|
||||
sess.chainedProtocolFactory = lambda: self.chainedProtocolFactory(avatarId)
|
||||
|
||||
comp.setComponent(iconch.IConchUser, user)
|
||||
comp.setComponent(iconch.ISession, sess)
|
||||
|
||||
return user
|
||||
|
||||
|
||||
class TerminalSessionTransport_getPeer:
|
||||
"""
|
||||
Taken from twisted's TerminalSessionTransport which doesn't
|
||||
provide getPeer to the transport. This one does.
|
||||
"""
|
||||
def __init__(self, proto, chainedProtocol, avatar, width, height):
|
||||
self.proto = proto
|
||||
self.avatar = avatar
|
||||
self.chainedProtocol = chainedProtocol
|
||||
|
||||
session = self.proto.session
|
||||
|
||||
self.proto.makeConnection(
|
||||
_Glue(write=self.chainedProtocol.dataReceived,
|
||||
loseConnection=lambda: avatar.conn.sendClose(session),
|
||||
name="SSH Proto Transport"))
|
||||
|
||||
def loseConnection():
|
||||
self.proto.loseConnection()
|
||||
|
||||
def getPeer():
|
||||
session.conn.transport.transport.getPeer()
|
||||
|
||||
self.chainedProtocol.makeConnection(
|
||||
_Glue(getPeer=getPeer, write=self.proto.write,
|
||||
loseConnection=loseConnection,
|
||||
name="Chained Proto Transport"))
|
||||
|
||||
self.chainedProtocol.terminalProtocol.terminalSize(width, height)
|
||||
|
||||
|
||||
def getKeyPair(pubkeyfile, privkeyfile):
|
||||
"""
|
||||
This function looks for RSA keypair files in the current directory. If they
|
||||
do not exist, the keypair is created.
|
||||
"""
|
||||
|
||||
if not (os.path.exists(pubkeyfile) and os.path.exists(privkeyfile)):
|
||||
# No keypair exists. Generate a new RSA keypair
|
||||
print " Generating SSH RSA keypair ...",
|
||||
from Crypto.PublicKey import RSA
|
||||
|
||||
KEY_LENGTH = 1024
|
||||
rsaKey = Key(RSA.generate(KEY_LENGTH))
|
||||
publicKeyString = rsaKey.public().toString(type="OPENSSH")
|
||||
privateKeyString = rsaKey.toString(type="OPENSSH")
|
||||
|
||||
# save keys for the future.
|
||||
file(pubkeyfile, 'w+b').write(publicKeyString)
|
||||
file(privkeyfile, 'w+b').write(privateKeyString)
|
||||
print " done."
|
||||
else:
|
||||
publicKeyString = file(pubkeyfile).read()
|
||||
privateKeyString = file(privkeyfile).read()
|
||||
|
||||
return Key.fromString(publicKeyString), Key.fromString(privateKeyString)
|
||||
|
||||
|
||||
def makeFactory(configdict):
|
||||
"""
|
||||
Creates the ssh server factory.
|
||||
"""
|
||||
|
||||
pubkeyfile = "ssh-public.key"
|
||||
privkeyfile = "ssh-private.key"
|
||||
|
||||
def chainProtocolFactory(username=None):
|
||||
return insults.ServerProtocol(
|
||||
configdict['protocolFactory'],
|
||||
*configdict.get('protocolConfigdict', (username,)),
|
||||
**configdict.get('protocolKwArgs', {}))
|
||||
|
||||
rlm = PassAvatarIdTerminalRealm()
|
||||
rlm.transportFactory = TerminalSessionTransport_getPeer
|
||||
rlm.chainedProtocolFactory = chainProtocolFactory
|
||||
factory = ConchFactory(Portal(rlm))
|
||||
factory.sessionhandler = configdict['sessions']
|
||||
|
||||
try:
|
||||
# create/get RSA keypair
|
||||
publicKey, privateKey = getKeyPair(pubkeyfile, privkeyfile)
|
||||
factory.publicKeys = {'ssh-rsa': publicKey}
|
||||
factory.privateKeys = {'ssh-rsa': privateKey}
|
||||
except Exception, e:
|
||||
print " getKeyPair error: %(e)s\n WARNING: Evennia could not auto-generate SSH keypair. Using conch default keys instead." % {'e': e}
|
||||
print " If this error persists, create game/%(pub)s and game/%(priv)s yourself using third-party tools." % {'pub': pubkeyfile, 'priv': privkeyfile}
|
||||
|
||||
factory.services = factory.services.copy()
|
||||
factory.services['ssh-userauth'] = ExtraInfoAuthServer
|
||||
|
||||
factory.portal.registerChecker(PlayerDBPasswordChecker(factory))
|
||||
|
||||
return factory
|
||||
84
lib/server/portal/ssl.py
Normal file
84
lib/server/portal/ssl.py
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
"""
|
||||
This is a simple context factory for auto-creating
|
||||
SSL keys and certificates.
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
from twisted.internet import ssl as twisted_ssl
|
||||
try:
|
||||
import OpenSSL
|
||||
except ImportError:
|
||||
print " SSL_ENABLED requires PyOpenSSL."
|
||||
sys.exit(5)
|
||||
|
||||
from src.server.portal.telnet import TelnetProtocol
|
||||
|
||||
|
||||
class SSLProtocol(TelnetProtocol):
|
||||
"""
|
||||
Communication is the same as telnet, except data transfer
|
||||
is done with encryption.
|
||||
"""
|
||||
pass
|
||||
|
||||
|
||||
def verify_SSL_key_and_cert(keyfile, certfile):
|
||||
"""
|
||||
This function looks for RSA key and certificate in the current
|
||||
directory. If files ssl.key and ssl.cert does not exist, they
|
||||
are created.
|
||||
"""
|
||||
|
||||
if not (os.path.exists(keyfile) and os.path.exists(certfile)):
|
||||
# key/cert does not exist. Create.
|
||||
import subprocess
|
||||
from Crypto.PublicKey import RSA
|
||||
from twisted.conch.ssh.keys import Key
|
||||
|
||||
print " Creating SSL key and certificate ... ",
|
||||
|
||||
try:
|
||||
# create the RSA key and store it.
|
||||
KEY_LENGTH = 1024
|
||||
rsaKey = Key(RSA.generate(KEY_LENGTH))
|
||||
keyString = rsaKey.toString(type="OPENSSH")
|
||||
file(keyfile, 'w+b').write(keyString)
|
||||
except Exception, e:
|
||||
print "rsaKey error: %(e)s\n WARNING: Evennia could not auto-generate SSL private key." % {'e': e}
|
||||
print "If this error persists, create game/%(keyfile)s yourself using third-party tools." % {'keyfile': keyfile}
|
||||
sys.exit(5)
|
||||
|
||||
# try to create the certificate
|
||||
CERT_EXPIRE = 365 * 20 # twenty years validity
|
||||
# default:
|
||||
#openssl req -new -x509 -key ssl.key -out ssl.cert -days 7300
|
||||
exestring = "openssl req -new -x509 -key %s -out %s -days %s" % (keyfile, certfile, CERT_EXPIRE)
|
||||
#print "exestring:", exestring
|
||||
try:
|
||||
#, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
|
||||
subprocess.call(exestring)
|
||||
except OSError, e:
|
||||
string = "\n".join([
|
||||
" %s\n" % e,
|
||||
" Evennia's SSL context factory could not automatically",
|
||||
" create an SSL certificate game/%(cert)s." % {'cert': certfile},
|
||||
" A private key 'ssl.key' was already created. Please",
|
||||
" create %(cert)s manually using the commands valid" % {'cert': certfile},
|
||||
" for your operating system.",
|
||||
" Example (linux, using the openssl program): ",
|
||||
" %s" % exestring])
|
||||
print string
|
||||
sys.exit(5)
|
||||
print "done."
|
||||
|
||||
|
||||
def getSSLContext():
|
||||
"""
|
||||
Returns an SSL context (key and certificate). This function
|
||||
verifies that key/cert exists before obtaining the context, and if
|
||||
not, creates them.
|
||||
"""
|
||||
keyfile, certfile = "ssl.key", "ssl.cert"
|
||||
verify_SSL_key_and_cert(keyfile, certfile)
|
||||
return twisted_ssl.DefaultOpenSSLContextFactory(keyfile, certfile)
|
||||
284
lib/server/portal/telnet.py
Normal file
284
lib/server/portal/telnet.py
Normal file
|
|
@ -0,0 +1,284 @@
|
|||
"""
|
||||
This module implements the telnet protocol.
|
||||
|
||||
This depends on a generic session module that implements
|
||||
the actual login procedure of the game, tracks
|
||||
sessions etc.
|
||||
|
||||
"""
|
||||
|
||||
import re
|
||||
from twisted.conch.telnet import Telnet, StatefulTelnetProtocol, IAC, LINEMODE, GA, WILL, WONT, ECHO
|
||||
from src.server.session import Session
|
||||
from src.server.portal import ttype, mssp, msdp, naws
|
||||
from src.server.portal.mccp import Mccp, mccp_compress, MCCP
|
||||
from src.server.portal.mxp import Mxp, mxp_parse
|
||||
from src.utils import utils, ansi, logger
|
||||
|
||||
_RE_N = re.compile(r"\{n$")
|
||||
_RE_LEND = re.compile(r"\n$|\r$", re.MULTILINE)
|
||||
|
||||
class TelnetProtocol(Telnet, StatefulTelnetProtocol, Session):
|
||||
"""
|
||||
Each player connecting over telnet (ie using most traditional mud
|
||||
clients) gets a telnet protocol instance assigned to them. All
|
||||
communication between game and player goes through here.
|
||||
"""
|
||||
def connectionMade(self):
|
||||
"""
|
||||
This is called when the connection is first
|
||||
established.
|
||||
"""
|
||||
# initialize the session
|
||||
self.iaw_mode = False
|
||||
self.no_lb_mode = False
|
||||
client_address = self.transport.client
|
||||
# this number is counted down for every handshake that completes.
|
||||
# when it reaches 0 the portal/server syncs their data
|
||||
self.handshakes = 6 # naws, ttype, mccp, mssp, msdp, mxp
|
||||
self.init_session("telnet", client_address, self.factory.sessionhandler)
|
||||
|
||||
# negotiate client size
|
||||
self.naws = naws.Naws(self)
|
||||
# negotiate ttype (client info)
|
||||
# Obs: mudlet ttype does not seem to work if we start mccp before ttype. /Griatch
|
||||
self.ttype = ttype.Ttype(self)
|
||||
# negotiate mccp (data compression) - turn this off for wireshark analysis
|
||||
self.mccp = Mccp(self)
|
||||
# negotiate mssp (crawler communication)
|
||||
self.mssp = mssp.Mssp(self)
|
||||
# msdp
|
||||
self.msdp = msdp.Msdp(self)
|
||||
# mxp support
|
||||
self.mxp = Mxp(self)
|
||||
# keepalive watches for dead links
|
||||
self.transport.setTcpKeepAlive(1)
|
||||
# add this new connection to sessionhandler so
|
||||
# the Server becomes aware of it.
|
||||
self.sessionhandler.connect(self)
|
||||
|
||||
# timeout the handshakes in case the client doesn't reply at all
|
||||
from src.utils.utils import delay
|
||||
delay(2, callback=self.handshake_done, retval=True)
|
||||
|
||||
def handshake_done(self, force=False):
|
||||
"""
|
||||
This is called by all telnet extensions once they are finished.
|
||||
When all have reported, a sync with the server is performed.
|
||||
The system will force-call this sync after a small time to handle
|
||||
clients that don't reply to handshakes at all.
|
||||
info - debug text from the protocol calling
|
||||
"""
|
||||
if self.handshakes > 0:
|
||||
if force:
|
||||
self.sessionhandler.sync(self)
|
||||
return
|
||||
self.handshakes -= 1
|
||||
if self.handshakes <= 0:
|
||||
# do the sync
|
||||
self.sessionhandler.sync(self)
|
||||
|
||||
def enableRemote(self, option):
|
||||
"""
|
||||
This sets up the remote-activated options we allow for this protocol.
|
||||
"""
|
||||
pass
|
||||
return (option == LINEMODE or
|
||||
option == ttype.TTYPE or
|
||||
option == naws.NAWS or
|
||||
option == MCCP or
|
||||
option == mssp.MSSP)
|
||||
|
||||
def enableLocal(self, option):
|
||||
"""
|
||||
Call to allow the activation of options for this protocol
|
||||
"""
|
||||
return (option == MCCP or option==ECHO)
|
||||
|
||||
def disableLocal(self, option):
|
||||
"""
|
||||
Disable a given option
|
||||
"""
|
||||
if option == ECHO:
|
||||
return True
|
||||
if option == MCCP:
|
||||
self.mccp.no_mccp(option)
|
||||
return True
|
||||
else:
|
||||
return super(TelnetProtocol, self).disableLocal(option)
|
||||
|
||||
def connectionLost(self, reason):
|
||||
"""
|
||||
this is executed when the connection is lost for
|
||||
whatever reason. it can also be called directly, from
|
||||
the disconnect method
|
||||
"""
|
||||
self.sessionhandler.disconnect(self)
|
||||
self.transport.loseConnection()
|
||||
|
||||
def dataReceived(self, data):
|
||||
"""
|
||||
This method will split the incoming data depending on if it
|
||||
starts with IAC (a telnet command) or not. All other data will
|
||||
be handled in line mode. Some clients also sends an erroneous
|
||||
line break after IAC, which we must watch out for.
|
||||
|
||||
OOB protocols (MSDP etc) already intercept subnegotiations
|
||||
on their own, never entering this method. They will relay
|
||||
their parsed data directly to self.data_in.
|
||||
|
||||
"""
|
||||
|
||||
if data and data[0] == IAC or self.iaw_mode:
|
||||
try:
|
||||
#print "IAC mode"
|
||||
super(TelnetProtocol, self).dataReceived(data)
|
||||
if len(data) == 1:
|
||||
self.iaw_mode = True
|
||||
else:
|
||||
self.iaw_mode = False
|
||||
return
|
||||
except Exception, err1:
|
||||
conv = ""
|
||||
try:
|
||||
for b in data:
|
||||
conv += " " + repr(ord(b))
|
||||
except Exception, err2:
|
||||
conv = str(err2) + ":", str(data)
|
||||
out = "Telnet Error (%s): %s (%s)" % (err1, data, conv)
|
||||
logger.log_trace(out)
|
||||
return
|
||||
|
||||
if self.no_lb_mode and _RE_LEND.match(data):
|
||||
# we are in no_lb_mode and we get a single line break
|
||||
# - this line break should have come with the previous
|
||||
# command - it was already added so we drop it here
|
||||
self.no_lb_mode = False
|
||||
return
|
||||
elif not _RE_LEND.search(data):
|
||||
# no line break at the end of the command, note this.
|
||||
data = data.rstrip("\r\n") + "\n"
|
||||
self.no_lb_mode = True
|
||||
|
||||
# if we get to this point the command should end with a linebreak.
|
||||
# We make sure to add it, to fix some clients messing this up.
|
||||
#print "line data in:", repr(data)
|
||||
StatefulTelnetProtocol.dataReceived(self, data)
|
||||
|
||||
def _write(self, data):
|
||||
"hook overloading the one used in plain telnet"
|
||||
# print "_write (%s): %s" % (self.state, " ".join(str(ord(c)) for c in data))
|
||||
data = data.replace('\n', '\r\n').replace('\r\r\n', '\r\n')
|
||||
#data = data.replace('\n', '\r\n')
|
||||
super(TelnetProtocol, self)._write(mccp_compress(self, data))
|
||||
|
||||
def sendLine(self, line):
|
||||
"hook overloading the one used by linereceiver"
|
||||
#print "sendLine (%s):\n%s" % (self.state, line)
|
||||
#escape IAC in line mode, and correctly add \r\n
|
||||
line += self.delimiter
|
||||
line = line.replace(IAC, IAC + IAC).replace('\n', '\r\n')
|
||||
return self.transport.write(mccp_compress(self, line))
|
||||
|
||||
def lineReceived(self, string):
|
||||
"""
|
||||
Telnet method called when data is coming in over the telnet
|
||||
connection. We pass it on to the game engine directly.
|
||||
"""
|
||||
self.data_in(text=string)
|
||||
|
||||
# Session hooks
|
||||
|
||||
def disconnect(self, reason=None):
|
||||
"""
|
||||
generic hook for the engine to call in order to
|
||||
disconnect this protocol.
|
||||
"""
|
||||
if reason:
|
||||
self.data_out(reason)
|
||||
self.connectionLost(reason)
|
||||
|
||||
def data_in(self, text=None, **kwargs):
|
||||
"""
|
||||
Data Telnet -> Server
|
||||
"""
|
||||
self.sessionhandler.data_in(self, text=text, **kwargs)
|
||||
|
||||
def data_out(self, text=None, **kwargs):
|
||||
"""
|
||||
Data Evennia -> Player.
|
||||
generic hook method for engine to call in order to send data
|
||||
through the telnet connection.
|
||||
|
||||
valid telnet kwargs:
|
||||
oob=<string> - supply an Out-of-Band instruction.
|
||||
xterm256=True/False - enforce xterm256 setting. If not
|
||||
given, ttype result is used. If
|
||||
client does not suport xterm256, the
|
||||
ansi fallback will be used
|
||||
mxp=True/False - enforce mxp setting. If not given, enables if we
|
||||
detected client support for it
|
||||
ansi=True/False - enforce ansi setting. If not given,
|
||||
ttype result is used.
|
||||
nomarkup=True - strip all ansi markup (this is the same as
|
||||
xterm256=False, ansi=False)
|
||||
raw=True - pass string through without any ansi
|
||||
processing (i.e. include Evennia ansi markers but do
|
||||
not convert them into ansi tokens)
|
||||
prompt=<string> - supply a prompt text which gets sent without a
|
||||
newline added to the end
|
||||
echo=True/False
|
||||
The telnet ttype negotiation flags, if any, are used if no kwargs
|
||||
are given.
|
||||
"""
|
||||
try:
|
||||
text = utils.to_str(text if text else "", encoding=self.encoding)
|
||||
except Exception, e:
|
||||
self.sendLine(str(e))
|
||||
return
|
||||
if "oob" in kwargs:
|
||||
oobstruct = self.sessionhandler.oobstruct_parser(kwargs.pop("oob"))
|
||||
if "MSDP" in self.protocol_flags:
|
||||
for cmdname, args, kwargs in oobstruct:
|
||||
#print "cmdname, args, kwargs:", cmdname, args, kwargs
|
||||
msdp_string = self.msdp.evennia_to_msdp(cmdname, *args, **kwargs)
|
||||
#print "msdp_string:", msdp_string
|
||||
self.msdp.data_out(msdp_string)
|
||||
|
||||
# parse **kwargs, falling back to ttype if nothing is given explicitly
|
||||
ttype = self.protocol_flags.get('TTYPE', {})
|
||||
xterm256 = kwargs.get("xterm256", ttype.get('256 COLORS', False) if ttype.get("init_done") else True)
|
||||
useansi = kwargs.get("ansi", ttype and ttype.get('ANSI', False) if ttype.get("init_done") else True)
|
||||
raw = kwargs.get("raw", False)
|
||||
nomarkup = kwargs.get("nomarkup", not (xterm256 or useansi))
|
||||
prompt = kwargs.get("prompt")
|
||||
echo = kwargs.get("echo", None)
|
||||
mxp = kwargs.get("mxp", self.protocol_flags.get("MXP", False))
|
||||
|
||||
#print "telnet kwargs=%s, message=%s" % (kwargs, text)
|
||||
#print "xterm256=%s, useansi=%s, raw=%s, nomarkup=%s, init_done=%s" % (xterm256, useansi, raw, nomarkup, ttype.get("init_done"))
|
||||
if raw:
|
||||
# no processing whatsoever
|
||||
self.sendLine(text)
|
||||
elif text:
|
||||
# we need to make sure to kill the color at the end in order
|
||||
# to match the webclient output.
|
||||
#print "telnet data out:", self.protocol_flags, id(self.protocol_flags), id(self), "nomarkup: %s, xterm256: %s" % (nomarkup, xterm256)
|
||||
linetosend = ansi.parse_ansi(_RE_N.sub("", text) + "{n", strip_ansi=nomarkup, xterm256=xterm256, mxp=mxp)
|
||||
if mxp:
|
||||
linetosend = mxp_parse(linetosend)
|
||||
self.sendLine(linetosend)
|
||||
|
||||
if prompt:
|
||||
# Send prompt separately
|
||||
prompt = ansi.parse_ansi(_RE_N.sub("", prompt) + "{n", strip_ansi=nomarkup, xterm256=xterm256)
|
||||
if mxp:
|
||||
prompt = mxp_parse(prompt)
|
||||
prompt = prompt.replace(IAC, IAC + IAC).replace('\n', '\r\n')
|
||||
prompt += IAC + GA
|
||||
self.transport.write(mccp_compress(self, prompt))
|
||||
if echo:
|
||||
self.transport.write(mccp_compress(self, IAC+WONT+ECHO))
|
||||
elif echo == False:
|
||||
self.transport.write(mccp_compress(self, IAC+WILL+ECHO))
|
||||
|
||||
145
lib/server/portal/ttype.py
Normal file
145
lib/server/portal/ttype.py
Normal file
|
|
@ -0,0 +1,145 @@
|
|||
"""
|
||||
TTYPE (MTTS) - Mud Terminal Type Standard
|
||||
|
||||
This module implements the TTYPE telnet protocol as per
|
||||
http://tintin.sourceforge.net/mtts/. It allows the server to ask the
|
||||
client about its capabilities. If the client also supports TTYPE, it
|
||||
will return with information such as its name, if it supports colour
|
||||
etc. If the client does not support TTYPE, this will be ignored.
|
||||
|
||||
All data will be stored on the protocol's protocol_flags dictionary,
|
||||
under the 'TTYPE' key.
|
||||
"""
|
||||
|
||||
# telnet option codes
|
||||
TTYPE = chr(24)
|
||||
IS = chr(0)
|
||||
SEND = chr(1)
|
||||
|
||||
# terminal capabilities and their codes
|
||||
MTTS = [(128, 'PROXY'),
|
||||
(64, 'SCREEN READER'),
|
||||
(32, 'OSC COLOR PALETTE'),
|
||||
(16, 'MOUSE TRACKING'),
|
||||
(8, '256 COLORS'),
|
||||
(4, 'UTF-8'),
|
||||
(2, 'VT100'),
|
||||
(1, 'ANSI')]
|
||||
|
||||
class Ttype(object):
|
||||
"""
|
||||
Handles ttype negotiations. Called and initiated by the
|
||||
telnet protocol.
|
||||
"""
|
||||
def __init__(self, protocol):
|
||||
"""
|
||||
initialize ttype by storing protocol on ourselves and calling
|
||||
the client to see if it supporst ttype.
|
||||
|
||||
the ttype_step indicates how far in the data retrieval we've
|
||||
gotten.
|
||||
"""
|
||||
self.ttype_step = 0
|
||||
self.protocol = protocol
|
||||
self.protocol.protocol_flags['TTYPE'] = {"init_done": False}
|
||||
# is it a safe bet to assume ANSI is always supported?
|
||||
self.protocol.protocol_flags['TTYPE']['ANSI'] = True
|
||||
# setup protocol to handle ttype initialization and negotiation
|
||||
self.protocol.negotiationMap[TTYPE] = self.will_ttype
|
||||
# ask if client will ttype, connect callback if it does.
|
||||
self.protocol.do(TTYPE).addCallbacks(self.will_ttype, self.wont_ttype)
|
||||
|
||||
def wont_ttype(self, option):
|
||||
"""
|
||||
Callback if ttype is not supported by client.
|
||||
"""
|
||||
self.protocol.protocol_flags['TTYPE']["init_done"] = True
|
||||
self.protocol.handshake_done()
|
||||
|
||||
def will_ttype(self, option):
|
||||
"""
|
||||
Handles negotiation of the ttype protocol once the
|
||||
client has confirmed that it will respond with the ttype
|
||||
protocol.
|
||||
|
||||
The negotiation proceeds in several steps, each returning a
|
||||
certain piece of information about the client. All data is
|
||||
stored on protocol.protocol_flags under the TTYPE key.
|
||||
"""
|
||||
options = self.protocol.protocol_flags.get('TTYPE')
|
||||
|
||||
if options and options.get('init_done') or self.ttype_step > 3:
|
||||
return
|
||||
|
||||
try:
|
||||
option = "".join(option).lstrip(IS)
|
||||
except TypeError:
|
||||
pass
|
||||
|
||||
#print "incoming TTYPE option:", option
|
||||
|
||||
if self.ttype_step == 0:
|
||||
# just start the request chain
|
||||
self.protocol.requestNegotiation(TTYPE, SEND)
|
||||
|
||||
elif self.ttype_step == 1:
|
||||
# this is supposed to be the name of the client/terminal.
|
||||
# For clients not supporting the extended TTYPE
|
||||
# definition, subsequent calls will just repeat-return this.
|
||||
clientname = option.upper()
|
||||
# use name to identify support for xterm256. Many of these
|
||||
# only support after a certain version, but all support
|
||||
# it since at least 4 years. We assume recent client here for now.
|
||||
xterm256 = False
|
||||
if clientname.startswith("MUDLET"):
|
||||
# supports xterm256 stably since 1.1 (2010?)
|
||||
xterm256 = clientname.split("MUDLET",1)[1].strip() >= "1.1"
|
||||
else:
|
||||
xterm256 = (clientname.startswith("XTERM") or
|
||||
clientname.endswith("-256COLOR") or
|
||||
clientname in ("ATLANTIS", # > 0.9.9.0 (aug 2009)
|
||||
"CMUD", # > 3.04 (mar 2009)
|
||||
"KILDCLIENT", # > 2.2.0 (sep 2005)
|
||||
"MUDLET", # > beta 15 (sep 2009)
|
||||
"MUSHCLIENT", # > 4.02 (apr 2007)
|
||||
"PUTTY", # > 0.58 (apr 2005)
|
||||
"BEIP")) # > 2.00.206 (late 2009) (BeipMu)
|
||||
|
||||
# all clients supporting TTYPE at all seem to support ANSI
|
||||
self.protocol.protocol_flags['TTYPE']['ANSI'] = True
|
||||
self.protocol.protocol_flags['TTYPE']['256 COLORS'] = xterm256
|
||||
self.protocol.protocol_flags['TTYPE']['CLIENTNAME'] = clientname
|
||||
self.protocol.requestNegotiation(TTYPE, SEND)
|
||||
|
||||
elif self.ttype_step == 2:
|
||||
# this is a term capabilities flag
|
||||
term = option
|
||||
# identify xterm256 based on flag
|
||||
xterm256 = (term.endswith("-256color") # Apple Terminal, old Tintin
|
||||
or term.endswith("xterm") and # old Tintin, Putty
|
||||
not term.endswith("-color"))
|
||||
if xterm256:
|
||||
self.protocol.protocol_flags['TTYPE']['ANSI'] = True
|
||||
self.protocol.protocol_flags['TTYPE']['256 COLORS'] = xterm256
|
||||
self.protocol.protocol_flags['TTYPE']['TERM'] = term
|
||||
# request next information
|
||||
self.protocol.requestNegotiation(TTYPE, SEND)
|
||||
|
||||
elif self.ttype_step == 3:
|
||||
# the MTTS bitstring identifying term capabilities
|
||||
if option.startswith("MTTS"):
|
||||
option = option.split(" ")[1]
|
||||
if option.isdigit():
|
||||
# a number - determine the actual capabilities
|
||||
option = int(option)
|
||||
support = dict((capability, True) for bitval, capability in MTTS if option & bitval > 0)
|
||||
self.protocol.protocol_flags['TTYPE'].update(support)
|
||||
else:
|
||||
# some clients send erroneous MTTS as a string. Add directly.
|
||||
self.protocol.protocol_flags['TTYPE'][option.upper()] = True
|
||||
|
||||
self.protocol.protocol_flags['TTYPE']['init_done'] = True
|
||||
# print "TTYPE final:", self.protocol.protocol_flags['TTYPE']
|
||||
# we must sync ttype once it'd done
|
||||
self.protocol.handshake_done()
|
||||
self.ttype_step += 1
|
||||
252
lib/server/portal/webclient.py
Normal file
252
lib/server/portal/webclient.py
Normal file
|
|
@ -0,0 +1,252 @@
|
|||
"""
|
||||
Web client server resource.
|
||||
|
||||
The Evennia web client consists of two components running
|
||||
on twisted and django. They are both a part of the Evennia
|
||||
website url tree (so the testing website might be located
|
||||
on http://localhost:8000/, whereas the webclient can be
|
||||
found on http://localhost:8000/webclient.)
|
||||
|
||||
/webclient - this url is handled through django's template
|
||||
system and serves the html page for the client
|
||||
itself along with its javascript chat program.
|
||||
/webclientdata - this url is called by the ajax chat using
|
||||
POST requests (long-polling when necessary)
|
||||
The WebClient resource in this module will
|
||||
handle these requests and act as a gateway
|
||||
to sessions connected over the webclient.
|
||||
"""
|
||||
import time
|
||||
import json
|
||||
|
||||
from hashlib import md5
|
||||
|
||||
from twisted.web import server, resource
|
||||
|
||||
from django.utils.functional import Promise
|
||||
from django.utils.encoding import force_unicode
|
||||
from django.conf import settings
|
||||
from src.utils import utils, logger
|
||||
from src.utils.text2html import parse_html
|
||||
from src.server import session
|
||||
|
||||
SERVERNAME = settings.SERVERNAME
|
||||
ENCODINGS = settings.ENCODINGS
|
||||
|
||||
# defining a simple json encoder for returning
|
||||
# django data to the client. Might need to
|
||||
# extend this if one wants to send more
|
||||
# complex database objects too.
|
||||
|
||||
class LazyEncoder(json.JSONEncoder):
|
||||
def default(self, obj):
|
||||
if isinstance(obj, Promise):
|
||||
return force_unicode(obj)
|
||||
return super(LazyEncoder, self).default(obj)
|
||||
|
||||
|
||||
def jsonify(obj):
|
||||
return utils.to_str(json.dumps(obj, ensure_ascii=False, cls=LazyEncoder))
|
||||
|
||||
|
||||
#
|
||||
# WebClient resource - this is called by the ajax client
|
||||
# using POST requests to /webclientdata.
|
||||
#
|
||||
|
||||
class WebClient(resource.Resource):
|
||||
"""
|
||||
An ajax/comet long-polling transport
|
||||
"""
|
||||
isLeaf = True
|
||||
allowedMethods = ('POST',)
|
||||
|
||||
def __init__(self):
|
||||
self.requests = {}
|
||||
self.databuffer = {}
|
||||
|
||||
#def getChild(self, path, request):
|
||||
# """
|
||||
# This is the place to put dynamic content.
|
||||
# """
|
||||
# return self
|
||||
|
||||
def _responseFailed(self, failure, suid, request):
|
||||
"callback if a request is lost/timed out"
|
||||
try:
|
||||
del self.requests[suid]
|
||||
except KeyError:
|
||||
pass
|
||||
|
||||
def lineSend(self, suid, string, data=None):
|
||||
"""
|
||||
This adds the data to the buffer and/or sends it to
|
||||
the client as soon as possible.
|
||||
"""
|
||||
request = self.requests.get(suid)
|
||||
if request:
|
||||
# we have a request waiting. Return immediately.
|
||||
request.write(jsonify({'msg': string, 'data': data}))
|
||||
request.finish()
|
||||
del self.requests[suid]
|
||||
else:
|
||||
# no waiting request. Store data in buffer
|
||||
dataentries = self.databuffer.get(suid, [])
|
||||
dataentries.append(jsonify({'msg': string, 'data': data}))
|
||||
self.databuffer[suid] = dataentries
|
||||
|
||||
def client_disconnect(self, suid):
|
||||
"""
|
||||
Disconnect session with given suid.
|
||||
"""
|
||||
if suid in self.requests:
|
||||
self.requests[suid].finish()
|
||||
del self.requests[suid]
|
||||
if suid in self.databuffer:
|
||||
del self.databuffer[suid]
|
||||
|
||||
def mode_init(self, request):
|
||||
"""
|
||||
This is called by render_POST when the client
|
||||
requests an init mode operation (at startup)
|
||||
"""
|
||||
#csess = request.getSession() # obs, this is a cookie, not
|
||||
# an evennia session!
|
||||
#csees.expireCallbacks.append(lambda : )
|
||||
suid = request.args.get('suid', ['0'])[0]
|
||||
|
||||
remote_addr = request.getClientIP()
|
||||
host_string = "%s (%s:%s)" % (SERVERNAME, request.getRequestHostname(), request.getHost().port)
|
||||
if suid == '0':
|
||||
# creating a unique id hash string
|
||||
suid = md5(str(time.time())).hexdigest()
|
||||
self.databuffer[suid] = []
|
||||
|
||||
sess = WebClientSession()
|
||||
sess.client = self
|
||||
sess.init_session("webclient", remote_addr, self.sessionhandler)
|
||||
sess.suid = suid
|
||||
sess.sessionhandler.connect(sess)
|
||||
return jsonify({'msg': host_string, 'suid': suid})
|
||||
|
||||
def mode_input(self, request):
|
||||
"""
|
||||
This is called by render_POST when the client
|
||||
is sending data to the server.
|
||||
"""
|
||||
suid = request.args.get('suid', ['0'])[0]
|
||||
if suid == '0':
|
||||
return ''
|
||||
sess = self.sessionhandler.session_from_suid(suid)
|
||||
if sess:
|
||||
sess = sess[0]
|
||||
text = request.args.get('msg', [''])[0]
|
||||
data = request.args.get('data', [None])[0]
|
||||
sess.sessionhandler.data_in(sess, text, data=data)
|
||||
return ''
|
||||
|
||||
def mode_receive(self, request):
|
||||
"""
|
||||
This is called by render_POST when the client is telling us
|
||||
that it is ready to receive data as soon as it is
|
||||
available. This is the basis of a long-polling (comet)
|
||||
mechanism: the server will wait to reply until data is
|
||||
available.
|
||||
"""
|
||||
suid = request.args.get('suid', ['0'])[0]
|
||||
if suid == '0':
|
||||
return ''
|
||||
|
||||
dataentries = self.databuffer.get(suid, [])
|
||||
if dataentries:
|
||||
return dataentries.pop(0)
|
||||
request.notifyFinish().addErrback(self._responseFailed, suid, request)
|
||||
if suid in self.requests:
|
||||
self.requests[suid].finish() # Clear any stale request.
|
||||
self.requests[suid] = request
|
||||
return server.NOT_DONE_YET
|
||||
|
||||
def mode_close(self, request):
|
||||
"""
|
||||
This is called by render_POST when the client is signalling
|
||||
that it is about to be closed.
|
||||
"""
|
||||
suid = request.args.get('suid', ['0'])[0]
|
||||
if suid == '0':
|
||||
self.client_disconnect(suid)
|
||||
else:
|
||||
try:
|
||||
sess = self.sessionhandler.session_from_suid(suid)[0]
|
||||
sess.sessionhandler.disconnect(sess)
|
||||
except IndexError:
|
||||
self.client_disconnect(suid)
|
||||
pass
|
||||
return ''
|
||||
|
||||
def render_POST(self, request):
|
||||
"""
|
||||
This function is what Twisted calls with POST requests coming
|
||||
in from the ajax client. The requests should be tagged with
|
||||
different modes depending on what needs to be done, such as
|
||||
initializing or sending/receving data through the request. It
|
||||
uses a long-polling mechanism to avoid sending data unless
|
||||
there is actual data available.
|
||||
"""
|
||||
dmode = request.args.get('mode', [None])[0]
|
||||
if dmode == 'init':
|
||||
# startup. Setup the server.
|
||||
return self.mode_init(request)
|
||||
elif dmode == 'input':
|
||||
# input from the client to the server
|
||||
return self.mode_input(request)
|
||||
elif dmode == 'receive':
|
||||
# the client is waiting to receive data.
|
||||
return self.mode_receive(request)
|
||||
elif dmode == 'close':
|
||||
# the client is closing
|
||||
return self.mode_close(request)
|
||||
else:
|
||||
# this should not happen if client sends valid data.
|
||||
return ''
|
||||
|
||||
|
||||
#
|
||||
# A session type handling communication over the
|
||||
# web client interface.
|
||||
#
|
||||
|
||||
class WebClientSession(session.Session):
|
||||
"""
|
||||
This represents a session running in a webclient.
|
||||
"""
|
||||
|
||||
def disconnect(self, reason=None):
|
||||
"""
|
||||
Disconnect from server
|
||||
"""
|
||||
if reason:
|
||||
self.client.lineSend(self.suid, reason)
|
||||
self.client.client_disconnect(self.suid)
|
||||
|
||||
def data_out(self, text=None, **kwargs):
|
||||
"""
|
||||
Data Evennia -> Player access hook.
|
||||
|
||||
webclient flags checked are
|
||||
raw=True - no parsing at all (leave ansi-to-html markers unparsed)
|
||||
nomarkup=True - clean out all ansi/html markers and tokens
|
||||
|
||||
"""
|
||||
# string handling is similar to telnet
|
||||
try:
|
||||
text = utils.to_str(text if text else "", encoding=self.encoding)
|
||||
raw = kwargs.get("raw", False)
|
||||
nomarkup = kwargs.get("nomarkup", False)
|
||||
if raw:
|
||||
self.client.lineSend(self.suid, text)
|
||||
else:
|
||||
self.client.lineSend(self.suid,
|
||||
parse_html(text, strip_ansi=nomarkup))
|
||||
return
|
||||
except Exception:
|
||||
logger.log_trace()
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue