Software / code / prosody
Annotate
plugins/mod_pubsub.lua @ 3875:e83f3728d101
net.xmppserver_listener: Removed unnecessary import of lxp.
| author | Waqas Hussain <waqas20@gmail.com> |
|---|---|
| date | Wed, 15 Dec 2010 03:59:48 +0500 |
| parent | 3823:5bde3e9d78a7 |
| child | 3760:3bfb65496773 |
| 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 = { | |
|
3820
6f25c09916a5
mod_pubsub: Use pubsub_error_reply everywhere
Florian Zeitz <florob@babelmonkeys.de>
parents:
3796
diff
changeset
|
28 ["conflict"] = { "cancel", "conflict" }; |
| 3619 | 29 ["invalid-jid"] = { "modify", "bad-request", nil, "invalid-jid" }; |
| 30 ["item-not-found"] = { "cancel", "item-not-found" }; | |
|
3821
cef2d5dc65e3
mod_pubsub, util.pubsub: Support for unsubscribing
Florian Zeitz <florob@babelmonkeys.de>
parents:
3820
diff
changeset
|
31 ["not-subscribed"] = { "modify", "unexpected-request", nil, "not-subscribed" }; |
| 3619 | 32 }; |
| 33 function pubsub_error_reply(stanza, error) | |
| 34 local e = pubsub_errors[error]; | |
| 35 local reply = st.error_reply(stanza, unpack(e, 1, 3)); | |
| 36 if e[4] then | |
| 37 reply:tag(e[4], { xmlns = xmlns_pubsub_errors }):up(); | |
| 38 end | |
| 39 return reply; | |
| 40 end | |
| 41 | |
|
3641
3603aeb325de
mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents:
3622
diff
changeset
|
42 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
|
43 local node = items.attr.node; |
|
3603aeb325de
mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents:
3622
diff
changeset
|
44 local item = items:get_child("item"); |
|
3603aeb325de
mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents:
3622
diff
changeset
|
45 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
|
46 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
|
47 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
|
48 data:add_child(entry); |
|
3603aeb325de
mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents:
3622
diff
changeset
|
49 end |
|
3603aeb325de
mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents:
3622
diff
changeset
|
50 if data then |
|
3603aeb325de
mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents:
3622
diff
changeset
|
51 reply = st.reply(stanza) |
|
3603aeb325de
mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents:
3622
diff
changeset
|
52 :tag("pubsub", { xmlns = xmlns_pubsub }) |
|
3603aeb325de
mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents:
3622
diff
changeset
|
53 :add_child(data); |
|
3603aeb325de
mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents:
3622
diff
changeset
|
54 else |
|
3820
6f25c09916a5
mod_pubsub: Use pubsub_error_reply everywhere
Florian Zeitz <florob@babelmonkeys.de>
parents:
3796
diff
changeset
|
55 reply = pubsub_error_reply(stanza, "item-not-found"); |
|
3641
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 return origin.send(reply); |
|
3603aeb325de
mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents:
3622
diff
changeset
|
58 end |
|
3603aeb325de
mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents:
3622
diff
changeset
|
59 |
|
3796
405231b1cb88
mod_pubsub, util.pubsub: Support node creation
Florian Zeitz <florob@babelmonkeys.de>
parents:
3641
diff
changeset
|
60 function handlers.set_create(origin, stanza, create) |
|
405231b1cb88
mod_pubsub, util.pubsub: Support node creation
Florian Zeitz <florob@babelmonkeys.de>
parents:
3641
diff
changeset
|
61 local node = create.attr.node; |
|
405231b1cb88
mod_pubsub, util.pubsub: Support node creation
Florian Zeitz <florob@babelmonkeys.de>
parents:
3641
diff
changeset
|
62 local ok, ret, reply; |
|
405231b1cb88
mod_pubsub, util.pubsub: Support node creation
Florian Zeitz <florob@babelmonkeys.de>
parents:
3641
diff
changeset
|
63 if node then |
|
405231b1cb88
mod_pubsub, util.pubsub: Support node creation
Florian Zeitz <florob@babelmonkeys.de>
parents:
3641
diff
changeset
|
64 ok, ret = service:create(node, stanza.attr.from); |
|
405231b1cb88
mod_pubsub, util.pubsub: Support node creation
Florian Zeitz <florob@babelmonkeys.de>
parents:
3641
diff
changeset
|
65 if ok then |
|
405231b1cb88
mod_pubsub, util.pubsub: Support node creation
Florian Zeitz <florob@babelmonkeys.de>
parents:
3641
diff
changeset
|
66 reply = st.reply(stanza); |
|
405231b1cb88
mod_pubsub, util.pubsub: Support node creation
Florian Zeitz <florob@babelmonkeys.de>
parents:
3641
diff
changeset
|
67 else |
|
3820
6f25c09916a5
mod_pubsub: Use pubsub_error_reply everywhere
Florian Zeitz <florob@babelmonkeys.de>
parents:
3796
diff
changeset
|
68 reply = pubsub_error_reply(stanza, ret); |
|
3796
405231b1cb88
mod_pubsub, util.pubsub: Support node creation
Florian Zeitz <florob@babelmonkeys.de>
parents:
3641
diff
changeset
|
69 end |
|
405231b1cb88
mod_pubsub, util.pubsub: Support node creation
Florian Zeitz <florob@babelmonkeys.de>
parents:
3641
diff
changeset
|
70 else |
|
405231b1cb88
mod_pubsub, util.pubsub: Support node creation
Florian Zeitz <florob@babelmonkeys.de>
parents:
3641
diff
changeset
|
71 repeat |
|
405231b1cb88
mod_pubsub, util.pubsub: Support node creation
Florian Zeitz <florob@babelmonkeys.de>
parents:
3641
diff
changeset
|
72 node = uuid_generate(); |
|
405231b1cb88
mod_pubsub, util.pubsub: Support node creation
Florian Zeitz <florob@babelmonkeys.de>
parents:
3641
diff
changeset
|
73 ok, ret = service:create(node, stanza.attr.from); |
|
405231b1cb88
mod_pubsub, util.pubsub: Support node creation
Florian Zeitz <florob@babelmonkeys.de>
parents:
3641
diff
changeset
|
74 until ok; |
|
405231b1cb88
mod_pubsub, util.pubsub: Support node creation
Florian Zeitz <florob@babelmonkeys.de>
parents:
3641
diff
changeset
|
75 reply = st.reply(stanza) |
|
405231b1cb88
mod_pubsub, util.pubsub: Support node creation
Florian Zeitz <florob@babelmonkeys.de>
parents:
3641
diff
changeset
|
76 :tag("pubsub", { xmlns = xmlns_pubsub }) |
|
405231b1cb88
mod_pubsub, util.pubsub: Support node creation
Florian Zeitz <florob@babelmonkeys.de>
parents:
3641
diff
changeset
|
77 :tag("create", { node = node }); |
|
405231b1cb88
mod_pubsub, util.pubsub: Support node creation
Florian Zeitz <florob@babelmonkeys.de>
parents:
3641
diff
changeset
|
78 end |
|
3822
d6028f4eb610
mod_pubsub: Support item retraction
Florian Zeitz <florob@babelmonkeys.de>
parents:
3821
diff
changeset
|
79 return origin.send(reply); |
|
3796
405231b1cb88
mod_pubsub, util.pubsub: Support node creation
Florian Zeitz <florob@babelmonkeys.de>
parents:
3641
diff
changeset
|
80 end |
|
405231b1cb88
mod_pubsub, util.pubsub: Support node creation
Florian Zeitz <florob@babelmonkeys.de>
parents:
3641
diff
changeset
|
81 |
| 3619 | 82 function handlers.set_subscribe(origin, stanza, subscribe) |
| 83 local node, jid = subscribe.attr.node, subscribe.attr.jid; | |
| 84 if jid_bare(jid) ~= jid_bare(stanza.attr.from) then | |
| 85 return origin.send(pubsub_error_reply(stanza, "invalid-jid")); | |
| 86 end | |
| 87 local ok, ret = service:add_subscription(node, stanza.attr.from, jid); | |
| 88 local reply; | |
| 89 if ok then | |
| 90 reply = st.reply(stanza) | |
| 91 :tag("pubsub", { xmlns = xmlns_pubsub }) | |
| 92 :tag("subscription", { | |
| 93 node = node, | |
| 94 jid = jid, | |
| 95 subscription = "subscribed" | |
| 96 }); | |
| 97 else | |
| 98 reply = pubsub_error_reply(stanza, ret); | |
| 99 end | |
| 100 return origin.send(reply); | |
| 101 end | |
| 102 | |
|
3821
cef2d5dc65e3
mod_pubsub, util.pubsub: Support for unsubscribing
Florian Zeitz <florob@babelmonkeys.de>
parents:
3820
diff
changeset
|
103 function handlers.set_unsubscribe(origin, stanza, unsubscribe) |
|
cef2d5dc65e3
mod_pubsub, util.pubsub: Support for unsubscribing
Florian Zeitz <florob@babelmonkeys.de>
parents:
3820
diff
changeset
|
104 local node, jid = unsubscribe.attr.node, unsubscribe.attr.jid; |
|
cef2d5dc65e3
mod_pubsub, util.pubsub: Support for unsubscribing
Florian Zeitz <florob@babelmonkeys.de>
parents:
3820
diff
changeset
|
105 if jid_bare(jid) ~= jid_bare(stanza.attr.from) then |
|
cef2d5dc65e3
mod_pubsub, util.pubsub: Support for unsubscribing
Florian Zeitz <florob@babelmonkeys.de>
parents:
3820
diff
changeset
|
106 return origin.send(pubsub_error_reply(stanza, "invalid-jid")); |
|
cef2d5dc65e3
mod_pubsub, util.pubsub: Support for unsubscribing
Florian Zeitz <florob@babelmonkeys.de>
parents:
3820
diff
changeset
|
107 end |
|
cef2d5dc65e3
mod_pubsub, util.pubsub: Support for unsubscribing
Florian Zeitz <florob@babelmonkeys.de>
parents:
3820
diff
changeset
|
108 local ok, ret = service:remove_subscription(node, stanza.attr.from, jid); |
|
cef2d5dc65e3
mod_pubsub, util.pubsub: Support for unsubscribing
Florian Zeitz <florob@babelmonkeys.de>
parents:
3820
diff
changeset
|
109 local reply; |
|
cef2d5dc65e3
mod_pubsub, util.pubsub: Support for unsubscribing
Florian Zeitz <florob@babelmonkeys.de>
parents:
3820
diff
changeset
|
110 if ok then |
|
cef2d5dc65e3
mod_pubsub, util.pubsub: Support for unsubscribing
Florian Zeitz <florob@babelmonkeys.de>
parents:
3820
diff
changeset
|
111 reply = st.reply(stanza); |
|
cef2d5dc65e3
mod_pubsub, util.pubsub: Support for unsubscribing
Florian Zeitz <florob@babelmonkeys.de>
parents:
3820
diff
changeset
|
112 else |
|
cef2d5dc65e3
mod_pubsub, util.pubsub: Support for unsubscribing
Florian Zeitz <florob@babelmonkeys.de>
parents:
3820
diff
changeset
|
113 reply = pubsub_error_reply(stanza, ret); |
|
cef2d5dc65e3
mod_pubsub, util.pubsub: Support for unsubscribing
Florian Zeitz <florob@babelmonkeys.de>
parents:
3820
diff
changeset
|
114 end |
|
cef2d5dc65e3
mod_pubsub, util.pubsub: Support for unsubscribing
Florian Zeitz <florob@babelmonkeys.de>
parents:
3820
diff
changeset
|
115 return origin.send(reply); |
|
cef2d5dc65e3
mod_pubsub, util.pubsub: Support for unsubscribing
Florian Zeitz <florob@babelmonkeys.de>
parents:
3820
diff
changeset
|
116 end |
|
cef2d5dc65e3
mod_pubsub, util.pubsub: Support for unsubscribing
Florian Zeitz <florob@babelmonkeys.de>
parents:
3820
diff
changeset
|
117 |
| 3619 | 118 function handlers.set_publish(origin, stanza, publish) |
| 119 local node = publish.attr.node; | |
| 120 local item = publish:get_child("item"); | |
| 121 local id = (item and item.attr.id) or uuid_generate(); | |
| 122 local ok, ret = service:publish(node, stanza.attr.from, id, item); | |
| 123 local reply; | |
| 124 if ok then | |
| 125 reply = st.reply(stanza) | |
| 126 :tag("pubsub", { xmlns = xmlns_pubsub }) | |
| 127 :tag("publish", { node = node }) | |
| 128 :tag("item", { id = id }); | |
| 129 else | |
| 130 reply = pubsub_error_reply(stanza, ret); | |
| 131 end | |
| 132 return origin.send(reply); | |
| 133 end | |
| 134 | |
|
3822
d6028f4eb610
mod_pubsub: Support item retraction
Florian Zeitz <florob@babelmonkeys.de>
parents:
3821
diff
changeset
|
135 function handlers.set_retract(origin, stanza, retract) |
|
d6028f4eb610
mod_pubsub: Support item retraction
Florian Zeitz <florob@babelmonkeys.de>
parents:
3821
diff
changeset
|
136 local node, notify = retract.attr.node, retract.attr.notify; |
|
d6028f4eb610
mod_pubsub: Support item retraction
Florian Zeitz <florob@babelmonkeys.de>
parents:
3821
diff
changeset
|
137 notify = (notify == "1") or (notify == "true"); |
|
d6028f4eb610
mod_pubsub: Support item retraction
Florian Zeitz <florob@babelmonkeys.de>
parents:
3821
diff
changeset
|
138 local item = retract:get_child("item"); |
|
d6028f4eb610
mod_pubsub: Support item retraction
Florian Zeitz <florob@babelmonkeys.de>
parents:
3821
diff
changeset
|
139 local id = item and item.attr.id |
|
d6028f4eb610
mod_pubsub: Support item retraction
Florian Zeitz <florob@babelmonkeys.de>
parents:
3821
diff
changeset
|
140 local reply, notifier; |
|
d6028f4eb610
mod_pubsub: Support item retraction
Florian Zeitz <florob@babelmonkeys.de>
parents:
3821
diff
changeset
|
141 if notify then |
|
d6028f4eb610
mod_pubsub: Support item retraction
Florian Zeitz <florob@babelmonkeys.de>
parents:
3821
diff
changeset
|
142 notifier = st.stanza("retract", { id = id }); |
|
d6028f4eb610
mod_pubsub: Support item retraction
Florian Zeitz <florob@babelmonkeys.de>
parents:
3821
diff
changeset
|
143 end |
|
d6028f4eb610
mod_pubsub: Support item retraction
Florian Zeitz <florob@babelmonkeys.de>
parents:
3821
diff
changeset
|
144 local ok, ret = service:retract(node, stanza.attr.from, id, notifier); |
|
d6028f4eb610
mod_pubsub: Support item retraction
Florian Zeitz <florob@babelmonkeys.de>
parents:
3821
diff
changeset
|
145 if ok then |
|
d6028f4eb610
mod_pubsub: Support item retraction
Florian Zeitz <florob@babelmonkeys.de>
parents:
3821
diff
changeset
|
146 reply = st.reply(stanza); |
|
d6028f4eb610
mod_pubsub: Support item retraction
Florian Zeitz <florob@babelmonkeys.de>
parents:
3821
diff
changeset
|
147 else |
|
d6028f4eb610
mod_pubsub: Support item retraction
Florian Zeitz <florob@babelmonkeys.de>
parents:
3821
diff
changeset
|
148 reply = pubsub_error_reply(stanza, ret); |
|
d6028f4eb610
mod_pubsub: Support item retraction
Florian Zeitz <florob@babelmonkeys.de>
parents:
3821
diff
changeset
|
149 end |
|
d6028f4eb610
mod_pubsub: Support item retraction
Florian Zeitz <florob@babelmonkeys.de>
parents:
3821
diff
changeset
|
150 return origin.send(reply); |
|
d6028f4eb610
mod_pubsub: Support item retraction
Florian Zeitz <florob@babelmonkeys.de>
parents:
3821
diff
changeset
|
151 end |
|
d6028f4eb610
mod_pubsub: Support item retraction
Florian Zeitz <florob@babelmonkeys.de>
parents:
3821
diff
changeset
|
152 |
| 3619 | 153 function simple_broadcast(node, jids, item) |
|
3823
5bde3e9d78a7
mod_pubsub: Ensure <item> is in correct scope when broadcasting an event
Matthew Wild <mwild1@gmail.com>
parents:
3822
diff
changeset
|
154 item = st.clone(item); |
|
5bde3e9d78a7
mod_pubsub: Ensure <item> is in correct scope when broadcasting an event
Matthew Wild <mwild1@gmail.com>
parents:
3822
diff
changeset
|
155 item.attr.xmlns = nil; -- Clear the pubsub namespace |
| 3619 | 156 local message = st.message({ from = module.host, type = "headline" }) |
| 157 :tag("event", { xmlns = xmlns_pubsub_event }) | |
| 158 :tag("items", { node = node }) | |
| 159 :add_child(item); | |
| 160 for jid in pairs(jids) do | |
| 161 module:log("debug", "Sending notification to %s", jid); | |
| 162 message.attr.to = jid; | |
| 163 core_post_stanza(hosts[module.host], message); | |
| 164 end | |
| 165 end | |
| 166 | |
|
3621
7e1a1dd42088
mod_pubsub: Expose 'service'
Matthew Wild <mwild1@gmail.com>
parents:
3619
diff
changeset
|
167 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
|
168 |
| 3619 | 169 service = pubsub.new({ |
| 170 broadcaster = simple_broadcast | |
| 171 }); | |
|
3622
418354197a02
mod_pubsub: Use module.environment to reference the module's environment
Matthew Wild <mwild1@gmail.com>
parents:
3621
diff
changeset
|
172 module.environment.service = service; |
| 3619 | 173 |