mcbot/cmds/dict.lua
changeset 0 89add07d6fe4
child 2 c7ac7b6a76e0
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mcbot/cmds/dict.lua	Sun Apr 11 18:13:18 2010 +0200
@@ -0,0 +1,20 @@
+
+local function dict (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/dict "..word.." 2> /dev/null"
+    local fh = io.popen(cmd)
+    result = fh:read("*a")  -- read dict output
+    fh:close()
+    if result:match("^$") then return nil, "dict: No output" end
+
+    -- Sometimes dict outpur is ugly... Let's filter out whitespace.
+    result = result:gsub("[ \t]+\n", "\n"):gsub("\n\n\n+", "\n\n")
+    -- Trim trailing newlines
+    return result:gsub("\n+$", "")
+end
+
+mcbot_register_command("dict", dict)