Mercurial > prosody-modules
annotate mod_welcome_page/mod_welcome_page.lua @ 4478:7ab0c423688a
mod_rest: Support GET for certain IQ queries
Example:
GET /rest/version/example.com
200 OK
{ version: { name: "thing", version: "1.0.0" } }
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sun, 28 Feb 2021 19:33:09 +0100 |
parents | 8d1e996034ee |
children | ffb709728210 |
rev | line source |
---|---|
4184
9127fa98ee1e
mod_welcome_page: New module to provide a friendly entrypoint to invite-based setups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 local st = require "util.stanza"; |
9127fa98ee1e
mod_welcome_page: New module to provide a friendly entrypoint to invite-based setups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
2 local url_escape = require "util.http".urlencode; |
9127fa98ee1e
mod_welcome_page: New module to provide a friendly entrypoint to invite-based setups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 local render_html_template = require"util.interpolation".new("%b{}", st.xml_escape, { |
9127fa98ee1e
mod_welcome_page: New module to provide a friendly entrypoint to invite-based setups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
4 urlescape = url_escape; |
9127fa98ee1e
mod_welcome_page: New module to provide a friendly entrypoint to invite-based setups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 }); |
9127fa98ee1e
mod_welcome_page: New module to provide a friendly entrypoint to invite-based setups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 |
9127fa98ee1e
mod_welcome_page: New module to provide a friendly entrypoint to invite-based setups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 local template_path = module:get_option_string("welcome_page_template_path", module:get_directory().."/html"); |
9127fa98ee1e
mod_welcome_page: New module to provide a friendly entrypoint to invite-based setups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 local user_vars = module:get_option("welcome_page_variables", {}); |
9127fa98ee1e
mod_welcome_page: New module to provide a friendly entrypoint to invite-based setups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 local site_name = module:get_option("site_name", module.host); |
9127fa98ee1e
mod_welcome_page: New module to provide a friendly entrypoint to invite-based setups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 local invite_only = module:get_option_boolean("registration_invite_only", true); |
9127fa98ee1e
mod_welcome_page: New module to provide a friendly entrypoint to invite-based setups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 local open_registration = module:get_option_boolean("welcome_page_open_registration", not invite_only); |
9127fa98ee1e
mod_welcome_page: New module to provide a friendly entrypoint to invite-based setups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 |
9127fa98ee1e
mod_welcome_page: New module to provide a friendly entrypoint to invite-based setups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 module:depends("http"); |
9127fa98ee1e
mod_welcome_page: New module to provide a friendly entrypoint to invite-based setups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 local invites = module:depends("invites"); |
9127fa98ee1e
mod_welcome_page: New module to provide a friendly entrypoint to invite-based setups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 |
9127fa98ee1e
mod_welcome_page: New module to provide a friendly entrypoint to invite-based setups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 local function load_template(path) |
9127fa98ee1e
mod_welcome_page: New module to provide a friendly entrypoint to invite-based setups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 local template_file, err = io.open(path); |
9127fa98ee1e
mod_welcome_page: New module to provide a friendly entrypoint to invite-based setups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 if not template_file then |
9127fa98ee1e
mod_welcome_page: New module to provide a friendly entrypoint to invite-based setups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 error("Unable to load template file: "..tostring(err)); |
9127fa98ee1e
mod_welcome_page: New module to provide a friendly entrypoint to invite-based setups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 end |
9127fa98ee1e
mod_welcome_page: New module to provide a friendly entrypoint to invite-based setups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 local template = template_file:read("*a"); |
9127fa98ee1e
mod_welcome_page: New module to provide a friendly entrypoint to invite-based setups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 template_file:close(); |
9127fa98ee1e
mod_welcome_page: New module to provide a friendly entrypoint to invite-based setups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 return template; |
9127fa98ee1e
mod_welcome_page: New module to provide a friendly entrypoint to invite-based setups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 end |
9127fa98ee1e
mod_welcome_page: New module to provide a friendly entrypoint to invite-based setups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 |
9127fa98ee1e
mod_welcome_page: New module to provide a friendly entrypoint to invite-based setups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 local template = load_template(template_path.."/index.html"); |
9127fa98ee1e
mod_welcome_page: New module to provide a friendly entrypoint to invite-based setups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 |
9127fa98ee1e
mod_welcome_page: New module to provide a friendly entrypoint to invite-based setups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 local function serve_page(event) |
9127fa98ee1e
mod_welcome_page: New module to provide a friendly entrypoint to invite-based setups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 event.response.headers["Content-Type"] = "text/html; charset=utf-8"; |
9127fa98ee1e
mod_welcome_page: New module to provide a friendly entrypoint to invite-based setups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 return render_html_template(template, { |
9127fa98ee1e
mod_welcome_page: New module to provide a friendly entrypoint to invite-based setups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 site_name = site_name; |
9127fa98ee1e
mod_welcome_page: New module to provide a friendly entrypoint to invite-based setups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 request = event.request; |
9127fa98ee1e
mod_welcome_page: New module to provide a friendly entrypoint to invite-based setups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 var = user_vars; |
9127fa98ee1e
mod_welcome_page: New module to provide a friendly entrypoint to invite-based setups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 }); |
9127fa98ee1e
mod_welcome_page: New module to provide a friendly entrypoint to invite-based setups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 end |
9127fa98ee1e
mod_welcome_page: New module to provide a friendly entrypoint to invite-based setups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 |
9127fa98ee1e
mod_welcome_page: New module to provide a friendly entrypoint to invite-based setups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 local function handle_submit(event) |
9127fa98ee1e
mod_welcome_page: New module to provide a friendly entrypoint to invite-based setups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 local submission = { allowed = open_registration, request = event.request }; |
9127fa98ee1e
mod_welcome_page: New module to provide a friendly entrypoint to invite-based setups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 module:fire_event("mod_welcome_page/submission", submission); |
9127fa98ee1e
mod_welcome_page: New module to provide a friendly entrypoint to invite-based setups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 if not submission.allowed then |
9127fa98ee1e
mod_welcome_page: New module to provide a friendly entrypoint to invite-based setups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 event.response.headers["Content-Type"] = "text/html; charset=utf-8"; |
9127fa98ee1e
mod_welcome_page: New module to provide a friendly entrypoint to invite-based setups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 return render_html_template(template, { |
9127fa98ee1e
mod_welcome_page: New module to provide a friendly entrypoint to invite-based setups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 site_name = site_name; |
9127fa98ee1e
mod_welcome_page: New module to provide a friendly entrypoint to invite-based setups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 request = event.request; |
9127fa98ee1e
mod_welcome_page: New module to provide a friendly entrypoint to invite-based setups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 var = user_vars; |
9127fa98ee1e
mod_welcome_page: New module to provide a friendly entrypoint to invite-based setups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 message = { |
9127fa98ee1e
mod_welcome_page: New module to provide a friendly entrypoint to invite-based setups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 class = "alert-danger"; |
9127fa98ee1e
mod_welcome_page: New module to provide a friendly entrypoint to invite-based setups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 text = submission.reason or "Account creation is not possible at this time"; |
9127fa98ee1e
mod_welcome_page: New module to provide a friendly entrypoint to invite-based setups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 }; |
9127fa98ee1e
mod_welcome_page: New module to provide a friendly entrypoint to invite-based setups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 }); |
9127fa98ee1e
mod_welcome_page: New module to provide a friendly entrypoint to invite-based setups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 end |
9127fa98ee1e
mod_welcome_page: New module to provide a friendly entrypoint to invite-based setups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 |
9127fa98ee1e
mod_welcome_page: New module to provide a friendly entrypoint to invite-based setups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 local invite = invites.create_account(nil, { source = module.name }); |
9127fa98ee1e
mod_welcome_page: New module to provide a friendly entrypoint to invite-based setups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 if not invite then |
9127fa98ee1e
mod_welcome_page: New module to provide a friendly entrypoint to invite-based setups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 return 500; |
9127fa98ee1e
mod_welcome_page: New module to provide a friendly entrypoint to invite-based setups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 end |
9127fa98ee1e
mod_welcome_page: New module to provide a friendly entrypoint to invite-based setups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 |
9127fa98ee1e
mod_welcome_page: New module to provide a friendly entrypoint to invite-based setups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 event.response.headers.Location = invite.landing_page or invite.uri; |
9127fa98ee1e
mod_welcome_page: New module to provide a friendly entrypoint to invite-based setups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
59 |
9127fa98ee1e
mod_welcome_page: New module to provide a friendly entrypoint to invite-based setups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
60 return 303; |
9127fa98ee1e
mod_welcome_page: New module to provide a friendly entrypoint to invite-based setups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 end |
9127fa98ee1e
mod_welcome_page: New module to provide a friendly entrypoint to invite-based setups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
62 |
9127fa98ee1e
mod_welcome_page: New module to provide a friendly entrypoint to invite-based setups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 local http_files = module:depends("http_files"); |
9127fa98ee1e
mod_welcome_page: New module to provide a friendly entrypoint to invite-based setups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
64 |
9127fa98ee1e
mod_welcome_page: New module to provide a friendly entrypoint to invite-based setups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
65 module:provides("http", { |
4193
8d1e996034ee
mod_welcome_page: Serve from top level path (/) by default
Kim Alvefur <zash@zash.se>
parents:
4184
diff
changeset
|
66 default_path = "/"; |
4184
9127fa98ee1e
mod_welcome_page: New module to provide a friendly entrypoint to invite-based setups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 route = { |
9127fa98ee1e
mod_welcome_page: New module to provide a friendly entrypoint to invite-based setups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 ["GET"] = serve_page; |
9127fa98ee1e
mod_welcome_page: New module to provide a friendly entrypoint to invite-based setups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
69 ["GET /*"] = http_files.serve({ path = template_path }); |
9127fa98ee1e
mod_welcome_page: New module to provide a friendly entrypoint to invite-based setups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 ["POST"] = handle_submit; |
9127fa98ee1e
mod_welcome_page: New module to provide a friendly entrypoint to invite-based setups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
71 }; |
9127fa98ee1e
mod_welcome_page: New module to provide a friendly entrypoint to invite-based setups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
72 }); |