diff options
| author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2001-04-25 20:08:21 +0000 |
|---|---|---|
| committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2001-04-25 20:08:21 +0000 |
| commit | 7d60e27bea63c4b9228197e400a7edac1118a4b5 (patch) | |
| tree | 45d452be739bd17742adb93382b1600ae6e22309 /etc | |
| parent | 8c6473577da993fb95ccd705214aa9dc11e8ca82 (diff) | |
| download | luasocket-7d60e27bea63c4b9228197e400a7edac1118a4b5.tar.gz luasocket-7d60e27bea63c4b9228197e400a7edac1118a4b5.tar.bz2 luasocket-7d60e27bea63c4b9228197e400a7edac1118a4b5.zip | |
Transformed in a library, instead of a sample program.
Diffstat (limited to 'etc')
| -rw-r--r-- | etc/dict.lua | 96 |
1 files changed, 62 insertions, 34 deletions
diff --git a/etc/dict.lua b/etc/dict.lua index c620cc4..4685ca1 100644 --- a/etc/dict.lua +++ b/etc/dict.lua | |||
| @@ -1,39 +1,67 @@ | |||
| 1 | -- dict.lua | 1 | function get_status(sock, valid) |
| 2 | -- simple client for DICT protocol (see http://www.dict.org/) | 2 | local line, err = sock:receive() |
| 3 | -- shows definitions for each word from stdin. uses only "wn" dictionary. | 3 | local code, par |
| 4 | -- if a word is "=", then the rest of the line is sent verbatim as a protocol | 4 | if not line then sock:close() return err end |
| 5 | -- command to the server. | 5 | _, _, code = strfind(line, "^(%d%d%d)") |
| 6 | code = tonumber(code) | ||
| 7 | if code ~= valid then return code end | ||
| 8 | if code == 150 then | ||
| 9 | _,_,_, par = strfind(line, "^(%d%d%d) (%d*)") | ||
| 10 | par = tonumber(par) | ||
| 11 | end | ||
| 12 | return nil, par | ||
| 13 | end | ||
| 14 | |||
| 15 | function get_def(sock) | ||
| 16 | local line, err = sock:receive() | ||
| 17 | local def = "" | ||
| 18 | while (not err) and line ~= "." do | ||
| 19 | def = def .. line .. "\n" | ||
| 20 | line, err = sock:receive() | ||
| 21 | end | ||
| 22 | if err then sock:close() return nil, err | ||
| 23 | else return def end | ||
| 24 | end | ||
| 6 | 25 | ||
| 7 | if verbose then verbose=write else verbose=function()end end | 26 | function dict_open() |
| 27 | local sock, err = connect("dict.org", 2628) | ||
| 28 | if not sock then return nil, err end | ||
| 29 | sock:timeout(10) | ||
| 30 | local code, par = get_status(sock, 220) | ||
| 31 | if code then return nil, code end | ||
| 32 | return sock | ||
| 33 | end | ||
| 8 | 34 | ||
| 9 | verbose(">>> connecting to server\n") | 35 | function dict_define(sock, word, dict) |
| 10 | local s,e=connect("dict.org",2628) | 36 | dict = dict or "web1913" |
| 11 | assert(s,e) | 37 | sock:send("DEFINE " .. dict .. " " .. word .. "\r\n") |
| 12 | verbose(">>> connected\n") | 38 | local code, par = get_status(sock, 150) |
| 39 | if code or not par then return nil, code end | ||
| 40 | local defs = "" | ||
| 41 | for i = 1, par do | ||
| 42 | local def | ||
| 43 | code, par = get_status(sock, 151) | ||
| 44 | if code then return nil, code end | ||
| 45 | def, err = get_def(sock) | ||
| 46 | if not def then return nil, err end | ||
| 47 | defs = defs .. def .. "\n" | ||
| 48 | end | ||
| 49 | code, par = get_status(sock, 250) | ||
| 50 | if code then return nil, code end | ||
| 51 | return gsub(defs, "%s%s$", "") | ||
| 52 | end | ||
| 13 | 53 | ||
| 14 | while 1 do | 54 | function dict_close(sock) |
| 15 | local w=read"*w" | 55 | sock:send("QUIT\r\n") |
| 16 | if w==nil then break end | 56 | local code, par = get_status(sock, 221) |
| 17 | if w=="=" then | 57 | sock:close() |
| 18 | w=read"*l" | 58 | return code |
| 19 | verbose(">>>",w,"\n") | ||
| 20 | s:send(w,"\r\n") | ||
| 21 | else | ||
| 22 | verbose(">>> looking up `",w,"'\n") | ||
| 23 | s:send("DEFINE wn ",w,"\r\n") | ||
| 24 | end | ||
| 25 | while 1 do | ||
| 26 | local l=s:receive() | ||
| 27 | if l==nil then break end | ||
| 28 | if strfind(l,"^[0-9]") then | ||
| 29 | write("<<< ",l,"\n") | ||
| 30 | else | ||
| 31 | write(l,"\n") | ||
| 32 | end | ||
| 33 | if strfind(l,"^250") or strfind(l,"^[45]") then break end | ||
| 34 | end | ||
| 35 | end | 59 | end |
| 36 | 60 | ||
| 37 | s:send("QUIT\r\n") | 61 | function dict_get(word, dict) |
| 38 | verbose("<<< ",s:receive(),"\n") | 62 | local sock, err = dict_open() |
| 39 | s:close() | 63 | if not sock then return nil, err end |
| 64 | local defs, err = dict_define(sock, word, dict) | ||
| 65 | dict_close(sock) | ||
| 66 | return defs, err | ||
| 67 | end | ||
