diff options
Diffstat (limited to 'src/tp.lua')
-rw-r--r-- | src/tp.lua | 26 |
1 files changed, 15 insertions, 11 deletions
@@ -2,24 +2,28 @@ | |||
2 | -- Unified SMTP/FTP subsystem | 2 | -- Unified SMTP/FTP subsystem |
3 | -- LuaSocket toolkit. | 3 | -- LuaSocket toolkit. |
4 | -- Author: Diego Nehab | 4 | -- Author: Diego Nehab |
5 | -- Conforming to: RFC 2616, LTN7 | ||
6 | -- RCS ID: $Id$ | 5 | -- RCS ID: $Id$ |
7 | ----------------------------------------------------------------------------- | 6 | ----------------------------------------------------------------------------- |
8 | 7 | ||
9 | ----------------------------------------------------------------------------- | 8 | ----------------------------------------------------------------------------- |
10 | -- Load other required modules | 9 | -- Load required modules |
11 | ----------------------------------------------------------------------------- | 10 | ----------------------------------------------------------------------------- |
12 | local socket = require("socket") | 11 | local socket = require("socket") |
12 | local ltn12 = require("ltn12") | ||
13 | 13 | ||
14 | ----------------------------------------------------------------------------- | 14 | ----------------------------------------------------------------------------- |
15 | -- Setup namespace | 15 | -- Setup namespace |
16 | ----------------------------------------------------------------------------- | 16 | ----------------------------------------------------------------------------- |
17 | tp = {} | 17 | _LOADED["tp"] = getfenv(1) |
18 | setmetatable(tp, { __index = _G }) | ||
19 | setfenv(1, tp) | ||
20 | 18 | ||
19 | ----------------------------------------------------------------------------- | ||
20 | -- Program constants | ||
21 | ----------------------------------------------------------------------------- | ||
21 | TIMEOUT = 60 | 22 | TIMEOUT = 60 |
22 | 23 | ||
24 | ----------------------------------------------------------------------------- | ||
25 | -- Implementation | ||
26 | ----------------------------------------------------------------------------- | ||
23 | -- gets server reply (works for SMTP and FTP) | 27 | -- gets server reply (works for SMTP and FTP) |
24 | local function get_reply(control) | 28 | local function get_reply(control) |
25 | local code, current, sep | 29 | local code, current, sep |
@@ -37,7 +41,6 @@ local function get_reply(control) | |||
37 | -- reply ends with same code | 41 | -- reply ends with same code |
38 | until code == current and sep == " " | 42 | until code == current and sep == " " |
39 | end | 43 | end |
40 | print(reply) | ||
41 | return code, reply | 44 | return code, reply |
42 | end | 45 | end |
43 | 46 | ||
@@ -46,6 +49,7 @@ local metat = { __index = {} } | |||
46 | 49 | ||
47 | function metat.__index:check(ok) | 50 | function metat.__index:check(ok) |
48 | local code, reply = get_reply(self.control) | 51 | local code, reply = get_reply(self.control) |
52 | print(reply) | ||
49 | if not code then return nil, reply end | 53 | if not code then return nil, reply end |
50 | if type(ok) ~= "function" then | 54 | if type(ok) ~= "function" then |
51 | if type(ok) == "table" then | 55 | if type(ok) == "table" then |
@@ -103,11 +107,11 @@ function metat.__index:close() | |||
103 | end | 107 | end |
104 | 108 | ||
105 | -- connect with server and return control object | 109 | -- connect with server and return control object |
106 | function connect(host, port) | 110 | connect = socket.protect(function(host, port, timeout) |
107 | local control, err = socket.connect(host, port) | 111 | local control = socket.try(socket.tcp()) |
108 | if not control then return nil, err end | 112 | socket.try(control:settimeout(timeout or TIMEOUT)) |
109 | control:settimeout(TIMEOUT) | 113 | socket.try(control:connect(host, port)) |
110 | return setmetatable({control = control}, metat) | 114 | return setmetatable({control = control}, metat) |
111 | end | 115 | end) |
112 | 116 | ||
113 | return tp | 117 | return tp |