Mercurial > prosody-modules
annotate mod_welcome_page/mod_welcome_page.lua @ 4530:33c149d0261d
mod_rest: Add mappings for a whole pile of XEPs
Look ma, programming in JSON!
XEP-0012: Last Activity
XEP-0077: In-Band Registration
XEP-0115: Entity Capabilities
XEP-0153: vCard-Based Avatars
XEP-0297: Stanza Forwarding
XEP-0308: Last Message Correction
XEP-0319: Last User Interaction in Presence
XEP-0333: Chat Markers
XEP-0367: Message Attaching
XEP-0372: References
XEP-0421: Anonymous unique occupant identifiers for MUCs
XEP-0428: Fallback Indication
XEP-0444: Message Reactions
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Tue, 23 Mar 2021 23:18:33 +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 }); |