Annotate

plugins/mod_invites.lua @ 13744:34ac05f6bd10 13.0

core.configmanager: Fix reporting delayed warnings from global section A Credential in the global section would be stored at delayed_warnings["*/secret"], but get("example.com","secret") would look for delayed_warnings["example.com/secret"] Storing the warnings in the config itself has the unfortunate side-effect that the config now contains util.error objects, which may be awkward if something bypasses get(). Should rawget() also do this filtering? getconfig() too? Currently this only affects prosodyctl, so maybe it won't be much of a problem.
author Kim Alvefur <zash@zash.se>
date Sat, 22 Feb 2025 00:08:18 +0100
parent 13741:e9edf9b50f32
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
9cd5b3484a1d mod_invites: Add support for invites_page option to use external invites pages
Matthew Wild <mwild1@gmail.com>
parents: 13520
diff changeset
224 invite_type = "account";
9cd5b3484a1d mod_invites: Add support for invites_page option to use external invites pages
Matthew Wild <mwild1@gmail.com>
parents: 13520
diff changeset
225 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
226 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
227 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
228 else
9cd5b3484a1d mod_invites: Add support for invites_page option to use external invites pages
Matthew Wild <mwild1@gmail.com>
parents: 13520
diff changeset
229 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
230 end
9cd5b3484a1d mod_invites: Add support for invites_page option to use external invites pages
Matthew Wild <mwild1@gmail.com>
parents: 13520
diff changeset
231 end
9cd5b3484a1d mod_invites: Add support for invites_page option to use external invites pages
Matthew Wild <mwild1@gmail.com>
parents: 13520
diff changeset
232 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
233 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
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
9cd5b3484a1d mod_invites: Add support for invites_page option to use external invites pages
Matthew Wild <mwild1@gmail.com>
parents: 13520
diff changeset
236 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
237 end
9cd5b3484a1d mod_invites: Add support for invites_page option to use external invites pages
Matthew Wild <mwild1@gmail.com>
parents: 13520
diff changeset
238
9cd5b3484a1d mod_invites: Add support for invites_page option to use external invites pages
Matthew Wild <mwild1@gmail.com>
parents: 13520
diff changeset
239 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
240
12142
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
241 --- shell command
13741
e9edf9b50f32 mod_invites: Hide --group flag unless mod_invites_groups is enabled
Matthew Wild <mwild1@gmail.com>
parents: 13740
diff changeset
242 -- 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
243 -- 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
244 -- 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
245 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
246
13353
27512ebcc8af mod_invites: Use new shell-command API
Matthew Wild <mwild1@gmail.com>
parents: 13209
diff changeset
247 module:add_item("shell-command", {
27512ebcc8af mod_invites: Use new shell-command API
Matthew Wild <mwild1@gmail.com>
parents: 13209
diff changeset
248 section = "invite";
27512ebcc8af mod_invites: Use new shell-command API
Matthew Wild <mwild1@gmail.com>
parents: 13209
diff changeset
249 section_desc = "Create and manage invitations";
27512ebcc8af mod_invites: Use new shell-command API
Matthew Wild <mwild1@gmail.com>
parents: 13209
diff changeset
250 name = "create_account";
27512ebcc8af mod_invites: Use new shell-command API
Matthew Wild <mwild1@gmail.com>
parents: 13209
diff changeset
251 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
252 args = {
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
253 { 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
254 };
13353
27512ebcc8af mod_invites: Use new shell-command API
Matthew Wild <mwild1@gmail.com>
parents: 13209
diff changeset
255 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
256 flags = {
13741
e9edf9b50f32 mod_invites: Hide --group flag unless mod_invites_groups is enabled
Matthew Wild <mwild1@gmail.com>
parents: 13740
diff changeset
257 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
258 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
259 };
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
260
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
261 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
262 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
263 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
264 local groups = opts and opts.group or {};
12142
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
265
13740
4cf2caa63277 mod_invites: Fix traceback when no flags passed
Matthew Wild <mwild1@gmail.com>
parents: 13738
diff changeset
266 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
267 -- 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
268 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
269 end
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
270
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
271 local ttl;
13740
4cf2caa63277 mod_invites: Fix traceback when no flags passed
Matthew Wild <mwild1@gmail.com>
parents: 13738
diff changeset
272 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
273 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
274 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
275 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
276 end
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
277 end
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
278
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
279 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
280 roles = roles;
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
281 groups = groups;
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
282 }, ttl));
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
283
12834
dcbff9f038a0 mod_invites: Prefer landing page over xmpp URI in shell command
Kim Alvefur <zash@zash.se>
parents: 12151
diff changeset
284 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
285 end;
27512ebcc8af mod_invites: Use new shell-command API
Matthew Wild <mwild1@gmail.com>
parents: 13209
diff changeset
286 });
12142
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
287
13353
27512ebcc8af mod_invites: Use new shell-command API
Matthew Wild <mwild1@gmail.com>
parents: 13209
diff changeset
288 module:add_item("shell-command", {
27512ebcc8af mod_invites: Use new shell-command API
Matthew Wild <mwild1@gmail.com>
parents: 13209
diff changeset
289 section = "invite";
27512ebcc8af mod_invites: Use new shell-command API
Matthew Wild <mwild1@gmail.com>
parents: 13209
diff changeset
290 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
291 name = "create_reset";
a186f1cfae75 mod_invites: Shell command to create reset links
Matthew Wild <mwild1@gmail.com>
parents: 13613
diff changeset
292 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
293 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
294 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
295 flags = {
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
296 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
297 };
13673
a186f1cfae75 mod_invites: Shell command to create reset links
Matthew Wild <mwild1@gmail.com>
parents: 13613
diff changeset
298
13738
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
299 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
300 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
301 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
302 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
303 end
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
304 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
305 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
306 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
307 end
13673
a186f1cfae75 mod_invites: Shell command to create reset links
Matthew Wild <mwild1@gmail.com>
parents: 13613
diff changeset
308 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
309 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
310 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
311 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
312 end;
a186f1cfae75 mod_invites: Shell command to create reset links
Matthew Wild <mwild1@gmail.com>
parents: 13613
diff changeset
313 });
a186f1cfae75 mod_invites: Shell command to create reset links
Matthew Wild <mwild1@gmail.com>
parents: 13613
diff changeset
314
a186f1cfae75 mod_invites: Shell command to create reset links
Matthew Wild <mwild1@gmail.com>
parents: 13613
diff changeset
315 module:add_item("shell-command", {
a186f1cfae75 mod_invites: Shell command to create reset links
Matthew Wild <mwild1@gmail.com>
parents: 13613
diff changeset
316 section = "invite";
a186f1cfae75 mod_invites: Shell command to create reset links
Matthew Wild <mwild1@gmail.com>
parents: 13613
diff changeset
317 section_desc = "Create and manage invitations";
13353
27512ebcc8af mod_invites: Use new shell-command API
Matthew Wild <mwild1@gmail.com>
parents: 13209
diff changeset
318 name = "create_contact";
27512ebcc8af mod_invites: Use new shell-command API
Matthew Wild <mwild1@gmail.com>
parents: 13209
diff changeset
319 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
320 args = { { name = "user_jid", type = "string" } };
13353
27512ebcc8af mod_invites: Use new shell-command API
Matthew Wild <mwild1@gmail.com>
parents: 13209
diff changeset
321 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
322 flags = {
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
323 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
324 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
325 };
13353
27512ebcc8af mod_invites: Use new shell-command API
Matthew Wild <mwild1@gmail.com>
parents: 13209
diff changeset
326
13738
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
327 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
328 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
329 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
330 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
331 end
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
332 local ttl;
13740
4cf2caa63277 mod_invites: Fix traceback when no flags passed
Matthew Wild <mwild1@gmail.com>
parents: 13738
diff changeset
333 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
334 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
335 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
336 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
337 end
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
338 end
13740
4cf2caa63277 mod_invites: Fix traceback when no flags passed
Matthew Wild <mwild1@gmail.com>
parents: 13738
diff changeset
339 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
340 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
341 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
342 end;
27512ebcc8af mod_invites: Use new shell-command API
Matthew Wild <mwild1@gmail.com>
parents: 13209
diff changeset
343 });
12142
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
344
13674
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
345 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
346 section = "invite";
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
347 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
348 name = "show";
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
349 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
350 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
351 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
352
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
353 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
354 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
355 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
356
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
357 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
358
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
359 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
360 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
361 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
362 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
363 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
364 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
365 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
366 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
367 else
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
368 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
369 end
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
370 else
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
371 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
372 end
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
373
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
374 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
375 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
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 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
379 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
380
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
381 print("");
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
382
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
383 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
384 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
385 end
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.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
388 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
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.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
392 print("");
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
393 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
394 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
395 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
396 end
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[2] then
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
398 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
399 end
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.groups 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("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
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 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
405 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
406 end
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
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
409 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
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 });
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 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
414 section = "invite";
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
415 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
416 name = "delete";
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
417 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
418 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
419 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
420
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
421 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
422 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
423 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
424 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
425 end;
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
426 });
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
427
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
428 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
429 section = "invite";
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
430 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
431 name = "list";
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
432 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
433 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
434 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
435
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
436 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
437 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
438 {
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
439 title = "Token";
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
440 key = "invite";
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
441 width = 24;
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
442 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
443 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
444 end;
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
445 };
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
446 {
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
447 title = "Expires";
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
448 key = "invite";
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
449 width = 20;
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
450 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
451 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
452 end;
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
453 };
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
454 {
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
455 title = "Description";
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
456 key = "invite";
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
457 width = "100%";
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
458 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
459 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
460 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
461 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
462 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
463 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
464 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
465 end
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
466 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
467 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
468 end
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
469 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
470 end
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
471 end;
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
472 };
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
473 }, 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
474
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
475 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
476 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
477 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
478 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
479 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
480 end
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
481 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
482 end;
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
483 });
1ba58459c919 mod_invites: Add shell commands to list, show and delete pending invitations
Matthew Wild <mwild1@gmail.com>
parents: 13673
diff changeset
484
13161
9ba11ef91ce4 mod_invites: Refactor argument handling using util.argparse
Kim Alvefur <zash@zash.se>
parents: 13096
diff changeset
485 local subcommands = {};
12142
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
486
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
487 --- prosodyctl command
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
488 function module.command(arg)
13161
9ba11ef91ce4 mod_invites: Refactor argument handling using util.argparse
Kim Alvefur <zash@zash.se>
parents: 13096
diff changeset
489 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
490 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
491 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
492 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
493 return 2;
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
494 end
13161
9ba11ef91ce4 mod_invites: Refactor argument handling using util.argparse
Kim Alvefur <zash@zash.se>
parents: 13096
diff changeset
495 return subcommands[cmd](arg);
9ba11ef91ce4 mod_invites: Refactor argument handling using util.argparse
Kim Alvefur <zash@zash.se>
parents: 13096
diff changeset
496 end
12142
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
497
13738
26a0f653793e mod_invites: Deprecate 'mod_invites generate' in favour of new shell commands
Matthew Wild <mwild1@gmail.com>
parents: 13725
diff changeset
498 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
499 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
500 return 1;
12142
87532eebd0b8 mod_invites: Import from prosdy-modules@5fc306239db3
Kim Alvefur <zash@zash.se>
parents:
diff changeset
501 end