diff options
| author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2005-08-12 05:56:32 +0000 |
|---|---|---|
| committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2005-08-12 05:56:32 +0000 |
| commit | 0c3cdd5ef2485a79d6fec9261f2850c41577d5b3 (patch) | |
| tree | d69164c9f815e2d0308ba3f0d15b18e67163d879 /src/smtp.lua | |
| parent | 37f7af4b9f1250e3c3439df03d43cf291a4d6f37 (diff) | |
| download | luasocket-0c3cdd5ef2485a79d6fec9261f2850c41577d5b3.tar.gz luasocket-0c3cdd5ef2485a79d6fec9261f2850c41577d5b3.tar.bz2 luasocket-0c3cdd5ef2485a79d6fec9261f2850c41577d5b3.zip | |
Final push for release...
Diffstat (limited to 'src/smtp.lua')
| -rw-r--r-- | src/smtp.lua | 29 |
1 files changed, 13 insertions, 16 deletions
diff --git a/src/smtp.lua b/src/smtp.lua index 6850f7f..46df1ab 100644 --- a/src/smtp.lua +++ b/src/smtp.lua | |||
| @@ -18,6 +18,7 @@ local tp = require("socket.tp") | |||
| 18 | local ltn12 = require("ltn12") | 18 | local ltn12 = require("ltn12") |
| 19 | local mime = require("mime") | 19 | local mime = require("mime") |
| 20 | module("socket.smtp") | 20 | module("socket.smtp") |
| 21 | getmetatable(_M).__index = nil | ||
| 21 | 22 | ||
| 22 | ----------------------------------------------------------------------------- | 23 | ----------------------------------------------------------------------------- |
| 23 | -- Program constants | 24 | -- Program constants |
| @@ -111,12 +112,12 @@ function metat.__index:send(mailt) | |||
| 111 | self:data(ltn12.source.chain(mailt.source, mime.stuff()), mailt.step) | 112 | self:data(ltn12.source.chain(mailt.source, mime.stuff()), mailt.step) |
| 112 | end | 113 | end |
| 113 | 114 | ||
| 114 | function open(server, port) | 115 | function open(server, port, create) |
| 115 | local tp = socket.try(tp.connect(server or SERVER, port or PORT, TIMEOUT)) | 116 | local tp = socket.try(tp.connect(server or SERVER, port or PORT, |
| 117 | create, TIMEOUT)) | ||
| 116 | local s = base.setmetatable({tp = tp}, metat) | 118 | local s = base.setmetatable({tp = tp}, metat) |
| 117 | -- make sure tp is closed if we get an exception | 119 | -- make sure tp is closed if we get an exception |
| 118 | s.try = socket.newtry(function() | 120 | s.try = socket.newtry(function() |
| 119 | if s.tp:command("QUIT") then s.tp:check("2..") end | ||
| 120 | s:close() | 121 | s:close() |
| 121 | end) | 122 | end) |
| 122 | return s | 123 | return s |
| @@ -165,10 +166,9 @@ end | |||
| 165 | local function send_source(mesgt) | 166 | local function send_source(mesgt) |
| 166 | -- set content-type if user didn't override | 167 | -- set content-type if user didn't override |
| 167 | if not mesgt.headers or not mesgt.headers["content-type"] then | 168 | if not mesgt.headers or not mesgt.headers["content-type"] then |
| 168 | coroutine.yield('content-type: text/plain; charset="iso-8859-1"\r\n') | 169 | coroutine.yield('content-type: text/plain; charset="iso-8859-1"\r\n\r\n') |
| 169 | end | 170 | else coroutine.yield("\r\n") end |
| 170 | -- finish headers | 171 | -- finish headers |
| 171 | coroutine.yield("\r\n") | ||
| 172 | -- send body from source | 172 | -- send body from source |
| 173 | while true do | 173 | while true do |
| 174 | local chunk, err = mesgt.body() | 174 | local chunk, err = mesgt.body() |
| @@ -182,21 +182,20 @@ end | |||
| 182 | local function send_string(mesgt) | 182 | local function send_string(mesgt) |
| 183 | -- set content-type if user didn't override | 183 | -- set content-type if user didn't override |
| 184 | if not mesgt.headers or not mesgt.headers["content-type"] then | 184 | if not mesgt.headers or not mesgt.headers["content-type"] then |
| 185 | coroutine.yield('content-type: text/plain; charset="iso-8859-1"\r\n') | 185 | coroutine.yield('content-type: text/plain; charset="iso-8859-1"\r\n\r\n') |
| 186 | end | 186 | else coroutine.yield("\r\n") end |
| 187 | -- finish headers | ||
| 188 | coroutine.yield("\r\n") | ||
| 189 | -- send body from string | 187 | -- send body from string |
| 190 | coroutine.yield(mesgt.body) | 188 | coroutine.yield(mesgt.body) |
| 191 | |||
| 192 | end | 189 | end |
| 193 | 190 | ||
| 194 | -- yield the headers one by one | 191 | -- yield the headers all at once |
| 195 | local function send_headers(mesgt) | 192 | local function send_headers(mesgt) |
| 196 | if mesgt.headers then | 193 | if mesgt.headers then |
| 194 | local h = "" | ||
| 197 | for i,v in base.pairs(mesgt.headers) do | 195 | for i,v in base.pairs(mesgt.headers) do |
| 198 | coroutine.yield(i .. ':' .. v .. "\r\n") | 196 | h = i .. ': ' .. v .. "\r\n" .. h |
| 199 | end | 197 | end |
| 198 | coroutine.yield(h) | ||
| 200 | end | 199 | end |
| 201 | end | 200 | end |
| 202 | 201 | ||
| @@ -237,12 +236,10 @@ end | |||
| 237 | -- High level SMTP API | 236 | -- High level SMTP API |
| 238 | ----------------------------------------------------------------------------- | 237 | ----------------------------------------------------------------------------- |
| 239 | send = socket.protect(function(mailt) | 238 | send = socket.protect(function(mailt) |
| 240 | local s = open(mailt.server, mailt.port) | 239 | local s = open(mailt.server, mailt.port, mailt.create) |
| 241 | local ext = s:greet(mailt.domain) | 240 | local ext = s:greet(mailt.domain) |
| 242 | s:auth(mailt.user, mailt.password, ext) | 241 | s:auth(mailt.user, mailt.password, ext) |
| 243 | s:send(mailt) | 242 | s:send(mailt) |
| 244 | s:quit() | 243 | s:quit() |
| 245 | return s:close() | 244 | return s:close() |
| 246 | end) | 245 | end) |
| 247 | |||
| 248 | --getmetatable(_M).__index = nil | ||
