increase mcc nickname length

This commit is contained in:
osmarks 2021-01-29 21:23:48 +00:00
parent 55fb458e1d
commit e032d0efcd
1 changed files with 7 additions and 7 deletions

14
mcc.py
View File

@ -87,10 +87,10 @@ local_id = random.randint(0, 0xFFFF)
def shorthex(x): return "{:04x}".format(x) def shorthex(x): return "{:04x}".format(x)
def encode_packet(ty, nick, content): def encode_packet(ty, nick, content):
return struct.pack("!BH10s", ty, local_id, nick.encode("utf-8")) + content.encode("utf-8") return struct.pack("!BH16s", ty, local_id, nick.encode("utf-8")) + content.encode("utf-8")
def decode_packet(pkt): def decode_packet(pkt):
ty, local_id, nick = struct.unpack("!BH10s", pkt[:13]) ty, local_id, nick = struct.unpack("!BH16s", pkt[:19])
content = pkt[13:] content = pkt[19:]
return ty, local_id, nick.rstrip(b"\0").decode("utf-8"), content.decode("utf-8") return ty, local_id, nick.rstrip(b"\0").decode("utf-8"), content.decode("utf-8")
Peer = collections.namedtuple("Peer", ["nick", "timeout_counter"]) Peer = collections.namedtuple("Peer", ["nick", "timeout_counter"])
@ -112,7 +112,7 @@ async def run():
ifn = socket.if_nametoindex(interface) ifn = socket.if_nametoindex(interface)
sock, addr = configure_multicast(maddr, ifn) sock, addr = configure_multicast(maddr, ifn)
own_nick = getpass.getuser() own_nick = f"{getpass.getuser()}@{socket.gethostname()}"[:16]
peers = {} peers = {}
def cb(): def cb():
@ -140,15 +140,15 @@ async def run():
async def outq(s): async def outq(s):
if s.startswith("/nick "): if s.startswith("/nick "):
newnick = s[6:] newnick = s[6:]
if len(newnick.encode("utf8")) > 10: if len(newnick.encode("utf8")) > 16:
inq.put_nowait("! Max nick length is 10 bytes") inq.put_nowait("! Max nick length is 16 bytes")
return return
inq.put_nowait(f"! You are now {newnick}") inq.put_nowait(f"! You are now {newnick}")
nonlocal own_nick nonlocal own_nick
own_nick = newnick own_nick = newnick
return return
elif s.startswith("/peer"): elif s.startswith("/peer"):
p = [f"{p.nick} ({i})" for i, p in peers.items()] p = [f"{repr(p.nick)} ({i})" for i, p in peers.items()]
inq.put_nowait(f"! Peers: {', '.join(p)}") inq.put_nowait(f"! Peers: {', '.join(p)}")
return return
inq.put_nowait(f"{own_nick}: {s}") inq.put_nowait(f"{own_nick}: {s}")