Last active
March 26, 2019 18:32
-
-
Save meskarune/4d979476ae44cc7cde6adc010bf86f14 to your computer and use it in GitHub Desktop.
get dictionary definitions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env lua | |
local host, port = "all.dict.org", 2628 | |
local socket = require("socket") | |
local tcp = assert(socket.tcp()) | |
-- rfc: https://tools.ietf.org/html/rfc2229 | |
-- list databases: SHOW DB | |
-- get definition: DEFINE gcide loquacious | |
function dictionary(query) | |
tcp:connect(host, port); | |
tcp:send(("DEFINE * %s\n"):format(query)); | |
while true do | |
local response = tcp:receive() | |
local status = response:match('^(%d%d%d) ') or nil | |
if status == nil then | |
print(response) | |
elseif status == '552' then | |
print('No definition available') | |
break | |
elseif status == '501' then | |
print('Syntax error') | |
break | |
elseif status == '250' then | |
break | |
end | |
end | |
tcp:close() | |
end | |
if #arg == 0 then | |
print('Please give me a search term: dict.lua "<search>"\n') | |
else | |
local query = ("'%s'"):format(table.concat(arg, ' ')) | |
:gsub('%s+', ' ') | |
:gsub('^%s*(.-)%s*$', '%1') | |
dictionary(query) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment