Mercurial > prosody-modules
comparison mod_register_redirect/mod_register_redirect.lua @ 530:3cc17ef98be0
mod_register_redirect: stick to one code "punctuation" style.
| author | Marco Cirillo <maranda@lightwitch.org> |
|---|---|
| date | Sat, 07 Jan 2012 18:11:39 +0000 |
| parents | 7679b8f6b886 |
| children | 5c7175be532b |
comparison
equal
deleted
inserted
replaced
| 529:84e992f70ba3 | 530:3cc17ef98be0 |
|---|---|
| 3 -- | 3 -- |
| 4 -- Registration Redirect module for Prosody | 4 -- Registration Redirect module for Prosody |
| 5 -- | 5 -- |
| 6 -- Redirects IP addresses not in the whitelist to a web page or another method to complete the registration. | 6 -- Redirects IP addresses not in the whitelist to a web page or another method to complete the registration. |
| 7 | 7 |
| 8 local st = require "util.stanza"; | 8 local st = require "util.stanza" |
| 9 local cman = require "core.configmanager"; | 9 local cman = require "core.configmanager" |
| 10 | 10 |
| 11 function reg_redirect(event) | 11 function reg_redirect(event) |
| 12 local stanza, origin = event.stanza, event.origin; | 12 local stanza, origin = event.stanza, event.origin |
| 13 local ip_wl = module:get_option("registration_whitelist") or { "127.0.0.1" }; | 13 local ip_wl = module:get_option("registration_whitelist") or { "127.0.0.1" } |
| 14 local url = module:get_option_string("registration_url", nil); | 14 local url = module:get_option_string("registration_url", nil) |
| 15 local inst_text = module:get_option_string("registration_text", nil); | 15 local inst_text = module:get_option_string("registration_text", nil) |
| 16 local oob = module:get_option_boolean("registration_oob", true); | 16 local oob = module:get_option_boolean("registration_oob", true) |
| 17 local admins_g = cman.get("*", "core", "admins"); | 17 local admins_g = cman.get("*", "core", "admins") |
| 18 local admins_l = cman.get(module:get_host(), "core", "admins"); | 18 local admins_l = cman.get(module:get_host(), "core", "admins") |
| 19 local no_wl = module:get_option_boolean("no_registration_whitelist", false); | 19 local no_wl = module:get_option_boolean("no_registration_whitelist", false) |
| 20 local test_ip = false; | 20 local test_ip = false |
| 21 | 21 |
| 22 if type(admins_g) ~= "table" then admins_g = nil; end | 22 if type(admins_g) ~= "table" then admins_g = nil end |
| 23 if type(admins_l) ~= "table" then admins_l = nil; end | 23 if type(admins_l) ~= "table" then admins_l = nil end |
| 24 | 24 |
| 25 -- perform checks to set default responses and sanity checks. | 25 -- perform checks to set default responses and sanity checks. |
| 26 if not inst_text then | 26 if not inst_text then |
| 27 if url and oob then | 27 if url and oob then |
| 28 if url:match("^%w+[:].*$") then | 28 if url:match("^%w+[:].*$") then |
| 34 inst_text = "Please contact "..module:get_host().."'s server administrator via xmpp to register an account on this server at: "..url:match("^%w+[:](.*)$") | 34 inst_text = "Please contact "..module:get_host().."'s server administrator via xmpp to register an account on this server at: "..url:match("^%w+[:](.*)$") |
| 35 else | 35 else |
| 36 module:log("error", "This module supports only http/https, mailto or xmpp as URL formats.") | 36 module:log("error", "This module supports only http/https, mailto or xmpp as URL formats.") |
| 37 module:log("error", "If you want to use personalized instructions without an Out-Of-Band method,") | 37 module:log("error", "If you want to use personalized instructions without an Out-Of-Band method,") |
| 38 module:log("error", "specify: register_oob = false; -- in your configuration along your banner string (register_text).") | 38 module:log("error", "specify: register_oob = false; -- in your configuration along your banner string (register_text).") |
| 39 origin.send(st.error_reply(stanza, "wait", "internal-server-error")); return true -- bouncing request. | 39 origin.send(st.error_reply(stanza, "wait", "internal-server-error")) ; return true -- bouncing request. |
| 40 end | 40 end |
| 41 else | 41 else |
| 42 module:log("error", "Please check your configuration, the URL you specified is invalid") | 42 module:log("error", "Please check your configuration, the URL you specified is invalid") |
| 43 origin.send(st.error_reply(stanza, "wait", "internal-server-error")); return true -- bouncing request. | 43 origin.send(st.error_reply(stanza, "wait", "internal-server-error")) ; return true -- bouncing request. |
| 44 end | 44 end |
| 45 else | 45 else |
| 46 if admins_l then | 46 if admins_l then |
| 47 local ajid; for _,v in ipairs(admins_l) do ajid = v; break; end | 47 local ajid; for _,v in ipairs(admins_l) do ajid = v ; break end |
| 48 inst_text = "Please contact "..module:get_host().."'s server administrator via xmpp to register an account on this server at: "..ajid | 48 inst_text = "Please contact "..module:get_host().."'s server administrator via xmpp to register an account on this server at: "..ajid |
| 49 else | 49 else |
| 50 if admins_g then | 50 if admins_g then |
| 51 local ajid; for _,v in ipairs(admins_g) do ajid = v; break; end | 51 local ajid; for _,v in ipairs(admins_g) do ajid = v ; break end |
| 52 inst_text = "Please contact "..module:get_host().."'s server administrator via xmpp to register an account on this server at: "..ajid | 52 inst_text = "Please contact "..module:get_host().."'s server administrator via xmpp to register an account on this server at: "..ajid |
| 53 else | 53 else |
| 54 module:log("error", "Please be sure to, _at the very least_, configure one server administrator either global or hostwise...") | 54 module:log("error", "Please be sure to, _at the very least_, configure one server administrator either global or hostwise...") |
| 55 module:log("error", "if you want to use this module, or read it's configuration wiki at: http://code.google.com/p/prosody-modules/wiki/mod_register_redirect") | 55 module:log("error", "if you want to use this module, or read it's configuration wiki at: http://code.google.com/p/prosody-modules/wiki/mod_register_redirect") |
| 56 origin.send(st.error_reply(stanza, "wait", "internal-server-error")) -- bouncing request. | 56 origin.send(st.error_reply(stanza, "wait", "internal-server-error")) -- bouncing request. |
| 57 return true; | 57 return true |
| 58 end | 58 end |
| 59 end | 59 end |
| 60 end | 60 end |
| 61 elseif text and oob then | 61 elseif text and oob then |
| 62 if not url:match("^%w+[:].*$") then | 62 if not url:match("^%w+[:].*$") then |
| 65 end | 65 end |
| 66 end | 66 end |
| 67 | 67 |
| 68 if not no_wl then | 68 if not no_wl then |
| 69 for i,ip in ipairs(ip_wl) do | 69 for i,ip in ipairs(ip_wl) do |
| 70 if origin.ip == ip then test_ip = true; end | 70 if origin.ip == ip then test_ip = true end |
| 71 break; | 71 break |
| 72 end | 72 end |
| 73 end | 73 end |
| 74 | 74 |
| 75 -- Prepare replies. | 75 -- Prepare replies. |
| 76 local reply = st.reply(event.stanza); | 76 local reply = st.reply(event.stanza) |
| 77 if oob then | 77 if oob then |
| 78 reply:query("jabber:iq:register") | 78 reply:query("jabber:iq:register") |
| 79 :tag("instructions"):text(inst_text):up() | 79 :tag("instructions"):text(inst_text):up() |
| 80 :tag("x", {xmlns = "jabber:x:oob"}) | 80 :tag("x", {xmlns = "jabber:x:oob"}) |
| 81 :tag("url"):text(url); | 81 :tag("url"):text(url); |
| 83 reply:query("jabber:iq:register") | 83 reply:query("jabber:iq:register") |
| 84 :tag("instructions"):text(inst_text):up() | 84 :tag("instructions"):text(inst_text):up() |
| 85 end | 85 end |
| 86 | 86 |
| 87 if stanza.attr.type == "get" then | 87 if stanza.attr.type == "get" then |
| 88 origin.send(reply); | 88 origin.send(reply) |
| 89 return true; | 89 return true |
| 90 else | 90 else |
| 91 origin.send(st.error_reply(stanza, "cancel", "not-authorized")) | 91 origin.send(st.error_reply(stanza, "cancel", "not-authorized")) |
| 92 return true | 92 return true |
| 93 end | 93 end |
| 94 end | 94 end |
| 95 | 95 |
| 96 module:hook("stanza/iq/jabber:iq:register:query", reg_redirect, 10); | 96 module:hook("stanza/iq/jabber:iq:register:query", reg_redirect, 10) |
