Better nickname parsing, warn other clients when they does not provide a parsable nickname

Signed-off-by: Yohann D'ANELLO <ynerant@crans.org>
This commit is contained in:
2020-12-21 16:04:15 +01:00
parent e37c43b78d
commit 9f070f594b
2 changed files with 32 additions and 8 deletions

View File

@@ -15,7 +15,7 @@ class Hazelnut:
"""
A hazelnut is a connected client, with its socket.
"""
def __init__(self, nickname: str = "anonymous", address: str = "localhost", port: int = 2500):
def __init__(self, nickname: str = None, address: str = "localhost", port: int = 2500):
self.nickname = nickname
try:
@@ -174,7 +174,7 @@ class Squirrel(Hazelnut):
elif key != "\n":
# Insert the pressed key in the current message
new_buffer = self.input_buffer[:self.input_index] + key + self.input_buffer[self.input_index:]
if len(DataTLV.construct(f"<{self.nickname}> {new_buffer}")) > 255 - 8 - 4:
if len(DataTLV.construct(f"{self.nickname}: {new_buffer}")) > 255 - 8 - 4:
# The message is too long to be sent once. We don't allow the user to type any other character.
curses.beep()
return
@@ -190,7 +190,7 @@ class Squirrel(Hazelnut):
if not msg:
return
msg = f"<{self.nickname}> {msg}"
msg = f"{self.nickname}: {msg}"
self.add_message(msg)
pkt = Packet.construct(DataTLV.construct(msg))
@@ -257,7 +257,7 @@ class Squirrel(Hazelnut):
Add a new system log message.
TODO: Configure logging levels to ignore some messages.
"""
return self.add_message(f"<system> *{msg}*")
return self.add_message(f"system: *{msg}*")
def print_markdown(self, pad: Any, y: int, x: int, msg: str,
bold: bool = False, italic: bool = False, underline: bool = False, strike: bool = False) -> int:
@@ -363,6 +363,8 @@ class Squirrel(Hazelnut):
if i + y_offset > curses.LINES - 3:
break
msg = re.sub("([^:]*): (.*)", "<\\1> \\2", msg, 1)
if not re.match("<.*> .*", msg):
msg = "<unknown> " + msg
match = re.match("<(.*)> (.*)", msg)