diff options
Diffstat (limited to 'src/smtp.lua')
-rw-r--r-- | src/smtp.lua | 22 |
1 files changed, 12 insertions, 10 deletions
diff --git a/src/smtp.lua b/src/smtp.lua index 8e672a0..1d1905e 100644 --- a/src/smtp.lua +++ b/src/smtp.lua | |||
@@ -1,8 +1,16 @@ | |||
1 | ----------------------------------------------------------------------------- | ||
2 | -- SMTP client support for the Lua language. | ||
3 | -- LuaSocket toolkit. | ||
4 | -- Author: Diego Nehab | ||
5 | -- Conforming to RFC 2821 | ||
6 | -- RCS ID: $Id$ | ||
7 | ----------------------------------------------------------------------------- | ||
1 | -- make sure LuaSocket is loaded | 8 | -- make sure LuaSocket is loaded |
2 | if not LUASOCKET_LIBNAME then error('module requires LuaSocket') end | 9 | if not LUASOCKET_LIBNAME then error('module requires LuaSocket') end |
3 | -- get LuaSocket namespace | 10 | -- get LuaSocket namespace |
4 | local socket = _G[LUASOCKET_LIBNAME] | 11 | local socket = _G[LUASOCKET_LIBNAME] |
5 | if not socket then error('module requires LuaSocket') end | 12 | if not socket then error('module requires LuaSocket') end |
13 | |||
6 | -- create smtp namespace inside LuaSocket namespace | 14 | -- create smtp namespace inside LuaSocket namespace |
7 | local smtp = socket.smtp or {} | 15 | local smtp = socket.smtp or {} |
8 | socket.smtp = smtp | 16 | socket.smtp = smtp |
@@ -20,11 +28,6 @@ DOMAIN = os.getenv("SERVER_NAME") or "localhost" | |||
20 | -- default time zone (means we don't know) | 28 | -- default time zone (means we don't know) |
21 | ZONE = "-0000" | 29 | ZONE = "-0000" |
22 | 30 | ||
23 | |||
24 | local function shift(a, b, c) | ||
25 | return b, c | ||
26 | end | ||
27 | |||
28 | -- high level stuffing filter | 31 | -- high level stuffing filter |
29 | function stuff() | 32 | function stuff() |
30 | return ltn12.filter.cycle(dot, 2) | 33 | return ltn12.filter.cycle(dot, 2) |
@@ -82,6 +85,7 @@ function metat.__index:send(mailt) | |||
82 | end | 85 | end |
83 | 86 | ||
84 | function open(server, port) | 87 | function open(server, port) |
88 | print(server or SERVER, port or PORT) | ||
85 | local tp, error = socket.tp.connect(server or SERVER, port or PORT) | 89 | local tp, error = socket.tp.connect(server or SERVER, port or PORT) |
86 | if not tp then return nil, error end | 90 | if not tp then return nil, error end |
87 | return setmetatable({tp = tp}, metat) | 91 | return setmetatable({tp = tp}, metat) |
@@ -180,17 +184,15 @@ function message(mesgt) | |||
180 | adjust_headers(mesgt) | 184 | adjust_headers(mesgt) |
181 | -- create and return message source | 185 | -- create and return message source |
182 | local co = coroutine.create(function() send_message(mesgt) end) | 186 | local co = coroutine.create(function() send_message(mesgt) end) |
183 | return function() return shift(coroutine.resume(co)) end | 187 | return function() return socket.skip(1, coroutine.resume(co)) end |
184 | end | 188 | end |
185 | 189 | ||
186 | --------------------------------------------------------------------------- | 190 | --------------------------------------------------------------------------- |
187 | -- High level SMTP API | 191 | -- High level SMTP API |
188 | ----------------------------------------------------------------------------- | 192 | ----------------------------------------------------------------------------- |
189 | send = socket.protect(function(mailt) | 193 | send = socket.protect(function(mailt) |
190 | local server = mailt.server or SERVER | 194 | local smtp = socket.try(open(mailt.server, mailt.port)) |
191 | local port = mailt.port or PORT | 195 | smtp:greet(mailt.domain) |
192 | local smtp = socket.try(open(server, port)) | ||
193 | smtp:greet(mailt.domain or DOMAIN) | ||
194 | smtp:send(mailt) | 196 | smtp:send(mailt) |
195 | smtp:quit() | 197 | smtp:quit() |
196 | return smtp:close() | 198 | return smtp:close() |