core.s2smanager, xmppserver_listener: Move the responsibility of setting session...
[prosody.git] / plugins / mod_pep.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 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 hosts = hosts;
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;
17 local next = next;
18 local type = type;
19 local load_roster = require "core.rostermanager".load_roster;
20 local sha1 = require "util.hashes".sha1;
21 local base64 = require "util.encodings".base64.encode;
22
23 local NULL = {};
24 local data = {};
25 local recipients = {};
26 local hash_map = {};
27
28 module:add_identity("pubsub", "pep");
29 module:add_feature("http://jabber.org/protocol/pubsub#publish");
30
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})
38                                 :add_child(item)
39                         :up()
40                 :up();
41
42         -- store for the future
43         local user_data = data[bare];
44         if disable then
45                 if user_data then
46                         user_data[node] = nil;
47                         if not next(user_data) then data[bare] = nil; end
48                 end
49         else
50                 if not user_data then user_data = {}; data[bare] = user_data; end
51                 user_data[node] = stanza;
52         end
53         
54         -- broadcast
55         for recipient, notify in pairs(recipients[bare] or NULL) do
56                 if notify[node] then
57                         stanza.attr.to = recipient;
58                         core_post_stanza(session, stanza);
59                 end
60         end
61 end
62 local function publish_all(user, recipient, session)
63         local d = data[user];
64         local notify = recipients[user] and recipients[user][recipient];
65         if d and notify then
66                 for node in pairs(notify) do
67                         local message = d[node];
68                         if message then
69                                 message.attr.to = recipient;
70                                 session.send(message);
71                         end
72                 end
73         end
74 end
75
76 local function get_caps_hash_from_presence(stanza, current)
77         local t = stanza.attr.type;
78         if not t then
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
84                                 else -- legacy caps
85                                         if attr.node and attr.ver then return attr.node.."#"..attr.ver.."#"..(attr.ext or ""), attr.node.."#"..attr.ver; end
86                                 end
87                                 return; -- bad caps format
88                         end
89                 end
90         elseif t == "unavailable" or t == "error" then
91                 return;
92         end
93         return current; -- no caps, could mean caps optimization, so return current
94 end
95
96 module:hook("presence/bare", function(event)
97         -- inbound presence to bare JID recieved
98         local origin, stanza = event.origin, event.stanza;
99         
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
108                 if not hash then
109                         if recipients[user] then recipients[user][recipient] = nil; end
110                 else
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);
115                         else
116                                 recipients[user][recipient] = hash;
117                                 origin.send(
118                                         st.stanza("iq", {from=stanza.attr.to, to=stanza.attr.from, id="disco", type="get"})
119                                                 :query("http://jabber.org/protocol/disco#info")
120                                 );
121                         end
122                 end
123         end
124 end, 10);
125
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));
138                                         return true;
139                                 end
140                         end
141                 end
142         end
143 end);
144
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
153                         local form = {};
154                         local FORM_TYPE;
155                         for _, field in pairs(tag.tags) do
156                                 if field.name == "field" and field.attr.var then
157                                         local values = {};
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
161                                         end
162                                         table.sort(values);
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, "<"));
167                                         else
168                                                 table.insert(form, field.attr.var);
169                                         end
170                                 end
171                         end
172                         table.sort(form);
173                         form = table.concat(form, "<");
174                         if FORM_TYPE then form = FORM_TYPE.."\0"..form; end
175                         table.insert(extensions, form);
176                 end
177         end
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));
186         return ver, S;
187 end
188
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
199                         local ver = current;
200                         if not string.find(current, "#") then
201                                 ver = calculate_hash(disco.tags); -- calculate hash
202                         end
203                         local notify = {};
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
208                                 end
209                         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);
214                 end
215         end
216 end);