mod_easy_invite/mod_easy_invite.lua
author Matthew Wild <mwild1@gmail.com>
Fri, 27 Dec 2019 10:41:01 +0000
changeset 3781 26559776a87e
child 3782 7209f481bcfe
permissions -rw-r--r--
mod_easy_invite: New module that implements XEP-0401/XEP-0379
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3781
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     1
-- XEP-0401: Easy User Onboarding
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     2
local dataforms = require "util.dataforms";
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     3
local datetime = require "util.datetime";
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     4
local jid_bare = require "util.jid".bare;
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     5
local jid_split = require "util.jid".split;
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     6
local split_jid = require "util.jid".split;
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     7
local rostermanager = require "core.rostermanager";
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     8
local st = require "util.stanza";
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     9
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    10
local invite_only = module:get_option_boolean("registration_invite_only", true);
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    11
local require_encryption = module:get_option_boolean("c2s_require_encryption",
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    12
	module:get_option_boolean("require_encryption", false));
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    13
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    14
local new_adhoc = module:require("adhoc").new;
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    15
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    16
-- Whether local users can invite other users to create an account on this server
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    17
local allow_user_invites = module:get_option_boolean("allow_user_invites", true);
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    18
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    19
local invites = module:depends("invites");
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    20
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    21
local invite_result_form = dataforms.new({
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    22
		title = "Your Invite",
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    23
		-- TODO instructions = something helpful
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    24
		{
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    25
			name = "uri";
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    26
			label = "Invite URI";
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    27
			-- TODO desc = something helpful
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    28
		},
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    29
		{
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    30
			name = "url" ;
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    31
			var = "landing-url";
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    32
			label = "Invite landing URL";
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    33
		},
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    34
		{
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    35
			name = "expire";
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    36
			label = "Token valid until";
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    37
		},
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    38
	});
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    39
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    40
module:depends("adhoc");
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    41
module:provides("adhoc", new_adhoc("New Invite", "urn:xmpp:invite#invite",
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    42
		function (_, data)
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    43
			local username = split_jid(data.from);
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    44
			local invite = invites.create_contact(username, allow_user_invites);
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    45
			--TODO: check errors
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    46
			return {
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    47
				status = "completed";
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    48
				form = {
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    49
					layout = invite_result_form;
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    50
					values = {
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    51
						uri = invite.uri;
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    52
						url = invite.landing_page;
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    53
						expire = datetime.datetime(invite.expires);
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    54
					};
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    55
				};
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    56
			};
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    57
		end, "local_user"));
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    58
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    59
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    60
-- TODO
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    61
-- module:provides("adhoc", new_adhoc("Create account", "urn:xmpp:invite#create-account", function () end, "admin"));
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    62
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    63
-- XEP-0379: Pre-Authenticated Roster Subscription
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    64
module:hook("presence/bare", function (event)
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    65
	local stanza = event.stanza;
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    66
	if stanza.attr.type ~= "subscribe" then return end
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    67
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    68
	local preauth = stanza:get_child("preauth", "urn:xmpp:pars:0");
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    69
	if not preauth then return end
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    70
	local token = preauth.attr.token;
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    71
	if not token then return end
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    72
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    73
	local username, host = jid_split(stanza.attr.to);
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    74
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    75
	local invite, err = invites.get(token, username);
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    76
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    77
	if not invite then
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    78
		module:log("debug", "Got invalid token, error: %s", err);
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    79
		return;
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    80
	end
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    81
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    82
	local contact = jid_bare(stanza.attr.from);
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    83
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    84
	module:log("debug", "Approving inbound subscription to %s from %s", username, contact);
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    85
	if rostermanager.set_contact_pending_in(username, host, contact, stanza) then
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    86
		if rostermanager.subscribed(username, host, contact) then
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    87
			invite:use();
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    88
			rostermanager.roster_push(username, host, contact);
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    89
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    90
			-- Send back a subscription request (goal is mutual subscription)
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    91
			if not rostermanager.is_user_subscribed(username, host, contact)
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    92
			and not rostermanager.is_contact_pending_out(username, host, contact) then
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    93
				module:log("debug", "Sending automatic subscription request to %s from %s", contact, username);
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    94
				if rostermanager.set_contact_pending_out(username, host, contact) then
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    95
					rostermanager.roster_push(username, host, contact);
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    96
					module:send(st.presence({type = "subscribe", to = contact }));
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    97
				else
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    98
					module:log("warn", "Failed to set contact pending out for %s", username);
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    99
				end
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   100
			end
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   101
		end
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   102
	end
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   103
end, 1);
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   104
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   105
-- TODO sender side, magic automatic mutual subscription
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   106
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   107
local invite_stream_feature = st.stanza("register", { xmlns = "urn:xmpp:invite" }):up();
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   108
module:hook("stream-features", function(event)
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   109
	local session, features = event.origin, event.features;
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   110
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   111
	-- Advertise to unauthorized clients only.
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   112
	if session.type ~= "c2s_unauthed" or (require_encryption and not session.secure) then
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   113
		return
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   114
	end
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   115
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   116
	features:add_child(invite_stream_feature);
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   117
end);
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   118
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   119
-- Client is submitting a preauth token to allow registration
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   120
module:hook("stanza/iq/urn:xmpp:pars:0:preauth", function(event)
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   121
	local preauth = event.stanza.tags[1];
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   122
	local token = preauth.attr.token;
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   123
	local validated_invite = invites.get(token);
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   124
	if not validated_invite then
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   125
		local reply = st.error_reply(event.stanza, "cancel", "forbidden", "The invite token is invalid or expired");
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   126
		event.origin.send(reply);
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   127
		return true;
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   128
	end
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   129
	event.origin.validated_invite = validated_invite;
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   130
	local reply = st.reply(event.stanza);
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   131
	event.origin.send(reply);
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   132
	return true;
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   133
end);
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   134
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   135
-- Registration attempt - ensure a valid preauth token has been supplied
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   136
module:hook("user-registering", function (event)
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   137
	local validated_invite = event.session.validated_invite;
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   138
	if invite_only and not validated_invite then
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   139
		event.allowed = false;
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   140
		event.reason = "Registration on this server is through invitation only";
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   141
		return;
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   142
	end
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   143
end);
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   144
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   145
-- Make a *one-way* subscription. User will see when contact is online,
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   146
-- contact will not see when user is online.
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   147
function subscribe(host, user_username, contact_username)
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   148
	local user_jid = user_username.."@"..host;
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   149
	local contact_jid = contact_username.."@"..host;
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   150
	-- Update user's roster to say subscription request is pending...
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   151
	rostermanager.set_contact_pending_out(user_username, host, contact_jid);
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   152
	-- Update contact's roster to say subscription request is pending...
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   153
	rostermanager.set_contact_pending_in(contact_username, host, user_jid);
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   154
	-- Update contact's roster to say subscription request approved...
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   155
	rostermanager.subscribed(contact_username, host, user_jid);
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   156
	-- Update user's roster to say subscription request approved...
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   157
	rostermanager.process_inbound_subscription_approval(user_username, host, contact_jid);
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   158
end
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   159
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   160
-- Make a mutual subscription between jid1 and jid2. Each JID will see
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   161
-- when the other one is online.
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   162
function subscribe_both(host, user1, user2)
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   163
	subscribe(host, user1, user2);
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   164
	subscribe(host, user2, user1);
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   165
end
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   166
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   167
-- Registration successful, if there was a preauth token, mark it as used
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   168
module:hook("user-registered", function (event)
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   169
	local validated_invite = event.session.validated_invite;
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   170
	if not validated_invite then
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   171
		return;
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   172
	end
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   173
	local inviter_username = validated_invite.inviter;
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   174
	validated_invite:use();
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   175
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   176
	if not inviter_username then return; end
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   177
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   178
	local contact_username = event.username;
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   179
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   180
	module:log("debug", "Creating mutual subscription between %s and %s", inviter_username, contact_username);
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   181
	subscribe_both(module.host, inviter_username, contact_username);
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   182
end);