Skip to content

Commit

Permalink
Massive server command despaghettification
Browse files Browse the repository at this point in the history
  • Loading branch information
LBPHacker committed Jun 20, 2021
1 parent 34fbfc0 commit d7f578b
Show file tree
Hide file tree
Showing 11 changed files with 415 additions and 405 deletions.
2 changes: 1 addition & 1 deletion tptmp/client/localcmd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ local cmdp = command_parser.new({
end
return false
end,
help = "/fpssync on\\check\\off: enables or disables FPS synchronization with those in the room who also have it enabled",
help = "/fpssync on\\check\\off: enables or disables FPS synchronization with those in the room who also have it enabled (not yet implemented)",
},
connect = {
macro = function(localcmd, message, words, offsets)
Expand Down
35 changes: 14 additions & 21 deletions tptmp/server/plugins/ban.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,15 @@ local jnet = require("jnet")

local server_ban_i = {}

function server_ban_i:insert_host_ban_(host_str)
local ok, host = pcall(jnet, host_str)
if not ok then
return nil, "einval", "invalid subnet"
end
function server_ban_i:insert_host_ban_(host)
if not self.host_bans_:insert(host) then
return nil, "eexist", "already banned"
end
self:save_host_bans_()
return true
end

function server_ban_i:remove_host_ban_(host_str)
local ok, host = pcall(jnet, host_str)
if not ok then
return nil, "einval", "invalid subnet"
end
function server_ban_i:remove_host_ban_(host)
if not self.host_bans_:remove(host) then
return nil, "enoent", "not currently banned"
end
Expand Down Expand Up @@ -97,53 +89,54 @@ return {
func = function(rcon, data)
local server = rcon:server()
if type(data.nick) ~= "string" then
return { status = "badnick", human = "invalid nick", nick = data.nick }
return { status = "badnick", human = "invalid nick" }
end
local uid = server:offline_user_by_nick(data.nick)
if not uid then
return { status = "nouser", human = "no such user", nick = data.nick }
return { status = "nouser", human = "no such user" }
end
if data.action == "insert" then
local ok, err, human = server:insert_uid_ban_(uid)
if not ok then
return { status = err, human = human, uid = uid }
return { status = err, human = human }
end
return { status = "ok" }
elseif data.action == "remove" then
local ok, err, human = server:remove_uid_ban_(uid)
if not ok then
return { status = err, human = human, uid = uid }
return { status = err, human = human }
end
return { status = "ok" }
elseif data.action == "check" then
return { status = "ok", banned = server:uid_banned_(uid) }
end
return { status = "badaction", human = "unrecognized action", action = data.action }
return { status = "badaction", human = "unrecognized action" }
end,
},
ipban = {
func = function(rcon, data)
local server = rcon:server()
if type(data.host) ~= "string" then
return { status = "badhost", human = "invalid host", host = data.host }
local ok, host = pcall(jnet, data.host)
if not ok then
return { status = "badhost", human = "invalid host: " .. host, reason = host }
end
if data.action == "insert" then
local ok, err, human = server:insert_host_ban_(data.host)
local ok, err, human = server:insert_host_ban_(host)
if not ok then
return { status = err, human = human }
end
return { status = "ok" }
elseif data.action == "remove" then
local ok, err, human = server:remove_host_ban_(data.host)
local ok, err, human = server:remove_host_ban_(host)
if not ok then
return { status = err, human = human }
end
return { status = "ok" }
elseif data.action == "check" then
local banned_subnet = server:host_banned_(data.host)
local banned_subnet = server:host_banned_(host)
return { status = "ok", banned = banned_subnet and tostring(banned_subnet) or false }
end
return { status = "badaction", human = "unrecognized action", action = data.action }
return { status = "badaction", human = "unrecognized action" }
end,
},
},
Expand Down
227 changes: 99 additions & 128 deletions tptmp/server/plugins/block.lua
Original file line number Diff line number Diff line change
@@ -1,188 +1,159 @@
local util = require("tptmp.server.util")
local config = require("tptmp.server.config")

local client_block_i = {}
local server_block_i = {}

function client_block_i:rehash_blocked_by_()
local dconf = self:server():dconf()
local uids = not self:guest() and dconf:root().blocked_by[tostring(self:uid())]
local lookup = {}
if uids then
for i = 1, #uids do
lookup[uids[i]] = true
function server_block_i:uid_blocks_(dest, src)
local dconf = self:dconf()
local block = dconf:root().block
local uids = block[tostring(dest)]
return uids and util.array_find(uids, src)
end

function server_block_i:uid_add_block_(dest, src)
local dconf = self:dconf()
local block = dconf:root().block
local uids = block[tostring(dest)]
local idx = uids and util.array_find(uids, src)
if not idx then
uids = uids or {}
block[tostring(dest)] = uids
table.insert(uids, src)
uids[0] = #uids
dconf:commit()
end
end

function server_block_i:uid_remove_block_(dest, src)
local dconf = self:dconf()
local block = dconf:root().block
local uids = block[tostring(dest)]
local idx = uids and util.array_find(uids, src)
if idx then
table.remove(uids, idx)
uids[0] = #uids
if #uids == 0 then
block[tostring(dest)] = nil
end
dconf:commit()
end
self.blocked_by_ = lookup
end

return {
commands = {
block = {
func = function(client, message, words, offsets)
if not words[3] then
-- * TODO[req]: list blocked users
return false
end
local rnick = words[3]
local server = client:server()
if words[2] == "add" then
local other_by_nick = server:client_by_nick(rnick)
local blocked = false
local nick
if other_by_nick and (other_by_nick:guest() or client:guest()) then
if not other_by_nick.temp_blocked_by_[client] then
blocked = true
other_by_nick.temp_blocked_by_[client] = true
end
nick = other_by_nick:nick()
else
local other_uid, other_nick = server:offline_user_by_nick(rnick)
if not other_uid then
client:send_server("* No such user")
return true
end
local dconf = server:dconf()
local blocked_by = dconf:root().blocked_by
local uids = blocked_by[tostring(other_uid)]
local idx = uids and util.array_find(uids, client:uid())
if not idx then
local blocks = uids and #uids or 0
if blocks >= config.max_blocks_per_user then
client:send_server("You have blocked too many users, contact staff")
return true
end
blocked = true
if not uids then
uids = {}
blocked_by[tostring(other_uid)] = uids
end
table.insert(uids, client:uid())
uids[0] = #uids
dconf:commit()
local other_by_uid = server:client_by_uid(other_uid)
if other_by_uid then
other_by_uid:rehash_blocked_by_()
end
end
nick = other_nick
local blockf, checkf, unblockf, other_nick
if client:guest() then
local other = server:client_by_nick(words[3])
if not other then
client:send_server("* User not online")
return true
end
if blocked then
server.log_inf_("$ blocked $", client:nick(), nick)
client:send_server(("* %s is now blocked"):format(nick))
else
client:send_server(("* %s is already blocked"):format(nick))
other_nick = other:nick()
function blockf()
client.blocks_clients_[other] = true
end
elseif words[2] == "check" then
local other_by_nick = server:client_by_nick(rnick)
local unblocked = false
local nick
if other_by_nick and (other_by_nick:guest() or client:guest()) then
if other_by_nick.temp_blocked_by_[client] then
unblocked = true
end
nick = other_by_nick:nick()
else
local other_uid, other_nick = server:offline_user_by_nick(rnick)
if not other_uid then
client:send_server("* No such user")
return true
end
local dconf = server:dconf()
local blocked_by = dconf:root().blocked_by
local uids = blocked_by[tostring(other_uid)]
local idx = uids and util.array_find(uids, client:uid())
if idx then
unblocked = true
end
nick = other_nick
function checkf()
return client.blocks_clients_[other]
end
function unblockf()
client.blocks_clients_[other] = nil
end
else
local other_uid
other_uid, other_nick = server:offline_user_by_nick(words[3])
if not other_uid then
client:send_server("* No such user")
return true
end
function blockf()
server:uid_add_block_(client:uid(), other_uid)
end
if unblocked then
client:send_server(("* %s is currently unblocked"):format(nick))
function checkf()
return server:uid_blocks_(client:uid(), other_uid)
end
function unblockf()
server:uid_remove_block_(client:uid(), other_uid)
end
end
if words[2] == "add" then
if not checkf() then
blockf()
client:send_server("* User is now blocked")
server.log_inf_("$ blocked $", client:nick(), other_nick)
else
client:send_server(("* %s is not currently blocked"):format(nick))
client:send_server("* User is already blocked")
end
elseif words[2] == "remove" then
local other_by_nick = server:client_by_nick(rnick)
local unblocked = false
local nick
if other_by_nick and (other_by_nick:guest() or client:guest()) then
if other_by_nick.temp_blocked_by_[client] then
unblocked = true
other_by_nick.temp_blocked_by_[client] = nil
end
nick = other_by_nick:nick()
return true
elseif words[2] == "check" then
if checkf() then
client:send_server("* User is currently blocked")
else
local other_uid, other_nick = server:offline_user_by_nick(rnick)
if not other_uid then
client:send_server("* No such user")
return true
end
local dconf = server:dconf()
local blocked_by = dconf:root().blocked_by
local uids = blocked_by[tostring(other_uid)]
local idx = uids and util.array_find(uids, client:uid())
if idx then
unblocked = true
table.remove(uids, idx)
uids[0] = #uids
if #uids == 0 then
blocked_by[tostring(other_uid)] = nil
end
dconf:commit()
local other_by_uid = server:client_by_uid(other_uid)
if other_by_uid then
other_by_uid:rehash_blocked_by_()
end
end
nick = other_nick
client:send_server("* User is not currently blocked")
end
if unblocked then
server.log_inf_("$ unblocked $", client:nick(), nick)
client:send_server(("* %s is now unblocked"):format(nick))
return true
elseif words[2] == "remove" then
if checkf() then
unblockf()
client:send_server("* User is no longer blocked")
server.log_inf_("$ unblocked $", client:nick(), other_nick)
else
client:send_server(("* %s is not currently blocked"):format(nick))
client:send_server("* User is not currently blocked")
end
else
return false
return true
end
return true
return false
end,
help = "/block add\\check\\remove [user]: blocks a user, preventing them from messaging you or interacting with you otherwise, checks whether a user is blocked, or unblocks a user",
},
},
hooks = {
load = {
func = function(mtidx_augment)
mtidx_augment("client", client_block_i)
mtidx_augment("server", server_block_i)
end,
},
init = {
func = function(server)
local dconf = server:dconf()
dconf:root().blocked_by = dconf:root().blocked_by or {}
dconf:root().block = dconf:root().block or {}
dconf:commit()
end,
},
connect = {
func = function(client)
client.temp_blocked_by_ = setmetatable({}, { __mode = "k" })
client:rehash_blocked_by_()
client.blocks_clients_ = {}
end,
},
cleanup_client = {
func = function(client)
for _, other in pairs(client:server():clients()) do
other.blocks_clients_[client] = nil
end
end,
},
},
checks = {
can_interact_with = {
func = function(src, dest)
if type(dest) ~= "number" then
if src.temp_blocked_by_[dest] then
if dest.blocks_clients_[src] then
return false, "temporarily blocked"
end
if dest:guest() then
return true
end
dest = dest:uid()
end
if src.blocked_by_[dest] then
if src:guest() then
return true
end
if src:server():uid_blocks_(dest, src:uid()) then
return false, "permanently blocked"
end
return true
Expand Down
Loading

0 comments on commit d7f578b

Please sign in to comment.