Improved the options command to manage other aspects of the protocol_flags.

This commit is contained in:
Griatch 2016-03-25 14:31:26 +01:00
parent 11ecdef7c8
commit db1356057a

View file

@ -399,50 +399,80 @@ class CmdOption(MuxPlayerCommand):
if self.session is None: if self.session is None:
return return
flags = self.session.protocol_flags
# Display current options
if not self.args: if not self.args:
# list the option settings # list the option settings
flags = self.session.protocol_flags options = dict(flags)
keys = sorted(flags) if "SCREENWIDTH" in options:
options = "\n".join(" {w%s{n: %s" % (key, flags[key]) for key in keys) if len(options["SCREENWIDTH"]) == 1:
self.msg("{wClient settings:\n%s" % options) options["SCREENWIDTH"] = options["SCREENWIDTH"][0]
#string = "{wEncoding{n:\n" else:
#pencoding = flags.get("ENCODING", "None") options["SCREENWIDTH"] = " \n".join("%s : %s" % (screenid, size)
#sencodings = settings.ENCODINGS for screenid, size in options["SCREENWIDTH"].iteritems())
#string += " Custom: %s\n Server: %s" % (pencoding, ", ".join(sencodings)) if "SCREENHEIGHT" in options:
#string += "\n{wScreen Reader mode:{n %s" % flags.get("SCREENREADER", False) if len(options["SCREENHEIGHT"]) == 1:
## display all options["SCREENHEIGHT"] = options["SCREENHEIGHT"][0]
#keys = else:
#string += "\n{wClient settings (read-only):\n%s" % options options["SCREENHEIGHT"] = " \n".join("%s : %s" % (screenid, size)
#self.msg(string) for screenid, size in options["SCREENHEIGHT"].iteritems())
options.pop("TTYPE", None)
options = "\n".join(" {w%s{n: %s" % (key, options[key]) for key in sorted(options))
self.msg("{wClient settings:{n\n%s{n" % options)
return return
if not self.rhs: if not self.rhs:
self.msg("Usage: @option [name = [value]]") self.msg("Usage: @option [name = [value]]")
return return
sync = False # Try to assign new values
if self.lhs == "encoding": def validate_encoding(val):
# change encoding # helper: change encoding
old_encoding = self.session.protocol_flags["ENCODING"]
new_encoding = self.rhs.strip() or "utf-8"
try: try:
utils.to_str(utils.to_unicode("test-string"), encoding=new_encoding) utils.to_str(utils.to_unicode("test-string"), encoding=val)
except LookupError: except LookupError:
string = "|rThe encoding '|w%s|r' is invalid. Keeping the previous encoding '|w%s|r'.|n" % (new_encoding, old_encoding) raise RuntimeError("The encoding '|w%s|n' is invalid. " % val)
else: return val
self.session.protocol_flags["ENCODING"] = new_encoding
string = "Encoding was changed from '|w%s|n' to '|w%s|n'." % (old_encoding, new_encoding)
self.msg(string)
return
if self.lhs == "screenreader": def validate_size(val):
onoff = self.rhs.lower() == "on" return {0: int(val)}
self.session.protocol_flags["SCREENREADER"] = onoff
self.msg("Screen reader mode was turned {w%s{n." % ("on" if onoff else "off"))
sync = True
if sync: def validate_bool(val):
return True if val.lower() in ("true", "on", "1") else False
def update(name, val, validator):
# helper: update property and report errors
try:
old_val = flags[name]
new_val = validator(val)
flags[name] = new_val
self.msg("Option |w%s|n was changed from '|w%s|n' to '|w%s|n'." % (name, old_val, new_val))
return True
except Exception, err:
self.msg("|rCould not set option |w%s|r:|n %s" % (name, err))
return False
validators = {"ANSI": validate_bool,
"CLIENTNAME": utils.to_str,
"ENCODING": validate_encoding,
"MCCP": validate_bool,
"MXP": validate_bool,
"OOB": validate_bool,
"SCREENHEIGHT": validate_size,
"SCREENWIDTH": validate_size,
"SCREENREADER": validate_bool,
"TERM": utils.to_str,
"UTF-8": validate_bool,
"XTERM256": validate_bool}
name = self.lhs.upper()
val = self.rhs.strip()
if val and name in validators:
do_sync = update(name, val, validators[name])
if do_sync:
self.session.sessionhandler.session_portal_sync(self.session) self.session.sessionhandler.session_portal_sync(self.session)