Comparison

plugins/mod_pep.lua @ 1437:cf2eba9b1716

mod_pep: Entity capabilities
author Waqas Hussain <waqas20@gmail.com>
date Sun, 28 Jun 2009 21:29:39 +0500
parent 1430:39169cf8d36f
child 1439:f989608964ec
comparison
equal deleted inserted replaced
1431:9fe9ba693f4a 1437:cf2eba9b1716
5 local hosts = hosts; 5 local hosts = hosts;
6 local user_exists = require "core.usermanager".user_exists; 6 local user_exists = require "core.usermanager".user_exists;
7 local is_contact_subscribed = require "core.rostermanager".is_contact_subscribed; 7 local is_contact_subscribed = require "core.rostermanager".is_contact_subscribed;
8 local pairs, ipairs = pairs, ipairs; 8 local pairs, ipairs = pairs, ipairs;
9 local next = next; 9 local next = next;
10 local type = type;
10 local load_roster = require "core.rostermanager".load_roster; 11 local load_roster = require "core.rostermanager".load_roster;
12 local sha1 = require "util.hashes".sha1;
13 local base64 = require "util.encodings".base64.encode;
11 14
12 local NULL = {}; 15 local NULL = {};
13 local data = {}; 16 local data = {};
14 local recipients = {}; 17 local recipients = {};
18 local hash_map = {};
15 19
16 module:add_identity("pubsub", "pep"); 20 module:add_identity("pubsub", "pep");
17 module:add_feature("http://jabber.org/protocol/pubsub#publish"); 21 module:add_feature("http://jabber.org/protocol/pubsub#publish");
18 22
19 local function publish(session, node, item) 23 local function publish(session, node, item)
35 if not user_data then user_data = {}; data[bare] = user_data; end 39 if not user_data then user_data = {}; data[bare] = user_data; end
36 user_data[node] = stanza; 40 user_data[node] = stanza;
37 end 41 end
38 42
39 -- broadcast 43 -- broadcast
40 for recipient in pairs(recipients[user] or NULL) do 44 for recipient, notify in pairs(recipients[bare] or NULL) do
41 stanza.attr.to = recipient; 45 if notify[node] then
42 core_post_stanza(session, stanza); 46 stanza.attr.to = recipient;
47 core_post_stanza(session, stanza);
48 end
43 end 49 end
44 end 50 end
45 51
46 local function get_caps_hash_from_presence(stanza, current) 52 local function get_caps_hash_from_presence(stanza, current)
47 local t = stanza.attr.type; 53 local t = stanza.attr.type;
78 if not hash then 84 if not hash then
79 if recipients[user] then recipients[user][recipient] = nil; end 85 if recipients[user] then recipients[user][recipient] = nil; end
80 else 86 else
81 recipients[user] = recipients[user] or {}; 87 recipients[user] = recipients[user] or {};
82 recipients[user][recipient] = hash; 88 recipients[user][recipient] = hash;
83 for node, message in pairs(data[user] or NULL) do 89 origin.send(
84 message.attr.to = stanza.attr.from; 90 st.stanza("iq", {from=stanza.attr.to, to=stanza.attr.from, id="disco", type="get"})
85 origin.send(message); 91 :query("http://jabber.org/protocol/disco#info")
86 end 92 );
87 end 93 end
88 end 94 end
89 end, 10); 95 end, 10);
90 96
91 module:hook("iq/bare/http://jabber.org/protocol/pubsub:pubsub", function(event) 97 module:hook("iq/bare/http://jabber.org/protocol/pubsub:pubsub", function(event)
104 end 110 end
105 end 111 end
106 end 112 end
107 end); 113 end);
108 114
115 local function calculate_hash(disco_info)
116 local identities, features, extensions = {}, {}, {};
117 for _, tag in pairs(disco_info) do
118 if tag.name == "identity" then
119 table.insert(identities, (tag.attr.category or "").."\0"..(tag.attr.type or "").."\0"..(tag.attr["xml:lang"] or "").."\0"..(tag.attr.name or ""));
120 elseif tag.name == "feature" then
121 table.insert(features, tag.attr.var or "");
122 elseif tag.name == "x" and tag.attr.xmlns == "jabber:x:data" then
123 local form = {};
124 local FORM_TYPE;
125 for _, field in pairs(tag.tags) do
126 if field.name == "field" and field.attr.var then
127 local values = {};
128 for _, val in pairs(field.tags) do
129 val = #val.tags == 0 and table.concat(val); -- FIXME use get_text?
130 if val then table.insert(values, val); end
131 end
132 table.sort(values);
133 if field.attr.var == "FORM_TYPE" then
134 FORM_TYPE = values[1];
135 elseif #values > 0 then
136 table.insert(form, field.attr.var.."\0"..table.concat(values, "<"));
137 else
138 table.insert(form, field.attr.var);
139 end
140 end
141 end
142 table.sort(form);
143 form = table.concat(form, "<");
144 if FORM_TYPE then form = FORM_TYPE.."\0"..form; end
145 table.insert(extensions, form);
146 end
147 end
148 table.sort(identities);
149 table.sort(features);
150 table.sort(extensions);
151 if #identities > 0 then identities = table.concat(identities, "<"):gsub("%z", "/").."<"; else identities = ""; end
152 if #features > 0 then features = table.concat(features, "<").."<"; else features = ""; end
153 if #extensions > 0 then extensions = table.concat(extensions, "<"):gsub("%z", "<").."<"; else extensions = ""; end
154 local S = identities..features..extensions;
155 local ver = base64(sha1(S));
156 return ver, S;
157 end
158
109 module:hook("iq/bare/disco", function(event) 159 module:hook("iq/bare/disco", function(event)
110 local session, stanza = event.origin, event.stanza; 160 local session, stanza = event.origin, event.stanza;
111 if stanza.attr.type == "result" then 161 if stanza.attr.type == "result" then
112 local disco = stanza.tags[1]; 162 local disco = stanza.tags[1];
113 if disco and disco.name == "query" and disco.attr.xmlns == "http://jabber.org/protocol/disco#info" then 163 if disco and disco.name == "query" and disco.attr.xmlns == "http://jabber.org/protocol/disco#info" then
114 -- Process disco response 164 -- Process disco response
115 -- TODO check if waiting for recipient's response 165 local user = stanza.attr.to or (session.username..'@'..session.host);
116 local hash; -- TODO calculate hash 166 local contact = stanza.attr.from;
117 -- TODO update hash map 167 local current = recipients[user] and recipients[user][contact];
118 -- TODO set recipient's data to calculated data 168 if type(current) ~= "string" then return; end -- check if waiting for recipient's response
169 local ver = current;
170 if not string.find(current, "#") then
171 ver = calculate_hash(disco.tags); -- calculate hash
172 end
173 local notify = {};
174 for _, feature in pairs(disco.tags) do
175 if feature.name == "feature" and feature.attr.var then
176 local nfeature = feature.attr.var:match("^(.*)+notify$");
177 if nfeature then notify[nfeature] = true; end
178 end
179 end
180 hash_map[ver] = notify; -- update hash map
181 recipients[user][contact] = notify; -- set recipient's data to calculated data
182 -- send messages to recipient
183 local d = data[user];
184 if d then
185 for node, message in pairs(notify) do
186 if d[node] then
187 message.attr.to = stanza.attr.from;
188 session.send(message);
189 end
190 end
191 end
119 end 192 end
120 end 193 end
121 end); 194 end);