aboutsummaryrefslogtreecommitdiff
path: root/src/smtp.lua
diff options
context:
space:
mode:
authorDiego Nehab <diego@tecgraf.puc-rio.br>2004-11-27 07:58:04 +0000
committerDiego Nehab <diego@tecgraf.puc-rio.br>2004-11-27 07:58:04 +0000
commit7c97e8e40aaa665226fb54449773dc3134e755b2 (patch)
tree47888d4c924fc24bf3b355bf58120ea3cdc74bc4 /src/smtp.lua
parenteb0fc857ddea6f084d338589e2a33d3e7d4eade6 (diff)
downloadluasocket-7c97e8e40aaa665226fb54449773dc3134e755b2.tar.gz
luasocket-7c97e8e40aaa665226fb54449773dc3134e755b2.tar.bz2
luasocket-7c97e8e40aaa665226fb54449773dc3134e755b2.zip
Almost ready for beta3
Diffstat (limited to 'src/smtp.lua')
-rw-r--r--src/smtp.lua25
1 files changed, 15 insertions, 10 deletions
diff --git a/src/smtp.lua b/src/smtp.lua
index 974d222..9d49178 100644
--- a/src/smtp.lua
+++ b/src/smtp.lua
@@ -8,13 +8,16 @@
8----------------------------------------------------------------------------- 8-----------------------------------------------------------------------------
9-- Declare module and import dependencies 9-- Declare module and import dependencies
10----------------------------------------------------------------------------- 10-----------------------------------------------------------------------------
11local base = require("base")
12local coroutine = require("coroutine")
13local string = require("string")
14local math = require("math")
15local os = require("os")
11local socket = require("socket") 16local socket = require("socket")
12local tp = require("socket.tp") 17local tp = require("socket.tp")
13
14local ltn12 = require("ltn12") 18local ltn12 = require("ltn12")
15local mime = require("mime") 19local mime = require("mime")
16 20local smtp = module("socket.smtp")
17module("socket.smtp")
18 21
19----------------------------------------------------------------------------- 22-----------------------------------------------------------------------------
20-- Program constants 23-- Program constants
@@ -98,8 +101,8 @@ end
98-- send message or throw an exception 101-- send message or throw an exception
99function metat.__index:send(mailt) 102function metat.__index:send(mailt)
100 self:mail(mailt.from) 103 self:mail(mailt.from)
101 if type(mailt.rcpt) == "table" then 104 if base.type(mailt.rcpt) == "table" then
102 for i,v in ipairs(mailt.rcpt) do 105 for i,v in base.ipairs(mailt.rcpt) do
103 self:rcpt(v) 106 self:rcpt(v)
104 end 107 end
105 else 108 else
@@ -110,7 +113,7 @@ end
110 113
111function open(server, port) 114function open(server, port)
112 local tp = socket.try(tp.connect(server or SERVER, port or PORT, TIMEOUT)) 115 local tp = socket.try(tp.connect(server or SERVER, port or PORT, TIMEOUT))
113 local s = setmetatable({tp = tp}, metat) 116 local s = base.setmetatable({tp = tp}, metat)
114 -- make sure tp is closed if we get an exception 117 -- make sure tp is closed if we get an exception
115 s.try = socket.newtry(function() 118 s.try = socket.newtry(function()
116 if s.tp:command("QUIT") then s.tp:check("2..") end 119 if s.tp:command("QUIT") then s.tp:check("2..") end
@@ -145,7 +148,7 @@ local function send_multipart(mesgt)
145 coroutine.yield("\r\n") 148 coroutine.yield("\r\n")
146 end 149 end
147 -- send each part separated by a boundary 150 -- send each part separated by a boundary
148 for i, m in ipairs(mesgt.body) do 151 for i, m in base.ipairs(mesgt.body) do
149 coroutine.yield("\r\n--" .. bd .. "\r\n") 152 coroutine.yield("\r\n--" .. bd .. "\r\n")
150 send_message(m) 153 send_message(m)
151 end 154 end
@@ -191,7 +194,7 @@ end
191-- yield the headers one by one 194-- yield the headers one by one
192local function send_headers(mesgt) 195local function send_headers(mesgt)
193 if mesgt.headers then 196 if mesgt.headers then
194 for i,v in pairs(mesgt.headers) do 197 for i,v in base.pairs(mesgt.headers) do
195 coroutine.yield(i .. ':' .. v .. "\r\n") 198 coroutine.yield(i .. ':' .. v .. "\r\n")
196 end 199 end
197 end 200 end
@@ -200,8 +203,8 @@ end
200-- message source 203-- message source
201function send_message(mesgt) 204function send_message(mesgt)
202 send_headers(mesgt) 205 send_headers(mesgt)
203 if type(mesgt.body) == "table" then send_multipart(mesgt) 206 if base.type(mesgt.body) == "table" then send_multipart(mesgt)
204 elseif type(mesgt.body) == "function" then send_source(mesgt) 207 elseif base.type(mesgt.body) == "function" then send_source(mesgt)
205 else send_string(mesgt) end 208 else send_string(mesgt) end
206end 209end
207 210
@@ -241,3 +244,5 @@ send = socket.protect(function(mailt)
241 s:quit() 244 s:quit()
242 return s:close() 245 return s:close()
243end) 246end)
247
248base.setmetatable(smtp, nil)