Annotate

plugins/mod_external_services.lua @ 11037:936ee55e1ae3

mod_external_services: Support adding services via items API
author Kim Alvefur <zash@zash.se>
date Sat, 25 Jul 2020 10:22:37 +0200
parent 11036:79e410cd7f6e
child 11038:efefdf71373b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11036
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2 local dt = require "util.datetime";
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3 local base64 = require "util.encodings".base64;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 local hashes = require "util.hashes";
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5 local st = require "util.stanza";
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6 local jid = require "util.jid";
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8 local default_host = module:get_option_string("external_service_host", module.host);
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9 local default_port = module:get_option_number("external_service_port");
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10 local default_secret = module:get_option_string("external_service_secret");
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 local default_ttl = module:get_option_number("external_service_ttl", 86400);
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 local configured_services = module:get_option_array("external_services", {});
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15 local access = module:get_option_set("external_service_access", {});
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 -- filter config into well-defined service records
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 local function prepare(item)
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 if type(item) ~= "table" then
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20 module:log("error", "Service definition is not a table: %q", item);
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 return nil;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22 end
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 local srv = {
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 type = nil;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 transport = nil;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 host = default_host;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 port = default_port;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 username = nil;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 password = nil;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 restricted = nil;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32 expires = nil;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33 };
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 if type(item.type) == "string" then
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 srv.type = item.type;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 else
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 module:log("error", "Service missing mandatory 'type' field: %q", item);
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39 return nil;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 end
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41 if type(item.transport) == "string" then
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 srv.transport = item.transport;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43 end
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44 if type(item.host) == "string" then
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45 srv.host = item.host;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46 end
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47 if type(item.port) == "number" then
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48 srv.port = item.port;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49 end
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
50 if type(item.username) == "string" then
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
51 srv.username = item.username;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52 end
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
53 if type(item.password) == "string" then
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
54 srv.password = item.password;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
55 srv.restricted = true;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
56 end
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
57 if item.restricted == true then
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58 srv.restricted = true;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59 end
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
60 if type(item.expires) == "number" then
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
61 srv.expires = item.expires;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
62 elseif type(item.ttl) == "number" then
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
63 srv.expires = os.time() + item.ttl;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
64 end
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
65 if (item.secret == true and default_secret) or type(item.secret) == "string" then
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
66 local ttl = default_ttl;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
67 if type(item.ttl) == "number" then
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
68 ttl = item.ttl;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
69 end
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
70 local expires = os.time() + ttl;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
71 local secret = item.secret;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
72 if secret == true then
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
73 secret = default_secret;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
74 end
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
75 local username;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
76 if type(item.username) == "string" then
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
77 username = string.format("%d:%s", expires, item.username);
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
78 else
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
79 username = string.format("%d", expires);
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
80 end
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
81 srv.username = username;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
82 srv.password = base64.encode(hashes.hmac_sha1(secret, srv.username));
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
83 srv.restricted = true;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
84 end
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
85 return srv;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
86 end
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
87
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
88 function module.load()
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
89 -- Trigger errors on startup
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
90 local services = configured_services / prepare;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
91 if #services == 0 then
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
92 module:log("warn", "No services configured or all had errors");
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
93 end
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
94 end
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
95
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
96 local function handle_services(event)
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
97 local origin, stanza = event.origin, event.stanza;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
98 local action = stanza.tags[1];
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
99
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
100 local user_bare = jid.bare(stanza.attr.from);
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
101 local user_host = jid.host(user_bare);
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
102 if not ((access:empty() and origin.type == "c2s") or access:contains(user_bare) or access:contains(user_host)) then
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
103 origin.send(st.error_reply(stanza, "auth", "forbidden"));
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
104 return true;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
105 end
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
106
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
107 local reply = st.reply(stanza):tag("services", { xmlns = action.attr.xmlns });
11037
936ee55e1ae3 mod_external_services: Support adding services via items API
Kim Alvefur <zash@zash.se>
parents: 11036
diff changeset
108 local extras = module:get_host_items("external_service");
936ee55e1ae3 mod_external_services: Support adding services via items API
Kim Alvefur <zash@zash.se>
parents: 11036
diff changeset
109 local services = ( configured_services + extras ) / prepare;
11036
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
110
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
111 local requested_type = action.attr.type;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
112 if requested_type then
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
113 services:filter(function(item)
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
114 return item.type == requested_type;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
115 end);
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
116 end
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
117
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
118 module:fire_event("external_service/services", {
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
119 origin = origin;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
120 stanza = stanza;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
121 reply = reply;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
122 requested_type = requested_type;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
123 services = services;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
124 });
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
125
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
126 for _, srv in ipairs(services) do
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
127 reply:tag("service", {
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
128 type = srv.type;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
129 transport = srv.transport;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
130 host = srv.host;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
131 port = srv.port and string.format("%d", srv.port) or nil;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
132 username = srv.username;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
133 password = srv.password;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
134 expires = srv.expires and dt.datetime(srv.expires) or nil;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
135 restricted = srv.restricted and "1" or nil;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
136 }):up();
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
137 end
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
138
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
139 origin.send(reply);
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
140 return true;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
141 end
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
142
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
143 local function handle_credentials(event)
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
144 local origin, stanza = event.origin, event.stanza;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
145 local action = stanza.tags[1];
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
146
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
147 if origin.type ~= "c2s" then
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
148 origin.send(st.error_reply(stanza, "auth", "forbidden"));
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
149 return true;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
150 end
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
151
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
152 local reply = st.reply(stanza):tag("credentials", { xmlns = action.attr.xmlns });
11037
936ee55e1ae3 mod_external_services: Support adding services via items API
Kim Alvefur <zash@zash.se>
parents: 11036
diff changeset
153 local extras = module:get_host_items("external_service");
936ee55e1ae3 mod_external_services: Support adding services via items API
Kim Alvefur <zash@zash.se>
parents: 11036
diff changeset
154 local services = ( configured_services + extras ) / prepare;
11036
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
155 services:filter(function (item)
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
156 return item.restricted;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
157 end)
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
158
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
159 local requested_credentials = {};
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
160 for service in action:childtags("service") do
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
161 table.insert(requested_credentials, {
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
162 type = service.attr.type;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
163 host = service.attr.host;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
164 port = tonumber(service.attr.port);
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
165 });
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
166 end
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
167
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
168 module:fire_event("external_service/credentials", {
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
169 origin = origin;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
170 stanza = stanza;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
171 reply = reply;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
172 requested_credentials = requested_credentials;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
173 services = services;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
174 });
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
175
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
176 for req_srv in action:childtags("service") do
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
177 for _, srv in ipairs(services) do
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
178 if srv.type == req_srv.attr.type and srv.host == req_srv.attr.host
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
179 and not req_srv.attr.port or srv.port == tonumber(req_srv.attr.port) then
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
180 reply:tag("service", {
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
181 type = srv.type;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
182 transport = srv.transport;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
183 host = srv.host;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
184 port = srv.port and string.format("%d", srv.port) or nil;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
185 username = srv.username;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
186 password = srv.password;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
187 expires = srv.expires and dt.datetime(srv.expires) or nil;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
188 restricted = srv.restricted and "1" or nil;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
189 }):up();
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
190 end
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
191 end
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
192 end
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
193
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
194 origin.send(reply);
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
195 return true;
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
196 end
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
197
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
198 -- XEP-0215 v0.7
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
199 module:add_feature("urn:xmpp:extdisco:2");
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
200 module:hook("iq-get/host/urn:xmpp:extdisco:2:services", handle_services);
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
201 module:hook("iq-get/host/urn:xmpp:extdisco:2:credentials", handle_credentials);
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
202
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
203 -- COMPAT XEP-0215 v0.6
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
204 -- Those still on the old version gets to deal with undefined attributes until they upgrade.
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
205 module:add_feature("urn:xmpp:extdisco:1");
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
206 module:hook("iq-get/host/urn:xmpp:extdisco:1:services", handle_services);
79e410cd7f6e mod_external_services: XEP-0215: External Service Discovery
Kim Alvefur <zash@zash.se>
parents:
diff changeset
207 module:hook("iq-get/host/urn:xmpp:extdisco:1:credentials", handle_credentials);