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("number").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("number")));
45 } catch (IOException e
) {
46 logger
.debug("Failed to check registered users", e
);
47 throw new IOErrorException("Unable to check if users are registered");
51 if (outputWriter
instanceof JsonWriter
) {
52 final var jsonWriter
= (JsonWriter
) outputWriter
;
54 var jsonUserStatuses
= registered
.entrySet().stream().map(entry
-> {
55 final var number
= entry
.getValue().first();
56 final var uuid
= entry
.getValue().second();
57 return new JsonUserStatus(entry
.getKey(), number
, uuid
== null ?
null : uuid
.toString(), uuid
!= null);
58 }).collect(Collectors
.toList());
60 jsonWriter
.write(jsonUserStatuses
);
62 final var writer
= (PlainTextWriter
) outputWriter
;
64 for (var entry
: registered
.entrySet()) {
65 writer
.println("{}: {}", entry
.getKey(), entry
.getValue());
70 private static final class JsonUserStatus
{
72 public final String name
;
74 public final String number
;
76 public final String uuid
;
78 public final boolean isRegistered
;
80 public JsonUserStatus(String name
, String number
, String uuid
, boolean isRegistered
) {
84 this.isRegistered
= isRegistered
;