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