comparison mod_pubsub_hub/mod_pubsub_hub.lua @ 1324:853a382c9bd6

mod_turncredentials: Advertise the XEP-0215 feature (thanks Gryffus)
author Kim Alvefur <zash@zash.se>
date Fri, 28 Feb 2014 15:36:06 +0100
parents 133ee88d19ae
children b21236b6b8d8
comparison
equal deleted inserted replaced
1323:c84ff82658cb 1324:853a382c9bd6
1 -- Copyright (C) 2011 - 2012 Kim Alvefur 1 -- Copyright (C) 2011 - 2012 Kim Alvefur
2 -- 2 --
3 -- This file is MIT/X11 licensed. 3 -- This file is MIT/X11 licensed.
4 4
5 local http = require "net.http"; 5 local http_request, formdecode, formencode = import("net.http", "request", "formdecode", "formencode");
6 local formdecode = http.formdecode;
7 local formencode = http.formencode;
8 local uuid = require "util.uuid".generate; 6 local uuid = require "util.uuid".generate;
9 local hmac_sha1 = require "util.hmac".sha1; 7 local hmac_sha1 = require "util.hmac".sha1;
10 local json_encode = require "util.json".encode; 8 local json_encode = require "util.json".encode;
11 local time = os.time; 9 local time = os.time;
12 local m_min, m_max = math.min, math.max; 10 local m_min, m_max = math.min, math.max;
13 local tostring = tostring; 11 local tostring = tostring;
12
14 local xmlns_pubsub = "http://jabber.org/protocol/pubsub"; 13 local xmlns_pubsub = "http://jabber.org/protocol/pubsub";
15 local xmlns_pubsub_event = xmlns_pubsub .. "#event"; 14 local xmlns_pubsub_event = xmlns_pubsub .. "#event";
16 local subs_by_topic = module:shared"subscriptions"; 15 local subs_by_topic = module:shared"subscriptions";
17 16
18 local max_lease, min_lease, default_lease = 86400, 600, 3600; 17 local max_lease, min_lease, default_lease = 86400, 600, 3600;
89 ["hub.verify_token"] = verify_token, 88 ["hub.verify_token"] = verify_token,
90 } 89 }
91 module:log("debug", require"util.serialization".serialize(verify_modes)); 90 module:log("debug", require"util.serialization".serialize(verify_modes));
92 if verify_modes["async"] then 91 if verify_modes["async"] then
93 module:log("debug", "Sending async verification request to %s for %s", tostring(callback_url), tostring(subscription)); 92 module:log("debug", "Sending async verification request to %s for %s", tostring(callback_url), tostring(subscription));
94 http.request(callback_url, nil, function(body, code) 93 http_request(callback_url, nil, function(body, code)
95 if body == challenge and code > 199 and code < 300 then 94 if body == challenge and code > 199 and code < 300 then
96 if not subscription.want_state then 95 if not subscription.want_state then
97 module:log("warn", "Verification of already verified request, probably"); 96 module:log("warn", "Verification of already verified request, probably");
98 return; 97 return;
99 end 98 end
107 subs_by_topic[topic][callback] = subscription; 106 subs_by_topic[topic][callback] = subscription;
108 end 107 end
109 end) 108 end)
110 return 202; 109 return 202;
111 elseif verify_modes["sync"] then 110 elseif verify_modes["sync"] then
112 http.request(callback_url, nil, function(body, code) 111 http_request(callback_url, nil, function(body, code)
113 if body == challenge and code > 199 and code < 300 then 112 if body == challenge and code > 199 and code < 300 then
114 if not subscription.want_state then 113 if not subscription.want_state then
115 module:log("warn", "Verification of already verified request, probably"); 114 module:log("warn", "Verification of already verified request, probably");
116 return; 115 return;
117 end 116 end
159 ["hub.topic"] = topic, 158 ["hub.topic"] = topic,
160 ["hub.challenge"] = challenge, 159 ["hub.challenge"] = challenge,
161 ["hub.lease_seconds"] = subscription.lease_seconds, 160 ["hub.lease_seconds"] = subscription.lease_seconds,
162 ["hub.verify_token"] = subscription.verify_token, 161 ["hub.verify_token"] = subscription.verify_token,
163 } 162 }
164 http.request(callback_url, nil, function(body, code) 163 http_request(callback_url, nil, function(body, code)
165 if body == challenge and code > 199 and code < 300 then 164 if body == challenge and code > 199 and code < 300 then
166 subscription.expires = now + subscription.lease_seconds; 165 subscription.expires = now + subscription.lease_seconds;
167 end 166 end
168 end); 167 end);
169 else 168 else
199 ["Content-Type"] = "application/xml", 198 ["Content-Type"] = "application/xml",
200 }; 199 };
201 if subscription.secret then 200 if subscription.secret then
202 headers["X-Hub-Signature"] = "sha1="..hmac_sha1(subscription.secret, body, true); 201 headers["X-Hub-Signature"] = "sha1="..hmac_sha1(subscription.secret, body, true);
203 end 202 end
204 http.request(subscription.callback, { method = "POST", body = body, headers = headers }, function(body, code) 203 http_request(subscription.callback, { method = "POST", body = body, headers = headers }, function(body, code)
205 if code >= 200 and code <= 299 then 204 if code >= 200 and code <= 299 then
206 module:log("debug", "Delivered"); 205 module:log("debug", "Delivered");
207 else 206 else
208 module:log("warn", "Got status code %d on delivery to %s", tonumber(code) or -1, tostring(subscription.callback)); 207 module:log("warn", "Got status code %d on delivery to %s", tonumber(code) or -1, tostring(subscription.callback));
209 -- TODO Retry 208 -- TODO Retry