luasocket/etc/dict.lua

153 lines
4.6 KiB
Lua
Raw Normal View History

2003-03-28 22:08:50 +01:00
-----------------------------------------------------------------------------
-- Little program to download DICT word definitions
-- LuaSocket sample files
-- Author: Diego Nehab
-- RCS ID: $Id$
2003-03-28 22:08:50 +01:00
-----------------------------------------------------------------------------
2004-06-17 23:46:22 +02:00
-----------------------------------------------------------------------------
-- Load required modules
-----------------------------------------------------------------------------
2005-06-14 06:29:23 +02:00
local base = _G
2004-11-27 08:58:04 +01:00
local string = require("string")
local table = require("table")
2004-06-04 17:15:45 +02:00
local socket = require("socket")
local url = require("socket.url")
local tp = require("socket.tp")
2004-12-23 23:32:12 +01:00
module("socket.dict")
2004-06-17 23:46:22 +02:00
-----------------------------------------------------------------------------
-- Globals
2004-06-17 23:46:22 +02:00
-----------------------------------------------------------------------------
HOST = "dict.org"
PORT = 2628
TIMEOUT = 10
-----------------------------------------------------------------------------
-- Low-level dict API
-----------------------------------------------------------------------------
local metat = { __index = {} }
function open(host, port)
local tp = socket.try(tp.connect(host or HOST, port or PORT, TIMEOUT))
2004-11-27 08:58:04 +01:00
return base.setmetatable({tp = tp}, metat)
end
2004-06-17 23:46:22 +02:00
function metat.__index:greet()
return socket.try(self.tp:check(220))
end
function metat.__index:check(ok)
local code, status = socket.try(self.tp:check(ok))
return code,
2004-11-27 08:58:04 +01:00
base.tonumber(socket.skip(2, string.find(status, "^%d%d%d (%d*)")))
2004-06-17 23:46:22 +02:00
end
function metat.__index:getdef()
local line = socket.try(self.tp:receive())
local def = {}
while line ~= "." do
table.insert(def, line)
line = socket.try(self.tp:receive())
end
return table.concat(def, "\n")
end
2000-12-29 23:15:09 +01:00
2004-06-17 23:46:22 +02:00
function metat.__index:define(database, word)
database = database or "!"
socket.try(self.tp:command("DEFINE", database .. " " .. word))
2004-06-17 23:46:22 +02:00
local code, count = self:check(150)
local defs = {}
for i = 1, count do
self:check(151)
table.insert(defs, self:getdef())
end
self:check(250)
2004-06-17 23:46:22 +02:00
return defs
end
2000-12-29 23:15:09 +01:00
2004-06-17 23:46:22 +02:00
function metat.__index:match(database, strat, word)
database = database or "!"
strat = strat or "."
socket.try(self.tp:command("MATCH", database .." ".. strat .." ".. word))
2004-06-17 23:46:22 +02:00
self:check(152)
local mat = {}
local line = socket.try(self.tp:receive())
2004-06-17 23:46:22 +02:00
while line ~= '.' do
database, word = socket.skip(2, string.find(line, "(%S+) (.*)"))
if not mat[database] then mat[database] = {} end
table.insert(mat[database], word)
line = socket.try(self.tp:receive())
end
self:check(250)
2004-06-17 23:46:22 +02:00
return mat
end
function metat.__index:quit()
self.tp:command("QUIT")
return self:check(221)
2004-06-17 23:46:22 +02:00
end
function metat.__index:close()
return self.tp:close()
end
2000-12-29 23:15:09 +01:00
2004-06-17 23:46:22 +02:00
-----------------------------------------------------------------------------
-- High-level dict API
-----------------------------------------------------------------------------
local default = {
scheme = "dict",
host = "dict.org"
}
local function there(f)
if f == "" then return nil
2004-06-17 23:46:22 +02:00
else return f end
2000-12-29 23:15:09 +01:00
end
2004-06-17 23:46:22 +02:00
local function parse(u)
local t = socket.try(url.parse(u, default))
socket.try(t.scheme == "dict", "invalid scheme '" .. t.scheme .. "'")
socket.try(t.path, "invalid path in url")
local cmd, arg = socket.skip(2, string.find(t.path, "^/(.)(.*)$"))
socket.try(cmd == "d" or cmd == "m", "<command> should be 'm' or 'd'")
socket.try(arg and arg ~= "", "need at least <word> in URL")
t.command, t.argument = cmd, arg
arg = string.gsub(arg, "^:([^:]+)", function(f) t.word = f end)
socket.try(t.word, "need at least <word> in URL")
arg = string.gsub(arg, "^:([^:]*)", function(f) t.database = there(f) end)
if cmd == "m" then
2004-06-17 23:46:22 +02:00
arg = string.gsub(arg, "^:([^:]*)", function(f) t.strat = there(f) end)
end
2004-11-27 08:58:04 +01:00
string.gsub(arg, ":([^:]*)$", function(f) t.n = base.tonumber(f) end)
2004-06-17 23:46:22 +02:00
return t
end
2003-03-28 22:08:50 +01:00
2004-06-17 23:46:22 +02:00
local function tget(gett)
local con = open(gett.host, gett.port)
con:greet()
if gett.command == "d" then
local def = con:define(gett.database, gett.word)
con:quit()
con:close()
if gett.n then return def[gett.n]
2004-06-17 23:46:22 +02:00
else return def end
elseif gett.command == "m" then
local mat = con:match(gett.database, gett.strat, gett.word)
con:quit()
con:close()
return mat
else return nil, "invalid command" end
2003-03-28 22:08:50 +01:00
end
2004-06-17 23:46:22 +02:00
local function sget(u)
local gett = parse(u)
return tget(gett)
end
get = socket.protect(function(gett)
2004-11-27 08:58:04 +01:00
if base.type(gett) == "string" then return sget(gett)
2004-06-17 23:46:22 +02:00
else return tget(gett) end
end)
2004-11-27 08:58:04 +01:00