Software /
code /
prosody-modules
Comparison
mod_invites_webgen/mod_invites_webgen.lua @ 5943:05125c29fd67
mod_invites_webgen: initial release
author | Trần H. Trung <xmpp:trần.h.trung@trung.fun> |
---|---|
date | Wed, 26 Feb 2025 17:36:14 +0700 |
child | 5944:d073ada49a86 |
comparison
equal
deleted
inserted
replaced
5942:abd1bbe5006e | 5943:05125c29fd67 |
---|---|
1 local http_formdecode = require "net.http".formdecode; | |
2 local usermanager = require "core.usermanager"; | |
3 local nodeprep = require "util.encodings".stringprep.nodeprep; | |
4 local st = require "util.stanza"; | |
5 local url_escape = require "util.http".urlencode; | |
6 local render_html_template = require "util.interpolation".new("%b{}", st.xml_escape, { | |
7 urlescape = url_escape; | |
8 }); | |
9 | |
10 local site_name = module:get_option_string("site_name", module.host); | |
11 | |
12 local invites = module:depends("invites"); | |
13 --local template_get = module:depends("invites_page").template_get; | |
14 | |
15 local templatePath = module:get_option_string("invites_template_html", "html"); | |
16 function template_get(filename, lang) | |
17 local template = lang and templatePath.."/"..filename.."."..lang..".html" | |
18 or templatePath.."/"..filename..".html"; | |
19 return assert(module:load_resource(template):read("*a")); | |
20 end | |
21 | |
22 | |
23 module:depends("http"); | |
24 | |
25 local default_path, lang; | |
26 | |
27 local function serve_webgen(event) | |
28 local query_params = event.request.url.query and http_formdecode(event.request.url.query); | |
29 lang = query_params and query_params.l; | |
30 | |
31 local webgen_template = template_get("webgen", lang); | |
32 event.response.headers["Content-Type"] = "text/html; charset=utf-8"; | |
33 | |
34 local webgen = render_html_template(webgen_template, { | |
35 site_name = site_name; | |
36 }); | |
37 return webgen; | |
38 end | |
39 | |
40 module:depends("invites_register_web"); | |
41 | |
42 local function handle_webgen(event) | |
43 local request, response = event.request, event.response; | |
44 local form_data = http_formdecode(request.body); | |
45 local username, password = form_data["username"], form_data["password"]; | |
46 | |
47 event.response.headers["Content-Type"] = "text/html; charset=utf-8"; | |
48 | |
49 local webgen_template = template_get("webgen", lang); | |
50 local htmlPath = module:http_url("webgen", "/webgen"); | |
51 | |
52 if not username or #username == 0 or not password or #password == 0 then | |
53 return render_html_template(webgen_template, { | |
54 site_name = site_name; | |
55 path = htmlPath; | |
56 msg_class = "alert-warning"; | |
57 message = "Please fill in all fields."; | |
58 }); | |
59 end | |
60 | |
61 local prepped_username = nodeprep(username); | |
62 local exists = usermanager.user_exists(prepped_username, module.host); | |
63 local pass = usermanager.test_password(prepped_username, module.host, password); | |
64 local allow_user = module:get_option_boolean("allow_user_invites", false); | |
65 local allow_contact = module:get_option_boolean("allow_contact_invites", false); | |
66 | |
67 local invite; | |
68 if exists and pass and allow_user and allow_contact then | |
69 invite = invites.create_contact(username, true, { source = "webgen" }); | |
70 else | |
71 return render_html_template(webgen_template, { | |
72 site_name = site_name; | |
73 path = htmlPath; | |
74 username = username; | |
75 msg_class = "alert-warning"; | |
76 message = "Invalid input!"; | |
77 }); | |
78 end | |
79 | |
80 if not invite then | |
81 return 500; | |
82 else | |
83 event.response.headers.Location = invite.landing_page or invite.uri; | |
84 return 303; | |
85 end | |
86 end | |
87 | |
88 module:provides("http", { | |
89 default_path = "webgen"; | |
90 route = { | |
91 ["GET"] = serve_webgen; | |
92 ["POST"] = handle_webgen; | |
93 }; | |
94 }); |