Changeset

718:a37e4149ccd1

mod_pubsub_feeds: Prepare for making it possible to unsubscribe, and some other minor changes.
author Kim Alvefur <zash@zash.se>
date Wed, 20 Jun 2012 16:08:47 +0200
parents 717:e79147fb39f9
children 719:5e71e24e33fc
files mod_pubsub_feeds/mod_pubsub_feeds.lua
diffstat 1 files changed, 13 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/mod_pubsub_feeds/mod_pubsub_feeds.lua	Wed Jun 20 15:59:45 2012 +0200
+++ b/mod_pubsub_feeds/mod_pubsub_feeds.lua	Wed Jun 20 16:08:47 2012 +0200
@@ -5,7 +5,7 @@
 -- Config:
 -- Component "pubsub.example.com" "pubsub"
 -- modules_enabled = {
---   "pubsub_feed";
+--   "pubsub_feeds";
 -- }
 -- feeds = { -- node -> url
 --   prosody_blog = "http://blog.prosody.im/feed/atom.xml";
@@ -160,12 +160,13 @@
 	return module:http_url(nil, "/callback") .. "?node=" .. urlencode(node);
 end	
 
-function subscribe(feed)
+function subscribe(feed, want)
+	want = want or "subscribe";
 	feed.token = uuid();
-	feed.secret = uuid();
+	feed.secret = feed.secret or uuid();
 	local body = formencode{
 		["hub.callback"] = format_url(feed.node);
-		["hub.mode"] = "subscribe"; --TODO unsubscribe
+		["hub.mode"] = want;
 		["hub.topic"] = feed.url;
 		["hub.verify"] = "async";
 		["hub.verify_token"] = feed.token;
@@ -176,7 +177,7 @@
 	--module:log("debug", "subscription request, body: %s", body);
 
 	--FIXME The subscription states and related stuff
-	feed.subscription = "subscribe";
+	feed.subscription = want;
 	http.request(feed.hub, { body = body }, function(data, code, req) 
 		module:log("debug", "subscription to %s submitted, status %s", feed.node, tostring(code));
 		if code >= 400 then
@@ -200,8 +201,12 @@
 	--module:log("debug", "Headers: %s", dump(request.headers));
 
 	local feed = feed_list[query.node];
+	if not feed then
+		return 404;
+	end
+
 	if method == "GET" then
-		if query.node and feed then
+		if query.node then
 			if query["hub.topic"] ~= feed.url then
 				module:log("debug", "Invalid topic: %s", tostring(query["hub.topic"]))
 				return 404
@@ -216,15 +221,14 @@
 			end
 			if query["hub.verify_token"] ~= feed.token then
 				module:log("debug", "Invalid verify_token: %s", tostring(query["hub.verify_token"]))
-				return 401
+				return 401;
 			end
 			module:log("debug", "Confirming %s request to %s", feed.subscription, feed.url)
 			return query["hub.challenge"];
 		end
 		return 400;
 	elseif method == "POST" then
-		local body = request.body;
-		if #body > 0 and feed then
+		if #body > 0 then
 			module:log("debug", "got %d bytes PuSHed for %s", #body, query.node);
 			local signature = request.headers.x_hub_signature;
 			if feed.secret then