Updated for LuaSocket 1.4, following LTN7 etc.

Module is now automaticaly tested.
This commit is contained in:
Diego Nehab 2001-09-12 18:16:31 +00:00
parent 504ecdc0aa
commit 480689a702

View File

@ -1,88 +1,55 @@
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
-- Simple SMTP support for the Lua language using the LuaSocket toolkit. -- SMTP support for the Lua language.
-- LuaSocket 1.4 toolkit
-- Author: Diego Nehab -- Author: Diego Nehab
-- Date: 26/12/2000 -- Date: 26/12/2000
-- Conforming to: RFC 821 -- Conforming to: RFC 821, LTN7
-- RCS ID: $Id$
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
local Public, Private = {}, {}
SMTP = Public
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
-- Program constants -- Program constants
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
-- timeout in secconds before we give up waiting -- timeout in secconds before we give up waiting
local TIMEOUT = 180 Public.TIMEOUT = 180
-- port used for connection -- port used for connection
local PORT = 25 Public.PORT = 25
-- domain used in HELO command. If we are under a CGI, try to get from -- domain used in HELO command and default sendmail
-- environment -- If we are under a CGI, try to get from environment
local DOMAIN = getenv("SERVER_NAME") Public.DOMAIN = getenv("SERVER_NAME") or "localhost"
if not DOMAIN then -- default server used to send e-mails
DOMAIN = "localhost" Public.SERVER = "localhost"
end
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
-- Tries to send DOS mode lines. Closes socket on error. -- Tries to send data through socket. Closes socket on error.
-- Input -- Input
-- sock: server socket -- sock: server socket
-- line: string to be sent -- data: string to be sent
-- Returns -- Returns
-- err: message in case of error, nil if successfull -- err: message in case of error, nil if successfull
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
local try_send = function(sock, line) function Private.try_send(sock, data)
local err = sock:send(line .. "\r\n") local err = sock:send(data)
if err then sock:close() end if err then sock:close() end
return err return err
end end
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
-- Gets command reply, (accepts multiple-line replies) -- Tries to get a pattern from the server and closes socket on error
-- Input -- sock: socket opened to the server
-- control: control connection socket -- ...: pattern to receive
-- Returns -- Returns
-- answer: whole server reply, nil if error -- ...: received pattern
-- code: reply status code or error message -- err: error message if any
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
local get_answer = function(control) function Private.try_receive(...)
local code, lastcode, sep local sock = arg[1]
local line, err = control:receive() local data, err = call(sock.receive, arg)
local answer = line if err then sock:close() end
if err then return nil, err end return data, err
_,_, 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
_,_, lastcode, sep = strfind(line, "^(%d%d%d)(.)")
answer = answer .. "\n" .. line
until code == lastcode and sep == " " -- answer ends with same code
end
return answer, tonumber(code)
end
-----------------------------------------------------------------------------
-- Checks if a message reply code is correct. Closes control connection
-- if not.
-- Input
-- control: control connection socket
-- success: table with successfull reply status code
-- Returns
-- code: reply code or nil in case of error
-- answer: complete server answer or system error message
-----------------------------------------------------------------------------
local check_answer = function(control, success)
local answer, code = %get_answer(control)
if not answer then
control:close()
return nil, code
end
if type(success) ~= "table" then success = {success} end
for i = 1, getn(success) do
if code == success[i] then
return code, answer
end
end
control:close()
return nil, answer
end end
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
@ -94,11 +61,60 @@ end
-- Returns -- Returns
-- err: error message if any -- err: error message if any
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
local send_command = function(sock, command, param) function Private.send_command(sock, command, param)
local line local line
if param then line = command .. " " .. param if param then line = command .. " " .. param .. "\r\n"
else line = command end else line = command .. "\r\n" end
return %try_send(sock, line) return %Private.try_send(sock, line)
end
-----------------------------------------------------------------------------
-- Gets command reply, (accepts multiple-line replies)
-- Input
-- control: control openion socket
-- Returns
-- answer: whole server reply, nil if error
-- code: reply status code or error message
-----------------------------------------------------------------------------
function Private.get_answer(control)
local code, lastcode, sep, _
local line, err = %Private.try_receive(control)
local answer = line
if err then return nil, err end
_,_, 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 = %Private.try_receive(control)
if err then return nil, err end
_,_, lastcode, sep = strfind(line, "^(%d%d%d)(.)")
answer = answer .. "\n" .. line
until code == lastcode and sep == " " -- answer ends with same code
end
return answer, tonumber(code)
end
-----------------------------------------------------------------------------
-- Checks if a message reply code is correct. Closes control openion
-- if not.
-- Input
-- control: control openion socket
-- success: table with successfull reply status code
-- Returns
-- code: reply code or nil in case of error
-- answer: complete server answer or system error message
-----------------------------------------------------------------------------
function Private.check_answer(control, success)
local answer, code = %Private.get_answer(control)
if not answer then return nil, code end
if type(success) ~= "table" then success = {success} end
for i = 1, getn(success) do
if code == success[i] then
return code, answer
end
end
control:close()
return nil, answer
end end
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
@ -109,49 +125,24 @@ end
-- code: server code if ok, nil if error -- code: server code if ok, nil if error
-- answer: complete server reply -- answer: complete server reply
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
local send_helo = function(sock) function Private.send_helo(sock)
local err = %send_command(sock, "HELO", %DOMAIN) local err = %Private.send_command(sock, "HELO", %Public.DOMAIN)
if err then return nil, err end if err then return nil, err end
return %check_answer(sock, 250) return %Private.check_answer(sock, 250)
end end
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
-- Sends mime headers -- Sends openion termination command
-- Input
-- sock: server socket
-- mime: table with mime headers to be sent
-- Returns
-- err: error message if any
-----------------------------------------------------------------------------
local send_mime = function(sock, mime)
local err
mime = mime or {}
-- send all headers
for name,value in mime do
err = sock:send(name .. ": " .. value .. "\r\n")
if err then
sock:close()
return err
end
end
-- end mime part
err = sock:send("\r\n")
if err then sock:close() end
return err
end
-----------------------------------------------------------------------------
-- Sends connection termination command
-- Input -- Input
-- 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 or error message -- answer: complete server reply or error message
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
local send_quit = function(sock) function Private.send_quit(sock)
local err = %send_command(sock, "QUIT") local err = %Private.send_command(sock, "QUIT")
if err then return nil, err end if err then return nil, err end
local code, answer = %check_answer(sock, 221) local code, answer = %Private.check_answer(sock, 221)
sock:close() sock:close()
return code, answer return code, answer
end end
@ -165,36 +156,55 @@ end
-- code: server status code, nil if error -- code: server status code, nil if error
-- answer: complete server reply or error message -- answer: complete server reply or error message
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
local send_mail = function(sock, sender) function Private.send_mail(sock, sender)
local param = format("FROM:<%s>", sender) local param = format("FROM:<%s>", sender or "")
local err = %send_command(sock, "MAIL", param) local err = %Private.send_command(sock, "MAIL", param)
if err then return nil, err end if err then return nil, err end
return %check_answer(sock, 250) return %Private.check_answer(sock, 250)
end
-----------------------------------------------------------------------------
-- Sends mime headers
-- Input
-- sock: server socket
-- headers: table with mime headers to be sent
-- Returns
-- err: error message if any
-----------------------------------------------------------------------------
function Private.send_headers(sock, headers)
local err
-- send request headers
for i, v in headers or {} do
err = %Private.try_send(sock, i .. ": " .. v .. "\r\n")
if err then return err end
end
-- mark end of request headers
return %Private.try_send(sock, "\r\n")
end end
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
-- Sends message mime headers and body -- Sends message mime headers and body
-- Input -- Input
-- sock: server socket -- sock: server socket
-- mime: table containing all mime headers to be sent -- headers: table containing all mime headers to be sent
-- 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 or error message -- answer: complete server reply or error message
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
local send_data = function (sock, mime, body) function Private.send_data(sock, headers, body)
local err = %send_command(sock, "DATA") local err = %Private.send_command(sock, "DATA")
if err then return nil, err end if err then return nil, err end
local code, answer = %check_answer(sock, 354) local code, answer = %Private.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%.%.")
-- mark end of message body -- mark end of message body
body = body .. "\r\n." body = body .. "\r\n.\r\n"
err = %send_mime(sock, mime) err = %Private.send_headers(sock, headers)
if err then return nil, err end if err then return nil, err end
err = %try_send(sock, body) err = %Private.try_send(sock, body)
return %check_answer(sock, 250) return %Private.check_answer(sock, 250)
end end
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
@ -206,107 +216,100 @@ end
-- code: server status code, nil if error -- code: server status code, nil if error
-- answer: complete server reply -- answer: complete server reply
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
local send_rcpt = function(sock, rcpt) function Private.send_rcpt(sock, rcpt)
local err, code, answer local err
local code, answer = nil, "No recipient specified"
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 = %Private.send_command(sock, "RCPT", format("TO:<%s>", rcpt[i]))
if err then return nil, err end if err then return nil, err end
code, answer = %check_answer(sock, {250, 251}) code, answer = %Private.check_answer(sock, {250, 251})
if not code then return code, answer end if not code then return code, answer end
end end
return code, answer return code, answer
end end
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
-- Connection oriented mail functions -- Starts the connection and greets server
-- Input
-- parsed: parsed URL components
-- Returns
-- sock: socket connected to server
-- err: error message if any
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
function smtp_connect(server) function Private.open(server)
local code, answer local code, answer
-- connect to server -- default server
local sock, err = connect(server, %PORT) server = server or %Public.SERVER
-- connect to server and make sure we won't hang
local sock, err = connect(server, %Public.PORT)
if not sock then return nil, err end if not sock then return nil, err end
sock:timeout(%TIMEOUT) sock:timeout(%Public.TIMEOUT)
-- initial server greeting -- initial server greeting
code, answer = %check_answer(sock, 220) code, answer = %Private.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 = %Private.send_helo(sock)
if not code then return nil, answer end if not code then return nil, answer end
return sock return sock
end end
function smtp_send(sock, from, rcpt, mime, body) -----------------------------------------------------------------------------
-- Sends a message using an opened server
-- Input
-- sock: socket connected to server
-- message: a table with the following fields:
-- from: message sender's e-mail
-- rcpt: message recipient's e-mail
-- headers: message mime headers
-- body: messge body
-- Returns
-- code: server status code, nil if error
-- answer: complete server reply
-----------------------------------------------------------------------------
function Private.send(sock, message)
local code, answer local code, answer
-- MAIL -- MAIL
code, answer = %send_mail(sock, from) code, answer = %Private.send_mail(sock, message.from)
if not code then return nil, answer end if not code then return nil, answer end
-- RCPT -- RCPT
code, answer = %send_rcpt(sock, rcpt) code, answer = %Private.send_rcpt(sock, message.rcpt)
if not code then return nil, answer end if not code then return nil, answer end
-- DATA -- DATA
return %send_data(sock, mime, body) return %Private.send_data(sock, message.headers, message.body)
end end
function smtp_close(sock) -----------------------------------------------------------------------------
-- Closes connection with server
-- Input
-- sock: socket connected to server
-- Returns
-- code: server status code, nil if error
-- answer: complete server reply
-----------------------------------------------------------------------------
function Private.close(sock)
-- QUIT -- QUIT
return %send_quit(sock) return %Private.send_quit(sock)
end end
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
-- Main mail function -- Main mail function
-- Input -- Input
-- from: message sender -- message: a table with the following fields:
-- rcpt: table containing message recipients -- from: message sender
-- mime: table containing mime headers -- rcpt: table containing message recipients
-- body: message body -- headers: table containing mime headers
-- server: smtp server to be used -- body: message body
-- server: smtp server to be used
-- Returns -- Returns
-- nil if successfull, error message in case of error -- nil if successfull, error message in case of error
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
function smtp_mail(from, rcpt, mime, body, server) function Public.mail(message)
local sock, err = smtp_connect(server) local sock, err = %Private.open(message.server)
if not sock then return err end if not sock then return err end
local code, answer = smtp_send(sock, from, rcpt, mime, body) local code, answer = %Private.send(sock, message)
if not code then return answer end if not code then return answer end
code, answer = smtp_close(sock) code, answer = %Private.close(sock)
if code then return nil end if code then return nil end
return answer return answer
end end
--===========================================================================
-- Compatibility functions
--===========================================================================
-----------------------------------------------------------------------------
-- Converts a comma separated list into a Lua table with one entry for each
-- list element.
-- Input
-- str: string containing the list to be converted
-- tab: table to be filled with entries
-- Returns
-- a table t, where t.n is the number of elements with an entry t[i]
-- for each element
-----------------------------------------------------------------------------
local fill = function(str, tab)
gsub(str, "([^%s,]+)", function (w) tinsert(%tab, w) end)
return tab
end
-----------------------------------------------------------------------------
-- Client mail function, implementing CGILUA 3.2 interface
-----------------------------------------------------------------------------
function mail(msg)
local rcpt = {}
local mime = {}
mime["Subject"] = msg.subject
mime["To"] = msg.to
mime["From"] = msg.from
%fill(msg.to, rcpt)
if msg.cc then
%fill(msg.cc, rcpt)
mime["Cc"] = msg.cc
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