1 local pubsub = require "util.pubsub";
2 local st = require "util.stanza";
3 local jid_bare = require "util.jid".bare;
4 local usermanager = require "core.usermanager";
6 local xmlns_pubsub = "http://jabber.org/protocol/pubsub";
7 local xmlns_pubsub_event = "http://jabber.org/protocol/pubsub#event";
8 local xmlns_pubsub_owner = "http://jabber.org/protocol/pubsub#owner";
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
14 local expose_publisher = module:get_option_boolean("expose_publisher", false)
18 local lib_pubsub = module:require "pubsub";
19 local handlers = lib_pubsub.handlers;
20 local pubsub_error_reply = lib_pubsub.pubsub_error_reply;
22 module:depends("disco");
23 module:add_identity("pubsub", "service", pubsub_disco_name);
24 module:add_feature("http://jabber.org/protocol/pubsub");
26 function handle_pubsub_iq(event)
27 local origin, stanza = event.origin, event.stanza;
28 local pubsub = stanza.tags[1];
29 local action = pubsub.tags[1];
31 origin.send(st.error_reply(stanza, "cancel", "bad-request"));
34 local handler = handlers[stanza.attr.type.."_"..action.name];
36 handler(origin, stanza, action, service);
41 function simple_broadcast(kind, node, jids, item, actor)
43 item = st.clone(item);
44 item.attr.xmlns = nil; -- Clear the pubsub namespace
45 if expose_publisher and actor then
46 item.attr.publisher = actor
49 local message = st.message({ from = module.host, type = "headline" })
50 :tag("event", { xmlns = xmlns_pubsub_event })
51 :tag(kind, { node = node })
53 for jid in pairs(jids) do
54 module:log("debug", "Sending notification to %s", jid);
55 message.attr.to = jid;
60 module:hook("iq/host/"..xmlns_pubsub..":pubsub", handle_pubsub_iq);
61 module:hook("iq/host/"..xmlns_pubsub_owner..":pubsub", handle_pubsub_iq);
64 create = { "create-nodes", "instant-nodes", "item-ids" };
65 retract = { "delete-items", "retract-items" };
66 purge = { "purge-nodes" };
67 publish = { "publish", autocreate_on_publish and "auto-create" };
68 delete = { "delete-nodes" };
69 get_items = { "retrieve-items" };
70 add_subscription = { "subscribe" };
71 get_subscriptions = { "retrieve-subscriptions" };
72 set_configure = { "config-node" };
73 get_default = { "retrieve-default" };
76 local function add_disco_features_from_service(service)
77 for method, features in pairs(feature_map) do
78 if service[method] then
79 for _, feature in ipairs(features) do
81 module:add_feature(xmlns_pubsub.."#"..feature);
86 for affiliation in pairs(service.config.capabilities) do
87 if affiliation ~= "none" and affiliation ~= "owner" then
88 module:add_feature(xmlns_pubsub.."#"..affiliation.."-affiliation");
93 module:hook("host-disco-info-node", function (event)
94 local stanza, origin, reply, node = event.stanza, event.origin, event.reply, event.node;
95 local ok, ret = service:get_nodes(stanza.attr.from);
96 if not ok or not ret[node] then
100 reply:tag("identity", { category = "pubsub", type = "leaf" });
103 module:hook("host-disco-items-node", function (event)
104 local stanza, origin, reply, node = event.stanza, event.origin, event.reply, event.node;
105 local ok, ret = service:get_items(node, stanza.attr.from);
110 for _, id in ipairs(ret) do
111 reply:tag("item", { jid = module.host, name = id }):up();
117 module:hook("host-disco-items", function (event)
118 local stanza, origin, reply = event.stanza, event.origin, event.reply;
119 local ok, ret = service:get_nodes(event.stanza.attr.from);
123 for node, node_obj in pairs(ret) do
124 reply:tag("item", { jid = module.host, node = node, name = node_obj.config.name }):up();
128 local admin_aff = module:get_option_string("default_admin_affiliation", "owner");
129 local unowned_aff = module:get_option_string("default_unowned_affiliation");
130 local function get_affiliation(jid, node)
131 local bare_jid = jid_bare(jid);
132 if bare_jid == module.host or usermanager.is_admin(bare_jid, module.host) then
140 function set_service(new_service)
141 service = new_service;
142 module.environment.service = service;
143 add_disco_features_from_service(service);
146 function module.save()
147 return { service = service };
150 function module.restore(data)
151 set_service(data.service);
154 function module.load()
155 if module.reloading then return; end
157 set_service(pubsub.new({
167 get_subscription = true;
168 get_subscriptions = true;
171 subscribe_other = false;
172 unsubscribe_other = false;
173 get_subscription_other = false;
174 get_subscriptions_other = false;
176 be_subscribed = true;
177 be_unsubscribed = true;
179 set_affiliation = false;
189 get_subscription = true;
190 get_subscriptions = true;
193 subscribe_other = false;
194 unsubscribe_other = false;
195 get_subscription_other = false;
196 get_subscriptions_other = false;
198 be_subscribed = true;
199 be_unsubscribed = true;
201 set_affiliation = false;
213 get_subscription = true;
214 get_subscriptions = true;
218 subscribe_other = true;
219 unsubscribe_other = true;
220 get_subscription_other = true;
221 get_subscriptions_other = true;
223 be_subscribed = true;
224 be_unsubscribed = true;
226 set_affiliation = true;
230 autocreate_on_publish = autocreate_on_publish;
231 autocreate_on_subscribe = autocreate_on_subscribe;
233 broadcaster = simple_broadcast;
234 get_affiliation = get_affiliation;
236 normalize_jid = jid_bare;