From 0a52763d8cf3765fee839114683bea0ae80be243 Mon Sep 17 00:00:00 2001 From: Someone Date: Sat, 3 Sep 2022 18:48:40 +0200 Subject: [PATCH] core/MMBot.py --- core/MMBot.py | 82 ++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 72 insertions(+), 10 deletions(-) diff --git a/core/MMBot.py b/core/MMBot.py index 2913116..bb1e47e 100644 --- a/core/MMBot.py +++ b/core/MMBot.py @@ -26,16 +26,18 @@ logger = logging.getLogger(__name__) class MMBot(): - def __init__(self, local_websrv_hostname="localhost", local_websrv_port=18065, api_user=None, api_user_pw=None, api_bearer=None, mm_api_url="http://localhost:8065/api", mm_ws_url="ws://localhost:8065/api/v4/websocket", debug_chan_id=None): + def __init__(self, local_websrv_hostname="localhost", local_websrv_port=18065, local_public_websrv_hostname="localhost", local_public_websrv_port=18066, api_user=None, api_user_pw=None, api_bearer=None, mm_api_url="http://localhost:8065/api", mm_ws_url="ws://localhost:8065/api/v4/websocket", debug_chan_id=None): self.local_websrv_hostname = local_websrv_hostname self.local_websrv_port = local_websrv_port + self.local_public_websrv_hostname = local_public_websrv_hostname + self.local_public_websrv_port = local_public_websrv_port self.api_user = api_user self.mm_api_url = mm_api_url self.mm_ws_url = mm_ws_url - self.local_websrv_url = ("http://"+self.local_websrv_hostname+":"+str(self.local_websrv_port)+"/").strip("/") self.modules = {} self.wsmodules = {} + self.modules_public = {} self.api = None self.debug_chan_id = debug_chan_id self.command_stats = {} @@ -117,17 +119,33 @@ class MMBot(): else: raise Exception("Multiple registration attempts for module: "+module.NAME+" and evtype: "+evtype) + # Register a public facing module with the bot. + def register_public(self, module): + self.modules_public = {} + + if module.TRIGGER not in self.modules_public: + self.modules_public[module.TRIGGER] = module + module._on_register_public(self) + + else: + raise Exception("Multiple registration attempts for module: "+module.TRIGGER+" and team: "+module.TEAM_ID) def start(self): logger.info("Starting: Almost there.") logger.info(pprint.pformat(self.modules)) logger.info(pprint.pformat(self.wsmodules)) + logger.info(pprint.pformat(self.modules_public)) if self.mm_ws_url is not None: self.mmws = mattermost.ws.MMws(self.websocket_handler, self.api, self.mm_ws_url) + if self.local_public_websrv_hostname is not None and self.local_public_websrv_port is not None: + th = threading.Thread(target=self.start_webserver_public, daemon=True) + th.setName("http public") + th.start() + if self.local_websrv_hostname is not None and self.local_websrv_port is not None: - self.start_webserver() + self.start_webserver_mattermost() def on_shutdown(self): @@ -232,11 +250,11 @@ class MMBot(): logger.exception("websocket_handler(): Exception in: %s\nEvent-data:%s", evtype+"::"+module_name, pprint.pformat(event_data)) - ################### - # Bot's webserver # - ################### - def start_webserver(self): - logger.info("Starting webserver.") + ############################## + # Bot's mattermost webserver # + ############################## + def start_webserver_mattermost(self): + logger.info("Starting mattermost facing webserver.") class HTTPRequestHandler(BaseHTTPRequestHandler): bot = None @@ -244,7 +262,6 @@ class MMBot(): responseURL = None def do_POST(self): - threading.current_thread().rename_current_thread("HTTPRequestHandler", threading.current_thread()) self.handled = False if self.headers["Content-Type"] == "application/x-www-form-urlencoded": @@ -421,5 +438,50 @@ class MMBot(): HTTPServer.serve_forever(self) self.httpd = MyHTTPServer((self.local_websrv_hostname, self.local_websrv_port), HTTPRequestHandler) - threading.current_thread().rename_current_thread("HTTPServer", threading.current_thread()) + self.httpd.serve_forever(self) + + + ################################# + # Bot's public facing webserver # + ################################# + def start_webserver_public(self): + logger.info("Starting public facing webserver.") + + class HTTPPublicRequestHandler(BaseHTTPRequestHandler): + def do_POST(self): + self.handled = False + + logger.debug("do_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]] + + self.bot.command_stats_inc("public::"+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```") + 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') + + 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') + + def do_GET(self): + self.handled = False + + + class MyHTTPServer(ThreadingMixIn, HTTPServer): + def serve_forever(self, bot): + self.RequestHandlerClass.bot = bot + HTTPServer.serve_forever(self) + + self.httpd = MyHTTPServer((self.local_public_websrv_hostname, self.local_public_websrv_port), HTTPPublicRequestHandler) self.httpd.serve_forever(self) -- 2.43.0