# HG changeset patch # User Kim Alvefur # Date 1385834274 -3600 # Node ID 6015434f0e05819e08dc0d3b250adaab3b28efe9 # Parent 0667624637daf46ee74ae442a80f78847a1a08a4 mod_register_web: Move HTML into separate template files diff -r 0667624637da -r 6015434f0e05 mod_register_web/mod_register_web.lua --- a/mod_register_web/mod_register_web.lua Sat Nov 30 18:25:32 2013 +0100 +++ b/mod_register_web/mod_register_web.lua Sat Nov 30 18:57:54 2013 +0100 @@ -3,28 +3,36 @@ local usermanager = require "core.usermanager"; local http = require "util.http"; +function template(data) + -- Like util.template, but deals with plain text + return { apply = function(values) return (data:gsub("{([^}]+)}", values)); end } +end + +local function get_template(name) + local fh = assert(module:load_resource("templates/"..name..".html")); + local data = assert(fh:read("*a")); + fh:close(); + return template(data); +end + +local function render(template, data) + return tostring(template.apply(data)); +end + +local register_tpl = get_template "register"; +local success_tpl = get_template "success"; +local recaptcha_tpl = get_template "recaptcha"; + function generate_captcha(display_options) - return (([[ - - - ]]):gsub("$$([^$]+)$%$", setmetatable({ + return recaptcha_tpl.apply(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 + })); end function verify_captcha(form, callback) http.request("https://www.google.com/recaptcha/api/verify", { @@ -46,27 +54,12 @@ function generate_page(event, display_options) local request = event.request; - return [[ - -

XMPP Account Registration

-
]] - ..("

%s

\n"):format((display_options or {}).register_error or "").. - [[ - - - - - - - - - - - -
Username:@]]..module.host..[[
Password:
]]..generate_captcha(display_options)..[[
- -
- ]]; + + return render(register_tpl, { + path = request.path; hostname = module.host; + notice = display_options and display_options.register_error or ""; + captcha = generate_captcha(display_options); + }) end function register_user(form) @@ -78,10 +71,7 @@ end function generate_success(event, form) - return [[ -

Registration succeeded! Your account is

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

]]; + return render(success_tpl, { jid = nodeprep(form.username).."@"..module.host }); end function generate_register_response(event, form, ok, err) diff -r 0667624637da -r 6015434f0e05 mod_register_web/templates/recaptcha.html --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mod_register_web/templates/recaptcha.html Sat Nov 30 18:57:54 2013 +0100 @@ -0,0 +1,11 @@ + + + + + + diff -r 0667624637da -r 6015434f0e05 mod_register_web/templates/register.html --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mod_register_web/templates/register.html Sat Nov 30 18:57:54 2013 +0100 @@ -0,0 +1,29 @@ + + + + + XMPP Account Registration + + +

XMPP Account Registration

+
+

{notice}

+ + + + + + + + + + + {captcha} + + + + +
Username:@{hostname}
Password:
+
+ + diff -r 0667624637da -r 6015434f0e05 mod_register_web/templates/success.html --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mod_register_web/templates/success.html Sat Nov 30 18:57:54 2013 +0100 @@ -0,0 +1,13 @@ + + + + + Registration succeeded! + + +

Registration succeeded!

+

Your account is

+
{jid}
+

- happy chatting!

+ +