annotate mod_invite/mod_invite.lua @ 3549:b059a3fb2a58

Update modules using mod_http_files to serve files for change in Prosody trunk See commits in Prosody: f1594893998f afc48785f738 a39f3681d685 c93fdec624c7
author Kim Alvefur <zash@zash.se>
date Fri, 05 Apr 2019 18:57:51 +0200
parents 5911030a1a66
children 5370bc374c83
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2058
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
1 local adhoc_new = module:require "adhoc".new;
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
2 local uuid_new = require "util.uuid".generate;
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
3 local jid_split = require "util.jid".split;
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
4 local jid_join = require "util.jid".join;
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
5 local http_formdecode = require "net.http".formdecode;
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
6 local usermanager = require "core.usermanager";
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
7 local rostermanager = require "core.rostermanager";
2238
dc4e77824318 mod_invite: Use XML/HTML entity escaping from util.stanza
Kim Alvefur <zash@zash.se>
parents: 2058
diff changeset
8 local tohtml = require "util.stanza".xml_escape
2058
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
9 local nodeprep = require "util.encodings".stringprep.nodeprep;
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
10 local tostring = tostring;
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
11
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
12 local invite_storage = module:open_store();
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
13 local inviter_storage = module:open_store("inviter");
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
14
3549
b059a3fb2a58 Update modules using mod_http_files to serve files for change in Prosody trunk
Kim Alvefur <zash@zash.se>
parents: 3480
diff changeset
15 local serve;
b059a3fb2a58 Update modules using mod_http_files to serve files for change in Prosody trunk
Kim Alvefur <zash@zash.se>
parents: 3480
diff changeset
16 if not pcall(function ()
b059a3fb2a58 Update modules using mod_http_files to serve files for change in Prosody trunk
Kim Alvefur <zash@zash.se>
parents: 3480
diff changeset
17 local http_files = require "net.http.files";
b059a3fb2a58 Update modules using mod_http_files to serve files for change in Prosody trunk
Kim Alvefur <zash@zash.se>
parents: 3480
diff changeset
18 serve = http_files.serve;
b059a3fb2a58 Update modules using mod_http_files to serve files for change in Prosody trunk
Kim Alvefur <zash@zash.se>
parents: 3480
diff changeset
19 end) then
b059a3fb2a58 Update modules using mod_http_files to serve files for change in Prosody trunk
Kim Alvefur <zash@zash.se>
parents: 3480
diff changeset
20 serve = module:depends"http_files".serve;
b059a3fb2a58 Update modules using mod_http_files to serve files for change in Prosody trunk
Kim Alvefur <zash@zash.se>
parents: 3480
diff changeset
21 end
2058
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
22
2239
df696c8d5282 mod_invite: Add explicit dependency on mod_adhoc to make sure it is loaded
Kim Alvefur <zash@zash.se>
parents: 2238
diff changeset
23 module:depends"adhoc";
2058
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
24 module:depends"http";
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
25
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
26 local function apply_template(template, args)
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
27 return
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
28 template:gsub("{{([^}]*)}}", function (k)
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
29 if args[k] then
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
30 return tohtml(args[k])
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
31 else
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
32 return k
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
33 end
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
34 end)
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
35 end
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
36
2614
a6e97031972c mod_invite: Remove unused arguments [luacheck]
Kim Alvefur <zash@zash.se>
parents: 2613
diff changeset
37 function generate_page(event)
2058
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
38 local request, response = event.request, event.response;
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
39
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
40 local tokens = invite_storage:get() or {};
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
41
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
42 local token = request.path:match("^/invite/([^/]*)$");
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
43
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
44 response.headers.content_type = "text/html; charset=utf-8";
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
45
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
46 if not token or not tokens[token] then
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
47 local template = assert(module:load_resource("invite/invite_result.html")):read("*a");
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
48
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
49 return apply_template(template, { classes = "alert-danger", message = "This invite has expired." })
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
50 end
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
51
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
52 local template = assert(module:load_resource("invite/invite.html")):read("*a");
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
53
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
54 return apply_template(template, { user = jid_join(tokens[token], module.host), server = module.host, token = token });
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
55 end
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
56
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
57 function subscribe(user1, user2)
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
58 local user1_jid = jid_join(user1, module.host);
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
59 local user2_jid = jid_join(user2, module.host);
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
60
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
61 rostermanager.set_contact_pending_out(user2, module.host, user1_jid);
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
62 rostermanager.set_contact_pending_in(user1, module.host, user2_jid);
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
63 rostermanager.subscribed(user1, module.host, user2_jid);
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
64 rostermanager.process_inbound_subscription_approval(user2, module.host, user1_jid);
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
65 end
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
66
2614
a6e97031972c mod_invite: Remove unused arguments [luacheck]
Kim Alvefur <zash@zash.se>
parents: 2613
diff changeset
67 function handle_form(event)
2058
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
68 local request, response = event.request, event.response;
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
69 local form_data = http_formdecode(request.body);
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
70 local user, password, token = form_data["user"], form_data["password"], form_data["token"];
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
71 local tokens = invite_storage:get() or {};
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
72
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
73 local template = assert(module:load_resource("invite/invite_result.html")):read("*a");
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
74
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
75 response.headers.content_type = "text/html; charset=utf-8";
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
76
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
77 if not user or #user == 0 or not password or #password == 0 or not token then
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
78 return apply_template(template, { classes = "alert-warning", message = "Please fill in all fields." })
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
79 end
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
80
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
81 if not tokens[token] then
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
82 return apply_template(template, { classes = "alert-danger", message = "This invite has expired." })
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
83 end
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
84
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
85 -- Shamelessly copied from mod_register_web.
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
86 local prepped_username = nodeprep(user);
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
87
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
88 if not prepped_username or #prepped_username == 0 then
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
89 return apply_template(template, { classes = "alert-warning", message = "This username contains invalid characters." })
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
90 end
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
91
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
92 if usermanager.user_exists(prepped_username, module.host) then
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
93 return apply_template(template, { classes = "alert-warning", message = "This username is already in use." })
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
94 end
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
95
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
96 local registering = { username = prepped_username , host = module.host, allowed = true }
2615
efb69a260723 mod_invite: Trim trailing whitespace [luacheck]
Kim Alvefur <zash@zash.se>
parents: 2614
diff changeset
97
2058
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
98 module:fire_event("user-registering", registering);
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
99
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
100 if not registering.allowed then
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
101 return apply_template(template, { classes = "alert-danger", message = "Registration is not allowed." })
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
102 end
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
103
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
104 local ok, err = usermanager.create_user(prepped_username, password, module.host);
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
105
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
106 if ok then
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
107 subscribe(prepped_username, tokens[token]);
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
108 subscribe(tokens[token], prepped_username);
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
109
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
110 inviter_storage:set(prepped_username, { inviter = tokens[token] });
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
111
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
112 rostermanager.roster_push(tokens[token], module.host, jid_join(prepped_username, module.host));
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
113
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
114 tokens[token] = nil;
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
115
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
116 invite_storage:set(nil, tokens);
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
117
3480
5911030a1a66 mod_invite: Fire event that signals that an user has registered (thanks jeybe)
Kim Alvefur <zash@zash.se>
parents: 2616
diff changeset
118 module:fire_event("user-registered", {
5911030a1a66 mod_invite: Fire event that signals that an user has registered (thanks jeybe)
Kim Alvefur <zash@zash.se>
parents: 2616
diff changeset
119 username = prepped_username, host = module.host, source = "mod_invite", });
5911030a1a66 mod_invite: Fire event that signals that an user has registered (thanks jeybe)
Kim Alvefur <zash@zash.se>
parents: 2616
diff changeset
120
2616
c814c667d4d1 mod_invite: Split long line [luacheck]
Kim Alvefur <zash@zash.se>
parents: 2615
diff changeset
121 return apply_template(template, { classes = "alert-success",
c814c667d4d1 mod_invite: Split long line [luacheck]
Kim Alvefur <zash@zash.se>
parents: 2615
diff changeset
122 message = "Your account has been created! You can now log in using an XMPP client." })
2058
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
123 else
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
124 module:log("debug", "Registration failed: " .. tostring(err));
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
125
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
126 return apply_template(template, { classes = "alert-danger", message = "An unknown error has occurred." })
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
127 end
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
128 end
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
129
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
130 module:provides("http", {
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
131 route = {
2240
967ba25edf58 mod_invite: Serve CSS correctly
Kim Alvefur <zash@zash.se>
parents: 2239
diff changeset
132 ["GET /bootstrap.min.css"] = serve(module:get_directory() .. "/invite/bootstrap.min.css");
2058
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
133 ["GET /*"] = generate_page;
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
134 POST = handle_form;
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
135 };
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
136 });
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
137
2614
a6e97031972c mod_invite: Remove unused arguments [luacheck]
Kim Alvefur <zash@zash.se>
parents: 2613
diff changeset
138 function invite_command_handler(_, data)
2058
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
139 local uuid = uuid_new();
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
140
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
141 local user, host = jid_split(data.from);
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
142
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
143 if host ~= module.host then
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
144 return { status = "completed", error = { message = "You are not allowed to invite users to this server." }};
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
145 end
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
146
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
147 local tokens = invite_storage:get() or {};
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
148
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
149 tokens[uuid] = user;
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
150
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
151 invite_storage:set(nil, tokens);
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
152
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
153 return { info = module:http_url() .. "/" .. uuid, status = "completed" };
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
154 end
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
155
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
156 local adhoc_invite = adhoc_new("Invite user", "invite", invite_command_handler, "user")
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
157
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
158 module:add_item("adhoc", adhoc_invite);