plugins/pubsub2room.lua

changeset 70
22670e551879
parent 69
2d7c29310a28
child 71
9ab5919aa416
equal deleted inserted replaced
69:2d7c29310a28 70:22670e551879
8 template = "${author.name} committed: ${title}"; 8 template = "${author.name} committed: ${title}";
9 }; 9 };
10 }; 10 };
11 --]] 11 --]]
12 12
13 local st = require "util.stanza";
14
13 -- FIXME Should this really be here? 15 -- FIXME Should this really be here?
14 local extractor_mt = { 16 local extractor_mt = {
15 __index = function (t, k) 17 __index = function (t, k)
16 local n = t.stanza; 18 local n;
19 if k:match("^data%.") then
20 k = k:gsub("^data.", "");
21 n = t.data;
22 else
23 n = t.stanza;
24 end
17 for x in k:gmatch("[^.]+") do 25 for x in k:gmatch("[^.]+") do
18 n = n:get_child(x); 26 n = n:get_child(x);
19 if not n then return end 27 if not n then return end
20 end 28 end
21 return n[1]; 29 return n[1];
22 end 30 end
23 }; 31 };
24 32
25 local function new_extractor(stanza, ...) 33 local function new_extractor(stanza, data)
26 local st = stanza:get_child(...) 34 return stanza and setmetatable({ stanza = stanza, data = data }, extractor_mt) or nil;
27 return st and setmetatable({ stanza = st }, extractor_mt) or nil;
28 end 35 end
29 36
30 function riddim.plugins.pubsub2room(bot) 37 function riddim.plugins.pubsub2room(bot)
31 local bare_jid = require "util.jid".bare; 38 local bare_jid = require "util.jid".bare;
32 bot:add_plugin("pubsub"); 39 bot:add_plugin("pubsub");
33 40
34 local config = bot.config.pubsub2room; 41 local config = bot.config.pubsub2room;
35 bot:hook("pubsub/event", function(event) 42 bot:hook("pubsub/event", function(event)
36 local conf = config[event.from .. "#" .. event.node]; 43 local conf = config[event.from .. "#" .. event.node];
37 local room = bot.rooms[conf.room]; 44 local room = bot.rooms[conf.room];
38 local entry = event.item and new_extractor(event.item, "entry", "http://www.w3.org/2005/Atom") 45 local data = st.stanza(""):tag("id"):text(event.item.attr.id);
39 -- FIXME or forever be limited to Atom! 46 local entry = event.item and new_extractor(event.item.tags[1], data)
40 47
41 if not conf or not entry or not room then return end 48 if not conf or not entry or not room then return end
42 local message = conf.template:gsub("%${([^}]+)}", entry); 49 local message = conf.template:gsub("%${([^}]+)}", entry);
43 room:send_message(message); 50 room:send_message(message);
44 end); 51 end);

mercurial