annotate mod_pubsub_feed/mod_pubsub_feed.lua @ 498:2865eaf43c74

mod_ircd: remove some duped code.
author Marco Cirillo <maranda@lightwitch.org>
date Mon, 05 Dec 2011 21:31:03 +0000
parents db5702bb9e41
children 0d3174d5a1cc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
278
653c1826739e mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 -- Fetches Atom feeds and publishes to PubSub nodes
653c1826739e mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2 --
322
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
3 -- Depends: http://code.matthewwild.co.uk/lua-feeds
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
4 --
278
653c1826739e mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5 -- Config:
653c1826739e mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6 -- Component "pubsub.example.com" "pubsub"
653c1826739e mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7 -- modules_enabled = {
653c1826739e mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8 -- "pubsub_feed";
653c1826739e mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9 -- }
653c1826739e mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10 -- feeds = { -- node -> url
279
aa0df3db4901 mod_pubsub_feed: Wrap entry in a item element.
Kim Alvefur <zash@zash.se>
parents: 278
diff changeset
11 -- prosody_blog = "http://blog.prosody.im/feed/atom.xml";
278
653c1826739e mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 -- }
653c1826739e mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 -- feed_pull_interval = 20 -- minutes
322
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
14 --
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
15 -- Reference
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
16 -- http://pubsubhubbub.googlecode.com/svn/trunk/pubsubhubbub-core-0.3.html
278
653c1826739e mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17
653c1826739e mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 local modules = hosts[module.host].modules;
653c1826739e mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 if not modules.pubsub then
322
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
20 --FIXME Should this throw an error() instead?
278
653c1826739e mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 module:log("warn", "Pubsub needs to be loaded on this host");
653c1826739e mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22 end
322
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
23
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
24
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
25 local t_insert = table.insert;
278
653c1826739e mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 local add_task = require "util.timer".add_task;
653c1826739e mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 local date, time = os.date, os.time;
279
aa0df3db4901 mod_pubsub_feed: Wrap entry in a item element.
Kim Alvefur <zash@zash.se>
parents: 278
diff changeset
28 local dt_parse, dt_datetime = require "util.datetime".parse, require "util.datetime".datetime;
323
433bf7dc3e7a mod_pubsub_feed: Stricter verification handling. Correctly echo the hubs challenge.
Kim Alvefur <zash@zash.se>
parents: 322
diff changeset
29 local uuid = require "util.uuid".generate;
325
4e50e591a7fc mod_pubsub_feed: Implement signature verification
Kim Alvefur <zash@zash.se>
parents: 324
diff changeset
30 local hmac_sha1 = require "util.hmac".sha1;
403
fc62b26dfdf6 mod_pubsub_feed: Major cleanup, and use newer APIs. (Thanks Maranda)
Kim Alvefur <zash@zash.se>
parents: 402
diff changeset
31 local parse_feed = require "feeds".feed_from_string;
fc62b26dfdf6 mod_pubsub_feed: Major cleanup, and use newer APIs. (Thanks Maranda)
Kim Alvefur <zash@zash.se>
parents: 402
diff changeset
32 local st = require "util.stanza";
322
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
33
403
fc62b26dfdf6 mod_pubsub_feed: Major cleanup, and use newer APIs. (Thanks Maranda)
Kim Alvefur <zash@zash.se>
parents: 402
diff changeset
34 local http = require "net.http";
fc62b26dfdf6 mod_pubsub_feed: Major cleanup, and use newer APIs. (Thanks Maranda)
Kim Alvefur <zash@zash.se>
parents: 402
diff changeset
35 local httpserver = require "net.httpserver";
fc62b26dfdf6 mod_pubsub_feed: Major cleanup, and use newer APIs. (Thanks Maranda)
Kim Alvefur <zash@zash.se>
parents: 402
diff changeset
36 local formdecode = http.formdecode;
fc62b26dfdf6 mod_pubsub_feed: Major cleanup, and use newer APIs. (Thanks Maranda)
Kim Alvefur <zash@zash.se>
parents: 402
diff changeset
37 local formencode = http.formencode;
fc62b26dfdf6 mod_pubsub_feed: Major cleanup, and use newer APIs. (Thanks Maranda)
Kim Alvefur <zash@zash.se>
parents: 402
diff changeset
38 local urldecode = http.urldecode;
fc62b26dfdf6 mod_pubsub_feed: Major cleanup, and use newer APIs. (Thanks Maranda)
Kim Alvefur <zash@zash.se>
parents: 402
diff changeset
39 local urlencode = http.urlencode;
278
653c1826739e mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40
475
db5702bb9e41 mod_pubsub_feed: Dynamicaly reloadable config.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
41 local feed_list = {};
db5702bb9e41 mod_pubsub_feed: Dynamicaly reloadable config.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
42 local refresh_interval;
db5702bb9e41 mod_pubsub_feed: Dynamicaly reloadable config.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
43
db5702bb9e41 mod_pubsub_feed: Dynamicaly reloadable config.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
44 -- Dynamicaly reloadable config.
db5702bb9e41 mod_pubsub_feed: Dynamicaly reloadable config.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
45 local function update_config()
db5702bb9e41 mod_pubsub_feed: Dynamicaly reloadable config.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
46 local config = module:get_option("feeds") or {
db5702bb9e41 mod_pubsub_feed: Dynamicaly reloadable config.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
47 planet_jabber = "http://planet.jabber.org/atom.xml";
db5702bb9e41 mod_pubsub_feed: Dynamicaly reloadable config.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
48 prosody_blog = "http://blog.prosody.im/feed/atom.xml";
db5702bb9e41 mod_pubsub_feed: Dynamicaly reloadable config.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
49 };
db5702bb9e41 mod_pubsub_feed: Dynamicaly reloadable config.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
50 refresh_interval = module:get_option_number("feed_pull_interval", 15) * 60;
db5702bb9e41 mod_pubsub_feed: Dynamicaly reloadable config.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
51 local new_feed_list = {};
db5702bb9e41 mod_pubsub_feed: Dynamicaly reloadable config.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
52 for node, url in pairs(config) do
db5702bb9e41 mod_pubsub_feed: Dynamicaly reloadable config.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
53 new_feed_list[node] = true;
db5702bb9e41 mod_pubsub_feed: Dynamicaly reloadable config.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
54 if not feed_list[node] then
db5702bb9e41 mod_pubsub_feed: Dynamicaly reloadable config.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
55 feed_list[node] = { url = url; node = node; last_update = 0 };
db5702bb9e41 mod_pubsub_feed: Dynamicaly reloadable config.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
56 else
db5702bb9e41 mod_pubsub_feed: Dynamicaly reloadable config.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
57 feed_list[node].url = url;
db5702bb9e41 mod_pubsub_feed: Dynamicaly reloadable config.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
58 end
db5702bb9e41 mod_pubsub_feed: Dynamicaly reloadable config.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
59 end
db5702bb9e41 mod_pubsub_feed: Dynamicaly reloadable config.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
60 for node in pairs(feed_list) do
db5702bb9e41 mod_pubsub_feed: Dynamicaly reloadable config.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
61 if not new_feed_list[node] then
db5702bb9e41 mod_pubsub_feed: Dynamicaly reloadable config.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
62 feed_list[node] = nil;
db5702bb9e41 mod_pubsub_feed: Dynamicaly reloadable config.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
63 end
db5702bb9e41 mod_pubsub_feed: Dynamicaly reloadable config.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
64 end
322
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
65 end
475
db5702bb9e41 mod_pubsub_feed: Dynamicaly reloadable config.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
66 update_config();
db5702bb9e41 mod_pubsub_feed: Dynamicaly reloadable config.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
67 module:hook("config-reloaded", update_config);
322
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
68
405
fe4fdba21a23 mod_pubsub_feed: Kill the timer on module unload
Kim Alvefur <zash@zash.se>
parents: 403
diff changeset
69 -- Used to kill the timer
fe4fdba21a23 mod_pubsub_feed: Kill the timer on module unload
Kim Alvefur <zash@zash.se>
parents: 403
diff changeset
70 local module_unloaded = false;
fe4fdba21a23 mod_pubsub_feed: Kill the timer on module unload
Kim Alvefur <zash@zash.se>
parents: 403
diff changeset
71 function module.unload()
fe4fdba21a23 mod_pubsub_feed: Kill the timer on module unload
Kim Alvefur <zash@zash.se>
parents: 403
diff changeset
72 module_unloaded = true;
fe4fdba21a23 mod_pubsub_feed: Kill the timer on module unload
Kim Alvefur <zash@zash.se>
parents: 403
diff changeset
73 end
fe4fdba21a23 mod_pubsub_feed: Kill the timer on module unload
Kim Alvefur <zash@zash.se>
parents: 403
diff changeset
74
475
db5702bb9e41 mod_pubsub_feed: Dynamicaly reloadable config.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
75 -- Config stuff that can't be reloaded, since it would need to re-bind HTTP stuff.
db5702bb9e41 mod_pubsub_feed: Dynamicaly reloadable config.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
76
db5702bb9e41 mod_pubsub_feed: Dynamicaly reloadable config.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
77 -- If module.host IN A doesn't point to this server, use this to override.
db5702bb9e41 mod_pubsub_feed: Dynamicaly reloadable config.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
78 local httphost = module:get_option_string("pubsubhubub_httphost", module.host);
db5702bb9e41 mod_pubsub_feed: Dynamicaly reloadable config.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
79 -- HTTP by default or not?
db5702bb9e41 mod_pubsub_feed: Dynamicaly reloadable config.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
80 local use_pubsubhubub = module:get_option_boolean("use_pubsubhubub", true);
db5702bb9e41 mod_pubsub_feed: Dynamicaly reloadable config.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
81
403
fc62b26dfdf6 mod_pubsub_feed: Major cleanup, and use newer APIs. (Thanks Maranda)
Kim Alvefur <zash@zash.se>
parents: 402
diff changeset
82 -- Thanks to Maranda for this
fc62b26dfdf6 mod_pubsub_feed: Major cleanup, and use newer APIs. (Thanks Maranda)
Kim Alvefur <zash@zash.se>
parents: 402
diff changeset
83 local port, base, ssl = 5280, "callback", false;
fc62b26dfdf6 mod_pubsub_feed: Major cleanup, and use newer APIs. (Thanks Maranda)
Kim Alvefur <zash@zash.se>
parents: 402
diff changeset
84 local ports = module:get_option("feeds_ports") or { port = port, base = base, ssl = ssl };
fc62b26dfdf6 mod_pubsub_feed: Major cleanup, and use newer APIs. (Thanks Maranda)
Kim Alvefur <zash@zash.se>
parents: 402
diff changeset
85 -- FIXME If ports isn't a table, this will cause an error
fc62b26dfdf6 mod_pubsub_feed: Major cleanup, and use newer APIs. (Thanks Maranda)
Kim Alvefur <zash@zash.se>
parents: 402
diff changeset
86 local _, first_port = next(ports); -- We base the callback URL on the first port config
fc62b26dfdf6 mod_pubsub_feed: Major cleanup, and use newer APIs. (Thanks Maranda)
Kim Alvefur <zash@zash.se>
parents: 402
diff changeset
87 if first_port then
fc62b26dfdf6 mod_pubsub_feed: Major cleanup, and use newer APIs. (Thanks Maranda)
Kim Alvefur <zash@zash.se>
parents: 402
diff changeset
88 if type(first_port) == "number" then
fc62b26dfdf6 mod_pubsub_feed: Major cleanup, and use newer APIs. (Thanks Maranda)
Kim Alvefur <zash@zash.se>
parents: 402
diff changeset
89 port = first_port;
fc62b26dfdf6 mod_pubsub_feed: Major cleanup, and use newer APIs. (Thanks Maranda)
Kim Alvefur <zash@zash.se>
parents: 402
diff changeset
90 elseif type(first_port) == "table" then
fc62b26dfdf6 mod_pubsub_feed: Major cleanup, and use newer APIs. (Thanks Maranda)
Kim Alvefur <zash@zash.se>
parents: 402
diff changeset
91 port, base, ssl =
fc62b26dfdf6 mod_pubsub_feed: Major cleanup, and use newer APIs. (Thanks Maranda)
Kim Alvefur <zash@zash.se>
parents: 402
diff changeset
92 first_port.port or port,
fc62b26dfdf6 mod_pubsub_feed: Major cleanup, and use newer APIs. (Thanks Maranda)
Kim Alvefur <zash@zash.se>
parents: 402
diff changeset
93 first_port.path or base,
fc62b26dfdf6 mod_pubsub_feed: Major cleanup, and use newer APIs. (Thanks Maranda)
Kim Alvefur <zash@zash.se>
parents: 402
diff changeset
94 first_port.ssl or ssl;
fc62b26dfdf6 mod_pubsub_feed: Major cleanup, and use newer APIs. (Thanks Maranda)
Kim Alvefur <zash@zash.se>
parents: 402
diff changeset
95 elseif type(first_port) == "string" then
fc62b26dfdf6 mod_pubsub_feed: Major cleanup, and use newer APIs. (Thanks Maranda)
Kim Alvefur <zash@zash.se>
parents: 402
diff changeset
96 base = first_port;
401
c85397063eca mod_pubsub_feed: Try harder to figure out ones callback URL.
Kim Alvefur <zash@zash.se>
parents: 400
diff changeset
97 end
c85397063eca mod_pubsub_feed: Try harder to figure out ones callback URL.
Kim Alvefur <zash@zash.se>
parents: 400
diff changeset
98 end
c85397063eca mod_pubsub_feed: Try harder to figure out ones callback URL.
Kim Alvefur <zash@zash.se>
parents: 400
diff changeset
99
322
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
100 local response_codes = {
323
433bf7dc3e7a mod_pubsub_feed: Stricter verification handling. Correctly echo the hubs challenge.
Kim Alvefur <zash@zash.se>
parents: 322
diff changeset
101 ["200"] = "OK";
322
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
102 ["202"] = "Accepted";
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
103 ["400"] = "Bad Request";
324
100b3ad2e10c mod_pubsub_feed: Fix verify_token checking.
Kim Alvefur <zash@zash.se>
parents: 323
diff changeset
104 ["403"] = "Forbidden";
323
433bf7dc3e7a mod_pubsub_feed: Stricter verification handling. Correctly echo the hubs challenge.
Kim Alvefur <zash@zash.se>
parents: 322
diff changeset
105 ["404"] = "Not Found";
405
fe4fdba21a23 mod_pubsub_feed: Kill the timer on module unload
Kim Alvefur <zash@zash.se>
parents: 403
diff changeset
106 ["500"] = "Internal Server Error";
322
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
107 ["501"] = "Not Implemented";
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
108 };
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
109
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
110 local function http_response(code, headers, body)
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
111 return {
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
112 status = (type(code) == "number" and code .. " " .. response_codes[tostring(code)]) or code;
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
113 headers = headers or {};
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
114 body = body or "<h1>" .. response_codes[tostring(code)] .. "</h1>\n";
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
115 };
278
653c1826739e mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
116 end
653c1826739e mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
117
322
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
118 local actor = module.host.."/"..module.name;
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
119
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
120 function update_entry(item)
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
121 local node = item.node;
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
122 --module:log("debug", "parsing %d bytes of data in node %s", #item.data or 0, node)
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
123 local feed = parse_feed(item.data);
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
124 module:log("debug", "updating node %s", node);
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
125 for _, entry in ipairs(feed) do
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
126 entry.attr.xmlns = "http://www.w3.org/2005/Atom";
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
127
403
fc62b26dfdf6 mod_pubsub_feed: Major cleanup, and use newer APIs. (Thanks Maranda)
Kim Alvefur <zash@zash.se>
parents: 402
diff changeset
128 local e_published = entry:get_child_text("published");
322
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
129 e_published = e_published and dt_parse(e_published);
403
fc62b26dfdf6 mod_pubsub_feed: Major cleanup, and use newer APIs. (Thanks Maranda)
Kim Alvefur <zash@zash.se>
parents: 402
diff changeset
130 local e_updated = entry:get_child_text("updated");
322
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
131 e_updated = e_updated and dt_parse(e_updated);
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
132
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
133 local timestamp = e_updated or e_published or nil;
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
134 --module:log("debug", "timestamp is %s, item.last_update is %s", tostring(timestamp), tostring(item.last_update));
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
135 if not timestamp or not item.last_update or timestamp > item.last_update then
403
fc62b26dfdf6 mod_pubsub_feed: Major cleanup, and use newer APIs. (Thanks Maranda)
Kim Alvefur <zash@zash.se>
parents: 402
diff changeset
136 local id = entry:get_child_text("id");
fc62b26dfdf6 mod_pubsub_feed: Major cleanup, and use newer APIs. (Thanks Maranda)
Kim Alvefur <zash@zash.se>
parents: 402
diff changeset
137 id = id or item.url.."#"..dt_datetime(timestamp); -- Missing id, so make one up
322
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
138 local xitem = st.stanza("item", { id = id }):add_child(entry);
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
139 -- TODO Put data from /feed into item/source
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
140
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
141 module:log("debug", "publishing to %s, id %s", node, id);
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
142 local ok, err = modules.pubsub.service:publish(node, actor, id, xitem);
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
143 if not ok then
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
144 if err == "item-not-found" then -- try again
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
145 module:log("debug", "got item-not-found, creating %s and trying again", node);
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
146 local ok, err = modules.pubsub.service:create(node, actor);
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
147 if not ok then
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
148 module:log("error", "could not create node %s: %s", node, err);
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
149 return;
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
150 end
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
151 local ok, err = modules.pubsub.service:publish(node, actor, id, xitem);
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
152 if not ok then
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
153 module:log("error", "could not create or publish node %s: %s", node, err);
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
154 return
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
155 end
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
156 else
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
157 module:log("error", "publishing %s failed: %s", node, err);
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
158 end
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
159 end
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
160 end
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
161 end
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
162
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
163 if use_pubsubhubub and not item.subscription then
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
164 module:log("debug", "check if %s has a hub", item.node);
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
165 local hub = feed.links and feed.links.hub;
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
166 if hub then
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
167 item.hub = hub;
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
168 module:log("debug", "%s has a hub: %s", item.node, item.hub);
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
169 subscribe(item);
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
170 end
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
171 end
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
172 end
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
173
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
174 function fetch(item, callback) -- HTTP Pull
278
653c1826739e mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
175 local headers = { };
653c1826739e mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
176 if item.data and item.last_update then
400
f42fe4229f8a mod_pubsub_feed: Fix timestamp on Windows, which has no %T
Kim Alvefur <zash@zash.se>
parents: 325
diff changeset
177 headers["If-Modified-Since"] = date("!%a, %d %b %Y %H:%M:%S %Z", item.last_update);
f42fe4229f8a mod_pubsub_feed: Fix timestamp on Windows, which has no %T
Kim Alvefur <zash@zash.se>
parents: 325
diff changeset
178 --COMPAT We could have saved 6 bytes here, but Microsoft apparently hates %T, so you got this gigantic comment instead.
278
653c1826739e mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
179 end
322
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
180 http.request(item.url, { headers = headers }, function(data, code, req)
278
653c1826739e mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
181 if code == 200 then
653c1826739e mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
182 item.data = data;
322
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
183 if callback then callback(item) end
278
653c1826739e mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
184 item.last_update = time();
653c1826739e mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
185 end
653c1826739e mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
186 if code == 304 then
653c1826739e mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
187 item.last_update = time();
653c1826739e mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
188 end
653c1826739e mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
189 end);
653c1826739e mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
190 end
653c1826739e mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
191
322
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
192 function refresh_feeds()
475
db5702bb9e41 mod_pubsub_feed: Dynamicaly reloadable config.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
193 local now = time();
405
fe4fdba21a23 mod_pubsub_feed: Kill the timer on module unload
Kim Alvefur <zash@zash.se>
parents: 403
diff changeset
194 if module_unloaded then return end
322
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
195 --module:log("debug", "Refreshing feeds");
278
653c1826739e mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
196 for node, item in pairs(feed_list) do
322
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
197 --FIXME Don't fetch feeds which have a subscription
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
198 -- Otoho, what if the subscription expires or breaks?
475
db5702bb9e41 mod_pubsub_feed: Dynamicaly reloadable config.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
199 if item.last_update + refresh_interval < now then
322
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
200 module:log("debug", "checking %s", item.node);
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
201 fetch(item, update_entry);
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
202 end
278
653c1826739e mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
203 end
653c1826739e mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
204 return refresh_interval;
653c1826739e mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
205 end
653c1826739e mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
206
401
c85397063eca mod_pubsub_feed: Try harder to figure out ones callback URL.
Kim Alvefur <zash@zash.se>
parents: 400
diff changeset
207 local function format_url(secure, host, port, path, node)
c85397063eca mod_pubsub_feed: Try harder to figure out ones callback URL.
Kim Alvefur <zash@zash.se>
parents: 400
diff changeset
208 return ("%s://%s:%d/%s?node=%s"):format(secure and "https" or "http", host, port, path, urlencode(node));
c85397063eca mod_pubsub_feed: Try harder to figure out ones callback URL.
Kim Alvefur <zash@zash.se>
parents: 400
diff changeset
209 end
c85397063eca mod_pubsub_feed: Try harder to figure out ones callback URL.
Kim Alvefur <zash@zash.se>
parents: 400
diff changeset
210
323
433bf7dc3e7a mod_pubsub_feed: Stricter verification handling. Correctly echo the hubs challenge.
Kim Alvefur <zash@zash.se>
parents: 322
diff changeset
211 function subscribe(feed)
324
100b3ad2e10c mod_pubsub_feed: Fix verify_token checking.
Kim Alvefur <zash@zash.se>
parents: 323
diff changeset
212 feed.token = uuid();
325
4e50e591a7fc mod_pubsub_feed: Implement signature verification
Kim Alvefur <zash@zash.se>
parents: 324
diff changeset
213 feed.secret = uuid();
403
fc62b26dfdf6 mod_pubsub_feed: Major cleanup, and use newer APIs. (Thanks Maranda)
Kim Alvefur <zash@zash.se>
parents: 402
diff changeset
214 local body = formencode{
fc62b26dfdf6 mod_pubsub_feed: Major cleanup, and use newer APIs. (Thanks Maranda)
Kim Alvefur <zash@zash.se>
parents: 402
diff changeset
215 ["hub.callback"] = format_url(ssl, httphost, port, base, feed.node);
322
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
216 ["hub.mode"] = "subscribe"; --TODO unsubscribe
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
217 ["hub.topic"] = feed.url;
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
218 ["hub.verify"] = "async";
324
100b3ad2e10c mod_pubsub_feed: Fix verify_token checking.
Kim Alvefur <zash@zash.se>
parents: 323
diff changeset
219 ["hub.verify_token"] = feed.token;
325
4e50e591a7fc mod_pubsub_feed: Implement signature verification
Kim Alvefur <zash@zash.se>
parents: 324
diff changeset
220 ["hub.secret"] = feed.secret;
322
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
221 --["hub.lease_seconds"] = "";
403
fc62b26dfdf6 mod_pubsub_feed: Major cleanup, and use newer APIs. (Thanks Maranda)
Kim Alvefur <zash@zash.se>
parents: 402
diff changeset
222 };
322
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
223
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
224 --module:log("debug", "subscription request, body: %s", body);
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
225
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
226 --FIXME The subscription states and related stuff
323
433bf7dc3e7a mod_pubsub_feed: Stricter verification handling. Correctly echo the hubs challenge.
Kim Alvefur <zash@zash.se>
parents: 322
diff changeset
227 feed.subscription = "subscribe";
322
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
228 http.request(feed.hub, { body = body }, function(data, code, req)
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
229 local code = tostring(code);
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
230 module:log("debug", "subscription to %s submitted, staus %s", feed.node, code);
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
231 end);
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
232 end
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
233
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
234 function handle_http_request(method, body, request)
405
fe4fdba21a23 mod_pubsub_feed: Kill the timer on module unload
Kim Alvefur <zash@zash.se>
parents: 403
diff changeset
235 if module_unloaded then
fe4fdba21a23 mod_pubsub_feed: Kill the timer on module unload
Kim Alvefur <zash@zash.se>
parents: 403
diff changeset
236 module:log("warn", "Received a HTTP request after module unload");
fe4fdba21a23 mod_pubsub_feed: Kill the timer on module unload
Kim Alvefur <zash@zash.se>
parents: 403
diff changeset
237 return http_response(500)
fe4fdba21a23 mod_pubsub_feed: Kill the timer on module unload
Kim Alvefur <zash@zash.se>
parents: 403
diff changeset
238 -- FIXME if this happens.
fe4fdba21a23 mod_pubsub_feed: Kill the timer on module unload
Kim Alvefur <zash@zash.se>
parents: 403
diff changeset
239 end
322
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
240 --module:log("debug", "%s request to %s%s with body %s", method, request.url.path, request.url.query and "?" .. request.url.query or "", #body > 0 and body or "empty");
402
c92a37a72b25 mod_pubsub_feed: Use httpserver.new_from_config(). Fixes SSL. And fix traceback when called without a querypart.
Kim Alvefur <zash@zash.se>
parents: 401
diff changeset
241 local query = request.url.query or {};
322
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
242 if query and type(query) == "string" then
403
fc62b26dfdf6 mod_pubsub_feed: Major cleanup, and use newer APIs. (Thanks Maranda)
Kim Alvefur <zash@zash.se>
parents: 402
diff changeset
243 query = formdecode(query);
322
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
244 --module:log("debug", "GET data: %s", dump(query));
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
245 end
325
4e50e591a7fc mod_pubsub_feed: Implement signature verification
Kim Alvefur <zash@zash.se>
parents: 324
diff changeset
246 --module:log("debug", "Headers: %s", dump(request.headers));
322
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
247
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
248 if method == "GET" then
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
249 if query.node and feed_list[query.node] then
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
250 local feed = feed_list[query.node];
323
433bf7dc3e7a mod_pubsub_feed: Stricter verification handling. Correctly echo the hubs challenge.
Kim Alvefur <zash@zash.se>
parents: 322
diff changeset
251 if query["hub.topic"] ~= feed.url then
433bf7dc3e7a mod_pubsub_feed: Stricter verification handling. Correctly echo the hubs challenge.
Kim Alvefur <zash@zash.se>
parents: 322
diff changeset
252 module:log("debug", "Invalid topic: %s", tostring(query["hub.topic"]))
433bf7dc3e7a mod_pubsub_feed: Stricter verification handling. Correctly echo the hubs challenge.
Kim Alvefur <zash@zash.se>
parents: 322
diff changeset
253 return http_response(404)
322
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
254 end
323
433bf7dc3e7a mod_pubsub_feed: Stricter verification handling. Correctly echo the hubs challenge.
Kim Alvefur <zash@zash.se>
parents: 322
diff changeset
255 if query["hub.mode"] ~= feed.subscription then
433bf7dc3e7a mod_pubsub_feed: Stricter verification handling. Correctly echo the hubs challenge.
Kim Alvefur <zash@zash.se>
parents: 322
diff changeset
256 module:log("debug", "Invalid mode: %s", tostring(query["hub.mode"]))
433bf7dc3e7a mod_pubsub_feed: Stricter verification handling. Correctly echo the hubs challenge.
Kim Alvefur <zash@zash.se>
parents: 322
diff changeset
257 return http_response(400)
433bf7dc3e7a mod_pubsub_feed: Stricter verification handling. Correctly echo the hubs challenge.
Kim Alvefur <zash@zash.se>
parents: 322
diff changeset
258 -- Would this work for unsubscribe?
433bf7dc3e7a mod_pubsub_feed: Stricter verification handling. Correctly echo the hubs challenge.
Kim Alvefur <zash@zash.se>
parents: 322
diff changeset
259 -- Also, if feed.subscription is changed here,
433bf7dc3e7a mod_pubsub_feed: Stricter verification handling. Correctly echo the hubs challenge.
Kim Alvefur <zash@zash.se>
parents: 322
diff changeset
260 -- it would probably invalidate the subscription
433bf7dc3e7a mod_pubsub_feed: Stricter verification handling. Correctly echo the hubs challenge.
Kim Alvefur <zash@zash.se>
parents: 322
diff changeset
261 -- when/if the hub asks if it should be renewed
433bf7dc3e7a mod_pubsub_feed: Stricter verification handling. Correctly echo the hubs challenge.
Kim Alvefur <zash@zash.se>
parents: 322
diff changeset
262 end
324
100b3ad2e10c mod_pubsub_feed: Fix verify_token checking.
Kim Alvefur <zash@zash.se>
parents: 323
diff changeset
263 if query["hub.verify_token"] ~= feed.token then
100b3ad2e10c mod_pubsub_feed: Fix verify_token checking.
Kim Alvefur <zash@zash.se>
parents: 323
diff changeset
264 module:log("debug", "Invalid verify_token: %s", tostring(query["hub.verify_token"]))
100b3ad2e10c mod_pubsub_feed: Fix verify_token checking.
Kim Alvefur <zash@zash.se>
parents: 323
diff changeset
265 return http_response(403)
323
433bf7dc3e7a mod_pubsub_feed: Stricter verification handling. Correctly echo the hubs challenge.
Kim Alvefur <zash@zash.se>
parents: 322
diff changeset
266 end
433bf7dc3e7a mod_pubsub_feed: Stricter verification handling. Correctly echo the hubs challenge.
Kim Alvefur <zash@zash.se>
parents: 322
diff changeset
267 module:log("debug", "Confirming %s request to %s", feed.subscription, feed.url)
433bf7dc3e7a mod_pubsub_feed: Stricter verification handling. Correctly echo the hubs challenge.
Kim Alvefur <zash@zash.se>
parents: 322
diff changeset
268 return http_response(200, nil, query["hub.challenge"])
322
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
269 end
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
270 return http_response(400);
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
271 elseif method == "POST" then
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
272 if #body > 0 and feed_list[query.node] then
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
273 module:log("debug", "got %d bytes PuSHed for %s", #body, query.node);
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
274 local feed = feed_list[query.node];
325
4e50e591a7fc mod_pubsub_feed: Implement signature verification
Kim Alvefur <zash@zash.se>
parents: 324
diff changeset
275 local signature = request.headers["x-hub-signature"];
4e50e591a7fc mod_pubsub_feed: Implement signature verification
Kim Alvefur <zash@zash.se>
parents: 324
diff changeset
276 if feed.secret then
4e50e591a7fc mod_pubsub_feed: Implement signature verification
Kim Alvefur <zash@zash.se>
parents: 324
diff changeset
277 local localsig = "sha1=" .. hmac_sha1(feed.secret, body, true);
4e50e591a7fc mod_pubsub_feed: Implement signature verification
Kim Alvefur <zash@zash.se>
parents: 324
diff changeset
278 if localsig ~= signature then
4e50e591a7fc mod_pubsub_feed: Implement signature verification
Kim Alvefur <zash@zash.se>
parents: 324
diff changeset
279 module:log("debug", "Invalid signature");
4e50e591a7fc mod_pubsub_feed: Implement signature verification
Kim Alvefur <zash@zash.se>
parents: 324
diff changeset
280 return http_response(403);
4e50e591a7fc mod_pubsub_feed: Implement signature verification
Kim Alvefur <zash@zash.se>
parents: 324
diff changeset
281 end
4e50e591a7fc mod_pubsub_feed: Implement signature verification
Kim Alvefur <zash@zash.se>
parents: 324
diff changeset
282 module:log("debug", "Valid signature");
4e50e591a7fc mod_pubsub_feed: Implement signature verification
Kim Alvefur <zash@zash.se>
parents: 324
diff changeset
283 end
322
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
284 feed.data = body;
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
285 update_entry(feed);
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
286 feed.last_update = time();
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
287 return http_response(202);
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
288 end
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
289 return http_response(400);
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
290 end
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
291 return http_response(501);
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
292 end
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
293
278
653c1826739e mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
294 function init()
322
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
295 module:log("debug", "initiating", module.name);
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
296 if use_pubsubhubub then
403
fc62b26dfdf6 mod_pubsub_feed: Major cleanup, and use newer APIs. (Thanks Maranda)
Kim Alvefur <zash@zash.se>
parents: 402
diff changeset
297 module:log("debug", "Starting http server on %s", format_url(ssl, httphost, port, base, "NODE"));
402
c92a37a72b25 mod_pubsub_feed: Use httpserver.new_from_config(). Fixes SSL. And fix traceback when called without a querypart.
Kim Alvefur <zash@zash.se>
parents: 401
diff changeset
298 httpserver.new_from_config( ports, handle_http_request, { base = "callback" } );
322
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
299 end
278
653c1826739e mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
300 add_task(0, refresh_feeds);
653c1826739e mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
301 end
653c1826739e mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
302
653c1826739e mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
303 if prosody.start_time then -- already started
653c1826739e mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
304 init();
653c1826739e mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
305 else
403
fc62b26dfdf6 mod_pubsub_feed: Major cleanup, and use newer APIs. (Thanks Maranda)
Kim Alvefur <zash@zash.se>
parents: 402
diff changeset
306 module:hook_global("server-started", init);
278
653c1826739e mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
307 end