BUG: multi-line replies were not supported.

Error logic simplified.
This commit is contained in:
Diego Nehab 2001-03-27 19:25:11 +00:00
parent 366fb989f3
commit bd0bf45979

View File

@ -27,56 +27,69 @@ end
-- Returns
-- err: message in case of error, nil if successfull
-----------------------------------------------------------------------------
local puts = function(sock, line)
local try_send = function(sock, line)
local err = sock:send(line .. "\r\n")
print(line)
if err then sock:close() end
return err
end
-----------------------------------------------------------------------------
-- Tries to receive DOS mode lines. Closes socket on error.
-- Gets command reply, (accepts multiple-line replies)
-- Input
-- sock: server socket
-- control: control connection socket
-- Returns
-- line: received string if successfull, nil in case of error
-- err: error message if any
-- answer: whole server reply, nil if error
-- code: reply status code or error message
-----------------------------------------------------------------------------
local gets = function(sock)
local line, err = sock:receive("*l")
if err then
sock:close()
return nil, err
local get_answer = function(control)
local code, lastcode, sep
local line, err = control:receive()
local answer = line
if err then return nil, err end
print(line)
_,_, code, sep = strfind(line, "^(%d%d%d)(.)")
if not code or not sep then return nil, answer end
if sep == "-" then -- answer is multiline
repeat
line, err = control:receive()
if err then return nil, err end
print(line)
_,_, lastcode, sep = strfind(line, "^(%d%d%d)(.)")
answer = answer .. "\n" .. line
until code == lastcode and sep == " " -- answer ends with same code
end
return line
return answer, tonumber(code)
end
-----------------------------------------------------------------------------
-- Gets a reply from the server and close connection if it is wrong
-- Checks if a message reply code is correct. Closes control connection
-- if not.
-- Input
-- sock: server socket
-- accept: acceptable errorcodes
-- control: control connection socket
-- success: table with successfull reply status code
-- Returns
-- code: server reply code. nil if error
-- line: complete server reply message or error message
-- code: reply code or nil in case of error
-- answer: complete server answer or system error message
-----------------------------------------------------------------------------
local get_reply = function(sock, accept)
local line, err = %gets(sock)
if line then
if type(accept) ~= "table" then accept = {accept} end
local _,_, code = strfind(line, "^(%d%d%d)")
if not code then return nil, line end
code = tonumber(code)
for i = 1, getn(accept) do
if code == accept[i] then return code, line end
local check_answer = function(control, success)
local answer, code = %get_answer(control)
if not answer then
control:close()
return nil, code
end
sock:close()
return nil, line
if type(success) ~= "table" then success = {success} end
for i = 1, getn(success) do
if code == success[i] then
return code, answer
end
return nil, err
end
control:close()
return nil, answer
end
-----------------------------------------------------------------------------
-- Sends a command to the server
-- Sends a command to the server (closes sock on error)
-- Input
-- sock: server socket
-- command: command to be sent
@ -88,19 +101,7 @@ local send_command = function(sock, command, param)
local line
if param then line = command .. " " .. param
else line = command end
return %puts(sock, line)
end
-----------------------------------------------------------------------------
-- Gets the initial server greeting
-- Input
-- sock: server socket
-- Returns
-- code: server status code, nil if error
-- answer: complete server reply
-----------------------------------------------------------------------------
local get_helo = function(sock)
return %get_reply(sock, 220)
return %try_send(sock, line)
end
-----------------------------------------------------------------------------
@ -108,14 +109,13 @@ end
-- Input
-- sock: server socket
-- Returns
-- code: server status code, nil if error
-- code: server code if ok, nil if error
-- answer: complete server reply
-----------------------------------------------------------------------------
local send_helo = function(sock)
local err = %send_command(sock, "HELO", %DOMAIN)
if not err then
return %get_reply(sock, 250)
else return nil, err end
if err then return nil, err end
return %check_answer(sock, 250)
end
-----------------------------------------------------------------------------
@ -149,16 +149,14 @@ end
-- sock: server socket
-- Returns
-- code: server status code, nil if error
-- answer: complete server reply
-- answer: complete server reply or error message
-----------------------------------------------------------------------------
local send_quit = function(sock)
local code, answer
local err = %send_command(sock, "QUIT")
if not err then
code, answer = %get_reply(sock, 221)
if err then return nil, err end
local code, answer = %check_answer(sock, 221)
sock:close()
return code, answer
else return nil, err end
end
-----------------------------------------------------------------------------
@ -168,14 +166,13 @@ end
-- sender: e-mail of sender
-- Returns
-- code: server status code, nil if error
-- answer: complete server reply
-- answer: complete server reply or error message
-----------------------------------------------------------------------------
local send_mail = function(sock, sender)
local param = format("FROM:<%s>", sender)
local err = %send_command(sock, "MAIL", param)
if not err then
return %get_reply(sock, 250)
else return nil, err end
if err then return nil, err end
return %check_answer(sock, 250)
end
-----------------------------------------------------------------------------
@ -186,12 +183,12 @@ end
-- body: message body
-- Returns
-- code: server status code, nil if error
-- answer: complete server reply
-- answer: complete server reply or error message
-----------------------------------------------------------------------------
local send_data = function (sock, mime, body)
local err = %send_command(sock, "DATA")
if not err then
local code, answer = %get_reply(sock, 354)
if err then return nil, err end
local code, answer = %check_answer(sock, 354)
if not code then return nil, answer end
-- avoid premature end in message body
body = gsub(body or "", "\n%.", "\n%.%.")
@ -199,9 +196,8 @@ local send_data = function (sock, mime, body)
body = body .. "\r\n."
err = %send_mime(sock, mime)
if err then return nil, err end
err = %puts(sock, body)
return %get_reply(sock, 250)
else return nil, err end
err = %try_send(sock, body)
return %check_answer(sock, 250)
end
-----------------------------------------------------------------------------
@ -218,30 +214,13 @@ local send_rcpt = function(sock, rcpt)
if type(rcpt) ~= "table" then rcpt = {rcpt} end
for i = 1, getn(rcpt) do
err = %send_command(sock, "RCPT", format("TO:<%s>", rcpt[i]))
if not err then
code, answer = %get_reply(sock, {250, 251})
if err then return nil, err end
code, answer = %check_answer(sock, {250, 251})
if not code then return code, answer end
else return nil, err end
end
return code, answer
end
-----------------------------------------------------------------------------
-- Sends verify recipient command
-- Input
-- sock: server socket
-- user: user to be verified
-- Returns
-- code: server status code, nil if error
-- answer: complete server reply
-----------------------------------------------------------------------------
local send_vrfy = function (sock, user)
local err = %send_command(sock, "VRFY", format("<%s>", user))
if not err then
return %get_reply(sock, {250, 251})
else return nil, err end
end
-----------------------------------------------------------------------------
-- Connection oriented mail functions
-----------------------------------------------------------------------------
@ -252,7 +231,7 @@ function smtp_connect(server)
if not sock then return nil, err end
sock:timeout(%TIMEOUT)
-- initial server greeting
code, answer = %get_helo(sock)
code, answer = %check_answer(sock, 220)
if not code then return nil, answer end
-- HELO
code, answer = %send_helo(sock)
@ -294,8 +273,8 @@ function smtp_mail(from, rcpt, mime, body, server)
local code, answer = smtp_send(sock, from, rcpt, mime, body)
if not code then return answer end
code, answer = smtp_close(sock)
if not code then return answer
else return nil end
if code then return nil end
return answer
end
--===========================================================================
@ -330,9 +309,7 @@ function mail(msg)
%fill(msg.cc, rcpt)
mime["Cc"] = msg.cc
end
if msg.bcc then
%fill(msg.bcc, rcpt)
end
if msg.bcc then %fill(msg.bcc, rcpt) end
rcpt.n = nil
return %smtp_mail(msg.from, rcpt, mime, msg.message, msg.mailserver)
end