Mercurial > prosody-hg
comparison plugins/mod_pubsub/pubsub.lib.lua @ 6438:b1c40054b59d
mod_pubsub: Add support for node configuration
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Sun, 28 Sep 2014 01:46:17 +0200 |
| parents | 382e03a40dd2 |
| children | bcb1ea9047d3 |
comparison
equal
deleted
inserted
replaced
| 6437:3f1f11dfdf10 | 6438:b1c40054b59d |
|---|---|
| 1 local st = require "util.stanza"; | 1 local st = require "util.stanza"; |
| 2 local uuid_generate = require "util.uuid".generate; | 2 local uuid_generate = require "util.uuid".generate; |
| 3 | 3 |
| 4 local xmlns_pubsub = "http://jabber.org/protocol/pubsub"; | 4 local xmlns_pubsub = "http://jabber.org/protocol/pubsub"; |
| 5 local xmlns_pubsub_errors = "http://jabber.org/protocol/pubsub#errors"; | 5 local xmlns_pubsub_errors = "http://jabber.org/protocol/pubsub#errors"; |
| 6 local xmlns_pubsub_owner = "http://jabber.org/protocol/pubsub#owner"; | |
| 6 | 7 |
| 7 local _M = {}; | 8 local _M = {}; |
| 8 | 9 |
| 9 local handlers = {}; | 10 local handlers = {}; |
| 10 _M.handlers = handlers; | 11 _M.handlers = handlers; |
| 15 ["jid-required"] = { "modify", "bad-request", nil, "jid-required" }; | 16 ["jid-required"] = { "modify", "bad-request", nil, "jid-required" }; |
| 16 ["nodeid-required"] = { "modify", "bad-request", nil, "nodeid-required" }; | 17 ["nodeid-required"] = { "modify", "bad-request", nil, "nodeid-required" }; |
| 17 ["item-not-found"] = { "cancel", "item-not-found" }; | 18 ["item-not-found"] = { "cancel", "item-not-found" }; |
| 18 ["not-subscribed"] = { "modify", "unexpected-request", nil, "not-subscribed" }; | 19 ["not-subscribed"] = { "modify", "unexpected-request", nil, "not-subscribed" }; |
| 19 ["forbidden"] = { "cancel", "forbidden" }; | 20 ["forbidden"] = { "cancel", "forbidden" }; |
| 21 ["not-allowed"] = { "cancel", "not-allowed" }; | |
| 20 }; | 22 }; |
| 21 local function pubsub_error_reply(stanza, error) | 23 local function pubsub_error_reply(stanza, error) |
| 22 local e = pubsub_errors[error]; | 24 local e = pubsub_errors[error]; |
| 23 local reply = st.error_reply(stanza, unpack(e, 1, 3)); | 25 local reply = st.error_reply(stanza, unpack(e, 1, 3)); |
| 24 if e[4] then | 26 if e[4] then |
| 220 reply = pubsub_error_reply(stanza, ret); | 222 reply = pubsub_error_reply(stanza, ret); |
| 221 end | 223 end |
| 222 return origin.send(reply); | 224 return origin.send(reply); |
| 223 end | 225 end |
| 224 | 226 |
| 227 function handlers.get_configure(origin, stanza, config, service) | |
| 228 local node = config.attr.node; | |
| 229 if not node then | |
| 230 return origin.send(pubsub_error_reply(stanza, "nodeid-required")); | |
| 231 end | |
| 232 | |
| 233 if not service:may(node, actor, "configure") then | |
| 234 return origin.send(pubsub_error_reply(stanza, "forbidden")); | |
| 235 end | |
| 236 | |
| 237 local node_obj = service.nodes[node]; | |
| 238 if not node_obj then | |
| 239 return origin.send(pubsub_error_reply(stanza, "item-not-found")); | |
| 240 end | |
| 241 | |
| 242 local form = self.config.node_config_form; | |
| 243 if not form then | |
| 244 return origin.send(pubsub_error_reply(stanza, "not-allowed")); | |
| 245 end | |
| 246 | |
| 247 local reply = st.reply(stanza) | |
| 248 :tag("pubsub", { xmlns = xmlns_pubsub_owner }) | |
| 249 :tag("configure", { node = node }) | |
| 250 :add_child(form:form(node_obj.config)); | |
| 251 return origin.send(reply); | |
| 252 end | |
| 253 | |
| 254 function handlers.set_configure(origin, stanza, config, service) | |
| 255 local node = config.attr.node; | |
| 256 if not node then | |
| 257 return origin.send(pubsub_error_reply(stanza, "nodeid-required")); | |
| 258 end | |
| 259 local form, node_obj = service:get_node_config_form(node, stanza.attr.from); | |
| 260 if not form then | |
| 261 return origin.send(pubsub_error_reply(stanza, node_obj)); | |
| 262 end | |
| 263 local new_config, err = form:data(config.tags[1]); | |
| 264 if not new_config then | |
| 265 return origin.send(st.error_reply(stanza, "modify", "bad-request", err)); | |
| 266 end | |
| 267 local ok, err = service:set_node_config(node, stanza.attr.from, new_config); | |
| 268 if not ok then | |
| 269 return origin.send(pubsub_error_reply(stanza, err)); | |
| 270 end | |
| 271 return origin.send(st.reply(stanza)); | |
| 272 end | |
| 273 | |
| 225 return _M; | 274 return _M; |
