21.06.2025, 06:08
Run as a resident script with 0 sleep time. Received messages and headers will be visible in LM Logs tab.
Code:
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
local server = require('socket').tcp()
server:bind('*', 25)
server:settimeout(60)
server:listen()
local function handleclient(client)
local init = {}
local headers = {}
local message = {}
local state = 'helo'
client:settimeout(3)
client:send('220 localhost\r\n')
local function sendok()
client:send('250 ok\r\n')
end
while true do
local line = client:receive()
-- log(line)
if not line then
break
end
if state == 'helo' then
local head = line:match('^(%w+)%s+')
if head == 'HELO' or head == 'EHLO' then
sendok()
state = 'init'
else
break
end
elseif state == 'init' then
if line == 'DATA' then
state = 'headers'
client:send('354 continue\r\n')
else
if line ~= '' then
init[ #init + 1 ] = line
end
sendok()
end
elseif state == 'headers' then
if line == '' then
state = 'message'
else
local header, value = line:match('^([^:]+):%s+(.+)$')
if header and value then
headers[ header:lower() ] = value
end
end
elseif state == 'message' then
if line == '.' then
state = 'quit'
sendok()
elseif line ~= '' then
message[ #message + 1 ] = line
end
elseif state == 'quit' then
if line == 'QUIT' then
state = 'done'
client:send('221 bye\r\n')
end
break
end
end
if state == 'done' then
log(init)
log(headers)
log(message)
end
client:close()
end
while true do
local client = server:accept()
if client then
handleclient(client)
end
end