78098423c64fb67a25ade0b93c7aa42921f17c51
[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 type = type;
11 local load_roster = require "core.rostermanager".load_roster;
12 local sha1 = require "util.hashes".sha1;
13 local base64 = require "util.encodings".base64.encode;
14
15 local NULL = {};
16 local data = {};
17 local recipients = {};
18 local hash_map = {};
19
20 module:add_identity("pubsub", "pep");
21 module:add_feature("http://jabber.org/protocol/pubsub#publish");
22
23 local function publish(session, node, item)
24         local disable = #item.tags ~= 1 or #item.tags[1].tags == 0;
25         local bare = session.username..'@'..session.host;
26         local stanza = st.message({from=bare, type='headline'})
27                 :tag('event', {xmlns='http://jabber.org/protocol/pubsub#event'})
28                         :tag('items', {node=node})
29                                 :add_child(item)
30                         :up()
31                 :up();
32
33         -- store for the future
34         local user_data = data[bare];
35         if disable then
36                 if user_data then user_data[node] = nil; end
37                 if not next(user_data) then data[bare] = nil; end
38         else
39                 if not user_data then user_data = {}; data[bare] = user_data; end
40                 user_data[node] = stanza;
41         end
42         
43         -- broadcast
44         for recipient, notify in pairs(recipients[bare] or NULL) do
45                 if notify[node] then
46                         stanza.attr.to = recipient;
47                         core_post_stanza(session, stanza);
48                 end
49         end
50 end
51 local function publish_all(user, recipient, session)
52         local d = data[user];
53         local notify = recipients[user] and recipients[user][recipient];
54         if d and notify then
55                 for node, message in pairs(notify) do
56                         if d[node] then
57                                 message.attr.to = recipient;
58                                 session.send(message);
59                         end
60                 end
61         end
62 end
63
64 local function get_caps_hash_from_presence(stanza, current)
65         local t = stanza.attr.type;
66         if not t then
67                 for _, child in pairs(stanza.tags) do
68                         if child.name == "c" and child.attr.xmlns == "http://jabber.org/protocol/caps" then
69                                 local attr = child.attr;
70                                 if attr.hash then -- new caps
71                                         if attr.hash == 'sha-1' and attr.node and attr.ver then return attr.ver, attr.node.."#"..attr.ver; end
72                                 else -- legacy caps
73                                         if attr.node and attr.ver then return attr.node.."#"..attr.ver.."#"..(attr.ext or ""), attr.node.."#"..attr.ver; end
74                                 end
75                                 return; -- bad caps format
76                         end
77                 end
78         elseif t == "unavailable" or t == "error" then
79                 return;
80         end
81         return current; -- no caps, could mean caps optimization, so return current
82 end
83
84 module:hook("presence/bare", function(event)
85         -- inbound presence to bare JID recieved
86         local origin, stanza = event.origin, event.stanza;
87         
88         local user = stanza.attr.to or (origin.username..'@'..origin.host);
89         local bare = jid_bare(stanza.attr.from);
90         local item = load_roster(jid_split(user))[bare];
91         if not stanza.attr.to or (item and (item.subscription == 'from' or item.subscription == 'both')) then
92                 local recipient = stanza.attr.from;
93                 local current = recipients[user] and recipients[user][recipient];
94                 local hash = get_caps_hash_from_presence(stanza, current);
95                 if current == hash then return; end
96                 if not hash then
97                         if recipients[user] then recipients[user][recipient] = nil; end
98                 else
99                         recipients[user] = recipients[user] or {};
100                         if hash_map[hash] then
101                                 recipients[user][recipient] = hash_map[hash];
102                                 publish_all(user, recipient);
103                         else
104                                 recipients[user][recipient] = hash;
105                                 origin.send(
106                                         st.stanza("iq", {from=stanza.attr.to, to=stanza.attr.from, id="disco", type="get"})
107                                                 :query("http://jabber.org/protocol/disco#info")
108                                 );
109                         end
110                 end
111         end
112 end, 10);
113
114 module:hook("iq/bare/http://jabber.org/protocol/pubsub:pubsub", function(event)
115         local session, stanza = event.origin, event.stanza;
116         if stanza.attr.type == 'set' and (not stanza.attr.to or jid_bare(stanza.attr.from) == stanza.attr.to) then
117                 local payload = stanza.tags[1];
118                 if payload.name == 'pubsub' then -- <pubsub xmlns='http://jabber.org/protocol/pubsub'>
119                         payload = payload.tags[1];
120                         if payload and payload.name == 'publish' and payload.attr.node then -- <publish node='http://jabber.org/protocol/tune'>
121                                 local node = payload.attr.node;
122                                 payload = payload.tags[1];
123                                 if payload then -- <item>
124                                         publish(session, node, payload);
125                                         return true;
126                                 end
127                         end
128                 end
129         end
130 end);
131
132 local function calculate_hash(disco_info)
133         local identities, features, extensions = {}, {}, {};
134         for _, tag in pairs(disco_info) do
135                 if tag.name == "identity" then
136                         table.insert(identities, (tag.attr.category or "").."\0"..(tag.attr.type or "").."\0"..(tag.attr["xml:lang"] or "").."\0"..(tag.attr.name or ""));
137                 elseif tag.name == "feature" then
138                         table.insert(features, tag.attr.var or "");
139                 elseif tag.name == "x" and tag.attr.xmlns == "jabber:x:data" then
140                         local form = {};
141                         local FORM_TYPE;
142                         for _, field in pairs(tag.tags) do
143                                 if field.name == "field" and field.attr.var then
144                                         local values = {};
145                                         for _, val in pairs(field.tags) do
146                                                 val = #val.tags == 0 and table.concat(val); -- FIXME use get_text?
147                                                 if val then table.insert(values, val); end
148                                         end
149                                         table.sort(values);
150                                         if field.attr.var == "FORM_TYPE" then
151                                                 FORM_TYPE = values[1];
152                                         elseif #values > 0 then
153                                                 table.insert(form, field.attr.var.."\0"..table.concat(values, "<"));
154                                         else
155                                                 table.insert(form, field.attr.var);
156                                         end
157                                 end
158                         end
159                         table.sort(form);
160                         form = table.concat(form, "<");
161                         if FORM_TYPE then form = FORM_TYPE.."\0"..form; end
162                         table.insert(extensions, form);
163                 end
164         end
165         table.sort(identities);
166         table.sort(features);
167         table.sort(extensions);
168         if #identities > 0 then identities = table.concat(identities, "<"):gsub("%z", "/").."<"; else identities = ""; end
169         if #features > 0 then features = table.concat(features, "<").."<"; else features = ""; end
170         if #extensions > 0 then extensions = table.concat(extensions, "<"):gsub("%z", "<").."<"; else extensions = ""; end
171         local S = identities..features..extensions;
172         local ver = base64(sha1(S));
173         return ver, S;
174 end
175
176 module:hook("iq/bare/disco", function(event)
177         local session, stanza = event.origin, event.stanza;
178         if stanza.attr.type == "result" then
179                 local disco = stanza.tags[1];
180                 if disco and disco.name == "query" and disco.attr.xmlns == "http://jabber.org/protocol/disco#info" then
181                         -- Process disco response
182                         local user = stanza.attr.to or (session.username..'@'..session.host);
183                         local contact = stanza.attr.from;
184                         local current = recipients[user] and recipients[user][contact];
185                         if type(current) ~= "string" then return; end -- check if waiting for recipient's response
186                         local ver = current;
187                         if not string.find(current, "#") then
188                                 ver = calculate_hash(disco.tags); -- calculate hash
189                         end
190                         local notify = {};
191                         for _, feature in pairs(disco.tags) do
192                                 if feature.name == "feature" and feature.attr.var then
193                                         local nfeature = feature.attr.var:match("^(.*)+notify$");
194                                         if nfeature then notify[nfeature] = true; end
195                                 end
196                         end
197                         hash_map[ver] = notify; -- update hash map
198                         recipients[user][contact] = notify; -- set recipient's data to calculated data
199                         -- send messages to recipient
200                         publish_all(user, contact, session);
201                 end
202         end
203 end);