configmanager: Added rawget().
[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 module:hook("iq-get/host/http://jabber.org/protocol/disco#items:query", function (event)
223         local ok, ret = service:get_nodes(event.stanza.attr.from);
224         if not ok then
225                 event.origin.send(pubsub_error_reply(stanza, ret));
226         else
227                 local reply = st.reply(event.stanza)
228                         :tag("query", { xmlns = "http://jabber.org/protocol/disco#items" });
229                 for node, node_obj in pairs(ret) do
230                         reply:tag("item", { jid = module.host, node = node, name = node_obj.config.name }):up();
231                 end
232                 event.origin.send(reply);
233         end
234         return true;
235 end);
236
237 local admin_aff = module:get_option_string("default_admin_affiliation", "owner");
238 local function get_affiliation(jid)
239         local bare_jid = jid_bare(jid);
240         if bare_jid == module.host or usermanager.is_admin(bare_jid, module.host) then
241                 return admin_aff;
242         end
243 end
244
245 function set_service(new_service)
246         service = new_service;
247         module.environment.service = service;
248         disco_info = build_disco_info(service);
249 end
250
251 function module.save()
252         return { service = service };
253 end
254
255 function module.restore(data)
256         set_service(data.service);
257 end
258
259 set_service(pubsub.new({
260         capabilities = {
261                 none = {
262                         create = false;
263                         publish = false;
264                         retract = false;
265                         get_nodes = true;
266                         
267                         subscribe = true;
268                         unsubscribe = true;
269                         get_subscription = true;
270                         get_items = true;
271                         
272                         subscribe_other = false;
273                         unsubscribe_other = false;
274                         get_subscription_other = false;
275                         
276                         be_subscribed = true;
277                         be_unsubscribed = true;
278                         
279                         set_affiliation = false;
280                 };
281                 owner = {
282                         create = true;
283                         publish = true;
284                         retract = true;
285                         get_nodes = true;
286                         
287                         subscribe = true;
288                         unsubscribe = true;
289                         get_subscription = true;
290                         get_items = true;
291                         
292                         
293                         subscribe_other = true;
294                         unsubscribe_other = true;
295                         get_subscription_other = true;
296                         
297                         be_subscribed = true;
298                         be_unsubscribed = true;
299                         
300                         set_affiliation = true;
301                 };
302         };
303         
304         autocreate_on_publish = autocreate_on_publish;
305         autocreate_on_subscribe = autocreate_on_subscribe;
306         
307         broadcaster = simple_broadcast;
308         get_affiliation = get_affiliation;
309         jids_equal = function (jid1, jid2)
310                 return jid_bare(jid1) == jid_bare(jid2);
311         end;
312 }));