1
0
Fork 0

Merge branch 'master' of cybre.tech:chr/matrix-appservice-minecraft

This commit is contained in:
khr 2020-03-29 03:26:11 +02:00
commit 52ad431877
1 changed files with 21 additions and 23 deletions

View File

@ -72,14 +72,14 @@ class socket_util(object):
string_message = json.dumps(message) string_message = json.dumps(message)
select.select([], [self.soc], []) select.select([], [self.soc], [])
self.write_int(len(string_message)) self.write_int(len(string_message))
self.write(string_message) self.write(string_message.encode('utf-8'))
return 0 return 0
def write_int(self, integer): def write_int(self, integer):
integer_buf = struct.pack('>i', integer) integer_buf = struct.pack('>i', integer)
self.write(integer_buf) self.write(integer_buf)
def write(self, data): def write(self, data: bytes):
#socket.bind(address) #socket.bind(address)
data_len = len(data) data_len = len(data)
offset = 0 offset = 0
@ -122,8 +122,10 @@ class socket_util(object):
class MinecraftWrapper(socket_util): class MinecraftWrapper(socket_util):
def __init__(self, command, host, port): def __init__(self, command, host, port):
super().__init__(host, port) super().__init__(host, port)
self.logger = LOG.getChild("MinecraftWrapper")
self.logger.debug(command)
self.command = command self.command = command
LOG.info("Starting Wrapper Polling Thread") self.logger.info("Starting Wrapper Polling Thread")
poll_process = threading.Thread(target=self.cli_poll) poll_process = threading.Thread(target=self.cli_poll)
poll_process.daemon = True poll_process.daemon = True
poll_process.start() poll_process.start()
@ -131,16 +133,19 @@ class MinecraftWrapper(socket_util):
def socket_reset(self): def socket_reset(self):
super().socket_reset() super().socket_reset()
self.logger.debug("Connecting to {}:{}".format(self.host, self.port))
self.soc.connect((self.host, self.port)) self.soc.connect((self.host, self.port))
LOG.info("Socket Connected") self.logger.info("Socket Connected")
def exe_mc(self): def exe_mc(self):
self.proc = subprocess.Popen(self.command, shell=True, stdout=PIPE, stdin=PIPE, universal_newlines=True) self.proc = subprocess.Popen(
" ".join(self.command), shell=True, stdout=PIPE, stdin=PIPE, universal_newlines=True
)
for stdout_line in iter(self.proc.stdout.readline, ""): for stdout_line in iter(self.proc.stdout.readline, ""):
yield stdout_line yield stdout_line
return_code = self.proc.wait() return_code = self.proc.wait()
if return_code: if return_code:
raise subprocess.CalledProcessError(return_code, cmd) raise subprocess.CalledProcessError(return_code, self.command)
def msg_process(self): def msg_process(self):
while(not self.exit): while(not self.exit):
@ -154,7 +159,7 @@ class MinecraftWrapper(socket_util):
self.msg_handle(rcv) self.msg_handle(rcv)
if status == 0: self.msglist.pop() if status == 0: self.msglist.pop()
except Exception as e: except Exception as e:
LOG.info(e) self.logger.exception(e)
self.socket_reset() self.socket_reset()
@ -167,23 +172,22 @@ class MinecraftWrapper(socket_util):
def proc_monitor(self): def proc_monitor(self):
try: try:
if self.proc.poll() is not None: if self.proc is not None and self.proc.poll() is not None:
self.exit = True self.exit = True
self.close_socket() self.close_socket()
sys.exit(0) sys.exit(0)
except: except Exception as e:
LOG.error("poll error") self.logger.exception("poll error")
pass pass
def cli_poll(self): def cli_poll(self):
prog = re.compile("^\[(.*)\] \[(.*)\]: <(.*)> (.*)") prog = re.compile("\[.*\] \[(.*)\] \[(.*)\]: <(.*)> (.*)")
EXAMPLE = "[07:36:28] [Server thread/INFO] [minecraft/DedicatedServer]: <khr_> test"
for line in self.exe_mc(): for line in self.exe_mc():
LOG.info(line.rstrip('\n')) self.logger.info(line.rstrip('\n'))
# regex to get user and text: ^<(.*)> (.*)\n
result = prog.search(line) result = prog.search(line)
if result: if result:
#LOG.info("user: " + result.group(3) + " msg: " +result.group(4).rstrip('\n')) self.logger.info("user: " + result.group(3) + " msg: " +result.group(4).rstrip('\n'))
#msb.send({"user":result.group(3),"msg":result.group(4).rstrip('\n')})
self.msglist.insert(0, {"user":result.group(3),"msg":result.group(4).rstrip('\n')}) self.msglist.insert(0, {"user":result.group(3),"msg":result.group(4).rstrip('\n')})
class MinecraftServerBridge(socket_util): class MinecraftServerBridge(socket_util):
@ -259,7 +263,7 @@ class MinecraftServerBridge(socket_util):
user_id = new_user + ":" + global_config['server_name'] user_id = new_user + ":" + global_config['server_name']
self.api.register("m.login.application_service",username = "mc_" + msg['user']) self.api.register("m.login.application_service",username = "mc_" + msg['user'])
except Exception as e: except Exception as e:
LOG.info(e) LOG.exception(e)
#for each room we're aware of, post server chat inside. Eventually 1 room should equal 1 server #for each room we're aware of, post server chat inside. Eventually 1 room should equal 1 server
for room in roomsync: for room in roomsync:
#generate a unique transaction id based on the current time #generate a unique transaction id based on the current time
@ -410,13 +414,7 @@ def main():
appservice_token=global_config["as_token"], appservice_token=global_config["as_token"],
) )
LOG.info("All Threads Running") LOG.info("All Threads Running")
cmd = ""
# Allow stdin commands to the minecraft server
while (not minecraft.exit): while (not minecraft.exit):
if minecraft.proc != None and 'stop' not in cmd:
cmd = input()
minecraft.proc.stdin.write(cmd + '\n')
else:
time.sleep(1) time.sleep(1)
LOG.info("Calling exit() in main thread...") LOG.info("Calling exit() in main thread...")
sys.exit() sys.exit()