Update fdie and frestart to use the new command API
This commit is contained in:
parent
1b695b4849
commit
fe692dd74e
@ -138,9 +138,9 @@ def connect_callback():
|
|||||||
signal.signal(signal.SIGINT, signal.SIG_DFL)
|
signal.signal(signal.SIGINT, signal.SIG_DFL)
|
||||||
|
|
||||||
if signum in (signal.SIGINT, signal.SIGTERM):
|
if signum in (signal.SIGINT, signal.SIGTERM):
|
||||||
forced_exit.func(cli, "<console>", botconfig.CHANNEL, "") # XXX: Old API
|
forced_exit.func(var, wrapper, "")
|
||||||
elif signum == SIGUSR1:
|
elif signum == SIGUSR1:
|
||||||
restart_program.func(cli, "<console>", botconfig.CHANNEL, "") # XXX: Old API
|
restart_program.func(var, wrapper, "")
|
||||||
elif signum == SIGUSR2:
|
elif signum == SIGUSR2:
|
||||||
plog("Scheduling aftergame restart")
|
plog("Scheduling aftergame restart")
|
||||||
aftergame.func(var, wrapper, "frestart")
|
aftergame.func(var, wrapper, "frestart")
|
||||||
@ -361,11 +361,11 @@ def refreshdb(cli, nick, chan, rest):
|
|||||||
expire_tempbans()
|
expire_tempbans()
|
||||||
reply(cli, nick, chan, "Done.")
|
reply(cli, nick, chan, "Done.")
|
||||||
|
|
||||||
@cmd("die", "bye", "fdie", "fbye", flag="D", pm=True)
|
@command("fdie", "fbye", flag="D", pm=True)
|
||||||
def forced_exit(cli, nick, chan, rest): # XXX: sighandler (top of file) also needs updating alongside this one
|
def forced_exit(var, wrapper, message):
|
||||||
"""Forces the bot to close."""
|
"""Forces the bot to close."""
|
||||||
|
|
||||||
args = rest.split()
|
args = message.split()
|
||||||
|
|
||||||
# Force in debug mode by default
|
# Force in debug mode by default
|
||||||
force = botconfig.DEBUG_MODE
|
force = botconfig.DEBUG_MODE
|
||||||
@ -375,107 +375,98 @@ def forced_exit(cli, nick, chan, rest): # XXX: sighandler (top of file) also nee
|
|||||||
os.abort()
|
os.abort()
|
||||||
elif args and args[0] == "-force":
|
elif args and args[0] == "-force":
|
||||||
force = True
|
force = True
|
||||||
rest = " ".join(args[1:])
|
message = " ".join(args[1:])
|
||||||
|
|
||||||
if var.PHASE in var.GAME_PHASES:
|
if var.PHASE in var.GAME_PHASES:
|
||||||
if var.PHASE == "join" or force or nick == "<console>":
|
if var.PHASE == "join" or force or wrapper.source.nick == "<console>":
|
||||||
stop_game(cli, log=False)
|
stop_game(wrapper.client, log=False)
|
||||||
else:
|
else:
|
||||||
reply(cli, nick, chan, messages["stop_bot_ingame_safeguard"].format(
|
wrapper.pm(messages["stop_bot_ingame_safeguard"].format(
|
||||||
what="stop", cmd="fdie", prefix=botconfig.CMD_CHAR), private=True)
|
what="stop", cmd="fdie", prefix=botconfig.CMD_CHAR))
|
||||||
return
|
return
|
||||||
|
|
||||||
reset_modes_timers(cli)
|
reset_modes_timers(wrapper.client)
|
||||||
reset()
|
reset()
|
||||||
|
|
||||||
msg = "{0} quit from {1}"
|
msg = "{0} quit from {1}"
|
||||||
|
|
||||||
if rest.strip():
|
if message.strip():
|
||||||
msg += " ({2})"
|
msg += " ({2})"
|
||||||
|
|
||||||
try:
|
hooks.quit(wrapper, msg.format("Scheduled" if forced_exit.aftergame else "Forced",
|
||||||
cli.quit(msg.format("Scheduled" if forced_exit.aftergame else "Forced",
|
wrapper.source, message.strip()))
|
||||||
nick,
|
|
||||||
rest.strip()))
|
|
||||||
except Exception:
|
|
||||||
# bot may have quit by this point, so can't use regular handler
|
|
||||||
# the operator should see this on console anyway even though it isn't logged
|
|
||||||
traceback.print_exc()
|
|
||||||
sys.exit()
|
|
||||||
|
|
||||||
def _restart_program(cli, mode=None):
|
def _restart_program(mode=None):
|
||||||
plog("RESTARTING")
|
plog("RESTARTING")
|
||||||
|
|
||||||
python = sys.executable
|
python = sys.executable
|
||||||
|
|
||||||
if mode:
|
if mode is not None:
|
||||||
|
print(mode)
|
||||||
assert mode in ("normal", "verbose", "debug")
|
assert mode in ("normal", "verbose", "debug")
|
||||||
os.execl(python, python, sys.argv[0], "--{0}".format(mode))
|
os.execl(python, python, sys.argv[0], "--{0}".format(mode))
|
||||||
else:
|
else:
|
||||||
os.execl(python, python, *sys.argv)
|
os.execl(python, python, *sys.argv)
|
||||||
|
|
||||||
|
|
||||||
@cmd("restart", "frestart", flag="D", pm=True)
|
@command("restart", "frestart", flag="D", pm=True)
|
||||||
def restart_program(cli, nick, chan, rest): # XXX: sighandler (top of file) also needs updating alongside this one
|
def restart_program(var, wrapper, message):
|
||||||
"""Restarts the bot."""
|
"""Restarts the bot."""
|
||||||
|
|
||||||
args = rest.split()
|
args = message.split()
|
||||||
|
|
||||||
# Force in debug mode by default
|
# Force in debug mode by default
|
||||||
force = botconfig.DEBUG_MODE
|
force = botconfig.DEBUG_MODE
|
||||||
|
|
||||||
if args and args[0] == "-force":
|
if args and args[0] == "-force":
|
||||||
force = True
|
force = True
|
||||||
rest = " ".join(args[1:])
|
message = " ".join(args[1:])
|
||||||
|
|
||||||
if var.PHASE in var.GAME_PHASES:
|
if var.PHASE in var.GAME_PHASES:
|
||||||
if var.PHASE == "join" or force:
|
if var.PHASE == "join" or force:
|
||||||
stop_game(cli, log=False)
|
stop_game(wrapper.client, log=False)
|
||||||
else:
|
else:
|
||||||
reply(cli, nick, chan, messages["stop_bot_ingame_safeguard"].format(
|
wrapper.pm(messages["stop_bot_ingame_safeguard"].format(
|
||||||
what="restart", cmd="frestart", prefix=botconfig.CMD_CHAR), private=True)
|
what="restart", cmd="frestart", prefix=botconfig.CMD_CHAR))
|
||||||
return
|
return
|
||||||
|
|
||||||
reset_modes_timers(cli)
|
reset_modes_timers(wrapper.client)
|
||||||
db.set_pre_restart_state(list_players())
|
db.set_pre_restart_state(list_players())
|
||||||
reset()
|
reset()
|
||||||
|
|
||||||
msg = "{0} restart from {1}".format(
|
msg = "{0} restart from {1}".format(
|
||||||
"Scheduled" if restart_program.aftergame else "Forced", nick)
|
"Scheduled" if restart_program.aftergame else "Forced", wrapper.source)
|
||||||
|
|
||||||
rest = rest.strip()
|
message = message.strip()
|
||||||
mode = None
|
mode = None
|
||||||
|
|
||||||
if rest:
|
if message:
|
||||||
args = rest.split()
|
args = message.split()
|
||||||
first_arg = args[0].lower()
|
first_arg = args[0].lower()
|
||||||
|
|
||||||
if first_arg.endswith("mode") and first_arg != "mode":
|
if first_arg.endswith("mode") and first_arg != "mode":
|
||||||
mode = first_arg.replace("mode", "")
|
mode = first_arg.replace("mode", "")
|
||||||
|
|
||||||
VALID_MODES = ("normal", "verbose", "debug")
|
valid_modes = ("normal", "verbose", "debug")
|
||||||
|
|
||||||
if mode not in VALID_MODES:
|
|
||||||
err_msg = messages["invalid_mode"].format(mode, ", ".join(VALID_MODES))
|
|
||||||
reply(cli, nick, chan, err_msg, private=True)
|
|
||||||
|
|
||||||
|
if mode not in valid_modes:
|
||||||
|
wrapper.pm(messages["invalid_mode"].format(mode, ", ".join(valid_modes)))
|
||||||
return
|
return
|
||||||
|
|
||||||
msg += " in {0} mode".format(mode)
|
msg += " in {0} mode".format(mode)
|
||||||
rest = " ".join(args[1:])
|
message = " ".join(args[1:])
|
||||||
|
|
||||||
if rest:
|
if message:
|
||||||
msg += " ({0})".format(rest.strip())
|
msg += " ({0})".format(message.strip())
|
||||||
|
|
||||||
cli.quit(msg.format(nick, rest.strip()))
|
hooks.quit(wrapper, msg.format(wrapper.source, message.strip()))
|
||||||
|
|
||||||
@hook("quit")
|
def restart_buffer(evt, var, user, reason):
|
||||||
def restart_buffer(cli, raw_nick, reason):
|
|
||||||
nick, _, __, host = parse_nick(raw_nick)
|
|
||||||
# restart the bot once our quit message goes though to ensure entire IRC queue is sent
|
# restart the bot once our quit message goes though to ensure entire IRC queue is sent
|
||||||
# if the bot is using a nick that isn't botconfig.NICK, then stop breaking things and fdie
|
if user is users.Bot:
|
||||||
if nick == botconfig.NICK:
|
_restart_program(mode)
|
||||||
_restart_program(cli, mode)
|
|
||||||
|
events.add_listener("server_quit", restart_buffer)
|
||||||
|
|
||||||
# This is checked in the on_error handler. Some IRCds, such as InspIRCd, don't send the bot
|
# This is checked in the on_error handler. Some IRCds, such as InspIRCd, don't send the bot
|
||||||
# its own QUIT message, so we need to use ERROR. Ideally, we shouldn't even need the above
|
# its own QUIT message, so we need to use ERROR. Ideally, we shouldn't even need the above
|
||||||
@ -6346,7 +6337,7 @@ def start(cli, nick, chan, forced = False, restart = ""):
|
|||||||
@hook("error")
|
@hook("error")
|
||||||
def on_error(cli, pfx, msg):
|
def on_error(cli, pfx, msg):
|
||||||
if var.RESTARTING or msg.endswith("(Excess Flood)"):
|
if var.RESTARTING or msg.endswith("(Excess Flood)"):
|
||||||
_restart_program(cli)
|
_restart_program()
|
||||||
elif msg.startswith("Closing Link:"):
|
elif msg.startswith("Closing Link:"):
|
||||||
raise SystemExit
|
raise SystemExit
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user