Last active
November 20, 2019 13:28
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 <[email protected]> | |
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', '[email protected]', '[email protected]', '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', '[email protected]', '[email protected]', '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