Software /
code /
prosody
Annotate
plugins/mod_pubsub.lua @ 3791:04c7b71ce078
mod_pep: Optimised PEP requests for disco info on caps change (issue #150).
author | Waqas Hussain <waqas20@gmail.com> |
---|---|
date | Sun, 28 Nov 2010 07:43:19 +0500 |
parent | 3641:3603aeb325de |
child | 3672:b24db47995ac |
rev | line source |
---|---|
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 | |
3641
3603aeb325de
mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents:
3622
diff
changeset
|
40 function handlers.get_items(origin, stanza, items) |
3603aeb325de
mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents:
3622
diff
changeset
|
41 local node = items.attr.node; |
3603aeb325de
mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents:
3622
diff
changeset
|
42 local item = items:get_child("item"); |
3603aeb325de
mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents:
3622
diff
changeset
|
43 local id = item and item.attr.id; |
3603aeb325de
mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents:
3622
diff
changeset
|
44 local data = st.stanza("items", { node = node }); |
3603aeb325de
mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents:
3622
diff
changeset
|
45 for _, entry in pairs(service:get(node, stanza.attr.from, id)) do |
3603aeb325de
mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents:
3622
diff
changeset
|
46 data:add_child(entry); |
3603aeb325de
mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents:
3622
diff
changeset
|
47 end |
3603aeb325de
mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents:
3622
diff
changeset
|
48 if data then |
3603aeb325de
mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents:
3622
diff
changeset
|
49 reply = st.reply(stanza) |
3603aeb325de
mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents:
3622
diff
changeset
|
50 :tag("pubsub", { xmlns = xmlns_pubsub }) |
3603aeb325de
mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents:
3622
diff
changeset
|
51 :add_child(data); |
3603aeb325de
mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents:
3622
diff
changeset
|
52 else |
3603aeb325de
mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents:
3622
diff
changeset
|
53 reply = st.error_reply(stanza, "cancel", "item-not-found", "Item could not be found in this node"); |
3603aeb325de
mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents:
3622
diff
changeset
|
54 end |
3603aeb325de
mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents:
3622
diff
changeset
|
55 return origin.send(reply); |
3603aeb325de
mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents:
3622
diff
changeset
|
56 end |
3603aeb325de
mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents:
3622
diff
changeset
|
57 |
3619 | 58 function handlers.set_subscribe(origin, stanza, subscribe) |
59 local node, jid = subscribe.attr.node, subscribe.attr.jid; | |
60 if jid_bare(jid) ~= jid_bare(stanza.attr.from) then | |
61 return origin.send(pubsub_error_reply(stanza, "invalid-jid")); | |
62 end | |
63 local ok, ret = service:add_subscription(node, stanza.attr.from, jid); | |
64 local reply; | |
65 if ok then | |
66 reply = st.reply(stanza) | |
67 :tag("pubsub", { xmlns = xmlns_pubsub }) | |
68 :tag("subscription", { | |
69 node = node, | |
70 jid = jid, | |
71 subscription = "subscribed" | |
72 }); | |
73 else | |
74 reply = pubsub_error_reply(stanza, ret); | |
75 end | |
76 return origin.send(reply); | |
77 end | |
78 | |
79 function handlers.set_publish(origin, stanza, publish) | |
80 local node = publish.attr.node; | |
81 local item = publish:get_child("item"); | |
82 local id = (item and item.attr.id) or uuid_generate(); | |
83 local ok, ret = service:publish(node, stanza.attr.from, id, item); | |
84 local reply; | |
85 if ok then | |
86 reply = st.reply(stanza) | |
87 :tag("pubsub", { xmlns = xmlns_pubsub }) | |
88 :tag("publish", { node = node }) | |
89 :tag("item", { id = id }); | |
90 else | |
91 reply = pubsub_error_reply(stanza, ret); | |
92 end | |
93 return origin.send(reply); | |
94 end | |
95 | |
96 function simple_broadcast(node, jids, item) | |
97 local message = st.message({ from = module.host, type = "headline" }) | |
98 :tag("event", { xmlns = xmlns_pubsub_event }) | |
99 :tag("items", { node = node }) | |
100 :add_child(item); | |
101 for jid in pairs(jids) do | |
102 module:log("debug", "Sending notification to %s", jid); | |
103 message.attr.to = jid; | |
104 core_post_stanza(hosts[module.host], message); | |
105 end | |
106 end | |
107 | |
3621
7e1a1dd42088
mod_pubsub: Expose 'service'
Matthew Wild <mwild1@gmail.com>
parents:
3619
diff
changeset
|
108 module:hook("iq/host/http://jabber.org/protocol/pubsub:pubsub", handle_pubsub_iq); |
7e1a1dd42088
mod_pubsub: Expose 'service'
Matthew Wild <mwild1@gmail.com>
parents:
3619
diff
changeset
|
109 |
3619 | 110 service = pubsub.new({ |
111 broadcaster = simple_broadcast | |
112 }); | |
3622
418354197a02
mod_pubsub: Use module.environment to reference the module's environment
Matthew Wild <mwild1@gmail.com>
parents:
3621
diff
changeset
|
113 module.environment.service = service; |
3619 | 114 |