95c18eec21dc24f7315dd35225cac2dfd7139ba0
[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 local next = next;
10 local load_roster = require "core.rostermanager".load_roster;
11
12 local data = {};
13 local recipients = {};
14
15 module:add_identity("pubsub", "pep");
16 module:add_feature("http://jabber.org/protocol/pubsub#publish");
17
18 local function publish(session, node, item)
19         local disable = #item.tags ~= 1 or #item.tags[1].tags == 0;
20         local bare = session.username..'@'..session.host;
21         local stanza = st.message({from=bare, type='headline'})
22                 :tag('event', {xmlns='http://jabber.org/protocol/pubsub#event'})
23                         :tag('items', {node=node})
24                                 :add_child(item)
25                         :up()
26                 :up();
27
28         -- store for the future
29         local user_data = data[bare];
30         if disable then
31                 if user_data then user_data[node] = nil; end
32                 if not next(user_data) then data[bare] = nil; end
33         else
34                 if not user_data then user_data = {}; data[bare] = user_data; end
35                 user_data[node] = stanza;
36         end
37         
38         -- broadcast to resources
39         stanza.attr.to = bare;
40         core_route_stanza(session, stanza);
41
42         -- broadcast to contacts
43         for jid, item in pairs(session.roster) do
44                 if jid and jid ~= "pending" and (item.subscription == 'from' or item.subscription == 'both') then
45                         stanza.attr.to = jid;
46                         core_route_stanza(session, stanza);
47                 end
48         end
49 end
50
51 local function get_caps_hash_from_presence(stanza, current)
52         if not stanza.attr.type then
53                 for _, child in pairs(stanza.tags) do
54                         if child.name == "c" and child.attr.xmlns == "http://jabber.org/protocol/caps" then
55                                 local attr = child.attr;
56                                 if attr.hash then -- new caps
57                                         if attr.hash == 'sha-1' and attr.node and attr.ver then return attr.ver, attr.node.."#"..attr.ver; end
58                                 else -- legacy caps
59                                         if attr.node and attr.ver then return attr.node.."#"..attr.ver.."#"..(attr.ext or ""), attr.node.."#"..attr.ver; end
60                                 end
61                                 return; -- bad caps format
62                         end
63                 end
64                 return current; -- no caps, could mean caps optimization, so return current
65         end
66 end
67
68 module:hook("presence/bare", function(event)
69         -- inbound presence to bare JID recieved
70         local origin, stanza = event.origin, event.stanza;
71         
72         local user = stanza.attr.to or (origin.username..'@'..origin.host);
73         local bare = jid_bare(stanza.attr.from);
74         local item = load_roster(jid_split(user))[bare];
75         if not stanza.attr.to or (item and (item.subscription == 'from' or item.subscription == 'both')) then
76                 local t = stanza.attr.type;
77                 local recipient = stanza.attr.from;
78                 if t == "unavailable" or t == "error" then
79                         if recipients[user] then recipients[user][recipient] = nil; end
80                 elseif not t then
81                         recipients[user] = recipients[user] or {};
82                         if not recipients[user][recipient] then
83                                 recipients[user][recipient] = true;
84                                 for node, message in pairs(data[user] or {}) do
85                                         message.attr.to = stanza.attr.from;
86                                         origin.send(message);
87                                 end
88                         end
89                 end
90         end
91 end, 10);
92
93 module:hook("iq/bare/http://jabber.org/protocol/pubsub:pubsub", function(event)
94         local session, stanza = event.origin, event.stanza;
95         if stanza.attr.type == 'set' and (not stanza.attr.to or jid_bare(stanza.attr.from) == stanza.attr.to) then
96                 local payload = stanza.tags[1];
97                 if payload.name == 'pubsub' then -- <pubsub xmlns='http://jabber.org/protocol/pubsub'>
98                         payload = payload.tags[1];
99                         if payload and payload.name == 'publish' and payload.attr.node then -- <publish node='http://jabber.org/protocol/tune'>
100                                 local node = payload.attr.node;
101                                 payload = payload.tags[1];
102                                 if payload then -- <item>
103                                         publish(session, node, payload);
104                                         return true;
105                                 end
106                         end
107                 end
108         end
109 end);
110
111 module:hook("iq/bare/disco", function(event)
112         local session, stanza = event.origin, event.stanza;
113         if stanza.attr.type == "result" then
114                 local disco = stanza.tags[1];
115                 if disco and disco.name == "query" and disco.attr.xmlns == "http://jabber.org/protocol/disco#info" then
116                         -- Process disco response
117                         -- TODO check if waiting for recipient's response
118                         local hash; -- TODO calculate hash
119                         -- TODO update hash map
120                         -- TODO set recipient's data to calculated data
121                 end
122         end
123 end);