Annotate

plugins/mod_invites_adhoc.lua @ 12315:cf2086a1bd45

util.poll: Expose API (epoll or select) used Could he handy to know for debugging or decisions
author Kim Alvefur <zash@zash.se>
date Sun, 27 Feb 2022 14:36:43 +0100
parent 12145:212bac94aedd
child 12491:dc0c20753d6c
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);
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48 if not user_roles then return; end
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49 if user_roles["prosody:admin"] then
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
50 return true;
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
51 end
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52 if allow_user_invite_roles then
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
53 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
54 if user_roles[allowed_role] then
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
55 return true;
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
56 end
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
57 end
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58 end
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59 if deny_user_invite_roles then
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
60 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
61 if user_roles[denied_role] then
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
62 return false;
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
63 end
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
64 end
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
65 end
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
66 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
67 return true; -- Admins may always create invitations
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 -- 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
70 return allow_user_invites;
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
71 end
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
72
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
73 module:depends("adhoc");
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
74
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
75 -- 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
76 -- 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
77 -- 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
78 -- subscription.
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
79 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
80 function (_, data)
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
81 local username, host = split_jid(data.from);
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
82 if host ~= module.host then
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
83 return {
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
84 status = "completed";
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
85 error = {
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
86 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
87 };
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
88 };
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
89 end
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
90 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
91 source = data.from
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 --TODO: check errors
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
94 return {
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
95 status = "completed";
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
96 form = {
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
97 layout = invite_result_form;
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
98 values = {
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
99 uri = invite.uri;
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
100 url = invite.landing_page;
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
101 expire = datetime.datetime(invite.expires);
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
102 };
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
103 };
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
104 };
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
105 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
106
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
107 -- 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
108 -- 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
109 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
110 function (_, data)
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
111 local invite = invites.create_account(nil, {
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
112 source = data.from
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
113 });
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
114 --TODO: check errors
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
115 return {
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
116 status = "completed";
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
117 form = {
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
118 layout = invite_result_form;
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
119 values = {
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
120 uri = invite.uri;
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
121 url = invite.landing_page;
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
122 expire = datetime.datetime(invite.expires);
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
123 };
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
124 };
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
125 };
212bac94aedd mod_invites_adhoc: Import from prosody-modules@5001104f0275
Kim Alvefur <zash@zash.se>
parents:
diff changeset
126 end, "admin"));