8cc4aedfb6004a5c85417802c8608273b8a2fc8a
[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         local bare = session.username..'@'..session.host;
34         local stanza = st.message({from=bare, type='headline'})
35                 :tag('event', {xmlns='http://jabber.org/protocol/pubsub#event'})
36                         :tag('items', {node=node})
37                                 :add_child(item)
38                         :up()
39                 :up();
40
41         -- store for the future
42         local user_data = data[bare];
43         if disable then
44                 if user_data then
45                         user_data[node] = nil;
46                         if not next(user_data) then data[bare] = nil; end
47                 end
48         else
49                 if not user_data then user_data = {}; data[bare] = user_data; end
50                 user_data[node] = stanza;
51         end
52         
53         -- broadcast
54         for recipient, notify in pairs(recipients[bare] or NULL) do
55                 if notify[node] then
56                         stanza.attr.to = recipient;
57                         core_post_stanza(session, stanza);
58                 end
59         end
60 end
61 local function publish_all(user, recipient, session)
62         local d = data[user];
63         local notify = recipients[user] and recipients[user][recipient];
64         if d and notify then
65                 for node in pairs(notify) do
66                         local message = d[node];
67                         if message then
68                                 message.attr.to = recipient;
69                                 session.send(message);
70                         end
71                 end
72         end
73 end
74
75 local function get_caps_hash_from_presence(stanza, current)
76         local t = stanza.attr.type;
77         if not t then
78                 for _, child in pairs(stanza.tags) do
79                         if child.name == "c" and child.attr.xmlns == "http://jabber.org/protocol/caps" then
80                                 local attr = child.attr;
81                                 if attr.hash then -- new caps
82                                         if attr.hash == 'sha-1' and attr.node and attr.ver then return attr.ver, attr.node.."#"..attr.ver; end
83                                 else -- legacy caps
84                                         if attr.node and attr.ver then return attr.node.."#"..attr.ver.."#"..(attr.ext or ""), attr.node.."#"..attr.ver; end
85                                 end
86                                 return; -- bad caps format
87                         end
88                 end
89         elseif t == "unavailable" or t == "error" then
90                 return;
91         end
92         return current; -- no caps, could mean caps optimization, so return current
93 end
94
95 module:hook("presence/bare", function(event)
96         -- inbound presence to bare JID recieved
97         local origin, stanza = event.origin, event.stanza;
98         
99         local user = stanza.attr.to or (origin.username..'@'..origin.host);
100         local bare = jid_bare(stanza.attr.from);
101         local item = load_roster(jid_split(user))[bare];
102         if not stanza.attr.to or (item and (item.subscription == 'from' or item.subscription == 'both')) then
103                 local recipient = stanza.attr.from;
104                 local current = recipients[user] and recipients[user][recipient];
105                 local hash = get_caps_hash_from_presence(stanza, current);
106                 if current == hash then return; end
107                 if not hash then
108                         if recipients[user] then recipients[user][recipient] = nil; end
109                 else
110                         recipients[user] = recipients[user] or {};
111                         if hash_map[hash] then
112                                 recipients[user][recipient] = hash_map[hash];
113                                 publish_all(user, recipient, origin);
114                         else
115                                 recipients[user][recipient] = hash;
116                                 origin.send(
117                                         st.stanza("iq", {from=stanza.attr.to, to=stanza.attr.from, id="disco", type="get"})
118                                                 :query("http://jabber.org/protocol/disco#info")
119                                 );
120                         end
121                 end
122         end
123 end, 10);
124
125 module:hook("iq/bare/http://jabber.org/protocol/pubsub:pubsub", function(event)
126         local session, stanza = event.origin, event.stanza;
127         if stanza.attr.type == 'set' and (not stanza.attr.to or jid_bare(stanza.attr.from) == stanza.attr.to) then
128                 local payload = stanza.tags[1];
129                 if payload.name == 'pubsub' then -- <pubsub xmlns='http://jabber.org/protocol/pubsub'>
130                         payload = payload.tags[1];
131                         if payload and payload.name == 'publish' and payload.attr.node then -- <publish node='http://jabber.org/protocol/tune'>
132                                 local node = payload.attr.node;
133                                 payload = payload.tags[1];
134                                 if payload then -- <item>
135                                         publish(session, node, payload);
136                                         return true;
137                                 end
138                         end
139                 end
140         end
141 end);
142
143 local function calculate_hash(disco_info)
144         local identities, features, extensions = {}, {}, {};
145         for _, tag in pairs(disco_info) do
146                 if tag.name == "identity" then
147                         table.insert(identities, (tag.attr.category or "").."\0"..(tag.attr.type or "").."\0"..(tag.attr["xml:lang"] or "").."\0"..(tag.attr.name or ""));
148                 elseif tag.name == "feature" then
149                         table.insert(features, tag.attr.var or "");
150                 elseif tag.name == "x" and tag.attr.xmlns == "jabber:x:data" then
151                         local form = {};
152                         local FORM_TYPE;
153                         for _, field in pairs(tag.tags) do
154                                 if field.name == "field" and field.attr.var then
155                                         local values = {};
156                                         for _, val in pairs(field.tags) do
157                                                 val = #val.tags == 0 and table.concat(val); -- FIXME use get_text?
158                                                 if val then table.insert(values, val); end
159                                         end
160                                         table.sort(values);
161                                         if field.attr.var == "FORM_TYPE" then
162                                                 FORM_TYPE = values[1];
163                                         elseif #values > 0 then
164                                                 table.insert(form, field.attr.var.."\0"..table.concat(values, "<"));
165                                         else
166                                                 table.insert(form, field.attr.var);
167                                         end
168                                 end
169                         end
170                         table.sort(form);
171                         form = table.concat(form, "<");
172                         if FORM_TYPE then form = FORM_TYPE.."\0"..form; end
173                         table.insert(extensions, form);
174                 end
175         end
176         table.sort(identities);
177         table.sort(features);
178         table.sort(extensions);
179         if #identities > 0 then identities = table.concat(identities, "<"):gsub("%z", "/").."<"; else identities = ""; end
180         if #features > 0 then features = table.concat(features, "<").."<"; else features = ""; end
181         if #extensions > 0 then extensions = table.concat(extensions, "<"):gsub("%z", "<").."<"; else extensions = ""; end
182         local S = identities..features..extensions;
183         local ver = base64(sha1(S));
184         return ver, S;
185 end
186
187 module:hook("iq/bare/disco", function(event)
188         local session, stanza = event.origin, event.stanza;
189         if stanza.attr.type == "result" then
190                 local disco = stanza.tags[1];
191                 if disco and disco.name == "query" and disco.attr.xmlns == "http://jabber.org/protocol/disco#info" then
192                         -- Process disco response
193                         local user = stanza.attr.to or (session.username..'@'..session.host);
194                         local contact = stanza.attr.from;
195                         local current = recipients[user] and recipients[user][contact];
196                         if type(current) ~= "string" then return; end -- check if waiting for recipient's response
197                         local ver = current;
198                         if not string.find(current, "#") then
199                                 ver = calculate_hash(disco.tags); -- calculate hash
200                         end
201                         local notify = {};
202                         for _, feature in pairs(disco.tags) do
203                                 if feature.name == "feature" and feature.attr.var then
204                                         local nfeature = feature.attr.var:match("^(.*)%+notify$");
205                                         if nfeature then notify[nfeature] = true; end
206                                 end
207                         end
208                         hash_map[ver] = notify; -- update hash map
209                         recipients[user][contact] = notify; -- set recipient's data to calculated data
210                         -- send messages to recipient
211                         publish_all(user, contact, session);
212                 end
213         end
214 end);