annotate src/send.json.luan @ 7:ecc665c507b5

minor
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 15 Sep 2024 08:18:08 -0600
parents bd4802730bab
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
bd4802730bab its working
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 local Luan = require "luan:Luan.luan"
bd4802730bab its working
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2 local error = Luan.error
bd4802730bab its working
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 local new_error = Luan.new_error or error()
bd4802730bab its working
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 local stringify = Luan.stringify or error()
bd4802730bab its working
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 local Parsers = require "luan:Parsers.luan"
bd4802730bab its working
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6 local json_parse = Parsers.json_parse or error()
bd4802730bab its working
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 local json_string = Parsers.json_string or error()
bd4802730bab its working
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 local Io = require "luan:Io.luan"
bd4802730bab its working
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 local Http = require "luan:http/Http.luan"
bd4802730bab its working
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 local Mail = require "luan:mail/Mail.luan"
bd4802730bab its working
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 local Logging = require "luan:logging/Logging.luan"
bd4802730bab its working
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 local logger = Logging.logger "send.json"
bd4802730bab its working
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13
bd4802730bab its working
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14
bd4802730bab its working
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 local function user_error(msg)
bd4802730bab its working
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 local e = new_error(msg)
bd4802730bab its working
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 e.error = {
bd4802730bab its working
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 okay = false
bd4802730bab its working
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 error = msg
bd4802730bab its working
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 }
bd4802730bab its working
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 e.throw()
bd4802730bab its working
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 end
bd4802730bab its working
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23
bd4802730bab its working
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 return function()
bd4802730bab its working
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 Io.stdout = Http.response.text_writer()
bd4802730bab its working
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 try
bd4802730bab its working
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 local server = Http.request.parameters.server or user_error "parameter 'server' missing"
bd4802730bab its working
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 local mail = Http.request.parameters.mail or user_error "parameter 'mail' missing"
bd4802730bab its working
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 try
bd4802730bab its working
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 server = json_parse(server)
bd4802730bab its working
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 catch e
bd4802730bab its working
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 user_error "invalid JSON in 'server'"
bd4802730bab its working
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 end
bd4802730bab its working
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 try
bd4802730bab its working
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 mail = json_parse(mail)
bd4802730bab its working
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 catch e
bd4802730bab its working
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 user_error "invalid JSON in 'mail'"
bd4802730bab its working
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38 end
bd4802730bab its working
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39 try
bd4802730bab its working
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40 Mail.sender(server).send(mail)
bd4802730bab its working
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
41 catch e
bd4802730bab its working
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
42 logger.warn(e..stringify(server).."\n"..stringify(mail))
bd4802730bab its working
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
43 user_error(e.get_message())
bd4802730bab its working
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44 end
bd4802730bab its working
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45 %><%=json_string{okay=true}%><%
bd4802730bab its working
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46 catch e
bd4802730bab its working
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 if e.error ~= nil then
bd4802730bab its working
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48 %><%=json_string(e.error)%><%
bd4802730bab its working
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49 return
bd4802730bab its working
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
50 end
bd4802730bab its working
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
51 e.throw()
bd4802730bab its working
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
52 end
bd4802730bab its working
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
53 end