diff options
author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-06-15 06:24:00 +0000 |
---|---|---|
committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-06-15 06:24:00 +0000 |
commit | 58096449c6044b7aade5cd41cfd71c6bec1d273d (patch) | |
tree | 1814ffebe89c4c2556d84f97f66db37a7e8b4554 /src/smtp.lua | |
parent | 9ed7f955e5fc69af9bf1794fa2c8cd227981ba24 (diff) | |
download | luasocket-58096449c6044b7aade5cd41cfd71c6bec1d273d.tar.gz luasocket-58096449c6044b7aade5cd41cfd71c6bec1d273d.tar.bz2 luasocket-58096449c6044b7aade5cd41cfd71c6bec1d273d.zip |
Manual is almost done. HTTP is missing.
Implemented new distribution scheme.
Select is now purely C.
HTTP reimplemented seems faster dunno why.
LTN12 functions that coroutines fail gracefully.
Diffstat (limited to 'src/smtp.lua')
-rw-r--r-- | src/smtp.lua | 50 |
1 files changed, 26 insertions, 24 deletions
diff --git a/src/smtp.lua b/src/smtp.lua index 7ae99a5..dc80c35 100644 --- a/src/smtp.lua +++ b/src/smtp.lua | |||
@@ -7,15 +7,9 @@ | |||
7 | ----------------------------------------------------------------------------- | 7 | ----------------------------------------------------------------------------- |
8 | 8 | ||
9 | ----------------------------------------------------------------------------- | 9 | ----------------------------------------------------------------------------- |
10 | -- Load SMTP from dynamic library | 10 | -- Load required modules |
11 | -- Comment these lines if you are loading static | ||
12 | ----------------------------------------------------------------------------- | ||
13 | local open = assert(loadlib("smtp", "luaopen_smtp")) | ||
14 | local smtp = assert(open()) | ||
15 | |||
16 | ----------------------------------------------------------------------------- | ||
17 | -- Load other required modules | ||
18 | ----------------------------------------------------------------------------- | 11 | ----------------------------------------------------------------------------- |
12 | local smtp = requirelib("smtp") | ||
19 | local socket = require("socket") | 13 | local socket = require("socket") |
20 | local ltn12 = require("ltn12") | 14 | local ltn12 = require("ltn12") |
21 | local tp = require("tp") | 15 | local tp = require("tp") |
@@ -23,10 +17,10 @@ local tp = require("tp") | |||
23 | ----------------------------------------------------------------------------- | 17 | ----------------------------------------------------------------------------- |
24 | -- Setup namespace | 18 | -- Setup namespace |
25 | ----------------------------------------------------------------------------- | 19 | ----------------------------------------------------------------------------- |
26 | -- make all module globals fall into smtp namespace | 20 | _LOADED["smtp"] = smtp |
27 | setmetatable(smtp, { __index = _G }) | ||
28 | setfenv(1, smtp) | ||
29 | 21 | ||
22 | -- timeout for connection | ||
23 | TIMEOUT = 60 | ||
30 | -- default server used to send e-mails | 24 | -- default server used to send e-mails |
31 | SERVER = "localhost" | 25 | SERVER = "localhost" |
32 | -- default port | 26 | -- default port |
@@ -94,9 +88,7 @@ function metat.__index:send(mailt) | |||
94 | end | 88 | end |
95 | 89 | ||
96 | function open(server, port) | 90 | function open(server, port) |
97 | print(server or SERVER, port or PORT) | 91 | local tp = socket.try(tp.connect(server or SERVER, port or PORT, TIMEOUT)) |
98 | local tp, error = tp.connect(server or SERVER, port or PORT) | ||
99 | if not tp then return nil, error end | ||
100 | return setmetatable({tp = tp}, metat) | 92 | return setmetatable({tp = tp}, metat) |
101 | end | 93 | end |
102 | 94 | ||
@@ -121,7 +113,10 @@ local function send_multipart(mesgt) | |||
121 | coroutine.yield('content-type: multipart/mixed; boundary="' .. | 113 | coroutine.yield('content-type: multipart/mixed; boundary="' .. |
122 | bd .. '"\r\n\r\n') | 114 | bd .. '"\r\n\r\n') |
123 | -- send preamble | 115 | -- send preamble |
124 | if mesgt.body.preamble then coroutine.yield(mesgt.body.preamble) end | 116 | if mesgt.body.preamble then |
117 | coroutine.yield(mesgt.body.preamble) | ||
118 | coroutine.yield("\r\n") | ||
119 | end | ||
125 | -- send each part separated by a boundary | 120 | -- send each part separated by a boundary |
126 | for i, m in ipairs(mesgt.body) do | 121 | for i, m in ipairs(mesgt.body) do |
127 | coroutine.yield("\r\n--" .. bd .. "\r\n") | 122 | coroutine.yield("\r\n--" .. bd .. "\r\n") |
@@ -130,7 +125,10 @@ local function send_multipart(mesgt) | |||
130 | -- send last boundary | 125 | -- send last boundary |
131 | coroutine.yield("\r\n--" .. bd .. "--\r\n\r\n") | 126 | coroutine.yield("\r\n--" .. bd .. "--\r\n\r\n") |
132 | -- send epilogue | 127 | -- send epilogue |
133 | if mesgt.body.epilogue then coroutine.yield(mesgt.body.epilogue) end | 128 | if mesgt.body.epilogue then |
129 | coroutine.yield(mesgt.body.epilogue) | ||
130 | coroutine.yield("\r\n") | ||
131 | end | ||
134 | end | 132 | end |
135 | 133 | ||
136 | -- yield message body from a source | 134 | -- yield message body from a source |
@@ -183,12 +181,12 @@ end | |||
183 | -- set defaul headers | 181 | -- set defaul headers |
184 | local function adjust_headers(mesgt) | 182 | local function adjust_headers(mesgt) |
185 | local lower = {} | 183 | local lower = {} |
186 | for i,v in (mesgt or lower) do | 184 | for i,v in (mesgt.headers or lower) do |
187 | lower[string.lower(i)] = v | 185 | lower[string.lower(i)] = v |
188 | end | 186 | end |
189 | lower["date"] = lower["date"] or | 187 | lower["date"] = lower["date"] or |
190 | os.date("!%a, %d %b %Y %H:%M:%S ") .. (mesgt.zone or ZONE) | 188 | os.date("!%a, %d %b %Y %H:%M:%S ") .. (mesgt.zone or ZONE) |
191 | lower["x-mailer"] = lower["x-mailer"] or socket.version | 189 | lower["x-mailer"] = lower["x-mailer"] or socket.VERSION |
192 | -- this can't be overriden | 190 | -- this can't be overriden |
193 | lower["mime-version"] = "1.0" | 191 | lower["mime-version"] = "1.0" |
194 | mesgt.headers = lower | 192 | mesgt.headers = lower |
@@ -198,18 +196,22 @@ function message(mesgt) | |||
198 | adjust_headers(mesgt) | 196 | adjust_headers(mesgt) |
199 | -- create and return message source | 197 | -- create and return message source |
200 | local co = coroutine.create(function() send_message(mesgt) end) | 198 | local co = coroutine.create(function() send_message(mesgt) end) |
201 | return function() return socket.skip(1, coroutine.resume(co)) end | 199 | return function() |
200 | local ret, a, b = coroutine.resume(co) | ||
201 | if ret then return a, b | ||
202 | else return nil, a end | ||
203 | end | ||
202 | end | 204 | end |
203 | 205 | ||
204 | --------------------------------------------------------------------------- | 206 | --------------------------------------------------------------------------- |
205 | -- High level SMTP API | 207 | -- High level SMTP API |
206 | ----------------------------------------------------------------------------- | 208 | ----------------------------------------------------------------------------- |
207 | send = socket.protect(function(mailt) | 209 | send = socket.protect(function(mailt) |
208 | local smtp = socket.try(open(mailt.server, mailt.port)) | 210 | local con = open(mailt.server, mailt.port) |
209 | smtp:greet(mailt.domain) | 211 | con:greet(mailt.domain) |
210 | smtp:send(mailt) | 212 | con:send(mailt) |
211 | smtp:quit() | 213 | con:quit() |
212 | return smtp:close() | 214 | return con:close() |
213 | end) | 215 | end) |
214 | 216 | ||
215 | return smtp | 217 | return smtp |