CC_snippets/centralLoggingSystem/server.lua
2023-09-05 12:41:49 +02:00

73 lines
1.8 KiB
Lua

settings.define("AES.key", {
description = "AES key used for encryption",
type = number
})
local printerSide = "right"
local modemSide = "top"
local channel = 59598
local AES = require("/AES")
local key = settings.get("AES.key") or error("No AES Key set")
local modem = peripheral.wrap(modemSide) or error("No modem found")
modem.closeAll()
modem.open(channel)
local printer = peripheral.wrap(printerSide) or error("No printer found")
function startPage()
if not printer.newPage() then
error("cant start new page")
end
printer.setPageTitle(os.date())
end
local pageOpened = false
function printNewLog(line)
print(line)
if not pageOpened then
startPage()
pageOpened = true
end
local width, height = printer.getPageSize()
local currX, currY = printer.getCursorPos()
local neededLines = math.ceil(#line / width)
if neededLines > height+1 - currY then -- +1 because being on the last line still leaves one free
startPage()
end
local i = 1
while i <= #line do
printer.write(line:sub(i, i + width-1))
i = i + width - 1
printer.setCursorPos(1, currY+1)
currX, currY = printer.getCursorPos()
end
if currY == height then
printer.endPage()
pageOpened = false
end
end
local modemMessage = {}
local decrypted = nil
local obj = nil
while true do
modemMessage = {os.pullEvent("modem_message")}
decrypted = AES.ECB_256(AES.decrypt, key, modemMessage[5])
obj = textutils.unserialise(decrypted)
if obj then
if obj.sender and obj.message then
printNewLog(string.format("%s %s: %s", os.date(), obj.sender, obj.message))
else
print("invalid object received")
print(decrypted)
end
else
print("invalid object received")
end
end