comparison mod_auto156/mod_auto156.lua @ 4613:6478442d217f

mod_auto156: Queries for XEP-0156 TXT records and prints in JSON format
author Kim Alvefur <zash@zash.se>
date Sat, 03 Jul 2021 20:18:21 +0200
parents
children 5d494dba9c02
comparison
equal deleted inserted replaced
4612:fe24bda72838 4613:6478442d217f
1 -- Synthesize XEP-0156 JSON from DNS
2 local array = require "util.array";
3 local encodings = require "util.encodings";
4 local json = require "util.json";
5 local promise = require "util.promise";
6
7 local dns = require"net.adns".resolver();
8
9 local function check_dns(domain)
10 return dns:lookup_promise("_xmppconnect." .. domain, "TXT");
11 end
12
13 local function check_domain(domain)
14 return promise.resolve(domain):next(encodings.stringprep.nameprep):next(encodings.idna.to_ascii):next(
15 function(domain_A)
16 if not domain_A then
17 return promise.reject(400);
18 else
19 return domain_A;
20 end
21 end):next(check_dns):next(function(txt)
22 local uris = array();
23 for _, cm in ipairs(txt) do
24 local kind, uri = tostring(cm.txt):match("^_xmpp%-client%-(%w+)=([hpstw]+s?://.*)");
25 if kind then
26 uris:push({rel = "urn:xmpp:alt-connections:" .. kind, href = uri});
27 end
28 end
29 if #uris == 0 then
30 return promise.reject(404);
31 end
32 return uris;
33 end);
34 end
35
36 module:depends("http");
37 module:provides("http", {
38 route = {
39 ["GET /*"] = function(_, domain)
40 return check_domain(domain):next(function(uris)
41 return {headers = {content_type = "application/json"}, body = json.encode({links = uris})};
42 end);
43 end,
44 },
45 });
46
47 function module.command(args)
48 local async = require "util.async";
49 for _, domain in ipairs(args) do
50 print(assert(async.wait_for(check_domain(domain):next(json.encode))));
51 end
52 end