32 |
32 |
33 local function new_extractor(stanza, data) |
33 local function new_extractor(stanza, data) |
34 return stanza and setmetatable({ stanza = stanza, data = data }, extractor_mt) or nil; |
34 return stanza and setmetatable({ stanza = stanza, data = data }, extractor_mt) or nil; |
35 end |
35 end |
36 |
36 |
|
37 local last_message_time = 0; |
|
38 |
37 function riddim.plugins.pubsub2room(bot) |
39 function riddim.plugins.pubsub2room(bot) |
38 local bare_jid = require "util.jid".bare; |
40 local bare_jid = require "util.jid".bare; |
39 bot:add_plugin("pubsub"); |
41 bot:add_plugin("pubsub"); |
40 |
42 |
41 local config = bot.config.pubsub2room; |
43 local config = bot.config.pubsub2room; |
45 local data = st.stanza(""):tag("id"):text(event.item.attr.id); |
47 local data = st.stanza(""):tag("id"):text(event.item.attr.id); |
46 local entry = event.item and new_extractor(event.item.tags[1], data) |
48 local entry = event.item and new_extractor(event.item.tags[1], data) |
47 |
49 |
48 if not conf or not entry or not room then return end |
50 if not conf or not entry or not room then return end |
49 local message = conf.template:gsub("%${([^}]+)}", entry); |
51 local message = conf.template:gsub("%${([^}]+)}", entry); |
50 room:send_message(message); |
52 |
|
53 -- Throttle to 1 message/second so we don't flood the room |
|
54 if os.time() - last_message_time > 0 then |
|
55 room:send_message(message); |
|
56 last_message_time = os.time(); |
|
57 else |
|
58 last_message_time = last_message_time + 1; |
|
59 verse.add_task(last_message_time - os.time(), function () |
|
60 room:send_message(message); |
|
61 end); |
|
62 end |
51 end); |
63 end); |
52 |
64 |
53 -- FIXME When to unsubscribe? |
65 -- FIXME When to unsubscribe? |
54 bot:hook("started", function() |
66 bot:hook("started", function() |
55 local jid = bare_jid(bot.stream.jid); |
67 local jid = bare_jid(bot.stream.jid); |