Annotate

plugins/mod_invites_adhoc.lua @ 12518:73ee3855f970

mod_smacks: Factor out some convenience functions Those lines are long and the risk of mistakes if another one needs to be added seems high, but lower when factored out like this.
author Kim Alvefur <zash@zash.se>
date Fri, 11 Feb 2022 16:09:42 +0100
parent 12491:dc0c20753d6c
child 12642:9061f9621330
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
12145
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 -- XEP-0401: Easy User Onboarding
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2 local dataforms = require "util.dataforms";
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3 local datetime = require "util.datetime";
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 local split_jid = require "util.jid".split;
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5 local usermanager = require "core.usermanager";
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7 local new_adhoc = module:require("adhoc").new;
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9 -- Whether local users can invite other users to create an account on this server
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10 local allow_user_invites = module:get_option_boolean("allow_user_invites", false);
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 -- Who can see and use the contact invite command. It is strongly recommended to
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 -- keep this available to all local users. To allow/disallow invite-registration
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 -- on the server, use the option above instead.
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 local allow_contact_invites = module:get_option_boolean("allow_contact_invites", true);
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16 local allow_user_invite_roles = module:get_option_set("allow_user_invites_by_roles");
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 local deny_user_invite_roles = module:get_option_set("deny_user_invites_by_roles");
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 local invites;
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20 if prosody.shutdown then -- COMPAT hack to detect prosodyctl
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 invites = module:depends("invites");
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22 end
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 local invite_result_form = dataforms.new({
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 title = "Your invite has been created",
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 {
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 name = "url" ;
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 var = "landing-url";
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 label = "Invite web page";
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 desc = "Share this link";
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 },
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32 {
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33 name = "uri";
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34 label = "Invite URI";
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 desc = "This alternative link can be opened with some XMPP clients";
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 },
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 {
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 name = "expire";
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39 label = "Invite valid until";
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 },
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41 });
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43 -- This is for checking if the specified JID may create invites
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44 -- that allow people to register accounts on this host.
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45 local function may_invite_new_users(jid)
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46 if usermanager.get_roles then
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47 local user_roles = usermanager.get_roles(jid, module.host);
12491
dc0c20753d6c mod_invites_adhoc: Fall back to generic allow_user_invites for role-less users
Matthew Wild <mwild1@gmail.com>
parents: 12145
diff changeset
48 if not user_roles then
dc0c20753d6c mod_invites_adhoc: Fall back to generic allow_user_invites for role-less users
Matthew Wild <mwild1@gmail.com>
parents: 12145
diff changeset
49 -- User has no roles we can check, just return default
dc0c20753d6c mod_invites_adhoc: Fall back to generic allow_user_invites for role-less users
Matthew Wild <mwild1@gmail.com>
parents: 12145
diff changeset
50 return allow_user_invites;
dc0c20753d6c mod_invites_adhoc: Fall back to generic allow_user_invites for role-less users
Matthew Wild <mwild1@gmail.com>
parents: 12145
diff changeset
51 end
dc0c20753d6c mod_invites_adhoc: Fall back to generic allow_user_invites for role-less users
Matthew Wild <mwild1@gmail.com>
parents: 12145
diff changeset
52
12145
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
53 if user_roles["prosody:admin"] then
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
54 return true;
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
55 end
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
56 if allow_user_invite_roles then
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
57 for allowed_role in allow_user_invite_roles do
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58 if user_roles[allowed_role] then
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59 return true;
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
60 end
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
61 end
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
62 end
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
63 if deny_user_invite_roles then
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
64 for denied_role in deny_user_invite_roles do
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
65 if user_roles[denied_role] then
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
66 return false;
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
67 end
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
68 end
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
69 end
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
70 elseif usermanager.is_admin(jid, module.host) then -- COMPAT w/0.11
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
71 return true; -- Admins may always create invitations
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
72 end
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
73 -- No role matches, so whatever the default is
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
74 return allow_user_invites;
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
75 end
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
76
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
77 module:depends("adhoc");
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
78
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
79 -- This command is available to all local users, even if allow_user_invites = false
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
80 -- If allow_user_invites is false, creating an invite still works, but the invite will
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
81 -- not be valid for registration on the current server, only for establishing a roster
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
82 -- subscription.
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
83 module:provides("adhoc", new_adhoc("Create new contact invite", "urn:xmpp:invite#invite",
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
84 function (_, data)
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
85 local username, host = split_jid(data.from);
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
86 if host ~= module.host then
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
87 return {
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
88 status = "completed";
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
89 error = {
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
90 message = "This command is only available to users of "..module.host;
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
91 };
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
92 };
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
93 end
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
94 local invite = invites.create_contact(username, may_invite_new_users(data.from), {
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
95 source = data.from
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
96 });
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
97 --TODO: check errors
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
98 return {
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
99 status = "completed";
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
100 form = {
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
101 layout = invite_result_form;
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
102 values = {
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
103 uri = invite.uri;
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
104 url = invite.landing_page;
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
105 expire = datetime.datetime(invite.expires);
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
106 };
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
107 };
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
108 };
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
109 end, allow_contact_invites and "local_user" or "admin"));
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
110
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
111 -- This is an admin-only command that creates a new invitation suitable for registering
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
112 -- a new account. It does not add the new user to the admin's roster.
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
113 module:provides("adhoc", new_adhoc("Create new account invite", "urn:xmpp:invite#create-account",
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
114 function (_, data)
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
115 local invite = invites.create_account(nil, {
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
116 source = data.from
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
117 });
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
118 --TODO: check errors
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
119 return {
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
120 status = "completed";
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
121 form = {
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
122 layout = invite_result_form;
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
123 values = {
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
124 uri = invite.uri;
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
125 url = invite.landing_page;
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
126 expire = datetime.datetime(invite.expires);
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
127 };
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
128 };
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
129 };
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
130 end, "admin"));