Mercurial > prosody-modules
comparison mod_pubsub_feed/mod_pubsub_feed.lua @ 475:db5702bb9e41
mod_pubsub_feed: Dynamicaly reloadable config.
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Thu, 10 Nov 2011 11:45:32 +0100 |
parents | bbea8081c865 |
children | 0d3174d5a1cc |
comparison
equal
deleted
inserted
replaced
474:942738953ff3 | 475:db5702bb9e41 |
---|---|
36 local formdecode = http.formdecode; | 36 local formdecode = http.formdecode; |
37 local formencode = http.formencode; | 37 local formencode = http.formencode; |
38 local urldecode = http.urldecode; | 38 local urldecode = http.urldecode; |
39 local urlencode = http.urlencode; | 39 local urlencode = http.urlencode; |
40 | 40 |
41 local config = module:get_option("feeds") or { | 41 local feed_list = {}; |
42 planet_jabber = "http://planet.jabber.org/atom.xml"; | 42 local refresh_interval; |
43 prosody_blog = "http://blog.prosody.im/feed/atom.xml"; | 43 |
44 }; | 44 -- Dynamicaly reloadable config. |
45 local refresh_interval = module:get_option_number("feed_pull_interval", 15) * 60; | 45 local function update_config() |
46 local use_pubsubhubub = module:get_option_boolean("use_pubsubhubub", true); -- HTTP by default or not? | 46 local config = module:get_option("feeds") or { |
47 local httphost = module:get_option_string("pubsubhubub_httphost", module.host); -- If module.host IN A doesn't point to this server, use this to override. | 47 planet_jabber = "http://planet.jabber.org/atom.xml"; |
48 local feed_list = { } | 48 prosody_blog = "http://blog.prosody.im/feed/atom.xml"; |
49 for node, url in pairs(config) do | 49 }; |
50 feed_list[node] = { url = url; node = node; last_update = 0 }; | 50 refresh_interval = module:get_option_number("feed_pull_interval", 15) * 60; |
51 end | 51 local new_feed_list = {}; |
52 -- TODO module:hook("config-reloaded", above loop); | 52 for node, url in pairs(config) do |
53 -- Also, keeping it somewhere persistent in order to avoid duplicated publishes? | 53 new_feed_list[node] = true; |
54 if not feed_list[node] then | |
55 feed_list[node] = { url = url; node = node; last_update = 0 }; | |
56 else | |
57 feed_list[node].url = url; | |
58 end | |
59 end | |
60 for node in pairs(feed_list) do | |
61 if not new_feed_list[node] then | |
62 feed_list[node] = nil; | |
63 end | |
64 end | |
65 end | |
66 update_config(); | |
67 module:hook("config-reloaded", update_config); | |
54 | 68 |
55 -- Used to kill the timer | 69 -- Used to kill the timer |
56 local module_unloaded = false; | 70 local module_unloaded = false; |
57 function module.unload() | 71 function module.unload() |
58 module_unloaded = true; | 72 module_unloaded = true; |
59 end | 73 end |
74 | |
75 -- Config stuff that can't be reloaded, since it would need to re-bind HTTP stuff. | |
76 | |
77 -- If module.host IN A doesn't point to this server, use this to override. | |
78 local httphost = module:get_option_string("pubsubhubub_httphost", module.host); | |
79 -- HTTP by default or not? | |
80 local use_pubsubhubub = module:get_option_boolean("use_pubsubhubub", true); | |
60 | 81 |
61 -- Thanks to Maranda for this | 82 -- Thanks to Maranda for this |
62 local port, base, ssl = 5280, "callback", false; | 83 local port, base, ssl = 5280, "callback", false; |
63 local ports = module:get_option("feeds_ports") or { port = port, base = base, ssl = ssl }; | 84 local ports = module:get_option("feeds_ports") or { port = port, base = base, ssl = ssl }; |
64 -- FIXME If ports isn't a table, this will cause an error | 85 -- FIXME If ports isn't a table, this will cause an error |
167 end | 188 end |
168 end); | 189 end); |
169 end | 190 end |
170 | 191 |
171 function refresh_feeds() | 192 function refresh_feeds() |
193 local now = time(); | |
172 if module_unloaded then return end | 194 if module_unloaded then return end |
173 --module:log("debug", "Refreshing feeds"); | 195 --module:log("debug", "Refreshing feeds"); |
174 for node, item in pairs(feed_list) do | 196 for node, item in pairs(feed_list) do |
175 --FIXME Don't fetch feeds which have a subscription | 197 --FIXME Don't fetch feeds which have a subscription |
176 -- Otoho, what if the subscription expires or breaks? | 198 -- Otoho, what if the subscription expires or breaks? |
177 if item.last_update + refresh_interval < time() then | 199 if item.last_update + refresh_interval < now then |
178 module:log("debug", "checking %s", item.node); | 200 module:log("debug", "checking %s", item.node); |
179 fetch(item, update_entry); | 201 fetch(item, update_entry); |
180 end | 202 end |
181 end | 203 end |
182 return refresh_interval; | 204 return refresh_interval; |