Annotate

plugins/mod_invites.lua @ 13864:a20017fb4458

util.pposix: Fix spelling in Teal definition
author Kim Alvefur <zash@zash.se>
date Sun, 30 Mar 2025 16:43:02 +0200
parent 13851:ecafafec7806
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";
13410
7efdd143fdfc mod_invites: Allow specifying invite ttl on command line
Kim Alvefur <zash@zash.se>
parents: 13355
diff changeset
7 local human_io = require "prosody.util.human.io";
12142
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8
13701
1aa7efabeacb mod_cloud_notify, mod_cron, mod_invites: Add 'prosody.' prefix to requires
Matthew Wild <mwild1@gmail.com>
parents: 13674
diff changeset
9 local url_escape = require "prosody.util.http".urlencode;
1aa7efabeacb mod_cloud_notify, mod_cron, mod_invites: Add 'prosody.' prefix to requires
Matthew Wild <mwild1@gmail.com>
parents: 13674
diff changeset
10 local render_url = require "prosody.util.interpolation".new("%b{}", url_escape, {
13613
9cd5b3484a1d mod_invites: Add support for invites_page option to use external invites pages
Matthew Wild <mwild1@gmail.com>
parents: 13520
diff changeset
11 urlescape = url_escape;
9cd5b3484a1d mod_invites: Add support for invites_page option to use external invites pages
Matthew Wild <mwild1@gmail.com>
parents: 13520
diff changeset
12 noscheme = function (urlstring)
9cd5b3484a1d mod_invites: Add support for invites_page option to use external invites pages
Matthew Wild <mwild1@gmail.com>
parents: 13520
diff changeset
13 return (urlstring:gsub("^[^:]+:", ""));
9cd5b3484a1d mod_invites: Add support for invites_page option to use external invites pages
Matthew Wild <mwild1@gmail.com>
parents: 13520
diff changeset
14 end;
9cd5b3484a1d mod_invites: Add support for invites_page option to use external invites pages
Matthew Wild <mwild1@gmail.com>
parents: 13520
diff changeset
15 });
9cd5b3484a1d mod_invites: Add support for invites_page option to use external invites pages
Matthew Wild <mwild1@gmail.com>
parents: 13520
diff changeset
16
13209
c8d949cf6b09 plugins: Switch to :get_option_period() for time range options
Kim Alvefur <zash@zash.se>
parents: 13161
diff changeset
17 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
18
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 local token_storage;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20 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
21 token_storage = module:open_store("invite_token", "map");
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 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
25 return url.build({
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 scheme = "xmpp",
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 path = jid,
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 query = action..";preauth="..token..(params and (";"..params) or ""),
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 });
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32 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
33 local token = id.medium();
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 local created_at = os.time();
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 local expires = created_at + (ttl or default_ttl);
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 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
39
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 local invite = {
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41 type = invite_action;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 jid = invite_jid;
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 token = token;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45 allow_registration = allow_registration;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46 additional_data = additional_data;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48 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
49
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
50 created_at = created_at;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
51 expires = expires;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
53 reusable = reusable;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
54 };
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
55
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
56 module:fire_event("invite-created", invite);
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
57
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58 if allow_registration then
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59 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
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 account 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 if invite_action == "roster" then
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
67 local username = jid_node(invite_jid);
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
68 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
69 if not ok then
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
70 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
71 return nil, "internal-server-error";
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
72 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
73 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 return invite;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
76 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
77
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
78 -- 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
79 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
80 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
81 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
82 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
83
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
84 -- Create invitation to reset the password for an account
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
85 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
86 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
87 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
88
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
89 -- 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
90 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
91 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
92 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
93
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
94 -- 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
95 -- 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
96 -- during that time period
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
97 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
98 local merged_additional_data = {
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
99 groups = group_ids;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
100 };
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
101 if additional_data then
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
102 for k, v in pairs(additional_data) do
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
103 merged_additional_data[k] = v;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
104 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
105 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
106 return 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
107 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
108
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
109 -- 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
110 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
111 local store = module:open_store("invite_token");
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
112 local now = os.time();
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
113 local function is_valid_invite(_, invite)
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
114 return invite.expires > now;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
115 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
116 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
117 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
118
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
119 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
120 if not token then
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
121 return nil, "no-token";
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
122 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
123
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
124 -- Fetch from host store (account invite)
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
125 local token_info = token_storage:get(nil, token);
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
126 if not token_info then
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
127 return nil, "token-invalid";
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
128 elseif os.time() > token_info.expires then
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
129 return nil, "token-expired";
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
130 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
131
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
132 return token_info;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
133 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
134
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
135 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
136 if not token then
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
137 return nil, "no-token";
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
138 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
139
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
140 return token_storage:set(nil, token, nil);
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
141 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
142
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
143 local valid_invite_methods = {};
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
144 local valid_invite_mt = { __index = valid_invite_methods };
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
145
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
146 function valid_invite_methods:use()
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
147 if self.reusable then
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
148 return true;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
149 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
150
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
151 if self.username then
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
152 -- 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
153 -- assumption that they now have a mutual subscription
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
154 token_storage:set(self.username, self.token, nil);
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
155 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
156 token_storage:set(nil, self.token, nil);
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
157
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
158 return true;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
159 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
160
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
161 -- 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
162 -- returned invite after it is actually successfully used
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
163 -- 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
164 -- the invite) must be passed.
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
165 -- 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
166 -- 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
167 -- be set to their username.
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
168 function get(token, username)
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
169 if not token then
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
170 return nil, "no-token";
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
171 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
172
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
173 local valid_until, inviter;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
174
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
175 -- Fetch from host store (account invite)
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
176 local token_info = token_storage:get(nil, token);
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
177
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
178 if username then -- token being used for subscription
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
179 -- Fetch from user store (subscription invite)
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
180 valid_until = token_storage:get(username, token);
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
181 else -- token being used for account creation
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
182 valid_until = token_info and token_info.expires;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
183 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
184 username = jid_node(token_info.jid);
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
185 inviter = username;
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 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
188
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
189 if not valid_until then
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
190 module:log("debug", "Got unknown token: %s", token);
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
191 return nil, "token-invalid";
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
192 elseif os.time() > valid_until then
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
193 module:log("debug", "Got expired token: %s", token);
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
194 return nil, "token-expired";
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
195 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
196
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
197 return setmetatable({
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
198 token = token;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
199 username = username;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
200 inviter = inviter;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
201 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
202 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
203 additional_data = token_info and token_info.additional_data or nil;
13519
0b6af170617b mod_invites: Fix traceback when token_info isn’t set
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 12834
diff changeset
204 reusable = token_info and token_info.reusable or false;
12142
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
205 }, valid_invite_mt);
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
206 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
207
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
208 function use(token) --luacheck: ignore 131/use
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
209 local invite = get(token);
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
210 return invite and invite:use();
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
211 end
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
212
13613
9cd5b3484a1d mod_invites: Add support for invites_page option to use external invites pages
Matthew Wild <mwild1@gmail.com>
parents: 13520
diff changeset
213 -- Point at e.g. a deployment of https://github.com/modernxmpp/easy-xmpp-invitation
9cd5b3484a1d mod_invites: Add support for invites_page option to use external invites pages
Matthew Wild <mwild1@gmail.com>
parents: 13520
diff changeset
214 -- This URL must always be absolute, as it is shared standalone
9cd5b3484a1d mod_invites: Add support for invites_page option to use external invites pages
Matthew Wild <mwild1@gmail.com>
parents: 13520
diff changeset
215 local invite_url_template = module:get_option_string("invites_page");
9cd5b3484a1d mod_invites: Add support for invites_page option to use external invites pages
Matthew Wild <mwild1@gmail.com>
parents: 13520
diff changeset
216 local invites_page_supports = module:get_option_set("invites_page_supports", { "account", "contact", "account-and-contact" });
9cd5b3484a1d mod_invites: Add support for invites_page option to use external invites pages
Matthew Wild <mwild1@gmail.com>
parents: 13520
diff changeset
217
9cd5b3484a1d mod_invites: Add support for invites_page option to use external invites pages
Matthew Wild <mwild1@gmail.com>
parents: 13520
diff changeset
218 local function add_landing_url(invite)
9cd5b3484a1d mod_invites: Add support for invites_page option to use external invites pages
Matthew Wild <mwild1@gmail.com>
parents: 13520
diff changeset
219 if not invite_url_template or invite.landing_page then return; end
9cd5b3484a1d mod_invites: Add support for invites_page option to use external invites pages
Matthew Wild <mwild1@gmail.com>
parents: 13520
diff changeset
220
9cd5b3484a1d mod_invites: Add support for invites_page option to use external invites pages
Matthew Wild <mwild1@gmail.com>
parents: 13520
diff changeset
221 -- Determine whether this type of invitation is supported by the landing page
9cd5b3484a1d mod_invites: Add support for invites_page option to use external invites pages
Matthew Wild <mwild1@gmail.com>
parents: 13520
diff changeset
222 local invite_type;
9cd5b3484a1d mod_invites: Add support for invites_page option to use external invites pages
Matthew Wild <mwild1@gmail.com>
parents: 13520
diff changeset
223 if invite.type == "register" then
13851
ecafafec7806 mod_invites: Consider password reset a distinct type wrt invite page
Kim Alvefur <zash@zash.se>
parents: 13741
diff changeset
224 if invite.additional_data and invite.additional_data.allow_reset then
ecafafec7806 mod_invites: Consider password reset a distinct type wrt invite page
Kim Alvefur <zash@zash.se>
parents: 13741
diff changeset
225 invite_type = "password-reset";
ecafafec7806 mod_invites: Consider password reset a distinct type wrt invite page
Kim Alvefur <zash@zash.se>
parents: 13741
diff changeset
226 else
ecafafec7806 mod_invites: Consider password reset a distinct type wrt invite page
Kim Alvefur <zash@zash.se>
parents: 13741
diff changeset
227 invite_type = "account";
ecafafec7806 mod_invites: Consider password reset a distinct type wrt invite page
Kim Alvefur <zash@zash.se>
parents: 13741
diff changeset
228 end
13613
9cd5b3484a1d mod_invites: Add support for invites_page option to use external invites pages
Matthew Wild <mwild1@gmail.com>
parents: 13520
diff changeset
229 elseif invite.type == "roster" then
9cd5b3484a1d mod_invites: Add support for invites_page option to use external invites pages
Matthew Wild <mwild1@gmail.com>
parents: 13520
diff changeset
230 if invite.allow_registration then
9cd5b3484a1d mod_invites: Add support for invites_page option to use external invites pages
Matthew Wild <mwild1@gmail.com>
parents: 13520
diff changeset
231 invite_type = "account-and-contact";
9cd5b3484a1d mod_invites: Add support for invites_page option to use external invites pages
Matthew Wild <mwild1@gmail.com>
parents: 13520
diff changeset
232 else
9cd5b3484a1d mod_invites: Add support for invites_page option to use external invites pages
Matthew Wild <mwild1@gmail.com>
parents: 13520
diff changeset
233 invite_type = "contact-only";
9cd5b3484a1d mod_invites: Add support for invites_page option to use external invites pages
Matthew Wild <mwild1@gmail.com>
parents: 13520
diff changeset
234 end
9cd5b3484a1d mod_invites: Add support for invites_page option to use external invites pages
Matthew Wild <mwild1@gmail.com>
parents: 13520
diff changeset
235 end
9cd5b3484a1d mod_invites: Add support for invites_page option to use external invites pages
Matthew Wild <mwild1@gmail.com>
parents: 13520
diff changeset
236 if not invites_page_supports:contains(invite_type) then
9cd5b3484a1d mod_invites: Add support for invites_page option to use external invites pages
Matthew Wild <mwild1@gmail.com>
parents: 13520
diff changeset
237 return; -- Invitation type unsupported
9cd5b3484a1d mod_invites: Add support for invites_page option to use external invites pages
Matthew Wild <mwild1@gmail.com>
parents: 13520
diff changeset
238 end
9cd5b3484a1d mod_invites: Add support for invites_page option to use external invites pages
Matthew Wild <mwild1@gmail.com>
parents: 13520
diff changeset
239
9cd5b3484a1d mod_invites: Add support for invites_page option to use external invites pages
Matthew Wild <mwild1@gmail.com>
parents: 13520
diff changeset
240 invite.landing_page = render_url(invite_url_template, { host = module.host, invite = invite });
9cd5b3484a1d mod_invites: Add support for invites_page option to use external invites pages
Matthew Wild <mwild1@gmail.com>
parents: 13520
diff changeset
241 end
9cd5b3484a1d mod_invites: Add support for invites_page option to use external invites pages
Matthew Wild <mwild1@gmail.com>
parents: 13520
diff changeset
242
9cd5b3484a1d mod_invites: Add support for invites_page option to use external invites pages
Matthew Wild <mwild1@gmail.com>
parents: 13520
diff changeset
243 module:hook("invite-created", add_landing_url, -1);
9cd5b3484a1d mod_invites: Add support for invites_page option to use external invites pages
Matthew Wild <mwild1@gmail.com>
parents: 13520
diff changeset
244
12142
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
245 --- shell command
13741
e9edf9b50f32 mod_invites: Hide --group flag unless mod_invites_groups is enabled
Matthew Wild <mwild1@gmail.com>
parents: 13740
diff changeset
246 -- COMPAT: Dynamic groups are work in progress as of 13.0, so we'll use the
e9edf9b50f32 mod_invites: Hide --group flag unless mod_invites_groups is enabled
Matthew Wild <mwild1@gmail.com>
parents: 13740
diff changeset
247 -- presence of mod_invites_groups (a community module) to determine whether to
e9edf9b50f32 mod_invites: Hide --group flag unless mod_invites_groups is enabled
Matthew Wild <mwild1@gmail.com>
parents: 13740
diff changeset
248 -- expose our support for invites to groups.
e9edf9b50f32 mod_invites: Hide --group flag unless mod_invites_groups is enabled
Matthew Wild <mwild1@gmail.com>
parents: 13740
diff changeset
249 local have_group_invites = module:get_option_inherited_set("modules_enabled"):contains("invites_groups");
e9edf9b50f32 mod_invites: Hide --group flag unless mod_invites_groups is enabled
Matthew Wild <mwild1@gmail.com>
parents: 13740
diff changeset
250
13353
27512ebcc8af mod_invites: Use new shell-command API
Matthew Wild <mwild1@gmail.com>
parents: 13209
diff changeset
251 module:add_item("shell-command", {
27512ebcc8af mod_invites: Use new shell-command API
Matthew Wild <mwild1@gmail.com>
parents: 13209
diff changeset
252 section = "invite";
27512ebcc8af mod_invites: Use new shell-command API
Matthew Wild <mwild1@gmail.com>
parents: 13209
diff changeset
253 section_desc = "Create and manage invitations";
27512ebcc8af mod_invites: Use new shell-command API
Matthew Wild <mwild1@gmail.com>
parents: 13209
diff changeset
254 name = "create_account";
27512ebcc8af mod_invites: Use new shell-command API
Matthew Wild <mwild1@gmail.com>
parents: 13209
diff changeset
255 desc = "Create an invitation to make an account on this server with the specified JID (supply only a hostname to allow any username)";
13738
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
256 args = {
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
257 { name = "user_jid", type = "string" };
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
258 };
13353
27512ebcc8af mod_invites: Use new shell-command API
Matthew Wild <mwild1@gmail.com>
parents: 13209
diff changeset
259 host_selector = "user_jid";
13738
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
260 flags = {
13741
e9edf9b50f32 mod_invites: Hide --group flag unless mod_invites_groups is enabled
Matthew Wild <mwild1@gmail.com>
parents: 13740
diff changeset
261 array_params = { role = true, group = have_group_invites };
13738
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
262 value_params = { expires_after = true };
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
263 };
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
264
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
265 handler = function (self, user_jid, opts) --luacheck: ignore 212/self
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
266 local username = jid_split(user_jid);
13740
4cf2caa63277 mod_invites: Fix traceback when no flags passed
Matthew Wild <mwild1@gmail.com>
parents: 13738
diff changeset
267 local roles = opts and opts.role or {};
4cf2caa63277 mod_invites: Fix traceback when no flags passed
Matthew Wild <mwild1@gmail.com>
parents: 13738
diff changeset
268 local groups = opts and opts.group or {};
12142
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
269
13740
4cf2caa63277 mod_invites: Fix traceback when no flags passed
Matthew Wild <mwild1@gmail.com>
parents: 13738
diff changeset
270 if opts and opts.admin then
13738
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
271 -- Insert it first since we don't get order out of argparse
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
272 table.insert(roles, 1, "prosody:admin");
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
273 end
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
274
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
275 local ttl;
13740
4cf2caa63277 mod_invites: Fix traceback when no flags passed
Matthew Wild <mwild1@gmail.com>
parents: 13738
diff changeset
276 if opts and opts.expires_after then
13738
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
277 ttl = human_io.parse_duration(opts.expires_after);
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
278 if not ttl then
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
279 return false, "Unable to parse duration: "..opts.expires_after;
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
280 end
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
281 end
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
282
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
283 local invite = assert(create_account(username, {
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
284 roles = roles;
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
285 groups = groups;
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
286 }, ttl));
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
287
12834
dcbff9f038a0 mod_invites: Prefer landing page over xmpp URI in shell command
Kim Alvefur <zash@zash.se>
parents: 12151
diff changeset
288 return true, invite.landing_page or invite.uri;
13353
27512ebcc8af mod_invites: Use new shell-command API
Matthew Wild <mwild1@gmail.com>
parents: 13209
diff changeset
289 end;
27512ebcc8af mod_invites: Use new shell-command API
Matthew Wild <mwild1@gmail.com>
parents: 13209
diff changeset
290 });
12142
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
291
13353
27512ebcc8af mod_invites: Use new shell-command API
Matthew Wild <mwild1@gmail.com>
parents: 13209
diff changeset
292 module:add_item("shell-command", {
27512ebcc8af mod_invites: Use new shell-command API
Matthew Wild <mwild1@gmail.com>
parents: 13209
diff changeset
293 section = "invite";
27512ebcc8af mod_invites: Use new shell-command API
Matthew Wild <mwild1@gmail.com>
parents: 13209
diff changeset
294 section_desc = "Create and manage invitations";
13673
a186f1cfae75 mod_invites: Shell command to create reset links
Matthew Wild <mwild1@gmail.com>
parents: 13613
diff changeset
295 name = "create_reset";
a186f1cfae75 mod_invites: Shell command to create reset links
Matthew Wild <mwild1@gmail.com>
parents: 13613
diff changeset
296 desc = "Create a password reset link for the specified user";
13738
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
297 args = { { name = "user_jid", type = "string" } };
13673
a186f1cfae75 mod_invites: Shell command to create reset links
Matthew Wild <mwild1@gmail.com>
parents: 13613
diff changeset
298 host_selector = "user_jid";
13738
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
299 flags = {
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
300 value_params = { expires_after = true };
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
301 };
13673
a186f1cfae75 mod_invites: Shell command to create reset links
Matthew Wild <mwild1@gmail.com>
parents: 13613
diff changeset
302
13738
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
303 handler = function (self, user_jid, opts) --luacheck: ignore 212/self
13673
a186f1cfae75 mod_invites: Shell command to create reset links
Matthew Wild <mwild1@gmail.com>
parents: 13613
diff changeset
304 local username = jid_split(user_jid);
13738
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
305 if not username then
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
306 return nil, "Supply the JID of the account you want to generate a password reset for";
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
307 end
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
308 local duration_sec = require "prosody.util.human.io".parse_duration(opts and opts.expires_after or "1d");
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
309 if not duration_sec then
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
310 return nil, "Unable to parse duration: "..opts.expires_after;
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
311 end
13673
a186f1cfae75 mod_invites: Shell command to create reset links
Matthew Wild <mwild1@gmail.com>
parents: 13613
diff changeset
312 local invite, err = create_account_reset(username, duration_sec);
a186f1cfae75 mod_invites: Shell command to create reset links
Matthew Wild <mwild1@gmail.com>
parents: 13613
diff changeset
313 if not invite then return nil, err; end
a186f1cfae75 mod_invites: Shell command to create reset links
Matthew Wild <mwild1@gmail.com>
parents: 13613
diff changeset
314 self.session.print(invite.landing_page or invite.uri);
a186f1cfae75 mod_invites: Shell command to create reset links
Matthew Wild <mwild1@gmail.com>
parents: 13613
diff changeset
315 return true, ("Password reset link for %s valid until %s"):format(user_jid, os.date("%Y-%m-%d %T", invite.expires));
a186f1cfae75 mod_invites: Shell command to create reset links
Matthew Wild <mwild1@gmail.com>
parents: 13613
diff changeset
316 end;
a186f1cfae75 mod_invites: Shell command to create reset links
Matthew Wild <mwild1@gmail.com>
parents: 13613
diff changeset
317 });
a186f1cfae75 mod_invites: Shell command to create reset links
Matthew Wild <mwild1@gmail.com>
parents: 13613
diff changeset
318
a186f1cfae75 mod_invites: Shell command to create reset links
Matthew Wild <mwild1@gmail.com>
parents: 13613
diff changeset
319 module:add_item("shell-command", {
a186f1cfae75 mod_invites: Shell command to create reset links
Matthew Wild <mwild1@gmail.com>
parents: 13613
diff changeset
320 section = "invite";
a186f1cfae75 mod_invites: Shell command to create reset links
Matthew Wild <mwild1@gmail.com>
parents: 13613
diff changeset
321 section_desc = "Create and manage invitations";
13353
27512ebcc8af mod_invites: Use new shell-command API
Matthew Wild <mwild1@gmail.com>
parents: 13209
diff changeset
322 name = "create_contact";
27512ebcc8af mod_invites: Use new shell-command API
Matthew Wild <mwild1@gmail.com>
parents: 13209
diff changeset
323 desc = "Create an invitation to become contacts with the specified user";
13738
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
324 args = { { name = "user_jid", type = "string" } };
13353
27512ebcc8af mod_invites: Use new shell-command API
Matthew Wild <mwild1@gmail.com>
parents: 13209
diff changeset
325 host_selector = "user_jid";
13738
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
326 flags = {
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
327 value_params = { expires_after = true };
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
328 kv_params = { allow_registration = true };
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
329 };
13353
27512ebcc8af mod_invites: Use new shell-command API
Matthew Wild <mwild1@gmail.com>
parents: 13209
diff changeset
330
13738
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
331 handler = function (self, user_jid, opts) --luacheck: ignore 212/self
13355
a6c8a50cdfb5 mod_invites: Fix linter issues
Matthew Wild <mwild1@gmail.com>
parents: 13353
diff changeset
332 local username = jid_split(user_jid);
13738
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
333 if not username then
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
334 return nil, "Supply the JID of the account you want the recipient to become a contact of";
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
335 end
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
336 local ttl;
13740
4cf2caa63277 mod_invites: Fix traceback when no flags passed
Matthew Wild <mwild1@gmail.com>
parents: 13738
diff changeset
337 if opts and opts.expires_after then
13738
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
338 ttl = require "prosody.util.human.io".parse_duration(opts.expires_after);
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
339 if not ttl then
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
340 return nil, "Unable to parse duration: "..opts.expires_after;
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
341 end
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
342 end
13740
4cf2caa63277 mod_invites: Fix traceback when no flags passed
Matthew Wild <mwild1@gmail.com>
parents: 13738
diff changeset
343 local invite, err = create_contact(username, opts and opts.allow_registration, nil, ttl);
12142
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
344 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
345 return true, invite.landing_page or invite.uri;
13353
27512ebcc8af mod_invites: Use new shell-command API
Matthew Wild <mwild1@gmail.com>
parents: 13209
diff changeset
346 end;
27512ebcc8af mod_invites: Use new shell-command API
Matthew Wild <mwild1@gmail.com>
parents: 13209
diff changeset
347 });
12142
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
348
13674
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
349 module:add_item("shell-command", {
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
350 section = "invite";
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
351 section_desc = "Create and manage invitations";
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
352 name = "show";
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
353 desc = "Show details of an account invitation token";
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
354 args = { { name = "host", type = "string" }, { name = "token", type = "string" } };
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
355 host_selector = "host";
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
356
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
357 handler = function (self, host, token) --luacheck: ignore 212/self 212/host
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
358 local invite, err = get_account_invite_info(token);
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
359 if not invite then return nil, err; end
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
360
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
361 local print = self.session.print;
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
362
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
363 if invite.type == "roster" then
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
364 print("Invitation to register and become a contact of "..invite.jid);
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
365 elseif invite.type == "register" then
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
366 local jid_user, jid_host = jid_split(invite.jid);
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
367 if invite.additional_data and invite.additional_data.allow_reset then
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
368 print("Password reset for "..invite.additional_data.allow_reset.."@"..jid_host);
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
369 elseif jid_user then
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
370 print("Invitation to register on "..jid_host.." with username '"..jid_user.."'");
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
371 else
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
372 print("Invitation to register on "..jid_host);
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
373 end
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
374 else
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
375 print("Unknown invitation type");
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
376 end
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
377
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
378 if invite.inviter then
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
379 print("Creator:", invite.inviter);
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
380 end
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
381
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
382 print("Created:", os.date("%Y-%m-%d %T", invite.created_at));
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
383 print("Expires:", os.date("%Y-%m-%d %T", invite.expires));
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
384
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
385 print("");
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
386
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
387 if invite.uri then
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
388 print("XMPP URI:", invite.uri);
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
389 end
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
390
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
391 if invite.landing_page then
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
392 print("Web link:", invite.landing_page);
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
393 end
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
394
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
395 if invite.additional_data then
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
396 print("");
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
397 if invite.additional_data.roles then
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
398 if invite.additional_data.roles[1] then
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
399 print("Role:", invite.additional_data.roles[1]);
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
400 end
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
401 if invite.additional_data.roles[2] then
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
402 print("Secondary roles:", table.concat(invite.additional_data.roles, ", ", 2, #invite.additional_data.roles));
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
403 end
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
404 end
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
405 if invite.additional_data.groups then
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
406 print("Groups:", table.concat(invite.additional_data.groups, ", "));
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
407 end
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
408 if invite.additional_data.note then
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
409 print("Comment:", invite.additional_data.note);
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
410 end
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
411 end
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
412
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
413 return true, "Invitation valid";
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
414 end;
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
415 });
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
416
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
417 module:add_item("shell-command", {
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
418 section = "invite";
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
419 section_desc = "Create and manage invitations";
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
420 name = "delete";
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
421 desc = "Delete/revoke an invitation token";
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
422 args = { { name = "host", type = "string" }, { name = "token", type = "string" } };
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
423 host_selector = "host";
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
424
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
425 handler = function (self, host, token) --luacheck: ignore 212/self 212/host
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
426 local invite, err = delete_account_invite(token);
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
427 if not invite then return nil, err; end
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
428 return true, "Invitation deleted";
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
429 end;
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
430 });
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
431
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
432 module:add_item("shell-command", {
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
433 section = "invite";
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
434 section_desc = "Create and manage invitations";
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
435 name = "list";
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
436 desc = "List pending invitations which allow account registration";
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
437 args = { { name = "host", type = "string" } };
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
438 host_selector = "host";
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
439
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
440 handler = function (self, host) -- luacheck: ignore 212/host
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
441 local print_row = human_io.table({
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
442 {
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
443 title = "Token";
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
444 key = "invite";
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
445 width = 24;
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
446 mapper = function (invite)
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
447 return invite.token;
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
448 end;
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
449 };
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
450 {
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
451 title = "Expires";
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
452 key = "invite";
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
453 width = 20;
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
454 mapper = function (invite)
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
455 return os.date("%Y-%m-%dT%T", invite.expires);
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
456 end;
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
457 };
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
458 {
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
459 title = "Description";
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
460 key = "invite";
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
461 width = "100%";
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
462 mapper = function (invite)
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
463 if invite.type == "roster" then
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
464 return "Contact with "..invite.jid;
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
465 elseif invite.type == "register" then
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
466 local jid_user, jid_host = jid_split(invite.jid);
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
467 if invite.additional_data and invite.additional_data.allow_reset then
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
468 return "Password reset for "..invite.additional_data.allow_reset.."@"..jid_host;
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
469 end
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
470 if jid_user then
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
471 return "Register on "..jid_host.." with username "..jid_user;
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
472 end
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
473 return "Register on "..jid_host;
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
474 end
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
475 end;
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
476 };
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
477 }, self.session.width);
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
478
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
479 self.session.print(print_row());
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
480 local count = 0;
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
481 for _, invite in pending_account_invites() do
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
482 count = count + 1;
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
483 self.session.print(print_row({ invite = invite }));
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
484 end
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
485 return true, ("%d pending invites"):format(count);
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
486 end;
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
487 });
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
488
13161
9ba11ef91ce4 mod_invites: Refactor argument handling using util.argparse
Kim Alvefur <zash@zash.se>
parents: 13096
diff changeset
489 local subcommands = {};
12142
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
490
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
491 --- prosodyctl command
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
492 function module.command(arg)
13161
9ba11ef91ce4 mod_invites: Refactor argument handling using util.argparse
Kim Alvefur <zash@zash.se>
parents: 13096
diff changeset
493 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
494 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
495 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
496 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
497 return 2;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
498 end
13161
9ba11ef91ce4 mod_invites: Refactor argument handling using util.argparse
Kim Alvefur <zash@zash.se>
parents: 13096
diff changeset
499 return subcommands[cmd](arg);
9ba11ef91ce4 mod_invites: Refactor argument handling using util.argparse
Kim Alvefur <zash@zash.se>
parents: 13096
diff changeset
500 end
12142
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
501
13738
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
502 function subcommands.generate()
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
503 print("This command is deprecated. Please see 'prosodyctl shell help invite' for available commands.");
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
504 return 1;
12142
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
505 end