2 local jid_bare = require "util.jid".bare;
3 local jid_split = require "util.jid".split;
4 local st = require "util.stanza";
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;
11 local load_roster = require "core.rostermanager".load_roster;
12 local sha1 = require "util.hashes".sha1;
13 local base64 = require "util.encodings".base64.encode;
17 local recipients = {};
20 module:add_identity("pubsub", "pep");
21 module:add_feature("http://jabber.org/protocol/pubsub#publish");
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})
33 -- store for the future
34 local user_data = data[bare];
36 if user_data then user_data[node] = nil; end
37 if not next(user_data) then data[bare] = nil; end
39 if not user_data then user_data = {}; data[bare] = user_data; end
40 user_data[node] = stanza;
44 for recipient, notify in pairs(recipients[bare] or NULL) do
46 stanza.attr.to = recipient;
47 core_post_stanza(session, stanza);
51 local function publish_all(user, recipient, session)
53 local notify = recipients[user] and recipients[user][recipient];
55 for node, message in pairs(notify) do
57 message.attr.to = recipient;
58 session.send(message);
64 local function get_caps_hash_from_presence(stanza, current)
65 local t = stanza.attr.type;
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
73 if attr.node and attr.ver then return attr.node.."#"..attr.ver.."#"..(attr.ext or ""), attr.node.."#"..attr.ver; end
75 return; -- bad caps format
78 elseif t == "unavailable" or t == "error" then
81 return current; -- no caps, could mean caps optimization, so return current
84 module:hook("presence/bare", function(event)
85 -- inbound presence to bare JID recieved
86 local origin, stanza = event.origin, event.stanza;
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
97 if recipients[user] then recipients[user][recipient] = nil; end
99 recipients[user] = recipients[user] or {};
100 if hash_map[hash] then
101 recipients[user][recipient] = hash_map[hash];
102 publish_all(user, recipient);
104 recipients[user][recipient] = hash;
106 st.stanza("iq", {from=stanza.attr.to, to=stanza.attr.from, id="disco", type="get"})
107 :query("http://jabber.org/protocol/disco#info")
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);
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
142 for _, field in pairs(tag.tags) do
143 if field.name == "field" and field.attr.var then
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
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, "<"));
155 table.insert(form, field.attr.var);
160 form = table.concat(form, "<");
161 if FORM_TYPE then form = FORM_TYPE.."\0"..form; end
162 table.insert(extensions, form);
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));
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
187 if not string.find(current, "#") then
188 ver = calculate_hash(disco.tags); -- calculate hash
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
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);