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);
62 reply = st.reply(stanza)
63 :tag("pubsub", { xmlns = xmlns_pubsub })
66 reply = pubsub_error_reply(stanza, "item-not-found");
68 return origin.send(reply);
71 function handlers.get_subscriptions(origin, stanza, subscriptions)
72 local node = subscriptions.attr.node;
73 local ok, ret = service:get_subscriptions(node, stanza.attr.from, stanza.attr.from);
75 return origin.send(pubsub_error_reply(stanza, ret));
77 local reply = st.reply(stanza)
78 :tag("pubsub", { xmlns = xmlns_pubsub })
79 :tag("subscriptions");
80 for _, sub in ipairs(ret) do
81 reply:tag("subscription", { node = sub.node, jid = sub.jid, subscription = 'subscribed' }):up();
83 return origin.send(reply);
86 function handlers.set_create(origin, stanza, create)
87 local node = create.attr.node;
90 ok, ret = service:create(node, stanza.attr.from);
92 reply = st.reply(stanza);
94 reply = pubsub_error_reply(stanza, ret);
98 node = uuid_generate();
99 ok, ret = service:create(node, stanza.attr.from);
100 until ok or ret ~= "conflict";
102 reply = st.reply(stanza)
103 :tag("pubsub", { xmlns = xmlns_pubsub })
104 :tag("create", { node = node });
106 reply = pubsub_error_reply(stanza, ret);
109 return origin.send(reply);
112 function handlers.set_subscribe(origin, stanza, subscribe)
113 local node, jid = subscribe.attr.node, subscribe.attr.jid;
114 local options_tag, options = stanza.tags[1]:get_child("options"), nil;
116 options = options_form:data(options_tag.tags[1]);
118 local ok, ret = service:add_subscription(node, stanza.attr.from, jid, options);
121 reply = st.reply(stanza)
122 :tag("pubsub", { xmlns = xmlns_pubsub })
123 :tag("subscription", {
126 subscription = "subscribed"
129 reply:add_child(options_tag);
132 reply = pubsub_error_reply(stanza, ret);
136 -- Send all current items
137 local ok, items = service:get_items(node, stanza.attr.from);
139 local jids = { [jid] = options or true };
140 for id, item in pairs(items) do
141 service.config.broadcaster(node, jids, item);
147 function handlers.set_unsubscribe(origin, stanza, unsubscribe)
148 local node, jid = unsubscribe.attr.node, unsubscribe.attr.jid;
149 local ok, ret = service:remove_subscription(node, stanza.attr.from, jid);
152 reply = st.reply(stanza);
154 reply = pubsub_error_reply(stanza, ret);
156 return origin.send(reply);
159 function handlers.set_publish(origin, stanza, publish)
160 local node = publish.attr.node;
161 local item = publish:get_child("item");
162 local id = (item and item.attr.id) or uuid_generate();
163 local ok, ret = service:publish(node, stanza.attr.from, id, item);
166 reply = st.reply(stanza)
167 :tag("pubsub", { xmlns = xmlns_pubsub })
168 :tag("publish", { node = node })
169 :tag("item", { id = id });
171 reply = pubsub_error_reply(stanza, ret);
173 return origin.send(reply);
176 function handlers.set_retract(origin, stanza, retract)
177 local node, notify = retract.attr.node, retract.attr.notify;
178 notify = (notify == "1") or (notify == "true");
179 local item = retract:get_child("item");
180 local id = item and item.attr.id
181 local reply, notifier;
183 notifier = st.stanza("retract", { id = id });
185 local ok, ret = service:retract(node, stanza.attr.from, id, notifier);
187 reply = st.reply(stanza);
189 reply = pubsub_error_reply(stanza, ret);
191 return origin.send(reply);
194 function simple_broadcast(node, jids, item)
195 item = st.clone(item);
196 item.attr.xmlns = nil; -- Clear the pubsub namespace
197 local message = st.message({ from = module.host, type = "headline" })
198 :tag("event", { xmlns = xmlns_pubsub_event })
199 :tag("items", { node = node })
201 for jid in pairs(jids) do
202 module:log("debug", "Sending notification to %s", jid);
203 message.attr.to = jid;
204 module:send(message);
208 module:hook("iq/host/http://jabber.org/protocol/pubsub:pubsub", handle_pubsub_iq);
212 local feature_map = {
213 create = { "create-nodes", autocreate_on_publish and "instant-nodes", "item-ids" };
214 retract = { "delete-items", "retract-items" };
215 publish = { "publish" };
216 get_items = { "retrieve-items" };
217 add_subscription = { "subscribe" };
218 get_subscriptions = { "retrieve-subscriptions" };
221 local function add_disco_features_from_service(disco, service)
222 for method, features in pairs(feature_map) do
223 if service[method] then
224 for _, feature in ipairs(features) do
226 disco:tag("feature", { var = xmlns_pubsub.."#"..feature }):up();
231 for affiliation in pairs(service.config.capabilities) do
232 if affiliation ~= "none" and affiliation ~= "owner" then
233 disco:tag("feature", { var = xmlns_pubsub.."#"..affiliation.."-affiliation" }):up();
238 local function build_disco_info(service)
239 local disco_info = st.stanza("query", { xmlns = "http://jabber.org/protocol/disco#info" })
240 :tag("identity", { category = "pubsub", type = "service", name = pubsub_disco_name }):up()
241 :tag("feature", { var = "http://jabber.org/protocol/pubsub" }):up();
242 add_disco_features_from_service(disco_info, service);
246 module:hook("iq-get/host/http://jabber.org/protocol/disco#info:query", function (event)
247 local origin, stanza = event.origin, event.stanza;
248 local node = stanza.tags[1].attr.node;
250 return origin.send(st.reply(stanza):add_child(disco_info));
252 local ok, ret = service:get_nodes(stanza.attr.from);
253 if ok and not ret[node] then
254 ok, ret = false, "item-not-found";
257 return origin.send(pubsub_error_reply(stanza, ret));
259 local reply = st.reply(stanza)
260 :tag("query", { xmlns = "http://jabber.org/protocol/disco#info", node = node })
261 :tag("identity", { category = "pubsub", type = "leaf" });
262 return origin.send(reply);
266 local function handle_disco_items_on_node(event)
267 local stanza, origin = event.stanza, event.origin;
268 local query = stanza.tags[1];
269 local node = query.attr.node;
270 local ok, ret = service:get_items(node, stanza.attr.from);
272 return origin.send(pubsub_error_reply(stanza, ret));
275 local reply = st.reply(stanza)
276 :tag("query", { xmlns = "http://jabber.org/protocol/disco#items", node = node });
278 for id, item in pairs(ret) do
279 reply:tag("item", { jid = module.host, name = id }):up();
282 return origin.send(reply);
286 module:hook("iq-get/host/http://jabber.org/protocol/disco#items:query", function (event)
287 if event.stanza.tags[1].attr.node then
288 return handle_disco_items_on_node(event);
290 local ok, ret = service:get_nodes(event.stanza.attr.from);
292 event.origin.send(pubsub_error_reply(stanza, ret));
294 local reply = st.reply(event.stanza)
295 :tag("query", { xmlns = "http://jabber.org/protocol/disco#items" });
296 for node, node_obj in pairs(ret) do
297 reply:tag("item", { jid = module.host, node = node, name = node_obj.config.name }):up();
299 event.origin.send(reply);
304 local admin_aff = module:get_option_string("default_admin_affiliation", "owner");
305 local function get_affiliation(jid)
306 local bare_jid = jid_bare(jid);
307 if bare_jid == module.host or usermanager.is_admin(bare_jid, module.host) then
312 function set_service(new_service)
313 service = new_service;
314 module.environment.service = service;
315 disco_info = build_disco_info(service);
318 function module.save()
319 return { service = service };
322 function module.restore(data)
323 set_service(data.service);
326 set_service(pubsub.new({
336 get_subscription = true;
337 get_subscriptions = true;
340 subscribe_other = false;
341 unsubscribe_other = false;
342 get_subscription_other = false;
343 get_subscriptions_other = false;
345 be_subscribed = true;
346 be_unsubscribed = true;
348 set_affiliation = false;
358 get_subscription = true;
359 get_subscriptions = true;
362 subscribe_other = false;
363 unsubscribe_other = false;
364 get_subscription_other = false;
365 get_subscriptions_other = false;
367 be_subscribed = true;
368 be_unsubscribed = true;
370 set_affiliation = false;
380 get_subscription = true;
381 get_subscriptions = true;
385 subscribe_other = true;
386 unsubscribe_other = true;
387 get_subscription_other = true;
388 get_subscriptions_other = true;
390 be_subscribed = true;
391 be_unsubscribed = true;
393 set_affiliation = true;
397 autocreate_on_publish = autocreate_on_publish;
398 autocreate_on_subscribe = autocreate_on_subscribe;
400 broadcaster = simple_broadcast;
401 get_affiliation = get_affiliation;
403 normalize_jid = jid_bare;