Annotate

plugins/mod_invites.lua @ 12522:1671cb924002 0.12

mod_smacks: Fix to use current method of counting acked stanzas Fixes #1757 These places seem to have been left since e62025f949f9 The logic around expected_h in should_ack() misbehaved, always comparing with 0 + unacked instead of acked + unacked.
author Kim Alvefur <zash@zash.se>
date Thu, 26 May 2022 17:38:55 +0200
parent 12151:669d93f1db6a
child 12834:dcbff9f038a0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
12142
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 local id = require "util.id";
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2 local it = require "util.iterators";
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3 local url = require "socket.url";
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 local jid_node = require "util.jid".node;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5 local jid_split = require "util.jid".split;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7 local default_ttl = module:get_option_number("invite_expiry", 86400 * 7);
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9 local token_storage;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10 if prosody.process_type == "prosody" or prosody.shutdown then
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 token_storage = module:open_store("invite_token", "map");
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 local function get_uri(action, jid, token, params) --> string
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15 return url.build({
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16 scheme = "xmpp",
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 path = jid,
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 query = action..";preauth="..token..(params and (";"..params) or ""),
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 });
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22 local function create_invite(invite_action, invite_jid, allow_registration, additional_data, ttl, reusable)
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23 local token = id.medium();
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 local created_at = os.time();
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 local expires = created_at + (ttl or default_ttl);
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 local invite_params = (invite_action == "roster" and allow_registration) and "ibr=y" or nil;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 local invite = {
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 type = invite_action;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32 jid = invite_jid;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34 token = token;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 allow_registration = allow_registration;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 additional_data = additional_data;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 uri = get_uri(invite_action, invite_jid, token, invite_params);
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 created_at = created_at;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41 expires = expires;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43 reusable = reusable;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44 };
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46 module:fire_event("invite-created", invite);
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48 if allow_registration then
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49 local ok, err = token_storage:set(nil, token, invite);
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
50 if not ok then
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
51 module:log("warn", "Failed to store account invite: %s", err);
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52 return nil, "internal-server-error";
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
53 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
54 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
55
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
56 if invite_action == "roster" then
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
57 local username = jid_node(invite_jid);
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58 local ok, err = token_storage:set(username, token, expires);
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59 if not ok then
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
60 module:log("warn", "Failed to store subscription invite: %s", err);
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
61 return nil, "internal-server-error";
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
62 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
63 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
64
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
65 return invite;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
66 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
67
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
68 -- Create invitation to register an account (optionally restricted to the specified username)
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
69 function create_account(account_username, additional_data, ttl) --luacheck: ignore 131/create_account
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
70 local jid = account_username and (account_username.."@"..module.host) or module.host;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
71 return create_invite("register", jid, true, additional_data, ttl);
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
72 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
73
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
74 -- Create invitation to reset the password for an account
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
75 function create_account_reset(account_username, ttl) --luacheck: ignore 131/create_account_reset
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
76 return create_account(account_username, { allow_reset = account_username }, ttl or 86400);
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
77 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
78
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
79 -- Create invitation to become a contact of a local user
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
80 function create_contact(username, allow_registration, additional_data, ttl) --luacheck: ignore 131/create_contact
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
81 return create_invite("roster", username.."@"..module.host, allow_registration, additional_data, ttl);
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
82 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
83
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
84 -- Create invitation to register an account and join a user group
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
85 -- If explicit ttl is passed, invite is valid for multiple signups
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
86 -- during that time period
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
87 function create_group(group_ids, additional_data, ttl) --luacheck: ignore 131/create_group
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
88 local merged_additional_data = {
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
89 groups = group_ids;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
90 };
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
91 if additional_data then
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
92 for k, v in pairs(additional_data) do
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
93 merged_additional_data[k] = v;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
94 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
95 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
96 return create_invite("register", module.host, true, merged_additional_data, ttl, not not ttl);
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
97 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
98
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
99 -- Iterates pending (non-expired, unused) invites that allow registration
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
100 function pending_account_invites() --luacheck: ignore 131/pending_account_invites
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
101 local store = module:open_store("invite_token");
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
102 local now = os.time();
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
103 local function is_valid_invite(_, invite)
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
104 return invite.expires > now;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
105 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
106 return it.filter(is_valid_invite, pairs(store:get(nil) or {}));
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
107 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
108
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
109 function get_account_invite_info(token) --luacheck: ignore 131/get_account_invite_info
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
110 if not token then
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
111 return nil, "no-token";
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
112 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
113
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
114 -- Fetch from host store (account invite)
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
115 local token_info = token_storage:get(nil, token);
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
116 if not token_info then
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
117 return nil, "token-invalid";
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
118 elseif os.time() > token_info.expires then
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
119 return nil, "token-expired";
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
120 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
121
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
122 return token_info;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
123 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
124
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
125 function delete_account_invite(token) --luacheck: ignore 131/delete_account_invite
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
126 if not token then
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
127 return nil, "no-token";
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
128 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
129
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
130 return token_storage:set(nil, token, nil);
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
131 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
132
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
133 local valid_invite_methods = {};
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
134 local valid_invite_mt = { __index = valid_invite_methods };
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
135
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
136 function valid_invite_methods:use()
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
137 if self.reusable then
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
138 return true;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
139 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
140
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
141 if self.username then
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
142 -- Also remove the contact invite if present, on the
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
143 -- assumption that they now have a mutual subscription
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
144 token_storage:set(self.username, self.token, nil);
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
145 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
146 token_storage:set(nil, self.token, nil);
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
147
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
148 return true;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
149 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
150
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
151 -- Get a validated invite (or nil, err). Must call :use() on the
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
152 -- returned invite after it is actually successfully used
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
153 -- For "roster" invites, the username of the local user (who issued
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
154 -- the invite) must be passed.
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
155 -- If no username is passed, but the registration is a roster invite
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
156 -- from a local user, the "inviter" field of the returned invite will
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
157 -- be set to their username.
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
158 function get(token, username)
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
159 if not token then
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
160 return nil, "no-token";
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
161 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
162
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
163 local valid_until, inviter;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
164
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
165 -- Fetch from host store (account invite)
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
166 local token_info = token_storage:get(nil, token);
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
167
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
168 if username then -- token being used for subscription
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
169 -- Fetch from user store (subscription invite)
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
170 valid_until = token_storage:get(username, token);
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
171 else -- token being used for account creation
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
172 valid_until = token_info and token_info.expires;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
173 if token_info and token_info.type == "roster" then
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
174 username = jid_node(token_info.jid);
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
175 inviter = username;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
176 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
177 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
178
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
179 if not valid_until then
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
180 module:log("debug", "Got unknown token: %s", token);
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
181 return nil, "token-invalid";
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
182 elseif os.time() > valid_until then
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
183 module:log("debug", "Got expired token: %s", token);
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
184 return nil, "token-expired";
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
185 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
186
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
187 return setmetatable({
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
188 token = token;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
189 username = username;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
190 inviter = inviter;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
191 type = token_info and token_info.type or "roster";
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
192 uri = token_info and token_info.uri or get_uri("roster", username.."@"..module.host, token);
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
193 additional_data = token_info and token_info.additional_data or nil;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
194 reusable = token_info.reusable;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
195 }, valid_invite_mt);
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
196 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
197
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
198 function use(token) --luacheck: ignore 131/use
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
199 local invite = get(token);
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
200 return invite and invite:use();
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
201 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
202
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
203 --- shell command
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
204 do
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
205 -- Since the console is global this overwrites the command for
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
206 -- each host it's loaded on, but this should be fine.
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
207
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
208 local get_module = require "core.modulemanager".get_module;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
209
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
210 local console_env = module:shared("/*/admin_shell/env");
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
211
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
212 -- luacheck: ignore 212/self
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
213 console_env.invite = {};
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
214 function console_env.invite:create_account(user_jid)
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
215 local username, host = jid_split(user_jid);
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
216 local mod_invites, err = get_module(host, "invites");
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
217 if not mod_invites then return nil, err or "mod_invites not loaded on this host"; end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
218 local invite, err = mod_invites.create_account(username);
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
219 if not invite then return nil, err; end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
220 return true, invite.uri;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
221 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
222
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
223 function console_env.invite:create_contact(user_jid, allow_registration)
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
224 local username, host = jid_split(user_jid);
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
225 local mod_invites, err = get_module(host, "invites");
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
226 if not mod_invites then return nil, err or "mod_invites not loaded on this host"; end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
227 local invite, err = mod_invites.create_contact(username, allow_registration);
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
228 if not invite then return nil, err; end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
229 return true, invite.uri;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
230 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
231 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
232
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
233 --- prosodyctl command
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
234 function module.command(arg)
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
235 if #arg < 2 or arg[1] ~= "generate" then
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
236 print("usage: prosodyctl mod_"..module.name.." generate example.com");
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
237 return 2;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
238 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
239 table.remove(arg, 1); -- pop command
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
240
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
241 local sm = require "core.storagemanager";
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
242 local mm = require "core.modulemanager";
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
243
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
244 local host = arg[1];
12143
51b7ade94d50 mod_invites: Silence luacheck warning
Kim Alvefur <zash@zash.se>
parents: 12142
diff changeset
245 assert(prosody.hosts[host], "Host "..tostring(host).." does not exist");
12142
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
246 sm.initialize_host(host);
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
247 table.remove(arg, 1); -- pop host
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
248 module.host = host; --luacheck: ignore 122/module
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
249 token_storage = module:open_store("invite_token", "map");
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
250
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
251 -- Load mod_invites
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
252 local invites = module:depends("invites");
12151
669d93f1db6a mod_invites: Comment on module loading for HTTP invite URLs
Kim Alvefur <zash@zash.se>
parents: 12143
diff changeset
253 -- Optional community module that if used, needs to be loaded here
12142
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
254 local invites_page_module = module:get_option_string("invites_page_module", "invites_page");
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
255 if mm.get_modules_for_host(host):contains(invites_page_module) then
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
256 module:depends(invites_page_module);
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
257 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
258
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
259 local allow_reset;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
260 local roles;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
261 local groups = {};
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
262
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
263 while #arg > 0 do
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
264 local value = arg[1];
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
265 table.remove(arg, 1);
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
266 if value == "--help" then
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
267 print("usage: prosodyctl mod_"..module.name.." generate DOMAIN --reset USERNAME")
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
268 print("usage: prosodyctl mod_"..module.name.." generate DOMAIN [--admin] [--role ROLE] [--group GROUPID]...")
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
269 print()
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
270 print("This command has two modes: password reset and new account.")
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
271 print("If --reset is given, the command operates in password reset mode and in new account mode otherwise.")
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
272 print()
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
273 print("required arguments in password reset mode:")
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
274 print()
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
275 print(" --reset USERNAME Generate a password reset link for the given USERNAME.")
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
276 print()
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
277 print("optional arguments in new account mode:")
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
278 print()
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
279 print(" --admin Make the new user privileged")
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
280 print(" Equivalent to --role prosody:admin")
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
281 print(" --role ROLE Grant the given ROLE to the new user")
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
282 print(" --group GROUPID Add the user to the group with the given ID")
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
283 print(" Can be specified multiple times")
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
284 print()
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
285 print("--role and --admin override each other; the last one wins")
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
286 print("--group can be specified multiple times; the user will be added to all groups.")
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
287 print()
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
288 print("--reset and the other options cannot be mixed.")
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
289 return 2
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
290 elseif value == "--reset" then
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
291 local nodeprep = require "util.encodings".stringprep.nodeprep;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
292 local username = nodeprep(arg[1])
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
293 table.remove(arg, 1);
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
294 if not username then
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
295 print("Please supply a valid username to generate a reset link for");
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
296 return 2;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
297 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
298 allow_reset = username;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
299 elseif value == "--admin" then
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
300 roles = { ["prosody:admin"] = true };
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
301 elseif value == "--role" then
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
302 local rolename = arg[1];
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
303 if not rolename then
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
304 print("Please supply a role name");
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
305 return 2;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
306 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
307 roles = { [rolename] = true };
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
308 table.remove(arg, 1);
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
309 elseif value == "--group" or value == "-g" then
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
310 local groupid = arg[1];
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
311 if not groupid then
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
312 print("Please supply a group ID")
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
313 return 2;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
314 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
315 table.insert(groups, groupid);
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
316 table.remove(arg, 1);
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
317 else
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
318 print("unexpected argument: "..value)
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
319 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
320 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
321
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
322 local invite;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
323 if allow_reset then
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
324 if roles then
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
325 print("--role/--admin and --reset are mutually exclusive")
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
326 return 2;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
327 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
328 if #groups > 0 then
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
329 print("--group and --reset are mutually exclusive")
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
330 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
331 invite = assert(invites.create_account_reset(allow_reset));
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
332 else
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
333 invite = assert(invites.create_account(nil, {
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
334 roles = roles,
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
335 groups = groups
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
336 }));
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
337 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
338
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
339 print(invite.landing_page or invite.uri);
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
340 end