ecb5115a68311636591bf9155d6cce67144cb446
[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 };
191
192 local function add_disco_features_from_service(disco, service)
193         for method, features in pairs(feature_map) do
194                 if service[method] then
195                         for _, feature in ipairs(features) do
196                                 if feature then
197                                         disco:tag("feature", { var = xmlns_pubsub.."#"..feature }):up();
198                                 end
199                         end
200                 end
201         end
202         for affiliation in pairs(service.config.capabilities) do
203                 if affiliation ~= "none" and affiliation ~= "owner" then
204                         disco:tag("feature", { var = xmlns_pubsub.."#"..affiliation.."-affiliation" }):up();
205                 end
206         end
207 end
208
209 local function build_disco_info(service)
210         local disco_info = st.stanza("query", { xmlns = "http://jabber.org/protocol/disco#info" })
211                 :tag("identity", { category = "pubsub", type = "service" }):up()
212                 :tag("feature", { var = "http://jabber.org/protocol/pubsub" }):up();
213         add_disco_features_from_service(disco_info, service);
214         return disco_info;
215 end
216
217 module:hook("iq-get/host/http://jabber.org/protocol/disco#info:query", function (event)
218         event.origin.send(st.reply(event.stanza):add_child(disco_info));
219         return true;
220 end);
221
222 local function handle_disco_items_on_node(event)
223         local stanza, origin = event.stanza, event.origin;
224         local query = stanza.tags[1];
225         local node = query.attr.node;
226         local ok, ret = service:get_items(node, stanza.attr.from);
227         if not ok then
228                 return origin.send(pubsub_error_reply(stanza, ret));
229         end
230         
231         local reply = st.reply(stanza)
232                 :tag("query", { xmlns = xmlns_disco_items });
233         
234         for id, item in pairs(ret) do
235                 reply:tag("item", { jid = module.host, name = id });
236         end
237         
238         return origin.send(reply);
239 end
240
241
242 module:hook("iq-get/host/http://jabber.org/protocol/disco#items:query", function (event)
243         if event.stanza.tags[1].attr.node then
244                 return handle_disco_items_on_node(event);
245         end
246         local ok, ret = service:get_nodes(event.stanza.attr.from);
247         if not ok then
248                 event.origin.send(pubsub_error_reply(stanza, ret));
249         else
250                 local reply = st.reply(event.stanza)
251                         :tag("query", { xmlns = "http://jabber.org/protocol/disco#items" });
252                 for node, node_obj in pairs(ret) do
253                         reply:tag("item", { jid = module.host, node = node, name = node_obj.config.name }):up();
254                 end
255                 event.origin.send(reply);
256         end
257         return true;
258 end);
259
260 local admin_aff = module:get_option_string("default_admin_affiliation", "owner");
261 local function get_affiliation(jid)
262         local bare_jid = jid_bare(jid);
263         if bare_jid == module.host or usermanager.is_admin(bare_jid, module.host) then
264                 return admin_aff;
265         end
266 end
267
268 function set_service(new_service)
269         service = new_service;
270         module.environment.service = service;
271         disco_info = build_disco_info(service);
272 end
273
274 function module.save()
275         return { service = service };
276 end
277
278 function module.restore(data)
279         set_service(data.service);
280 end
281
282 set_service(pubsub.new({
283         capabilities = {
284                 none = {
285                         create = false;
286                         publish = false;
287                         retract = false;
288                         get_nodes = true;
289                         
290                         subscribe = true;
291                         unsubscribe = true;
292                         get_subscription = true;
293                         get_items = true;
294                         
295                         subscribe_other = false;
296                         unsubscribe_other = false;
297                         get_subscription_other = false;
298                         
299                         be_subscribed = true;
300                         be_unsubscribed = true;
301                         
302                         set_affiliation = false;
303                 };
304                 owner = {
305                         create = true;
306                         publish = true;
307                         retract = true;
308                         get_nodes = true;
309                         
310                         subscribe = true;
311                         unsubscribe = true;
312                         get_subscription = true;
313                         get_items = true;
314                         
315                         
316                         subscribe_other = true;
317                         unsubscribe_other = true;
318                         get_subscription_other = true;
319                         
320                         be_subscribed = true;
321                         be_unsubscribed = true;
322                         
323                         set_affiliation = true;
324                 };
325         };
326         
327         autocreate_on_publish = autocreate_on_publish;
328         autocreate_on_subscribe = autocreate_on_subscribe;
329         
330         broadcaster = simple_broadcast;
331         get_affiliation = get_affiliation;
332         jids_equal = function (jid1, jid2)
333                 return jid_bare(jid1) == jid_bare(jid2);
334         end;
335 }));