Comparison

plugins/mod_pubsub.lua @ 3914:f10282c2e410

mod_pubsub: Build disco#info based on the methods provided by the pubsub service object. Add public set_service() method to change the service object used by the module.
author Matthew Wild <mwild1@gmail.com>
date Tue, 21 Dec 2010 04:06:36 +0000
parent 3913:e748d29b18d6
child 3915:e24fcbb01fb6
comparison
equal deleted inserted replaced
3913:e748d29b18d6 3914:f10282c2e410
6 require "core.modulemanager".load(module.host, "iq"); 6 require "core.modulemanager".load(module.host, "iq");
7 7
8 local xmlns_pubsub = "http://jabber.org/protocol/pubsub"; 8 local xmlns_pubsub = "http://jabber.org/protocol/pubsub";
9 local xmlns_pubsub_errors = "http://jabber.org/protocol/pubsub#errors"; 9 local xmlns_pubsub_errors = "http://jabber.org/protocol/pubsub#errors";
10 local xmlns_pubsub_event = "http://jabber.org/protocol/pubsub#event"; 10 local xmlns_pubsub_event = "http://jabber.org/protocol/pubsub#event";
11
12 local autocreate_on_publish = module:get_option_boolean("autocreate_on_publish", false);
13 local autocreate_on_subscribe = module:get_option_boolean("autocreate_on_subscribe", false);
11 14
12 local service; 15 local service;
13 16
14 local handlers = {}; 17 local handlers = {};
15 18
175 end 178 end
176 end 179 end
177 180
178 module:hook("iq/host/http://jabber.org/protocol/pubsub:pubsub", handle_pubsub_iq); 181 module:hook("iq/host/http://jabber.org/protocol/pubsub:pubsub", handle_pubsub_iq);
179 182
180 local disco_info = st.stanza("query", { xmlns = "http://jabber.org/protocol/disco#info" }) 183 local disco_info;
181 :tag("identity", { category = "pubsub", type = "service" }):up() 184
182 :tag("feature", { var = "http://jabber.org/protocol/pubsub" }):up(); 185 local feature_map = {
186 create = { "create-nodes", autocreate_on_publish and "instant-nodes", "item-ids" };
187 retract = { "delete-items", "retract-items" };
188 publish = { "publish" };
189 get_items = { "retrieve-items" };
190 };
191
192 local function add_disco_features_from_service(disco, service)
193 for method, features in pairs(feature_map) do
194 if service[method] then
195 for _, feature in ipairs(feature_map) do
196 disco:tag("feature", { var = xmlns_pubsub.."#"..feature }):up();
197 end
198 end
199 end
200 for affiliation in pairs(service.config.capabilities) do
201 if affiliation ~= "none" and affiliation ~= "owner" then
202 disco:tag("feature", { var = xmlns_pubsub.."#"..affiliation.."-affiliation" }):up();
203 end
204 end
205 end
206
207 local function build_disco_info(service)
208 disco_info = st.stanza("query", { xmlns = "http://jabber.org/protocol/disco#info" })
209 :tag("identity", { category = "pubsub", type = "service" }):up()
210 :tag("feature", { var = "http://jabber.org/protocol/pubsub" }):up();
211 add_disco_features_from_service(disco_info, service);
212 end
183 213
184 module:hook("iq-get/host/http://jabber.org/protocol/disco#info:query", function (event) 214 module:hook("iq-get/host/http://jabber.org/protocol/disco#info:query", function (event)
185 event.origin.send(st.reply(event.stanza):add_child(disco_info)); 215 event.origin.send(st.reply(event.stanza):add_child(disco_info));
186 return true; 216 return true;
187 end); 217 end);
206 if jid == module.host or usermanager.is_admin(jid, module.host) then 236 if jid == module.host or usermanager.is_admin(jid, module.host) then
207 return admin_aff; 237 return admin_aff;
208 end 238 end
209 end 239 end
210 240
211 service = pubsub.new({ 241 function set_service(new_service)
242 service = new_service;
243 module.environment.service = service;
244 disco_info = build_disco_info(service);
245 end
246
247 function module.save()
248 return { service = service };
249 end
250
251 function module.restore(data)
252 set_service(data.service);
253 end
254
255 set_service(pubsub.new({
212 capabilities = { 256 capabilities = {
213 none = { 257 none = {
214 create = false; 258 create = false;
215 publish = false; 259 publish = false;
216 retract = false; 260 retract = false;
251 295
252 set_affiliation = true; 296 set_affiliation = true;
253 }; 297 };
254 }; 298 };
255 299
256 autocreate_on_publish = module:get_option_boolean("autocreate_on_publish"); 300 autocreate_on_publish = autocreate_on_publish;
257 autocreate_on_subscribe = module:get_option_boolean("autocreate_on_subscribe"); 301 autocreate_on_subscribe = autocreate_on_subscribe;
258 302
259 broadcaster = simple_broadcast; 303 broadcaster = simple_broadcast;
260 get_affiliation = get_affiliation; 304 get_affiliation = get_affiliation;
261 jids_equal = function (jid1, jid2) 305 jids_equal = function (jid1, jid2)
262 return jid_bare(jid1) == jid_bare(jid2); 306 return jid_bare(jid1) == jid_bare(jid2);
263 end; 307 end;
264 }); 308 }));
265 module.environment.service = service;
266
267 function module.save()
268 return { service = service };
269 end
270
271 function module.restore(data)
272 service = data.service;
273 module.environment.service = service;
274 end