mcbot/cmds/dict.lua
author Mikael Berthe <mikael@lilotux.net>
Tue, 27 Nov 2012 16:26:04 +0100
changeset 66 d9c00a9fe9d5
parent 42 99477d3c2dda
permissions -rw-r--r--
Add notices before public release


--  This module is part of the McBot / mcabbot project
--
-- Copyright (C) 2010-2012 Mikael Berthe <mikael@lilotux.net>
--
-- This program is free software; you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation; either version 2 of the License, or (at
-- your option) any later version.
--
-- Please check the license in the COPYING file at the root of the tree.
--

local dict = { ["desc"] = "Dictionnary" }

function dict.cmd (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 not result or 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
    result = result:gsub("\n+$", "")
    return result
end

mcbot_register_command("dict", dict)