mcbot/cmds/littre.lua
author Mikael Berthe <mikael@lilotux.net>
Sun, 23 Oct 2011 14:10:39 +0200
changeset 56 a7c6c2b3454b
parent 55 7f235c928661
child 66 d9c00a9fe9d5
permissions -rw-r--r--
littre: allow some more chars: space and ', -


local littre = { ["desc"] = "French Dictionnary Littré" }

function littre.cmd (args)
    if not args then return nil, "Give me a word, please" end
    -- Be careful as we pass it as an argument to a shell command
    local word = string.match(args, "^([%wàäâéèëêïîöôùüûç' -]+)[%s%?%.%!]*$")
    if not word then return nil, "Can you repeat please?" end

    local cmd = '/usr/bin/sdcv --non-interactive -- "'..word..'" 2> /dev/null'
    local fh = io.popen(cmd)
    result = fh:read("*a")  -- read littre output
    fh:close()
    if not result or result:match("^$") then return nil, "littre: No output" end

    -- Let's filter out whitespace.
    result = result:gsub("[ \t]+\n", "\n"):gsub("\n\n\n+", "\n\n")
    -- Trim trailing newlines
    result = result:gsub("\n+$", "")
    return result
end

mcbot_register_command("littre", littre)