mod_easy_invite/mod_easy_invite.lua
author Matthew Wild <mwild1@gmail.com>
Mon, 07 Sep 2020 12:50:36 +0100
changeset 4086 6cdbca89b8be
parent 4027 7e2db4d61f6c
child 4087 e7eb46d976ae
permissions -rw-r--r--
mod_easy_invite: Minor refactoring to begin merging additional changes from Snikket
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
3782
7209f481bcfe mod_easy_invite: Add prosodyctl command to generate account invites
Matthew Wild <mwild1@gmail.com>
parents: 3781
diff changeset
    19
local invites;
7209f481bcfe mod_easy_invite: Add prosodyctl command to generate account invites
Matthew Wild <mwild1@gmail.com>
parents: 3781
diff changeset
    20
if prosody.shutdown then -- COMPAT hack to detect prosodyctl
7209f481bcfe mod_easy_invite: Add prosodyctl command to generate account invites
Matthew Wild <mwild1@gmail.com>
parents: 3781
diff changeset
    21
	invites = module:depends("invites");
7209f481bcfe mod_easy_invite: Add prosodyctl command to generate account invites
Matthew Wild <mwild1@gmail.com>
parents: 3781
diff changeset
    22
end
3781
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    23
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    24
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
    25
		title = "Your Invite",
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    26
		-- TODO instructions = something helpful
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    27
		{
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    28
			name = "uri";
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    29
			label = "Invite URI";
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    30
			-- TODO desc = something helpful
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    31
		},
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    32
		{
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    33
			name = "url" ;
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    34
			var = "landing-url";
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    35
			label = "Invite landing URL";
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    36
		},
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
			name = "expire";
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    39
			label = "Token valid until";
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    40
		},
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    41
	});
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    42
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    43
module:depends("adhoc");
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    44
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
    45
		function (_, data)
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    46
			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
    47
			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
    48
			--TODO: check errors
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    49
			return {
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    50
				status = "completed";
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    51
				form = {
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    52
					layout = invite_result_form;
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    53
					values = {
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    54
						uri = invite.uri;
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    55
						url = invite.landing_page;
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    56
						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
    57
					};
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
		end, "local_user"));
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    61
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
-- TODO
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    64
-- 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
    65
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    66
-- 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
    67
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
    68
	local stanza = event.stanza;
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    69
	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
    70
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    71
	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
    72
	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
    73
	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
    74
	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
    75
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    76
	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
    77
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    78
	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
    79
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    80
	if not invite then
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    81
		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
    82
		return;
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    83
	end
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    84
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    85
	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
    86
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    87
	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
    88
	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
    89
		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
    90
			invite:use();
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    91
			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
    92
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    93
			-- 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
    94
			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
    95
			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
    96
				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
    97
				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
    98
					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
    99
					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
   100
				else
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   101
					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
   102
				end
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   103
			end
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   104
		end
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   105
	end
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   106
end, 1);
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   107
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   108
-- 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
   109
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   110
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
   111
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
   112
	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
   113
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   114
	-- 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
   115
	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
   116
		return
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
	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
   120
end);
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   121
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   122
-- 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
   123
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
   124
	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
   125
	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
   126
	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
   127
	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
   128
		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
   129
		event.origin.send(reply);
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   130
		return true;
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   131
	end
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   132
	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
   133
	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
   134
	event.origin.send(reply);
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   135
	return true;
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   136
end);
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   137
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   138
-- 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
   139
module:hook("user-registering", function (event)
4027
7e2db4d61f6c mod_easy_invite: backport: ensure session exists before accessing it
Maxime “pep” Buquet <pep@bouah.net>
parents: 4026
diff changeset
   140
	local validated_invite = event.validated_invite or (event.session and event.session.validated_invite);
3781
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   141
	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
   142
		event.allowed = false;
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   143
		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
   144
		return;
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   145
	end
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   146
end);
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   147
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   148
-- 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
   149
-- 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
   150
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
   151
	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
   152
	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
   153
	-- 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
   154
	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
   155
	-- 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
   156
	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
   157
	-- 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
   158
	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
   159
	-- 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
   160
	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
   161
end
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   162
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   163
-- 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
   164
-- 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
   165
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
   166
	subscribe(host, user1, user2);
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   167
	subscribe(host, user2, user1);
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   168
end
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   169
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   170
-- 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
   171
