mirror of
https://github.com/lunarmodules/luasocket.git
synced 2024-12-26 20:38:22 +01:00
BUG: multi-line replies were not supported.
Error logic simplified.
This commit is contained in:
parent
366fb989f3
commit
bd0bf45979
149
src/smtp.lua
149
src/smtp.lua
@ -27,56 +27,69 @@ end
|
|||||||
-- Returns
|
-- Returns
|
||||||
-- err: message in case of error, nil if successfull
|
-- 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")
|
local err = sock:send(line .. "\r\n")
|
||||||
|
print(line)
|
||||||
if err then sock:close() end
|
if err then sock:close() end
|
||||||
return err
|
return err
|
||||||
end
|
end
|
||||||
|
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
-- Tries to receive DOS mode lines. Closes socket on error.
|
-- Gets command reply, (accepts multiple-line replies)
|
||||||
-- Input
|
-- Input
|
||||||
-- sock: server socket
|
-- control: control connection socket
|
||||||
-- Returns
|
-- Returns
|
||||||
-- line: received string if successfull, nil in case of error
|
-- answer: whole server reply, nil if error
|
||||||
-- err: error message if any
|
-- code: reply status code or error message
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
local gets = function(sock)
|
local get_answer = function(control)
|
||||||
local line, err = sock:receive("*l")
|
local code, lastcode, sep
|
||||||
if err then
|
local line, err = control:receive()
|
||||||
sock:close()
|
local answer = line
|
||||||
return nil, err
|
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
|
end
|
||||||
return line
|
return answer, tonumber(code)
|
||||||
end
|
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
|
-- Input
|
||||||
-- sock: server socket
|
-- control: control connection socket
|
||||||
-- accept: acceptable errorcodes
|
-- success: table with successfull reply status code
|
||||||
-- Returns
|
-- Returns
|
||||||
-- code: server reply code. nil if error
|
-- code: reply code or nil in case of error
|
||||||
-- line: complete server reply message or error message
|
-- answer: complete server answer or system error message
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
local get_reply = function(sock, accept)
|
local check_answer = function(control, success)
|
||||||
local line, err = %gets(sock)
|
local answer, code = %get_answer(control)
|
||||||
if line then
|
if not answer then
|
||||||
if type(accept) ~= "table" then accept = {accept} end
|
control:close()
|
||||||
local _,_, code = strfind(line, "^(%d%d%d)")
|
return nil, code
|
||||||
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
|
|
||||||
end
|
end
|
||||||
sock:close()
|
if type(success) ~= "table" then success = {success} end
|
||||||
return nil, line
|
for i = 1, getn(success) do
|
||||||
|
if code == success[i] then
|
||||||
|
return code, answer
|
||||||
end
|
end
|
||||||
return nil, err
|
end
|
||||||
|
control:close()
|
||||||
|
return nil, answer
|
||||||
end
|
end
|
||||||
|
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
-- Sends a command to the server
|
-- Sends a command to the server (closes sock on error)
|
||||||
-- Input
|
-- Input
|
||||||
-- sock: server socket
|
-- sock: server socket
|
||||||
-- command: command to be sent
|
-- command: command to be sent
|
||||||
@ -88,19 +101,7 @@ local send_command = function(sock, command, param)
|
|||||||
local line
|
local line
|
||||||
if param then line = command .. " " .. param
|
if param then line = command .. " " .. param
|
||||||
else line = command end
|
else line = command end
|
||||||
return %puts(sock, line)
|
return %try_send(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)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
@ -108,14 +109,13 @@ end
|
|||||||
-- Input
|
-- Input
|
||||||
-- sock: server socket
|
-- sock: server socket
|
||||||
-- Returns
|
-- Returns
|
||||||
-- code: server status code, nil if error
|
-- code: server code if ok, nil if error
|
||||||
-- answer: complete server reply
|
-- answer: complete server reply
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
local send_helo = function(sock)
|
local send_helo = function(sock)
|
||||||
local err = %send_command(sock, "HELO", %DOMAIN)
|
local err = %send_command(sock, "HELO", %DOMAIN)
|
||||||
if not err then
|
if err then return nil, err end
|
||||||
return %get_reply(sock, 250)
|
return %check_answer(sock, 250)
|
||||||
else return nil, err end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
@ -149,16 +149,14 @@ end
|
|||||||
-- sock: server socket
|
-- sock: server socket
|
||||||
-- Returns
|
-- Returns
|
||||||
-- code: server status code, nil if error
|
-- code: server status code, nil if error
|
||||||
-- answer: complete server reply
|
-- answer: complete server reply or error message
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
local send_quit = function(sock)
|
local send_quit = function(sock)
|
||||||
local code, answer
|
|
||||||
local err = %send_command(sock, "QUIT")
|
local err = %send_command(sock, "QUIT")
|
||||||
if not err then
|
if err then return nil, err end
|
||||||
code, answer = %get_reply(sock, 221)
|
local code, answer = %check_answer(sock, 221)
|
||||||
sock:close()
|
sock:close()
|
||||||
return code, answer
|
return code, answer
|
||||||
else return nil, err end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
@ -168,14 +166,13 @@ end
|
|||||||
-- sender: e-mail of sender
|
-- sender: e-mail of sender
|
||||||
-- Returns
|
-- Returns
|
||||||
-- code: server status code, nil if error
|
-- 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 send_mail = function(sock, sender)
|
||||||
local param = format("FROM:<%s>", sender)
|
local param = format("FROM:<%s>", sender)
|
||||||
local err = %send_command(sock, "MAIL", param)
|
local err = %send_command(sock, "MAIL", param)
|
||||||
if not err then
|
if err then return nil, err end
|
||||||
return %get_reply(sock, 250)
|
return %check_answer(sock, 250)
|
||||||
else return nil, err end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
@ -186,12 +183,12 @@ end
|
|||||||
-- body: message body
|
-- body: message body
|
||||||
-- Returns
|
-- Returns
|
||||||
-- code: server status code, nil if error
|
-- 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 send_data = function (sock, mime, body)
|
||||||
local err = %send_command(sock, "DATA")
|
local err = %send_command(sock, "DATA")
|
||||||
if not err then
|
if err then return nil, err end
|
||||||
local code, answer = %get_reply(sock, 354)
|
local code, answer = %check_answer(sock, 354)
|
||||||
if not code then return nil, answer end
|
if not code then return nil, answer end
|
||||||
-- avoid premature end in message body
|
-- avoid premature end in message body
|
||||||
body = gsub(body or "", "\n%.", "\n%.%.")
|
body = gsub(body or "", "\n%.", "\n%.%.")
|
||||||
@ -199,9 +196,8 @@ local send_data = function (sock, mime, body)
|
|||||||
body = body .. "\r\n."
|
body = body .. "\r\n."
|
||||||
err = %send_mime(sock, mime)
|
err = %send_mime(sock, mime)
|
||||||
if err then return nil, err end
|
if err then return nil, err end
|
||||||
err = %puts(sock, body)
|
err = %try_send(sock, body)
|
||||||
return %get_reply(sock, 250)
|
return %check_answer(sock, 250)
|
||||||
else return nil, err end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
@ -218,30 +214,13 @@ local send_rcpt = function(sock, rcpt)
|
|||||||
if type(rcpt) ~= "table" then rcpt = {rcpt} end
|
if type(rcpt) ~= "table" then rcpt = {rcpt} end
|
||||||
for i = 1, getn(rcpt) do
|
for i = 1, getn(rcpt) do
|
||||||
err = %send_command(sock, "RCPT", format("TO:<%s>", rcpt[i]))
|
err = %send_command(sock, "RCPT", format("TO:<%s>", rcpt[i]))
|
||||||
if not err then
|
if err then return nil, err end
|
||||||
code, answer = %get_reply(sock, {250, 251})
|
code, answer = %check_answer(sock, {250, 251})
|
||||||
if not code then return code, answer end
|
if not code then return code, answer end
|
||||||
else return nil, err end
|
|
||||||
end
|
end
|
||||||
return code, answer
|
return code, answer
|
||||||
end
|
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
|
-- Connection oriented mail functions
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
@ -252,7 +231,7 @@ function smtp_connect(server)
|
|||||||
if not sock then return nil, err end
|
if not sock then return nil, err end
|
||||||
sock:timeout(%TIMEOUT)
|
sock:timeout(%TIMEOUT)
|
||||||
-- initial server greeting
|
-- initial server greeting
|
||||||
code, answer = %get_helo(sock)
|
code, answer = %check_answer(sock, 220)
|
||||||
if not code then return nil, answer end
|
if not code then return nil, answer end
|
||||||
-- HELO
|
-- HELO
|
||||||
code, answer = %send_helo(sock)
|
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)
|
local code, answer = smtp_send(sock, from, rcpt, mime, body)
|
||||||
if not code then return answer end
|
if not code then return answer end
|
||||||
code, answer = smtp_close(sock)
|
code, answer = smtp_close(sock)
|
||||||
if not code then return answer
|
if code then return nil end
|
||||||
else return nil end
|
return answer
|
||||||
end
|
end
|
||||||
|
|
||||||
--===========================================================================
|
--===========================================================================
|
||||||
@ -330,9 +309,7 @@ function mail(msg)
|
|||||||
%fill(msg.cc, rcpt)
|
%fill(msg.cc, rcpt)
|
||||||
mime["Cc"] = msg.cc
|
mime["Cc"] = msg.cc
|
||||||
end
|
end
|
||||||
if msg.bcc then
|
if msg.bcc then %fill(msg.bcc, rcpt) end
|
||||||
%fill(msg.bcc, rcpt)
|
|
||||||
end
|
|
||||||
rcpt.n = nil
|
rcpt.n = nil
|
||||||
return %smtp_mail(msg.from, rcpt, mime, msg.message, msg.mailserver)
|
return %smtp_mail(msg.from, rcpt, mime, msg.message, msg.mailserver)
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user