mcbot/cmds/spell.lua
changeset 0 89add07d6fe4
child 16 064a50911e05
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mcbot/cmds/spell.lua	Sun Apr 11 18:13:18 2010 +0200
@@ -0,0 +1,34 @@
+
+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
+
+local function spell (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)