186 lines
4.5 KiB
Lua
186 lines
4.5 KiB
Lua
--[[
|
|
Utility Functions
|
|
Helper functions for logging, address handling, and iris control
|
|
]]
|
|
|
|
local utils = {}
|
|
|
|
-- Import config (set by startup.lua)
|
|
local config
|
|
local gate
|
|
|
|
function utils.init(cfg, gateInterface)
|
|
config = cfg
|
|
gate = gateInterface
|
|
end
|
|
|
|
---------------------------------------------
|
|
-- LOGGING
|
|
---------------------------------------------
|
|
|
|
function utils.log(message)
|
|
if config.enableLogging then
|
|
local timestamp = os.date("%Y-%m-%d %H:%M:%S")
|
|
local logEntry = "[" .. timestamp .. "] " .. message
|
|
|
|
-- Write to console
|
|
print(logEntry)
|
|
|
|
-- Write to file
|
|
local file = fs.open(config.logFile, "a")
|
|
if file then
|
|
file.writeLine(logEntry)
|
|
file.close()
|
|
end
|
|
end
|
|
end
|
|
|
|
---------------------------------------------
|
|
-- ADDRESS UTILITIES
|
|
---------------------------------------------
|
|
|
|
function utils.addressToTable(address)
|
|
if type(address) == "table" then
|
|
return address
|
|
end
|
|
return {}
|
|
end
|
|
|
|
function utils.compareAddresses(addr1, addr2)
|
|
addr1 = gate.addressToString(addr1)
|
|
addr2 = gate.addressToString(addr2)
|
|
return addr1 == addr2
|
|
end
|
|
|
|
function utils.deepcopy(orig)
|
|
local orig_type = type(orig)
|
|
local copy
|
|
if orig_type == 'table' then
|
|
copy = {}
|
|
for orig_key, orig_value in next, orig, nil do
|
|
copy[utils.deepcopy(orig_key)] = utils.deepcopy(orig_value)
|
|
end
|
|
setmetatable(copy, utils.deepcopy(getmetatable(orig)))
|
|
else
|
|
copy = orig
|
|
end
|
|
return copy
|
|
end
|
|
|
|
function utils.isAddressInList(address, list)
|
|
for _, entry in ipairs(list) do
|
|
local listAddr = entry[2]
|
|
local tmp = utils.deepcopy(listAddr)
|
|
table.remove(tmp) -- Remove point of origin
|
|
if utils.compareAddresses(address, tmp) then
|
|
return true, entry[1]
|
|
end
|
|
end
|
|
return false, nil
|
|
end
|
|
|
|
function utils.isAddressAllowed(address)
|
|
-- Check blacklist first
|
|
local isBlacklisted, blackName = utils.isAddressInList(address, config.blacklist)
|
|
if isBlacklisted then
|
|
return false, "Blacklisted: " .. blackName
|
|
end
|
|
|
|
-- If whitelist is not empty, check whitelist
|
|
if #config.whitelist > 0 then
|
|
local isWhitelisted, whiteName = utils.isAddressInList(address, config.whitelist)
|
|
if isWhitelisted then
|
|
return true, "Whitelisted: " .. whiteName
|
|
else
|
|
return false, "Not on whitelist"
|
|
end
|
|
end
|
|
|
|
-- If no whitelist, allow all non-blacklisted
|
|
return true, "Allowed"
|
|
end
|
|
|
|
---------------------------------------------
|
|
-- IRIS CONTROL
|
|
---------------------------------------------
|
|
|
|
function utils.closeIris()
|
|
if config.irisEnabled then
|
|
gate.closeIris()
|
|
utils.log("Iris closed")
|
|
return true
|
|
end
|
|
return false
|
|
end
|
|
|
|
function utils.openIris()
|
|
if config.irisEnabled then
|
|
gate.openIris()
|
|
utils.log("Iris opened")
|
|
return true
|
|
end
|
|
return false
|
|
end
|
|
|
|
function utils.getIrisState()
|
|
if config.irisEnabled then
|
|
local progress = gate.getIrisProgressPercentage()
|
|
if progress == 0 then
|
|
return "OPEN"
|
|
elseif progress == 100 then
|
|
return "CLOSED"
|
|
else
|
|
return "MOVING"
|
|
end
|
|
end
|
|
return "NO IRIS"
|
|
end
|
|
|
|
---------------------------------------------
|
|
-- MESSAGING
|
|
---------------------------------------------
|
|
|
|
function utils.sendVersionMessage()
|
|
if config.enableMessaging then
|
|
local message = "SGCS_V" .. config.programVersion
|
|
gate.sendStargateMessage(message)
|
|
utils.log("Sent version message: " .. message)
|
|
return true
|
|
end
|
|
return false
|
|
end
|
|
|
|
function utils.sendPasswordResponse(success)
|
|
if config.enableMessaging then
|
|
if success then
|
|
gate.sendStargateMessage("IRIS_OPEN")
|
|
utils.log("Sent iris open confirmation")
|
|
else
|
|
gate.sendStargateMessage("IRIS_DENIED")
|
|
utils.log("Sent iris access denied")
|
|
end
|
|
return true
|
|
end
|
|
return false
|
|
end
|
|
|
|
function utils.sendPasswordAttempt(password)
|
|
if config.enableMessaging then
|
|
gate.sendStargateMessage("IRIS_PASSWORD:" .. password)
|
|
utils.log("Sent password attempt")
|
|
return true
|
|
end
|
|
return false
|
|
end
|
|
|
|
function utils.sendPasswordRequest()
|
|
if config.enableMessaging then
|
|
gate.sendStargateMessage("IRIS_PASSWORD_REQUIRED")
|
|
utils.log("Sent password request")
|
|
return true
|
|
end
|
|
return false
|
|
end
|
|
|
|
return utils
|