diff src/send.json.luan @ 0:bd4802730bab

its working
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 18 May 2023 21:20:59 -0600
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/send.json.luan	Thu May 18 21:20:59 2023 -0600
@@ -0,0 +1,53 @@
+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