module:hook("user-registered", function (event)
4027
7e2db4d61f6c mod_easy_invite: backport: ensure session exists before accessing it
Maxime “pep” Buquet <pep@bouah.net>
parents: 4026
diff changeset
   172
	local validated_invite = event.validated_invite or (event.session and event.session.validated_invite);
3781
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   173
	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
   174
		return;
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   175
	end
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   176
	local inviter_username = validated_invite.inviter;
4086
6cdbca89b8be mod_easy_invite: Minor refactoring to begin merging additional changes from Snikket
Matthew Wild <mwild1@gmail.com>
parents: 4027
diff changeset
   177
	local contact_username = event.username;
3781
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   178
	validated_invite:use();
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   179
4086
6cdbca89b8be mod_easy_invite: Minor refactoring to begin merging additional changes from Snikket
Matthew Wild <mwild1@gmail.com>
parents: 4027
diff changeset
   180
	if inviter_username then
6cdbca89b8be mod_easy_invite: Minor refactoring to begin merging additional changes from Snikket
Matthew Wild <mwild1@gmail.com>
parents: 4027
diff changeset
   181
		module:log("debug", "Creating mutual subscription between %s and %s", inviter_username, contact_username);
6cdbca89b8be mod_easy_invite: Minor refactoring to begin merging additional changes from Snikket
Matthew Wild <mwild1@gmail.com>
parents: 4027
diff changeset
   182
		subscribe_both(module.host, inviter_username, contact_username);
6cdbca89b8be mod_easy_invite: Minor refactoring to begin merging additional changes from Snikket
Matthew Wild <mwild1@gmail.com>
parents: 4027
diff changeset
   183
	end
3781
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   184
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   185
26559776a87e mod_easy_invite: New module that implements XEP-0401/XEP-0379
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   186
end);
3782
7209f481bcfe mod_easy_invite: Add prosodyctl command to generate account invites
Matthew Wild <mwild1@gmail.com>
parents: 3781
diff changeset
   187
4026
3ac31ddab7eb mod_easy_invite: Add commands to telnet console
Kim Alvefur <zash@zash.se>
parents: 3792
diff changeset
   188
do
3ac31ddab7eb mod_easy_invite: Add commands to telnet console
Kim Alvefur <zash@zash.se>
parents: 3792
diff changeset
   189
	-- Telnet command
3ac31ddab7eb mod_easy_invite: Add commands to telnet console
Kim Alvefur <zash@zash.se>
parents: 3792
diff changeset
   190
	-- Since the telnet console is global this overwrites the command for
3ac31ddab7eb mod_easy_invite: Add commands to telnet console
Kim Alvefur <zash@zash.se>
parents: 3792
diff changeset
   191
	-- each host it's loaded on, but this should be fine.
3ac31ddab7eb mod_easy_invite: Add commands to telnet console
Kim Alvefur <zash@zash.se>
parents: 3792
diff changeset
   192
3ac31ddab7eb mod_easy_invite: Add commands to telnet console
Kim Alvefur <zash@zash.se>
parents: 3792
diff changeset
   193
	local get_module = require "core.modulemanager".get_module;
3ac31ddab7eb mod_easy_invite: Add commands to telnet console
Kim Alvefur <zash@zash.se>
parents: 3792
diff changeset
   194
3ac31ddab7eb mod_easy_invite: Add commands to telnet console
Kim Alvefur <zash@zash.se>
parents: 3792
diff changeset
   195
	local console_env = module:shared("/*/admin_telnet/env");
3ac31ddab7eb mod_easy_invite: Add commands to telnet console
Kim Alvefur <zash@zash.se>
parents: 3792
diff changeset
   196
3ac31ddab7eb mod_easy_invite: Add commands to telnet console
Kim Alvefur <zash@zash.se>
parents: 3792
diff changeset
   197
	-- luacheck: ignore 212/self
3ac31ddab7eb mod_easy_invite: Add commands to telnet console
Kim Alvefur <zash@zash.se>
parents: 3792
diff changeset
   198
	console_env.invite = {};
3ac31ddab7eb mod_easy_invite: Add commands to telnet console
Kim Alvefur <zash@zash.se>
parents: 3792
diff changeset
   199
	function console_env.invite:create_account(user_jid)
3ac31ddab7eb mod_easy_invite: Add commands to telnet console
Kim Alvefur <zash@zash.se>
parents: 3792
diff changeset
   200
		local username, host = jid_split(user_jid);
3ac31ddab7eb mod_easy_invite: Add commands to telnet console
Kim Alvefur <zash@zash.se>
parents: 3792
diff changeset
   201
		local mod_invites, err = get_module(host, "invites");
3ac31ddab7eb mod_easy_invite: Add commands to telnet console
Kim Alvefur <zash@zash.se>
parents: 3792
diff changeset
   202
		if not mod_invites then return nil, err or "mod_invites not loaded on this host"; end
3ac31ddab7eb mod_easy_invite: Add commands to telnet console
Kim Alvefur <zash@zash.se>
parents: 3792
diff changeset
   203
		local invite, err = mod_invites.create_account(username);
3ac31ddab7eb mod_easy_invite: Add commands to telnet console
Kim Alvefur <zash@zash.se>
parents: 3792
diff changeset
   204
		if not invite then return nil, err; end
3ac31ddab7eb mod_easy_invite: Add commands to telnet console
Kim Alvefur <zash@zash.se>
parents: 3792
diff changeset
   205
		return true, invite.uri;
3ac31ddab7eb mod_easy_invite: Add commands to telnet console
Kim Alvefur <zash@zash.se>
parents: 3792
diff changeset
   206
	end
3ac31ddab7eb mod_easy_invite: Add commands to telnet console
Kim Alvefur <zash@zash.se>
parents: 3792
diff changeset
   207
