diff options
Diffstat (limited to 'src/smtp.lua')
-rw-r--r-- | src/smtp.lua | 32 |
1 files changed, 18 insertions, 14 deletions
diff --git a/src/smtp.lua b/src/smtp.lua index 896b536..b113d00 100644 --- a/src/smtp.lua +++ b/src/smtp.lua | |||
@@ -17,22 +17,24 @@ local tp = require("socket.tp") | |||
17 | local ltn12 = require("ltn12") | 17 | local ltn12 = require("ltn12") |
18 | local headers = require("socket.headers") | 18 | local headers = require("socket.headers") |
19 | local mime = require("mime") | 19 | local mime = require("mime") |
20 | module("socket.smtp") | 20 | |
21 | socket.smtp = {} | ||
22 | local _M = socket.smtp | ||
21 | 23 | ||
22 | ----------------------------------------------------------------------------- | 24 | ----------------------------------------------------------------------------- |
23 | -- Program constants | 25 | -- Program constants |
24 | ----------------------------------------------------------------------------- | 26 | ----------------------------------------------------------------------------- |
25 | -- timeout for connection | 27 | -- timeout for connection |
26 | TIMEOUT = 60 | 28 | _M.TIMEOUT = 60 |
27 | -- default server used to send e-mails | 29 | -- default server used to send e-mails |
28 | SERVER = "localhost" | 30 | _M.SERVER = "localhost" |
29 | -- default port | 31 | -- default port |
30 | PORT = 25 | 32 | _M.PORT = 25 |
31 | -- domain used in HELO command and default sendmail | 33 | -- domain used in HELO command and default sendmail |
32 | -- If we are under a CGI, try to get from environment | 34 | -- If we are under a CGI, try to get from environment |
33 | DOMAIN = os.getenv("SERVER_NAME") or "localhost" | 35 | _M.DOMAIN = os.getenv("SERVER_NAME") or "localhost" |
34 | -- default time zone (means we don't know) | 36 | -- default time zone (means we don't know) |
35 | ZONE = "-0000" | 37 | _M.ZONE = "-0000" |
36 | 38 | ||
37 | --------------------------------------------------------------------------- | 39 | --------------------------------------------------------------------------- |
38 | -- Low level SMTP API | 40 | -- Low level SMTP API |
@@ -41,7 +43,7 @@ local metat = { __index = {} } | |||
41 | 43 | ||
42 | function metat.__index:greet(domain) | 44 | function metat.__index:greet(domain) |
43 | self.try(self.tp:check("2..")) | 45 | self.try(self.tp:check("2..")) |
44 | self.try(self.tp:command("EHLO", domain or DOMAIN)) | 46 | self.try(self.tp:command("EHLO", domain or _M.DOMAIN)) |
45 | return socket.skip(1, self.try(self.tp:check("2.."))) | 47 | return socket.skip(1, self.try(self.tp:check("2.."))) |
46 | end | 48 | end |
47 | 49 | ||
@@ -111,9 +113,9 @@ function metat.__index:send(mailt) | |||
111 | self:data(ltn12.source.chain(mailt.source, mime.stuff()), mailt.step) | 113 | self:data(ltn12.source.chain(mailt.source, mime.stuff()), mailt.step) |
112 | end | 114 | end |
113 | 115 | ||
114 | function open(server, port, create) | 116 | function _M.open(server, port, create) |
115 | local tp = socket.try(tp.connect(server or SERVER, port or PORT, | 117 | local tp = socket.try(tp.connect(server or _M.SERVER, port or _M.PORT, |
116 | TIMEOUT, create)) | 118 | _M.TIMEOUT, create)) |
117 | local s = base.setmetatable({tp = tp}, metat) | 119 | local s = base.setmetatable({tp = tp}, metat) |
118 | -- make sure tp is closed if we get an exception | 120 | -- make sure tp is closed if we get an exception |
119 | s.try = socket.newtry(function() | 121 | s.try = socket.newtry(function() |
@@ -221,14 +223,14 @@ end | |||
221 | local function adjust_headers(mesgt) | 223 | local function adjust_headers(mesgt) |
222 | local lower = lower_headers(mesgt.headers) | 224 | local lower = lower_headers(mesgt.headers) |
223 | lower["date"] = lower["date"] or | 225 | lower["date"] = lower["date"] or |
224 | os.date("!%a, %d %b %Y %H:%M:%S ") .. (mesgt.zone or ZONE) | 226 | os.date("!%a, %d %b %Y %H:%M:%S ") .. (mesgt.zone or _M.ZONE) |
225 | lower["x-mailer"] = lower["x-mailer"] or socket._VERSION | 227 | lower["x-mailer"] = lower["x-mailer"] or socket._VERSION |
226 | -- this can't be overriden | 228 | -- this can't be overriden |
227 | lower["mime-version"] = "1.0" | 229 | lower["mime-version"] = "1.0" |
228 | return lower | 230 | return lower |
229 | end | 231 | end |
230 | 232 | ||
231 | function message(mesgt) | 233 | function _M.message(mesgt) |
232 | mesgt.headers = adjust_headers(mesgt) | 234 | mesgt.headers = adjust_headers(mesgt) |
233 | -- create and return message source | 235 | -- create and return message source |
234 | local co = coroutine.create(function() send_message(mesgt) end) | 236 | local co = coroutine.create(function() send_message(mesgt) end) |
@@ -242,11 +244,13 @@ end | |||
242 | --------------------------------------------------------------------------- | 244 | --------------------------------------------------------------------------- |
243 | -- High level SMTP API | 245 | -- High level SMTP API |
244 | ----------------------------------------------------------------------------- | 246 | ----------------------------------------------------------------------------- |
245 | send = socket.protect(function(mailt) | 247 | _M.send = socket.protect(function(mailt) |
246 | local s = open(mailt.server, mailt.port, mailt.create) | 248 | local s = _M.open(mailt.server, mailt.port, mailt.create) |
247 | local ext = s:greet(mailt.domain) | 249 | local ext = s:greet(mailt.domain) |
248 | s:auth(mailt.user, mailt.password, ext) | 250 | s:auth(mailt.user, mailt.password, ext) |
249 | s:send(mailt) | 251 | s:send(mailt) |
250 | s:quit() | 252 | s:quit() |
251 | return s:close() | 253 | return s:close() |
252 | end) | 254 | end) |
255 | |||
256 | return _M \ No newline at end of file | ||