annotate mod_pubsub_feeds/mod_pubsub_feeds.lua @ 5930:acd39d33170e

mod_cloud_notify_extensions: Linkify dependency list
author Kim Alvefur <zash@zash.se>
date Thu, 11 Jul 2024 19:13:18 +0200
parents bc292c84f56c
children
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
792
db27f07342d4 mod_pubsub_feeds: Use reference to mod_pubsub from module:depends
Kim Alvefur <zash@zash.se>
parents: 765
diff changeset
3 local pubsub = module:depends"pubsub";
322
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
4
3047
7c55f05327a2 mod_pubsub_feeds: Use ETag instead of problematic If-Modified-Since
Kim Alvefur <zash@zash.se>
parents: 2645
diff changeset
5 local time = os.time;
1325
b21236b6b8d8 Backed out changeset 853a382c9bd6
Kim Alvefur <zash@zash.se>
parents: 1324
diff changeset
6 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
7 local uuid = require "util.uuid".generate;
1107
04cf0b75fe2a mod_pubsub_feeds: Use HMAC-SHA1 from util.hashes (util.hmac was deprecated)
Kim Alvefur <zash@zash.se>
parents: 983
diff changeset
8 local hmac_sha1 = require "util.hashes".hmac_sha1;
2137
0fe725b50f77 mod_pubsub_feeds.lua: util.xml not uit.xml
Michael Töglhofer <michael@toeglhofer.net>
parents: 2134
diff changeset
9 local parse_xml = require "util.xml".parse;
403
fc62b26dfdf6 mod_pubsub_feed: Major cleanup, and use newer APIs. (Thanks Maranda)
Kim Alvefur <zash@zash.se>
parents: 402
diff changeset
10 local st = require "util.stanza";
2132
b149ea428b81 mod_pubsub_feeds: Switch to use util.xml for parsing feeds and include RSS to Atom translation code from lua-feeds
Kim Alvefur <zash@zash.se>
parents: 1457
diff changeset
11 local translate_rss = module:require("feeds").translate_rss;
668
343b115ebbea mod_pubsub_feed: Cleanup and update to new APIs in 0.9
Kim Alvefur <zash@zash.se>
parents: 633
diff changeset
12
343b115ebbea mod_pubsub_feed: Cleanup and update to new APIs in 0.9
Kim Alvefur <zash@zash.se>
parents: 633
diff changeset
13 local xmlns_atom = "http://www.w3.org/2005/Atom";
343b115ebbea mod_pubsub_feed: Cleanup and update to new APIs in 0.9
Kim Alvefur <zash@zash.se>
parents: 633
diff changeset
14
2132
b149ea428b81 mod_pubsub_feeds: Switch to use util.xml for parsing feeds and include RSS to Atom translation code from lua-feeds
Kim Alvefur <zash@zash.se>
parents: 1457
diff changeset
15 local function parse_feed(data)
4883
68bd8ae9b827 mod_pubsub_feeds: Allow but ignore XML comments and processing instructions
Kim Alvefur <zash@zash.se>
parents: 4282
diff changeset
16 local feed, err = parse_xml(data, { allow_processing_instructions = true; allow_comments = true });
2132
b149ea428b81 mod_pubsub_feeds: Switch to use util.xml for parsing feeds and include RSS to Atom translation code from lua-feeds
Kim Alvefur <zash@zash.se>
parents: 1457
diff changeset
17 if not feed then return feed, err; end
b149ea428b81 mod_pubsub_feeds: Switch to use util.xml for parsing feeds and include RSS to Atom translation code from lua-feeds
Kim Alvefur <zash@zash.se>
parents: 1457
diff changeset
18 if feed.attr.xmlns == xmlns_atom then
b149ea428b81 mod_pubsub_feeds: Switch to use util.xml for parsing feeds and include RSS to Atom translation code from lua-feeds
Kim Alvefur <zash@zash.se>
parents: 1457
diff changeset
19 return feed;
b149ea428b81 mod_pubsub_feeds: Switch to use util.xml for parsing feeds and include RSS to Atom translation code from lua-feeds
Kim Alvefur <zash@zash.se>
parents: 1457
diff changeset
20 elseif feed.attr.xmlns == nil and feed.name == "rss" then
b149ea428b81 mod_pubsub_feeds: Switch to use util.xml for parsing feeds and include RSS to Atom translation code from lua-feeds
Kim Alvefur <zash@zash.se>
parents: 1457
diff changeset
21 return translate_rss(feed);
b149ea428b81 mod_pubsub_feeds: Switch to use util.xml for parsing feeds and include RSS to Atom translation code from lua-feeds
Kim Alvefur <zash@zash.se>
parents: 1457
diff changeset
22 end
b149ea428b81 mod_pubsub_feeds: Switch to use util.xml for parsing feeds and include RSS to Atom translation code from lua-feeds
Kim Alvefur <zash@zash.se>
parents: 1457
diff changeset
23 return nil, "unsupported-format";
b149ea428b81 mod_pubsub_feeds: Switch to use util.xml for parsing feeds and include RSS to Atom translation code from lua-feeds
Kim Alvefur <zash@zash.se>
parents: 1457
diff changeset
24 end
b149ea428b81 mod_pubsub_feeds: Switch to use util.xml for parsing feeds and include RSS to Atom translation code from lua-feeds
Kim Alvefur <zash@zash.se>
parents: 1457
diff changeset
25
5569
1f8c9e27b625 mod_pubsub_feeds: Disable WebSub (formerly PubSubHubbub) by default
Kim Alvefur <zash@zash.se>
parents: 4883
diff changeset
26 local use_pubsubhubub = module:get_option_boolean("use_pubsubhubub", false);
668
343b115ebbea mod_pubsub_feed: Cleanup and update to new APIs in 0.9
Kim Alvefur <zash@zash.se>
parents: 633
diff changeset
27 if use_pubsubhubub then
343b115ebbea mod_pubsub_feed: Cleanup and update to new APIs in 0.9
Kim Alvefur <zash@zash.se>
parents: 633
diff changeset
28 module:depends"http";
343b115ebbea mod_pubsub_feed: Cleanup and update to new APIs in 0.9
Kim Alvefur <zash@zash.se>
parents: 633
diff changeset
29 end
322
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
30
403
fc62b26dfdf6 mod_pubsub_feed: Major cleanup, and use newer APIs. (Thanks Maranda)
Kim Alvefur <zash@zash.se>
parents: 402
diff changeset
31 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
32 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
33 local formencode = http.formencode;
278
653c1826739e mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34
668
343b115ebbea mod_pubsub_feed: Cleanup and update to new APIs in 0.9
Kim Alvefur <zash@zash.se>
parents: 633
diff changeset
35 local feed_list = module:shared("feed_list");
5570
f93b1fc1aa31 mod_pubsub_feeds: Add new interval setting in seconds (old still works)
Kim Alvefur <zash@zash.se>
parents: 5569
diff changeset
36 local legacy_refresh_interval = module:get_option_number("feed_pull_interval", 15);
f93b1fc1aa31 mod_pubsub_feeds: Add new interval setting in seconds (old still works)
Kim Alvefur <zash@zash.se>
parents: 5569
diff changeset
37 local refresh_interval = module:get_option_number("feed_pull_interval_seconds", legacy_refresh_interval*60);
2401
7a1625a84624 mod_pubsub_feeds: Ask for leases that expire after one day (Years after testing this module, I was still subscribed to a GNU Social instance)
Kim Alvefur <zash@zash.se>
parents: 2384
diff changeset
38 local lease_length = tostring(math.floor(module:get_option_number("feed_lease_length", 86400)));
475
db5702bb9e41 mod_pubsub_feed: Dynamicaly reloadable config.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
39
794
5a8fa70faa13 mod_pubsub_feeds: Remove config-reloaded hook, do setup in module.load() instead
Kim Alvefur <zash@zash.se>
parents: 793
diff changeset
40 function module.load()
2377
02532f28b207 mod_pubsub_feeds: Remove default feeds
Kim Alvefur <zash@zash.se>
parents: 2137
diff changeset
41 local config = module:get_option("feeds", { });
797
68a067d5ed3b mod_pubsub_feeds: util.pubsub methods return bool, data
Kim Alvefur <zash@zash.se>
parents: 796
diff changeset
42 local ok, nodes = pubsub.service:get_nodes(true);
68a067d5ed3b mod_pubsub_feeds: util.pubsub methods return bool, data
Kim Alvefur <zash@zash.se>
parents: 796
diff changeset
43 if not ok then nodes = {}; end
475
db5702bb9e41 mod_pubsub_feed: Dynamicaly reloadable config.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
44 local new_feed_list = {};
db5702bb9e41 mod_pubsub_feed: Dynamicaly reloadable config.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
45 for node, url in pairs(config) do
765
82d292e4f30f mod_pubsub_feeds: Use URL as node name if the config index is numeric
Kim Alvefur <zash@zash.se>
parents: 763
diff changeset
46 if type(node) == "number" then
82d292e4f30f mod_pubsub_feeds: Use URL as node name if the config index is numeric
Kim Alvefur <zash@zash.se>
parents: 763
diff changeset
47 node = url;
82d292e4f30f mod_pubsub_feeds: Use URL as node name if the config index is numeric
Kim Alvefur <zash@zash.se>
parents: 763
diff changeset
48 end
475
db5702bb9e41 mod_pubsub_feed: Dynamicaly reloadable config.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
49 new_feed_list[node] = true;
db5702bb9e41 mod_pubsub_feed: Dynamicaly reloadable config.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
50 if not feed_list[node] then
5572
fd1c535dcb92 mod_pubsub_feeds: Create pubsub nodes on module load instead of later
Kim Alvefur <zash@zash.se>
parents: 5571
diff changeset
51 local ok, err = pubsub.service:create(node, true);
5576
c729a38f21b1 mod_pubsub_feeds: Handle node already existing
Kim Alvefur <zash@zash.se>
parents: 5575
diff changeset
52 if ok or err == "conflict" then
5572
fd1c535dcb92 mod_pubsub_feeds: Create pubsub nodes on module load instead of later
Kim Alvefur <zash@zash.se>
parents: 5571
diff changeset
53 feed_list[node] = { url = url; node = node; last_update = 0 };
fd1c535dcb92 mod_pubsub_feeds: Create pubsub nodes on module load instead of later
Kim Alvefur <zash@zash.se>
parents: 5571
diff changeset
54 else
fd1c535dcb92 mod_pubsub_feeds: Create pubsub nodes on module load instead of later
Kim Alvefur <zash@zash.se>
parents: 5571
diff changeset
55 module:log("error", "Could not create node %s: %s", node, err);
fd1c535dcb92 mod_pubsub_feeds: Create pubsub nodes on module load instead of later
Kim Alvefur <zash@zash.se>
parents: 5571
diff changeset
56 end
475
db5702bb9e41 mod_pubsub_feed: Dynamicaly reloadable config.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
57 else
db5702bb9e41 mod_pubsub_feed: Dynamicaly reloadable config.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
58 feed_list[node].url = url;
db5702bb9e41 mod_pubsub_feed: Dynamicaly reloadable config.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
59 end
795
c7f507a190a0 mod_pubsub_feeds: Check if nodes exists in pubsub, make sure they get restored if not. (Thanks Maranda)
Kim Alvefur <zash@zash.se>
parents: 794
diff changeset
60 if not nodes[node] then
c7f507a190a0 mod_pubsub_feeds: Check if nodes exists in pubsub, make sure they get restored if not. (Thanks Maranda)
Kim Alvefur <zash@zash.se>
parents: 794
diff changeset
61 feed_list[node].last_update = 0;
c7f507a190a0 mod_pubsub_feeds: Check if nodes exists in pubsub, make sure they get restored if not. (Thanks Maranda)
Kim Alvefur <zash@zash.se>
parents: 794
diff changeset
62 end
475
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 for node in pairs(feed_list) do
db5702bb9e41 mod_pubsub_feed: Dynamicaly reloadable config.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
65 if not new_feed_list[node] then
db5702bb9e41 mod_pubsub_feed: Dynamicaly reloadable config.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
66 feed_list[node] = nil;
db5702bb9e41 mod_pubsub_feed: Dynamicaly reloadable config.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
67 end
db5702bb9e41 mod_pubsub_feed: Dynamicaly reloadable config.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
68 end
322
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
69 end
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
70
5578
b9821e9a21a2 mod_pubsub_feeds: Pass feed data as argument instead of storing on object
Kim Alvefur <zash@zash.se>
parents: 5577
diff changeset
71 function update_entry(item, data)
322
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
72 local node = item.node;
5578
b9821e9a21a2 mod_pubsub_feeds: Pass feed data as argument instead of storing on object
Kim Alvefur <zash@zash.se>
parents: 5577
diff changeset
73 module:log("debug", "parsing %d bytes of data in node %s", #data or 0, node)
b9821e9a21a2 mod_pubsub_feeds: Pass feed data as argument instead of storing on object
Kim Alvefur <zash@zash.se>
parents: 5577
diff changeset
74 local feed, err = parse_feed(data);
3281
27cc66bf918b mod_pubsub_feeds: Handle feed parsing error (thanks apollo13)
Kim Alvefur <zash@zash.se>
parents: 3280
diff changeset
75 if not feed then
27cc66bf918b mod_pubsub_feeds: Handle feed parsing error (thanks apollo13)
Kim Alvefur <zash@zash.se>
parents: 3280
diff changeset
76 module:log("error", "Could not parse feed %q: %s", item.url, err);
5578
b9821e9a21a2 mod_pubsub_feeds: Pass feed data as argument instead of storing on object
Kim Alvefur <zash@zash.se>
parents: 5577
diff changeset
77 module:log("debug", "Feed data:\n%s\n.", data);
3281
27cc66bf918b mod_pubsub_feeds: Handle feed parsing error (thanks apollo13)
Kim Alvefur <zash@zash.se>
parents: 3280
diff changeset
78 return;
27cc66bf918b mod_pubsub_feeds: Handle feed parsing error (thanks apollo13)
Kim Alvefur <zash@zash.se>
parents: 3280
diff changeset
79 end
2379
bc6e63ab2a7c mod_pubsub_feeds: Iterate over feed backwards due to most feeds being in reverse chronological order
Kim Alvefur <zash@zash.se>
parents: 2377
diff changeset
80 local entries = {};
2132
b149ea428b81 mod_pubsub_feeds: Switch to use util.xml for parsing feeds and include RSS to Atom translation code from lua-feeds
Kim Alvefur <zash@zash.se>
parents: 1457
diff changeset
81 for entry in feed:childtags("entry") do
2379
bc6e63ab2a7c mod_pubsub_feeds: Iterate over feed backwards due to most feeds being in reverse chronological order
Kim Alvefur <zash@zash.se>
parents: 2377
diff changeset
82 table.insert(entries, entry);
bc6e63ab2a7c mod_pubsub_feeds: Iterate over feed backwards due to most feeds being in reverse chronological order
Kim Alvefur <zash@zash.se>
parents: 2377
diff changeset
83 end
5577
e7792188540a mod_pubsub_feeds: Retrieve only the most recent item to compare
Kim Alvefur <zash@zash.se>
parents: 5576
diff changeset
84 local ok, last_id = pubsub.service:get_last_item(node, true);
2382
f159ad7ac18a mod_pubsub_feeds: Check if node exists earlier and attempt to create it if not
Kim Alvefur <zash@zash.se>
parents: 2381
diff changeset
85 if not ok then
5577
e7792188540a mod_pubsub_feeds: Retrieve only the most recent item to compare
Kim Alvefur <zash@zash.se>
parents: 5576
diff changeset
86 module:log("error", "PubSub node %q missing: %s", node, last_id);
5572
fd1c535dcb92 mod_pubsub_feeds: Create pubsub nodes on module load instead of later
Kim Alvefur <zash@zash.se>
parents: 5571
diff changeset
87 return
2382
f159ad7ac18a mod_pubsub_feeds: Check if node exists earlier and attempt to create it if not
Kim Alvefur <zash@zash.se>
parents: 2381
diff changeset
88 end
5571
ca3c2d11823c mod_pubsub_feeds: Track latest timestamp seen in feeds instead of last poll
Kim Alvefur <zash@zash.se>
parents: 5570
diff changeset
89
ca3c2d11823c mod_pubsub_feeds: Track latest timestamp seen in feeds instead of last poll
Kim Alvefur <zash@zash.se>
parents: 5570
diff changeset
90 local start_from = #entries;
ca3c2d11823c mod_pubsub_feeds: Track latest timestamp seen in feeds instead of last poll
Kim Alvefur <zash@zash.se>
parents: 5570
diff changeset
91 for i, entry in ipairs(entries) do
ca3c2d11823c mod_pubsub_feeds: Track latest timestamp seen in feeds instead of last poll
Kim Alvefur <zash@zash.se>
parents: 5570
diff changeset
92 local id = entry:get_child_text("id");
ca3c2d11823c mod_pubsub_feeds: Track latest timestamp seen in feeds instead of last poll
Kim Alvefur <zash@zash.se>
parents: 5570
diff changeset
93 if not id then
ca3c2d11823c mod_pubsub_feeds: Track latest timestamp seen in feeds instead of last poll
Kim Alvefur <zash@zash.se>
parents: 5570
diff changeset
94 local link = entry:get_child("link");
ca3c2d11823c mod_pubsub_feeds: Track latest timestamp seen in feeds instead of last poll
Kim Alvefur <zash@zash.se>
parents: 5570
diff changeset
95 if link then
5573
ed177fa5b54c mod_pubsub_feeds: Fix mixup between feed object and parsed feed
Kim Alvefur <zash@zash.se>
parents: 5572
diff changeset
96 module:log("debug", "Feed %q item %s is missing an id, using <link> instead", item.url, entry:top_tag());
5571
ca3c2d11823c mod_pubsub_feeds: Track latest timestamp seen in feeds instead of last poll
Kim Alvefur <zash@zash.se>
parents: 5570
diff changeset
97 id = link and link.attr.href;
ca3c2d11823c mod_pubsub_feeds: Track latest timestamp seen in feeds instead of last poll
Kim Alvefur <zash@zash.se>
parents: 5570
diff changeset
98 else
5574
10b7118bbd8f mod_pubsub_feeds: Remove broken attempt to generate an ID from content
Kim Alvefur <zash@zash.se>
parents: 5573
diff changeset
99 module:log("error", "Feed %q item %s is missing both id and link, this feed is unusable", item.url, entry:top_tag());
10b7118bbd8f mod_pubsub_feeds: Remove broken attempt to generate an ID from content
Kim Alvefur <zash@zash.se>
parents: 5573
diff changeset
100 return;
5571
ca3c2d11823c mod_pubsub_feeds: Track latest timestamp seen in feeds instead of last poll
Kim Alvefur <zash@zash.se>
parents: 5570
diff changeset
101 end
ca3c2d11823c mod_pubsub_feeds: Track latest timestamp seen in feeds instead of last poll
Kim Alvefur <zash@zash.se>
parents: 5570
diff changeset
102 entry:text_tag("id", id);
ca3c2d11823c mod_pubsub_feeds: Track latest timestamp seen in feeds instead of last poll
Kim Alvefur <zash@zash.se>
parents: 5570
diff changeset
103 end
ca3c2d11823c mod_pubsub_feeds: Track latest timestamp seen in feeds instead of last poll
Kim Alvefur <zash@zash.se>
parents: 5570
diff changeset
104
5577
e7792188540a mod_pubsub_feeds: Retrieve only the most recent item to compare
Kim Alvefur <zash@zash.se>
parents: 5576
diff changeset
105 if last_id == id then
5571
ca3c2d11823c mod_pubsub_feeds: Track latest timestamp seen in feeds instead of last poll
Kim Alvefur <zash@zash.se>
parents: 5570
diff changeset
106 -- This should be the first item that we already have.
ca3c2d11823c mod_pubsub_feeds: Track latest timestamp seen in feeds instead of last poll
Kim Alvefur <zash@zash.se>
parents: 5570
diff changeset
107 start_from = i-1;
ca3c2d11823c mod_pubsub_feeds: Track latest timestamp seen in feeds instead of last poll
Kim Alvefur <zash@zash.se>
parents: 5570
diff changeset
108 break
ca3c2d11823c mod_pubsub_feeds: Track latest timestamp seen in feeds instead of last poll
Kim Alvefur <zash@zash.se>
parents: 5570
diff changeset
109 end
ca3c2d11823c mod_pubsub_feeds: Track latest timestamp seen in feeds instead of last poll
Kim Alvefur <zash@zash.se>
parents: 5570
diff changeset
110 end
ca3c2d11823c mod_pubsub_feeds: Track latest timestamp seen in feeds instead of last poll
Kim Alvefur <zash@zash.se>
parents: 5570
diff changeset
111
ca3c2d11823c mod_pubsub_feeds: Track latest timestamp seen in feeds instead of last poll
Kim Alvefur <zash@zash.se>
parents: 5570
diff changeset
112 for i = start_from, 1, -1 do -- Feeds are usually in reverse order
2381
a4614a329bdd mod_pubsub_feeds: Get entry from list of entries (thanks Link Mauve)
Kim Alvefur <zash@zash.se>
parents: 2380
diff changeset
113 local entry = entries[i];
668
343b115ebbea mod_pubsub_feed: Cleanup and update to new APIs in 0.9
Kim Alvefur <zash@zash.se>
parents: 633
diff changeset
114 entry.attr.xmlns = xmlns_atom;
322
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
115
5571
ca3c2d11823c mod_pubsub_feeds: Track latest timestamp seen in feeds instead of last poll
Kim Alvefur <zash@zash.se>
parents: 5570
diff changeset
116 local id = entry:get_child_text("id");
322
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
117
5571
ca3c2d11823c mod_pubsub_feeds: Track latest timestamp seen in feeds instead of last poll
Kim Alvefur <zash@zash.se>
parents: 5570
diff changeset
118 local timestamp = dt_parse(entry:get_child_text("published"));
ca3c2d11823c mod_pubsub_feeds: Track latest timestamp seen in feeds instead of last poll
Kim Alvefur <zash@zash.se>
parents: 5570
diff changeset
119 if not timestamp then
ca3c2d11823c mod_pubsub_feeds: Track latest timestamp seen in feeds instead of last poll
Kim Alvefur <zash@zash.se>
parents: 5570
diff changeset
120 timestamp = time();
ca3c2d11823c mod_pubsub_feeds: Track latest timestamp seen in feeds instead of last poll
Kim Alvefur <zash@zash.se>
parents: 5570
diff changeset
121 entry:text_tag("published", dt_datetime(timestamp));
ca3c2d11823c mod_pubsub_feeds: Track latest timestamp seen in feeds instead of last poll
Kim Alvefur <zash@zash.se>
parents: 5570
diff changeset
122 end
ca3c2d11823c mod_pubsub_feeds: Track latest timestamp seen in feeds instead of last poll
Kim Alvefur <zash@zash.se>
parents: 5570
diff changeset
123
322
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
124 if not timestamp or not item.last_update or timestamp > item.last_update then
5571
ca3c2d11823c mod_pubsub_feeds: Track latest timestamp seen in feeds instead of last poll
Kim Alvefur <zash@zash.se>
parents: 5570
diff changeset
125 local xitem = st.stanza("item", { id = id, xmlns = "http://jabber.org/protocol/pubsub" }):add_child(entry);
ca3c2d11823c mod_pubsub_feeds: Track latest timestamp seen in feeds instead of last poll
Kim Alvefur <zash@zash.se>
parents: 5570
diff changeset
126 -- TODO Put data from /feed into item/source
322
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
127
5571
ca3c2d11823c mod_pubsub_feeds: Track latest timestamp seen in feeds instead of last poll
Kim Alvefur <zash@zash.se>
parents: 5570
diff changeset
128 local ok, err = pubsub.service:publish(node, true, id, xitem);
ca3c2d11823c mod_pubsub_feeds: Track latest timestamp seen in feeds instead of last poll
Kim Alvefur <zash@zash.se>
parents: 5570
diff changeset
129 if not ok then
ca3c2d11823c mod_pubsub_feeds: Track latest timestamp seen in feeds instead of last poll
Kim Alvefur <zash@zash.se>
parents: 5570
diff changeset
130 module:log("error", "Publishing to node %s failed: %s", node, err);
ca3c2d11823c mod_pubsub_feeds: Track latest timestamp seen in feeds instead of last poll
Kim Alvefur <zash@zash.se>
parents: 5570
diff changeset
131 elseif timestamp then
ca3c2d11823c mod_pubsub_feeds: Track latest timestamp seen in feeds instead of last poll
Kim Alvefur <zash@zash.se>
parents: 5570
diff changeset
132 item.last_update = timestamp;
322
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
133 end
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
134 end
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
135 end
1343
7dbde05b48a9 all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1325
diff changeset
136
1457
720ff25d94e6 mod_pubsub_feeds: Check that lease time exists before comparing
Kim Alvefur <zash@zash.se>
parents: 1456
diff changeset
137 if item.lease_expires and item.lease_expires > time() then
1455
13e359c48b5b mod_pubsub_feeds: Fix refreshing of subscriptions
Kim Alvefur <zash@zash.se>
parents: 1454
diff changeset
138 item.subscription = nil;
13e359c48b5b mod_pubsub_feeds: Fix refreshing of subscriptions
Kim Alvefur <zash@zash.se>
parents: 1454
diff changeset
139 item.lease_expires = nil;
13e359c48b5b mod_pubsub_feeds: Fix refreshing of subscriptions
Kim Alvefur <zash@zash.se>
parents: 1454
diff changeset
140 end
322
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
141 if use_pubsubhubub and not item.subscription then
668
343b115ebbea mod_pubsub_feed: Cleanup and update to new APIs in 0.9
Kim Alvefur <zash@zash.se>
parents: 633
diff changeset
142 --module:log("debug", "check if %s has a hub", item.node);
2132
b149ea428b81 mod_pubsub_feeds: Switch to use util.xml for parsing feeds and include RSS to Atom translation code from lua-feeds
Kim Alvefur <zash@zash.se>
parents: 1457
diff changeset
143 for link in feed:childtags("link") do
b149ea428b81 mod_pubsub_feeds: Switch to use util.xml for parsing feeds and include RSS to Atom translation code from lua-feeds
Kim Alvefur <zash@zash.se>
parents: 1457
diff changeset
144 if link.attr.rel == "hub" then
b149ea428b81 mod_pubsub_feeds: Switch to use util.xml for parsing feeds and include RSS to Atom translation code from lua-feeds
Kim Alvefur <zash@zash.se>
parents: 1457
diff changeset
145 item.hub = link.attr.href;
b149ea428b81 mod_pubsub_feeds: Switch to use util.xml for parsing feeds and include RSS to Atom translation code from lua-feeds
Kim Alvefur <zash@zash.se>
parents: 1457
diff changeset
146 module:log("debug", "Node %s has a hub: %s", item.node, item.hub);
b149ea428b81 mod_pubsub_feeds: Switch to use util.xml for parsing feeds and include RSS to Atom translation code from lua-feeds
Kim Alvefur <zash@zash.se>
parents: 1457
diff changeset
147 return subscribe(item);
b149ea428b81 mod_pubsub_feeds: Switch to use util.xml for parsing feeds and include RSS to Atom translation code from lua-feeds
Kim Alvefur <zash@zash.se>
parents: 1457
diff changeset
148 end
322
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
149 end
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 end
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
152
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
153 function fetch(item, callback) -- HTTP Pull
5579
bc292c84f56c mod_pubsub_feeds: Specify acceptable formats in Accept header
Kim Alvefur <zash@zash.se>
parents: 5578
diff changeset
154 local headers = {
bc292c84f56c mod_pubsub_feeds: Specify acceptable formats in Accept header
Kim Alvefur <zash@zash.se>
parents: 5578
diff changeset
155 ["If-None-Match"] = item.etag;
bc292c84f56c mod_pubsub_feeds: Specify acceptable formats in Accept header
Kim Alvefur <zash@zash.se>
parents: 5578
diff changeset
156 ["Accept"] = "application/atom+xml, application/x-rss+xml, application/xml";
bc292c84f56c mod_pubsub_feeds: Specify acceptable formats in Accept header
Kim Alvefur <zash@zash.se>
parents: 5578
diff changeset
157 };
3047
7c55f05327a2 mod_pubsub_feeds: Use ETag instead of problematic If-Modified-Since
Kim Alvefur <zash@zash.se>
parents: 2645
diff changeset
158 http.request(item.url, { headers = headers }, function(data, code, resp)
278
653c1826739e mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
159 if code == 200 then
5578
b9821e9a21a2 mod_pubsub_feeds: Pass feed data as argument instead of storing on object
Kim Alvefur <zash@zash.se>
parents: 5577
diff changeset
160 if callback then callback(item, data) end
3047
7c55f05327a2 mod_pubsub_feeds: Use ETag instead of problematic If-Modified-Since
Kim Alvefur <zash@zash.se>
parents: 2645
diff changeset
161 if resp.headers then
7c55f05327a2 mod_pubsub_feeds: Use ETag instead of problematic If-Modified-Since
Kim Alvefur <zash@zash.se>
parents: 2645
diff changeset
162 item.etag = resp.headers.etag
7c55f05327a2 mod_pubsub_feeds: Use ETag instead of problematic If-Modified-Since
Kim Alvefur <zash@zash.se>
parents: 2645
diff changeset
163 end
668
343b115ebbea mod_pubsub_feed: Cleanup and update to new APIs in 0.9
Kim Alvefur <zash@zash.se>
parents: 633
diff changeset
164 elseif code == 304 then
5571
ca3c2d11823c mod_pubsub_feeds: Track latest timestamp seen in feeds instead of last poll
Kim Alvefur <zash@zash.se>
parents: 5570
diff changeset
165 module:log("debug", "No updates to %q", item.url);
3279
e6b25054c110 mod_pubsub_feeds: Log a message for HTTP 301 redirects
Kim Alvefur <zash@zash.se>
parents: 3278
diff changeset
166 elseif code == 301 and resp.headers.location then
e6b25054c110 mod_pubsub_feeds: Log a message for HTTP 301 redirects
Kim Alvefur <zash@zash.se>
parents: 3278
diff changeset
167 module:log("info", "Feed %q has moved to %q", item.url, resp.headers.location);
3280
645c4f14e03d mod_pubsub_feeds: Fix syntax error
Kim Alvefur <zash@zash.se>
parents: 3279
diff changeset
168 elseif code <= 100 then
3278
958f0b8b502c mod_pubsub_feeds: Log non-HTTP errors like certificate problems etc
Kim Alvefur <zash@zash.se>
parents: 3277
diff changeset
169 module:log("error", "Error fetching %q: %q[%d]", item.url, data, code);
3277
78b11fb291a2 mod_pubsub_feeds: Log a debug message for status codes not handled
Kim Alvefur <zash@zash.se>
parents: 3242
diff changeset
170 else
78b11fb291a2 mod_pubsub_feeds: Log a debug message for status codes not handled
Kim Alvefur <zash@zash.se>
parents: 3242
diff changeset
171 module:log("debug", "Unhandled status code %d when fetching %q", code, item.url);
278
653c1826739e mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
172 end
653c1826739e mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
173 end);
653c1826739e mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
174 end
653c1826739e mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
175
1453
7b53cfc6ba8d mod_pubsub_feeds: Fix refresh_feeds() allways refreshing all feeds
Kim Alvefur <zash@zash.se>
parents: 1343
diff changeset
176 function refresh_feeds(now)
322
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
177 --module:log("debug", "Refreshing feeds");
2134
bd7744df0b4a mod_pubsub_feeds: Remove unused imports [luacheck]
Kim Alvefur <zash@zash.se>
parents: 2132
diff changeset
178 for _, item in pairs(feed_list) do
1453
7b53cfc6ba8d mod_pubsub_feeds: Fix refresh_feeds() allways refreshing all feeds
Kim Alvefur <zash@zash.se>
parents: 1343
diff changeset
179 if item.subscription ~= "subscribe" and item.last_update + refresh_interval < now then
668
343b115ebbea mod_pubsub_feed: Cleanup and update to new APIs in 0.9
Kim Alvefur <zash@zash.se>
parents: 633
diff changeset
180 --module:log("debug", "checking %s", item.node);
322
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
181 fetch(item, update_entry);
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
182 end
278
653c1826739e mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
183 end
653c1826739e mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
184 return refresh_interval;
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
668
343b115ebbea mod_pubsub_feed: Cleanup and update to new APIs in 0.9
Kim Alvefur <zash@zash.se>
parents: 633
diff changeset
187 local function format_url(node)
2134
bd7744df0b4a mod_pubsub_feeds: Remove unused imports [luacheck]
Kim Alvefur <zash@zash.se>
parents: 2132
diff changeset
188 return module:http_url(nil, "/callback") .. "?" .. formencode({ node = node });
1343
7dbde05b48a9 all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1325
diff changeset
189 end
401
c85397063eca mod_pubsub_feed: Try harder to figure out ones callback URL.
Kim Alvefur <zash@zash.se>
parents: 400
diff changeset
190
718
a37e4149ccd1 mod_pubsub_feeds: Prepare for making it possible to unsubscribe, and some other minor changes.
Kim Alvefur <zash@zash.se>
parents: 717
diff changeset
191 function subscribe(feed, want)
a37e4149ccd1 mod_pubsub_feeds: Prepare for making it possible to unsubscribe, and some other minor changes.
Kim Alvefur <zash@zash.se>
parents: 717
diff changeset
192 want = want or "subscribe";
a37e4149ccd1 mod_pubsub_feeds: Prepare for making it possible to unsubscribe, and some other minor changes.
Kim Alvefur <zash@zash.se>
parents: 717
diff changeset
193 feed.secret = feed.secret or uuid();
403
fc62b26dfdf6 mod_pubsub_feed: Major cleanup, and use newer APIs. (Thanks Maranda)
Kim Alvefur <zash@zash.se>
parents: 402
diff changeset
194 local body = formencode{
668
343b115ebbea mod_pubsub_feed: Cleanup and update to new APIs in 0.9
Kim Alvefur <zash@zash.se>
parents: 633
diff changeset
195 ["hub.callback"] = format_url(feed.node);
718
a37e4149ccd1 mod_pubsub_feeds: Prepare for making it possible to unsubscribe, and some other minor changes.
Kim Alvefur <zash@zash.se>
parents: 717
diff changeset
196 ["hub.mode"] = want;
322
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
197 ["hub.topic"] = feed.url;
1454
480c6f0576b1 mod_pubsub_feeds: Update to 0.4 version of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents: 1453
diff changeset
198 ["hub.verify"] = "async"; -- COMPAT this is REQUIRED in the 0.3 draft but removed in 0.4
325
4e50e591a7fc mod_pubsub_feed: Implement signature verification
Kim Alvefur <zash@zash.se>
parents: 324
diff changeset
199 ["hub.secret"] = feed.secret;
2401
7a1625a84624 mod_pubsub_feeds: Ask for leases that expire after one day (Years after testing this module, I was still subscribed to a GNU Social instance)
Kim Alvefur <zash@zash.se>
parents: 2384
diff changeset
200 ["hub.lease_seconds"] = lease_length;
403
fc62b26dfdf6 mod_pubsub_feed: Major cleanup, and use newer APIs. (Thanks Maranda)
Kim Alvefur <zash@zash.se>
parents: 402
diff changeset
201 };
322
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
202
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
203 --module:log("debug", "subscription request, body: %s", body);
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
204
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
205 --FIXME The subscription states and related stuff
718
a37e4149ccd1 mod_pubsub_feeds: Prepare for making it possible to unsubscribe, and some other minor changes.
Kim Alvefur <zash@zash.se>
parents: 717
diff changeset
206 feed.subscription = want;
983
ac9bf3fcbcfe mod_pubsub_feeds, mod_sms_clickatell, mod_twitter: Update for net.http API change in prosody:e3b9dc9dd940
Matthew Wild <mwild1@gmail.com>
parents: 797
diff changeset
207 http.request(feed.hub, { body = body }, function(data, code)
668
343b115ebbea mod_pubsub_feed: Cleanup and update to new APIs in 0.9
Kim Alvefur <zash@zash.se>
parents: 633
diff changeset
208 module:log("debug", "subscription to %s submitted, status %s", feed.node, tostring(code));
343b115ebbea mod_pubsub_feed: Cleanup and update to new APIs in 0.9
Kim Alvefur <zash@zash.se>
parents: 633
diff changeset
209 if code >= 400 then
343b115ebbea mod_pubsub_feed: Cleanup and update to new APIs in 0.9
Kim Alvefur <zash@zash.se>
parents: 633
diff changeset
210 module:log("error", "There was something wrong with our subscription request, body: %s", tostring(data));
343b115ebbea mod_pubsub_feed: Cleanup and update to new APIs in 0.9
Kim Alvefur <zash@zash.se>
parents: 633
diff changeset
211 feed.subscription = "failed";
343b115ebbea mod_pubsub_feed: Cleanup and update to new APIs in 0.9
Kim Alvefur <zash@zash.se>
parents: 633
diff changeset
212 end
322
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
213 end);
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
214 end
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
215
668
343b115ebbea mod_pubsub_feed: Cleanup and update to new APIs in 0.9
Kim Alvefur <zash@zash.se>
parents: 633
diff changeset
216 function handle_http_request(event)
343b115ebbea mod_pubsub_feed: Cleanup and update to new APIs in 0.9
Kim Alvefur <zash@zash.se>
parents: 633
diff changeset
217 local request = event.request;
343b115ebbea mod_pubsub_feed: Cleanup and update to new APIs in 0.9
Kim Alvefur <zash@zash.se>
parents: 633
diff changeset
218 local method = request.method;
343b115ebbea mod_pubsub_feed: Cleanup and update to new APIs in 0.9
Kim Alvefur <zash@zash.se>
parents: 633
diff changeset
219 local body = request.body;
343b115ebbea mod_pubsub_feed: Cleanup and update to new APIs in 0.9
Kim Alvefur <zash@zash.se>
parents: 633
diff changeset
220
343b115ebbea mod_pubsub_feed: Cleanup and update to new APIs in 0.9
Kim Alvefur <zash@zash.se>
parents: 633
diff changeset
221 local query = request.url.query or {}; --FIXME
322
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
222 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
223 query = formdecode(query);
322
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
224 end
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
225
668
343b115ebbea mod_pubsub_feed: Cleanup and update to new APIs in 0.9
Kim Alvefur <zash@zash.se>
parents: 633
diff changeset
226 local feed = feed_list[query.node];
718
a37e4149ccd1 mod_pubsub_feeds: Prepare for making it possible to unsubscribe, and some other minor changes.
Kim Alvefur <zash@zash.se>
parents: 717
diff changeset
227 if not feed then
2422
57235bce06fc mod_pubsub_feeds: Always approve unsubscription from unknown feeds
Kim Alvefur <zash@zash.se>
parents: 2421
diff changeset
228 if query["hub.mode"] == "unsubscribe" then
57235bce06fc mod_pubsub_feeds: Always approve unsubscription from unknown feeds
Kim Alvefur <zash@zash.se>
parents: 2421
diff changeset
229 -- Unsubscribe from unknown feed
57235bce06fc mod_pubsub_feeds: Always approve unsubscription from unknown feeds
Kim Alvefur <zash@zash.se>
parents: 2421
diff changeset
230 module:log("debug", "Unsubscribe from unknown feed %s -- %s", query["hub.topic"], formencode(query));
57235bce06fc mod_pubsub_feeds: Always approve unsubscription from unknown feeds
Kim Alvefur <zash@zash.se>
parents: 2421
diff changeset
231 return query["hub.challenge"];
57235bce06fc mod_pubsub_feeds: Always approve unsubscription from unknown feeds
Kim Alvefur <zash@zash.se>
parents: 2421
diff changeset
232 end
2421
a9add2d1adf5 mod_pubsub_feeds: Log pushes for unknown feeds to aid in manual unsubscription from them
Kim Alvefur <zash@zash.se>
parents: 2401
diff changeset
233 module:log("debug", "Push for unknown feed %s -- %s", query["hub.topic"], formencode(query));
718
a37e4149ccd1 mod_pubsub_feeds: Prepare for making it possible to unsubscribe, and some other minor changes.
Kim Alvefur <zash@zash.se>
parents: 717
diff changeset
234 return 404;
a37e4149ccd1 mod_pubsub_feeds: Prepare for making it possible to unsubscribe, and some other minor changes.
Kim Alvefur <zash@zash.se>
parents: 717
diff changeset
235 end
a37e4149ccd1 mod_pubsub_feeds: Prepare for making it possible to unsubscribe, and some other minor changes.
Kim Alvefur <zash@zash.se>
parents: 717
diff changeset
236
322
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
237 if method == "GET" then
718
a37e4149ccd1 mod_pubsub_feeds: Prepare for making it possible to unsubscribe, and some other minor changes.
Kim Alvefur <zash@zash.se>
parents: 717
diff changeset
238 if query.node then
323
433bf7dc3e7a mod_pubsub_feed: Stricter verification handling. Correctly echo the hubs challenge.
Kim Alvefur <zash@zash.se>
parents: 322
diff changeset
239 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
240 module:log("debug", "Invalid topic: %s", tostring(query["hub.topic"]))
668
343b115ebbea mod_pubsub_feed: Cleanup and update to new APIs in 0.9
Kim Alvefur <zash@zash.se>
parents: 633
diff changeset
241 return 404
322
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
242 end
1456
2cd667d8edbd mod_pubsub_feeds: Handle denied subscription requests
Kim Alvefur <zash@zash.se>
parents: 1455
diff changeset
243 if query["hub.mode"] == "denied" then
2cd667d8edbd mod_pubsub_feeds: Handle denied subscription requests
Kim Alvefur <zash@zash.se>
parents: 1455
diff changeset
244 module:log("info", "Subscription denied: %s", tostring(query["hub.reason"] or "No reason given"))
2cd667d8edbd mod_pubsub_feeds: Handle denied subscription requests
Kim Alvefur <zash@zash.se>
parents: 1455
diff changeset
245 feed.subscription = "denied";
2cd667d8edbd mod_pubsub_feeds: Handle denied subscription requests
Kim Alvefur <zash@zash.se>
parents: 1455
diff changeset
246 return "Ok then :(";
2cd667d8edbd mod_pubsub_feeds: Handle denied subscription requests
Kim Alvefur <zash@zash.se>
parents: 1455
diff changeset
247 elseif query["hub.mode"] == feed.subscription then
2cd667d8edbd mod_pubsub_feeds: Handle denied subscription requests
Kim Alvefur <zash@zash.se>
parents: 1455
diff changeset
248 module:log("debug", "Confirming %s request to %s", feed.subscription, feed.url)
2cd667d8edbd mod_pubsub_feeds: Handle denied subscription requests
Kim Alvefur <zash@zash.se>
parents: 1455
diff changeset
249 else
323
433bf7dc3e7a mod_pubsub_feed: Stricter verification handling. Correctly echo the hubs challenge.
Kim Alvefur <zash@zash.se>
parents: 322
diff changeset
250 module:log("debug", "Invalid mode: %s", tostring(query["hub.mode"]))
668
343b115ebbea mod_pubsub_feed: Cleanup and update to new APIs in 0.9
Kim Alvefur <zash@zash.se>
parents: 633
diff changeset
251 return 400
323
433bf7dc3e7a mod_pubsub_feed: Stricter verification handling. Correctly echo the hubs challenge.
Kim Alvefur <zash@zash.se>
parents: 322
diff changeset
252 end
1454
480c6f0576b1 mod_pubsub_feeds: Update to 0.4 version of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents: 1453
diff changeset
253 local lease_seconds = tonumber(query["hub.lease_seconds"]);
480c6f0576b1 mod_pubsub_feeds: Update to 0.4 version of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents: 1453
diff changeset
254 if lease_seconds then
480c6f0576b1 mod_pubsub_feeds: Update to 0.4 version of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents: 1453
diff changeset
255 feed.lease_expires = time() + lease_seconds - refresh_interval * 2;
323
433bf7dc3e7a mod_pubsub_feed: Stricter verification handling. Correctly echo the hubs challenge.
Kim Alvefur <zash@zash.se>
parents: 322
diff changeset
256 end
668
343b115ebbea mod_pubsub_feed: Cleanup and update to new APIs in 0.9
Kim Alvefur <zash@zash.se>
parents: 633
diff changeset
257 return query["hub.challenge"];
322
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
258 end
668
343b115ebbea mod_pubsub_feed: Cleanup and update to new APIs in 0.9
Kim Alvefur <zash@zash.se>
parents: 633
diff changeset
259 return 400;
322
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
260 elseif method == "POST" then
718
a37e4149ccd1 mod_pubsub_feeds: Prepare for making it possible to unsubscribe, and some other minor changes.
Kim Alvefur <zash@zash.se>
parents: 717
diff changeset
261 if #body > 0 then
322
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
262 module:log("debug", "got %d bytes PuSHed for %s", #body, query.node);
668
343b115ebbea mod_pubsub_feed: Cleanup and update to new APIs in 0.9
Kim Alvefur <zash@zash.se>
parents: 633
diff changeset
263 local signature = request.headers.x_hub_signature;
325
4e50e591a7fc mod_pubsub_feed: Implement signature verification
Kim Alvefur <zash@zash.se>
parents: 324
diff changeset
264 if feed.secret then
4e50e591a7fc mod_pubsub_feed: Implement signature verification
Kim Alvefur <zash@zash.se>
parents: 324
diff changeset
265 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
266 if localsig ~= signature then
668
343b115ebbea mod_pubsub_feed: Cleanup and update to new APIs in 0.9
Kim Alvefur <zash@zash.se>
parents: 633
diff changeset
267 module:log("debug", "Invalid signature, got %s but wanted %s", tostring(signature), tostring(localsig));
343b115ebbea mod_pubsub_feed: Cleanup and update to new APIs in 0.9
Kim Alvefur <zash@zash.se>
parents: 633
diff changeset
268 return 401;
325
4e50e591a7fc mod_pubsub_feed: Implement signature verification
Kim Alvefur <zash@zash.se>
parents: 324
diff changeset
269 end
4e50e591a7fc mod_pubsub_feed: Implement signature verification
Kim Alvefur <zash@zash.se>
parents: 324
diff changeset
270 module:log("debug", "Valid signature");
4e50e591a7fc mod_pubsub_feed: Implement signature verification
Kim Alvefur <zash@zash.se>
parents: 324
diff changeset
271 end
5578
b9821e9a21a2 mod_pubsub_feeds: Pass feed data as argument instead of storing on object
Kim Alvefur <zash@zash.se>
parents: 5577
diff changeset
272 update_entry(feed, body);
668
343b115ebbea mod_pubsub_feed: Cleanup and update to new APIs in 0.9
Kim Alvefur <zash@zash.se>
parents: 633
diff changeset
273 return 202;
322
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
274 end
668
343b115ebbea mod_pubsub_feed: Cleanup and update to new APIs in 0.9
Kim Alvefur <zash@zash.se>
parents: 633
diff changeset
275 return 400;
322
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
276 end
668
343b115ebbea mod_pubsub_feed: Cleanup and update to new APIs in 0.9
Kim Alvefur <zash@zash.se>
parents: 633
diff changeset
277 return 501;
322
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
278 end
637dc0a04052 mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents: 300
diff changeset
279
668
343b115ebbea mod_pubsub_feed: Cleanup and update to new APIs in 0.9
Kim Alvefur <zash@zash.se>
parents: 633
diff changeset
280 if use_pubsubhubub then
343b115ebbea mod_pubsub_feed: Cleanup and update to new APIs in 0.9
Kim Alvefur <zash@zash.se>
parents: 633
diff changeset
281 module:provides("http", {
343b115ebbea mod_pubsub_feed: Cleanup and update to new APIs in 0.9
Kim Alvefur <zash@zash.se>
parents: 633
diff changeset
282 default_path = "/callback";
343b115ebbea mod_pubsub_feed: Cleanup and update to new APIs in 0.9
Kim Alvefur <zash@zash.se>
parents: 633
diff changeset
283 route = {
343b115ebbea mod_pubsub_feed: Cleanup and update to new APIs in 0.9
Kim Alvefur <zash@zash.se>
parents: 633
diff changeset
284 GET = handle_http_request;
343b115ebbea mod_pubsub_feed: Cleanup and update to new APIs in 0.9
Kim Alvefur <zash@zash.se>
parents: 633
diff changeset
285 POST = handle_http_request;
343b115ebbea mod_pubsub_feed: Cleanup and update to new APIs in 0.9
Kim Alvefur <zash@zash.se>
parents: 633
diff changeset
286 -- This all?
343b115ebbea mod_pubsub_feed: Cleanup and update to new APIs in 0.9
Kim Alvefur <zash@zash.se>
parents: 633
diff changeset
287 };
343b115ebbea mod_pubsub_feed: Cleanup and update to new APIs in 0.9
Kim Alvefur <zash@zash.se>
parents: 633
diff changeset
288 });
278
653c1826739e mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
289 end
653c1826739e mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
290
668
343b115ebbea mod_pubsub_feed: Cleanup and update to new APIs in 0.9
Kim Alvefur <zash@zash.se>
parents: 633
diff changeset
291 module:add_timer(1, refresh_feeds);