Mercurial > prosody-modules
annotate mod_pubsub_feeds/mod_pubsub_feeds.lua @ 4033:7b6bcb91493e
mod_muc_markers: Allow tracking multiple markers
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Thu, 21 May 2020 16:35:07 +0100 |
parents | 27cc66bf918b |
children | 281a864e7472 |
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 -- |
653c1826739e
mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
3 -- Config: |
653c1826739e
mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
4 -- Component "pubsub.example.com" "pubsub" |
653c1826739e
mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
5 -- modules_enabled = { |
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
|
6 -- "pubsub_feeds"; |
278
653c1826739e
mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
7 -- } |
653c1826739e
mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
8 -- feeds = { -- node -> url |
279
aa0df3db4901
mod_pubsub_feed: Wrap entry in a item element.
Kim Alvefur <zash@zash.se>
parents:
278
diff
changeset
|
9 -- 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
|
10 -- } |
653c1826739e
mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
11 -- feed_pull_interval = 20 -- minutes |
322
637dc0a04052
mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents:
300
diff
changeset
|
12 -- |
637dc0a04052
mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents:
300
diff
changeset
|
13 -- Reference |
1454
480c6f0576b1
mod_pubsub_feeds: Update to 0.4 version of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
1453
diff
changeset
|
14 -- http://pubsubhubbub.googlecode.com/svn/trunk/pubsubhubbub-core-0.4.html |
278
653c1826739e
mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
15 |
792
db27f07342d4
mod_pubsub_feeds: Use reference to mod_pubsub from module:depends
Kim Alvefur <zash@zash.se>
parents:
765
diff
changeset
|
16 local pubsub = module:depends"pubsub"; |
322
637dc0a04052
mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents:
300
diff
changeset
|
17 |
3047
7c55f05327a2
mod_pubsub_feeds: Use ETag instead of problematic If-Modified-Since
Kim Alvefur <zash@zash.se>
parents:
2645
diff
changeset
|
18 local time = os.time; |
1325
b21236b6b8d8
Backed out changeset 853a382c9bd6
Kim Alvefur <zash@zash.se>
parents:
1324
diff
changeset
|
19 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
|
20 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
|
21 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
|
22 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
|
23 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
|
24 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
|
25 |
343b115ebbea
mod_pubsub_feed: Cleanup and update to new APIs in 0.9
Kim Alvefur <zash@zash.se>
parents:
633
diff
changeset
|
26 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
|
27 |
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
|
28 local function parse_feed(data) |
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
|
29 local feed, err = parse_xml(data); |
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
|
30 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
|
31 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
|
32 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
|
33 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
|
34 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
|
35 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
|
36 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
|
37 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
|
38 |
668
343b115ebbea
mod_pubsub_feed: Cleanup and update to new APIs in 0.9
Kim Alvefur <zash@zash.se>
parents:
633
diff
changeset
|
39 local use_pubsubhubub = module:get_option_boolean("use_pubsubhubub", true); |
343b115ebbea
mod_pubsub_feed: Cleanup and update to new APIs in 0.9
Kim Alvefur <zash@zash.se>
parents:
633
diff
changeset
|
40 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
|
41 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
|
42 end |
322
637dc0a04052
mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents:
300
diff
changeset
|
43 |
403
fc62b26dfdf6
mod_pubsub_feed: Major cleanup, and use newer APIs. (Thanks Maranda)
Kim Alvefur <zash@zash.se>
parents:
402
diff
changeset
|
44 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
|
45 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
|
46 local formencode = http.formencode; |
278
653c1826739e
mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
47 |
668
343b115ebbea
mod_pubsub_feed: Cleanup and update to new APIs in 0.9
Kim Alvefur <zash@zash.se>
parents:
633
diff
changeset
|
48 local feed_list = module:shared("feed_list"); |
2423
1b6027ef5191
mod_pubsub_feeds: Load config option once
Kim Alvefur <zash@zash.se>
parents:
2422
diff
changeset
|
49 local refresh_interval = module:get_option_number("feed_pull_interval", 15) * 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
|
50 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
|
51 |
794
5a8fa70faa13
mod_pubsub_feeds: Remove config-reloaded hook, do setup in module.load() instead
Kim Alvefur <zash@zash.se>
parents:
793
diff
changeset
|
52 function module.load() |
2377
02532f28b207
mod_pubsub_feeds: Remove default feeds
Kim Alvefur <zash@zash.se>
parents:
2137
diff
changeset
|
53 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
|
54 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
|
55 if not ok then nodes = {}; end |
475
db5702bb9e41
mod_pubsub_feed: Dynamicaly reloadable config.
Kim Alvefur <zash@zash.se>
parents:
461
diff
changeset
|
56 local new_feed_list = {}; |
db5702bb9e41
mod_pubsub_feed: Dynamicaly reloadable config.
Kim Alvefur <zash@zash.se>
parents:
461
diff
changeset
|
57 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
|
58 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
|
59 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
|
60 end |
475
db5702bb9e41
mod_pubsub_feed: Dynamicaly reloadable config.
Kim Alvefur <zash@zash.se>
parents:
461
diff
changeset
|
61 new_feed_list[node] = true; |
db5702bb9e41
mod_pubsub_feed: Dynamicaly reloadable config.
Kim Alvefur <zash@zash.se>
parents:
461
diff
changeset
|
62 if not feed_list[node] then |
db5702bb9e41
mod_pubsub_feed: Dynamicaly reloadable config.
Kim Alvefur <zash@zash.se>
parents:
461
diff
changeset
|
63 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
|
64 else |
db5702bb9e41
mod_pubsub_feed: Dynamicaly reloadable config.
Kim Alvefur <zash@zash.se>
parents:
461
diff
changeset
|
65 feed_list[node].url = url; |
db5702bb9e41
mod_pubsub_feed: Dynamicaly reloadable config.
Kim Alvefur <zash@zash.se>
parents:
461
diff
changeset
|
66 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
|
67 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
|
68 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
|
69 end |
475
db5702bb9e41
mod_pubsub_feed: Dynamicaly reloadable config.
Kim Alvefur <zash@zash.se>
parents:
461
diff
changeset
|
70 end |
db5702bb9e41
mod_pubsub_feed: Dynamicaly reloadable config.
Kim Alvefur <zash@zash.se>
parents:
461
diff
changeset
|
71 for node in pairs(feed_list) do |
db5702bb9e41
mod_pubsub_feed: Dynamicaly reloadable config.
Kim Alvefur <zash@zash.se>
parents:
461
diff
changeset
|
72 if not new_feed_list[node] then |
db5702bb9e41
mod_pubsub_feed: Dynamicaly reloadable config.
Kim Alvefur <zash@zash.se>
parents:
461
diff
changeset
|
73 feed_list[node] = nil; |
db5702bb9e41
mod_pubsub_feed: Dynamicaly reloadable config.
Kim Alvefur <zash@zash.se>
parents:
461
diff
changeset
|
74 end |
db5702bb9e41
mod_pubsub_feed: Dynamicaly reloadable config.
Kim Alvefur <zash@zash.se>
parents:
461
diff
changeset
|
75 end |
322
637dc0a04052
mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents:
300
diff
changeset
|
76 end |
637dc0a04052
mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents:
300
diff
changeset
|
77 |
637dc0a04052
mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents:
300
diff
changeset
|
78 function update_entry(item) |
637dc0a04052
mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents:
300
diff
changeset
|
79 local node = item.node; |
668
343b115ebbea
mod_pubsub_feed: Cleanup and update to new APIs in 0.9
Kim Alvefur <zash@zash.se>
parents:
633
diff
changeset
|
80 module:log("debug", "parsing %d bytes of data in node %s", #item.data or 0, node) |
3281
27cc66bf918b
mod_pubsub_feeds: Handle feed parsing error (thanks apollo13)
Kim Alvefur <zash@zash.se>
parents:
3280
diff
changeset
|
81 local feed, err = parse_feed(item.data); |
27cc66bf918b
mod_pubsub_feeds: Handle feed parsing error (thanks apollo13)
Kim Alvefur <zash@zash.se>
parents:
3280
diff
changeset
|
82 if not feed then |
27cc66bf918b
mod_pubsub_feeds: Handle feed parsing error (thanks apollo13)
Kim Alvefur <zash@zash.se>
parents:
3280
diff
changeset
|
83 module:log("error", "Could not parse feed %q: %s", item.url, err); |
27cc66bf918b
mod_pubsub_feeds: Handle feed parsing error (thanks apollo13)
Kim Alvefur <zash@zash.se>
parents:
3280
diff
changeset
|
84 module:log("debug", "Feed data:\n%s\n.", item.data); |
27cc66bf918b
mod_pubsub_feeds: Handle feed parsing error (thanks apollo13)
Kim Alvefur <zash@zash.se>
parents:
3280
diff
changeset
|
85 return; |
27cc66bf918b
mod_pubsub_feeds: Handle feed parsing error (thanks apollo13)
Kim Alvefur <zash@zash.se>
parents:
3280
diff
changeset
|
86 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
|
87 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
|
88 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
|
89 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
|
90 end |
2383
18323c6aa133
mod_pubsub_feeds: Halt feed parsing when we see a node we already know about
Kim Alvefur <zash@zash.se>
parents:
2382
diff
changeset
|
91 local ok, items = pubsub.service:get_items(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
|
92 if not ok then |
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
|
93 local ok, err = pubsub.service:create(node, true); |
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
|
94 if not ok then |
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
|
95 module:log("error", "Could not create node %s: %s", node, err); |
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
|
96 return; |
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
|
97 end |
2383
18323c6aa133
mod_pubsub_feeds: Halt feed parsing when we see a node we already know about
Kim Alvefur <zash@zash.se>
parents:
2382
diff
changeset
|
98 items = {}; |
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
|
99 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
|
100 for i = #entries, 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
|
101 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
|
102 entry.attr.xmlns = xmlns_atom; |
322
637dc0a04052
mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents:
300
diff
changeset
|
103 |
403
fc62b26dfdf6
mod_pubsub_feed: Major cleanup, and use newer APIs. (Thanks Maranda)
Kim Alvefur <zash@zash.se>
parents:
402
diff
changeset
|
104 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
|
105 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
|
106 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
|
107 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
|
108 |
637dc0a04052
mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents:
300
diff
changeset
|
109 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
|
110 --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
|
111 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
|
112 local id = entry:get_child_text("id"); |
2380
2f083426fe98
mod_pubsub_feeds: Use URL as ID if no id set. If that's not there either then hash stuff and call it a day
Kim Alvefur <zash@zash.se>
parents:
2379
diff
changeset
|
113 if not id then |
2f083426fe98
mod_pubsub_feeds: Use URL as ID if no id set. If that's not there either then hash stuff and call it a day
Kim Alvefur <zash@zash.se>
parents:
2379
diff
changeset
|
114 local link = entry:get_child("link"); |
2f083426fe98
mod_pubsub_feeds: Use URL as ID if no id set. If that's not there either then hash stuff and call it a day
Kim Alvefur <zash@zash.se>
parents:
2379
diff
changeset
|
115 id = link and link.attr.href; |
2f083426fe98
mod_pubsub_feeds: Use URL as ID if no id set. If that's not there either then hash stuff and call it a day
Kim Alvefur <zash@zash.se>
parents:
2379
diff
changeset
|
116 end |
2f083426fe98
mod_pubsub_feeds: Use URL as ID if no id set. If that's not there either then hash stuff and call it a day
Kim Alvefur <zash@zash.se>
parents:
2379
diff
changeset
|
117 if not id then |
2f083426fe98
mod_pubsub_feeds: Use URL as ID if no id set. If that's not there either then hash stuff and call it a day
Kim Alvefur <zash@zash.se>
parents:
2379
diff
changeset
|
118 -- Sigh, no link? |
2f083426fe98
mod_pubsub_feeds: Use URL as ID if no id set. If that's not there either then hash stuff and call it a day
Kim Alvefur <zash@zash.se>
parents:
2379
diff
changeset
|
119 id = feed.url .. "#" .. hmac_sha1(feed.url, tostring(entry), true) .. "@" .. dt_datetime(timestamp); |
2f083426fe98
mod_pubsub_feeds: Use URL as ID if no id set. If that's not there either then hash stuff and call it a day
Kim Alvefur <zash@zash.se>
parents:
2379
diff
changeset
|
120 end |
2383
18323c6aa133
mod_pubsub_feeds: Halt feed parsing when we see a node we already know about
Kim Alvefur <zash@zash.se>
parents:
2382
diff
changeset
|
121 if items[id] then |
18323c6aa133
mod_pubsub_feeds: Halt feed parsing when we see a node we already know about
Kim Alvefur <zash@zash.se>
parents:
2382
diff
changeset
|
122 -- Assume that this existing means we've added all new items |
2384
feba6439b6fb
mod_pubsub_feeds: Add fixme for future
Kim Alvefur <zash@zash.se>
parents:
2383
diff
changeset
|
123 -- FIXME Entries updated after publishing ... |
2383
18323c6aa133
mod_pubsub_feeds: Halt feed parsing when we see a node we already know about
Kim Alvefur <zash@zash.se>
parents:
2382
diff
changeset
|
124 break; |
18323c6aa133
mod_pubsub_feeds: Halt feed parsing when we see a node we already know about
Kim Alvefur <zash@zash.se>
parents:
2382
diff
changeset
|
125 end |
3242
fe4194f10c75
mod_pubsub_feeds: Include pubsub namespace on wrapper item as required by mod_pubsub
Kim Alvefur <zash@zash.se>
parents:
3047
diff
changeset
|
126 local xitem = st.stanza("item", { id = id, xmlns = "http://jabber.org/protocol/pubsub" }):add_child(entry); |
322
637dc0a04052
mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents:
300
diff
changeset
|
127 -- TODO Put data from /feed into item/source |
637dc0a04052
mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents:
300
diff
changeset
|
128 |
668
343b115ebbea
mod_pubsub_feed: Cleanup and update to new APIs in 0.9
Kim Alvefur <zash@zash.se>
parents:
633
diff
changeset
|
129 --module:log("debug", "publishing to %s, id %s", node, id); |
792
db27f07342d4
mod_pubsub_feeds: Use reference to mod_pubsub from module:depends
Kim Alvefur <zash@zash.se>
parents:
765
diff
changeset
|
130 local ok, err = pubsub.service:publish(node, true, id, xitem); |
322
637dc0a04052
mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents:
300
diff
changeset
|
131 if not ok then |
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
|
132 module:log("error", "Publishing to node %s failed: %s", node, err); |
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 |
278
653c1826739e
mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
154 local headers = { }; |
3047
7c55f05327a2
mod_pubsub_feeds: Use ETag instead of problematic If-Modified-Since
Kim Alvefur <zash@zash.se>
parents:
2645
diff
changeset
|
155 if item.data and item.etag then |
7c55f05327a2
mod_pubsub_feeds: Use ETag instead of problematic If-Modified-Since
Kim Alvefur <zash@zash.se>
parents:
2645
diff
changeset
|
156 headers["If-None-Match"] = item.etag; |
278
653c1826739e
mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
157 end |
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 |
653c1826739e
mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
160 item.data = data; |
322
637dc0a04052
mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents:
300
diff
changeset
|
161 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
|
162 item.last_update = time(); |
3047
7c55f05327a2
mod_pubsub_feeds: Use ETag instead of problematic If-Modified-Since
Kim Alvefur <zash@zash.se>
parents:
2645
diff
changeset
|
163 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
|
164 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
|
165 end |
668
343b115ebbea
mod_pubsub_feed: Cleanup and update to new APIs in 0.9
Kim Alvefur <zash@zash.se>
parents:
633
diff
changeset
|
166 elseif code == 304 then |
278
653c1826739e
mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
167 item.last_update = time(); |
3279
e6b25054c110
mod_pubsub_feeds: Log a message for HTTP 301 redirects
Kim Alvefur <zash@zash.se>
parents:
3278
diff
changeset
|
168 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
|
169 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
|
170 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
|
171 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
|
172 else |
78b11fb291a2
mod_pubsub_feeds: Log a debug message for status codes not handled
Kim Alvefur <zash@zash.se>
parents:
3242
diff
changeset
|
173 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
|
174 end |
653c1826739e
mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
175 end); |
653c1826739e
mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
176 end |
653c1826739e
mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
177 |
1453
7b53cfc6ba8d
mod_pubsub_feeds: Fix refresh_feeds() allways refreshing all feeds
Kim Alvefur <zash@zash.se>
parents:
1343
diff
changeset
|
178 function refresh_feeds(now) |
322
637dc0a04052
mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents:
300
diff
changeset
|
179 --module:log("debug", "Refreshing feeds"); |
2134
bd7744df0b4a
mod_pubsub_feeds: Remove unused imports [luacheck]
Kim Alvefur <zash@zash.se>
parents:
2132
diff
changeset
|
180 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
|
181 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
|
182 --module:log("debug", "checking %s", item.node); |
322
637dc0a04052
mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents:
300
diff
changeset
|
183 fetch(item, update_entry); |
637dc0a04052
mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents:
300
diff
changeset
|
184 end |
278
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 return refresh_interval; |
653c1826739e
mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
187 end |
653c1826739e
mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
188 |
668
343b115ebbea
mod_pubsub_feed: Cleanup and update to new APIs in 0.9
Kim Alvefur <zash@zash.se>
parents:
633
diff
changeset
|
189 local function format_url(node) |
2134
bd7744df0b4a
mod_pubsub_feeds: Remove unused imports [luacheck]
Kim Alvefur <zash@zash.se>
parents:
2132
diff
changeset
|
190 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
|
191 end |
401
c85397063eca
mod_pubsub_feed: Try harder to figure out ones callback URL.
Kim Alvefur <zash@zash.se>
parents:
400
diff
changeset
|
192 |
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
|
193 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
|
194 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
|
195 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
|
196 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
|
197 ["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
|
198 ["hub.mode"] = want; |
322
637dc0a04052
mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents:
300
diff
changeset
|
199 ["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
|
200 ["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
|
201 ["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
|
202 ["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
|
203 }; |
322
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 --module:log("debug", "subscription request, body: %s", body); |
637dc0a04052
mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents:
300
diff
changeset
|
206 |
637dc0a04052
mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents:
300
diff
changeset
|
207 --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
|
208 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
|
209 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
|
210 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
|
211 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
|
212 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
|
213 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
|
214 end |
322
637dc0a04052
mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents:
300
diff
changeset
|
215 end); |
637dc0a04052
mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents:
300
diff
changeset
|
216 end |
637dc0a04052
mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents:
300
diff
changeset
|
217 |
668
343b115ebbea
mod_pubsub_feed: Cleanup and update to new APIs in 0.9
Kim Alvefur <zash@zash.se>
parents:
633
diff
changeset
|
218 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
|
219 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
|
220 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
|
221 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
|
222 |
343b115ebbea
mod_pubsub_feed: Cleanup and update to new APIs in 0.9
Kim Alvefur <zash@zash.se>
parents:
633
diff
changeset
|
223 local query = request.url.query or {}; --FIXME |
322
637dc0a04052
mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents:
300
diff
changeset
|
224 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
|
225 query = formdecode(query); |
322
637dc0a04052
mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents:
300
diff
changeset
|
226 end |
637dc0a04052
mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents:
300
diff
changeset
|
227 |
668
343b115ebbea
mod_pubsub_feed: Cleanup and update to new APIs in 0.9
Kim Alvefur <zash@zash.se>
parents:
633
diff
changeset
|
228 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
|
229 if not feed then |
2422
57235bce06fc
mod_pubsub_feeds: Always approve unsubscription from unknown feeds
Kim Alvefur <zash@zash.se>
parents:
2421
diff
changeset
|
230 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
|
231 -- Unsubscribe from unknown feed |
57235bce06fc
mod_pubsub_feeds: Always approve unsubscription from unknown feeds
Kim Alvefur <zash@zash.se>
parents:
2421
diff
changeset
|
232 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
|
233 return query["hub.challenge"]; |
57235bce06fc
mod_pubsub_feeds: Always approve unsubscription from unknown feeds
Kim Alvefur <zash@zash.se>
parents:
2421
diff
changeset
|
234 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
|
235 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
|
236 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
|
237 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
|
238 |
322
637dc0a04052
mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents:
300
diff
changeset
|
239 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
|
240 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
|
241 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
|
242 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
|
243 return 404 |
322
637dc0a04052
mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents:
300
diff
changeset
|
244 end |
1456
2cd667d8edbd
mod_pubsub_feeds: Handle denied subscription requests
Kim Alvefur <zash@zash.se>
parents:
1455
diff
changeset
|
245 if query["hub.mode"] == "denied" then |
2cd667d8edbd
mod_pubsub_feeds: Handle denied subscription requests
Kim Alvefur <zash@zash.se>
parents:
1455
diff
changeset
|
246 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
|
247 feed.subscription = "denied"; |
2cd667d8edbd
mod_pubsub_feeds: Handle denied subscription requests
Kim Alvefur <zash@zash.se>
parents:
1455
diff
changeset
|
248 return "Ok then :("; |
2cd667d8edbd
mod_pubsub_feeds: Handle denied subscription requests
Kim Alvefur <zash@zash.se>
parents:
1455
diff
changeset
|
249 elseif query["hub.mode"] == feed.subscription then |
2cd667d8edbd
mod_pubsub_feeds: Handle denied subscription requests
Kim Alvefur <zash@zash.se>
parents:
1455
diff
changeset
|
250 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
|
251 else |
323
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 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
|
253 return 400 |
323
433bf7dc3e7a
mod_pubsub_feed: Stricter verification handling. Correctly echo the hubs challenge.
Kim Alvefur <zash@zash.se>
parents:
322
diff
changeset
|
254 end |
1454
480c6f0576b1
mod_pubsub_feeds: Update to 0.4 version of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
1453
diff
changeset
|
255 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
|
256 if lease_seconds then |
480c6f0576b1
mod_pubsub_feeds: Update to 0.4 version of PubSubHubbub
Kim Alvefur <zash@zash.se>
parents:
1453
diff
changeset
|
257 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
|
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 query["hub.challenge"]; |
322
637dc0a04052
mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents:
300
diff
changeset
|
260 end |
668
343b115ebbea
mod_pubsub_feed: Cleanup and update to new APIs in 0.9
Kim Alvefur <zash@zash.se>
parents:
633
diff
changeset
|
261 return 400; |
322
637dc0a04052
mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents:
300
diff
changeset
|
262 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
|
263 if #body > 0 then |
322
637dc0a04052
mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents:
300
diff
changeset
|
264 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
|
265 local signature = request.headers.x_hub_signature; |
325
4e50e591a7fc
mod_pubsub_feed: Implement signature verification
Kim Alvefur <zash@zash.se>
parents:
324
diff
changeset
|
266 if feed.secret then |
4e50e591a7fc
mod_pubsub_feed: Implement signature verification
Kim Alvefur <zash@zash.se>
parents:
324
diff
changeset
|
267 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
|
268 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
|
269 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
|
270 return 401; |
325
4e50e591a7fc
mod_pubsub_feed: Implement signature verification
Kim Alvefur <zash@zash.se>
parents:
324
diff
changeset
|
271 end |
4e50e591a7fc
mod_pubsub_feed: Implement signature verification
Kim Alvefur <zash@zash.se>
parents:
324
diff
changeset
|
272 module:log("debug", "Valid signature"); |
4e50e591a7fc
mod_pubsub_feed: Implement signature verification
Kim Alvefur <zash@zash.se>
parents:
324
diff
changeset
|
273 end |
322
637dc0a04052
mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents:
300
diff
changeset
|
274 feed.data = body; |
637dc0a04052
mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents:
300
diff
changeset
|
275 update_entry(feed); |
637dc0a04052
mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents:
300
diff
changeset
|
276 feed.last_update = time(); |
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 202; |
322
637dc0a04052
mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents:
300
diff
changeset
|
278 end |
668
343b115ebbea
mod_pubsub_feed: Cleanup and update to new APIs in 0.9
Kim Alvefur <zash@zash.se>
parents:
633
diff
changeset
|
279 return 400; |
322
637dc0a04052
mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents:
300
diff
changeset
|
280 end |
668
343b115ebbea
mod_pubsub_feed: Cleanup and update to new APIs in 0.9
Kim Alvefur <zash@zash.se>
parents:
633
diff
changeset
|
281 return 501; |
322
637dc0a04052
mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents:
300
diff
changeset
|
282 end |
637dc0a04052
mod_pubsub_feed: Implement PubSubHubbub subscriber
Kim Alvefur <zash@zash.se>
parents:
300
diff
changeset
|
283 |
668
343b115ebbea
mod_pubsub_feed: Cleanup and update to new APIs in 0.9
Kim Alvefur <zash@zash.se>
parents:
633
diff
changeset
|
284 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
|
285 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
|
286 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
|
287 route = { |
343b115ebbea
mod_pubsub_feed: Cleanup and update to new APIs in 0.9
Kim Alvefur <zash@zash.se>
parents:
633
diff
changeset
|
288 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
|
289 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
|
290 -- This all? |
343b115ebbea
mod_pubsub_feed: Cleanup and update to new APIs in 0.9
Kim Alvefur <zash@zash.se>
parents:
633
diff
changeset
|
291 }; |
343b115ebbea
mod_pubsub_feed: Cleanup and update to new APIs in 0.9
Kim Alvefur <zash@zash.se>
parents:
633
diff
changeset
|
292 }); |
278
653c1826739e
mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
293 end |
653c1826739e
mod_pubsub_feed: Fetches Atom feeds and publishes to PubSub
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
294 |
668
343b115ebbea
mod_pubsub_feed: Cleanup and update to new APIs in 0.9
Kim Alvefur <zash@zash.se>
parents:
633
diff
changeset
|
295 module:add_timer(1, refresh_feeds); |