aboutsummaryrefslogtreecommitdiff
path: root/src/smtp.lua
diff options
context:
space:
mode:
authorDiego Nehab <diego@tecgraf.puc-rio.br>2004-06-15 06:24:00 +0000
committerDiego Nehab <diego@tecgraf.puc-rio.br>2004-06-15 06:24:00 +0000
commit58096449c6044b7aade5cd41cfd71c6bec1d273d (patch)
tree1814ffebe89c4c2556d84f97f66db37a7e8b4554 /src/smtp.lua
parent9ed7f955e5fc69af9bf1794fa2c8cd227981ba24 (diff)
downloadluasocket-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.lua50
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-----------------------------------------------------------------------------
13local open = assert(loadlib("smtp", "luaopen_smtp"))
14local smtp = assert(open())
15
16-----------------------------------------------------------------------------
17-- Load other required modules
18----------------------------------------------------------------------------- 11-----------------------------------------------------------------------------
12local smtp = requirelib("smtp")
19local socket = require("socket") 13local socket = require("socket")
20local ltn12 = require("ltn12") 14local ltn12 = require("ltn12")
21local tp = require("tp") 15local 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
27setmetatable(smtp, { __index = _G })
28setfenv(1, smtp)
29 21
22-- timeout for connection
23TIMEOUT = 60
30-- default server used to send e-mails 24-- default server used to send e-mails
31SERVER = "localhost" 25SERVER = "localhost"
32-- default port 26-- default port
@@ -94,9 +88,7 @@ function metat.__index:send(mailt)
94end 88end
95 89
96function open(server, port) 90function 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)
101end 93end
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
134end 132end
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
184local function adjust_headers(mesgt) 182local 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
202end 204end
203 205
204--------------------------------------------------------------------------- 206---------------------------------------------------------------------------
205-- High level SMTP API 207-- High level SMTP API
206----------------------------------------------------------------------------- 208-----------------------------------------------------------------------------
207send = socket.protect(function(mailt) 209send = 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()
213end) 215end)
214 216
215return smtp 217return smtp