plugins/pubsub2room.lua

changeset 69
2d7c29310a28
child 70
22670e551879
equal deleted inserted replaced
68:f388a8a57d1d 69:2d7c29310a28
1 --[[ pubsub2room
2 relays pubsub broadcasts to rooms
3
4 example conf:
5 pubsub2room = {
6 ["pubsub.prosody.im#commits"] = {
7 room = "prosody@conference.prosody.im";
8 template = "${author.name} committed: ${title}";
9 };
10 };
11 --]]
12
13 -- FIXME Should this really be here?
14 local extractor_mt = {
15 __index = function (t, k)
16 local n = t.stanza;
17 for x in k:gmatch("[^.]+") do
18 n = n:get_child(x);
19 if not n then return end
20 end
21 return n[1];
22 end
23 };
24
25 local function new_extractor(stanza, ...)
26 local st = stanza:get_child(...)
27 return st and setmetatable({ stanza = st }, extractor_mt) or nil;
28 end
29
30 function riddim.plugins.pubsub2room(bot)
31 local bare_jid = require "util.jid".bare;
32 bot:add_plugin("pubsub");
33
34 local config = bot.config.pubsub2room;
35 bot:hook("pubsub/event", function(event)
36 local conf = config[event.from .. "#" .. event.node];
37 local room = bot.rooms[conf.room];
38 local entry = event.item and new_extractor(event.item, "entry", "http://www.w3.org/2005/Atom")
39 -- FIXME or forever be limited to Atom!
40
41 if not conf or not entry or not room then return end
42 local message = conf.template:gsub("%${([^}]+)}", entry);
43 room:send_message(message);
44 end);
45
46 -- FIXME When to unsubscribe?
47 bot:hook("started", function()
48 local jid = bare_jid(bot.stream.jid);
49 for hostnode in pairs(config) do
50 local host, node = hostnode:match("^([^#]+)#(.*)");
51 bot.pubsub:subscribe(host, node, jid, print);
52 end
53 end);
54 end

mercurial