2016-05-23 16:01:04 +02:00
|
|
|
# -*- coding: UTF-8 -*-
|
|
|
|
'''
|
|
|
|
rtx - RetroText
|
|
|
|
rtxHelpers: verschiedene Hilfsfunktionen
|
|
|
|
by Anna Christina Naß <acn@acn.wtf>
|
|
|
|
released under GPL
|
|
|
|
'''
|
|
|
|
|
|
|
|
from rtxPage import rtxPage
|
2016-05-24 12:05:02 +02:00
|
|
|
import cept
|
2016-05-23 16:01:04 +02:00
|
|
|
import glob
|
|
|
|
import logging
|
2016-05-31 15:35:32 +02:00
|
|
|
import config
|
2016-05-23 16:01:04 +02:00
|
|
|
|
|
|
|
def process_byte(in_byte):
|
2016-06-02 16:49:43 +02:00
|
|
|
""" takes the received byte and processes the command line
|
|
|
|
if a new command has been entered, it will call process_command()
|
|
|
|
if a short link has been entered, the link_list of the current page will be searched
|
|
|
|
and the found target will be sent to process_command()
|
|
|
|
"""
|
|
|
|
instr=str(in_byte, encoding="latin-1")
|
|
|
|
|
|
|
|
if config.MODE=="modem":
|
|
|
|
logging.debug("> %s < CD: %s", instr, glob.ser.getCD())
|
|
|
|
|
|
|
|
# what to send back to the client
|
|
|
|
if instr == cept.INI:
|
|
|
|
logging.debug("INI")
|
|
|
|
echo_char = "*"
|
|
|
|
elif instr == cept.TER:
|
|
|
|
logging.debug("TER")
|
|
|
|
echo_char = "#"
|
|
|
|
else:
|
|
|
|
echo_char=instr
|
|
|
|
|
|
|
|
# start of line: clear rest of cmdline
|
|
|
|
if len(glob.cmdline) == 0:
|
|
|
|
glob.ser.write(bytes(cept.CLEARLINE, "latin-1"))
|
|
|
|
|
|
|
|
# echo the character to the client
|
|
|
|
glob.ser.write(bytes(echo_char, "latin-1"))
|
|
|
|
|
|
|
|
glob.cmdline += instr
|
|
|
|
|
|
|
|
# Backspace:
|
|
|
|
if instr == cept.BSP:
|
|
|
|
glob.cmdline = glob.cmdline[:-1]
|
|
|
|
return
|
|
|
|
|
|
|
|
logging.debug("Cmdline: >%s<", glob.cmdline)
|
|
|
|
|
|
|
|
# process links inside the page:
|
|
|
|
if ( len(glob.cmdline) == 1 or len(glob.cmdline) == 2 ) and glob.curpage.get_link(glob.cmdline):
|
|
|
|
glob.cmdline = cept.INI + glob.curpage.get_link(glob.cmdline) + cept.TER
|
|
|
|
process_command(glob.cmdline)
|
|
|
|
return
|
|
|
|
|
|
|
|
# reset cmdline:
|
|
|
|
if instr == cept.INI and glob.cmdline[-2:] == cept.INI+cept.INI:
|
|
|
|
logging.debug("reset cmdline")
|
|
|
|
glob.cmdline = ""
|
|
|
|
glob.ser.write(bytes(cept.clear_line24, "latin-1"))
|
|
|
|
return
|
|
|
|
|
|
|
|
# goto last page:
|
|
|
|
if instr == cept.TER and glob.cmdline == cept.INI+cept.TER:
|
|
|
|
if glob.prevpage.get_page_id() == -1:
|
|
|
|
glob.cmdline = ""
|
|
|
|
send_error("Keine vorige Seite vorhanden.")
|
|
|
|
return
|
|
|
|
glob.cmdline = "" # reset cmdline
|
|
|
|
goto_last_page()
|
|
|
|
return
|
|
|
|
|
|
|
|
# "Make it so":
|
|
|
|
if instr == cept.TER:
|
|
|
|
# or instr == cept.SEND or instr == cept.CR:
|
|
|
|
logging.info("Kommando: >%s<", glob.cmdline)
|
|
|
|
process_command(glob.cmdline)
|
|
|
|
return
|
2016-05-23 16:01:04 +02:00
|
|
|
|
|
|
|
def process_command(cmd):
|
2016-06-02 16:49:43 +02:00
|
|
|
""" processes the given command line
|
|
|
|
"""
|
|
|
|
|
|
|
|
# reset cmdline
|
|
|
|
glob.cmdline = ""
|
|
|
|
|
|
|
|
# process *cmdline#, normally a page-loading command:
|
|
|
|
if cmd[0] == cept.INI and cmd[-1:] == cept.TER:
|
|
|
|
pgnr = cmd[1:-1]
|
|
|
|
check_and_send_page(pgnr, "Die Seite kann nicht gefunden werden.")
|
|
|
|
|
|
|
|
# if only # is entered, either go to the link behind '#'
|
|
|
|
# or to the next sub-page (2000a => 2000b) if it exists
|
|
|
|
if cmd == cept.TER:
|
|
|
|
if glob.curpage.get_link("#"):
|
|
|
|
pgnr = glob.curpage.get_link("#")
|
|
|
|
check_and_send_page(pgnr, "Die Seite kann nicht gefunden werden.")
|
|
|
|
else:
|
|
|
|
# check if the next page exists, e.g. 2000a => 2000b
|
|
|
|
curid = glob.curpage.get_page_id()
|
2016-06-09 13:24:31 +02:00
|
|
|
if curid == -1:
|
2016-06-09 13:26:39 +02:00
|
|
|
send_error("Keine n"+ cept.UMLAUT +"achste Seite gefunden.")
|
2016-06-09 13:27:38 +02:00
|
|
|
elif str(curid[-1:]).isalpha():
|
2016-06-02 16:49:43 +02:00
|
|
|
# aus 2000a 2000b machen:
|
|
|
|
nextid = curid[:-1] + chr(ord(curid[-1:])+1)
|
2016-06-09 13:26:39 +02:00
|
|
|
check_and_send_page(nextid, "Keine n" + cept.UMLAUT +"achste Seite gefunden.")
|
2016-06-02 16:49:43 +02:00
|
|
|
|
|
|
|
# special commands:
|
|
|
|
if cmd == "!1" + cept.TER:
|
|
|
|
send_welcome_page()
|
|
|
|
return
|
|
|
|
if cmd == "!2" + cept.TER:
|
|
|
|
glob.ser.write(bytes(cept.init_screen, "latin-1"))
|
|
|
|
return
|
2016-05-23 16:01:04 +02:00
|
|
|
|
|
|
|
def goto_last_page():
|
2016-06-02 16:49:43 +02:00
|
|
|
""" go back to the previous page and make the current page the previous page
|
|
|
|
"""
|
|
|
|
logging.info("Letzte Seite: %s", str(glob.prevpage.get_page_id()))
|
2016-05-23 16:01:04 +02:00
|
|
|
|
2016-06-02 16:49:43 +02:00
|
|
|
# swap prevpage and curpage
|
|
|
|
temp = glob.curpage
|
|
|
|
glob.curpage = glob.prevpage
|
|
|
|
glob.prevpage = temp
|
2016-05-23 16:01:04 +02:00
|
|
|
|
2016-06-02 16:49:43 +02:00
|
|
|
# send the previous page (now curpage) to the client
|
|
|
|
send_page(glob.curpage)
|
2016-05-23 16:01:04 +02:00
|
|
|
|
|
|
|
def send_welcome_page():
|
2016-06-02 16:49:43 +02:00
|
|
|
""" get the btxlogo, make a rtxPage of it and send it to the client """
|
|
|
|
wpage = rtxPage()
|
|
|
|
wpage.set_page(bytes(cept.btxlogo, "latin-1"))
|
|
|
|
send_page(wpage)
|
2016-05-23 16:01:04 +02:00
|
|
|
|
|
|
|
def check_and_send_page(pgnr, errormsg):
|
2016-06-02 16:49:43 +02:00
|
|
|
""" checks if the page "pgnr" exists
|
|
|
|
if yes, it loads it into curpage and sends it to the client
|
|
|
|
if not, it sends the error message "errormsg" to the client
|
|
|
|
"""
|
|
|
|
logging.info("suche Seite: >%s<", pgnr)
|
|
|
|
|
|
|
|
if rtxPage.exists(pgnr):
|
|
|
|
glob.prevpage = glob.curpage
|
|
|
|
glob.curpage = rtxPage(pgnr)
|
|
|
|
send_page(glob.curpage) # takes a rtxPage object!
|
|
|
|
return True
|
|
|
|
elif pgnr[-1:].isnumeric() and rtxPage.exists(pgnr + "a"):
|
|
|
|
glob.prevpage = glob.curpage
|
|
|
|
glob.curpage = rtxPage(pgnr + "a")
|
|
|
|
send_page(glob.curpage) # takes a rtxPage object!
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
send_error(errormsg)
|
|
|
|
return False
|
2016-05-23 16:01:04 +02:00
|
|
|
|
|
|
|
def send_error(msg):
|
2016-06-02 16:49:43 +02:00
|
|
|
""" send an error message to the client """
|
|
|
|
glob.ser.write(bytes(cept.error_prefix + msg + cept.error_suffix, "latin-1"))
|
2016-05-23 16:01:04 +02:00
|
|
|
|
|
|
|
def send_page(page):
|
2016-06-02 16:49:43 +02:00
|
|
|
""" sends a page to the client """
|
|
|
|
glob.ser.write(page.get_page())
|
2016-05-23 16:01:04 +02:00
|
|
|
|