Mercurial > prosody-hg
comparison plugins/mod_pubsub/mod_pubsub.lua @ 5626:8416d4619d80
mod_pubsub: Split out handlers into a module library
| author | Florian Zeitz <florob@babelmonkeys.de> |
|---|---|
| date | Fri, 17 May 2013 18:33:32 +0200 |
| parents | |
| children | 630e7224a65f |
comparison
equal
deleted
inserted
replaced
| 5625:e67891ad18d6 | 5626:8416d4619d80 |
|---|---|
| 1 local pubsub = require "util.pubsub"; | |
| 2 local st = require "util.stanza"; | |
| 3 local jid_bare = require "util.jid".bare; | |
| 4 local usermanager = require "core.usermanager"; | |
| 5 | |
| 6 local xmlns_pubsub = "http://jabber.org/protocol/pubsub"; | |
| 7 local xmlns_pubsub_event = "http://jabber.org/protocol/pubsub#event"; | |
| 8 local xmlns_pubsub_owner = "http://jabber.org/protocol/pubsub#owner"; | |
| 9 | |
| 10 local autocreate_on_publish = module:get_option_boolean("autocreate_on_publish", false); | |
| 11 local autocreate_on_subscribe = module:get_option_boolean("autocreate_on_subscribe", false); | |
| 12 local pubsub_disco_name = module:get_option("name"); | |
| 13 if type(pubsub_disco_name) ~= "string" then pubsub_disco_name = "Prosody PubSub Service"; end | |
| 14 | |
| 15 local service; | |
| 16 | |
| 17 local lib_pubsub = module:require "pubsub"; | |
| 18 local handlers = lib_pubsub.handlers; | |
| 19 local pubsub_error_reply = lib_pubsub.pubsub_error_reply; | |
| 20 | |
| 21 function handle_pubsub_iq(event) | |
| 22 local origin, stanza = event.origin, event.stanza; | |
| 23 local pubsub = stanza.tags[1]; | |
| 24 local action = pubsub.tags[1]; | |
| 25 if not action then | |
| 26 return origin.send(st.error_reply(stanza, "cancel", "bad-request")); | |
| 27 end | |
| 28 local handler = handlers[stanza.attr.type.."_"..action.name]; | |
| 29 if handler then | |
| 30 handler(origin, stanza, action, service); | |
| 31 return true; | |
| 32 end | |
| 33 end | |
| 34 | |
| 35 function simple_broadcast(kind, node, jids, item) | |
| 36 if item then | |
| 37 item = st.clone(item); | |
| 38 item.attr.xmlns = nil; -- Clear the pubsub namespace | |
| 39 end | |
| 40 local message = st.message({ from = module.host, type = "headline" }) | |
| 41 :tag("event", { xmlns = xmlns_pubsub_event }) | |
| 42 :tag(kind, { node = node }) | |
| 43 :add_child(item); | |
| 44 for jid in pairs(jids) do | |
| 45 module:log("debug", "Sending notification to %s", jid); | |
| 46 message.attr.to = jid; | |
| 47 module:send(message); | |
| 48 end | |
| 49 end | |
| 50 | |
| 51 module:hook("iq/host/"..xmlns_pubsub..":pubsub", handle_pubsub_iq); | |
| 52 module:hook("iq/host/"..xmlns_pubsub_owner..":pubsub", handle_pubsub_iq); | |
| 53 | |
| 54 local disco_info; | |
| 55 | |
| 56 local feature_map = { | |
| 57 create = { "create-nodes", "instant-nodes", "item-ids" }; | |
| 58 retract = { "delete-items", "retract-items" }; | |
| 59 purge = { "purge-nodes" }; | |
| 60 publish = { "publish", autocreate_on_publish and "auto-create" }; | |
| 61 delete = { "delete-nodes" }; | |
| 62 get_items = { "retrieve-items" }; | |
| 63 add_subscription = { "subscribe" }; | |
| 64 get_subscriptions = { "retrieve-subscriptions" }; | |
| 65 }; | |
| 66 | |
| 67 local function add_disco_features_from_service(disco, service) | |
| 68 for method, features in pairs(feature_map) do | |
| 69 if service[method] then | |
| 70 for _, feature in ipairs(features) do | |
| 71 if feature then | |
| 72 disco:tag("feature", { var = xmlns_pubsub.."#"..feature }):up(); | |
| 73 end | |
| 74 end | |
| 75 end | |
| 76 end | |
| 77 for affiliation in pairs(service.config.capabilities) do | |
| 78 if affiliation ~= "none" and affiliation ~= "owner" then | |
| 79 disco:tag("feature", { var = xmlns_pubsub.."#"..affiliation.."-affiliation" }):up(); | |
| 80 end | |
| 81 end | |
| 82 end | |
| 83 | |
| 84 local function build_disco_info(service) | |
| 85 local disco_info = st.stanza("query", { xmlns = "http://jabber.org/protocol/disco#info" }) | |
| 86 :tag("identity", { category = "pubsub", type = "service", name = pubsub_disco_name }):up() | |
| 87 :tag("feature", { var = "http://jabber.org/protocol/pubsub" }):up(); | |
| 88 add_disco_features_from_service(disco_info, service); | |
| 89 return disco_info; | |
| 90 end | |
| 91 | |
| 92 module:hook("iq-get/host/http://jabber.org/protocol/disco#info:query", function (event) | |
| 93 local origin, stanza = event.origin, event.stanza; | |
| 94 local node = stanza.tags[1].attr.node; | |
| 95 if not node then | |
| 96 return origin.send(st.reply(stanza):add_child(disco_info)); | |
| 97 else | |
| 98 local ok, ret = service:get_nodes(stanza.attr.from); | |
| 99 if ok and not ret[node] then | |
| 100 ok, ret = false, "item-not-found"; | |
| 101 end | |
| 102 if not ok then | |
| 103 return origin.send(pubsub_error_reply(stanza, ret)); | |
| 104 end | |
| 105 local reply = st.reply(stanza) | |
| 106 :tag("query", { xmlns = "http://jabber.org/protocol/disco#info", node = node }) | |
| 107 :tag("identity", { category = "pubsub", type = "leaf" }); | |
| 108 return origin.send(reply); | |
| 109 end | |
| 110 end); | |
| 111 | |
| 112 local function handle_disco_items_on_node(event) | |
| 113 local stanza, origin = event.stanza, event.origin; | |
| 114 local query = stanza.tags[1]; | |
| 115 local node = query.attr.node; | |
| 116 local ok, ret = service:get_items(node, stanza.attr.from); | |
| 117 if not ok then | |
| 118 return origin.send(pubsub_error_reply(stanza, ret)); | |
| 119 end | |
| 120 | |
| 121 local reply = st.reply(stanza) | |
| 122 :tag("query", { xmlns = "http://jabber.org/protocol/disco#items", node = node }); | |
| 123 | |
| 124 for id, item in pairs(ret) do | |
| 125 reply:tag("item", { jid = module.host, name = id }):up(); | |
| 126 end | |
| 127 | |
| 128 return origin.send(reply); | |
| 129 end | |
| 130 | |
| 131 | |
| 132 module:hook("iq-get/host/http://jabber.org/protocol/disco#items:query", function (event) | |
| 133 if event.stanza.tags[1].attr.node then | |
| 134 return handle_disco_items_on_node(event); | |
| 135 end | |
| 136 local ok, ret = service:get_nodes(event.stanza.attr.from); | |
| 137 if not ok then | |
| 138 event.origin.send(pubsub_error_reply(event.stanza, ret)); | |
| 139 else | |
| 140 local reply = st.reply(event.stanza) | |
| 141 :tag("query", { xmlns = "http://jabber.org/protocol/disco#items" }); | |
| 142 for node, node_obj in pairs(ret) do | |
| 143 reply:tag("item", { jid = module.host, node = node, name = node_obj.config.name }):up(); | |
| 144 end | |
| 145 event.origin.send(reply); | |
| 146 end | |
| 147 return true; | |
| 148 end); | |
| 149 | |
| 150 local admin_aff = module:get_option_string("default_admin_affiliation", "owner"); | |
| 151 local function get_affiliation(jid) | |
| 152 local bare_jid = jid_bare(jid); | |
| 153 if bare_jid == module.host or usermanager.is_admin(bare_jid, module.host) then | |
| 154 return admin_aff; | |
| 155 end | |
| 156 end | |
| 157 | |
| 158 function set_service(new_service) | |
| 159 service = new_service; | |
| 160 module.environment.service = service; | |
| 161 disco_info = build_disco_info(service); | |
| 162 end | |
| 163 | |
| 164 function module.save() | |
| 165 return { service = service }; | |
| 166 end | |
| 167 | |
| 168 function module.restore(data) | |
| 169 set_service(data.service); | |
| 170 end | |
| 171 | |
| 172 set_service(pubsub.new({ | |
| 173 capabilities = { | |
| 174 none = { | |
| 175 create = false; | |
| 176 publish = false; | |
| 177 retract = false; | |
| 178 get_nodes = true; | |
| 179 | |
| 180 subscribe = true; | |
| 181 unsubscribe = true; | |
| 182 get_subscription = true; | |
| 183 get_subscriptions = true; | |
| 184 get_items = true; | |
| 185 | |
| 186 subscribe_other = false; | |
| 187 unsubscribe_other = false; | |
| 188 get_subscription_other = false; | |
| 189 get_subscriptions_other = false; | |
| 190 | |
| 191 be_subscribed = true; | |
| 192 be_unsubscribed = true; | |
| 193 | |
| 194 set_affiliation = false; | |
| 195 }; | |
| 196 publisher = { | |
| 197 create = false; | |
| 198 publish = true; | |
| 199 retract = true; | |
| 200 get_nodes = true; | |
| 201 | |
| 202 subscribe = true; | |
| 203 unsubscribe = true; | |
| 204 get_subscription = true; | |
| 205 get_subscriptions = true; | |
| 206 get_items = true; | |
| 207 | |
| 208 subscribe_other = false; | |
| 209 unsubscribe_other = false; | |
| 210 get_subscription_other = false; | |
| 211 get_subscriptions_other = false; | |
| 212 | |
| 213 be_subscribed = true; | |
| 214 be_unsubscribed = true; | |
| 215 | |
| 216 set_affiliation = false; | |
| 217 }; | |
| 218 owner = { | |
| 219 create = true; | |
| 220 publish = true; | |
| 221 retract = true; | |
| 222 delete = true; | |
| 223 get_nodes = true; | |
| 224 | |
| 225 subscribe = true; | |
| 226 unsubscribe = true; | |
| 227 get_subscription = true; | |
| 228 get_subscriptions = true; | |
| 229 get_items = true; | |
| 230 | |
| 231 | |
| 232 subscribe_other = true; | |
| 233 unsubscribe_other = true; | |
| 234 get_subscription_other = true; | |
| 235 get_subscriptions_other = true; | |
| 236 | |
| 237 be_subscribed = true; | |
| 238 be_unsubscribed = true; | |
| 239 | |
| 240 set_affiliation = true; | |
| 241 }; | |
| 242 }; | |
| 243 | |
| 244 autocreate_on_publish = autocreate_on_publish; | |
| 245 autocreate_on_subscribe = autocreate_on_subscribe; | |
| 246 | |
| 247 broadcaster = simple_broadcast; | |
| 248 get_affiliation = get_affiliation; | |
| 249 | |
| 250 normalize_jid = jid_bare; | |
| 251 })); |
