From 95aa8098e66a0de5c5c702cfd4c6fd7dc536b1fc Mon Sep 17 00:00:00 2001 From: Someone Date: Mon, 5 Sep 2022 21:27:01 +0200 Subject: [PATCH] core/MMBot.py --- core/MMBot.py | 85 +++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 76 insertions(+), 9 deletions(-) diff --git a/core/MMBot.py b/core/MMBot.py index bb1e47e..c2fa396 100644 --- a/core/MMBot.py +++ b/core/MMBot.py @@ -261,6 +261,9 @@ class MMBot(): handled = False responseURL = None + def version_string(self): + return "SomeBot" + def do_POST(self): self.handled = False @@ -448,34 +451,98 @@ class MMBot(): logger.info("Starting public facing webserver.") class HTTPPublicRequestHandler(BaseHTTPRequestHandler): + bot = None + handled = False + + def version_string(self): + return "SomeBot" + def do_POST(self): self.handled = False + data = None - logger.debug("do_POST(): request incomming.") + logger.debug("do_public_POST(): request incomming.") try: module = 'not yet known' splitpath = self.path.strip("/").split("/") - if splitpath[0] in self.bot.modules: - module = self.bot.modules[splitpath[0]] + if splitpath[0] in self.bot.modules_public: + module = self.bot.modules_public[splitpath[0]] - self.bot.command_stats_inc("public::"+splitpath[0]) + self.bot.command_stats_inc("public-post::"+splitpath[0]) module._on_public_POST(self, data) else: # Invalid command/unknown command - logger.error("do_POST(): Invalid command/unknown command.") - self.bot.debug_chan("do_POST(): Invalid command/unknown command.\n# :boom::boom::boom::boom::boom:\n```\n"+self.path+"\n```") + logger.error("do_public_POST(): Invalid command/unknown command") + self.bot.debug_chan("do_public_POST(): Invalid command/unknown command.\n# :boom::boom::boom::boom::boom:\n```\n"+self.path+"\n```") self.respond_cmd_err("Invalid command/unknown command") # always try to fail to retain userinput. If previously responded to, nothing will happen. - self.respond(400, if_nonzero_secondary='ignore') + self.respond_public(400, if_nonzero_secondary='ignore') except Exception as exc: self.bot.debug_chan("##### Exception in ``"+self.path.strip("/")+"``: ``"+repr(exc)+"``\n# :boom::boom::boom::boom::boom:") - logger.exception("do_POST(): Exception in: %s\nRequest-data:%s", self.path.strip("/"), pprint.pformat(data)) - self.respond_cmd_err("A serverside error occured. @someone should have been contacted.", http_code=500, if_nonzero_secondary='ignore') + logger.exception("do_public_POST(): Exception in: %s\nRequest-data:%s", self.path.strip("/"), pprint.pformat(data)) + self.respond_public(500, {"error":"A serverside error occured. @someone should have been contacted."}, if_nonzero_secondary='ignore') + def do_GET(self): self.handled = False + data = None + + logger.debug("do_public_GET(): request incomming.") + try: + module = 'not yet known' + splitpath = self.path.strip("/").split("/") + if splitpath[0] in self.bot.modules_public: + module = self.bot.modules_public[splitpath[0]] + + self.bot.command_stats_inc("public-get::"+splitpath[0]) + module._on_public_GET(self, data) + + else: # Invalid command/unknown command + logger.error("do_public_GET(): Invalid command/unknown command") + self.bot.debug_chan("do_public_GET(): Invalid command/unknown command.\n# :boom::boom::boom::boom::boom:\n```\n"+self.path+"\n```") + self.respond_public(400, {"error":"Invalid command/unknown command"} ,if_nonzero_secondary='ignore') + + # always try to fail to retain userinput. If previously responded to, nothing will happen. + self.respond_public(400, if_nonzero_secondary='ignore') + + except Exception as exc: + self.bot.debug_chan("##### Exception in ``"+self.path.strip("/")+"``: ``"+repr(exc)+"``\n# :boom::boom::boom::boom::boom:") + logger.exception("do_public_GET(): Exception in: %s\nRequest-data:%s", self.path.strip("/"), pprint.pformat(data)) + self.respond_public(500, {"error":"A serverside error occured. @someone should have been contacted."}, if_nonzero_secondary='ignore') + + + def respond_public(self, http_code=200, data=None, if_nonzero_secondary='exc'): + """ + First response call must have a valid http code. + """ + + if data is None: + data = {} + + # First response + if not self.handled: + if http_code >= 600 or http_code < 100: + raise Exception("respond_public(): Primary response must have a valid http code.") + + self.handled = True + self.send_response(http_code) + self.send_header("Content-Type", "application/json") + self.send_header("Content-Length", len(bytes(json.dumps(data), "utf8"))) + self.end_headers() + self.wfile.write(bytes(json.dumps(data), "utf8")) + logger.debug("respond_public(): Primary response send.") + + # Secondary responses + else: + if http_code != 0 and if_nonzero_secondary == "ignore": + logger.info("respond_public(): Secondary responses must have a zero http code, but if_nonzero_secondary='ignore'. Doing nothing.") + return + + elif if_nonzero_secondary != "ignore": + raise Exception("respond_public(): Secondary responses must be ignored.") + class MyHTTPServer(ThreadingMixIn, HTTPServer): -- 2.43.0