Skip to content

Instantly share code, notes, and snippets.

@RunsFor
Last active November 20, 2019 13:28
Show Gist options
  • Save RunsFor/5fca2a5c96ecce25cd73edfdac1620f3 to your computer and use it in GitHub Desktop.
Save RunsFor/5fca2a5c96ecce25cd73edfdac1620f3 to your computer and use it in GitHub Desktop.
This snippet tests how smtp client (curl internally) handles error messages from smtp server
#!/usr/bin/env tarantool
local socket = require('socket')
local log = require('log')
local vars = {}
local function smtp_server(s)
s:write('220 localhost ESMTP Tarantool\r\n')
local l
local mail = {rcpt = {}}
while true do
l = s:read('\r\n')
if l:find('EHLO') then
s:write('250-localhost Hello localhost.lan [127.0.0.1]\r\n')
s:write('250-SIZE 52428800\r\n')
s:write('250-8BITMIME\r\n')
s:write('250-PIPELINING\r\n')
s:write('250-CHUNKING\r\n')
s:write('250-PRDR\r\n')
s:write('250 HELP\r\n')
elseif l:find('MAIL FROM:') then
mail.from = l:sub(11):sub(1, -3)
s:write('250 OK\r\n')
elseif l:find('RCPT TO:') then
local rcpt = l:sub(9):sub(1, -3)
mail.rcpt[#mail.rcpt + 1] = rcpt
-- Parse adresses like <510@tarantool.io>
local status = tonumber(rcpt:lower():match('^<(%-?%d+).*@'))
if status and status > 100 and status < 600 then
s:write(tostring(status)
.. ' Reply code was extracted from recipient address\r\n')
else
s:write('250 OK\r\n')
end
elseif l == 'DATA\r\n' then
s:write('354 Enter message, ending with "." on a line by itself\r\n')
while true do
local l = s:read('\r\n')
if l == '.\r\n' then
break
end
mail.text = (mail.text or '') .. l
end
log.info('Got mail:\n\n%s', require('yaml').encode(mail))
s:write('250 OK\r\n')
elseif l:find('QUIT') then
s:write('221 Bye\r\n')
return
elseif l ~= nil then
s:write('502 Not implemented')
else
log.error('Error while reading from socket...')
return
end
end
end
local function init()
-- TODO: Wrap it with background module
socket.tcp_server(
os.getenv('SMTP_LISTEN') or '0.0.0.0',
os.getenv('SMTP_PORT') or '2500',
smtp_server
)
end
init()
local fiber = require('fiber')
local yaml = require('yaml')
local client = require('smtp').new()
fiber.sleep(1)
local ok, res = pcall(client.request, client, 'smtp://localhost:2500', 'from@tarantool.io', 'nulldev@tarantool.io', 'Hi there!')
if ok then
print('SUCCESS, ', yaml.encode(res))
else
print('FAIL, ', yaml.encode(res))
end
ok, res = pcall(client.request, client, 'smtp://localhost:2500', 'from@tarantool.io', '500@tarantool.io', 'Hi there!')
if ok then
print('SUCCESS, ', yaml.encode(res))
else
print('FAIL, ', yaml.encode(res))
end
os.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment