author | Mikael Berthe <mikael@lilotux.net> |
Tue, 13 Apr 2010 20:34:59 +0200 | |
changeset 2 | c7ac7b6a76e0 |
parent 0 | 89add07d6fe4 |
child 16 | 064a50911e05 |
permissions | -rw-r--r-- |
0 | 1 |
|
2 |
local function dict (args) |
|
3 |
if not args then return nil, "Give me a word, please" end |
|
4 |
-- Be careful as we pass it as an argument to a shell command |
|
5 |
local word = string.match(args, "^(%w+)[%s%?%.%!]*$") |
|
6 |
if not word then return nil, "Can you repeat please?" end |
|
7 |
||
8 |
local cmd = "/usr/bin/dict "..word.." 2> /dev/null" |
|
9 |
local fh = io.popen(cmd) |
|
10 |
result = fh:read("*a") -- read dict output |
|
11 |
fh:close() |
|
12 |
if result:match("^$") then return nil, "dict: No output" end |
|
13 |
||
14 |
-- Sometimes dict outpur is ugly... Let's filter out whitespace. |
|
15 |
result = result:gsub("[ \t]+\n", "\n"):gsub("\n\n\n+", "\n\n") |
|
16 |
-- Trim trailing newlines |
|
2
c7ac7b6a76e0
Small update to dict command
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
17 |
result = result:gsub("\n+$", "") |
c7ac7b6a76e0
Small update to dict command
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
18 |
return result |
0 | 19 |
end |
20 |
||
21 |
mcbot_register_command("dict", dict) |