mod_ping: Convert from Windows line endings
[prosody.git] / plugins / mod_pep.lua
1
2 local jid_bare = require "util.jid".bare;
3 local jid_split = require "util.jid".split;
4 local st = require "util.stanza";
5 local hosts = hosts;
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;
9 local next = next;
10 local type = type;
11 local load_roster = require "core.rostermanager".load_roster;
12 local sha1 = require "util.hashes".sha1;
13 local base64 = require "util.encodings".base64.encode;
14
15 local NULL = {};
16 local data = {};
17 local recipients = {};
18 local hash_map = {};
19
20 module:add_identity("pubsub", "pep");
21 module:add_feature("http://jabber.org/protocol/pubsub#publish");
22
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})
29                                 :add_child(item)
30                         :up()
31                 :up();
32
33         -- store for the future
34         local user_data = data[bare];
35         if disable then
36                 if user_data then
37                         user_data[node] = nil;
38                         if not next(user_data) then data[bare] = nil; end
39                 end
40         else
41                 if not user_data then user_data = {}; data[bare] = user_data; end
42                 user_data[node] = stanza;
43         end
44         
45         -- broadcast
46         for recipient, notify in pairs(recipients[bare] or NULL) do
47                 if notify[node] then
48                         stanza.attr.to = recipient;
49                         core_post_stanza(session, stanza);
50                 end
51         end
52 end
53 local function publish_all(user, recipient, session)
54         local d = data[user];
55         local notify = recipients[user] and recipients[user][recipient];
56         if d and notify then
57                 for node in pairs(notify) do
58                         local message = d[node];
59                         if message then
60                                 message.attr.to = recipient;
61                                 session.send(message);
62                         end
63                 end
64         end
65 end
66
67 local function get_caps_hash_from_presence(stanza, current)
68         local t = stanza.attr.type;
69         if not t then
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
75                                 else -- legacy caps
76                                         if attr.node and attr.ver then return attr.node.."#"..attr.ver.."#"..(attr.ext or ""), attr.node.."#"..attr.ver; end
77                                 end
78                                 return; -- bad caps format
79                         end
80                 end
81         elseif t == "unavailable" or t == "error" then
82                 return;
83         end
84         return current; -- no caps, could mean caps optimization, so return current
85 end
86
87 module:hook("presence/bare", function(event)
88         -- inbound presence to bare JID recieved
89         local origin, stanza = event.origin, event.stanza;
90         
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
99                 if not hash then
100                         if recipients[user] then recipients[user][recipient] = nil; end
101                 else
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);
106                         else
107                                 recipients[user][recipient] = hash;
108                                 origin.send(
109                                         st.stanza("iq", {from=stanza.attr.to, to=stanza.attr.from, id="disco", type="get"})
110                                                 :query("http://jabber.org/protocol/disco#info")
111                                 );
112                         end
113                 end
114         end
115 end, 10);
116
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);
128                                         return true;
129                                 end
130                         end
131                 end
132         end
133 end);
134
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
143                         local form = {};
144                         local FORM_TYPE;
145                         for _, field in pairs(tag.tags) do
146                                 if field.name == "field" and field.attr.var then
147                                         local values = {};
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
151                                         end
152                                         table.sort(values);
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, "<"));
157                                         else
158                                                 table.insert(form, field.attr.var);
159                                         end
160                                 end
161                         end
162                         table.sort(form);
163                         form = table.concat(form, "<");
164                         if FORM_TYPE then form = FORM_TYPE.."\0"..form; end
165                         table.insert(extensions, form);
166                 end
167         end
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));
176         return ver, S;
177 end
178
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
189                         local ver = current;
190                         if not string.find(current, "#") then
191                                 ver = calculate_hash(disco.tags); -- calculate hash
192                         end
193                         local notify = {};
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
198                                 end
199                         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);
204                 end
205         end
206 end);