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];
37 user_data[node] = nil;
38 if not next(user_data) then data[bare] = nil; end
41 if not user_data then user_data = {}; data[bare] = user_data; end
42 user_data[node] = stanza;
46 for recipient, notify in pairs(recipients[bare] or NULL) do
48 stanza.attr.to = recipient;
49 core_post_stanza(session, stanza);
53 local function publish_all(user, recipient, session)
55 local notify = recipients[user] and recipients[user][recipient];
57 for node in pairs(notify) do
58 local message = d[node];
60 message.attr.to = recipient;
61 session.send(message);
67 local function get_caps_hash_from_presence(stanza, current)
68 local t = stanza.attr.type;
70 for _, child in pairs(stanza.tags) do
71 if child.name == "c" and child.attr.xmlns == "http://jabber.org/protocol/caps" then
72 local attr = child.attr;
73 if attr.hash then -- new caps
74 if attr.hash == 'sha-1' and attr.node and attr.ver then return attr.ver, attr.node.."#"..attr.ver; end
76 if attr.node and attr.ver then return attr.node.."#"..attr.ver.."#"..(attr.ext or ""), attr.node.."#"..attr.ver; end
78 return; -- bad caps format
81 elseif t == "unavailable" or t == "error" then
84 return current; -- no caps, could mean caps optimization, so return current
87 module:hook("presence/bare", function(event)
88 -- inbound presence to bare JID recieved
89 local origin, stanza = event.origin, event.stanza;
91 local user = stanza.attr.to or (origin.username..'@'..origin.host);
92 local bare = jid_bare(stanza.attr.from);
93 local item = load_roster(jid_split(user))[bare];
94 if not stanza.attr.to or (item and (item.subscription == 'from' or item.subscription == 'both')) then
95 local recipient = stanza.attr.from;
96 local current = recipients[user] and recipients[user][recipient];
97 local hash = get_caps_hash_from_presence(stanza, current);
98 if current == hash then return; end
100 if recipients[user] then recipients[user][recipient] = nil; end
102 recipients[user] = recipients[user] or {};
103 if hash_map[hash] then
104 recipients[user][recipient] = hash_map[hash];
105 publish_all(user, recipient, origin);
107 recipients[user][recipient] = hash;
109 st.stanza("iq", {from=stanza.attr.to, to=stanza.attr.from, id="disco", type="get"})
110 :query("http://jabber.org/protocol/disco#info")
117 module:hook("iq/bare/http://jabber.org/protocol/pubsub:pubsub", function(event)
118 local session, stanza = event.origin, event.stanza;
119 if stanza.attr.type == 'set' and (not stanza.attr.to or jid_bare(stanza.attr.from) == stanza.attr.to) then
120 local payload = stanza.tags[1];
121 if payload.name == 'pubsub' then -- <pubsub xmlns='http://jabber.org/protocol/pubsub'>
122 payload = payload.tags[1];
123 if payload and payload.name == 'publish' and payload.attr.node then -- <publish node='http://jabber.org/protocol/tune'>
124 local node = payload.attr.node;
125 payload = payload.tags[1];
126 if payload then -- <item>
127 publish(session, node, payload);
135 local function calculate_hash(disco_info)
136 local identities, features, extensions = {}, {}, {};
137 for _, tag in pairs(disco_info) do
138 if tag.name == "identity" then
139 table.insert(identities, (tag.attr.category or "").."\0"..(tag.attr.type or "").."\0"..(tag.attr["xml:lang"] or "").."\0"..(tag.attr.name or ""));
140 elseif tag.name == "feature" then
141 table.insert(features, tag.attr.var or "");
142 elseif tag.name == "x" and tag.attr.xmlns == "jabber:x:data" then
145 for _, field in pairs(tag.tags) do
146 if field.name == "field" and field.attr.var then
148 for _, val in pairs(field.tags) do
149 val = #val.tags == 0 and table.concat(val); -- FIXME use get_text?
150 if val then table.insert(values, val); end
153 if field.attr.var == "FORM_TYPE" then
154 FORM_TYPE = values[1];
155 elseif #values > 0 then
156 table.insert(form, field.attr.var.."\0"..table.concat(values, "<"));
158 table.insert(form, field.attr.var);
163 form = table.concat(form, "<");
164 if FORM_TYPE then form = FORM_TYPE.."\0"..form; end
165 table.insert(extensions, form);
168 table.sort(identities);
169 table.sort(features);
170 table.sort(extensions);
171 if #identities > 0 then identities = table.concat(identities, "<"):gsub("%z", "/").."<"; else identities = ""; end
172 if #features > 0 then features = table.concat(features, "<").."<"; else features = ""; end
173 if #extensions > 0 then extensions = table.concat(extensions, "<"):gsub("%z", "<").."<"; else extensions = ""; end
174 local S = identities..features..extensions;
175 local ver = base64(sha1(S));
179 module:hook("iq/bare/disco", function(event)
180 local session, stanza = event.origin, event.stanza;
181 if stanza.attr.type == "result" then
182 local disco = stanza.tags[1];
183 if disco and disco.name == "query" and disco.attr.xmlns == "http://jabber.org/protocol/disco#info" then
184 -- Process disco response
185 local user = stanza.attr.to or (session.username..'@'..session.host);
186 local contact = stanza.attr.from;
187 local current = recipients[user] and recipients[user][contact];
188 if type(current) ~= "string" then return; end -- check if waiting for recipient's response
190 if not string.find(current, "#") then
191 ver = calculate_hash(disco.tags); -- calculate hash
194 for _, feature in pairs(disco.tags) do
195 if feature.name == "feature" and feature.attr.var then
196 local nfeature = feature.attr.var:match("^(.*)%+notify$");
197 if nfeature then notify[nfeature] = true; end
200 hash_map[ver] = notify; -- update hash map
201 recipients[user][contact] = notify; -- set recipient's data to calculated data
202 -- send messages to recipient
203 publish_all(user, contact, session);