view src/send.json.luan @ 9:14932af93327 default tip

minor fix
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 23 Sep 2024 18:35:54 -0600
parents bd4802730bab
children
line wrap: on
line source

local Luan = require "luan:Luan.luan"
local error = Luan.error
local new_error = Luan.new_error or error()
local stringify = Luan.stringify or error()
local Parsers = require "luan:Parsers.luan"
local json_parse = Parsers.json_parse or error()
local json_string = Parsers.json_string or error()
local Io = require "luan:Io.luan"
local Http = require "luan:http/Http.luan"
local Mail = require "luan:mail/Mail.luan"
local Logging = require "luan:logging/Logging.luan"
local logger = Logging.logger "send.json"


local function user_error(msg)
	local e = new_error(msg)
	e.error = {
		okay = false
		error = msg
	}
	e.throw()
end

return function()
	Io.stdout = Http.response.text_writer()
	try 
		local server = Http.request.parameters.server or user_error "parameter 'server' missing"
		local mail = Http.request.parameters.mail or user_error "parameter 'mail' missing"
		try
			server = json_parse(server)
		catch e
			user_error "invalid JSON in 'server'"
		end
		try
			mail = json_parse(mail)
		catch e
			user_error "invalid JSON in 'mail'"
		end
		try
			Mail.sender(server).send(mail)
		catch e
			logger.warn(e..stringify(server).."\n"..stringify(mail))
			user_error(e.get_message())
		end
		%><%=json_string{okay=true}%><%
	catch e
		if e.error ~= nil then
			%><%=json_string(e.error)%><%
			return
		end
		e.throw()
	end
end