mod_pep: Fix undefined global access
[prosody.git] / plugins / mod_pep.lua
1
2 local jid_bare = require "util.jid".bare;
3 local jid_split = require "util.jid".split;
4 local st = require "util.stanza";
5 local hosts = hosts;
6 local user_exists = require "core.usermanager".user_exists;
7 local is_contact_subscribed = require "core.rostermanager".is_contact_subscribed;
8 local pairs, ipairs = pairs, ipairs;
9
10 local data = {};
11
12 local function publish(session, node, item)
13         local stanza = st.message({from=session.full_jid, type='headline'})
14                 :tag('event', {xmlns='http://jabber.org/protocol/pubsub#event'})
15                         :tag('items', {node=node})
16                                 :add_child(item)
17                         :up()
18                 :up();
19
20         local bare = session.username..'@'..session.host;
21         -- store for the future
22         local user_data = data[bare];
23         if not user_data then user_data = {}; data[bare] = user_data; end
24         user_data[node] = stanza;
25         
26         -- broadcast to resources
27         stanza.attr.to = bare;
28         core_route_stanza(session, stanza);
29
30         -- broadcast to contacts
31         for jid, item in pairs(session.roster) do
32                 if jid and jid ~= "pending" and (item.subscription == 'from' or item.subscription == 'both') then
33                         stanza.attr.to = jid;
34                         core_route_stanza(session, stanza);
35                 end
36         end
37 end
38
39 module:add_iq_handler("c2s", "http://jabber.org/protocol/pubsub", function (session, stanza)
40         if stanza.attr.type == 'set' and (not stanza.attr.to or jid_bare(stanza.attr.from) == stanza.attr.to) then
41                 local payload = stanza.tags[1];
42                 if payload.name == 'pubsub' then
43                         payload = payload.tags[1];
44                         if payload and payload.name == 'publish' and payload.attr.node then
45                                 local node = payload.attr.node;
46                                 payload = payload.tags[1];
47                                 if payload then
48                                         publish(session, node, payload);
49                                         return true;
50                                 end -- TODO else error
51                         end -- TODO else error
52                 end
53         end
54         session.send(st.error_reply(stanza, "cancel", "service-unavailable"));
55 end);
56