3ac31ddab7eb mod_easy_invite: Add commands to telnet console
Kim Alvefur <zash@zash.se>
parents: 3792
diff changeset
   208
	function console_env.invite:create_contact(user_jid, allow_registration)
3ac31ddab7eb mod_easy_invite: Add commands to telnet console
Kim Alvefur <zash@zash.se>
parents: 3792
diff changeset
   209
		local username, host = jid_split(user_jid);
3ac31ddab7eb mod_easy_invite: Add commands to telnet console
Kim Alvefur <zash@zash.se>
parents: 3792
diff changeset
   210
		local mod_invites, err = get_module(host, "invites");
3ac31ddab7eb mod_easy_invite: Add commands to telnet console
Kim Alvefur <zash@zash.se>
parents: 3792
diff changeset
   211
		if not mod_invites then return nil, err or "mod_invites not loaded on this host"; end
3ac31ddab7eb mod_easy_invite: Add commands to telnet console
Kim Alvefur <zash@zash.se>
parents: 3792
diff changeset
   212
		local invite, err = mod_invites.create_contact(username, allow_registration);
3ac31ddab7eb mod_easy_invite: Add commands to telnet console
Kim Alvefur <zash@zash.se>
parents: 3792
diff changeset
   213
		if not invite then return nil, err; end
3ac31ddab7eb mod_easy_invite: Add commands to telnet console
Kim Alvefur <zash@zash.se>
parents: 3792
diff changeset
   214
		return true, invite.uri;
3ac31ddab7eb mod_easy_invite: Add commands to telnet console
Kim Alvefur <zash@zash.se>
parents: 3792
diff changeset
   215
	end
3ac31ddab7eb mod_easy_invite: Add commands to telnet console
Kim Alvefur <zash@zash.se>
parents: 3792
diff changeset
   216
end
3782
7209f481bcfe mod_easy_invite: Add prosodyctl command to generate account invites
Matthew Wild <mwild1@gmail.com>
parents: 3781
diff changeset
   217
7209f481bcfe mod_easy_invite: Add prosodyctl command to generate account invites
Matthew Wild <mwild1@gmail.com>
parents: 3781
diff changeset
   218
local sm = require "core.storagemanager";
7209f481bcfe mod_easy_invite: Add prosodyctl command to generate account invites
Matthew Wild <mwild1@gmail.com>
parents: 3781
diff changeset
   219
function module.command(arg)
3792
14028430638b mod_easy_invite: Change command name to 'generate' (from 'register')
Matthew Wild <mwild1@gmail.com>
parents: 3782
diff changeset
   220
	if #arg < 2 or arg[2] ~= "generate" then
14028430638b mod_easy_invite: Change command name to 'generate' (from 'register')
Matthew Wild <mwild1@gmail.com>
parents: 3782
diff changeset
   221
		print("usage: prosodyctl mod_easy_invite example.net generate");
3782
7209f481bcfe mod_easy_invite: Add prosodyctl command to generate account invites
Matthew Wild <mwild1@gmail.com>
parents: 3781
diff changeset
   222
		return;
7209f481bcfe mod_easy_invite: Add prosodyctl command to generate account invites
Matthew Wild <mwild1@gmail.com>
parents: 3781
diff changeset
   223
	end
7209f481bcfe mod_easy_invite: Add prosodyctl command to generate account invites
Matthew Wild <mwild1@gmail.com>
parents: 3781
diff changeset
   224
7209f481bcfe mod_easy_invite: Add prosodyctl command to generate account invites
Matthew Wild <mwild1@gmail.com>
parents: 3781
diff changeset
   225
	local host = arg[1];
7209f481bcfe mod_easy_invite: Add prosodyctl command to generate account invites
Matthew Wild <mwild1@gmail.com>
parents: 3781
diff changeset
   226
	assert(hosts[host], "Host "..tostring(host).." does not exist");
7209f481bcfe mod_easy_invite: Add prosodyctl command to generate account invites
Matthew Wild <mwild1@gmail.com>
parents: 3781
diff changeset
   227
	sm.initialize_host(host);
7209f481bcfe mod_easy_invite: Add prosodyctl command to generate account invites
Matthew Wild <mwild1@gmail.com>
parents: 3781
diff changeset
   228
7209f481bcfe mod_easy_invite: Add prosodyctl command to generate account invites
Matthew Wild <mwild1@gmail.com>
parents: 3781
diff changeset
   229
	invites = module:context(host):depends("invites");
7209f481bcfe mod_easy_invite: Add prosodyctl command to generate account invites
Matthew Wild <mwild1@gmail.com>
parents: 3781
diff changeset
   230
	local invite = invites.create_account();
7209f481bcfe mod_easy_invite: Add prosodyctl command to generate account invites
Matthew Wild <mwild1@gmail.com>
parents: 3781
diff changeset
   231
	print(invite.uri);
7209f481bcfe mod_easy_invite: Add prosodyctl command to generate account invites
Matthew Wild <mwild1@gmail.com>
parents: 3781
diff changeset
   232
end
7209f481bcfe mod_easy_invite: Add prosodyctl command to generate account invites
Matthew Wild <mwild1@gmail.com>
parents: 3781
diff changeset
   233