Comparison

mod_invites/mod_invites.lua @ 4421:94805a7e7b30

mod_invites: rework CLI parsing to support groups To make this sensible, the code had to move from rather simple parsing to something which looks more like getopt or your typical shell script.
author Jonas Schäfer <jonas@wielicki.name>
date Sun, 31 Jan 2021 19:16:36 +0100
parent 4377:a0f1fb5e7829
child 4422:2047dd56cc40
comparison
equal deleted inserted replaced
4420:a81516906822 4421:94805a7e7b30
232 232
233 --- prosodyctl command 233 --- prosodyctl command
234 function module.command(arg) 234 function module.command(arg)
235 if #arg < 2 or arg[1] ~= "generate" then 235 if #arg < 2 or arg[1] ~= "generate" then
236 print("usage: prosodyctl mod_"..module.name.." generate example.com"); 236 print("usage: prosodyctl mod_"..module.name.." generate example.com");
237 return; 237 return 2;
238 end 238 end
239 table.remove(arg, 1); -- pop command 239 table.remove(arg, 1); -- pop command
240 240
241 local sm = require "core.storagemanager"; 241 local sm = require "core.storagemanager";
242 local mm = require "core.modulemanager"; 242 local mm = require "core.modulemanager";
253 local invites_page_module = module:get_option_string("invites_page_module", "invites_page"); 253 local invites_page_module = module:get_option_string("invites_page_module", "invites_page");
254 if mm.get_modules_for_host(host):contains(invites_page_module) then 254 if mm.get_modules_for_host(host):contains(invites_page_module) then
255 module:depends(invites_page_module); 255 module:depends(invites_page_module);
256 end 256 end
257 257
258 258 local allow_reset;
259 local invite, roles; 259 local roles;
260 if arg[1] == "--reset" then 260 local groups = {};
261 local nodeprep = require "util.encodings".stringprep.nodeprep; 261
262 local username = nodeprep(arg[2]); 262 while #arg > 0 do
263 if not username then 263 local value = arg[1];
264 print("Please supply a valid username to generate a reset link for"); 264 table.remove(arg, 1);
265 return; 265 if value == "--reset" then
266 end 266 local nodeprep = require "util.encodings".stringprep.nodeprep;
267 invite = assert(invites.create_account_reset(username)); 267 local username = nodeprep(arg[1])
268 table.remove(arg, 1);
269 if not username then
270 print("Please supply a valid username to generate a reset link for");
271 return 2;
272 end
273 allow_reset = username;
274 elseif value == "--admin" then
275 roles = { ["prosody:admin"] = true };
276 elseif value == "--role" then
277 local rolename = arg[1];
278 if not rolename then
279 print("Please supply a role name");
280 return 2;
281 end
282 roles = { [rolename] = true };
283 table.remove(arg, 1);
284 elseif value == "--group" or value == "-g" then
285 local groupid = arg[1];
286 if not groupid then
287 print("Please supply a group ID")
288 return 2;
289 end
290 table.insert(groups, groupid);
291 table.remove(arg, 1);
292 else
293 print("unexpected argument: "..value)
294 end
295 end
296
297 local invite;
298 if allow_reset then
299 if roles then
300 print("--role/--admin and --reset are mutually exclusive")
301 return 2;
302 end
303 if #groups > 0 then
304 print("--group and --reset are mutually exclusive")
305 end
306 invite = assert(invites.create_account_reset(allow_reset));
268 else 307 else
269 if arg[1] == "--admin" then 308 invite = assert(invites.create_account(nil, {
270 roles = { ["prosody:admin"] = true }; 309 roles = roles,
271 elseif arg[1] == "--role" then 310 groups = groups
272 roles = { [arg[2]] = true }; 311 }));
273 end
274 invite = assert(invites.create_account(nil, { roles = roles }));
275 end 312 end
276 313
277 print(invite.landing_page or invite.uri); 314 print(invite.landing_page or invite.uri);
278 end 315 end