Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert IRC colour codes to minetest colour codes. #41

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
59 changes: 46 additions & 13 deletions hooks.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,47 @@ local b64e = ie.require("mime").b64
irc.hooks = {}
irc.registered_hooks = {}

local colour_codes = {
[0] = "white",
[1] = "black",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You only really need the index for [0]. The others are auto-numbered starting at 1.

[2] = "blue",
[3] = "green",
[4] = "red",
[5] = "darkred",
[6] = "purple",
[7] = "orange",
[8] = "yellow",
[9] = "limegreen",
[10] = "teal",
[11] = "cyan",
[12] = "deepskyblue",
[13] = "pink",
[14] = "grey",
[15] = "lightgrey",
[99] = "#ffffff"
}

local function convert_colour(text)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor nit: color.

local lines = text:split("\3", true)
for index, line in pairs(lines) do
local irc_code = line:match("^(%d+)")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This block would take e.g. "01foo" at the start of the string as an escape sequence.

line = line:gsub("^(%d+)", "")
local mt_code = colour_codes[tonumber(irc_code)]
if mt_code then
line = core.get_color_escape_sequence(mt_code) .. line
end

lines[index] = line
end
return table.concat(lines, "")
end

local stripped_chars = "[\2\31]"

local function normalize(text)
-- Strip colors
text = text:gsub("\3[0-9][0-9,]*", "")

return text:gsub(stripped_chars, "")
-- convert colors
text = convert_colour(text)
return text:gsub(stripped_chars, "") .. core.get_color_escape_sequence("#ffffff")
end


Expand Down Expand Up @@ -86,7 +119,7 @@ function irc.hooks.ctcp(msg)

if command == "ACTION" and msg.args[1] == irc.config.channel then
local action = text:sub(8, -1)
irc.sendLocal(("* %s@IRC %s"):format(msg.user.nick, action))
irc.sendLocal(("* %s@IRC %s"):format(msg.user.nick, normalize(action)))
elseif command == "VERSION" then
reply(("Minetest version %s, IRC mod version %s.")
:format(get_core_version(), irc.version))
Expand All @@ -99,14 +132,14 @@ end


function irc.hooks.channelChat(msg)
local text = normalize(msg.args[2])

local text_coloured = normalize(msg.args[2])
local text = core.strip_colors(text_coloured)
irc.check_botcmd(msg)

-- Don't let a user impersonate someone else by using the nick "IRC"
local fake = msg.user.nick:lower():match("^[il|]rc$")
if fake then
irc.sendLocal("<"..msg.user.nick.."@IRC> "..text)
irc.sendLocal("<"..msg.user.nick.."@IRC> "..text_coloured)
return
end

Expand Down Expand Up @@ -138,7 +171,7 @@ function irc.hooks.channelChat(msg)
irc.sendLocal(("* %s@%s %s")
:format(actionnick, msg.user.nick, actionmessage))
else
irc.sendLocal(("<%s@IRC> %s"):format(msg.user.nick, text))
irc.sendLocal(("<%s@IRC> %s"):format(msg.user.nick, text_coloured))
end
end

Expand All @@ -160,14 +193,14 @@ function irc.hooks.kick(channel, target, prefix, reason)
irc.disconnect("Kicked")
else
irc.sendLocal(("-!- %s was kicked from %s by %s [%s]")
:format(target, channel, prefix.nick, reason))
:format(target, channel, prefix.nick, normalize(reason)))
end
end


function irc.hooks.notice(user, target, message)
if user.nick and target == irc.config.channel then
irc.sendLocal("-"..user.nick.."@IRC- "..message)
irc.sendLocal("-"..user.nick.."@IRC- "..normalize(message))
end
end

Expand Down Expand Up @@ -202,13 +235,13 @@ end
function irc.hooks.part(user, channel, reason)
reason = reason or ""
irc.sendLocal(("-!- %s has left %s [%s]")
:format(user.nick, channel, reason))
:format(user.nick, channel, normalize(reason)))
end


function irc.hooks.quit(user, reason)
irc.sendLocal(("-!- %s has quit [%s]")
:format(user.nick, reason))
:format(user.nick, normalize(reason)))
end


Expand Down
6 changes: 5 additions & 1 deletion init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,11 @@ setmetatable(irc, {

dofile(modpath.."/config.lua")
dofile(modpath.."/messages.lua")
loadfile(modpath.."/hooks.lua")(ie)
local chunk, errorval = loadfile(modpath.."/hooks.lua")
if chunk == nil then
error(errorval)
end
chunk(ie)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be simplified to assert(loadfile(modpath.."/hooks.lua"))(ie).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be split from this commit, IMHO. It's unrelated to the feature being added.

dofile(modpath.."/callback.lua")
dofile(modpath.."/chatcmds.lua")
dofile(modpath.."/botcmds.lua")
Expand Down
2 changes: 1 addition & 1 deletion messages.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
irc.msgs = irc.lib.msgs

function irc.logChat(message)
minetest.log("action", "IRC CHAT: "..message)
minetest.log("action", "IRC CHAT: " .. core.strip_colors(message))
end

function irc.sendLocal(message)
Expand Down