Fix error in yesnoquestion causing lockout

This commit is contained in:
Griatch 2021-05-11 23:57:09 +02:00
parent adf484b9df
commit 770fac275d
2 changed files with 26 additions and 17 deletions

View file

@ -1029,11 +1029,12 @@ class CmdChannel(COMMAND_DEFAULT_CLASS):
ask_yes_no( ask_yes_no(
caller, caller,
f"Are you sure you want to delete channel '{channel.key}'" prompt=f"Are you sure you want to delete channel '{channel.key}' "
"(make sure name is correct!)? This will disconnect and " "(make sure name is correct!)? This will disconnect and "
"remove all users' aliases. {options}?", "remove all users' aliases. {options}?",
_perform_delete, yes_action=_perform_delete,
"Aborted." no_action="Aborted.",
default="N"
) )
if 'desc' in switches: if 'desc' in switches:
@ -1143,11 +1144,11 @@ class CmdChannel(COMMAND_DEFAULT_CLASS):
if reason else '') if reason else '')
ask_yes_no( ask_yes_no(
caller, caller,
f"Are you sure you want to boot user {target.key} from " prompt=f"Are you sure you want to boot user {target.key} from "
f"channel(s) {channames} (make sure name/channels are correct{reasonwarn}). " f"channel(s) {channames} (make sure name/channels are correct{reasonwarn}). "
"{options}?", "{options}?",
_boot_user, yes_action=_boot_user,
"Aborted.", no_action="Aborted.",
default="Y" default="Y"
) )
return return

View file

@ -1631,10 +1631,12 @@ class CmdYesNoQuestion(Command):
aliases = [_CMD_NOMATCH, "yes", "no", 'y', 'n', 'a', 'abort'] aliases = [_CMD_NOMATCH, "yes", "no", 'y', 'n', 'a', 'abort']
arg_regex = r"^$" arg_regex = r"^$"
def _clean(self): def _clean(self, caller):
del self.caller.ndb._yes_no_question del caller.ndb._yes_no_question
self.caller.cmdset.remove(YesNoQuestionCmdSet) if not caller.cmdset.has(YesNoQuestionCmdSet) and hasattr(caller, "account"):
caller.account.cmdset.remove(YesNoQuestionCmdSet)
else:
caller.cmdset.remove(YesNoQuestionCmdSet)
def func(self): def func(self):
"""This is called when user enters anything.""" """This is called when user enters anything."""
@ -1645,6 +1647,10 @@ class CmdYesNoQuestion(Command):
yes_no_question = caller.account.ndb._yes_no_question yes_no_question = caller.account.ndb._yes_no_question
caller = caller.account caller = caller.account
if not yes_no_question:
self._clean(caller)
return
inp = self.cmdname inp = self.cmdname
if inp == _CMD_NOINPUT: if inp == _CMD_NOINPUT:
@ -1657,7 +1663,7 @@ class CmdYesNoQuestion(Command):
if inp in ('a', 'abort') and yes_no_question.allow_abort: if inp in ('a', 'abort') and yes_no_question.allow_abort:
caller.msg("Aborted.") caller.msg("Aborted.")
self._clean() self._clean(caller)
return return
caller.ndb._yes_no_question.session = self.session caller.ndb._yes_no_question.session = self.session
@ -1677,12 +1683,12 @@ class CmdYesNoQuestion(Command):
return return
# cleanup # cleanup
self._clean() self._clean(caller)
except Exception as err: except Exception as err:
# make sure to clean up cmdset if something goes wrong # make sure to clean up cmdset if something goes wrong
caller.msg("|rError in ask_yes_no. Choice not confirmed (report to admin)|n") caller.msg("|rError in ask_yes_no. Choice not confirmed (report to admin)|n")
logger.log_trace("Error in ask_yes_no") logger.log_trace("Error in ask_yes_no")
self._clean() self._clean(caller)
raise raise
@ -1703,8 +1709,8 @@ class YesNoQuestionCmdSet(CmdSet):
self.add(CmdYesNoQuestion()) self.add(CmdYesNoQuestion())
def ask_yes_no(caller, prompt, yes_action, no_action, default=None, def ask_yes_no(caller, prompt="Yes or No {options}?", yes_action="Yes", no_action="No",
allow_abort=False, session=None, *args, **kwargs): default=None, allow_abort=False, session=None, *args, **kwargs):
""" """
A helper question for asking a simple yes/no question. This will cause A helper question for asking a simple yes/no question. This will cause
the system to pause and wait for input from the player. the system to pause and wait for input from the player.
@ -1721,8 +1727,9 @@ def ask_yes_no(caller, prompt, yes_action, no_action, default=None,
with `(caller, *args, **kwargs)` when the No-choice is made. with `(caller, *args, **kwargs)` when the No-choice is made.
If a string, this string will be echoed back to the caller. If a string, this string will be echoed back to the caller.
default (str optional): This is what the user will get if they just press the default (str optional): This is what the user will get if they just press the
return key without giving any input. One of 'N', 'Y', 'A' or 'None' return key without giving any input. One of 'N', 'Y', 'A' or `None`
for no default. If 'A' is given, `allow_abort` is auto-set. for no default (an explicit choice must be given). If 'A' is given,
`allow_abort` kwarg is ignored and assumed set.
allow_abort (bool, optional): If set, the 'A(bort)' option is available allow_abort (bool, optional): If set, the 'A(bort)' option is available
(a third option meaning neither yes or no but just exits the prompt). (a third option meaning neither yes or no but just exits the prompt).
session (Session, optional): This allows to specify the session (Session, optional): This allows to specify the
@ -1780,6 +1787,7 @@ def ask_yes_no(caller, prompt, yes_action, no_action, default=None,
prompt = prompt.format(options=options) prompt = prompt.format(options=options)
caller.ndb._yes_no_question = _Prompt() caller.ndb._yes_no_question = _Prompt()
caller.ndb._yes_no_question.prompt = prompt
caller.ndb._yes_no_question.session = session caller.ndb._yes_no_question.session = session
caller.ndb._yes_no_question.prompt = prompt caller.ndb._yes_no_question.prompt = prompt
caller.ndb._yes_no_question.default = default caller.ndb._yes_no_question.default = default