09845962c8144eccc318c56d5382288fa5026aa9
[prosody.git] / plugins / mod_pubsub.lua
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;
5
6 require "core.modulemanager".load(module.host, "iq");
7
8 local xmlns_pubsub = "http://jabber.org/protocol/pubsub";
9 local xmlns_pubsub_errors = "http://jabber.org/protocol/pubsub#errors";
10 local xmlns_pubsub_event = "http://jabber.org/protocol/pubsub#event";
11
12 local autocreate_on_publish = module:get_option_boolean("autocreate_on_publish", false);
13 local autocreate_on_subscribe = module:get_option_boolean("autocreate_on_subscribe", false);
14
15 local service;
16
17 local handlers = {};
18
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];
24         if handler then
25                 handler(origin, stanza, action);
26                 return true;
27         end
28 end
29
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" };
36 };
37 function pubsub_error_reply(stanza, error)
38         local e = pubsub_errors[error];
39         local reply = st.error_reply(stanza, unpack(e, 1, 3));
40         if e[4] then
41                 reply:tag(e[4], { xmlns = xmlns_pubsub_errors }):up();
42         end
43         return reply;
44 end
45
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;
50         
51         local ok, results = service:get_items(node, stanza.attr.from, id);
52         if not ok then
53                 return origin.send(pubsub_error_reply(stanza, results));
54         end
55         
56         local data = st.stanza("items", { node = node });
57         for _, entry in pairs(results) do
58                 data:add_child(entry);
59         end
60         if data then
61                 reply = st.reply(stanza)
62                         :tag("pubsub", { xmlns = xmlns_pubsub })
63                                 :add_child(data);
64         else
65                 reply = pubsub_error_reply(stanza, "item-not-found");
66         end
67         return origin.send(reply);
68 end
69
70 function handlers.set_create(origin, stanza, create)
71         local node = create.attr.node;
72         local ok, ret, reply;
73         if node then
74                 ok, ret = service:create(node, stanza.attr.from);
75                 if ok then
76                         reply = st.reply(stanza);
77                 else
78                         reply = pubsub_error_reply(stanza, ret);
79                 end
80         else
81                 repeat
82                         node = uuid_generate();
83                         ok, ret = service:create(node, stanza.attr.from);
84                 until ok or ret ~= "conflict";
85                 if ok then
86                         reply = st.reply(stanza)
87                                 :tag("pubsub", { xmlns = xmlns_pubsub })
88                                         :tag("create", { node = node });
89                 else
90                         reply = pubsub_error_reply(stanza, ret);
91                 end
92         end
93         return origin.send(reply);
94 end
95
96 function handlers.set_subscribe(origin, stanza, subscribe)
97         local node, jid = subscribe.attr.node, subscribe.attr.jid;
98         if jid_bare(jid) ~= jid_bare(stanza.attr.from) then
99                 return origin.send(pubsub_error_reply(stanza, "invalid-jid"));
100         end
101         local ok, ret = service:add_subscription(node, stanza.attr.from, jid);
102         local reply;
103         if ok then
104                 reply = st.reply(stanza)
105                         :tag("pubsub", { xmlns = xmlns_pubsub })
106                                 :tag("subscription", {
107                                         node = node,
108                                         jid = jid,
109                                         subscription = "subscribed"
110                                 });
111         else
112                 reply = pubsub_error_reply(stanza, ret);
113         end
114         return origin.send(reply);
115 end
116
117 function handlers.set_unsubscribe(origin, stanza, unsubscribe)
118         local node, jid = unsubscribe.attr.node, unsubscribe.attr.jid;
119         if jid_bare(jid) ~= jid_bare(stanza.attr.from) then
120                 return origin.send(pubsub_error_reply(stanza, "invalid-jid"));
121         end
122         local ok, ret = service:remove_subscription(node, stanza.attr.from, jid);
123         local reply;
124         if ok then
125                 reply = st.reply(stanza);
126         else
127                 reply = pubsub_error_reply(stanza, ret);
128         end
129         return origin.send(reply);
130 end
131
132 function handlers.set_publish(origin, stanza, publish)
133         local node = publish.attr.node;
134         local item = publish:get_child("item");
135         local id = (item and item.attr.id) or uuid_generate();
136         local ok, ret = service:publish(node, stanza.attr.from, id, item);
137         local reply;
138         if ok then
139                 reply = st.reply(stanza)
140                         :tag("pubsub", { xmlns = xmlns_pubsub })
141                                 :tag("publish", { node = node })
142                                         :tag("item", { id = id });
143         else
144                 reply = pubsub_error_reply(stanza, ret);
145         end
146         return origin.send(reply);
147 end
148
149 function handlers.set_retract(origin, stanza, retract)
150         local node, notify = retract.attr.node, retract.attr.notify;
151         notify = (notify == "1") or (notify == "true");
152         local item = retract:get_child("item");
153         local id = item and item.attr.id
154         local reply, notifier;
155         if notify then
156                 notifier = st.stanza("retract", { id = id });
157         end
158         local ok, ret = service:retract(node, stanza.attr.from, id, notifier);
159         if ok then
160                 reply = st.reply(stanza);
161         else
162                 reply = pubsub_error_reply(stanza, ret);
163         end
164         return origin.send(reply);
165 end
166
167 function simple_broadcast(node, jids, item)
168         item = st.clone(item);
169         item.attr.xmlns = nil; -- Clear the pubsub namespace
170         local message = st.message({ from = module.host, type = "headline" })
171                 :tag("event", { xmlns = xmlns_pubsub_event })
172                         :tag("items", { node = node })
173                                 :add_child(item);
174         for jid in pairs(jids) do
175                 module:log("debug", "Sending notification to %s", jid);
176                 message.attr.to = jid;
177                 core_post_stanza(hosts[module.host], message);
178         end
179 end
180
181 module:hook("iq/host/http://jabber.org/protocol/pubsub:pubsub", handle_pubsub_iq);
182
183 local disco_info;
184
185 local feature_map = {
186         create = { "create-nodes", autocreate_on_publish and "instant-nodes", "item-ids" };
187         retract = { "delete-items", "retract-items" };
188         publish = { "publish" };
189         get_items = { "retrieve-items" };
190         add_subscription = { "subscribe" };
191         get_subscriptions = { "retrieve-subscriptions" };
192 };
193
194 local function add_disco_features_from_service(disco, service)
195         for method, features in pairs(feature_map) do
196                 if service[method] then
197                         for _, feature in ipairs(features) do
198                                 if feature then
199                                         disco:tag("feature", { var = xmlns_pubsub.."#"..feature }):up();
200                                 end
201                         end
202                 end
203         end
204         for affiliation in pairs(service.config.capabilities) do
205                 if affiliation ~= "none" and affiliation ~= "owner" then
206                         disco:tag("feature", { var = xmlns_pubsub.."#"..affiliation.."-affiliation" }):up();
207                 end
208         end
209 end
210
211 local function build_disco_info(service)
212         local disco_info = st.stanza("query", { xmlns = "http://jabber.org/protocol/disco#info" })
213                 :tag("identity", { category = "pubsub", type = "service" }):up()
214                 :tag("feature", { var = "http://jabber.org/protocol/pubsub" }):up();
215         add_disco_features_from_service(disco_info, service);
216         return disco_info;
217 end
218
219 module:hook("iq-get/host/http://jabber.org/protocol/disco#info:query", function (event)
220         event.origin.send(st.reply(event.stanza):add_child(disco_info));
221         return true;
222 end);
223
224 local function handle_disco_items_on_node(event)
225         local stanza, origin = event.stanza, event.origin;
226         local query = stanza.tags[1];
227         local node = query.attr.node;
228         local ok, ret = service:get_items(node, stanza.attr.from);
229         if not ok then
230                 return origin.send(pubsub_error_reply(stanza, ret));
231         end
232         
233         local reply = st.reply(stanza)
234                 :tag("query", { xmlns = xmlns_disco_items });
235         
236         for id, item in pairs(ret) do
237                 reply:tag("item", { jid = module.host, name = id }):up();
238         end
239         
240         return origin.send(reply);
241 end
242
243
244 module:hook("iq-get/host/http://jabber.org/protocol/disco#items:query", function (event)
245         if event.stanza.tags[1].attr.node then
246                 return handle_disco_items_on_node(event);
247         end
248         local ok, ret = service:get_nodes(event.stanza.attr.from);
249         if not ok then
250                 event.origin.send(pubsub_error_reply(stanza, ret));
251         else
252                 local reply = st.reply(event.stanza)
253                         :tag("query", { xmlns = "http://jabber.org/protocol/disco#items" });
254                 for node, node_obj in pairs(ret) do
255                         reply:tag("item", { jid = module.host, node = node, name = node_obj.config.name }):up();
256                 end
257                 event.origin.send(reply);
258         end
259         return true;
260 end);
261
262 local admin_aff = module:get_option_string("default_admin_affiliation", "owner");
263 local function get_affiliation(jid)
264         local bare_jid = jid_bare(jid);
265         if bare_jid == module.host or usermanager.is_admin(bare_jid, module.host) then
266                 return admin_aff;
267         end
268 end
269
270 function set_service(new_service)
271         service = new_service;
272         module.environment.service = service;
273         disco_info = build_disco_info(service);
274 end
275
276 function module.save()
277         return { service = service };
278 end
279
280 function module.restore(data)
281         set_service(data.service);
282 end
283
284 set_service(pubsub.new({
285         capabilities = {
286                 none = {
287                         create = false;
288                         publish = false;
289                         retract = false;
290                         get_nodes = true;
291                         
292                         subscribe = true;
293                         unsubscribe = true;
294                         get_subscription = true;
295                         get_items = true;
296                         
297                         subscribe_other = false;
298                         unsubscribe_other = false;
299                         get_subscription_other = false;
300                         
301                         be_subscribed = true;
302                         be_unsubscribed = true;
303                         
304                         set_affiliation = false;
305                 };
306                 owner = {
307                         create = true;
308                         publish = true;
309                         retract = true;
310                         get_nodes = true;
311                         
312                         subscribe = true;
313                         unsubscribe = true;
314                         get_subscription = true;
315                         get_items = true;
316                         
317                         
318                         subscribe_other = true;
319                         unsubscribe_other = true;
320                         get_subscription_other = true;
321                         
322                         be_subscribed = true;
323                         be_unsubscribed = true;
324                         
325                         set_affiliation = true;
326                 };
327         };
328         
329         autocreate_on_publish = autocreate_on_publish;
330         autocreate_on_subscribe = autocreate_on_subscribe;
331         
332         broadcaster = simple_broadcast;
333         get_affiliation = get_affiliation;
334         
335         normalize_jid = jid_bare;
336 }));