3619
|
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 service; |
|
13 |
|
|
14 |
local handlers = {}; |
|
15 |
|
|
16 |
function handle_pubsub_iq(event) |
|
17 |
local origin, stanza = event.origin, event.stanza; |
|
18 |
local pubsub = stanza.tags[1]; |
|
19 |
local action = pubsub.tags[1]; |
|
20 |
local handler = handlers[stanza.attr.type.."_"..action.name]; |
|
21 |
if handler then |
|
22 |
handler(origin, stanza, action); |
|
23 |
return true; |
|
24 |
end |
|
25 |
end |
|
26 |
|
|
27 |
local pubsub_errors = { |
|
28 |
["invalid-jid"] = { "modify", "bad-request", nil, "invalid-jid" }; |
|
29 |
["item-not-found"] = { "cancel", "item-not-found" }; |
|
30 |
}; |
|
31 |
function pubsub_error_reply(stanza, error) |
|
32 |
local e = pubsub_errors[error]; |
|
33 |
local reply = st.error_reply(stanza, unpack(e, 1, 3)); |
|
34 |
if e[4] then |
|
35 |
reply:tag(e[4], { xmlns = xmlns_pubsub_errors }):up(); |
|
36 |
end |
|
37 |
return reply; |
|
38 |
end |
|
39 |
|
|
40 |
function handlers.set_subscribe(origin, stanza, subscribe) |
|
41 |
local node, jid = subscribe.attr.node, subscribe.attr.jid; |
|
42 |
if jid_bare(jid) ~= jid_bare(stanza.attr.from) then |
|
43 |
return origin.send(pubsub_error_reply(stanza, "invalid-jid")); |
|
44 |
end |
|
45 |
local ok, ret = service:add_subscription(node, stanza.attr.from, jid); |
|
46 |
local reply; |
|
47 |
if ok then |
|
48 |
reply = st.reply(stanza) |
|
49 |
:tag("pubsub", { xmlns = xmlns_pubsub }) |
|
50 |
:tag("subscription", { |
|
51 |
node = node, |
|
52 |
jid = jid, |
|
53 |
subscription = "subscribed" |
|
54 |
}); |
|
55 |
else |
|
56 |
reply = pubsub_error_reply(stanza, ret); |
|
57 |
end |
|
58 |
return origin.send(reply); |
|
59 |
end |
|
60 |
|
|
61 |
function handlers.set_publish(origin, stanza, publish) |
|
62 |
local node = publish.attr.node; |
|
63 |
local item = publish:get_child("item"); |
|
64 |
local id = (item and item.attr.id) or uuid_generate(); |
|
65 |
local ok, ret = service:publish(node, stanza.attr.from, id, item); |
|
66 |
local reply; |
|
67 |
if ok then |
|
68 |
reply = st.reply(stanza) |
|
69 |
:tag("pubsub", { xmlns = xmlns_pubsub }) |
|
70 |
:tag("publish", { node = node }) |
|
71 |
:tag("item", { id = id }); |
|
72 |
else |
|
73 |
reply = pubsub_error_reply(stanza, ret); |
|
74 |
end |
|
75 |
return origin.send(reply); |
|
76 |
end |
|
77 |
|
|
78 |
function simple_broadcast(node, jids, item) |
|
79 |
local message = st.message({ from = module.host, type = "headline" }) |
|
80 |
:tag("event", { xmlns = xmlns_pubsub_event }) |
|
81 |
:tag("items", { node = node }) |
|
82 |
:add_child(item); |
|
83 |
for jid in pairs(jids) do |
|
84 |
module:log("debug", "Sending notification to %s", jid); |
|
85 |
message.attr.to = jid; |
|
86 |
core_post_stanza(hosts[module.host], message); |
|
87 |
end |
|
88 |
end |
|
89 |
|
|
90 |
service = pubsub.new({ |
|
91 |
broadcaster = simple_broadcast |
|
92 |
}); |
|
93 |
|
|
94 |
module:hook("iq/host/http://jabber.org/protocol/pubsub:pubsub", handle_pubsub_iq); |