Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 41 additions & 32 deletions lua/entities/gmod_wire_expression2/core/console.lua
Original file line number Diff line number Diff line change
@@ -1,34 +1,44 @@
/******************************************************************************\
Console support
\******************************************************************************/
--
-- Console support
--

E2Lib.RegisterExtension("console", true, "Lets E2 chips run concommands and retrieve convars")

local function tokenizeAndGetCommands(str)
local function tokenizeAndGetCommands(concmd)
-- Tokenize!
local tokens = {}
local curtoken = {}
local escaped = false
for i=1, #str do
local char = string.sub(str, i, i)

for i = 1, #concmd do
local char = string.sub(concmd, i, i)

if (escaped and char ~= "\"") or string.match(char, "[%w+-]") then
curtoken[#curtoken + 1] = char
table.insert(curtoken, char)
else
if #curtoken>0 then tokens[#tokens + 1] = table.concat(curtoken) curtoken = {} end
if #curtoken > 0 then
table.insert(tokens, table.concat(curtoken))
curtoken = {}
end

if char == "\"" then
escaped = not escaped
elseif char ~= " " then
tokens[#tokens + 1] = char
table.insert(tokens, char)
end
end
end
if #curtoken>0 then tokens[#tokens+1] = table.concat(curtoken) end

if #curtoken > 0 then
table.insert(tokens, table.concat(curtoken))
end

-- Get table of commands used
local commands = {tokens[1] or ""}
for i=1, #tokens do

for i = 1, #tokens do
if tokens[i]==";" then
commands[#commands + 1] = tokens[i+1] or ""
table.insert(commands, tokens[i + 1])
end
end

Expand All @@ -38,17 +48,15 @@ end
---@param cvar "wire_expression2_concmd_whitelist"|"wire_expression2_convar_whitelist"
---@return table whitelist # Whitelist for specific commands, if empty, disregard whitelist and allow everything
local function getWhitelist(ply, cvar)
local whitelist = (ply:GetInfo(cvar) or ""):Trim()
local whitelist = {}

local whitelistTbl = {}

for k, v in pairs(string.Split(whitelist, ",")) do
if v~="" then
whitelistTbl[v] = true
for _, v in ipairs(string.Split(string.Trim(ply:GetInfo(cvar)), ",")) do
if v ~= "" then
whitelist[v] = true
end
end

return whitelistTbl
return whitelist
end

local function checkConCmd(self, cmd)
Expand All @@ -61,17 +69,18 @@ local function checkConCmd(self, cmd)
if ply:GetInfoNum("wire_expression2_concmd", 0) == 0 then return self:throw("Concmd is disabled through wire_expression2_concmd", false) end
if IsConCommandBlocked(cmd) then return self:throw("This concmd is blacklisted by gmod, see https://wiki.facepunch.com/gmod/Blocked_ConCommands", false) end

if hook.Run( "Expression2_CanConCmd", ply, cmd ) == false then
if hook.Run("Expression2_CanConCmd", ply, cmd) == false then
return self:throw("Command '" .. cmd .. "' was blocked by the server. ", false)
end

local whitelist = getWhitelist(ply, "wire_expression2_concmd_whitelist")
if table.IsEmpty(whitelist) then return true end

local commands = tokenizeAndGetCommands(cmd)
for _, command in pairs(commands) do

for _, command in ipairs(commands) do
if not whitelist[command] then
return self:throw("Command '" .. command .. "' is not whitelisted w/ wire_expression2_concmd_whitelist", false)
return self:throw("Command '" .. command .. "' is not whitelisted in wire_expression2_concmd_whitelist", false)
end
end

Expand All @@ -86,19 +95,20 @@ local function checkConVar(self, var)
if ply:GetInfoNum("wire_expression2_convar", 0) == 0 then return self:throw("Convar is disabled through wire_expression2_convar", false) end
var = var:match("%s*([%w_]+)%s*")

if hook.Run("Expression2_CanConVar", ply, var) == false then
return self:throw("Convar '" .. var .. "' was blocked by the server. ", false)
end

local whitelist = getWhitelist(ply, "wire_expression2_convar_whitelist")
if table.IsEmpty(whitelist) then return true end

if whitelist[var] == nil then return self:throw("Convar '" .. var .. "' is not whitelisted w/ wire_expression2_convar_whitelist ", false) end

if hook.Run("Expression2_CanConVar", ply, var) == false then
return self:throw("Convar '" .. var .. "' was blocked by the server. ", false)
end
if whitelist[var] == nil then
return self:throw("Convar '" .. var .. "' is not whitelisted in wire_expression2_convar_whitelist ", false)
end

return true
end


__e2setcost(5)

e2function number concmd(string command)
Expand All @@ -118,11 +128,10 @@ e2function number convarnum(string cvar)
end

e2function number maxOfType(string typename)
if typename == "wire_holograms" then return GetConVarNumber("wire_holograms_max") or 0 end
return GetConVarNumber("sbox_max"..typename) or 0
if typename == "wire_holograms" then return cvars.Number("wire_holograms_max", 0) end
return cvars.Number("sbox_max" .. typename, 0)
end

e2function number playerDamage()
local ret = GetConVarNumber("sbox_playershurtplayers") or 0
return ret ~= 0 and 1 or 0
return cvars.Bool("sbox_playershurtplayers") and 1 or 0
end
Loading