author | Mikael Berthe <mikael@lilotux.net> |
Tue, 27 Nov 2012 16:26:04 +0100 | |
changeset 66 | d9c00a9fe9d5 |
parent 42 | 99477d3c2dda |
permissions | -rw-r--r-- |
66
d9c00a9fe9d5
Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents:
42
diff
changeset
|
1 |
|
d9c00a9fe9d5
Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents:
42
diff
changeset
|
2 |
-- This module is part of the McBot / mcabbot project |
d9c00a9fe9d5
Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents:
42
diff
changeset
|
3 |
-- |
d9c00a9fe9d5
Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents:
42
diff
changeset
|
4 |
-- Copyright (C) 2010-2012 Mikael Berthe <mikael@lilotux.net> |
d9c00a9fe9d5
Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents:
42
diff
changeset
|
5 |
-- |
d9c00a9fe9d5
Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents:
42
diff
changeset
|
6 |
-- This program is free software; you can redistribute it and/or modify |
d9c00a9fe9d5
Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents:
42
diff
changeset
|
7 |
-- it under the terms of the GNU General Public License as published by |
d9c00a9fe9d5
Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents:
42
diff
changeset
|
8 |
-- the Free Software Foundation; either version 2 of the License, or (at |
d9c00a9fe9d5
Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents:
42
diff
changeset
|
9 |
-- your option) any later version. |
d9c00a9fe9d5
Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents:
42
diff
changeset
|
10 |
-- |
d9c00a9fe9d5
Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents:
42
diff
changeset
|
11 |
-- Please check the license in the COPYING file at the root of the tree. |
d9c00a9fe9d5
Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents:
42
diff
changeset
|
12 |
-- |
0 | 13 |
|
16
064a50911e05
Update command infrastructure
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
14 |
local dict = { ["desc"] = "Dictionnary" } |
064a50911e05
Update command infrastructure
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
15 |
|
064a50911e05
Update command infrastructure
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
16 |
function dict.cmd (args) |
0 | 17 |
if not args then return nil, "Give me a word, please" end |
18 |
-- Be careful as we pass it as an argument to a shell command |
|
19 |
local word = string.match(args, "^(%w+)[%s%?%.%!]*$") |
|
20 |
if not word then return nil, "Can you repeat please?" end |
|
21 |
||
22 |
local cmd = "/usr/bin/dict "..word.." 2> /dev/null" |
|
23 |
local fh = io.popen(cmd) |
|
24 |
result = fh:read("*a") -- read dict output |
|
25 |
fh:close() |
|
42
99477d3c2dda
Fix case where dict doesn't return anything
Mikael Berthe <mikael@lilotux.net>
parents:
16
diff
changeset
|
26 |
if not result or result:match("^$") then return nil, "dict: No output" end |
0 | 27 |
|
28 |
-- Sometimes dict outpur is ugly... Let's filter out whitespace. |
|
29 |
result = result:gsub("[ \t]+\n", "\n"):gsub("\n\n\n+", "\n\n") |
|
30 |
-- Trim trailing newlines |
|
2
c7ac7b6a76e0
Small update to dict command
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
31 |
result = result:gsub("\n+$", "") |
c7ac7b6a76e0
Small update to dict command
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
32 |
return result |
0 | 33 |
end |
34 |
||
35 |
mcbot_register_command("dict", dict) |