Mercurial > prosody-modules
comparison mod_ipcheck/mod_ipcheck.lua @ 3269:b0628bc93acf
mod_ipcheck: Simplify iq handling by hooking on iq-get/ instead of iq/.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Fri, 24 Aug 2018 21:15:38 +0200 |
parents | e5039f14e2a7 |
children |
comparison
equal
deleted
inserted
replaced
3268:4cdd1ddae72c | 3269:b0628bc93acf |
---|---|
4 | 4 |
5 local st = require "util.stanza"; | 5 local st = require "util.stanza"; |
6 | 6 |
7 module:add_feature("urn:xmpp:sic:0"); | 7 module:add_feature("urn:xmpp:sic:0"); |
8 | 8 |
9 module:hook("iq/bare/urn:xmpp:sic:0:ip", function(event) | 9 module:hook("iq-get/bare/urn:xmpp:sic:0:ip", function(event) |
10 local origin, stanza = event.origin, event.stanza; | 10 local origin, stanza = event.origin, event.stanza; |
11 if stanza.attr.type == "get" then | 11 if stanza.attr.to then |
12 if stanza.attr.to then | 12 origin.send(st.error_reply(stanza, "auth", "forbidden", "You can only ask about your own IP address")); |
13 origin.send(st.error_reply(stanza, "auth", "forbidden", "You can only ask about your own IP address")); | 13 elseif origin.ip then |
14 elseif origin.ip then | 14 origin.send(st.reply(stanza):tag("ip", {xmlns='urn:xmpp:sic:0'}):text(origin.ip)); |
15 origin.send(st.reply(stanza):tag("ip", {xmlns='urn:xmpp:sic:0'}):text(origin.ip)); | 15 else |
16 else | 16 -- IP addresses should normally be available, but in case they are not |
17 -- IP addresses should normally be available, but in case they are not | 17 origin.send(st.error_reply(stanza, "cancel", "service-unavailable", "IP address for this session is not available")); |
18 origin.send(st.error_reply(stanza, "cancel", "service-unavailable", "IP address for this session is not available")); | |
19 end | |
20 return true; | |
21 end | 18 end |
19 return true; | |
22 end); | 20 end); |
23 | 21 |
24 module:add_feature("urn:xmpp:sic:1"); | 22 module:add_feature("urn:xmpp:sic:1"); |
25 | 23 |
26 module:hook("iq/bare/urn:xmpp:sic:1:address", function(event) | 24 module:hook("iq-get/bare/urn:xmpp:sic:1:address", function(event) |
27 local origin, stanza = event.origin, event.stanza; | 25 local origin, stanza = event.origin, event.stanza; |
28 if stanza.attr.type == "get" then | 26 if stanza.attr.to then |
29 if stanza.attr.to then | 27 origin.send(st.error_reply(stanza, "auth", "forbidden", "You can only ask about your own IP address")); |
30 origin.send(st.error_reply(stanza, "auth", "forbidden", "You can only ask about your own IP address")); | 28 elseif origin.ip then |
31 elseif origin.ip then | 29 local reply = st.reply(stanza):tag("address", {xmlns='urn:xmpp:sic:1'}) |
32 local reply = st.reply(stanza):tag("address", {xmlns='urn:xmpp:sic:1'}) | 30 :tag("ip"):text(origin.ip):up() |
33 :tag("ip"):text(origin.ip):up() | 31 if origin.conn and origin.conn.port then -- server_event |
34 if origin.conn and origin.conn.port then -- server_event | 32 reply:tag("port"):text(tostring(origin.conn:port())) |
35 reply:tag("port"):text(tostring(origin.conn:port())) | 33 elseif origin.conn and origin.conn.clientport then -- server_select |
36 elseif origin.conn and origin.conn.clientport then -- server_select | 34 reply:tag("port"):text(tostring(origin.conn:clientport())) |
37 reply:tag("port"):text(tostring(origin.conn:clientport())) | |
38 end | |
39 origin.send(reply); | |
40 else | |
41 -- IP addresses should normally be available, but in case they are not | |
42 origin.send(st.error_reply(stanza, "cancel", "service-unavailable", "IP address for this session is not available")); | |
43 end | 35 end |
44 return true; | 36 origin.send(reply); |
37 else | |
38 -- IP addresses should normally be available, but in case they are not | |
39 origin.send(st.error_reply(stanza, "cancel", "service-unavailable", "IP address for this session is not available")); | |
45 end | 40 end |
41 return true; | |
46 end); | 42 end); |