mod_invite/mod_invite.lua
author Matthew Wild <mwild1@gmail.com>
Sat, 24 Sep 2022 09:25:46 +0100
changeset 5062 39c2824c2880
parent 4980 75b6e5df65f9
permissions -rw-r--r--
mod_cloud_notify: README overhaul
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2062
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";
2242
dc4e77824318 mod_invite: Use XML/HTML entity escaping from util.stanza
Kim Alvefur <zash@zash.se>
parents: 2062
diff changeset
     8
local tohtml = require "util.stanza".xml_escape
2062
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
3553
b059a3fb2a58 Update modules using mod_http_files to serve files for change in Prosody trunk
Kim Alvefur <zash@zash.se>
parents: 3484
diff changeset
    15
local serve;
4980
75b6e5df65f9 various: Improve error reporting if missing file server module on 0.12
Kim Alvefur <zash@zash.se>
parents: 3783
diff changeset
    16
if prosody.process_type == "prosody" then
3553
b059a3fb2a58 Update modules using mod_http_files to serve files for change in Prosody trunk
Kim Alvefur <zash@zash.se>
parents: 3484
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: 3484
diff changeset
    18
	serve = http_files.serve;
4980
75b6e5df65f9 various: Improve error reporting if missing file server module on 0.12
Kim Alvefur <zash@zash.se>
parents: 3783
diff changeset
    19
else
3553
b059a3fb2a58 Update modules using mod_http_files to serve files for change in Prosody trunk
Kim Alvefur <zash@zash.se>
parents: 3484
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: 3484
diff changeset
    21
end
2062
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
2243
df696c8d5282 mod_invite: Add explicit dependency on mod_adhoc to make sure it is loaded
Kim Alvefur <zash@zash.se>
parents: 2242
diff changeset
    23
module:depends"adhoc";
2062
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
3556
5370bc374c83 mod_invite: Use path passed by http stack instead of pattern matching
Kim Alvefur <zash@zash.se>
parents: 3553
diff changeset
    37
function generate_page(event, token)
3557
5b15ce870a5b mod_invite: Remove unused local variable [luacheck]
Kim Alvefur <zash@zash.se>
parents: 3556
diff changeset
    38
	local response = event.response;
2062
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
	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
    43
3558
4e99005cf917 mod_invite: Show a different error if no token has been given
Kim Alvefur <zash@zash.se>
parents: 3557
diff changeset
    44
	if not token or token == "" then
4e99005cf917 mod_invite: Show a different error if no token has been given
Kim Alvefur <zash@zash.se>
parents: 3557
diff changeset
    45
		local template = assert(module:load_resource("invite/invite_result.html")):read("*a");
4e99005cf917 mod_invite: Show a different error if no token has been given
Kim Alvefur <zash@zash.se>
parents: 3557
diff changeset
    46
4e99005cf917 mod_invite: Show a different error if no token has been given
Kim Alvefur <zash@zash.se>
parents: 3557
diff changeset
    47
		-- TODO maybe show a friendlier information page instead?
4e99005cf917 mod_invite: Show a different error if no token has been given
Kim Alvefur <zash@zash.se>
parents: 3557
diff changeset
    48
		return apply_template(template, { classes = "alert-danger", message = "No token given" })
4e99005cf917 mod_invite: Show a different error if no token has been given
Kim Alvefur <zash@zash.se>
parents: 3557
diff changeset
    49
	elseif not tokens[token] then
2062
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
		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
    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
		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
    53
	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
    54
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
	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
    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
	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
    58
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
    59
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
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
    61
	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
    62
	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
    63
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.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
    65
	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
    66
	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
    67
	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
    68
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
    69
2618
a6e97031972c mod_invite: Remove unused arguments [luacheck]
Kim Alvefur <zash@zash.se>
parents: 2617
diff changeset
    70
function handle_form(event)
2062
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 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
    72
	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
    73
	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
    74
	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
    75
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
	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
    77
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
	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
    79
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
	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
    81
		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
    82
	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
    83
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
	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
    85
		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
    86
	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
    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
	-- 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
    89
	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
    90
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
	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
    92
		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
    93
	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
    94
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
	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
    96
		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
    97
	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
    98
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
	local registering = { username = prepped_username , host = module.host, allowed = true }
2619
efb69a260723 mod_invite: Trim trailing whitespace [luacheck]
Kim Alvefur <zash@zash.se>
parents: 2618
diff changeset
   100
2062
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
	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
   102
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
	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
   104
		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
   105
	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
   106
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
	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
   108
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
	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
   110
		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
   111
		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
   112
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
		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
   114
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
		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
   116
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
		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
   118
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
   119
		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
   120
3484
5911030a1a66 mod_invite: Fire event that signals that an user has registered (thanks jeybe)
Kim Alvefur <zash@zash.se>
parents: 2620
diff changeset
   121
		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: 2620
diff changeset
   122
			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: 2620
diff changeset
   123
2620
c814c667d4d1 mod_invite: Split long line [luacheck]
Kim Alvefur <zash@zash.se>
parents: 2619
diff changeset
   124
		return apply_template(template, { classes = "alert-success",
c814c667d4d1 mod_invite: Split long line [luacheck]
Kim Alvefur <zash@zash.se>
parents: 2619
diff changeset
   125
			message = "Your account has been created! You can now log in using an XMPP client." })
2062
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
	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
   127
		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
   128
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
		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
   130
	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
   131
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
   132
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
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
   134
	route = {
2244
967ba25edf58 mod_invite: Serve CSS correctly
Kim Alvefur <zash@zash.se>
parents: 2243
diff changeset
   135
		["GET /bootstrap.min.css"] = serve(module:get_directory() .. "/invite/bootstrap.min.css");
2062
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
		["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
   137
		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
   138
	};
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
});
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
2618
a6e97031972c mod_invite: Remove unused arguments [luacheck]
Kim Alvefur <zash@zash.se>
parents: 2617
diff changeset
   141
function invite_command_handler(_, data)
2062
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
	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
   143
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
	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
   145
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
	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
   147
		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
   148
	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
   149
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
	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
   151
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
	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
   153
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
	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
   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
	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
   157
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
   158
3783
5e69a4358053 mod_invite: Update to use local_user ad-hoc permission (thanks Martin)
Matthew Wild <mwild1@gmail.com>
parents: 3558
diff changeset
   159
local adhoc_invite = adhoc_new("Invite user", "invite", invite_command_handler, "local_user")
2062
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
   160
3783
5e69a4358053 mod_invite: Update to use local_user ad-hoc permission (thanks Martin)
Matthew Wild <mwild1@gmail.com>
parents: 3558
diff changeset
   161
module:add_item("adhoc", adhoc_invite);