mcbot/cmds/dict.lua
author Mikael Berthe <mikael@lilotux.net>
Tue, 01 Jun 2010 16:41:49 +0200
changeset 42 99477d3c2dda
parent 16 064a50911e05
child 66 d9c00a9fe9d5
permissions -rw-r--r--
Fix case where dict doesn't return anything
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     1
16
064a50911e05 Update command infrastructure
Mikael Berthe <mikael@lilotux.net>
parents: 2
diff changeset
     2
local dict = { ["desc"] = "Dictionnary" }
064a50911e05 Update command infrastructure
Mikael Berthe <mikael@lilotux.net>
parents: 2
diff changeset
     3
064a50911e05 Update command infrastructure
Mikael Berthe <mikael@lilotux.net>
parents: 2
diff changeset
     4
function dict.cmd (args)
0
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     5
    if not args then return nil, "Give me a word, please" end
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     6
    -- Be careful as we pass it as an argument to a shell command
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     7
    local word = string.match(args, "^(%w+)[%s%?%.%!]*$")
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     8
    if not word then return nil, "Can you repeat please?" end
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     9
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    10
    local cmd = "/usr/bin/dict "..word.." 2> /dev/null"
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    11
    local fh = io.popen(cmd)
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    12
    result = fh:read("*a")  -- read dict output
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    13
    fh:close()
42
99477d3c2dda Fix case where dict doesn't return anything
Mikael Berthe <mikael@lilotux.net>
parents: 16
diff changeset
    14
    if not result or result:match("^$") then return nil, "dict: No output" end
0
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    15
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    16
    -- Sometimes dict outpur is ugly... Let's filter out whitespace.
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    17
    result = result:gsub("[ \t]+\n", "\n"):gsub("\n\n\n+", "\n\n")
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    18
    -- Trim trailing newlines
2
c7ac7b6a76e0 Small update to dict command
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    19
    result = result:gsub("\n+$", "")
c7ac7b6a76e0 Small update to dict command
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    20
    return result
0
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    21
end
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    22
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    23
mcbot_register_command("dict", dict)