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 function handle_pubsub_iq(event)
22 local origin, stanza = event.origin, event.stanza;
23 local pubsub = stanza.tags[1];
24 local action = pubsub.tags[1];
26 return origin.send(st.error_reply(stanza, "cancel", "bad-request"));
28 local handler = handlers[stanza.attr.type.."_"..action.name];
30 handler(origin, stanza, action, service);
35 function simple_broadcast(kind, node, jids, item)
37 item = st.clone(item);
38 item.attr.xmlns = nil; -- Clear the pubsub namespace
40 local message = st.message({ from = module.host, type = "headline" })
41 :tag("event", { xmlns = xmlns_pubsub_event })
42 :tag(kind, { node = node })
44 for jid in pairs(jids) do
45 module:log("debug", "Sending notification to %s", jid);
46 message.attr.to = jid;
51 module:hook("iq/host/"..xmlns_pubsub..":pubsub", handle_pubsub_iq);
52 module:hook("iq/host/"..xmlns_pubsub_owner..":pubsub", handle_pubsub_iq);
57 create = { "create-nodes", "instant-nodes", "item-ids" };
58 retract = { "delete-items", "retract-items" };
59 purge = { "purge-nodes" };
60 publish = { "publish", autocreate_on_publish and "auto-create" };
61 delete = { "delete-nodes" };
62 get_items = { "retrieve-items" };
63 add_subscription = { "subscribe" };
64 get_subscriptions = { "retrieve-subscriptions" };
67 local function add_disco_features_from_service(disco, service)
68 for method, features in pairs(feature_map) do
69 if service[method] then
70 for _, feature in ipairs(features) do
72 disco:tag("feature", { var = xmlns_pubsub.."#"..feature }):up();
77 for affiliation in pairs(service.config.capabilities) do
78 if affiliation ~= "none" and affiliation ~= "owner" then
79 disco:tag("feature", { var = xmlns_pubsub.."#"..affiliation.."-affiliation" }):up();
84 local function build_disco_info(service)
85 local disco_info = st.stanza("query", { xmlns = "http://jabber.org/protocol/disco#info" })
86 :tag("identity", { category = "pubsub", type = "service", name = pubsub_disco_name }):up()
87 :tag("feature", { var = "http://jabber.org/protocol/pubsub" }):up();
88 add_disco_features_from_service(disco_info, service);
92 module:hook("iq-get/host/http://jabber.org/protocol/disco#info:query", function (event)
93 local origin, stanza = event.origin, event.stanza;
94 local node = stanza.tags[1].attr.node;
96 return origin.send(st.reply(stanza):add_child(disco_info));
98 local ok, ret = service:get_nodes(stanza.attr.from);
99 if ok and not ret[node] then
100 ok, ret = false, "item-not-found";
103 return origin.send(pubsub_error_reply(stanza, ret));
105 local reply = st.reply(stanza)
106 :tag("query", { xmlns = "http://jabber.org/protocol/disco#info", node = node })
107 :tag("identity", { category = "pubsub", type = "leaf" });
108 return origin.send(reply);
112 local function handle_disco_items_on_node(event)
113 local stanza, origin = event.stanza, event.origin;
114 local query = stanza.tags[1];
115 local node = query.attr.node;
116 local ok, ret = service:get_items(node, stanza.attr.from);
118 return origin.send(pubsub_error_reply(stanza, ret));
121 local reply = st.reply(stanza)
122 :tag("query", { xmlns = "http://jabber.org/protocol/disco#items", node = node });
124 for id, item in pairs(ret) do
125 reply:tag("item", { jid = module.host, name = id }):up();
128 return origin.send(reply);
132 module:hook("iq-get/host/http://jabber.org/protocol/disco#items:query", function (event)
133 if event.stanza.tags[1].attr.node then
134 return handle_disco_items_on_node(event);
136 local ok, ret = service:get_nodes(event.stanza.attr.from);
138 event.origin.send(pubsub_error_reply(event.stanza, ret));
140 local reply = st.reply(event.stanza)
141 :tag("query", { xmlns = "http://jabber.org/protocol/disco#items" });
142 for node, node_obj in pairs(ret) do
143 reply:tag("item", { jid = module.host, node = node, name = node_obj.config.name }):up();
145 event.origin.send(reply);
150 local admin_aff = module:get_option_string("default_admin_affiliation", "owner");
151 local function get_affiliation(jid)
152 local bare_jid = jid_bare(jid);
153 if bare_jid == module.host or usermanager.is_admin(bare_jid, module.host) then
158 function set_service(new_service)
159 service = new_service;
160 module.environment.service = service;
161 disco_info = build_disco_info(service);
164 function module.save()
165 return { service = service };
168 function module.restore(data)
169 set_service(data.service);
172 set_service(pubsub.new({
182 get_subscription = true;
183 get_subscriptions = true;
186 subscribe_other = false;
187 unsubscribe_other = false;
188 get_subscription_other = false;
189 get_subscriptions_other = false;
191 be_subscribed = true;
192 be_unsubscribed = true;
194 set_affiliation = false;
204 get_subscription = true;
205 get_subscriptions = true;
208 subscribe_other = false;
209 unsubscribe_other = false;
210 get_subscription_other = false;
211 get_subscriptions_other = false;
213 be_subscribed = true;
214 be_unsubscribed = true;
216 set_affiliation = false;
227 get_subscription = true;
228 get_subscriptions = true;
232 subscribe_other = true;
233 unsubscribe_other = true;
234 get_subscription_other = true;
235 get_subscriptions_other = true;
237 be_subscribed = true;
238 be_unsubscribed = true;
240 set_affiliation = true;
244 autocreate_on_publish = autocreate_on_publish;
245 autocreate_on_subscribe = autocreate_on_subscribe;
247 broadcaster = simple_broadcast;
248 get_affiliation = get_affiliation;
250 normalize_jid = jid_bare;