mod_invites_register/mod_invites_register.lua
author Matthew Wild <mwild1@gmail.com>
Mon, 14 Sep 2020 15:05:19 +0100
changeset 4125 ee8e7f5196b4
parent 4124 d3c7be9e36d9
child 4352 46fb40d7f65a
permissions -rw-r--r--
mod_invites_register: Fix traceback for non-invite registrations
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4110
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     1
local st = require "util.stanza";
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     2
local jid_split = require "util.jid".split;
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     3
local jid_bare = require "util.jid".bare;
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     4
local rostermanager = require "core.rostermanager";
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     5
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     6
local require_encryption = module:get_option_boolean("c2s_require_encryption",
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     7
	module:get_option_boolean("require_encryption", false));
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     8
local invite_only = module:get_option_boolean("registration_invite_only", true);
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     9
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    10
local invites;
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    11
if prosody.shutdown then -- COMPAT hack to detect prosodyctl
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    12
	invites = module:depends("invites");
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    13
end
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    14
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    15
local invite_stream_feature = st.stanza("register", { xmlns = "urn:xmpp:invite" }):up();
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    16
module:hook("stream-features", function(event)
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    17
	local session, features = event.origin, event.features;
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    18
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    19
	-- Advertise to unauthorized clients only.
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    20
	if session.type ~= "c2s_unauthed" or (require_encryption and not session.secure) then
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    21
		return
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    22
	end
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    23
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    24
	features:add_child(invite_stream_feature);
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    25
end);
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    26
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    27
-- XEP-0379: Pre-Authenticated Roster Subscription
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    28
module:hook("presence/bare", function (event)
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    29
	local stanza = event.stanza;
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    30
	if stanza.attr.type ~= "subscribe" then return end
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    31
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    32
	local preauth = stanza:get_child("preauth", "urn:xmpp:pars:0");
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    33
	if not preauth then return end
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    34
	local token = preauth.attr.token;
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    35
	if not token then return end
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    36
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    37
	local username, host = jid_split(stanza.attr.to);
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    38
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    39
	local invite, err = invites.get(token, username);
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    40
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    41
	if not invite then
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    42
		module:log("debug", "Got invalid token, error: %s", err);
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    43
		return;
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    44
	end
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    45
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    46
	local contact = jid_bare(stanza.attr.from);
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    47
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    48
	module:log("debug", "Approving inbound subscription to %s from %s", username, contact);
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    49
	if rostermanager.set_contact_pending_in(username, host, contact, stanza) then
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    50
		if rostermanager.subscribed(username, host, contact) then
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    51
			invite:use();
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    52
			rostermanager.roster_push(username, host, contact);
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    53
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    54
			-- Send back a subscription request (goal is mutual subscription)
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    55
			if not rostermanager.is_user_subscribed(username, host, contact)
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    56
			and not rostermanager.is_contact_pending_out(username, host, contact) then
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    57
				module:log("debug", "Sending automatic subscription request to %s from %s", contact, username);
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    58
				if rostermanager.set_contact_pending_out(username, host, contact) then
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    59
					rostermanager.roster_push(username, host, contact);
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    60
					module:send(st.presence({type = "subscribe", to = contact }));
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    61
				else
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    62
					module:log("warn", "Failed to set contact pending out for %s", username);
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    63
				end
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    64
			end
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    65
		end
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    66
	end
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    67
end, 1);
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    68
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    69
-- Client is submitting a preauth token to allow registration
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    70
module:hook("stanza/iq/urn:xmpp:pars:0:preauth", function(event)
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    71
	local preauth = event.stanza.tags[1];
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    72
	local token = preauth.attr.token;
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    73
	local validated_invite = invites.get(token);
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    74
	if not validated_invite then
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    75
		local reply = st.error_reply(event.stanza, "cancel", "forbidden", "The invite token is invalid or expired");
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    76
		event.origin.send(reply);
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    77
		return true;
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    78
	end
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    79
	event.origin.validated_invite = validated_invite;
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    80
	local reply = st.reply(event.stanza);
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    81
	event.origin.send(reply);
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    82
	return true;
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    83
end);
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    84
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    85
-- Registration attempt - ensure a valid preauth token has been supplied
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    86
module:hook("user-registering", function (event)
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    87
	local validated_invite = event.validated_invite or (event.session and event.session.validated_invite);
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    88
	if invite_only and not validated_invite then
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    89
		event.allowed = false;
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    90
		event.reason = "Registration on this server is through invitation only";
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    91
		return;
4125
ee8e7f5196b4 mod_invites_register: Fix traceback for non-invite registrations
Matthew Wild <mwild1@gmail.com>
parents: 4124
diff changeset
    92
	elseif not validated_invite then
ee8e7f5196b4 mod_invites_register: Fix traceback for non-invite registrations
Matthew Wild <mwild1@gmail.com>
parents: 4124
diff changeset
    93
		-- This registration is not using an invite, but
ee8e7f5196b4 mod_invites_register: Fix traceback for non-invite registrations
Matthew Wild <mwild1@gmail.com>
parents: 4124
diff changeset
    94
		-- the server is not in invite-only mode, so nothing
ee8e7f5196b4 mod_invites_register: Fix traceback for non-invite registrations
Matthew Wild <mwild1@gmail.com>
parents: 4124
diff changeset
    95
		-- for this module to do...
ee8e7f5196b4 mod_invites_register: Fix traceback for non-invite registrations
Matthew Wild <mwild1@gmail.com>
parents: 4124
diff changeset
    96
		return;
4110
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    97
	end
4124
d3c7be9e36d9 mod_invites_register: Fix traceback on registration via other module (thanks franck)
Kim Alvefur <zash@zash.se>
parents: 4110
diff changeset
    98
	if validated_invite and validated_invite.additional_data and validated_invite.additional_data.allow_reset then
4110
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    99
		event.allow_reset = validated_invite.additional_data.allow_reset;
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   100
	end
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   101
end);
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   102
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   103
-- Make a *one-way* subscription. User will see when contact is online,
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   104
-- contact will not see when user is online.
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   105
function subscribe(host, user_username, contact_username)
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   106
	local user_jid = user_username.."@"..host;
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   107
	local contact_jid = contact_username.."@"..host;
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   108
	-- Update user's roster to say subscription request is pending...
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   109
	rostermanager.set_contact_pending_out(user_username, host, contact_jid);
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   110
	-- Update contact's roster to say subscription request is pending...
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   111
	rostermanager.set_contact_pending_in(contact_username, host, user_jid);
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   112
	-- Update contact's roster to say subscription request approved...
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   113
	rostermanager.subscribed(contact_username, host, user_jid);
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   114
	-- Update user's roster to say subscription request approved...
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   115
	rostermanager.process_inbound_subscription_approval(user_username, host, contact_jid);
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   116
end
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   117
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   118
-- Make a mutual subscription between jid1 and jid2. Each JID will see
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   119
-- when the other one is online.
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   120
function subscribe_both(host, user1, user2)
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   121
	subscribe(host, user1, user2);
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   122
	subscribe(host, user2, user1);
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   123
end
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   124
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   125
-- Registration successful, if there was a preauth token, mark it as used
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   126
module:hook("user-registered", function (event)
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   127
	local validated_invite = event.validated_invite or (event.session and event.session.validated_invite);
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   128
	if not validated_invite then
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   129
		return;
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   130
	end
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   131
	local inviter_username = validated_invite.inviter;
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   132
	local contact_username = event.username;
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   133
	validated_invite:use();
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   134
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   135
	if inviter_username then
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   136
		module:log("debug", "Creating mutual subscription between %s and %s", inviter_username, contact_username);
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   137
		subscribe_both(module.host, inviter_username, contact_username);
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   138
	end
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   139
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   140
	if validated_invite.additional_data then
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   141
		module:log("debug", "Importing roles from invite");
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   142
		local roles = validated_invite.additional_data.roles;
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   143
		if roles then
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   144
			module:open_store("roles"):set(contact_username, roles);
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   145
		end
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   146
	end
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   147
end);
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   148
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   149
-- Equivalent of user-registered but for when the account already existed
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   150
-- (i.e. password reset)
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   151
module:hook("user-password-reset", function (event)
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   152
	local validated_invite = event.validated_invite or (event.session and event.session.validated_invite);
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   153
	if not validated_invite then
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   154
		return;
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   155
	end
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   156
	validated_invite:use();
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   157
end);
d34572047488 mod_invites_register: New module to allow IBR with invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   158