annotate mod_pubsub_hub/mod_pubsub_hub.lua @ 768:af06e69c807a

mod_admin_web: Update dependency versions
author Florian Zeitz <florob@babelmonkeys.de>
date Thu, 02 Aug 2012 20:44:40 +0200
parents e5667f1da6bf
children e781e63a49f4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
764
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 -- Copyright (C) 2011 - 2012 Kim Alvefur
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2 --
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3 -- This file is MIT/X11 licensed.
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5 local http = require "net.http";
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6 local formdecode = http.formdecode;
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7 local formencode = http.formencode;
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8 local uuid = require "util.uuid".generate;
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9 local hmac_sha1 = require "util.hmac".sha1;
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10 local json_encode = require "util.json".encode;
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 local time = os.time;
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 local m_min, m_max = math.min, math.max;
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 local tostring = tostring;
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 local xmlns_pubsub = "http://jabber.org/protocol/pubsub";
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15 local xmlns_pubsub_event = xmlns_pubsub .. "#event";
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16 local subs_by_topic = module:shared"subscriptions";
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 local max_lease, min_lease, default_lease = 86400, 600, 3600;
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20 module:depends"pubsub";
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22 local valid_modes = { ["subscribe"] = true, ["unsubscribe"] = true, }
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 local function do_subscribe(subscription)
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 -- FIXME handle other states
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 if subscription.state == "subscribed" then
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 local ok, err = hosts[module.host].modules.pubsub.service:add_subscription(subscription.topic, true, module.host);
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 module:log(ok and "debug" or "error", "add_subscription() => %s, %s", tostring(ok), tostring(err));
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 end
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 end
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32 local function handle_request(event)
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33 local request, response = event.request, event.response;
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34 local method, body = request.method, request.body;
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 local query = request.url.query or {};
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 if query and type(query) == "string" then
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 query = formdecode(query);
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39 end
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 if body and request.headers.content_type == "application/x-www-form-urlencoded" then
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41 body = formdecode(body);
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 end
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44 if method == "POST" then
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45 -- Subscription request
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46 if body["hub.callback"] and body["hub.mode"] and valid_modes[body["hub.mode"]]
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47 and body["hub.topic"] and body["hub.verify"] then
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49 -- http://pubsubhubbub.googlecode.com/svn/trunk/pubsubhubbub-core-0.3.html#anchor5
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
50 local callback = body["hub.callback"];
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
51 local mode = body["hub.mode"];
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52 local topic = body["hub.topic"];
767
e5667f1da6bf mod_pubsub_hub: Enforce minimal lease time
Kim Alvefur <zash@zash.se>
parents: 766
diff changeset
53 local lease_seconds = m_max(min_lease, m_min(tonumber(body["hub.lease_seconds"]) or default_lease, max_lease));
764
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
54 local secret = body["hub.secret"];
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
55 local verify_token = body["hub.verify_token"];
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
56
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
57 module:log("debug", "topic is "..(type(topic)=="string" and "%q" or "%s"), topic);
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59 if not subs_by_topic[topic] then
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
60 subs_by_topic[topic] = {};
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
61 end
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
62 local subscription = subs_by_topic[topic][callback];
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
63
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
64 local verify_modes = {};
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
65 for i=1,#body do
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
66 if body[i].name == "hub.verify" then
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
67 verify_modes[body[i].value] = true;
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
68 end
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
69 end
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
70
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
71 subscription = subscription or {
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
72 id = uuid(),
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
73 callback = callback,
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
74 topic = topic,
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
75 state = "unsubscribed",
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
76 secret = secret,
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
77 want_state = mode,
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
78 lease_seconds = lease_seconds,
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
79 expires = time() + lease_seconds,
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
80 };
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
81 subs_by_topic[topic][callback] = subscription;
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
82 local challenge = uuid();
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
83
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
84 local callback_url = callback .. (callback:match("%?") and "&" or "?") .. formencode{
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
85 ["hub.mode"] = mode,
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
86 ["hub.topic"] = topic,
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
87 ["hub.challenge"] = challenge,
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
88 ["hub.lease_seconds"] = tostring(lease_seconds),
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
89 ["hub.verify_token"] = verify_token,
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
90 }
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
91 module:log("debug", require"util.serialization".serialize(verify_modes));
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
92 if verify_modes["async"] then
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
93 module:log("debug", "Sending async verification request to %s for %s", tostring(callback_url), tostring(subscription));
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
94 http.request(callback_url, nil, function(body, code)
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
95 if body == challenge and code > 199 and code < 300 then
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
96 if not subscription.want_state then
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
97 module:log("warn", "Verification of already verified request, probably");
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
98 return;
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
99 end
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
100 subscription.state = subscription.want_state .. "d";
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
101 subscription.want_state = nil;
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
102 module:log("debug", "calling do_subscribe()");
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
103 do_subscribe(subscription);
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
104 subs_by_topic[topic][callback] = subscription;
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
105 else
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
106 module:log("warn", "status %d and body was %q", tostring(code), tostring(body));
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
107 subs_by_topic[topic][callback] = subscription;
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
108 end
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
109 end)
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
110 return 202;
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
111 elseif verify_modes["sync"] then
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
112 http.request(callback_url, nil, function(body, code)
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
113 if body == challenge and code > 199 and code < 300 then
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
114 if not subscription.want_state then
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
115 module:log("warn", "Verification of already verified request, probably");
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
116 return;
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
117 end
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
118 if mode == "unsubscribe" then
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
119 subs_by_topic[topic][callback] = nil;
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
120 else
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
121 subscription.state = subscription.want_state .. "d";
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
122 subscription.want_state = nil;
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
123 module:log("debug", "calling do_subscribe()");
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
124 do_subscribe(subscription);
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
125 subs_by_topic[topic][callback] = subscription;
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
126 end
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
127 else
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
128 subs_by_topic[topic][callback] = subscription;
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
129 end
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
130 response.status = 204;
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
131 response:send();
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
132 end)
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
133 return true;
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
134 end
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
135 return 400;
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
136 else
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
137 response.status = 400;
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
138 response.headers.content_type = "text/html";
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
139 return "<h1>Bad Request</h1>\n<a href='http://pubsubhubbub.googlecode.com/svn/trunk/pubsubhubbub-core-0.3.html#anchor5'>Missing required parameter(s)</a>\n"
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
140 end
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
141 end
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
142 end
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
143
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
144 local function periodic()
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
145 local now = time();
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
146 local next_check = now + max_lease;
766
1184fe8ebb21 mod_pubsub_hub: Try to choose time until the next periodic check in a sane way
Kim Alvefur <zash@zash.se>
parents: 764
diff changeset
147 local purge = false;
764
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
148 for topic, callbacks in pairs(subs_by_topic) do
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
149 for callback, subscription in pairs(callbacks) do
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
150 if subscription.mode == "subscribed" then
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
151 if subscription.expires < now then
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
152 -- Subscription has expired, drop it.
766
1184fe8ebb21 mod_pubsub_hub: Try to choose time until the next periodic check in a sane way
Kim Alvefur <zash@zash.se>
parents: 764
diff changeset
153 purge = true;
1184fe8ebb21 mod_pubsub_hub: Try to choose time until the next periodic check in a sane way
Kim Alvefur <zash@zash.se>
parents: 764
diff changeset
154 elseif subscription.expires < now + min_lease then
764
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
155 -- Subscription set to expire soon, re-confirm it.
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
156 local challenge = uuid();
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
157 local callback_url = callback .. (callback:match("%?") and "&" or "?") .. formencode{
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
158 ["hub.mode"] = subscription.state,
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
159 ["hub.topic"] = topic,
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
160 ["hub.challenge"] = challenge,
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
161 ["hub.lease_seconds"] = subscription.lease_seconds,
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
162 ["hub.verify_token"] = subscription.verify_token,
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
163 }
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
164 http.request(callback_url, nil, function(body, code)
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
165 if body == challenge and code > 199 and code < 300 then
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
166 subscription.expires = now + subscription.lease_seconds;
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
167 end
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
168 end);
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
169 else
766
1184fe8ebb21 mod_pubsub_hub: Try to choose time until the next periodic check in a sane way
Kim Alvefur <zash@zash.se>
parents: 764
diff changeset
170 next_check = m_min(next_check, subscription.expires);
764
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
171 end
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
172 end
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
173 end
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
174 if purge then
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
175 local new_callbacks = {};
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
176 for callback, subscription in pairs(callbacks) do
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
177 if (subscription.state == "subscribed" and subscription.expires < now)
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
178 and subscription.want_state ~= "remove" then
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
179 new_callbacks[callback] = subscription;
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
180 end
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
181 end
766
1184fe8ebb21 mod_pubsub_hub: Try to choose time until the next periodic check in a sane way
Kim Alvefur <zash@zash.se>
parents: 764
diff changeset
182 subs_by_topic[topic] = new_callbacks;
1184fe8ebb21 mod_pubsub_hub: Try to choose time until the next periodic check in a sane way
Kim Alvefur <zash@zash.se>
parents: 764
diff changeset
183 purge = false;
764
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
184 end
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
185 end
766
1184fe8ebb21 mod_pubsub_hub: Try to choose time until the next periodic check in a sane way
Kim Alvefur <zash@zash.se>
parents: 764
diff changeset
186 return now - m_max(next_check - min_lease, min_lease);
764
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
187 end
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
188
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
189 local function on_notify(subscription, content)
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
190 local body = tostring(content);
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
191 local headers = {
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
192 ["Content-Type"] = "application/xml",
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
193 };
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
194 if subscription.secret then
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
195 headers["X-Hub-Signature"] = "sha1="..hmac_sha1(subscription.secret, body, true);
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
196 end
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
197 http.request(subscription.callback, { method = "POST", body = body, headers = headers }, function(body, code)
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
198 if code >= 200 and code <= 299 then
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
199 module:log("debug", "Delivered");
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
200 else
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
201 module:log("warn", "Got status code %d on delivery to %s", tonumber(code) or -1, tostring(subscription.callback));
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
202 -- TODO Retry
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
203 -- ... but the spec says that you should not retry, wtf?
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
204 end
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
205 end);
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
206 end
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
207
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
208 module:hook("message/host", function(event)
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
209 local stanza = event.stanza;
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
210 if stanza.attr.from ~= module.host then return end;
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
211
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
212 for pubsub_event in stanza:childtags("event", xmlns_pubsub_event) do
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
213 local items = pubsub_event:get_child("items");
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
214 local node = items.attr.node;
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
215 if items and node and subs_by_topic[node] then
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
216 for item in items:childtags("item") do
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
217 local content = item.tags[1];
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
218 for callback, subscription in pairs(subs_by_topic[node]) do
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
219 on_notify(subscription, content)
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
220 end
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
221 end
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
222 end
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
223 end
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
224 return true;
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
225 end, 10);
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
226
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
227 module:depends"http";
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
228 module:provides("http", {
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
229 default_path = "/hub";
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
230 route = {
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
231 POST = handle_request;
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
232 GET = function()
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
233 return json_encode(subs_by_topic);
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
234 end;
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
235 ["GET /topic/*"] = function(event, path)
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
236 return json_encode(subs_by_topic[path])
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
237 end;
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
238 };
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
239 });
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
240
d11d91ee81ed mod_pubsub_hub: New module that implements the Hub part of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
241 module:add_timer(1, periodic);