2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
10 local jid_bare = require "util.jid".bare;
11 local jid_split = require "util.jid".split;
12 local st = require "util.stanza";
14 local user_exists = require "core.usermanager".user_exists;
15 local is_contact_subscribed = require "core.rostermanager".is_contact_subscribed;
16 local pairs, ipairs = pairs, ipairs;
19 local load_roster = require "core.rostermanager".load_roster;
20 local sha1 = require "util.hashes".sha1;
21 local base64 = require "util.encodings".base64.encode;
25 local recipients = {};
28 module:add_identity("pubsub", "pep");
29 module:add_feature("http://jabber.org/protocol/pubsub#publish");
31 local function publish(session, node, item)
32 local disable = #item.tags ~= 1 or #item.tags[1].tags == 0;
33 if #item.tags == 0 then item.name = "retract"; end
34 local bare = session.username..'@'..session.host;
35 local stanza = st.message({from=bare, type='headline'})
36 :tag('event', {xmlns='http://jabber.org/protocol/pubsub#event'})
37 :tag('items', {node=node})
42 -- store for the future
43 local user_data = data[bare];
46 user_data[node] = nil;
47 if not next(user_data) then data[bare] = nil; end
50 if not user_data then user_data = {}; data[bare] = user_data; end
51 user_data[node] = stanza;
55 for recipient, notify in pairs(recipients[bare] or NULL) do
57 stanza.attr.to = recipient;
58 core_post_stanza(session, stanza);
62 local function publish_all(user, recipient, session)
64 local notify = recipients[user] and recipients[user][recipient];
66 for node in pairs(notify) do
67 local message = d[node];
69 message.attr.to = recipient;
70 session.send(message);
76 local function get_caps_hash_from_presence(stanza, current)
77 local t = stanza.attr.type;
79 for _, child in pairs(stanza.tags) do
80 if child.name == "c" and child.attr.xmlns == "http://jabber.org/protocol/caps" then
81 local attr = child.attr;
82 if attr.hash then -- new caps
83 if attr.hash == 'sha-1' and attr.node and attr.ver then return attr.ver, attr.node.."#"..attr.ver; end
85 if attr.node and attr.ver then return attr.node.."#"..attr.ver.."#"..(attr.ext or ""), attr.node.."#"..attr.ver; end
87 return; -- bad caps format
90 elseif t == "unavailable" or t == "error" then
93 return current; -- no caps, could mean caps optimization, so return current
96 module:hook("presence/bare", function(event)
97 -- inbound presence to bare JID recieved
98 local origin, stanza = event.origin, event.stanza;
100 local user = stanza.attr.to or (origin.username..'@'..origin.host);
101 local bare = jid_bare(stanza.attr.from);
102 local item = load_roster(jid_split(user))[bare];
103 if not stanza.attr.to or (item and (item.subscription == 'from' or item.subscription == 'both')) then
104 local recipient = stanza.attr.from;
105 local current = recipients[user] and recipients[user][recipient];
106 local hash = get_caps_hash_from_presence(stanza, current);
107 if current == hash then return; end
109 if recipients[user] then recipients[user][recipient] = nil; end
111 recipients[user] = recipients[user] or {};
112 if hash_map[hash] then
113 recipients[user][recipient] = hash_map[hash];
114 publish_all(user, recipient, origin);
116 recipients[user][recipient] = hash;
118 st.stanza("iq", {from=stanza.attr.to, to=stanza.attr.from, id="disco", type="get"})
119 :query("http://jabber.org/protocol/disco#info")
126 module:hook("iq/bare/http://jabber.org/protocol/pubsub:pubsub", function(event)
127 local session, stanza = event.origin, event.stanza;
128 if stanza.attr.type == 'set' and (not stanza.attr.to or jid_bare(stanza.attr.from) == stanza.attr.to) then
129 local payload = stanza.tags[1];
130 if payload.name == 'pubsub' then -- <pubsub xmlns='http://jabber.org/protocol/pubsub'>
131 payload = payload.tags[1];
132 if payload and (payload.name == 'publish' or payload.name == 'retract') and payload.attr.node then -- <publish node='http://jabber.org/protocol/tune'>
133 local node = payload.attr.node;
134 payload = payload.tags[1];
135 if payload then -- <item>
136 publish(session, node, payload);
137 session.send(st.reply(stanza));
145 local function calculate_hash(disco_info)
146 local identities, features, extensions = {}, {}, {};
147 for _, tag in pairs(disco_info) do
148 if tag.name == "identity" then
149 table.insert(identities, (tag.attr.category or "").."\0"..(tag.attr.type or "").."\0"..(tag.attr["xml:lang"] or "").."\0"..(tag.attr.name or ""));
150 elseif tag.name == "feature" then
151 table.insert(features, tag.attr.var or "");
152 elseif tag.name == "x" and tag.attr.xmlns == "jabber:x:data" then
155 for _, field in pairs(tag.tags) do
156 if field.name == "field" and field.attr.var then
158 for _, val in pairs(field.tags) do
159 val = #val.tags == 0 and table.concat(val); -- FIXME use get_text?
160 if val then table.insert(values, val); end
163 if field.attr.var == "FORM_TYPE" then
164 FORM_TYPE = values[1];
165 elseif #values > 0 then
166 table.insert(form, field.attr.var.."\0"..table.concat(values, "<"));
168 table.insert(form, field.attr.var);
173 form = table.concat(form, "<");
174 if FORM_TYPE then form = FORM_TYPE.."\0"..form; end
175 table.insert(extensions, form);
178 table.sort(identities);
179 table.sort(features);
180 table.sort(extensions);
181 if #identities > 0 then identities = table.concat(identities, "<"):gsub("%z", "/").."<"; else identities = ""; end
182 if #features > 0 then features = table.concat(features, "<").."<"; else features = ""; end
183 if #extensions > 0 then extensions = table.concat(extensions, "<"):gsub("%z", "<").."<"; else extensions = ""; end
184 local S = identities..features..extensions;
185 local ver = base64(sha1(S));
189 module:hook("iq/bare/disco", function(event)
190 local session, stanza = event.origin, event.stanza;
191 if stanza.attr.type == "result" then
192 local disco = stanza.tags[1];
193 if disco and disco.name == "query" and disco.attr.xmlns == "http://jabber.org/protocol/disco#info" then
194 -- Process disco response
195 local user = stanza.attr.to or (session.username..'@'..session.host);
196 local contact = stanza.attr.from;
197 local current = recipients[user] and recipients[user][contact];
198 if type(current) ~= "string" then return; end -- check if waiting for recipient's response
200 if not string.find(current, "#") then
201 ver = calculate_hash(disco.tags); -- calculate hash
204 for _, feature in pairs(disco.tags) do
205 if feature.name == "feature" and feature.attr.var then
206 local nfeature = feature.attr.var:match("^(.*)%+notify$");
207 if nfeature then notify[nfeature] = true; end
210 hash_map[ver] = notify; -- update hash map
211 recipients[user][contact] = notify; -- set recipient's data to calculated data
212 -- send messages to recipient
213 publish_all(user, contact, session);