mcbot/cmds/littre.lua
author Mikael Berthe <mikael@lilotux.net>
Sun, 23 Oct 2011 14:10:39 +0200
changeset 56 a7c6c2b3454b
parent 55 7f235c928661
child 66 d9c00a9fe9d5
permissions -rw-r--r--
littre: allow some more chars: space and ', -
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
54
be04d7241ef6 New command: littre
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     1
be04d7241ef6 New command: littre
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     2
local littre = { ["desc"] = "French Dictionnary Littré" }
be04d7241ef6 New command: littre
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     3
be04d7241ef6 New command: littre
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     4
function littre.cmd (args)
be04d7241ef6 New command: littre
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     5
    if not args then return nil, "Give me a word, please" end
be04d7241ef6 New command: littre
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     6
    -- Be careful as we pass it as an argument to a shell command
56
a7c6c2b3454b littre: allow some more chars: space and ', -
Mikael Berthe <mikael@lilotux.net>
parents: 55
diff changeset
     7
    local word = string.match(args, "^([%wàäâéèëêïîöôùüûç' -]+)[%s%?%.%!]*$")
54
be04d7241ef6 New command: littre
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     8
    if not word then return nil, "Can you repeat please?" end
be04d7241ef6 New command: littre
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     9
56
a7c6c2b3454b littre: allow some more chars: space and ', -
Mikael Berthe <mikael@lilotux.net>
parents: 55
diff changeset
    10
    local cmd = '/usr/bin/sdcv --non-interactive -- "'..word..'" 2> /dev/null'
54
be04d7241ef6 New command: littre
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    11
    local fh = io.popen(cmd)
be04d7241ef6 New command: littre
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    12
    result = fh:read("*a")  -- read littre output
be04d7241ef6 New command: littre
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    13
    fh:close()
be04d7241ef6 New command: littre
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    14
    if not result or result:match("^$") then return nil, "littre: No output" end
be04d7241ef6 New command: littre
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    15
be04d7241ef6 New command: littre
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    16
    -- Let's filter out whitespace.
be04d7241ef6 New command: littre
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    17
    result = result:gsub("[ \t]+\n", "\n"):gsub("\n\n\n+", "\n\n")
be04d7241ef6 New command: littre
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    18
    -- Trim trailing newlines
be04d7241ef6 New command: littre
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    19
    result = result:gsub("\n+$", "")
be04d7241ef6 New command: littre
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    20
    return result
be04d7241ef6 New command: littre
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    21
end
be04d7241ef6 New command: littre
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    22
be04d7241ef6 New command: littre
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    23
mcbot_register_command("littre", littre)