comparison mod_invites/mod_invites.lua @ 4096:24f4eb35ab60

mod_invites: Absorb shell and prosodyctl commands from mod_easy_invite
author Matthew Wild <mwild1@gmail.com>
date Fri, 11 Sep 2020 13:53:43 +0100
parents 3c18d8deeb38
children a104440c20a4
comparison
equal deleted inserted replaced
4095:de2390d6bbe4 4096:24f4eb35ab60
1 local id = require "util.id"; 1 local id = require "util.id";
2 local url = require "socket.url"; 2 local url = require "socket.url";
3 local jid_node = require "util.jid".node; 3 local jid_node = require "util.jid".node;
4 local jid_split = require "util.jid".split;
4 5
5 local invite_ttl = module:get_option_number("invite_expiry", 86400 * 7); 6 local invite_ttl = module:get_option_number("invite_expiry", 86400 * 7);
6 7
7 local token_storage = module:open_store("invite_token", "map"); 8 local token_storage;
9 if prosody.process_type == "prosody" or prosody.shutdown then
10 token_storage = module:open_store("invite_token", "map");
11 end
8 12
9 local function get_uri(action, jid, token, params) --> string 13 local function get_uri(action, jid, token, params) --> string
10 return url.build({ 14 return url.build({
11 scheme = "xmpp", 15 scheme = "xmpp",
12 path = jid, 16 path = jid,
83 -- Also remove the contact invite if present, on the 87 -- Also remove the contact invite if present, on the
84 -- assumption that they now have a mutual subscription 88 -- assumption that they now have a mutual subscription
85 token_storage:set(self.username, self.token, nil); 89 token_storage:set(self.username, self.token, nil);
86 end 90 end
87 token_storage:set(nil, self.token, nil); 91 token_storage:set(nil, self.token, nil);
92
88 return true; 93 return true;
89 end 94 end
90 95
91 -- Get a validated invite (or nil, err). Must call :use() on the 96 -- Get a validated invite (or nil, err). Must call :use() on the
92 -- returned invite after it is actually successfully used 97 -- returned invite after it is actually successfully used
136 141
137 function use(token) --luacheck: ignore 131/use 142 function use(token) --luacheck: ignore 131/use
138 local invite = get(token); 143 local invite = get(token);
139 return invite and invite:use(); 144 return invite and invite:use();
140 end 145 end
146
147 --- shell command
148 do
149 -- Since the console is global this overwrites the command for
150 -- each host it's loaded on, but this should be fine.
151
152 local get_module = require "core.modulemanager".get_module;
153
154 local console_env = module:shared("/*/admin_shell/env");
155
156 -- luacheck: ignore 212/self
157 console_env.invite = {};
158 function console_env.invite:create_account(user_jid)
159 local username, host = jid_split(user_jid);
160 local mod_invites, err = get_module(host, "invites");
161 if not mod_invites then return nil, err or "mod_invites not loaded on this host"; end
162 local invite, err = mod_invites.create_account(username);
163 if not invite then return nil, err; end
164 return true, invite.uri;
165 end
166
167 function console_env.invite:create_contact(user_jid, allow_registration)
168 local username, host = jid_split(user_jid);
169 local mod_invites, err = get_module(host, "invites");
170 if not mod_invites then return nil, err or "mod_invites not loaded on this host"; end
171 local invite, err = mod_invites.create_contact(username, allow_registration);
172 if not invite then return nil, err; end
173 return true, invite.uri;
174 end
175 end
176
177 --- prosodyctl command
178 function module.command(arg)
179 if #arg < 2 or arg[1] ~= "generate" then
180 print("usage: prosodyctl mod_"..module.name.." generate example.com");
181 return;
182 end
183 table.remove(arg, 1); -- pop command
184
185 local sm = require "core.storagemanager";
186 local mm = require "core.modulemanager";
187
188 local host = arg[1];
189 assert(hosts[host], "Host "..tostring(host).." does not exist");
190 sm.initialize_host(host);
191 table.remove(arg, 1); -- pop host
192 module.host = host;
193 token_storage = module:open_store("invite_token", "map");
194
195 -- Load mod_invites
196 local invites = module:depends("invites");
197 local invites_page_module = module:get_option_string("invites_page_module", "invites_page");
198 if mm.get_modules_for_host(host):contains(invites_page_module) then
199 module:depends(invites_page_module);
200 end
201
202
203 local invite, roles;
204 if arg[1] == "--reset" then
205 local nodeprep = require "util.encodings".stringprep.nodeprep;
206 local username = nodeprep(arg[2]);
207 if not username then
208 print("Please supply a valid username to generate a reset link for");
209 return;
210 end
211 invite = assert(invites.create_account_reset(username));
212 else
213 if arg[1] == "--admin" then
214 roles = { ["prosody:admin"] = true };
215 elseif arg[1] == "--role" then
216 roles = { [arg[2]] = true };
217 end
218 invite = assert(invites.create_account(nil, { roles = roles }));
219 end
220
221 print(invite.landing_page or invite.uri);
222 end