Mercurial > prosody-hg
comparison plugins/mod_invites.lua @ 13674:1ba58459c919 13.0
mod_invites: Add shell commands to list, show and delete pending invitations
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Thu, 13 Feb 2025 13:04:37 +0000 |
| parents | a186f1cfae75 |
| children | 1aa7efabeacb |
comparison
equal
deleted
inserted
replaced
| 13673:a186f1cfae75 | 13674:1ba58459c919 |
|---|---|
| 287 if not invite then return nil, err; end | 287 if not invite then return nil, err; end |
| 288 return true, invite.landing_page or invite.uri; | 288 return true, invite.landing_page or invite.uri; |
| 289 end; | 289 end; |
| 290 }); | 290 }); |
| 291 | 291 |
| 292 module:add_item("shell-command", { | |
| 293 section = "invite"; | |
| 294 section_desc = "Create and manage invitations"; | |
| 295 name = "show"; | |
| 296 desc = "Show details of an account invitation token"; | |
| 297 args = { { name = "host", type = "string" }, { name = "token", type = "string" } }; | |
| 298 host_selector = "host"; | |
| 299 | |
| 300 handler = function (self, host, token) --luacheck: ignore 212/self 212/host | |
| 301 local invite, err = get_account_invite_info(token); | |
| 302 if not invite then return nil, err; end | |
| 303 | |
| 304 local print = self.session.print; | |
| 305 | |
| 306 if invite.type == "roster" then | |
| 307 print("Invitation to register and become a contact of "..invite.jid); | |
| 308 elseif invite.type == "register" then | |
| 309 local jid_user, jid_host = jid_split(invite.jid); | |
| 310 if invite.additional_data and invite.additional_data.allow_reset then | |
| 311 print("Password reset for "..invite.additional_data.allow_reset.."@"..jid_host); | |
| 312 elseif jid_user then | |
| 313 print("Invitation to register on "..jid_host.." with username '"..jid_user.."'"); | |
| 314 else | |
| 315 print("Invitation to register on "..jid_host); | |
| 316 end | |
| 317 else | |
| 318 print("Unknown invitation type"); | |
| 319 end | |
| 320 | |
| 321 if invite.inviter then | |
| 322 print("Creator:", invite.inviter); | |
| 323 end | |
| 324 | |
| 325 print("Created:", os.date("%Y-%m-%d %T", invite.created_at)); | |
| 326 print("Expires:", os.date("%Y-%m-%d %T", invite.expires)); | |
| 327 | |
| 328 print(""); | |
| 329 | |
| 330 if invite.uri then | |
| 331 print("XMPP URI:", invite.uri); | |
| 332 end | |
| 333 | |
| 334 if invite.landing_page then | |
| 335 print("Web link:", invite.landing_page); | |
| 336 end | |
| 337 | |
| 338 if invite.additional_data then | |
| 339 print(""); | |
| 340 if invite.additional_data.roles then | |
| 341 if invite.additional_data.roles[1] then | |
| 342 print("Role:", invite.additional_data.roles[1]); | |
| 343 end | |
| 344 if invite.additional_data.roles[2] then | |
| 345 print("Secondary roles:", table.concat(invite.additional_data.roles, ", ", 2, #invite.additional_data.roles)); | |
| 346 end | |
| 347 end | |
| 348 if invite.additional_data.groups then | |
| 349 print("Groups:", table.concat(invite.additional_data.groups, ", ")); | |
| 350 end | |
| 351 if invite.additional_data.note then | |
| 352 print("Comment:", invite.additional_data.note); | |
| 353 end | |
| 354 end | |
| 355 | |
| 356 return true, "Invitation valid"; | |
| 357 end; | |
| 358 }); | |
| 359 | |
| 360 module:add_item("shell-command", { | |
| 361 section = "invite"; | |
| 362 section_desc = "Create and manage invitations"; | |
| 363 name = "delete"; | |
| 364 desc = "Delete/revoke an invitation token"; | |
| 365 args = { { name = "host", type = "string" }, { name = "token", type = "string" } }; | |
| 366 host_selector = "host"; | |
| 367 | |
| 368 handler = function (self, host, token) --luacheck: ignore 212/self 212/host | |
| 369 local invite, err = delete_account_invite(token); | |
| 370 if not invite then return nil, err; end | |
| 371 return true, "Invitation deleted"; | |
| 372 end; | |
| 373 }); | |
| 374 | |
| 375 module:add_item("shell-command", { | |
| 376 section = "invite"; | |
| 377 section_desc = "Create and manage invitations"; | |
| 378 name = "list"; | |
| 379 desc = "List pending invitations which allow account registration"; | |
| 380 args = { { name = "host", type = "string" } }; | |
| 381 host_selector = "host"; | |
| 382 | |
| 383 handler = function (self, host) -- luacheck: ignore 212/host | |
| 384 local print_row = human_io.table({ | |
| 385 { | |
| 386 title = "Token"; | |
| 387 key = "invite"; | |
| 388 width = 24; | |
| 389 mapper = function (invite) | |
| 390 return invite.token; | |
| 391 end; | |
| 392 }; | |
| 393 { | |
| 394 title = "Expires"; | |
| 395 key = "invite"; | |
| 396 width = 20; | |
| 397 mapper = function (invite) | |
| 398 return os.date("%Y-%m-%dT%T", invite.expires); | |
| 399 end; | |
| 400 }; | |
| 401 { | |
| 402 title = "Description"; | |
| 403 key = "invite"; | |
| 404 width = "100%"; | |
| 405 mapper = function (invite) | |
| 406 if invite.type == "roster" then | |
| 407 return "Contact with "..invite.jid; | |
| 408 elseif invite.type == "register" then | |
| 409 local jid_user, jid_host = jid_split(invite.jid); | |
| 410 if invite.additional_data and invite.additional_data.allow_reset then | |
| 411 return "Password reset for "..invite.additional_data.allow_reset.."@"..jid_host; | |
| 412 end | |
| 413 if jid_user then | |
| 414 return "Register on "..jid_host.." with username "..jid_user; | |
| 415 end | |
| 416 return "Register on "..jid_host; | |
| 417 end | |
| 418 end; | |
| 419 }; | |
| 420 }, self.session.width); | |
| 421 | |
| 422 self.session.print(print_row()); | |
| 423 local count = 0; | |
| 424 for _, invite in pending_account_invites() do | |
| 425 count = count + 1; | |
| 426 self.session.print(print_row({ invite = invite })); | |
| 427 end | |
| 428 return true, ("%d pending invites"):format(count); | |
| 429 end; | |
| 430 }); | |
| 431 | |
| 292 local subcommands = {}; | 432 local subcommands = {}; |
| 293 | 433 |
| 294 --- prosodyctl command | 434 --- prosodyctl command |
| 295 function module.command(arg) | 435 function module.command(arg) |
| 296 local opts = argparse.parse(arg, { short_params = { h = "help"; ["?"] = "help" } }); | 436 local opts = argparse.parse(arg, { short_params = { h = "help"; ["?"] = "help" } }); |
