mcbot/cmds/spell.lua
author Mikael Berthe <mikael@lilotux.net>
Thu, 15 Apr 2010 18:57:38 +0200
changeset 16 064a50911e05
parent 0 89add07d6fe4
permissions -rw-r--r--
Update command infrastructure


local spell = { desc = "Spell checker" }

local function check (text, lang)
    local fname = os.tmpname()
    local fh = io.open(fname, "w")

    fh:write(text)
    fh:close()

    local cmd = "/usr/bin/enchant -a "..fname
    if lang and not lang:match("[^_%w]") then
        cmd = cmd.." -d "..lang
    end
    fh = io.popen(cmd)
    fh:read("*l")           -- skip header
    result = fh:read("*a")  -- read spellchecker output
    fh:close()

    os.remove(fname)
    return result
end

function spell.cmd (args)
    if not args then return nil, "What do you want me to spellcheck?" end
    local r
    local l, s = string.match(args, "^%s*-d%s?([%w_]+)%s+(.*)%s*$")
    if l then
        r = check(s, l)
    else
        r = check(args)
    end
    return r:gsub("\n+$", "")
end

mcbot_register_command("spell", spell)