local captcha_options = module:get_option("captcha_options", {}); local nodeprep = require "util.encodings".stringprep.nodeprep; function generate_captcha(display_options) return (([[ ]]):gsub("$$([^$]+)$%$", setmetatable({ recaptcha_display_error = display_options and display_options.recaptcha_error and ("&error="..display_options.recaptcha_error) or ""; }, { __index = function (t, k) if captcha_options[k] then return captcha_options[k]; end module:log("error", "Missing parameter from captcha_options: %s", k); end }) )); end function generate_page(event, display_options) local request = event.request; return [[

XMPP Account Registration

]] ..("

%s

\n"):format((display_options or {}).register_error or "").. [[
Username: @server
Password:
]]..generate_captcha(display_options)..[[
]]; end function register_user(form) local prepped_username = nodeprep(form.username); if usermanager.user_exists(prepped_username, module.host) then return nil, "user-exists"; end return usermanager.create_user(prepped_username, form.password, module.host); end function generate_success(event, form) return [[

Registration succeeded! Your account is

]]
		..form.username.."@"..module.host..
	[[
- happy chatting!

]]; end function generate_register_response(event, form, ok, err) local message; if ok then return generate_success(event, form); else return generate_page(event, { register_error = err }); end end function handle_form(event) local request, response = event.request, event.response; local form = http.formdecode(request.body); http.request("http://www.google.com/recaptcha/api/verify", { body = http.formencode { privatekey = captcha_options.recaptcha_private_key; remoteip = request.conn:ip(); challenge = form.recaptcha_challenge_field; response = form.recaptcha_response_field; }; }, function (verify_result, code) local verify_ok, verify_err = verify_result:match("^([^\n]+)\n([^\n]+)"); if verify_ok == "true" then local register_ok, register_err = register_user(form); response:send(generate_register_response(event, form, register_ok, register_err)); else response:send(generate_page(event, { register_error = verify_err })); end end); return true; -- Leave connection open until we respond above end module:provides("http", { route = { GET = generate_page; POST = handle_form; }; });