| 1 | local base = _G |
| 2 | local coroutine = require("coroutine") |
| 3 | local string = require("string") |
| 4 | local math = require("math") |
| 5 | local os = require("os") |
| 6 | local socket = socket or require("socket") ; |
| 7 | local tp = socket.tp or require("socket.tp"); |
| 8 | local ltn12 = ltn12 or require("ltn12"); |
| 9 | local headers = socket.headers or require("socket.headers"); |
| 10 | local mime = mime or require("mime"); |
| 11 | socket.smtp = {} |
| 12 | local _M = socket.smtp |
| 13 | module("socket.smtp") |
| 14 | _M.TIMEOUT = 60 |
| 15 | _M.SERVER = "localhost" |
| 16 | _M.PORT = 25 |
| 17 | _M.DOMAIN = os.getenv("SERVER_NAME") or "localhost" |
| 18 | _M.ZONE = "-0000" |
| 19 | local metat = { __index = {} } |
| 20 | function metat.__index:greet(domain) |
| 21 | self.try(self.tp:check("2..")) |
| 22 | self.try(self.tp:command("EHLO", domain or _M.DOMAIN)) |
| 23 | return socket.skip(1, self.try(self.tp:check("2.."))) |
| 24 | end |
| 25 | function metat.__index:mail(from) |
| 26 | self.try(self.tp:command("MAIL", "FROM:" .. from)) |
| 27 | return self.try(self.tp:check("2..")) |
| 28 | end |
| 29 | function metat.__index:rcpt(to) |
| 30 | self.try(self.tp:command("RCPT", "TO:" .. to)) |
| 31 | return self.try(self.tp:check("2..")) |
| 32 | end |
| 33 | function metat.__index:data(src, step) |
| 34 | self.try(self.tp:command("DATA")) |
| 35 | self.try(self.tp:check("3..")) |
| 36 | self.try(self.tp:source(src, step)) |
| 37 | self.try(self.tp:send("\r\n.\r\n")) |
| 38 | return self.try(self.tp:check("2..")) |
| 39 | end |
| 40 | function metat.__index:quit() |
| 41 | self.try(self.tp:command("QUIT")) |
| 42 | return self.try(self.tp:check("2..")) |
| 43 | end |
| 44 | function metat.__index:close() |
| 45 | return self.tp:close() |
| 46 | end |
| 47 | function metat.__index:login(user, password) |
| 48 | self.try(self.tp:command("AUTH", "LOGIN")) |
| 49 | self.try(self.tp:check("3..")) |
| 50 | self.try(self.tp:send(mime.b64(user) .. "\r\n")) |
| 51 | self.try(self.tp:check("3..")) |
| 52 | self.try(self.tp:send(mime.b64(password) .. "\r\n")) |
| 53 | return self.try(self.tp:check("2..")) |
| 54 | end |
| 55 | function metat.__index:plain(user, password) |
| 56 | local auth = "PLAIN " .. mime.b64("\0" .. user .. "\0" .. password) |
| 57 | self.try(self.tp:command("AUTH", auth)) |
| 58 | return self.try(self.tp:check("2..")) |
| 59 | end |
| 60 | function metat.__index:auth(user, password, ext) |
| 61 | if not user or not password then return 1 end |
| 62 | if string.find(ext, "AUTH[^\n]+LOGIN") then |
| 63 | return self:login(user, password) |
| 64 | elseif string.find(ext, "AUTH[^\n]+PLAIN") then |
| 65 | return self:plain(user, password) |
| 66 | else |
| 67 | self.try(nil, "authentication not supported") |
| 68 | end |
| 69 | end |
| 70 | function metat.__index:send(mailt) |
| 71 | self:mail(mailt.from) |
| 72 | if base.type(mailt.rcpt) == "table" then |
| 73 | for i,v in base.ipairs(mailt.rcpt) do |
| 74 | self:rcpt(v) |
| 75 | end |
| 76 | else |
| 77 | self:rcpt(mailt.rcpt) |
| 78 | end |
| 79 | self:data(ltn12.source.chain(mailt.source, mime.stuff()), mailt.step) |
| 80 | end |
| 81 | function _M.open(server, port, create) |
| 82 | local tp = socket.try(tp.connect(server or _M.SERVER, port or _M.PORT, |
| 83 | _M.TIMEOUT, create)) |
| 84 | local s = base.setmetatable({tp = tp}, metat) |
| 85 | s.try = socket.newtry(function() |
| 86 | s:close() |
| 87 | end) |
| 88 | return s |
| 89 | end |
| 90 | local function lower_headers(headers) |
| 91 | local lower = {} |
| 92 | for i,v in base.pairs(headers or lower) do |
| 93 | lower[string.lower(i)] = v |
| 94 | end |
| 95 | return lower |
| 96 | end |
| 97 | local seqno = 0 |
| 98 | local function newboundary() |
| 99 | seqno = seqno + 1 |
| 100 | return string.format('%s%05d==%05u', os.date('%d%m%Y%H%M%S'), |
| 101 | math.random(0, 99999), seqno) |
| 102 | end |
| 103 | local send_message |
| 104 | local function send_headers(tosend) |
| 105 | local canonic = headers.canonic |
| 106 | local h = "\r\n" |
| 107 | for f,v in base.pairs(tosend) do |
| 108 | h = (canonic[f] or f) .. ': ' .. v .. "\r\n" .. h |
| 109 | end |
| 110 | coroutine.yield(h) |
| 111 | end |
| 112 | local function send_multipart(mesgt) |
| 113 | local bd = newboundary() |
| 114 | local headers = lower_headers(mesgt.headers or {}) |
| 115 | headers['content-type'] = headers['content-type'] or 'multipart/mixed' |
| 116 | headers['content-type'] = headers['content-type'] .. |
| 117 | '; boundary="' .. bd .. '"' |
| 118 | send_headers(headers) |
| 119 | if mesgt.body.preamble then |
| 120 | coroutine.yield(mesgt.body.preamble) |
| 121 | coroutine.yield("\r\n") |
| 122 | end |
| 123 | for i, m in base.ipairs(mesgt.body) do |
| 124 | coroutine.yield("\r\n--" .. bd .. "\r\n") |
| 125 | send_message(m) |
| 126 | end |
| 127 | coroutine.yield("\r\n--" .. bd .. "--\r\n\r\n") |
| 128 | if mesgt.body.epilogue then |
| 129 | coroutine.yield(mesgt.body.epilogue) |
| 130 | coroutine.yield("\r\n") |
| 131 | end |
| 132 | end |
| 133 | local function send_source(mesgt) |
| 134 | local headers = lower_headers(mesgt.headers or {}) |
| 135 | headers['content-type'] = headers['content-type'] or |
| 136 | 'text/plain; charset="iso-8859-1"' |
| 137 | send_headers(headers) |
| 138 | while true do |
| 139 | local chunk, err = mesgt.body() |
| 140 | if err then coroutine.yield(nil, err) |
| 141 | elseif chunk then coroutine.yield(chunk) |
| 142 | else break end |
| 143 | end |
| 144 | end |
| 145 | local function send_string(mesgt) |
| 146 | local headers = lower_headers(mesgt.headers or {}) |
| 147 | headers['content-type'] = headers['content-type'] or |
| 148 | 'text/plain; charset="iso-8859-1"' |
| 149 | send_headers(headers) |
| 150 | coroutine.yield(mesgt.body) |
| 151 | end |
| 152 | function send_message(mesgt) |
| 153 | if base.type(mesgt.body) == "table" then send_multipart(mesgt) |
| 154 | elseif base.type(mesgt.body) == "function" then send_source(mesgt) |
| 155 | else send_string(mesgt) end |
| 156 | end |
| 157 | local function adjust_headers(mesgt) |
| 158 | local lower = lower_headers(mesgt.headers) |
| 159 | lower["date"] = lower["date"] or |
| 160 | os.date("!%a, %d %b %Y %H:%M:%S ") .. (mesgt.zone or _M.ZONE) |
| 161 | lower["x-mailer"] = lower["x-mailer"] or socket._VERSION |
| 162 | lower["mime-version"] = "1.0" |
| 163 | return lower |
| 164 | end |
| 165 | function _M.message(mesgt) |
| 166 | mesgt.headers = adjust_headers(mesgt) |
| 167 | local co = coroutine.create(function() send_message(mesgt) end) |
| 168 | return function() |
| 169 | local ret, a, b = coroutine.resume(co) |
| 170 | if ret then return a, b |
| 171 | else return nil, a end |
| 172 | end |
| 173 | end |
| 174 | _M.send = socket.protect(function(mailt) |
| 175 | local s = _M.open(mailt.server, mailt.port, mailt.create) |
| 176 | local ext = s:greet(mailt.domain) |
| 177 | s:auth(mailt.user, mailt.password, ext) |
| 178 | s:send(mailt) |
| 179 | s:quit() |
| 180 | return s:close() |
| 181 | end) |
| 182 | socket.smtp = _M |
| 183 | return _M |