300 lines
8.5 KiB
Lua
300 lines
8.5 KiB
Lua
-- Skynet v1.2 // server
|
|
-- orig. written by minish - 2023-10
|
|
-- revived 2025-01
|
|
|
|
--#region Imports
|
|
local cfg = require("cfg")
|
|
local ch = require("chat")
|
|
local tk = require("tracking")
|
|
--#endregion
|
|
|
|
--#region Machine Preparation
|
|
settings.set("shell.allow_disk_startup", false)
|
|
--#endregion
|
|
|
|
local function q(v)
|
|
return tostring(v or "??")
|
|
end
|
|
|
|
local cmds = {
|
|
loc = function(unm, args)
|
|
local tg = tk:resTg(args[1])
|
|
if not tg then
|
|
ch.send(unm, "missing target!", "loc")
|
|
return
|
|
end
|
|
|
|
tk:refreshOne(tg)
|
|
local p = tk.plrs[tg]
|
|
if not p then
|
|
ch.send(unm, "couldn't locate target", "loc")
|
|
return
|
|
end
|
|
|
|
ch.send(unm, string.format("user: %s xyz:[%s, %s, %s] dim=%s", tg, q(p.x), q(p.y), q(p.z), q(p.dimension)))
|
|
end,
|
|
rpt = function(unm, args)
|
|
tk:refresh()
|
|
|
|
local ins = table.insert
|
|
|
|
local mparts = {}
|
|
ins(mparts, { text = "finished generating report!\n" })
|
|
|
|
local n, nplrs = 0, 0
|
|
for _, _ in next, tk.plrs do nplrs = nplrs + 1 end
|
|
for i, v in pairs(tk.plrs) do
|
|
local line = string.format("user: %s xyz:[%s, %s, %s] dim=%s", i, q(v.x), q(v.y), q(v.z), q(v.dimension))
|
|
n = n + 1
|
|
if n ~= nplrs then
|
|
line = line .. "\n"
|
|
end
|
|
|
|
ins(mparts, { text = "[" })
|
|
ins(mparts, { text = "s", color = "aqua" })
|
|
ins(mparts, { text = "] " .. line })
|
|
end
|
|
|
|
ch.send(unm, mparts, "rpt")
|
|
end,
|
|
dims = function(unm, args)
|
|
tk:refresh()
|
|
|
|
local ins = table.insert
|
|
|
|
local mparts = {}
|
|
ins(mparts, { text = "finished generating dimension report!\n" })
|
|
|
|
local n, nplrs = 0, 0
|
|
for _, _ in next, tk.plrs do nplrs = nplrs + 1 end
|
|
for i, v in pairs(tk.plrs) do
|
|
local line = string.format("user: %s dim=%s", i, q(v.dimension))
|
|
n = n + 1
|
|
if n ~= nplrs then
|
|
line = line .. "\n"
|
|
end
|
|
|
|
ins(mparts, { text = "[" })
|
|
ins(mparts, { text = "s", color = "aqua" })
|
|
ins(mparts, { text = "] " .. line })
|
|
end
|
|
|
|
ch.send(unm, mparts, "rpt")
|
|
end,
|
|
unsublog = function(unm, args)
|
|
if not cfg.users[unm].subscribeLog then
|
|
ch.send(unm, "you're already aren't subscribed!", "users")
|
|
ch.reportLog("user " .. unm .. " tries log unsub, but they weren't there")
|
|
else
|
|
cfg.users[unm].subscribeLog = false
|
|
ch.reportLog("user " .. unm .. " unsubscribes from log")
|
|
end
|
|
end,
|
|
sublog = function(unm, args)
|
|
if cfg.users[unm].subscribeLog then
|
|
ch.send(unm, "you're already subscribed!", "users")
|
|
ch.reportLog("user " .. unm .. " tries log sub, but already there")
|
|
else
|
|
cfg.users[unm].subscribeLog = true
|
|
ch.reportLog("user " .. unm .. " subscribes to log")
|
|
end
|
|
end,
|
|
allow = function(unm, args)
|
|
local tg = tk:resTg(args[1])
|
|
if not tg then
|
|
ch.send(unm, "No user was specified!", "users")
|
|
return
|
|
end
|
|
|
|
if cfg.users[tg] ~= nil then
|
|
ch.send(unm, "They're already allowed!!", "users")
|
|
ch.reportLog("user " .. unm .. " tries to allow " .. tg .. ", but they already are")
|
|
else
|
|
cfg.users[tg] = {}
|
|
ch.reportLog("user " .. unm .. " allows " .. tg)
|
|
ch.send(tg, "You were added to skynet, welcome!! // try $help for list of commands", "users")
|
|
end
|
|
end,
|
|
disallow = function(unm, args)
|
|
local tg = tk:resTg(args[1])
|
|
if not tg then
|
|
ch.send(unm, "No user was specified!", "users")
|
|
return
|
|
end
|
|
if cfg.users[tg] == nil then
|
|
ch.send(unm, "That person is already disallowed", "users")
|
|
ch.reportLog("user " .. unm .. " tries to disallow " .. tg .. ", but they already are")
|
|
else
|
|
cfg.users[tg] = nil
|
|
ch.reportLog("user " .. unm .. " disallows " .. tg)
|
|
end
|
|
end,
|
|
ping = function(unm, args)
|
|
ch.send(unm, "Alive!!!!")
|
|
end,
|
|
shutdown = function(unm, args)
|
|
if cfg.users[unm].admin then
|
|
ch.reportLog("system shutdown requested by " .. unm)
|
|
ch.userBc("Shutting down..")
|
|
return true
|
|
else
|
|
ch.reportLog("non-admin user " .. unm .. " attempted to shutdown")
|
|
ch.send(unm, "no permission to do that..", "cmd")
|
|
end
|
|
end,
|
|
say = function(unm, args)
|
|
local msg = table.concat(args, " ")
|
|
|
|
ch.say(msg)
|
|
end,
|
|
faketpa = function(unm, args)
|
|
local tgtplr = table.remove(args, 1)
|
|
local sender = table.remove(args, 1)
|
|
local fakeplr = table.remove(args, 1)
|
|
local fakemsg = table.concat(args, " ")
|
|
|
|
if not tgtplr then
|
|
ch.send(unm, "no target player specified", "cmd")
|
|
return
|
|
end
|
|
if not sender then
|
|
ch.send(unm, "no sender name specified", "cmd")
|
|
return
|
|
end
|
|
if not fakeplr then
|
|
ch.send(unm, "no fake player specified", "cmd")
|
|
return
|
|
end
|
|
|
|
tgtplr = tk:resTg(tgtplr)
|
|
if not tgtplr then
|
|
ch.send(unm, "target player specified not found", "cmd")
|
|
return
|
|
end
|
|
|
|
ch.faketpa(tgtplr, sender, fakeplr, fakemsg)
|
|
end,
|
|
help = function(unm, args)
|
|
ch.send(unm, "not added yet sry")
|
|
end,
|
|
}
|
|
|
|
local function processMessage(unm, msg, hidden)
|
|
if not hidden and msg:sub(1, #cfg.prefix) ~= cfg.prefix then return end
|
|
|
|
if not hidden then
|
|
msg = msg:sub(#cfg.prefix + 1)
|
|
end
|
|
|
|
local args = {}
|
|
|
|
for arg in msg:gmatch("%S+") do
|
|
table.insert(args, arg)
|
|
end
|
|
|
|
local cmd = table.remove(args, 1)
|
|
|
|
local user = cfg.users[unm]
|
|
if user == nil then
|
|
ch.reportLog("foreign user " .. unm .. " tries: " .. cmd)
|
|
return
|
|
end
|
|
|
|
if user.limited and (cmd ~= "dims" and cmd ~= "faketpa" and cmd ~= "help" and cmd ~= "ping") then
|
|
ch.reportLog("limited user " .. unm .. " tries: " .. cmd)
|
|
return
|
|
end
|
|
|
|
local handler = cmds[cmd]
|
|
if handler then
|
|
ch.reportLog("user " .. unm .. " runs: " .. cmd)
|
|
local res = handler(unm, args)
|
|
|
|
if res then
|
|
-- command requested shutdown
|
|
return true
|
|
end
|
|
else
|
|
ch.reportLog("user " .. unm .. " tries invalid cmd: " .. cmd)
|
|
ch.send(unm, "not a valid command", "cmd")
|
|
end
|
|
end
|
|
|
|
local function scheduleScan()
|
|
os.startTimer(2)
|
|
end
|
|
|
|
local termStreak = 0
|
|
local termStreakAt = 0
|
|
local lastPlrList
|
|
local listeners = {
|
|
["chat"] = function(username, message, uuid, isHidden)
|
|
return processMessage(username, message, isHidden)
|
|
end,
|
|
["playerChangedDimension"] = function(username, fromDim, toDim)
|
|
ch.reportLog(username .. " goes from " .. fromDim .. " to " .. toDim, "dims")
|
|
end,
|
|
["playerJoin"] = function(username, dimension)
|
|
tk.plrs[username] = { dimension = dimension }
|
|
end,
|
|
["playerLeave"] = function(username, dimension)
|
|
tk.plrs[username] = nil
|
|
end,
|
|
["timer"] = function()
|
|
local plrs = tk:scanRange(16)
|
|
table.sort(plrs)
|
|
|
|
local plrList = table.concat(plrs, ", ")
|
|
if plrList ~= lastPlrList then
|
|
if #plrs > 0 then
|
|
ch.reportLog("players in range: " .. plrList, "scan")
|
|
elseif lastPlrList ~= nil then
|
|
ch.reportLog("players left range", "scan")
|
|
end
|
|
|
|
lastPlrList = plrList
|
|
end
|
|
|
|
scheduleScan()
|
|
end,
|
|
["terminate"] = function()
|
|
-- should we reset term streak
|
|
if os.clock() - termStreakAt > 1 then
|
|
-- yes
|
|
termStreak = 1
|
|
termStreakAt = os.clock()
|
|
else
|
|
-- no so increase streak
|
|
termStreak = termStreak + 1
|
|
end
|
|
|
|
-- check if streak is too much
|
|
if termStreak >= 2 then
|
|
ch.reportLog("restarting to prevent sys access..")
|
|
os.reboot()
|
|
end
|
|
|
|
print("?")
|
|
ch.reportLog("ALERT!! somebody tried to terminate skynet runner")
|
|
end,
|
|
}
|
|
|
|
local function evDispatcher()
|
|
while true do
|
|
local params = { os.pullEventRaw() }
|
|
local event = table.remove(params, 1)
|
|
local listener = listeners[event]
|
|
|
|
if listener and listener(table.unpack(params)) then
|
|
break
|
|
end
|
|
end
|
|
end
|
|
|
|
ch.userBc("Loaded!")
|
|
ch.reportLog("you are auto subbed to log, welcome!")
|
|
|
|
tk:refresh()
|
|
scheduleScan()
|
|
evDispatcher()
|