1 local pubsub = require "util.pubsub";
2 local st = require "util.stanza";
3 local jid_bare = require "util.jid".bare;
4 local uuid_generate = require "util.uuid".generate;
6 local xmlns_pubsub = "http://jabber.org/protocol/pubsub";
7 local xmlns_pubsub_errors = "http://jabber.org/protocol/pubsub#errors";
8 local xmlns_pubsub_event = "http://jabber.org/protocol/pubsub#event";
10 local autocreate_on_publish = module:get_option_boolean("autocreate_on_publish", false);
11 local autocreate_on_subscribe = module:get_option_boolean("autocreate_on_subscribe", false);
12 local pubsub_disco_name = module:get_option("name");
13 if type(pubsub_disco_name) ~= "string" then pubsub_disco_name = "Prosody PubSub Service"; end
19 function handle_pubsub_iq(event)
20 local origin, stanza = event.origin, event.stanza;
21 local pubsub = stanza.tags[1];
22 local action = pubsub.tags[1];
23 local handler = handlers[stanza.attr.type.."_"..action.name];
25 handler(origin, stanza, action);
30 local pubsub_errors = {
31 ["conflict"] = { "cancel", "conflict" };
32 ["invalid-jid"] = { "modify", "bad-request", nil, "invalid-jid" };
33 ["item-not-found"] = { "cancel", "item-not-found" };
34 ["not-subscribed"] = { "modify", "unexpected-request", nil, "not-subscribed" };
35 ["forbidden"] = { "cancel", "forbidden" };
37 function pubsub_error_reply(stanza, error)
38 local e = pubsub_errors[error];
39 local reply = st.error_reply(stanza, unpack(e, 1, 3));
41 reply:tag(e[4], { xmlns = xmlns_pubsub_errors }):up();
46 function handlers.get_items(origin, stanza, items)
47 local node = items.attr.node;
48 local item = items:get_child("item");
49 local id = item and item.attr.id;
51 local ok, results = service:get_items(node, stanza.attr.from, id);
53 return origin.send(pubsub_error_reply(stanza, results));
56 local data = st.stanza("items", { node = node });
57 for _, entry in pairs(results) do
58 data:add_child(entry);
61 reply = st.reply(stanza)
62 :tag("pubsub", { xmlns = xmlns_pubsub })
65 reply = pubsub_error_reply(stanza, "item-not-found");
67 return origin.send(reply);
70 function handlers.get_subscriptions(origin, stanza, subscriptions)
71 local node = subscriptions.attr.node;
72 local ok, ret = service:get_subscriptions(node, stanza.attr.from, stanza.attr.from);
74 return origin.send(pubsub_error_reply(stanza, ret));
76 local reply = st.reply(stanza)
77 :tag("pubsub", { xmlns = xmlns_pubsub })
78 :tag("subscriptions");
79 for _, sub in ipairs(ret) do
80 reply:tag("subscription", { node = sub.node, jid = sub.jid, subscription = 'subscribed' }):up();
82 return origin.send(reply);
85 function handlers.set_create(origin, stanza, create)
86 local node = create.attr.node;
89 ok, ret = service:create(node, stanza.attr.from);
91 reply = st.reply(stanza);
93 reply = pubsub_error_reply(stanza, ret);
97 node = uuid_generate();
98 ok, ret = service:create(node, stanza.attr.from);
99 until ok or ret ~= "conflict";
101 reply = st.reply(stanza)
102 :tag("pubsub", { xmlns = xmlns_pubsub })
103 :tag("create", { node = node });
105 reply = pubsub_error_reply(stanza, ret);
108 return origin.send(reply);
111 function handlers.set_subscribe(origin, stanza, subscribe)
112 local node, jid = subscribe.attr.node, subscribe.attr.jid;
113 local options_tag, options = stanza.tags[1]:get_child("options"), nil;
115 options = options_form:data(options_tag.tags[1]);
117 local ok, ret = service:add_subscription(node, stanza.attr.from, jid, options);
120 reply = st.reply(stanza)
121 :tag("pubsub", { xmlns = xmlns_pubsub })
122 :tag("subscription", {
125 subscription = "subscribed"
128 reply:add_child(options_tag);
131 reply = pubsub_error_reply(stanza, ret);
135 -- Send all current items
136 local ok, items = service:get_items(node, stanza.attr.from);
138 local jids = { [jid] = options or true };
139 for id, item in pairs(items) do
140 service.config.broadcaster(node, jids, item);
146 function handlers.set_unsubscribe(origin, stanza, unsubscribe)
147 local node, jid = unsubscribe.attr.node, unsubscribe.attr.jid;
148 local ok, ret = service:remove_subscription(node, stanza.attr.from, jid);
151 reply = st.reply(stanza);
153 reply = pubsub_error_reply(stanza, ret);
155 return origin.send(reply);
158 function handlers.set_publish(origin, stanza, publish)
159 local node = publish.attr.node;
160 local item = publish:get_child("item");
161 local id = (item and item.attr.id) or uuid_generate();
162 local ok, ret = service:publish(node, stanza.attr.from, id, item);
165 reply = st.reply(stanza)
166 :tag("pubsub", { xmlns = xmlns_pubsub })
167 :tag("publish", { node = node })
168 :tag("item", { id = id });
170 reply = pubsub_error_reply(stanza, ret);
172 return origin.send(reply);
175 function handlers.set_retract(origin, stanza, retract)
176 local node, notify = retract.attr.node, retract.attr.notify;
177 notify = (notify == "1") or (notify == "true");
178 local item = retract:get_child("item");
179 local id = item and item.attr.id
180 local reply, notifier;
182 notifier = st.stanza("retract", { id = id });
184 local ok, ret = service:retract(node, stanza.attr.from, id, notifier);
186 reply = st.reply(stanza);
188 reply = pubsub_error_reply(stanza, ret);
190 return origin.send(reply);
193 function simple_broadcast(node, jids, item)
194 item = st.clone(item);
195 item.attr.xmlns = nil; -- Clear the pubsub namespace
196 local message = st.message({ from = module.host, type = "headline" })
197 :tag("event", { xmlns = xmlns_pubsub_event })
198 :tag("items", { node = node })
200 for jid in pairs(jids) do
201 module:log("debug", "Sending notification to %s", jid);
202 message.attr.to = jid;
203 core_post_stanza(hosts[module.host], message);
207 module:hook("iq/host/http://jabber.org/protocol/pubsub:pubsub", handle_pubsub_iq);
211 local feature_map = {
212 create = { "create-nodes", autocreate_on_publish and "instant-nodes", "item-ids" };
213 retract = { "delete-items", "retract-items" };
214 publish = { "publish" };
215 get_items = { "retrieve-items" };
216 add_subscription = { "subscribe" };
217 get_subscriptions = { "retrieve-subscriptions" };
220 local function add_disco_features_from_service(disco, service)
221 for method, features in pairs(feature_map) do
222 if service[method] then
223 for _, feature in ipairs(features) do
225 disco:tag("feature", { var = xmlns_pubsub.."#"..feature }):up();
230 for affiliation in pairs(service.config.capabilities) do
231 if affiliation ~= "none" and affiliation ~= "owner" then
232 disco:tag("feature", { var = xmlns_pubsub.."#"..affiliation.."-affiliation" }):up();
237 local function build_disco_info(service)
238 local disco_info = st.stanza("query", { xmlns = "http://jabber.org/protocol/disco#info" })
239 :tag("identity", { category = "pubsub", type = "service", name = pubsub_disco_name }):up()
240 :tag("feature", { var = "http://jabber.org/protocol/pubsub" }):up();
241 add_disco_features_from_service(disco_info, service);
245 module:hook("iq-get/host/http://jabber.org/protocol/disco#info:query", function (event)
246 local origin, stanza = event.origin, event.stanza;
247 local node = stanza.tags[1].attr.node;
249 return origin.send(st.reply(stanza):add_child(disco_info));
251 local ok, ret = service:get_nodes(stanza.attr.from);
252 if ok and not ret[node] then
253 ok, ret = false, "item-not-found";
256 return origin.send(pubsub_error_reply(stanza, ret));
258 local reply = st.reply(stanza)
259 :tag("query", { xmlns = "http://jabber.org/protocol/disco#info", node = node })
260 :tag("identity", { category = "pubsub", type = "leaf" });
261 return origin.send(reply);
265 local function handle_disco_items_on_node(event)
266 local stanza, origin = event.stanza, event.origin;
267 local query = stanza.tags[1];
268 local node = query.attr.node;
269 local ok, ret = service:get_items(node, stanza.attr.from);
271 return origin.send(pubsub_error_reply(stanza, ret));
274 local reply = st.reply(stanza)
275 :tag("query", { xmlns = "http://jabber.org/protocol/disco#items", node = node });
277 for id, item in pairs(ret) do
278 reply:tag("item", { jid = module.host, name = id }):up();
281 return origin.send(reply);
285 module:hook("iq-get/host/http://jabber.org/protocol/disco#items:query", function (event)
286 if event.stanza.tags[1].attr.node then
287 return handle_disco_items_on_node(event);
289 local ok, ret = service:get_nodes(event.stanza.attr.from);
291 event.origin.send(pubsub_error_reply(stanza, ret));
293 local reply = st.reply(event.stanza)
294 :tag("query", { xmlns = "http://jabber.org/protocol/disco#items" });
295 for node, node_obj in pairs(ret) do
296 reply:tag("item", { jid = module.host, node = node, name = node_obj.config.name }):up();
298 event.origin.send(reply);
303 local admin_aff = module:get_option_string("default_admin_affiliation", "owner");
304 local function get_affiliation(jid)
305 local bare_jid = jid_bare(jid);
306 if bare_jid == module.host or usermanager.is_admin(bare_jid, module.host) then
311 function set_service(new_service)
312 service = new_service;
313 module.environment.service = service;
314 disco_info = build_disco_info(service);
317 function module.save()
318 return { service = service };
321 function module.restore(data)
322 set_service(data.service);
325 set_service(pubsub.new({
335 get_subscription = true;
336 get_subscriptions = true;
339 subscribe_other = false;
340 unsubscribe_other = false;
341 get_subscription_other = false;
342 get_subscriptions_other = false;
344 be_subscribed = true;
345 be_unsubscribed = true;
347 set_affiliation = false;
357 get_subscription = true;
358 get_subscriptions = true;
361 subscribe_other = false;
362 unsubscribe_other = false;
363 get_subscription_other = false;
364 get_subscriptions_other = false;
366 be_subscribed = true;
367 be_unsubscribed = true;
369 set_affiliation = false;
379 get_subscription = true;
380 get_subscriptions = true;
384 subscribe_other = true;
385 unsubscribe_other = true;
386 get_subscription_other = true;
387 get_subscriptions_other = true;
389 be_subscribed = true;
390 be_unsubscribed = true;
392 set_affiliation = true;
396 autocreate_on_publish = autocreate_on_publish;
397 autocreate_on_subscribe = autocreate_on_subscribe;
399 broadcaster = simple_broadcast;
400 get_affiliation = get_affiliation;
402 normalize_jid = jid_bare;