Annotate

plugins/mod_invites.lua @ 13270:14bbfb2cc8dd default tip

lint: Teach luacheck about module:once Silence warning for using this introduced in 9c62ffbdf2ae
author Kim Alvefur <zash@zash.se>
date Sun, 15 Oct 2023 16:43:14 +0200
parent 13209:c8d949cf6b09
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
12977
74b9e05af71e plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12834
diff changeset
1 local id = require "prosody.util.id";
74b9e05af71e plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12834
diff changeset
2 local it = require "prosody.util.iterators";
12142
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3 local url = require "socket.url";
12977
74b9e05af71e plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12834
diff changeset
4 local jid_node = require "prosody.util.jid".node;
74b9e05af71e plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12834
diff changeset
5 local jid_split = require "prosody.util.jid".split;
13161
9ba11ef91ce4 mod_invites: Refactor argument handling using util.argparse
Kim Alvefur <zash@zash.se>
parents: 13096
diff changeset
6 local argparse = require "prosody.util.argparse";
12142
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7
13209
c8d949cf6b09 plugins: Switch to :get_option_period() for time range options
Kim Alvefur <zash@zash.se>
parents: 13161
diff changeset
8 local default_ttl = module:get_option_period("invite_expiry", "1 week");
12142
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10 local token_storage;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 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
12 token_storage = module:open_store("invite_token", "map");
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15 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
16 return url.build({
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 scheme = "xmpp",
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 path = jid,
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 query = action..";preauth="..token..(params and (";"..params) or ""),
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20 });
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23 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
24 local token = id.medium();
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 local created_at = os.time();
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 local expires = created_at + (ttl or default_ttl);
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 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
30
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 local invite = {
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32 type = invite_action;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33 jid = invite_jid;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 token = token;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 allow_registration = allow_registration;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 additional_data = additional_data;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39 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
40
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41 created_at = created_at;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 expires = expires;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44 reusable = reusable;
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
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47 module:fire_event("invite-created", invite);
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49 if allow_registration then
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
50 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
51 if not ok then
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52 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
53 return nil, "internal-server-error";
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 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
56
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
57 if invite_action == "roster" then
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58 local username = jid_node(invite_jid);
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59 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
60 if not ok then
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
61 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
62 return nil, "internal-server-error";
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 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
65
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
66 return invite;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
67 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
68
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
69 -- 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
70 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
71 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
72 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
73 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
74
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
75 -- 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
76 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
77 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
78 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
79
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
80 -- 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
81 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
82 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
83 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
84
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
85 -- 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
86 -- 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
87 -- during that time period
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
88 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
89 local merged_additional_data = {
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
90 groups = group_ids;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
91 };
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
92 if additional_data then
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
93 for k, v in pairs(additional_data) do
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
94 merged_additional_data[k] = v;
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 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
97 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
98 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
99
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
100 -- 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
101 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
102 local store = module:open_store("invite_token");
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
103 local now = os.time();
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
104 local function is_valid_invite(_, invite)
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
105 return invite.expires > now;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
106 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
107 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
108 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
109
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
110 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
111 if not token then
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
112 return nil, "no-token";
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
113 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
114
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
115 -- Fetch from host store (account invite)
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
116 local token_info = token_storage:get(nil, token);
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
117 if not token_info then
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
118 return nil, "token-invalid";
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
119 elseif os.time() > token_info.expires then
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
120 return nil, "token-expired";
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
121 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
122
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
123 return token_info;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
124 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
125
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
126 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
127 if not token then
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
128 return nil, "no-token";
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
129 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
130
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
131 return token_storage:set(nil, token, nil);
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
132 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
133
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
134 local valid_invite_methods = {};
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
135 local valid_invite_mt = { __index = valid_invite_methods };
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
136
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
137 function valid_invite_methods:use()
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
138 if self.reusable then
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
139 return true;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
140 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
141
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
142 if self.username then
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
143 -- 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
144 -- assumption that they now have a mutual subscription
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
145 token_storage:set(self.username, self.token, nil);
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
146 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
147 token_storage:set(nil, self.token, nil);
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
148
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
149 return true;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
150 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
151
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
152 -- 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
153 -- returned invite after it is actually successfully used
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
154 -- 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
155 -- the invite) must be passed.
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
156 -- 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
157 -- 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
158 -- be set to their username.
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
159 function get(token, username)
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
160 if not token then
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
161 return nil, "no-token";
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
162 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
163
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
164 local valid_until, inviter;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
165
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
166 -- Fetch from host store (account invite)
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
167 local token_info = token_storage:get(nil, token);
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
168
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
169 if username then -- token being used for subscription
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
170 -- Fetch from user store (subscription invite)
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
171 valid_until = token_storage:get(username, token);
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
172 else -- token being used for account creation
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
173 valid_until = token_info and token_info.expires;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
174 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
175 username = jid_node(token_info.jid);
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
176 inviter = username;
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 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
179
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
180 if not valid_until then
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
181 module:log("debug", "Got unknown token: %s", token);
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
182 return nil, "token-invalid";
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
183 elseif os.time() > valid_until then
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
184 module:log("debug", "Got expired token: %s", token);
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
185 return nil, "token-expired";
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
186 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
187
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
188 return setmetatable({
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
189 token = token;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
190 username = username;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
191 inviter = inviter;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
192 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
193 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
194 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
195 reusable = token_info.reusable;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
196 }, valid_invite_mt);
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
197 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
198
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
199 function use(token) --luacheck: ignore 131/use
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
200 local invite = get(token);
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
201 return invite and invite:use();
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
202 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
203
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
204 --- shell command
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
205 do
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
206 -- 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
207 -- 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
208
12977
74b9e05af71e plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12834
diff changeset
209 local get_module = require "prosody.core.modulemanager".get_module;
12142
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
210
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
211 local console_env = module:shared("/*/admin_shell/env");
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
212
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
213 -- luacheck: ignore 212/self
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
214 console_env.invite = {};
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
215 function console_env.invite:create_account(user_jid)
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
216 local username, host = jid_split(user_jid);
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
217 local mod_invites, err = get_module(host, "invites");
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
218 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
219 local invite, err = mod_invites.create_account(username);
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
220 if not invite then return nil, err; end
12834
dcbff9f038a0 mod_invites: Prefer landing page over xmpp URI in shell command
Kim Alvefur <zash@zash.se>
parents: 12151
diff changeset
221 return true, invite.landing_page or invite.uri;
12142
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
222 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
223
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
224 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
225 local username, host = jid_split(user_jid);
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
226 local mod_invites, err = get_module(host, "invites");
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
227 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
228 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
229 if not invite then return nil, err; end
12834
dcbff9f038a0 mod_invites: Prefer landing page over xmpp URI in shell command
Kim Alvefur <zash@zash.se>
parents: 12151
diff changeset
230 return true, invite.landing_page or invite.uri;
12142
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 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
233
13161
9ba11ef91ce4 mod_invites: Refactor argument handling using util.argparse
Kim Alvefur <zash@zash.se>
parents: 13096
diff changeset
234 local subcommands = {};
9ba11ef91ce4 mod_invites: Refactor argument handling using util.argparse
Kim Alvefur <zash@zash.se>
parents: 13096
diff changeset
235
12142
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
236 --- prosodyctl command
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
237 function module.command(arg)
13161
9ba11ef91ce4 mod_invites: Refactor argument handling using util.argparse
Kim Alvefur <zash@zash.se>
parents: 13096
diff changeset
238 local opts = argparse.parse(arg, { short_params = { h = "help"; ["?"] = "help" } });
9ba11ef91ce4 mod_invites: Refactor argument handling using util.argparse
Kim Alvefur <zash@zash.se>
parents: 13096
diff changeset
239 local cmd = table.remove(arg, 1); -- pop command
9ba11ef91ce4 mod_invites: Refactor argument handling using util.argparse
Kim Alvefur <zash@zash.se>
parents: 13096
diff changeset
240 if opts.help or not cmd or not subcommands[cmd] then
12142
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
241 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
242 return 2;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
243 end
13161
9ba11ef91ce4 mod_invites: Refactor argument handling using util.argparse
Kim Alvefur <zash@zash.se>
parents: 13096
diff changeset
244 return subcommands[cmd](arg);
9ba11ef91ce4 mod_invites: Refactor argument handling using util.argparse
Kim Alvefur <zash@zash.se>
parents: 13096
diff changeset
245 end
9ba11ef91ce4 mod_invites: Refactor argument handling using util.argparse
Kim Alvefur <zash@zash.se>
parents: 13096
diff changeset
246
9ba11ef91ce4 mod_invites: Refactor argument handling using util.argparse
Kim Alvefur <zash@zash.se>
parents: 13096
diff changeset
247 function subcommands.generate(arg)
12142
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
248
12977
74b9e05af71e plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12834
diff changeset
249 local sm = require "prosody.core.storagemanager";
74b9e05af71e plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12834
diff changeset
250 local mm = require "prosody.core.modulemanager";
12142
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
251
13161
9ba11ef91ce4 mod_invites: Refactor argument handling using util.argparse
Kim Alvefur <zash@zash.se>
parents: 13096
diff changeset
252 local host = table.remove(arg, 1); -- pop host
12143
51b7ade94d50 mod_invites: Silence luacheck warning
Kim Alvefur <zash@zash.se>
parents: 12142
diff changeset
253 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
254 sm.initialize_host(host);
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
255 module.host = host; --luacheck: ignore 122/module
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
256 token_storage = module:open_store("invite_token", "map");
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
257
13161
9ba11ef91ce4 mod_invites: Refactor argument handling using util.argparse
Kim Alvefur <zash@zash.se>
parents: 13096
diff changeset
258 local opts = argparse.parse(arg, {
9ba11ef91ce4 mod_invites: Refactor argument handling using util.argparse
Kim Alvefur <zash@zash.se>
parents: 13096
diff changeset
259 short_params = { h = "help"; ["?"] = "help"; g = "group" };
9ba11ef91ce4 mod_invites: Refactor argument handling using util.argparse
Kim Alvefur <zash@zash.se>
parents: 13096
diff changeset
260 value_params = { group = true; reset = true; role = true };
9ba11ef91ce4 mod_invites: Refactor argument handling using util.argparse
Kim Alvefur <zash@zash.se>
parents: 13096
diff changeset
261 array_params = { group = true; role = true };
9ba11ef91ce4 mod_invites: Refactor argument handling using util.argparse
Kim Alvefur <zash@zash.se>
parents: 13096
diff changeset
262 });
9ba11ef91ce4 mod_invites: Refactor argument handling using util.argparse
Kim Alvefur <zash@zash.se>
parents: 13096
diff changeset
263
9ba11ef91ce4 mod_invites: Refactor argument handling using util.argparse
Kim Alvefur <zash@zash.se>
parents: 13096
diff changeset
264
9ba11ef91ce4 mod_invites: Refactor argument handling using util.argparse
Kim Alvefur <zash@zash.se>
parents: 13096
diff changeset
265 if opts.help then
9ba11ef91ce4 mod_invites: Refactor argument handling using util.argparse
Kim Alvefur <zash@zash.se>
parents: 13096
diff changeset
266 print("usage: prosodyctl mod_" .. module.name .. " generate DOMAIN --reset USERNAME")
9ba11ef91ce4 mod_invites: Refactor argument handling using util.argparse
Kim Alvefur <zash@zash.se>
parents: 13096
diff changeset
267 print("usage: prosodyctl mod_" .. module.name .. " generate DOMAIN [--admin] [--role ROLE] [--group GROUPID]...")
9ba11ef91ce4 mod_invites: Refactor argument handling using util.argparse
Kim Alvefur <zash@zash.se>
parents: 13096
diff changeset
268 print()
9ba11ef91ce4 mod_invites: Refactor argument handling using util.argparse
Kim Alvefur <zash@zash.se>
parents: 13096
diff changeset
269 print("This command has two modes: password reset and new account.")
9ba11ef91ce4 mod_invites: Refactor argument handling using util.argparse
Kim Alvefur <zash@zash.se>
parents: 13096
diff changeset
270 print("If --reset is given, the command operates in password reset mode and in new account mode otherwise.")
9ba11ef91ce4 mod_invites: Refactor argument handling using util.argparse
Kim Alvefur <zash@zash.se>
parents: 13096
diff changeset
271 print()
9ba11ef91ce4 mod_invites: Refactor argument handling using util.argparse
Kim Alvefur <zash@zash.se>
parents: 13096
diff changeset
272 print("required arguments in password reset mode:")
9ba11ef91ce4 mod_invites: Refactor argument handling using util.argparse
Kim Alvefur <zash@zash.se>
parents: 13096
diff changeset
273 print()
9ba11ef91ce4 mod_invites: Refactor argument handling using util.argparse
Kim Alvefur <zash@zash.se>
parents: 13096
diff changeset
274 print(" --reset USERNAME Generate a password reset link for the given USERNAME.")
9ba11ef91ce4 mod_invites: Refactor argument handling using util.argparse
Kim Alvefur <zash@zash.se>
parents: 13096
diff changeset
275 print()
9ba11ef91ce4 mod_invites: Refactor argument handling using util.argparse
Kim Alvefur <zash@zash.se>
parents: 13096
diff changeset
276 print("optional arguments in new account mode:")
9ba11ef91ce4 mod_invites: Refactor argument handling using util.argparse
Kim Alvefur <zash@zash.se>
parents: 13096
diff changeset
277 print()
9ba11ef91ce4 mod_invites: Refactor argument handling using util.argparse
Kim Alvefur <zash@zash.se>
parents: 13096
diff changeset
278 print(" --admin Make the new user privileged")
9ba11ef91ce4 mod_invites: Refactor argument handling using util.argparse
Kim Alvefur <zash@zash.se>
parents: 13096
diff changeset
279 print(" Equivalent to --role prosody:admin")
9ba11ef91ce4 mod_invites: Refactor argument handling using util.argparse
Kim Alvefur <zash@zash.se>
parents: 13096
diff changeset
280 print(" --role ROLE Grant the given ROLE to the new user")
9ba11ef91ce4 mod_invites: Refactor argument handling using util.argparse
Kim Alvefur <zash@zash.se>
parents: 13096
diff changeset
281 print(" --group GROUPID Add the user to the group with the given ID")
9ba11ef91ce4 mod_invites: Refactor argument handling using util.argparse
Kim Alvefur <zash@zash.se>
parents: 13096
diff changeset
282 print(" Can be specified multiple times")
9ba11ef91ce4 mod_invites: Refactor argument handling using util.argparse
Kim Alvefur <zash@zash.se>
parents: 13096
diff changeset
283 print()
9ba11ef91ce4 mod_invites: Refactor argument handling using util.argparse
Kim Alvefur <zash@zash.se>
parents: 13096
diff changeset
284 print("--group can be specified multiple times; the user will be added to all groups.")
9ba11ef91ce4 mod_invites: Refactor argument handling using util.argparse
Kim Alvefur <zash@zash.se>
parents: 13096
diff changeset
285 print()
9ba11ef91ce4 mod_invites: Refactor argument handling using util.argparse
Kim Alvefur <zash@zash.se>
parents: 13096
diff changeset
286 print("--reset and the other options cannot be mixed.")
9ba11ef91ce4 mod_invites: Refactor argument handling using util.argparse
Kim Alvefur <zash@zash.se>
parents: 13096
diff changeset
287 return 2
9ba11ef91ce4 mod_invites: Refactor argument handling using util.argparse
Kim Alvefur <zash@zash.se>
parents: 13096
diff changeset
288 end
9ba11ef91ce4 mod_invites: Refactor argument handling using util.argparse
Kim Alvefur <zash@zash.se>
parents: 13096
diff changeset
289
12142
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
290 -- Load mod_invites
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
291 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
292 -- 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
293 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
294 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
295 module:depends(invites_page_module);
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
296 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
297
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
298 local allow_reset;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
299
13161
9ba11ef91ce4 mod_invites: Refactor argument handling using util.argparse
Kim Alvefur <zash@zash.se>
parents: 13096
diff changeset
300 if opts.reset then
9ba11ef91ce4 mod_invites: Refactor argument handling using util.argparse
Kim Alvefur <zash@zash.se>
parents: 13096
diff changeset
301 local nodeprep = require "prosody.util.encodings".stringprep.nodeprep;
9ba11ef91ce4 mod_invites: Refactor argument handling using util.argparse
Kim Alvefur <zash@zash.se>
parents: 13096
diff changeset
302 local username = nodeprep(opts.reset)
9ba11ef91ce4 mod_invites: Refactor argument handling using util.argparse
Kim Alvefur <zash@zash.se>
parents: 13096
diff changeset
303 if not username then
9ba11ef91ce4 mod_invites: Refactor argument handling using util.argparse
Kim Alvefur <zash@zash.se>
parents: 13096
diff changeset
304 print("Please supply a valid username to generate a reset link for");
9ba11ef91ce4 mod_invites: Refactor argument handling using util.argparse
Kim Alvefur <zash@zash.se>
parents: 13096
diff changeset
305 return 2;
12142
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
306 end
13161
9ba11ef91ce4 mod_invites: Refactor argument handling using util.argparse
Kim Alvefur <zash@zash.se>
parents: 13096
diff changeset
307 allow_reset = username;
9ba11ef91ce4 mod_invites: Refactor argument handling using util.argparse
Kim Alvefur <zash@zash.se>
parents: 13096
diff changeset
308 end
9ba11ef91ce4 mod_invites: Refactor argument handling using util.argparse
Kim Alvefur <zash@zash.se>
parents: 13096
diff changeset
309
9ba11ef91ce4 mod_invites: Refactor argument handling using util.argparse
Kim Alvefur <zash@zash.se>
parents: 13096
diff changeset
310 local roles = opts.role or {};
9ba11ef91ce4 mod_invites: Refactor argument handling using util.argparse
Kim Alvefur <zash@zash.se>
parents: 13096
diff changeset
311 local groups = opts.groups or {};
9ba11ef91ce4 mod_invites: Refactor argument handling using util.argparse
Kim Alvefur <zash@zash.se>
parents: 13096
diff changeset
312
9ba11ef91ce4 mod_invites: Refactor argument handling using util.argparse
Kim Alvefur <zash@zash.se>
parents: 13096
diff changeset
313 if opts.admin then
9ba11ef91ce4 mod_invites: Refactor argument handling using util.argparse
Kim Alvefur <zash@zash.se>
parents: 13096
diff changeset
314 -- Insert it first since we don't get order out of argparse
9ba11ef91ce4 mod_invites: Refactor argument handling using util.argparse
Kim Alvefur <zash@zash.se>
parents: 13096
diff changeset
315 table.insert(roles, 1, "prosody:admin");
12142
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
316 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
317
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
318 local invite;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
319 if allow_reset then
13096
9638ff8b1c81 mod_invites: Fix password reset invites
Kim Alvefur <zash@zash.se>
parents: 13012
diff changeset
320 if roles[1] then
12142
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
321 print("--role/--admin and --reset are mutually exclusive")
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
322 return 2;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
323 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
324 if #groups > 0 then
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
325 print("--group and --reset are mutually exclusive")
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
326 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
327 invite = assert(invites.create_account_reset(allow_reset));
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
328 else
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
329 invite = assert(invites.create_account(nil, {
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
330 roles = roles,
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
331 groups = groups
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
332 }));
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
333 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
334
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
335 print(invite.landing_page or invite.uri);
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
336 end