aboutsummaryrefslogtreecommitdiff
path: root/src/tp.lua
diff options
context:
space:
mode:
Diffstat (limited to 'src/tp.lua')
-rw-r--r--src/tp.lua26
1 files changed, 15 insertions, 11 deletions
diff --git a/src/tp.lua b/src/tp.lua
index 3e9dba6..56dd8bc 100644
--- a/src/tp.lua
+++ b/src/tp.lua
@@ -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-----------------------------------------------------------------------------
12local socket = require("socket") 11local socket = require("socket")
12local ltn12 = require("ltn12")
13 13
14----------------------------------------------------------------------------- 14-----------------------------------------------------------------------------
15-- Setup namespace 15-- Setup namespace
16----------------------------------------------------------------------------- 16-----------------------------------------------------------------------------
17tp = {} 17_LOADED["tp"] = getfenv(1)
18setmetatable(tp, { __index = _G })
19setfenv(1, tp)
20 18
19-----------------------------------------------------------------------------
20-- Program constants
21-----------------------------------------------------------------------------
21TIMEOUT = 60 22TIMEOUT = 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)
24local function get_reply(control) 28local 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
40print(reply)
41 return code, reply 44 return code, reply
42end 45end
43 46
@@ -46,6 +49,7 @@ local metat = { __index = {} }
46 49
47function metat.__index:check(ok) 50function metat.__index:check(ok)
48 local code, reply = get_reply(self.control) 51 local code, reply = get_reply(self.control)
52print(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()
103end 107end
104 108
105-- connect with server and return control object 109-- connect with server and return control object
106function connect(host, port) 110connect = 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)
111end 115end)
112 116
113return tp 117return tp