comparison mod_register_redirect/mod_register_redirect.lua @ 440:9a71493368de

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