Compare commits

...

3 Commits

Author SHA1 Message Date
de91d075b4 Protocol fixes 2023-07-27 02:05:49 +03:00
a7c02e0b52 Added kick command 2023-07-26 22:38:50 +03:00
6dd3de63a9 Added support async command 2023-07-26 22:37:32 +03:00
4 changed files with 39 additions and 6 deletions

View File

@ -194,7 +194,13 @@ class Client:
async def _recv(self, one=False): async def _recv(self, one=False):
while self.__alive: while self.__alive:
try: try:
header = await self.__reader.read(4) header = b""
while len(header) < 4 and self.__alive:
h = await self.__reader.read(4)
if not h:
break
else:
header += h
int_header = int.from_bytes(header, byteorder='little', signed=True) int_header = int.from_bytes(header, byteorder='little', signed=True)
@ -219,9 +225,14 @@ class Client:
self.__packets_queue.append(None) self.__packets_queue.append(None)
continue continue
data = await self.__reader.read(int_header) data = b""
while len(data) < int_header and self.__alive:
buffer = await self.__reader.read(int_header - len(data))
if not buffer:
break
else:
data += buffer
# self.log.debug(f"int_header: {int_header}; data: `{data}`;")
abg = b"ABG:" abg = b"ABG:"
if len(data) > len(abg) and data.startswith(abg): if len(data) > len(abg) and data.startswith(abg):
data = zlib.decompress(data[len(abg):]) data = zlib.decompress(data[len(abg):])

View File

@ -217,6 +217,22 @@ class Core:
except Exception as e: except Exception as e:
self.log.error(f"Error in heartbeat: {e}") self.log.error(f"Error in heartbeat: {e}")
async def kick_cmd(self, args):
if not len(args) > 0:
return "\nUsage: kick <nick>|:<id> [reason]\nExamples:\n\tkick admin bad boy\n\tkick :0 bad boy"
reason = "kicked by console."
if len(args) > 1:
reason = " ".join(args[1:])
cl = args[0]
if cl.startswith(":") and cl[1:].isdigit():
client = self.get_client(cid=int(cl[1:]))
else:
client = self.get_client(nick=cl)
if client:
await client.kick(reason)
else:
return "Client not found."
async def main(self): async def main(self):
self.tcp = self.tcp(self, self.server_ip, self.server_port) self.tcp = self.tcp(self, self.server_ip, self.server_port)
self.udp = self.udp(self, self.server_ip, self.server_port) self.udp = self.udp(self, self.server_ip, self.server_port)
@ -224,6 +240,7 @@ class Core:
"list", "list",
lambda x: f"Players list: {self.get_clients_list(True)}" lambda x: f"Players list: {self.get_clients_list(True)}"
) )
console.add_command("kick", self.kick_cmd)
pl_dir = "plugins" pl_dir = "plugins"
self.log.debug("Initializing PluginsLoaders...") self.log.debug("Initializing PluginsLoaders...")

View File

@ -100,8 +100,8 @@ class UDPServer(asyncio.DatagramTransport):
await asyncio.sleep(0.2) await asyncio.sleep(0.2)
except OSError as e: except OSError as e:
self.run = False # self.run = False
self.Core.run = False # self.Core.run = False
self.log.error(f"Cannot bind port or other error: {e}") self.log.error(f"Cannot bind port or other error: {e}")
except Exception as e: except Exception as e:
self.log.error(f"Error: {e}") self.log.error(f"Error: {e}")

View File

@ -7,6 +7,7 @@
# Licence: FPA # Licence: FPA
# (c) kuitoi.su 2023 # (c) kuitoi.su 2023
import builtins import builtins
import inspect
import logging import logging
from typing import AnyStr from typing import AnyStr
@ -242,7 +243,11 @@ class Console:
self.log(text) self.log(text)
command_object = self.__func.get(cmd) command_object = self.__func.get(cmd)
if command_object: if command_object:
out = command_object['f'](cmd_s[1:]) func = command_object['f']
if inspect.iscoroutinefunction(func):
out = await func(cmd_s[1:])
else:
out = func(cmd_s[1:])
if out: if out:
self.log(out) self.log(out)
else: else: