Skip to content

Instantly share code, notes, and snippets.

@idealwebsolutions
Last active August 1, 2020 18:38
Show Gist options
  • Select an option

  • Save idealwebsolutions/455e844cd704721bb03b073d3311da7c to your computer and use it in GitHub Desktop.

Select an option

Save idealwebsolutions/455e844cd704721bb03b073d3311da7c to your computer and use it in GitHub Desktop.
Morse code script for Weechat
local SCRIPT_NAME = 'morse'
local SCRIPT_AUTHOR = 'unaffiliated <[email protected]>'
local SCRIPT_VERSION = '1'
local SCRIPT_LICENSE = 'GPL3'
local SCRIPT_DESC = 'Morse code translator'
-- International Morse Alphabet (ITU)
local MORSE = {
a = ".-",
b = "-...",
c = "-.-.",
d = "-..",
e = ".",
f = "..-.",
g = "--.",
h = "....",
i = "..",
j = ".---",
k = "-.-",
l = ".-..",
m = "--",
n = "-.",
o = "---",
p = ".--.",
q = "--.-",
r = ".-.",
s = "...",
t = "-",
u = "..-",
v = "...-",
w = ".--",
x = "-..-",
y = "-.--",
z = "--..",
["1"] = ".----",
["2"] = "..---",
["3"] = "...--",
["4"] = "....-",
["5"] = ".....",
["6"] = "-....",
["7"] = "--...",
["8"] = "---..",
["9"] = "----.",
["0"] = "-----",
[" "] = " " }
local w = weechat
local function find_key(table, element)
for key, value in pairs(table) do
if value == element then
return key
end
end
return nil
end
function to_morse(text)
assert(type(text) == 'string', 'input: ' .. text .. ' is not a string')
local t = {''}
for ch in text:gmatch('.') do
if MORSE[ch] then
table.insert(t, MORSE[ch])
end
end
return table.concat(t, '')
end
function from_morse(morse)
assert(type(morse) == 'string', 'input: ' .. morse .. ' is not a string')
local t = {''}
morse = string.gsub(morse, '%s%s', ' /')
for ch in morse:gmatch('%S+') do
local val = find_key(MORSE, ch)
if ch == '/' then
table.insert(t, ' ')
elseif val then
table.insert(t, val)
end
end
return table.concat(t, '')
end
function morse_replace_input_string(buffer)
local input_str = w.buffer_get_string(buffer, 'input')
if input_str:match('^/script ') then
return w.WEECHAT_RC_OK
end
w.buffer_set(buffer, 'input', to_morse(input_str))
return w.WEECHAT_RC_OK
end
function morse_input_replacer(data, buffer, command)
if command == '/input return' then
return morse_replace_input_string(buffer)
end
return w.WEECHAT_RC_OK
end
function m_init()
if w.register(SCRIPT_NAME, SCRIPT_AUTHOR,
SCRIPT_VERSION, SCRIPT_LICENSE,
SCRIPT_DESC, '', '') then
w.hook_command_run('/input return', 'morse_input_replacer', '')
end
end
m_init()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment