Software / code / prosody
Comparison
plugins/mod_proxy65.lua @ 3688:f7de887ea0be
mod_proxy65: Updated to use sub-events. Now only hooks what it needs to.
| author | Waqas Hussain <waqas20@gmail.com> |
|---|---|
| date | Fri, 03 Dec 2010 00:02:53 +0500 |
| parent | 3676:13a1d90b4e1c |
| child | 3689:2ca76b4f6404 |
comparison
equal
deleted
inserted
replaced
| 3687:55acf6df9a18 | 3688:f7de887ea0be |
|---|---|
| 116 -- Clean up any session-related stuff here | 116 -- Clean up any session-related stuff here |
| 117 sessions[conn] = nil; | 117 sessions[conn] = nil; |
| 118 end | 118 end |
| 119 end | 119 end |
| 120 | 120 |
| 121 local function get_disco_info(stanza) | 121 module:hook("iq-get/host/http://jabber.org/protocol/disco#info:query", function(event) |
| 122 local origin, stanza = event.origin, event.stanza; | |
| 122 local reply = replies_cache.disco_info; | 123 local reply = replies_cache.disco_info; |
| 123 if reply == nil then | 124 if reply == nil then |
| 124 reply = st.iq({type='result', from=host}):query("http://jabber.org/protocol/disco#info") | 125 reply = st.iq({type='result', from=host}):query("http://jabber.org/protocol/disco#info") |
| 125 :tag("identity", {category='proxy', type='bytestreams', name=name}):up() | 126 :tag("identity", {category='proxy', type='bytestreams', name=name}):up() |
| 126 :tag("feature", {var="http://jabber.org/protocol/bytestreams"}); | 127 :tag("feature", {var="http://jabber.org/protocol/bytestreams"}); |
| 127 replies_cache.disco_info = reply; | 128 replies_cache.disco_info = reply; |
| 128 end | 129 end |
| 129 | 130 |
| 130 reply.attr.id = stanza.attr.id; | 131 reply.attr.id = stanza.attr.id; |
| 131 reply.attr.to = stanza.attr.from; | 132 reply.attr.to = stanza.attr.from; |
| 132 return reply; | 133 origin.send(reply); |
| 133 end | 134 return true; |
| 134 | 135 end, -1); |
| 135 local function get_disco_items(stanza) | 136 |
| 137 module:hook("iq-get/host/http://jabber.org/protocol/disco#items:query", function(event) | |
| 138 local origin, stanza = event.origin, event.stanza; | |
| 136 local reply = replies_cache.disco_items; | 139 local reply = replies_cache.disco_items; |
| 137 if reply == nil then | 140 if reply == nil then |
| 138 reply = st.iq({type='result', from=host}):query("http://jabber.org/protocol/disco#items"); | 141 reply = st.iq({type='result', from=host}):query("http://jabber.org/protocol/disco#items"); |
| 139 replies_cache.disco_items = reply; | 142 replies_cache.disco_items = reply; |
| 140 end | 143 end |
| 141 | 144 |
| 142 reply.attr.id = stanza.attr.id; | 145 reply.attr.id = stanza.attr.id; |
| 143 reply.attr.to = stanza.attr.from; | 146 reply.attr.to = stanza.attr.from; |
| 144 return reply; | 147 origin.send(reply); |
| 145 end | 148 return true; |
| 146 | 149 end, -1); |
| 147 local function get_stream_host(origin, stanza) | 150 |
| 151 module:hook("iq-get/host/http://jabber.org/protocol/bytestreams:query", function(event) | |
| 152 local origin, stanza = event.origin, event.stanza; | |
| 148 local reply = replies_cache.stream_host; | 153 local reply = replies_cache.stream_host; |
| 149 local err_reply = replies_cache.stream_host_err; | 154 local err_reply = replies_cache.stream_host_err; |
| 150 local sid = stanza.tags[1].attr.sid; | 155 local sid = stanza.tags[1].attr.sid; |
| 151 local allow = false; | 156 local allow = false; |
| 152 local jid = stanza.attr.from; | 157 local jid = stanza.attr.from; |
| 177 reply = err_reply; | 182 reply = err_reply; |
| 178 end | 183 end |
| 179 reply.attr.id = stanza.attr.id; | 184 reply.attr.id = stanza.attr.id; |
| 180 reply.attr.to = stanza.attr.from; | 185 reply.attr.to = stanza.attr.from; |
| 181 reply.tags[1].attr.sid = sid; | 186 reply.tags[1].attr.sid = sid; |
| 182 return reply; | 187 origin.send(reply); |
| 183 end | 188 return true; |
| 189 end); | |
| 184 | 190 |
| 185 module.unload = function() | 191 module.unload = function() |
| 186 connlisteners.deregister(module.host .. ':proxy65'); | 192 connlisteners.deregister(module.host .. ':proxy65'); |
| 187 end | 193 end |
| 188 | 194 |
| 202 reply.attr.id = stanza.attr.id; | 208 reply.attr.id = stanza.attr.id; |
| 203 end | 209 end |
| 204 return reply, from, to, sid; | 210 return reply, from, to, sid; |
| 205 end | 211 end |
| 206 | 212 |
| 207 function handle_to_domain(event) | 213 module:hook("iq-set/host/http://jabber.org/protocol/bytestreams:query", function(event) |
| 208 local origin, stanza = event.origin, event.stanza; | 214 local origin, stanza = event.origin, event.stanza; |
| 209 if stanza.attr.type == "get" then | 215 |
| 210 local xmlns = stanza.tags[1].attr.xmlns | 216 module:log("debug", "Received activation request from %s", stanza.attr.from); |
| 211 if xmlns == "http://jabber.org/protocol/disco#info" then | 217 local reply, from, to, sid = set_activation(stanza); |
| 212 origin.send(get_disco_info(stanza)); | 218 if reply ~= nil and from ~= nil and to ~= nil and sid ~= nil then |
| 213 return true; | 219 local sha = sha1(sid .. from .. to, true); |
| 214 elseif xmlns == "http://jabber.org/protocol/disco#items" then | 220 if transfers[sha] == nil then |
| 215 origin.send(get_disco_items(stanza)); | 221 module:log("error", "transfers[sha]: nil"); |
| 216 return true; | 222 elseif(transfers[sha] ~= nil and transfers[sha].initiator ~= nil and transfers[sha].target ~= nil) then |
| 217 elseif xmlns == "http://jabber.org/protocol/bytestreams" then | 223 origin.send(reply); |
| 218 origin.send(get_stream_host(origin, stanza)); | 224 transfers[sha].activated = true; |
| 219 return true; | 225 transfers[sha].target:lock_read(false); |
| 226 transfers[sha].initiator:lock_read(false); | |
| 220 else | 227 else |
| 221 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); | 228 module:log("debug", "Both parties were not yet connected"); |
| 222 return true; | 229 local message = "Neither party is connected to the proxy"; |
| 223 end | 230 if transfers[sha].initiator then |
| 224 else -- stanza.attr.type == "set" | 231 message = "The recipient is not connected to the proxy"; |
| 225 module:log("debug", "Received activation request from %s", stanza.attr.from); | 232 elseif transfers[sha].target then |
| 226 local reply, from, to, sid = set_activation(stanza); | 233 message = "The sender (you) is not connected to the proxy"; |
| 227 if reply ~= nil and from ~= nil and to ~= nil and sid ~= nil then | 234 end |
| 228 local sha = sha1(sid .. from .. to, true); | 235 origin.send(st.error_reply(stanza, "cancel", "not-allowed", message)); |
| 229 if transfers[sha] == nil then | 236 end |
| 230 module:log("error", "transfers[sha]: nil"); | 237 return true; |
| 231 elseif(transfers[sha] ~= nil and transfers[sha].initiator ~= nil and transfers[sha].target ~= nil) then | 238 else |
| 232 origin.send(reply); | 239 module:log("error", "activation failed: sid: %s, initiator: %s, target: %s", tostring(sid), tostring(from), tostring(to)); |
| 233 transfers[sha].activated = true; | 240 end |
| 234 transfers[sha].target:lock_read(false); | 241 end); |
| 235 transfers[sha].initiator:lock_read(false); | |
| 236 else | |
| 237 module:log("debug", "Both parties were not yet connected"); | |
| 238 local message = "Neither party is connected to the proxy"; | |
| 239 if transfers[sha].initiator then | |
| 240 message = "The recipient is not connected to the proxy"; | |
| 241 elseif transfers[sha].target then | |
| 242 message = "The sender (you) is not connected to the proxy"; | |
| 243 end | |
| 244 origin.send(st.error_reply(stanza, "cancel", "not-allowed", message)); | |
| 245 end | |
| 246 return true; | |
| 247 else | |
| 248 module:log("error", "activation failed: sid: %s, initiator: %s, target: %s", tostring(sid), tostring(from), tostring(to)); | |
| 249 end | |
| 250 end | |
| 251 end | |
| 252 module:hook("iq/host", handle_to_domain, -1); | |
| 253 | 242 |
| 254 if not connlisteners.register(module.host .. ':proxy65', connlistener) then | 243 if not connlisteners.register(module.host .. ':proxy65', connlistener) then |
| 255 module:log("error", "mod_proxy65: Could not establish a connection listener. Check your configuration please."); | 244 module:log("error", "mod_proxy65: Could not establish a connection listener. Check your configuration please."); |
| 256 module:log("error", "Possibly two proxy65 components are configured to share the same port."); | 245 module:log("error", "Possibly two proxy65 components are configured to share the same port."); |
| 257 end | 246 end |