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/smtp.lua | |
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/smtp.lua')
-rw-r--r-- | src/smtp.lua | 33 |
1 files changed, 30 insertions, 3 deletions
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() |