Pyscii-BBS/comms.py
2025-07-19 17:33:10 -06:00

89 lines
2.3 KiB
Python

import auth, sys
import configparser
config = configparser.ConfigParser()
config.read(sys.argv[1])
def sendString(conn, text):
conn.sendall(text.encode('ascii'))
def getString(conn, max):
result = ""
index = 0
while index < max:
c = getChar(conn)
if (c[0] == '\b' or c[0] == 127) and index > 0:
result = result[:-1]
index -= 1
sendString(conn, "\x1b[D \x1b[D")
continue
elif c[0] == '\b' or c[0] == 127:
continue
if c[0] == '\r':
return result
result += c[0]
sendString(conn, c)
index += 1
return result
def getCharRaw(conn):
c = conn.recv(1)
if c == b'':
print("Connection dropped")
raise RuntimeError("Socket connection dropped")
return c
def getChar(conn):
while True:
c = getCharRaw(conn)
while c[0] == 255:
c = getCharRaw(conn)
if c[0] == 251 or c[0] == 252 or c[0] == 253 or c[0] == 254:
c = getCharRaw(conn)
elif c[0] == 250:
c = getCharRaw(conn)
while c[0] != 240:
c = getCharRaw(conn)
c = getCharRaw(conn)
if c[0] != '\n':
break
return str(c, 'ascii')
def comms(conn, addr, node):
"""
comms - Initializes the communication with the user, authorizes them, and passes them to the menus
"""
iac_echo = bytes([255, 251, 1])
iac_sga = bytes([255, 251, 3])
conn.sendall(iac_echo)
conn.sendall(iac_sga)
# Splash Screen
try:
with open("pages/splash.asc") as splash:
for line in splash.readlines():
line = line.replace("%node%", str(node))
sendString(conn, line + "\r")
getString(conn, 1)
except:
sendString(conn, config.get("Main","DefaultSplash").replace("%node%", str(node)))
# Login Screen
sendString(conn, "\r\nEnter your username, or type NEW if a new user.")
sendString(conn, "\r\nLogin: ")
username = getString(conn, 16)
if username.replace('\x00','') == "NEW":
auth.create(conn)
else:
sendString(conn, "\r\nPassword: ")
password = getString(conn, 16)
auth.login(conn,username,password, node)