comparison mod_register_web/mod_register_web.lua @ 1227:6015434f0e05

mod_register_web: Move HTML into separate template files
author Kim Alvefur <zash@zash.se>
date Sat, 30 Nov 2013 18:57:54 +0100
parents 0667624637da
children db85ff22ae97
comparison
equal deleted inserted replaced
1226:0667624637da 1227:6015434f0e05
1 local captcha_options = module:get_option("captcha_options", {}); 1 local captcha_options = module:get_option("captcha_options", {});
2 local nodeprep = require "util.encodings".stringprep.nodeprep; 2 local nodeprep = require "util.encodings".stringprep.nodeprep;
3 local usermanager = require "core.usermanager"; 3 local usermanager = require "core.usermanager";
4 local http = require "util.http"; 4 local http = require "util.http";
5 5
6 function template(data)
7 -- Like util.template, but deals with plain text
8 return { apply = function(values) return (data:gsub("{([^}]+)}", values)); end }
9 end
10
11 local function get_template(name)
12 local fh = assert(module:load_resource("templates/"..name..".html"));
13 local data = assert(fh:read("*a"));
14 fh:close();
15 return template(data);
16 end
17
18 local function render(template, data)
19 return tostring(template.apply(data));
20 end
21
22 local register_tpl = get_template "register";
23 local success_tpl = get_template "success";
24 local recaptcha_tpl = get_template "recaptcha";
25
6 function generate_captcha(display_options) 26 function generate_captcha(display_options)
7 return (([[ 27 return recaptcha_tpl.apply(setmetatable({
8 <script type="text/javascript"
9 src="https://www.google.com/recaptcha/api/challenge?k=$$recaptcha_public_key$$">
10 </script>
11 <noscript>
12 <iframe src="https://www.google.com/recaptcha/api/noscript?k=$$recaptcha_public_key$$$$recaptcha_display_error$$"
13 height="300" width="500" frameborder="0"></iframe><br>
14 <textarea name="recaptcha_challenge_field" rows="3" cols="40">
15 </textarea>
16 <input type="hidden" name="recaptcha_response_field"
17 value="manual_challenge">
18 </noscript>
19 ]]):gsub("$$([^$]+)$%$", setmetatable({
20 recaptcha_display_error = display_options and display_options.recaptcha_error 28 recaptcha_display_error = display_options and display_options.recaptcha_error
21 and ("&error="..display_options.recaptcha_error) or ""; 29 and ("&error="..display_options.recaptcha_error) or "";
22 }, { 30 }, {
23 __index = function (t, k) 31 __index = function (t, k)
24 if captcha_options[k] then return captcha_options[k]; end 32 if captcha_options[k] then return captcha_options[k]; end
25 module:log("error", "Missing parameter from captcha_options: %s", k); 33 module:log("error", "Missing parameter from captcha_options: %s", k);
26 end }) 34 end
27 )); 35 }));
28 end 36 end
29 function verify_captcha(form, callback) 37 function verify_captcha(form, callback)
30 http.request("https://www.google.com/recaptcha/api/verify", { 38 http.request("https://www.google.com/recaptcha/api/verify", {
31 body = http.formencode { 39 body = http.formencode {
32 privatekey = captcha_options.recaptcha_private_key; 40 privatekey = captcha_options.recaptcha_private_key;
44 end); 52 end);
45 end 53 end
46 54
47 function generate_page(event, display_options) 55 function generate_page(event, display_options)
48 local request = event.request; 56 local request = event.request;
49 return [[<!DOCTYPE html> 57
50 <html><body> 58 return render(register_tpl, {
51 <h1>XMPP Account Registration</h1> 59 path = request.path; hostname = module.host;
52 <form action="]]..request.path..[[" method="POST">]] 60 notice = display_options and display_options.register_error or "";
53 ..("<p>%s</p>\n"):format((display_options or {}).register_error or "").. 61 captcha = generate_captcha(display_options);
54 [[ <table> 62 })
55 <tr>
56 <td>Username:</td>
57 <td><input type="text" name="username">@]]..module.host..[[</td>
58 </tr>
59 <tr>
60 <td>Password:</td>
61 <td><input type="password" name="password"></td>
62 </tr>
63 <tr>
64 <td colspan='2'>]]..generate_captcha(display_options)..[[</td>
65 </tr>
66 </table>
67 <input type="submit" value="Register!">
68 </form>
69 </body></html>]];
70 end 63 end
71 64
72 function register_user(form) 65 function register_user(form)
73 local prepped_username = nodeprep(form.username); 66 local prepped_username = nodeprep(form.username);
74 if usermanager.user_exists(prepped_username, module.host) then 67 if usermanager.user_exists(prepped_username, module.host) then
76 end 69 end
77 return usermanager.create_user(prepped_username, form.password, module.host); 70 return usermanager.create_user(prepped_username, form.password, module.host);
78 end 71 end
79 72
80 function generate_success(event, form) 73 function generate_success(event, form)
81 return [[<!DOCTYPE html> 74 return render(success_tpl, { jid = nodeprep(form.username).."@"..module.host });
82 <html><body><p>Registration succeeded! Your account is <pre>]]
83 ..form.username.."@"..module.host..
84 [[</pre> - happy chatting!</p></body></html>]];
85 end 75 end
86 76
87 function generate_register_response(event, form, ok, err) 77 function generate_register_response(event, form, ok, err)
88 local message; 78 local message;
89 if ok then 79 if ok then