From d13ab94d4b414c15ad505be6dda4b8300292ea2d Mon Sep 17 00:00:00 2001 From: chr Date: Thu, 2 Apr 2020 23:20:38 -0700 Subject: [PATCH] Advancements --- service.py | 40 ++++++++++++++++++++++++++++------------ wrapper.py | 35 +++++++++++++++++++++++------------ 2 files changed, 51 insertions(+), 24 deletions(-) diff --git a/service.py b/service.py index 31d9070..2d46a9a 100644 --- a/service.py +++ b/service.py @@ -41,14 +41,12 @@ def on_receive_events(transaction): and event["content"]["msgtype"] == "m.text" and event["user_id"].find("@mc_") == -1 ): - m_user = USER_RE.search(event["user_id"]).group(0) m_cont = event["content"]["body"] - m_user, m_cont if global_msg_queue is not None: global_msg_queue.add({ - "command": - '/tellraw @a {"text":"<' + m_user + '> ' + m_cont + '","insertion":"/tellraw @p %s"}', + "user": m_user, + "msg": m_cont, }) else: LOG.warning("No message queue available") @@ -135,14 +133,32 @@ class Appservice: # Not the end of the world if it fails, send the message now. # attempt to post in room - LOG.info("Attempting to post in Room") - self.api._send( - "PUT", - "/rooms/" + room + "/send/m.room.message/" + txn_id, - content={"msgtype": "m.text", "body": msg["msg"]}, - query_params={"user_id": user_id}, - headers={"Content-Type": "application/json"}, - ) + if "action" in msg: + if msg["action"] == "joined": + LOG.info("Setting {} to present".format(user_id)) + self.api._send( + "PUT", + "/presence/" + user_id + "/status", + content={"presence":"online"}, + headers={"Content-Type": "application/json"}, + ) + elif msg["action"] == "left": + LOG.info("Setting {} to offline".format(user_id)) + self.api._send( + "PUT", + "/presence/" + user_id + "/status", + content={"presence":"offline"}, + headers={"Content-Type": "application/json"}, + ) + if "msg" in msg: + LOG.info("Attempting to post in Room") + self.api._send( + "PUT", + "/rooms/" + room + "/send/m.room.message/" + txn_id, + content={"msgtype": "m.text", "body": msg["msg"]}, + query_params={"user_id": user_id}, + headers={"Content-Type": "application/json"}, + ) def get_mc_skin(self, user, user_id): LOG.info("Getting Minecraft Avatar") diff --git a/wrapper.py b/wrapper.py index 2e69b47..702ebac 100644 --- a/wrapper.py +++ b/wrapper.py @@ -41,21 +41,26 @@ class ProcessWrapper: def send_process_output( process: ProcessWrapper, msg_queue: message_queue.MessageQueue ): - # "[07:36:28] [Server thread/INFO] [minecraft/DedicatedServer]: test" log = LOG.getChild("process_output") - prog = re.compile(r"(\[.*\] )?\[(.*)\] \[(.*)\]: <(.*)> (.*)") + # "[07:36:28] [Server thread/INFO] [minecraft/DedicatedServer]: test" + chat_pattern = re.compile(r"(\[.*\] )?\[(.*)\] \[(.*)\]: <(.*)> (.*)") + # "[05:09:54] [Server thread/INFO] [minecraft/DedicatedServer]: khr_ joined the game" + login_pattern = re.compile(r"\[.*\] \[(.*)\] \[(.*)\]: ([^ ]*]) joined the game") + # "[05:12:24] [Server thread/INFO] [minecraft/DedicatedServer]: khr_ left the game" + logout_pattern = re.compile(r"\[.*\] \[(.*)\] \[(.*)\]: ([^ ]*]) left the game") for line in process: log.info(line.rstrip("\n")) - result = prog.search(line) - if result: - log.info( - "user: {} msg: {}".format( - result.group(4), result.group(5).rstrip("\n"), - ) - ) + chat_result = chat_pattern.search(line) + login_result = login_pattern.search(line) + logout_result = logout_pattern.search(line) + if chat_result: msg_queue.add( - {"user": result.group(4), "msg": result.group(5).rstrip("\n")}, + {"user": result.group(4), "action": "chat", "msg": result.group(5).rstrip("\n")}, ) + elif login_result: + msg_queue.add({"user": result.group(3), "action": "joined"}) + elif logout_result: + msg_queue.add({"user": result.group(3), "action": "left"}) def relay_queue_input( @@ -66,8 +71,14 @@ def relay_queue_input( try: for message in msg_queue: log.debug(message) - if "command" in message: - command = message["command"] + "\n" + if "msg" in message: + tellraw_params = { + "text": "<{}> {}".format( + message["user"], message["msg"] + ), + "insertion": "/tellraw @p %s", + } + command = "/tellraw @a {}\n".format(tellraw_params) log.debug("forwarding to process: {!r}".format(command)) process.send(command) else: