diff options
author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-06-16 04:28:21 +0000 |
---|---|---|
committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-06-16 04:28:21 +0000 |
commit | 0a4c1534f39511894728da193ab8225ad6022de9 (patch) | |
tree | 683b711accf64eca486b138cbc034c609f93a53a /src | |
parent | 8e80e38f2c3121242b3b2f7a45a960c9af4d1a68 (diff) | |
download | luasocket-0a4c1534f39511894728da193ab8225ad6022de9.tar.gz luasocket-0a4c1534f39511894728da193ab8225ad6022de9.tar.bz2 luasocket-0a4c1534f39511894728da193ab8225ad6022de9.zip |
Still work to do in the manual...
Diffstat (limited to 'src')
-rw-r--r-- | src/ltn12.lua | 8 | ||||
-rw-r--r-- | src/smtp.lua | 33 |
2 files changed, 38 insertions, 3 deletions
diff --git a/src/ltn12.lua b/src/ltn12.lua index bb527bd..09771d4 100644 --- a/src/ltn12.lua +++ b/src/ltn12.lua | |||
@@ -23,6 +23,7 @@ BLOCKSIZE = 2048 | |||
23 | ----------------------------------------------------------------------------- | 23 | ----------------------------------------------------------------------------- |
24 | -- returns a high level filter that cycles a low-level filter | 24 | -- returns a high level filter that cycles a low-level filter |
25 | function filter.cycle(low, ctx, extra) | 25 | function filter.cycle(low, ctx, extra) |
26 | assert(low) | ||
26 | return function(chunk) | 27 | return function(chunk) |
27 | local ret | 28 | local ret |
28 | ret, ctx = low(ctx, chunk, extra) | 29 | ret, ctx = low(ctx, chunk, extra) |
@@ -32,6 +33,7 @@ end | |||
32 | 33 | ||
33 | -- chains two filters together | 34 | -- chains two filters together |
34 | local function chain2(f1, f2) | 35 | local function chain2(f1, f2) |
36 | assert(f1 and f2) | ||
35 | local co = coroutine.create(function(chunk) | 37 | local co = coroutine.create(function(chunk) |
36 | while true do | 38 | while true do |
37 | local filtered1 = f1(chunk) | 39 | local filtered1 = f1(chunk) |
@@ -95,6 +97,7 @@ end | |||
95 | 97 | ||
96 | -- turns a fancy source into a simple source | 98 | -- turns a fancy source into a simple source |
97 | function source.simplify(src) | 99 | function source.simplify(src) |
100 | assert(src) | ||
98 | return function() | 101 | return function() |
99 | local chunk, err_or_new = src() | 102 | local chunk, err_or_new = src() |
100 | src = err_or_new or src | 103 | src = err_or_new or src |
@@ -118,6 +121,7 @@ end | |||
118 | 121 | ||
119 | -- creates rewindable source | 122 | -- creates rewindable source |
120 | function source.rewind(src) | 123 | function source.rewind(src) |
124 | assert(src) | ||
121 | local t = {} | 125 | local t = {} |
122 | return function(chunk) | 126 | return function(chunk) |
123 | if not chunk then | 127 | if not chunk then |
@@ -132,6 +136,7 @@ end | |||
132 | 136 | ||
133 | -- chains a source with a filter | 137 | -- chains a source with a filter |
134 | function source.chain(src, f) | 138 | function source.chain(src, f) |
139 | assert(src and f) | ||
135 | local co = coroutine.create(function() | 140 | local co = coroutine.create(function() |
136 | while true do | 141 | while true do |
137 | local chunk, err = src() | 142 | local chunk, err = src() |
@@ -186,6 +191,7 @@ end | |||
186 | 191 | ||
187 | -- turns a fancy sink into a simple sink | 192 | -- turns a fancy sink into a simple sink |
188 | function sink.simplify(snk) | 193 | function sink.simplify(snk) |
194 | assert(snk) | ||
189 | return function(chunk, err) | 195 | return function(chunk, err) |
190 | local ret, err_or_new = snk(chunk, err) | 196 | local ret, err_or_new = snk(chunk, err) |
191 | if not ret then return nil, err_or_new end | 197 | if not ret then return nil, err_or_new end |
@@ -224,6 +230,7 @@ end | |||
224 | 230 | ||
225 | -- chains a sink with a filter | 231 | -- chains a sink with a filter |
226 | function sink.chain(f, snk) | 232 | function sink.chain(f, snk) |
233 | assert(f and snk) | ||
227 | return function(chunk, err) | 234 | return function(chunk, err) |
228 | local filtered = f(chunk) | 235 | local filtered = f(chunk) |
229 | local done = chunk and "" | 236 | local done = chunk and "" |
@@ -248,6 +255,7 @@ end | |||
248 | 255 | ||
249 | -- pumps all data from a source to a sink, using a step function | 256 | -- pumps all data from a source to a sink, using a step function |
250 | function pump.all(src, snk, step) | 257 | function pump.all(src, snk, step) |
258 | assert(src and snk) | ||
251 | step = step or pump.step | 259 | step = step or pump.step |
252 | while true do | 260 | while true do |
253 | local ret, err = step(src, snk) | 261 | local ret, err = step(src, snk) |
diff --git a/src/smtp.lua b/src/smtp.lua index fb76ea4..2ea6097 100644 --- a/src/smtp.lua +++ b/src/smtp.lua | |||
@@ -11,6 +11,7 @@ | |||
11 | local smtp = requirelib("smtp", "luaopen_smtp", getfenv(1)) | 11 | local smtp = requirelib("smtp", "luaopen_smtp", getfenv(1)) |
12 | local socket = require("socket") | 12 | local socket = require("socket") |
13 | local ltn12 = require("ltn12") | 13 | local ltn12 = require("ltn12") |
14 | local mime = require("mime") | ||
14 | local tp = require("tp") | 15 | local tp = require("tp") |
15 | 16 | ||
16 | ----------------------------------------------------------------------------- | 17 | ----------------------------------------------------------------------------- |
@@ -43,7 +44,7 @@ local metat = { __index = {} } | |||
43 | function metat.__index:greet(domain) | 44 | function metat.__index:greet(domain) |
44 | socket.try(self.tp:check("2..")) | 45 | socket.try(self.tp:check("2..")) |
45 | socket.try(self.tp:command("EHLO", domain or DOMAIN)) | 46 | socket.try(self.tp:command("EHLO", domain or DOMAIN)) |
46 | return socket.try(self.tp:check("2..")) | 47 | return socket.skip(1, socket.try(self.tp:check("2.."))) |
47 | end | 48 | end |
48 | 49 | ||
49 | function metat.__index:mail(from) | 50 | function metat.__index:mail(from) |
@@ -73,6 +74,32 @@ function metat.__index:close() | |||
73 | return socket.try(self.tp:close()) | 74 | return socket.try(self.tp:close()) |
74 | end | 75 | end |
75 | 76 | ||
77 | function metat.__index:login(user, password) | ||
78 | socket.try(self.tp:command("AUTH", "LOGIN")) | ||
79 | socket.try(self.tp:check("3..")) | ||
80 | socket.try(self.tp:command(mime.b64(user))) | ||
81 | socket.try(self.tp:check("3..")) | ||
82 | socket.try(self.tp:command(mime.b64(password))) | ||
83 | return socket.try(self.tp:check("2..")) | ||
84 | end | ||
85 | |||
86 | function metat.__index:plain(user, password) | ||
87 | local auth = "PLAIN " .. mime.b64("\0" .. user .. "\0" .. password) | ||
88 | socket.try(self.tp:command("AUTH", auth)) | ||
89 | return socket.try(self.tp:check("2..")) | ||
90 | end | ||
91 | |||
92 | function metat.__index:auth(user, password, ext) | ||
93 | if not user or not password then return 1 end | ||
94 | if string.find(ext, "AUTH[^\n]+LOGIN") then | ||
95 | return self:login(user, password) | ||
96 | elseif string.find(ext, "AUTH[^\n]+PLAIN") then | ||
97 | return self:plain(user, password) | ||
98 | else | ||
99 | socket.try(nil, "authentication not supported") | ||
100 | end | ||
101 | end | ||
102 | |||
76 | -- send message or throw an exception | 103 | -- send message or throw an exception |
77 | function metat.__index:send(mailt) | 104 | function metat.__index:send(mailt) |
78 | self:mail(mailt.from) | 105 | self:mail(mailt.from) |
@@ -205,10 +232,10 @@ end | |||
205 | --------------------------------------------------------------------------- | 232 | --------------------------------------------------------------------------- |
206 | -- High level SMTP API | 233 | -- High level SMTP API |
207 | ----------------------------------------------------------------------------- | 234 | ----------------------------------------------------------------------------- |
208 | socket.protect = function(a) return a end | ||
209 | send = socket.protect(function(mailt) | 235 | send = socket.protect(function(mailt) |
210 | local con = open(mailt.server, mailt.port) | 236 | local con = open(mailt.server, mailt.port) |
211 | con:greet(mailt.domain) | 237 | local ext = con:greet(mailt.domain) |
238 | con:auth(mailt.user, mailt.password, ext) | ||
212 | con:send(mailt) | 239 | con:send(mailt) |
213 | con:quit() | 240 | con:quit() |
214 | return con:close() | 241 | return con:close() |