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
17 local lib_pubsub = module:require "pubsub";
18 local handlers = lib_pubsub.handlers;
19 local pubsub_error_reply = lib_pubsub.pubsub_error_reply;
21 module:depends("disco");
22 module:add_identity("pubsub", "service", pubsub_disco_name);
23 module:add_feature("http://jabber.org/protocol/pubsub");
25 function handle_pubsub_iq(event)
26 local origin, stanza = event.origin, event.stanza;
27 local pubsub = stanza.tags[1];
28 local action = pubsub.tags[1];
30 return origin.send(st.error_reply(stanza, "cancel", "bad-request"));
32 local handler = handlers[stanza.attr.type.."_"..action.name];
34 handler(origin, stanza, action, service);
39 function simple_broadcast(kind, node, jids, item)
41 item = st.clone(item);
42 item.attr.xmlns = nil; -- Clear the pubsub namespace
44 local message = st.message({ from = module.host, type = "headline" })
45 :tag("event", { xmlns = xmlns_pubsub_event })
46 :tag(kind, { node = node })
48 for jid in pairs(jids) do
49 module:log("debug", "Sending notification to %s", jid);
50 message.attr.to = jid;
55 module:hook("iq/host/"..xmlns_pubsub..":pubsub", handle_pubsub_iq);
56 module:hook("iq/host/"..xmlns_pubsub_owner..":pubsub", handle_pubsub_iq);
59 create = { "create-nodes", "instant-nodes", "item-ids" };
60 retract = { "delete-items", "retract-items" };
61 purge = { "purge-nodes" };
62 publish = { "publish", autocreate_on_publish and "auto-create" };
63 delete = { "delete-nodes" };
64 get_items = { "retrieve-items" };
65 add_subscription = { "subscribe" };
66 get_subscriptions = { "retrieve-subscriptions" };
69 local function add_disco_features_from_service(service)
70 for method, features in pairs(feature_map) do
71 if service[method] then
72 for _, feature in ipairs(features) do
74 module:add_feature(xmlns_pubsub.."#"..feature);
79 for affiliation in pairs(service.config.capabilities) do
80 if affiliation ~= "none" and affiliation ~= "owner" then
81 module:add_feature(xmlns_pubsub.."#"..affiliation.."-affiliation");
86 module:hook("host-disco-info-node", function (event)
87 local stanza, origin, reply, node = event.stanza, event.origin, event.reply, event.node;
88 local ok, ret = service:get_nodes(stanza.attr.from);
89 if not ok or not ret[node] then
93 reply:tag("identity", { category = "pubsub", type = "leaf" });
96 module:hook("host-disco-items-node", function (event)
97 local stanza, origin, reply, node = event.stanza, event.origin, event.reply, event.node;
98 local ok, ret = service:get_items(node, stanza.attr.from);
103 for _, id in ipairs(ret) do
104 reply:tag("item", { jid = module.host, name = id }):up();
110 module:hook("host-disco-items", function (event)
111 local stanza, origin, reply = event.stanza, event.origin, event.reply;
112 local ok, ret = service:get_nodes(event.stanza.attr.from);
116 for node, node_obj in pairs(ret) do
117 reply:tag("item", { jid = module.host, node = node, name = node_obj.config.name }):up();
121 local admin_aff = module:get_option_string("default_admin_affiliation", "owner");
122 local function get_affiliation(jid)
123 local bare_jid = jid_bare(jid);
124 if bare_jid == module.host or usermanager.is_admin(bare_jid, module.host) then
129 function set_service(new_service)
130 service = new_service;
131 module.environment.service = service;
132 add_disco_features_from_service(service);
135 function module.save()
136 return { service = service };
139 function module.restore(data)
140 set_service(data.service);
143 function module.load()
144 if module.reloading then return; end
146 set_service(pubsub.new({
156 get_subscription = true;
157 get_subscriptions = true;
160 subscribe_other = false;
161 unsubscribe_other = false;
162 get_subscription_other = false;
163 get_subscriptions_other = false;
165 be_subscribed = true;
166 be_unsubscribed = true;
168 set_affiliation = false;
178 get_subscription = true;
179 get_subscriptions = true;
182 subscribe_other = false;
183 unsubscribe_other = false;
184 get_subscription_other = false;
185 get_subscriptions_other = false;
187 be_subscribed = true;
188 be_unsubscribed = true;
190 set_affiliation = false;
201 get_subscription = true;
202 get_subscriptions = true;
206 subscribe_other = true;
207 unsubscribe_other = true;
208 get_subscription_other = true;
209 get_subscriptions_other = true;
211 be_subscribed = true;
212 be_unsubscribed = true;
214 set_affiliation = true;
218 autocreate_on_publish = autocreate_on_publish;
219 autocreate_on_subscribe = autocreate_on_subscribe;
221 broadcaster = simple_broadcast;
222 get_affiliation = get_affiliation;
224 normalize_jid = jid_bare;