Comparison

plugins/mod_pep_simple.lua @ 9074:0462405b1cfb

mod_pep -> mod_pep_simple, mod_pep_plus -> mod_pep
author Matthew Wild <mwild1@gmail.com>
date Wed, 01 Aug 2018 19:08:09 +0100
parent 8967:plugins/mod_pep.lua@c809f334363c
child 9691:e11e076f0eb8
comparison
equal deleted inserted replaced
9073:a5daf3f6d588 9074:0462405b1cfb
1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 --
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8
9
10 local jid_bare = require "util.jid".bare;
11 local jid_split = require "util.jid".split;
12 local st = require "util.stanza";
13 local is_contact_subscribed = require "core.rostermanager".is_contact_subscribed;
14 local pairs = pairs;
15 local next = next;
16 local type = type;
17 local calculate_hash = require "util.caps".calculate_hash;
18 local core_post_stanza = prosody.core_post_stanza;
19 local bare_sessions = prosody.bare_sessions;
20
21 local xmlns_pubsub = "http://jabber.org/protocol/pubsub";
22
23 -- Used as canonical 'empty table'
24 local NULL = {};
25 -- data[user_bare_jid][node] = item_stanza
26 local data = {};
27 --- recipients[user_bare_jid][contact_full_jid][subscribed_node] = true
28 local recipients = {};
29 -- hash_map[hash][subscribed_nodes] = true
30 local hash_map = {};
31
32 module.save = function()
33 return { data = data, recipients = recipients, hash_map = hash_map };
34 end
35 module.restore = function(state)
36 data = state.data or {};
37 recipients = state.recipients or {};
38 hash_map = state.hash_map or {};
39 end
40
41 local function subscription_presence(user_bare, recipient)
42 local recipient_bare = jid_bare(recipient);
43 if (recipient_bare == user_bare) then return true end
44 local username, host = jid_split(user_bare);
45 return is_contact_subscribed(username, host, recipient_bare);
46 end
47
48 module:hook("pep-publish-item", function (event)
49 local session, bare, node, id, item = event.session, event.user, event.node, event.id, event.item;
50 item.attr.xmlns = nil;
51 local disable = #item.tags ~= 1 or #item.tags[1] == 0;
52 if #item.tags == 0 then item.name = "retract"; end
53 local stanza = st.message({from=bare, type='headline'})
54 :tag('event', {xmlns='http://jabber.org/protocol/pubsub#event'})
55 :tag('items', {node=node})
56 :add_child(item)
57 :up()
58 :up();
59
60 -- store for the future
61 local user_data = data[bare];
62 if disable then
63 if user_data then
64 user_data[node] = nil;
65 if not next(user_data) then data[bare] = nil; end
66 end
67 else
68 if not user_data then user_data = {}; data[bare] = user_data; end
69 user_data[node] = {id, item};
70 end
71
72 -- broadcast
73 for recipient, notify in pairs(recipients[bare] or NULL) do
74 if notify[node] then
75 stanza.attr.to = recipient;
76 core_post_stanza(session, stanza);
77 end
78 end
79 end);
80
81 local function publish_all(user, recipient, session)
82 local d = data[user];
83 local notify = recipients[user] and recipients[user][recipient];
84 if d and notify then
85 for node in pairs(notify) do
86 if d[node] then
87 local id, item = unpack(d[node]);
88 session.send(st.message({from=user, to=recipient, type='headline'})
89 :tag('event', {xmlns='http://jabber.org/protocol/pubsub#event'})
90 :tag('items', {node=node})
91 :add_child(item)
92 :up()
93 :up());
94 end
95 end
96 end
97 end
98
99 local function get_caps_hash_from_presence(stanza, current)
100 local t = stanza.attr.type;
101 if not t then
102 for _, child in pairs(stanza.tags) do
103 if child.name == "c" and child.attr.xmlns == "http://jabber.org/protocol/caps" then
104 local attr = child.attr;
105 if attr.hash then -- new caps
106 if attr.hash == 'sha-1' and attr.node and attr.ver then return attr.ver, attr.node.."#"..attr.ver; end
107 else -- legacy caps
108 if attr.node and attr.ver then return attr.node.."#"..attr.ver.."#"..(attr.ext or ""), attr.node.."#"..attr.ver; end
109 end
110 return; -- bad caps format
111 end
112 end
113 elseif t == "unavailable" or t == "error" then
114 return;
115 end
116 return current; -- no caps, could mean caps optimization, so return current
117 end
118
119 module:hook("presence/bare", function(event)
120 -- inbound presence to bare JID received
121 local origin, stanza = event.origin, event.stanza;
122 local user = stanza.attr.to or (origin.username..'@'..origin.host);
123 local t = stanza.attr.type;
124 local self = not stanza.attr.to;
125
126 -- Only cache subscriptions if user is online
127 if not bare_sessions[user] then return; end
128
129 if not t then -- available presence
130 if self or subscription_presence(user, stanza.attr.from) then
131 local recipient = stanza.attr.from;
132 local current = recipients[user] and recipients[user][recipient];
133 local hash = get_caps_hash_from_presence(stanza, current);
134 if current == hash or (current and current == hash_map[hash]) then return; end
135 if not hash then
136 if recipients[user] then recipients[user][recipient] = nil; end
137 else
138 recipients[user] = recipients[user] or {};
139 if hash_map[hash] then
140 recipients[user][recipient] = hash_map[hash];
141 publish_all(user, recipient, origin);
142 else
143 recipients[user][recipient] = hash;
144 local from_bare = origin.type == "c2s" and origin.username.."@"..origin.host;
145 if self or origin.type ~= "c2s" or (recipients[from_bare] and recipients[from_bare][origin.full_jid]) ~= hash then
146 -- COMPAT from ~= stanza.attr.to because OneTeam and Asterisk 1.8 can't deal with missing from attribute
147 origin.send(
148 st.stanza("iq", {from=user, to=stanza.attr.from, id="disco", type="get"})
149 :query("http://jabber.org/protocol/disco#info")
150 );
151 end
152 end
153 end
154 end
155 elseif t == "unavailable" then
156 if recipients[user] then recipients[user][stanza.attr.from] = nil; end
157 elseif not self and t == "unsubscribe" then
158 local from = jid_bare(stanza.attr.from);
159 local subscriptions = recipients[user];
160 if subscriptions then
161 for subscriber in pairs(subscriptions) do
162 if jid_bare(subscriber) == from then
163 recipients[user][subscriber] = nil;
164 end
165 end
166 end
167 end
168 end, 10);
169
170 module:hook("iq/bare/http://jabber.org/protocol/pubsub:pubsub", function(event)
171 local session, stanza = event.origin, event.stanza;
172 local payload = stanza.tags[1];
173
174 if stanza.attr.type == 'set' and (not stanza.attr.to or jid_bare(stanza.attr.from) == stanza.attr.to) then
175 payload = payload.tags[1]; -- <publish node='http://jabber.org/protocol/tune'>
176 if payload and (payload.name == 'publish' or payload.name == 'retract') and payload.attr.node then
177 local node = payload.attr.node;
178 payload = payload.tags[1];
179 if payload and payload.name == "item" then -- <item>
180 local id = payload.attr.id or "1";
181 payload.attr.id = id;
182 session.send(st.reply(stanza));
183 module:fire_event("pep-publish-item", {
184 node = node, user = jid_bare(session.full_jid), actor = session.jid,
185 id = id, session = session, item = st.clone(payload);
186 });
187 return true;
188 else
189 module:log("debug", "Payload is missing the <item>", node);
190 end
191 else
192 module:log("debug", "Unhandled payload: %s", payload and payload:top_tag() or "(no payload)");
193 end
194 elseif stanza.attr.type == 'get' then
195 local user = stanza.attr.to and jid_bare(stanza.attr.to) or session.username..'@'..session.host;
196 if subscription_presence(user, stanza.attr.from) then
197 local user_data = data[user];
198 local node, requested_id;
199 payload = payload.tags[1];
200 if payload and payload.name == 'items' then
201 node = payload.attr.node;
202 local item = payload.tags[1];
203 if item and item.name == "item" then
204 requested_id = item.attr.id;
205 end
206 end
207 if node and user_data and user_data[node] then -- Send the last item
208 local id, item = unpack(user_data[node]);
209 if not requested_id or id == requested_id then
210 local reply_stanza = st.reply(stanza)
211 :tag('pubsub', {xmlns='http://jabber.org/protocol/pubsub'})
212 :tag('items', {node=node})
213 :add_child(item)
214 :up()
215 :up();
216 session.send(reply_stanza);
217 return true;
218 else -- requested item doesn't exist
219 local reply_stanza = st.reply(stanza)
220 :tag('pubsub', {xmlns='http://jabber.org/protocol/pubsub'})
221 :tag('items', {node=node})
222 :up();
223 session.send(reply_stanza);
224 return true;
225 end
226 elseif node then -- node doesn't exist
227 session.send(st.error_reply(stanza, 'cancel', 'item-not-found'));
228 module:log("debug", "Item '%s' not found", node)
229 return true;
230 else --invalid request
231 session.send(st.error_reply(stanza, 'modify', 'bad-request'));
232 module:log("debug", "Invalid request: %s", tostring(payload));
233 return true;
234 end
235 else --no presence subscription
236 session.send(st.error_reply(stanza, 'auth', 'not-authorized')
237 :tag('presence-subscription-required', {xmlns='http://jabber.org/protocol/pubsub#errors'}));
238 module:log("debug", "Unauthorized request: %s", tostring(payload));
239 return true;
240 end
241 end
242 end);
243
244 module:hook("iq-result/bare/disco", function(event)
245 local session, stanza = event.origin, event.stanza;
246 if stanza.attr.type == "result" then
247 local disco = stanza.tags[1];
248 if disco and disco.name == "query" and disco.attr.xmlns == "http://jabber.org/protocol/disco#info" then
249 -- Process disco response
250 local self = not stanza.attr.to;
251 local user = stanza.attr.to or (session.username..'@'..session.host);
252 local contact = stanza.attr.from;
253 local current = recipients[user] and recipients[user][contact];
254 if type(current) ~= "string" then return; end -- check if waiting for recipient's response
255 local ver = current;
256 if not string.find(current, "#") then
257 ver = calculate_hash(disco.tags); -- calculate hash
258 end
259 local notify = {};
260 for _, feature in pairs(disco.tags) do
261 if feature.name == "feature" and feature.attr.var then
262 local nfeature = feature.attr.var:match("^(.*)%+notify$");
263 if nfeature then notify[nfeature] = true; end
264 end
265 end
266 hash_map[ver] = notify; -- update hash map
267 if self then
268 for jid, item in pairs(session.roster) do -- for all interested contacts
269 if item.subscription == "both" or item.subscription == "from" then
270 if not recipients[jid] then recipients[jid] = {}; end
271 recipients[jid][contact] = notify;
272 publish_all(jid, contact, session);
273 end
274 end
275 end
276 recipients[user][contact] = notify; -- set recipient's data to calculated data
277 -- send messages to recipient
278 publish_all(user, contact, session);
279 end
280 end
281 end);
282
283 module:hook("account-disco-info", function(event)
284 local reply = event.reply;
285 reply:tag('identity', {category='pubsub', type='pep'}):up();
286 reply:tag('feature', {var=xmlns_pubsub}):up();
287 local features = {
288 "access-presence",
289 "auto-create",
290 "auto-subscribe",
291 "filtered-notifications",
292 "item-ids",
293 "last-published",
294 "presence-notifications",
295 "presence-subscribe",
296 "publish",
297 "retract-items",
298 "retrieve-items",
299 };
300 for _, feature in ipairs(features) do
301 reply:tag('feature', {var=xmlns_pubsub.."#"..feature}):up();
302 end
303 end);
304
305 module:hook("account-disco-items", function(event)
306 local reply = event.reply;
307 local bare = reply.attr.to;
308 local user_data = data[bare];
309
310 if user_data then
311 for node, _ in pairs(user_data) do
312 reply:tag('item', {jid=bare, node=node}):up();
313 end
314 end
315 end);
316
317 module:hook("account-disco-info-node", function (event)
318 local stanza, node = event.stanza, event.node;
319 local user = stanza.attr.to;
320 local user_data = data[user];
321 if user_data and user_data[node] then
322 event.exists = true;
323 event.reply:tag('identity', {category='pubsub', type='leaf'}):up();
324 end
325 end);
326
327 module:hook("resource-unbind", function (event)
328 local user_bare_jid = event.session.username.."@"..event.session.host;
329 if not bare_sessions[user_bare_jid] then -- User went offline
330 -- We don't need this info cached anymore, clear it.
331 recipients[user_bare_jid] = nil;
332 end
333 end);