mod_invite/mod_invite.lua
author Kim Alvefur <zash@zash.se>
Tue, 14 Mar 2017 04:19:06 +0100
changeset 2617 2103135f1851
parent 2244 967ba25edf58
child 2618 a6e97031972c
permissions -rw-r--r--
mod_invite: Remove unused inport [luacheck]
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
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
    15
local serve = module:depends"http_files".serve;
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
    16
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
    17
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
    18
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
    19
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
    20
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
    21
	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
    22
		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
    23
			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
    24
				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
    25
			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
    26
				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
    27
			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
    28
		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
    29
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
    30
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can 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
function generate_page(event, display_options)
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can 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
	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
    33
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can 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
	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
    35
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can 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
	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
    37
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can 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
	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
    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
	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
    41
		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
    42
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can 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
		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
    44
	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
    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
	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
    47
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can 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
	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
    49
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
    50
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can 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
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
    52
	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
    53
	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
    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
	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
    56
	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
    57
	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
    58
	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
    59
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
    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
function handle_form(event, display_options)
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can 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 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
    63
	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
    64
	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
    65
	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
    66
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can 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
	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
    68
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can 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
	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
    70
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can 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
	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
    72
		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
    73
	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
    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
	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
    76
		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
    77
	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
    78
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can 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
	-- 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
    80
	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
    81
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can 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
	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
    83
		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
    84
	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
    85
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can 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
	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
    87
		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
    88
	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
    89
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can 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
	local registering = { username = prepped_username , host = module.host, allowed = true }
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can 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
	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
    93
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can 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
	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
    95
		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
    96
	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
    97
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can 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
	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
    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 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
   101
		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
   102
		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
   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
		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
   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
		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
   107
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can 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
		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
   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
		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
   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
		return apply_template(template, { classes = "alert-success", message = "Your account has been created! You can now log in using an XMPP client." })
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can 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
	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
   114
		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
   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
		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
   117
	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
   118
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
   119
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can 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
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
   121
	route = {
2244
967ba25edf58 mod_invite: Serve CSS correctly
Kim Alvefur <zash@zash.se>
parents: 2243
diff changeset
   122
		["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
   123
		["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
   124
		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
   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
});
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can 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
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can 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
function invite_command_handler(self, data, state)
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can 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
	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
   130
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can 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
	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
   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
	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
   134
		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
   135
	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
   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
	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
   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
	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
   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
	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
   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
	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
   144
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
   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
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
   147
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can 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
module:add_item("adhoc", adhoc_invite);