Files
StingNET/telnet-basic/main.py
T

126 lines
4.2 KiB
Python

import sys, os
import asyncio
from prompt_toolkit import Application
from prompt_toolkit.layout.layout import Layout
from prompt_toolkit.layout import FloatContainer
from prompt_toolkit.widgets import Label
from prompt_toolkit.application.current import get_app
from prompt_toolkit.key_binding import KeyBindings
from helpers import defaultAppSettings, message_dialog, HSelectLayout, TabLayout, errorTxt
from login import loginScreen, firstSetupScreen
from account_settings import AccountSettings
from interface_settings import InterfaceSettings
from admin_panel import AdminPanel
parentdir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(parentdir)
from api import ServerConnection
server = ServerConnection()
guestLogin = False
intset = {} # interface settings
floats = []
async def mainScreen():
kb = KeyBindings()
@kb.add("c-q",eager=True)
def exit_(event):
event.app.exit()
if guestLogin:
name = "[guest]"
admin = False
else:
stat = await server.status()
if stat["type"]=="error" or not stat["response"]["login"]: return
name = stat["response"]["name"]
admin = stat["response"]["admin"]
def restart():
get_app().exit(result="restart")
acc = AccountSettings(name,guestLogin,server,floats)
interface = InterfaceSettings(intset,guestLogin,restart,server,floats)
admp = AdminPanel(floats,server)
# ------------------------------------------------------------
# Settings Menu
# ------------------------------------------------------------
settings = HSelectLayout(
kb,
[acc.el,interface.el]+([ admp.el ] if admin else []),
[None,None,None],
["Account","Interface"]+(["Admin panel"] if admin else [])
)
# ------------------------------------------------------------
# Tab Layout
# ------------------------------------------------------------
tablayout = TabLayout(kb,[
Label("Not implemented yet 1"),
Label("Not implemented yet 2"),
settings.el
],[
None,
None,
settings.select
],["Forum Boards","E-Messages","Settings"],name)
# ------------------------------------------------------------
# Application
# ------------------------------------------------------------
layout = Layout(FloatContainer(content=tablayout.el,floats=floats),focused_element=tablayout.tabs[0])
app = Application(layout=layout, key_bindings=kb, **defaultAppSettings(intset))
app.pre_run_callables.append(tablayout.focusTabWindow)
return await app.run_async()
async def main():
global intset, guestLogin
try:
await asyncio.wait_for(server.connect(), timeout=10)
except TimeoutError:
print("The internal server is currently down. Please press Ctrl-C (you may see a KeyboardInterrupt error).")
exit()
try:
while True:
stat = await server.status()
if stat["type"]=="error": continue
if stat["response"]["login"]:
ires = await server.getisettings("basic")
if ires["type"]=="error":
intset = await firstSetupScreen(intset,server)
ires = await server.setisettings("basic",intset)
if ires["type"]=="error":
await message_dialog(intset,title="Error",text=f"{errorTxt(ires)}\n\nThese settings might not have been sent to the internal server.\nYou will be asked again the next time you log in to your account.").run_async()
else:
intset = ires["response"]
if stat["response"]["login"] or guestLogin:
res = await mainScreen()
if res!="restart":
if res!="login":
break
if guestLogin:
guestLogin = False
else:
await server.logout()
else:
intset = {"color":4,"encoding":"ascii"}
loginRes = await loginScreen(intset,server)
if loginRes=="guest": guestLogin = True
finally:
await server.disconnect()
asyncio.run(main())