diff options
Diffstat (limited to 'src/smtp.lua')
-rw-r--r-- | src/smtp.lua | 64 |
1 files changed, 33 insertions, 31 deletions
diff --git a/src/smtp.lua b/src/smtp.lua index 1708053..d6357d2 100644 --- a/src/smtp.lua +++ b/src/smtp.lua | |||
@@ -31,51 +31,51 @@ ZONE = "-0000" | |||
31 | local metat = { __index = {} } | 31 | local metat = { __index = {} } |
32 | 32 | ||
33 | function metat.__index:greet(domain) | 33 | function metat.__index:greet(domain) |
34 | socket.try(self.tp:check("2..")) | 34 | self.try(self.tp:check("2..")) |
35 | socket.try(self.tp:command("EHLO", domain or DOMAIN)) | 35 | self.try(self.tp:command("EHLO", domain or DOMAIN)) |
36 | return socket.skip(1, socket.try(self.tp:check("2.."))) | 36 | return socket.skip(1, self.try(self.tp:check("2.."))) |
37 | end | 37 | end |
38 | 38 | ||
39 | function metat.__index:mail(from) | 39 | function metat.__index:mail(from) |
40 | socket.try(self.tp:command("MAIL", "FROM:" .. from)) | 40 | self.try(self.tp:command("MAIL", "FROM:" .. from)) |
41 | return socket.try(self.tp:check("2..")) | 41 | return self.try(self.tp:check("2..")) |
42 | end | 42 | end |
43 | 43 | ||
44 | function metat.__index:rcpt(to) | 44 | function metat.__index:rcpt(to) |
45 | socket.try(self.tp:command("RCPT", "TO:" .. to)) | 45 | self.try(self.tp:command("RCPT", "TO:" .. to)) |
46 | return socket.try(self.tp:check("2..")) | 46 | return self.try(self.tp:check("2..")) |
47 | end | 47 | end |
48 | 48 | ||
49 | function metat.__index:data(src, step) | 49 | function metat.__index:data(src, step) |
50 | socket.try(self.tp:command("DATA")) | 50 | self.try(self.tp:command("DATA")) |
51 | socket.try(self.tp:check("3..")) | 51 | self.try(self.tp:check("3..")) |
52 | socket.try(self.tp:source(src, step)) | 52 | self.try(self.tp:source(src, step)) |
53 | socket.try(self.tp:send("\r\n.\r\n")) | 53 | self.try(self.tp:send("\r\n.\r\n")) |
54 | return socket.try(self.tp:check("2..")) | 54 | return self.try(self.tp:check("2..")) |
55 | end | 55 | end |
56 | 56 | ||
57 | function metat.__index:quit() | 57 | function metat.__index:quit() |
58 | socket.try(self.tp:command("QUIT")) | 58 | self.try(self.tp:command("QUIT")) |
59 | return socket.try(self.tp:check("2..")) | 59 | return self.try(self.tp:check("2..")) |
60 | end | 60 | end |
61 | 61 | ||
62 | function metat.__index:close() | 62 | function metat.__index:close() |
63 | return socket.try(self.tp:close()) | 63 | return self.try(self.tp:close()) |
64 | end | 64 | end |
65 | 65 | ||
66 | function metat.__index:login(user, password) | 66 | function metat.__index:login(user, password) |
67 | socket.try(self.tp:command("AUTH", "LOGIN")) | 67 | self.try(self.tp:command("AUTH", "LOGIN")) |
68 | socket.try(self.tp:check("3..")) | 68 | self.try(self.tp:check("3..")) |
69 | socket.try(self.tp:command(mime.b64(user))) | 69 | self.try(self.tp:command(mime.b64(user))) |
70 | socket.try(self.tp:check("3..")) | 70 | self.try(self.tp:check("3..")) |
71 | socket.try(self.tp:command(mime.b64(password))) | 71 | self.try(self.tp:command(mime.b64(password))) |
72 | return socket.try(self.tp:check("2..")) | 72 | return self.try(self.tp:check("2..")) |
73 | end | 73 | end |
74 | 74 | ||
75 | function metat.__index:plain(user, password) | 75 | function metat.__index:plain(user, password) |
76 | local auth = "PLAIN " .. mime.b64("\0" .. user .. "\0" .. password) | 76 | local auth = "PLAIN " .. mime.b64("\0" .. user .. "\0" .. password) |
77 | socket.try(self.tp:command("AUTH", auth)) | 77 | self.try(self.tp:command("AUTH", auth)) |
78 | return socket.try(self.tp:check("2..")) | 78 | return self.try(self.tp:check("2..")) |
79 | end | 79 | end |
80 | 80 | ||
81 | function metat.__index:auth(user, password, ext) | 81 | function metat.__index:auth(user, password, ext) |
@@ -85,7 +85,7 @@ function metat.__index:auth(user, password, ext) | |||
85 | elseif string.find(ext, "AUTH[^\n]+PLAIN") then | 85 | elseif string.find(ext, "AUTH[^\n]+PLAIN") then |
86 | return self:plain(user, password) | 86 | return self:plain(user, password) |
87 | else | 87 | else |
88 | socket.try(nil, "authentication not supported") | 88 | self.try(nil, "authentication not supported") |
89 | end | 89 | end |
90 | end | 90 | end |
91 | 91 | ||
@@ -104,7 +104,9 @@ end | |||
104 | 104 | ||
105 | function open(server, port) | 105 | function open(server, port) |
106 | local tp = socket.try(tp.connect(server or SERVER, port or PORT, TIMEOUT)) | 106 | local tp = socket.try(tp.connect(server or SERVER, port or PORT, TIMEOUT)) |
107 | return setmetatable({tp = tp}, metat) | 107 | -- make sure tp is closed if we get an exception |
108 | local try = socket.newtry(function() tp:close() end) | ||
109 | return setmetatable({ tp = tp, try = try}, metat) | ||
108 | end | 110 | end |
109 | 111 | ||
110 | --------------------------------------------------------------------------- | 112 | --------------------------------------------------------------------------- |
@@ -222,10 +224,10 @@ end | |||
222 | -- High level SMTP API | 224 | -- High level SMTP API |
223 | ----------------------------------------------------------------------------- | 225 | ----------------------------------------------------------------------------- |
224 | send = socket.protect(function(mailt) | 226 | send = socket.protect(function(mailt) |
225 | local con = open(mailt.server, mailt.port) | 227 | local s = open(mailt.server, mailt.port) |
226 | local ext = con:greet(mailt.domain) | 228 | local ext = s:greet(mailt.domain) |
227 | con:auth(mailt.user, mailt.password, ext) | 229 | s:auth(mailt.user, mailt.password, ext) |
228 | con:send(mailt) | 230 | s:send(mailt) |
229 | con:quit() | 231 | s:quit() |
230 | return con:close() | 232 | return s:close() |
231 | end) | 233 | end) |