luasocket/src/tp.lua

109 lines
3.3 KiB
Lua
Raw Normal View History

-----------------------------------------------------------------------------
-- Unified SMTP/FTP subsystem
-- LuaSocket toolkit.
-- Author: Diego Nehab
-- RCS ID: $Id$
-----------------------------------------------------------------------------
2004-05-30 23:36:22 +02:00
2004-06-04 17:15:45 +02:00
-----------------------------------------------------------------------------
-- Load required modules
2004-06-04 17:15:45 +02:00
-----------------------------------------------------------------------------
local socket = require("socket")
local ltn12 = require("ltn12")
2004-06-04 17:15:45 +02:00
-----------------------------------------------------------------------------
-- Program constants
-----------------------------------------------------------------------------
TIMEOUT = 60
-----------------------------------------------------------------------------
-- Implementation
-----------------------------------------------------------------------------
-- gets server reply (works for SMTP and FTP)
local function get_reply(control)
2004-05-28 08:16:43 +02:00
local code, current, sep
local line, err = control:receive()
local reply = line
if err then return nil, err end
2004-05-28 08:16:43 +02:00
code, sep = socket.skip(2, string.find(line, "^(%d%d%d)(.?)"))
if not code then return nil, "invalid server reply" end
2004-05-28 08:16:43 +02:00
if sep == "-" then -- reply is multiline
repeat
line, err = control:receive()
if err then return nil, err end
2004-05-28 08:16:43 +02:00
current, sep = socket.skip(2, string.find(line, "^(%d%d%d)(.?)"))
reply = reply .. "\n" .. line
-- reply ends with same code
2004-05-28 08:16:43 +02:00
until code == current and sep == " "
end
return code, reply
end
-- metatable for sock object
local metat = { __index = {} }
function metat.__index:check(ok)
local code, reply = get_reply(self.control)
if not code then return nil, reply end
if type(ok) ~= "function" then
if type(ok) == "table" then
for i, v in ipairs(ok) do
if string.find(code, v) then return tonumber(code), reply end
end
return nil, reply
else
if string.find(code, ok) then return tonumber(code), reply
else return nil, reply end
end
else return ok(tonumber(code), reply) end
end
function metat.__index:command(cmd, arg)
if arg then return self.control:send(cmd .. " " .. arg.. "\r\n")
else return self.control:send(cmd .. "\r\n") end
end
function metat.__index:sink(snk, pat)
local chunk, err = control:receive(pat)
return snk(chunk, err)
end
function metat.__index:send(data)
return self.control:send(data)
end
function metat.__index:receive(pat)
return self.control:receive(pat)
end
function metat.__index:getfd()
return self.control:getfd()
end
function metat.__index:dirty()
return self.control:dirty()
end
function metat.__index:getcontrol()
return self.control
end
2004-05-26 06:58:32 +02:00
function metat.__index:source(source, step)
local sink = socket.sink("keep-open", self.control)
return ltn12.pump.all(source, sink, step or ltn12.pump.step)
end
-- closes the underlying control
function metat.__index:close()
self.control:close()
return 1
end
-- connect with server and return control object
connect = socket.protect(function(host, port, timeout)
local control = socket.try(socket.tcp())
socket.try(control:settimeout(timeout or TIMEOUT))
socket.try(control:connect(host, port))
return setmetatable({control = control}, metat)
end)