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