1 package org
.asamk
.signal
.commands
;
3 import net
.sourceforge
.argparse4j
.inf
.Namespace
;
4 import net
.sourceforge
.argparse4j
.inf
.Subparser
;
6 import org
.asamk
.signal
.JsonWriter
;
7 import org
.asamk
.signal
.OutputWriter
;
8 import org
.asamk
.signal
.PlainTextWriter
;
9 import org
.asamk
.signal
.commands
.exceptions
.CommandException
;
10 import org
.asamk
.signal
.commands
.exceptions
.IOErrorException
;
11 import org
.asamk
.signal
.manager
.Manager
;
12 import org
.slf4j
.Logger
;
13 import org
.slf4j
.LoggerFactory
;
14 import org
.whispersystems
.libsignal
.util
.Pair
;
16 import java
.io
.IOException
;
17 import java
.util
.HashSet
;
19 import java
.util
.UUID
;
20 import java
.util
.stream
.Collectors
;
22 public class GetUserStatusCommand
implements JsonRpcLocalCommand
{
24 private final static Logger logger
= LoggerFactory
.getLogger(GetUserStatusCommand
.class);
27 public String
getName() {
28 return "getUserStatus";
32 public void attachToSubparser(final Subparser subparser
) {
33 subparser
.help("Check if the specified phone number/s have been registered");
34 subparser
.addArgument("recipient").help("Phone number").nargs("+");
38 public void handleCommand(
39 final Namespace ns
, final Manager m
, final OutputWriter outputWriter
40 ) throws CommandException
{
41 // Get a map of registration statuses
42 Map
<String
, Pair
<String
, UUID
>> registered
;
44 registered
= m
.areUsersRegistered(new HashSet
<>(ns
.getList("recipient")));
45 } catch (IOException e
) {
46 throw new IOErrorException("Unable to check if users are registered", e
);
50 if (outputWriter
instanceof JsonWriter jsonWriter
) {
52 var jsonUserStatuses
= registered
.entrySet().stream().map(entry
-> {
53 final var number
= entry
.getValue().first();
54 final var uuid
= entry
.getValue().second();
55 return new JsonUserStatus(entry
.getKey(), number
, uuid
== null ?
null : uuid
.toString(), uuid
!= null);
56 }).collect(Collectors
.toList());
58 jsonWriter
.write(jsonUserStatuses
);
60 final var writer
= (PlainTextWriter
) outputWriter
;
62 for (var entry
: registered
.entrySet()) {
63 final var uuid
= entry
.getValue().second();
64 writer
.println("{}: {}", entry
.getKey(), uuid
!= null);
69 private static final class JsonUserStatus
{
71 public final String recipient
;
73 public final String number
;
75 public final String uuid
;
77 public final boolean isRegistered
;
79 public JsonUserStatus(String recipient
, String number
, String uuid
, boolean isRegistered
) {
80 this.recipient
= recipient
;
83 this.isRegistered
= isRegistered
;