Skip to content

Commit

Permalink
Merge pull request #659 from Blightmud/telnet_charset_negotiation
Browse files Browse the repository at this point in the history
Introduces CHARSET negotiation (RFC-2066)
  • Loading branch information
LiquidityC committed Sep 8, 2022
2 parents eac1f1d + 5814a5c commit e929ba7
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
74 changes: 74 additions & 0 deletions resources/lua/telnet_charset.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
local PROTOCOL = 42
local REQUEST = 1
local ACCEPTED = 2
local REJECTED = 3

local ACCEPTED_ENCODINGS = {
"UTF-8",
"ASCII",
"US-ASCII",
}

local unpack = table.unpack
local lower = string.lower

core.enable_protocol(PROTOCOL)

local function split(istr, sep)
if sep == nil then
sep = "%s"
end
local t={}
for str in string.gmatch(istr, "([^" .. sep .. "]+)") do
table.insert(t, str)
end
return t
end

local function string_to_bytes(str)
local values = {}
for i, v in utf8.codes(str) do
values[i] = v
end
return values
end

local function send_accept(option)
blight.debug("TELCHR[sending]: ACCEPTED " .. option)
local payload = string_to_bytes(option)
table.insert(payload, 1, ACCEPTED)
core.subneg_send(PROTOCOL, payload)
end

local function send_reject()
blight.debug("TELCHR[sending]: REJECTED")
core.subneg_send(PROTOCOL, { REJECTED })
end

core.subneg_recv(function (proto, recv)
if proto ~= PROTOCOL or recv[1] ~= REQUEST then
return
end

table.remove(recv, 1) -- Remove the negotiation type
local sep = utf8.char(recv[1]) -- Extract separator
table.remove(recv, 1) -- Remove first separator
local options = utf8.char(unpack(recv))
blight.debug("TELCHR[received]: " .. options)

for _,opt in ipairs(split(options, sep)) do
for _,accepted in ipairs(ACCEPTED_ENCODINGS) do
if lower(opt) == lower(accepted) then
send_accept(opt)
return
end
end
end
send_reject()
end)

core.on_protocol_enabled(function (proto)
if proto == PROTOCOL then
mud.add_tag("TELCHR")
end
end)
3 changes: 3 additions & 0 deletions src/lua/lua_script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ fn create_default_lua_state(
state
.load(include_str!("../../resources/lua/plugins.lua"))
.exec()?;
state
.load(include_str!("../../resources/lua/telnet_charset.lua"))
.exec()?;

let lua_gmcp = state
.load(include_str!("../../resources/lua/gmcp.lua"))
Expand Down

0 comments on commit e929ba7

Please sign in to comment.