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)
select.select([], [self.soc], [])
self.write_int(len(string_message))
self.write(string_message)
self.write(string_message.encode('utf-8'))
return 0
def write_int(self, integer):
integer_buf = struct.pack('>i', integer)
self.write(integer_buf)
def write(self, data):
def write(self, data: bytes):
#socket.bind(address)
data_len = len(data)
offset = 0
@ -122,8 +122,10 @@ class socket_util(object):
class MinecraftWrapper(socket_util):
def __init__(self, command, host, port):
super().__init__(host, port)
self.logger = LOG.getChild("MinecraftWrapper")
self.logger.debug(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.daemon = True
poll_process.start()
@ -131,16 +133,19 @@ class MinecraftWrapper(socket_util):
def socket_reset(self):
super().socket_reset()
self.logger.debug("Connecting to {}:{}".format(self.host, self.port))
self.soc.connect((self.host, self.port))
LOG.info("Socket Connected")
self.logger.info("Socket Connected")
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, ""):
yield stdout_line
return_code = self.proc.wait()
if return_code:
raise subprocess.CalledProcessError(return_code, cmd)
raise subprocess.CalledProcessError(return_code, self.command)
def msg_process(self):
while(not self.exit):
@ -154,7 +159,7 @@ class MinecraftWrapper(socket_util):
self.msg_handle(rcv)
if status == 0: self.msglist.pop()
except Exception as e:
LOG.info(e)
self.logger.exception(e)
self.socket_reset()
@ -167,23 +172,22 @@ class MinecraftWrapper(socket_util):
def proc_monitor(self):
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.close_socket()
sys.exit(0)
except:
LOG.error("poll error")
except Exception as e:
self.logger.exception("poll error")
pass
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():
LOG.info(line.rstrip('\n'))
# regex to get user and text: ^<(.*)> (.*)\n
self.logger.info(line.rstrip('\n'))
result = prog.search(line)
if result:
#LOG.info("user: " + result.group(3) + " msg: " +result.group(4).rstrip('\n'))
#msb.send({"user":result.group(3),"msg":result.group(4).rstrip('\n')})
self.logger.info("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):
@ -259,7 +263,7 @@ class MinecraftServerBridge(socket_util):
user_id = new_user + ":" + global_config['server_name']
self.api.register("m.login.application_service",username = "mc_" + msg['user'])
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 room in roomsync:
#generate a unique transaction id based on the current time
@ -410,14 +414,8 @@ def main():
appservice_token=global_config["as_token"],
)
LOG.info("All Threads Running")
cmd = ""
# Allow stdin commands to the minecraft server
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...")
sys.exit()