Mercurial > prosody-modules
annotate mod_invites/mod_invites.lua @ 4353:535d80be110d
mod_http_admin_api: Add groups management endpoints
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sun, 17 Jan 2021 17:52:03 +0000 |
parents | 0ec482e617bb |
children | a49ca492e621 |
rev | line source |
---|---|
3776
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 local id = require "util.id"; |
4341
a104440c20a4
mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents:
4096
diff
changeset
|
2 local it = require "util.iterators"; |
3776
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 local url = require "socket.url"; |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
4 local jid_node = require "util.jid".node; |
4096
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
5 local jid_split = require "util.jid".split; |
3776
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 |
4346
671bc55f0fc9
mod_invites: Add support to internal API for creating reusable and custom-TTL invites
Matthew Wild <mwild1@gmail.com>
parents:
4344
diff
changeset
|
7 local default_ttl = module:get_option_number("invite_expiry", 86400 * 7); |
3776
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 |
4096
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
9 local token_storage; |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
10 if prosody.process_type == "prosody" or prosody.shutdown then |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
11 token_storage = module:open_store("invite_token", "map"); |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
12 end |
3776
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 local function get_uri(action, jid, token, params) --> string |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 return url.build({ |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 scheme = "xmpp", |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 path = jid, |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 query = action..";preauth="..token..(params and (";"..params) or ""), |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 }); |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 end |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 |
4346
671bc55f0fc9
mod_invites: Add support to internal API for creating reusable and custom-TTL invites
Matthew Wild <mwild1@gmail.com>
parents:
4344
diff
changeset
|
22 local function create_invite(invite_action, invite_jid, allow_registration, additional_data, ttl, reusable) |
3776
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 local token = id.medium(); |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 local created_at = os.time(); |
4346
671bc55f0fc9
mod_invites: Add support to internal API for creating reusable and custom-TTL invites
Matthew Wild <mwild1@gmail.com>
parents:
4344
diff
changeset
|
26 local expires = created_at + (ttl or default_ttl); |
3776
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 local invite_params = (invite_action == "roster" and allow_registration) and "ibr=y" or nil; |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 local invite = { |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 type = invite_action; |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 jid = invite_jid; |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 token = token; |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 allow_registration = allow_registration; |
4077
f85ea76447dd
mod_invites: Allow inclusion of 'additional data' in invites (from Snikket)
Matthew Wild <mwild1@gmail.com>
parents:
3776
diff
changeset
|
36 additional_data = additional_data; |
3776
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 uri = get_uri(invite_action, invite_jid, token, invite_params); |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 created_at = created_at; |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 expires = expires; |
4346
671bc55f0fc9
mod_invites: Add support to internal API for creating reusable and custom-TTL invites
Matthew Wild <mwild1@gmail.com>
parents:
4344
diff
changeset
|
42 |
671bc55f0fc9
mod_invites: Add support to internal API for creating reusable and custom-TTL invites
Matthew Wild <mwild1@gmail.com>
parents:
4344
diff
changeset
|
43 reusable = reusable; |
3776
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 }; |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 module:fire_event("invite-created", invite); |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 if allow_registration then |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 local ok, err = token_storage:set(nil, token, invite); |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 if not ok then |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 module:log("warn", "Failed to store account invite: %s", err); |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 return nil, "internal-server-error"; |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 end |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 end |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 if invite_action == "roster" then |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 local username = jid_node(invite_jid); |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 local ok, err = token_storage:set(username, token, expires); |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
59 if not ok then |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
60 module:log("warn", "Failed to store subscription invite: %s", err); |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 return nil, "internal-server-error"; |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
62 end |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 end |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
64 |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
65 return invite; |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
66 end |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 -- Create invitation to register an account (optionally restricted to the specified username) |
4077
f85ea76447dd
mod_invites: Allow inclusion of 'additional data' in invites (from Snikket)
Matthew Wild <mwild1@gmail.com>
parents:
3776
diff
changeset
|
69 function create_account(account_username, additional_data) --luacheck: ignore 131/create_account |
3776
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 local jid = account_username and (account_username.."@"..module.host) or module.host; |
4077
f85ea76447dd
mod_invites: Allow inclusion of 'additional data' in invites (from Snikket)
Matthew Wild <mwild1@gmail.com>
parents:
3776
diff
changeset
|
71 return create_invite("register", jid, true, additional_data); |
3776
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
72 end |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
73 |
4078
2f0c8670d2fa
mod_invites: Add API to create account reset invites (from Snikket)
Matthew Wild <mwild1@gmail.com>
parents:
4077
diff
changeset
|
74 -- Create invitation to reset the password for an account |
2f0c8670d2fa
mod_invites: Add API to create account reset invites (from Snikket)
Matthew Wild <mwild1@gmail.com>
parents:
4077
diff
changeset
|
75 function create_account_reset(account_username) --luacheck: ignore 131/create_account_reset |
2f0c8670d2fa
mod_invites: Add API to create account reset invites (from Snikket)
Matthew Wild <mwild1@gmail.com>
parents:
4077
diff
changeset
|
76 return create_account(account_username, { allow_reset = account_username }); |
2f0c8670d2fa
mod_invites: Add API to create account reset invites (from Snikket)
Matthew Wild <mwild1@gmail.com>
parents:
4077
diff
changeset
|
77 end |
2f0c8670d2fa
mod_invites: Add API to create account reset invites (from Snikket)
Matthew Wild <mwild1@gmail.com>
parents:
4077
diff
changeset
|
78 |
3776
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
79 -- Create invitation to become a contact of a local user |
4077
f85ea76447dd
mod_invites: Allow inclusion of 'additional data' in invites (from Snikket)
Matthew Wild <mwild1@gmail.com>
parents:
3776
diff
changeset
|
80 function create_contact(username, allow_registration, additional_data) --luacheck: ignore 131/create_contact |
f85ea76447dd
mod_invites: Allow inclusion of 'additional data' in invites (from Snikket)
Matthew Wild <mwild1@gmail.com>
parents:
3776
diff
changeset
|
81 return create_invite("roster", username.."@"..module.host, allow_registration, additional_data); |
3776
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
82 end |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
83 |
4347
0ec482e617bb
mod_invites: Add public API method for creating group invites
Matthew Wild <mwild1@gmail.com>
parents:
4346
diff
changeset
|
84 -- Create invitation to register an account and join a user group |
0ec482e617bb
mod_invites: Add public API method for creating group invites
Matthew Wild <mwild1@gmail.com>
parents:
4346
diff
changeset
|
85 -- If explicit ttl is passed, invite is valid for multiple signups |
0ec482e617bb
mod_invites: Add public API method for creating group invites
Matthew Wild <mwild1@gmail.com>
parents:
4346
diff
changeset
|
86 -- during that time period |
0ec482e617bb
mod_invites: Add public API method for creating group invites
Matthew Wild <mwild1@gmail.com>
parents:
4346
diff
changeset
|
87 function create_group(group_id, ttl, additional_data) --luacheck: ignore 131/create_group |
0ec482e617bb
mod_invites: Add public API method for creating group invites
Matthew Wild <mwild1@gmail.com>
parents:
4346
diff
changeset
|
88 local merged_additional_data = { |
0ec482e617bb
mod_invites: Add public API method for creating group invites
Matthew Wild <mwild1@gmail.com>
parents:
4346
diff
changeset
|
89 groups = { group_id }; |
0ec482e617bb
mod_invites: Add public API method for creating group invites
Matthew Wild <mwild1@gmail.com>
parents:
4346
diff
changeset
|
90 }; |
0ec482e617bb
mod_invites: Add public API method for creating group invites
Matthew Wild <mwild1@gmail.com>
parents:
4346
diff
changeset
|
91 if merged_additional_data then |
0ec482e617bb
mod_invites: Add public API method for creating group invites
Matthew Wild <mwild1@gmail.com>
parents:
4346
diff
changeset
|
92 for k, v in pairs(additional_data) do |
0ec482e617bb
mod_invites: Add public API method for creating group invites
Matthew Wild <mwild1@gmail.com>
parents:
4346
diff
changeset
|
93 merged_additional_data[k] = v; |
0ec482e617bb
mod_invites: Add public API method for creating group invites
Matthew Wild <mwild1@gmail.com>
parents:
4346
diff
changeset
|
94 end |
0ec482e617bb
mod_invites: Add public API method for creating group invites
Matthew Wild <mwild1@gmail.com>
parents:
4346
diff
changeset
|
95 end |
0ec482e617bb
mod_invites: Add public API method for creating group invites
Matthew Wild <mwild1@gmail.com>
parents:
4346
diff
changeset
|
96 return create_invite("register", module.host, true, merged_additional_data, ttl, not not ttl); |
0ec482e617bb
mod_invites: Add public API method for creating group invites
Matthew Wild <mwild1@gmail.com>
parents:
4346
diff
changeset
|
97 end |
0ec482e617bb
mod_invites: Add public API method for creating group invites
Matthew Wild <mwild1@gmail.com>
parents:
4346
diff
changeset
|
98 |
4341
a104440c20a4
mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents:
4096
diff
changeset
|
99 -- Iterates pending (non-expired, unused) invites that allow registration |
a104440c20a4
mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents:
4096
diff
changeset
|
100 function pending_account_invites() --luacheck: ignore 131/pending_account_invites |
a104440c20a4
mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents:
4096
diff
changeset
|
101 local store = module:open_store("invite_token"); |
a104440c20a4
mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents:
4096
diff
changeset
|
102 local now = os.time(); |
a104440c20a4
mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents:
4096
diff
changeset
|
103 local function is_valid_invite(_, invite) |
a104440c20a4
mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents:
4096
diff
changeset
|
104 return invite.expires > now; |
a104440c20a4
mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents:
4096
diff
changeset
|
105 end |
a104440c20a4
mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents:
4096
diff
changeset
|
106 return it.filter(is_valid_invite, pairs(store:get(nil) or {})); |
a104440c20a4
mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents:
4096
diff
changeset
|
107 end |
a104440c20a4
mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents:
4096
diff
changeset
|
108 |
4344
844cfc8c4039
mod_invites: Fix some more luacheck warnings
Matthew Wild <mwild1@gmail.com>
parents:
4342
diff
changeset
|
109 function get_account_invite_info(token) --luacheck: ignore 131/get_account_invite_info |
4341
a104440c20a4
mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents:
4096
diff
changeset
|
110 if not token then |
a104440c20a4
mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents:
4096
diff
changeset
|
111 return nil, "no-token"; |
a104440c20a4
mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents:
4096
diff
changeset
|
112 end |
a104440c20a4
mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents:
4096
diff
changeset
|
113 |
a104440c20a4
mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents:
4096
diff
changeset
|
114 -- Fetch from host store (account invite) |
a104440c20a4
mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents:
4096
diff
changeset
|
115 local token_info = token_storage:get(nil, token); |
a104440c20a4
mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents:
4096
diff
changeset
|
116 if not token_info then |
a104440c20a4
mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents:
4096
diff
changeset
|
117 return nil, "token-invalid"; |
a104440c20a4
mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents:
4096
diff
changeset
|
118 elseif os.time() > token_info.expires then |
a104440c20a4
mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents:
4096
diff
changeset
|
119 return nil, "token-expired"; |
a104440c20a4
mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents:
4096
diff
changeset
|
120 end |
a104440c20a4
mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents:
4096
diff
changeset
|
121 |
a104440c20a4
mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents:
4096
diff
changeset
|
122 return token_info; |
a104440c20a4
mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents:
4096
diff
changeset
|
123 end |
a104440c20a4
mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents:
4096
diff
changeset
|
124 |
4344
844cfc8c4039
mod_invites: Fix some more luacheck warnings
Matthew Wild <mwild1@gmail.com>
parents:
4342
diff
changeset
|
125 function delete_account_invite(token) --luacheck: ignore 131/delete_account_invite |
4341
a104440c20a4
mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents:
4096
diff
changeset
|
126 if not token then |
a104440c20a4
mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents:
4096
diff
changeset
|
127 return nil, "no-token"; |
a104440c20a4
mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents:
4096
diff
changeset
|
128 end |
a104440c20a4
mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents:
4096
diff
changeset
|
129 |
a104440c20a4
mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents:
4096
diff
changeset
|
130 return token_storage:set(nil, token, nil); |
a104440c20a4
mod_invites: Add internal API to list, read and delete account invites
Matthew Wild <mwild1@gmail.com>
parents:
4096
diff
changeset
|
131 end |
4080
14a3f5223074
mod_invites: Whitespace (from Snikket)
Matthew Wild <mwild1@gmail.com>
parents:
4079
diff
changeset
|
132 |
3776
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
133 local valid_invite_methods = {}; |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
134 local valid_invite_mt = { __index = valid_invite_methods }; |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
135 |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
136 function valid_invite_methods:use() |
4346
671bc55f0fc9
mod_invites: Add support to internal API for creating reusable and custom-TTL invites
Matthew Wild <mwild1@gmail.com>
parents:
4344
diff
changeset
|
137 if self.reusable then |
671bc55f0fc9
mod_invites: Add support to internal API for creating reusable and custom-TTL invites
Matthew Wild <mwild1@gmail.com>
parents:
4344
diff
changeset
|
138 return true; |
671bc55f0fc9
mod_invites: Add support to internal API for creating reusable and custom-TTL invites
Matthew Wild <mwild1@gmail.com>
parents:
4344
diff
changeset
|
139 end |
671bc55f0fc9
mod_invites: Add support to internal API for creating reusable and custom-TTL invites
Matthew Wild <mwild1@gmail.com>
parents:
4344
diff
changeset
|
140 |
3776
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
141 if self.username then |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
142 -- Also remove the contact invite if present, on the |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
143 -- assumption that they now have a mutual subscription |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
144 token_storage:set(self.username, self.token, nil); |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
145 end |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
146 token_storage:set(nil, self.token, nil); |
4096
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
147 |
3776
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
148 return true; |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
149 end |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
150 |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
151 -- Get a validated invite (or nil, err). Must call :use() on the |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
152 -- returned invite after it is actually successfully used |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
153 -- For "roster" invites, the username of the local user (who issued |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
154 -- the invite) must be passed. |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
155 -- If no username is passed, but the registration is a roster invite |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
156 -- from a local user, the "inviter" field of the returned invite will |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
157 -- be set to their username. |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
158 function get(token, username) |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
159 if not token then |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
160 return nil, "no-token"; |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
161 end |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
162 |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
163 local valid_until, inviter; |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
164 |
4079
b68b917e568f
mod_invites: Include invite type and uri in validated invites (from Snikket)
Matthew Wild <mwild1@gmail.com>
parents:
4078
diff
changeset
|
165 -- Fetch from host store (account invite) |
b68b917e568f
mod_invites: Include invite type and uri in validated invites (from Snikket)
Matthew Wild <mwild1@gmail.com>
parents:
4078
diff
changeset
|
166 local token_info = token_storage:get(nil, token); |
b68b917e568f
mod_invites: Include invite type and uri in validated invites (from Snikket)
Matthew Wild <mwild1@gmail.com>
parents:
4078
diff
changeset
|
167 |
3776
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
168 if username then -- token being used for subscription |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
169 -- Fetch from user store (subscription invite) |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
170 valid_until = token_storage:get(username, token); |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
171 else -- token being used for account creation |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
172 valid_until = token_info and token_info.expires; |
4081
3c18d8deeb38
mod_invites: Fix potential traceback when invalid token used (from Snikket)
Matthew Wild <mwild1@gmail.com>
parents:
4080
diff
changeset
|
173 if token_info and token_info.type == "roster" then |
3776
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
174 username = jid_node(token_info.jid); |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
175 inviter = username; |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
176 end |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
177 end |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
178 |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
179 if not valid_until then |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
180 module:log("debug", "Got unknown token: %s", token); |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
181 return nil, "token-invalid"; |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
182 elseif os.time() > valid_until then |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
183 module:log("debug", "Got expired token: %s", token); |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
184 return nil, "token-expired"; |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
185 end |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
186 |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
187 return setmetatable({ |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
188 token = token; |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
189 username = username; |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
190 inviter = inviter; |
4079
b68b917e568f
mod_invites: Include invite type and uri in validated invites (from Snikket)
Matthew Wild <mwild1@gmail.com>
parents:
4078
diff
changeset
|
191 type = token_info and token_info.type or "roster"; |
b68b917e568f
mod_invites: Include invite type and uri in validated invites (from Snikket)
Matthew Wild <mwild1@gmail.com>
parents:
4078
diff
changeset
|
192 uri = token_info and token_info.uri or get_uri("roster", username.."@"..module.host, token); |
4077
f85ea76447dd
mod_invites: Allow inclusion of 'additional data' in invites (from Snikket)
Matthew Wild <mwild1@gmail.com>
parents:
3776
diff
changeset
|
193 additional_data = token_info and token_info.additional_data or nil; |
4346
671bc55f0fc9
mod_invites: Add support to internal API for creating reusable and custom-TTL invites
Matthew Wild <mwild1@gmail.com>
parents:
4344
diff
changeset
|
194 reusable = token_info.reusable; |
3776
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
195 }, valid_invite_mt); |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
196 end |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
197 |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
198 function use(token) --luacheck: ignore 131/use |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
199 local invite = get(token); |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
200 return invite and invite:use(); |
80830d97da81
mod_invites: New module providing an API to create/manage invite tokens
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
201 end |
4096
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
202 |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
203 --- shell command |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
204 do |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
205 -- Since the console is global this overwrites the command for |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
206 -- each host it's loaded on, but this should be fine. |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
207 |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
208 local get_module = require "core.modulemanager".get_module; |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
209 |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
210 local console_env = module:shared("/*/admin_shell/env"); |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
211 |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
212 -- luacheck: ignore 212/self |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
213 console_env.invite = {}; |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
214 function console_env.invite:create_account(user_jid) |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
215 local username, host = jid_split(user_jid); |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
216 local mod_invites, err = get_module(host, "invites"); |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
217 if not mod_invites then return nil, err or "mod_invites not loaded on this host"; end |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
218 local invite, err = mod_invites.create_account(username); |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
219 if not invite then return nil, err; end |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
220 return true, invite.uri; |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
221 end |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
222 |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
223 function console_env.invite:create_contact(user_jid, allow_registration) |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
224 local username, host = jid_split(user_jid); |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
225 local mod_invites, err = get_module(host, "invites"); |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
226 if not mod_invites then return nil, err or "mod_invites not loaded on this host"; end |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
227 local invite, err = mod_invites.create_contact(username, allow_registration); |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
228 if not invite then return nil, err; end |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
229 return true, invite.uri; |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
230 end |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
231 end |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
232 |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
233 --- prosodyctl command |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
234 function module.command(arg) |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
235 if #arg < 2 or arg[1] ~= "generate" then |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
236 print("usage: prosodyctl mod_"..module.name.." generate example.com"); |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
237 return; |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
238 end |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
239 table.remove(arg, 1); -- pop command |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
240 |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
241 local sm = require "core.storagemanager"; |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
242 local mm = require "core.modulemanager"; |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
243 |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
244 local host = arg[1]; |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
245 assert(hosts[host], "Host "..tostring(host).." does not exist"); |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
246 sm.initialize_host(host); |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
247 table.remove(arg, 1); -- pop host |
4342
84e60c3d6e61
mod_invites: Fix luacheck warning
Matthew Wild <mwild1@gmail.com>
parents:
4341
diff
changeset
|
248 module.host = host; --luacheck: ignore 122/module |
4096
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
249 token_storage = module:open_store("invite_token", "map"); |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
250 |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
251 -- Load mod_invites |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
252 local invites = module:depends("invites"); |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
253 local invites_page_module = module:get_option_string("invites_page_module", "invites_page"); |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
254 if mm.get_modules_for_host(host):contains(invites_page_module) then |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
255 module:depends(invites_page_module); |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
256 end |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
257 |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
258 |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
259 local invite, roles; |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
260 if arg[1] == "--reset" then |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
261 local nodeprep = require "util.encodings".stringprep.nodeprep; |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
262 local username = nodeprep(arg[2]); |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
263 if not username then |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
264 print("Please supply a valid username to generate a reset link for"); |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
265 return; |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
266 end |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
267 invite = assert(invites.create_account_reset(username)); |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
268 else |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
269 if arg[1] == "--admin" then |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
270 roles = { ["prosody:admin"] = true }; |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
271 elseif arg[1] == "--role" then |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
272 roles = { [arg[2]] = true }; |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
273 end |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
274 invite = assert(invites.create_account(nil, { roles = roles })); |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
275 end |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
276 |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
277 print(invite.landing_page or invite.uri); |
24f4eb35ab60
mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
Matthew Wild <mwild1@gmail.com>
parents:
4081
diff
changeset
|
278 end |