diff options
author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-06-17 21:46:22 +0000 |
---|---|---|
committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-06-17 21:46:22 +0000 |
commit | 597a062b1bc38488df1363995eece8a9f19c7111 (patch) | |
tree | ba4ccaa8be98d1100aed63abb9c42c099f2c41bb /etc | |
parent | eac26d2c8dafab604ef2e97e504326cfaf1033cf (diff) | |
download | luasocket-597a062b1bc38488df1363995eece8a9f19c7111.tar.gz luasocket-597a062b1bc38488df1363995eece8a9f19c7111.tar.bz2 luasocket-597a062b1bc38488df1363995eece8a9f19c7111.zip |
Almost ready for distribution...
Diffstat (limited to 'etc')
-rw-r--r-- | etc/README | 30 | ||||
-rw-r--r-- | etc/dict.lua | 182 | ||||
-rw-r--r-- | etc/qp.lua | 10 |
3 files changed, 151 insertions, 71 deletions
@@ -4,34 +4,42 @@ This directory contains code that is more useful than the examples. This code | |||
4 | lua.lua | 4 | lua.lua |
5 | 5 | ||
6 | These are modules to suport dynamic loading of LuaSocket by the stand alone | 6 | These are modules to suport dynamic loading of LuaSocket by the stand alone |
7 | Lua Interpreter with the use of the "require" function. For my Mac OS X | 7 | Lua Interpreter with the use of new "require" and "requirelib" functions. |
8 | system, I place all files in /Users/diego/tec/luasocket | 8 | For my Mac OS X box, for instance, I place all files in |
9 | and set the following environment variables: | 9 | /Users/diego/tec/luasocket and set the following environment variables: |
10 | 10 | ||
11 | LUA_PATH=/Users/diego/tec/luasocket/?.lua | ||
12 | LUA_INIT=@/Users/diego/tec/luasocket/lua.lua | 11 | LUA_INIT=@/Users/diego/tec/luasocket/lua.lua |
13 | LUA_FUNCNAME=? | 12 | LUA_PATH=/Users/diego/tec/luasocket/?.lua;?.lua |
14 | LUA_LIBNAME=/Users/diego/tec/luasocket/?.dylib | 13 | LUA_PATHLIB=/Users/diego/tec/luasocket/?.dylib;?.dylib |
15 | 14 | ||
16 | With that, I can run any luasocket application with the command line: | 15 | With that, I can run any luasocket application with the command line: |
17 | 16 | ||
18 | lua -l socket <script> | 17 | lua <script> |
19 | 18 | ||
19 | as long as the script uses "require" to load the needed namespaces. | ||
20 | Much nicer than having to build a new executable just to initialize | 20 | Much nicer than having to build a new executable just to initialize |
21 | LuaSocket! | 21 | LuaSocket! |
22 | 22 | ||
23 | |||
24 | tftp.lua -- Trivial FTP client | 23 | tftp.lua -- Trivial FTP client |
25 | 24 | ||
26 | This module implements file retrieval by the TFTP protocol. Its main use | 25 | This module implements file retrieval by the TFTP protocol. Its main use |
27 | is to test the UDP code, but someone might find it usefull. | 26 | was to test the UDP code, but since someone found it usefull, I turned it |
27 | into a module that is almost official (no uploads, yet). | ||
28 | |||
29 | dict.lua -- Dict client | ||
30 | |||
31 | The dict.lua module started with a cool simple client for the DICT | ||
32 | protocol, written by Luiz Henrique Figueiredo. This new version has been | ||
33 | converted into a library, similar to the HTTP and FTP libraries, that can | ||
34 | be used from within any luasocket application. Take a look on the source | ||
35 | code and you will be able to figure out how to use it. | ||
28 | 36 | ||
29 | get.lua -- file retriever | 37 | get.lua -- file retriever |
30 | 38 | ||
31 | This little program is a client that uses the FTP and HTTP code to | 39 | This little program is a client that uses the FTP and HTTP code to |
32 | implement a command line file graber. Just run | 40 | implement a command line file graber. Just run |
33 | 41 | ||
34 | lua -l socket get.lua <remote-file> [<local-file>] | 42 | lua get.lua <remote-file> [<local-file>] |
35 | 43 | ||
36 | to download a remote file (either ftp:// or http://) to the specified | 44 | to download a remote file (either ftp:// or http://) to the specified |
37 | local file. The program also prints the download throughput, elapsed | 45 | local file. The program also prints the download throughput, elapsed |
@@ -44,7 +52,7 @@ similar to check-links.pl by Jamie Zawinski, but uses all facilities of | |||
44 | the LuaSocket library and the Lua language. It has not been thoroughly | 52 | the LuaSocket library and the Lua language. It has not been thoroughly |
45 | tested, but it should work. Just run | 53 | tested, but it should work. Just run |
46 | 54 | ||
47 | lua -l socket check-links.lua {<url>} > output | 55 | lua check-links.lua {<url>} > output |
48 | 56 | ||
49 | and open the result to see a list of broken links. | 57 | and open the result to see a list of broken links. |
50 | 58 | ||
diff --git a/etc/dict.lua b/etc/dict.lua index dd001cf..b9247dc 100644 --- a/etc/dict.lua +++ b/etc/dict.lua | |||
@@ -4,78 +4,144 @@ | |||
4 | -- Author: Diego Nehab | 4 | -- Author: Diego Nehab |
5 | -- RCS ID: $Id$ | 5 | -- RCS ID: $Id$ |
6 | ----------------------------------------------------------------------------- | 6 | ----------------------------------------------------------------------------- |
7 | |||
8 | ----------------------------------------------------------------------------- | ||
9 | -- Load required modules | ||
10 | ----------------------------------------------------------------------------- | ||
7 | local socket = require("socket") | 11 | local socket = require("socket") |
12 | local url = require("url") | ||
13 | local tp = require("tp") | ||
8 | 14 | ||
9 | function get_status(sock, valid) | 15 | ----------------------------------------------------------------------------- |
10 | local line, err = sock:receive() | 16 | -- Globals |
11 | local code, par | 17 | ----------------------------------------------------------------------------- |
12 | if not line then sock:close() return err end | 18 | HOST = "dict.org" |
13 | code = socket.skip(2, string.find(line, "^(%d%d%d)")) | 19 | PORT = 2628 |
14 | code = tonumber(code) | 20 | TIMEOUT = 10 |
15 | if code ~= valid then return code end | 21 | |
16 | if code == 150 then | 22 | ----------------------------------------------------------------------------- |
17 | par = tonumber(socket.skip(2, string.find(line, "^%d%d%d (%d*)"))) | 23 | -- Low-level dict API |
18 | end | 24 | ----------------------------------------------------------------------------- |
19 | return nil, par | 25 | local metat = { __index = {} } |
26 | |||
27 | function open(host, port) | ||
28 | local tp = socket.try(tp.connect(host or HOST, port or PORT, TIMEOUT)) | ||
29 | return setmetatable({tp = tp}, metat) | ||
20 | end | 30 | end |
21 | 31 | ||
22 | function get_def(sock) | 32 | function metat.__index:greet() |
23 | local line, err = sock:receive() | 33 | return socket.try(self.tp:check(220)) |
24 | local def = "" | 34 | end |
25 | while (not err) and line ~= "." do | 35 | |
26 | def = def .. line .. "\n" | 36 | function metat.__index:check(ok) |
27 | line, err = sock:receive() | 37 | local code, status = socket.try(self.tp:check(ok)) |
38 | return code, tonumber(socket.skip(2, string.find(status, "^%d%d%d (%d*)"))) | ||
39 | end | ||
40 | |||
41 | function metat.__index:getdef() | ||
42 | local line = socket.try(self.tp:receive()) | ||
43 | local def = {} | ||
44 | while line ~= "." do | ||
45 | table.insert(def, line) | ||
46 | line = socket.try(self.tp:receive()) | ||
28 | end | 47 | end |
29 | if err then sock:close() return nil, err | 48 | return table.concat(def, "\n") |
30 | else return def end | ||
31 | end | 49 | end |
32 | 50 | ||
33 | function dict_open() | 51 | function metat.__index:define(database, word) |
34 | local sock, err = socket.connect("dict.org", 2628) | 52 | database = database or "!" |
35 | if not sock then return nil, err end | 53 | socket.try(self.tp:command("DEFINE", database .. " " .. word)) |
36 | sock:settimeout(10) | 54 | local code, count = self:check(150) |
37 | local code, par = get_status(sock, 220) | 55 | local defs = {} |
38 | if code then return nil, code end | 56 | for i = 1, count do |
39 | return sock | 57 | self:check(151) |
58 | table.insert(defs, self:getdef()) | ||
59 | end | ||
60 | self:check(250) | ||
61 | return defs | ||
40 | end | 62 | end |
41 | 63 | ||
42 | function dict_define(sock, word, dict) | 64 | function metat.__index:match(database, strat, word) |
43 | dict = dict or "web1913" | 65 | database = database or "!" |
44 | sock:send("DEFINE " .. dict .. " " .. word .. "\r\n") | 66 | strat = strat or "." |
45 | local code, par = get_status(sock, 150) | 67 | socket.try(self.tp:command("MATCH", database .." ".. strat .." ".. word)) |
46 | if code or not par then return nil, code end | 68 | self:check(152) |
47 | local defs = "" | 69 | local mat = {} |
48 | for i = 1, par do | 70 | local line = socket.try(self.tp:receive()) |
49 | local def | 71 | while line ~= '.' do |
50 | code, par = get_status(sock, 151) | 72 | database, word = socket.skip(2, string.find(line, "(%S+) (.*)")) |
51 | if code then return nil, code end | 73 | if not mat[database] then mat[database] = {} end |
52 | def, err = get_def(sock) | 74 | table.insert(mat[database], word) |
53 | if not def then return nil, err end | 75 | line = socket.try(self.tp:receive()) |
54 | defs = defs .. def .. "\n" | ||
55 | end | 76 | end |
56 | code, par = get_status(sock, 250) | 77 | self:check(250) |
57 | if code then return nil, code end | 78 | return mat |
58 | return string.gsub(defs, "%s%s$", "") | 79 | end |
80 | |||
81 | function metat.__index:quit() | ||
82 | self.tp:command("QUIT") | ||
83 | return self:check(221) | ||
84 | end | ||
85 | |||
86 | function metat.__index:close() | ||
87 | return self.tp:close() | ||
59 | end | 88 | end |
60 | 89 | ||
61 | function dict_close(sock) | 90 | ----------------------------------------------------------------------------- |
62 | sock:send("QUIT\r\n") | 91 | -- High-level dict API |
63 | local code, par = get_status(sock, 221) | 92 | ----------------------------------------------------------------------------- |
64 | sock:close() | 93 | local default = { |
65 | return code | 94 | scheme = "dict", |
95 | host = "dict.org" | ||
96 | } | ||
97 | |||
98 | local function there(f) | ||
99 | if f == "" then return nil | ||
100 | else return f end | ||
66 | end | 101 | end |
67 | 102 | ||
68 | function dict_get(word, dict) | 103 | local function parse(u) |
69 | local sock, err = dict_open() | 104 | local t = socket.try(url.parse(u, default)) |
70 | if not sock then return nil, err end | 105 | socket.try(t.scheme == "dict", "invalid scheme '" .. t.scheme .. "'") |
71 | local defs, err = dict_define(sock, word, dict) | 106 | socket.try(t.path, "invalid path in url") |
72 | dict_close(sock) | 107 | local cmd, arg = socket.skip(2, string.find(t.path, "^/(.)(.*)$")) |
73 | return defs, err | 108 | socket.try(cmd == "d" or cmd == "m", "<command> should be 'm' or 'd'") |
109 | socket.try(arg and arg ~= "", "need at least <word> in URL") | ||
110 | t.command, t.argument = cmd, arg | ||
111 | arg = string.gsub(arg, "^:([^:]+)", function(f) t.word = f end) | ||
112 | socket.try(t.word, "need at least <word> in URL") | ||
113 | arg = string.gsub(arg, "^:([^:]*)", function(f) t.database = there(f) end) | ||
114 | if cmd == "m" then | ||
115 | arg = string.gsub(arg, "^:([^:]*)", function(f) t.strat = there(f) end) | ||
116 | end | ||
117 | string.gsub(arg, ":([^:]*)$", function(f) t.n = tonumber(f) end) | ||
118 | return t | ||
74 | end | 119 | end |
75 | 120 | ||
76 | if arg and arg[1] then | 121 | local function tget(gett) |
77 | defs, err = dict_get(arg[1], arg[2]) | 122 | local con = open(gett.host, gett.port) |
78 | print(defs or err) | 123 | con:greet() |
79 | else | 124 | if gett.command == "d" then |
80 | io.write("Usage:\n lua dict.lua <word> [<dictionary>]\n") | 125 | local def = con:define(gett.database, gett.word) |
126 | con:quit() | ||
127 | con:close() | ||
128 | if gett.n then return def[gett.n] | ||
129 | else return def end | ||
130 | elseif gett.command == "m" then | ||
131 | local mat = con:match(gett.database, gett.strat, gett.word) | ||
132 | con:quit() | ||
133 | con:close() | ||
134 | return mat | ||
135 | else return nil, "invalid command" end | ||
81 | end | 136 | end |
137 | |||
138 | local function sget(u) | ||
139 | local gett = parse(u) | ||
140 | return tget(gett) | ||
141 | end | ||
142 | |||
143 | --function socket.protect(f) return f end | ||
144 | get = socket.protect(function(gett) | ||
145 | if type(gett) == "string" then return sget(gett) | ||
146 | else return tget(gett) end | ||
147 | end) | ||
@@ -1,5 +1,11 @@ | |||
1 | require("ltn12") | 1 | ----------------------------------------------------------------------------- |
2 | require("mime") | 2 | -- Little program to convert to and from Quoted-Printable |
3 | -- LuaSocket sample files | ||
4 | -- Author: Diego Nehab | ||
5 | -- RCS ID: $Id$ | ||
6 | ----------------------------------------------------------------------------- | ||
7 | local ltn12 = require("ltn12") | ||
8 | local mime = require("mime") | ||
3 | local convert | 9 | local convert |
4 | arg = arg or {} | 10 | arg = arg or {} |
5 | local mode = arg and arg[1] or "-et" | 11 | local mode = arg and arg[1] or "-et" |