Added AsamK's suggestions

This commit is contained in:
david-harley 2021-01-01 21:14:37 +10:30
parent bb7622ea89
commit b585bee61a
4 changed files with 52 additions and 21 deletions

View file

@ -124,14 +124,14 @@ Only works, if this is the master device.
Specify the device you want to remove. Specify the device you want to remove.
Use listDevices to see the deviceIds. Use listDevices to see the deviceIds.
=== isRegistered === getUserStatus
Checks if a list of phone numbers are registered on Signal or not. Uses a list of phone numbers to determine the statuses of those users. Shows if they are registered on the Signal Servers or not.
[NUMBER [NUMBER ...]]:: [NUMBER [NUMBER ...]]::
One or more numbers to check. One or more numbers to check.
*--json*:: *--json*::
Output number registration statuses in json format, one object per line. Output the statuses as an array of json objects.
=== send === send

View file

@ -11,7 +11,7 @@ public class Commands {
addCommand("addDevice", new AddDeviceCommand()); addCommand("addDevice", new AddDeviceCommand());
addCommand("block", new BlockCommand()); addCommand("block", new BlockCommand());
addCommand("daemon", new DaemonCommand()); addCommand("daemon", new DaemonCommand());
addCommand("isRegistered", new IsRegisteredCommand()); addCommand("getUserStatus", new GetUserStatusCommand());
addCommand("link", new LinkCommand()); addCommand("link", new LinkCommand());
addCommand("listContacts", new ListContactsCommand()); addCommand("listContacts", new ListContactsCommand());
addCommand("listDevices", new ListDevicesCommand()); addCommand("listDevices", new ListDevicesCommand());

View file

@ -13,8 +13,13 @@ import net.sourceforge.argparse4j.inf.Subparser;
import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.Manager;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import java.util.Map;
import java.util.List;
public class IsRegisteredCommand implements LocalCommand { public class GetUserStatusCommand implements LocalCommand {
@Override @Override
public void attachToSubparser(final Subparser subparser) { public void attachToSubparser(final Subparser subparser) {
@ -32,33 +37,48 @@ public class IsRegisteredCommand implements LocalCommand {
return 1; return 1;
} }
// Setup the json object mapper
ObjectMapper jsonProcessor = new ObjectMapper(); ObjectMapper jsonProcessor = new ObjectMapper();
jsonProcessor.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); // disable autodetect jsonProcessor.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); // disable autodetect
jsonProcessor.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); jsonProcessor.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
jsonProcessor.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET); jsonProcessor.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
for (String phone_number : ns.<String>getList("number")) { // Get a map of registration statuses
Map<String, Boolean> registered;
try {
registered = m.areUsersRegistered(new HashSet<>(ns.<String>getList("number")));
} catch (IOException e) {
System.err.println("Unable to check if users are registered");
return 1;
}
// Output
if (ns.getBoolean("json")) {
List<JsonIsRegistered> objects = new ArrayList<>();
for (Map.Entry<String, Boolean> entry : registered.entrySet()) {
objects.add(new JsonIsRegistered(entry.getKey(), entry.getValue()));
}
try { try {
boolean isRegistered = m.isUserRegistered(phone_number); System.out.println(jsonProcessor.writeValueAsString(objects));
if (ns.getBoolean("json")) {
JsonIsReceived object = new JsonIsReceived(phone_number, isRegistered);
System.out.println(jsonProcessor.writeValueAsString(object));
} else {
System.out.println(phone_number + ": " + isRegistered);
}
} catch (IOException e) { } catch (IOException e) {
System.err.println(e.getMessage()); System.err.println(e.getMessage());
} }
} else {
for (Map.Entry<String, Boolean> entry : registered.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
} }
return 0; return 0;
} }
private class JsonIsReceived { private class JsonIsRegistered {
String name; String name;
boolean isRegistered; boolean isRegistered;
public JsonIsReceived(String name, boolean isRegistered) { public JsonIsRegistered(String name, boolean isRegistered) {
this.name = name; this.name = name;
this.isRegistered = isRegistered; this.isRegistered = isRegistered;
} }

View file

@ -346,14 +346,25 @@ public class Manager implements Closeable {
} }
/** /**
* This is used for checking if someone else's number is registered on Signal * This is used for checking a set of phone numbers for registration on Signal
* *
* @param number The phone number in question * @param numbers The set of phone number in question
* @return True if registered, false otherwise * @return A map of numbers to booleans. True if registered, false otherwise. Should never be null
* @throws IOException if its unable to check if the user is registered * @throws IOException if its unable to check if the users are registered
*/ */
public boolean isUserRegistered(String number) throws IOException { public Map<String, Boolean> areUsersRegistered(Set<String> numbers) throws IOException {
return this.accountManager.getContact(number).orNull() != null; // Note "contactDetails" has no optionals. It only gives us info on users who are registered
List<ContactTokenDetails> contactDetails = this.accountManager.getContacts(numbers);
// Make the initial map with all numbers set to false for now
Map<String, Boolean> usersRegistered = numbers.stream().collect(Collectors.toMap(x -> x, x -> false));
// Override the contacts we did obtain
for (ContactTokenDetails contactDetail : contactDetails) {
usersRegistered.put(contactDetail.getNumber(), true);
}
return usersRegistered;
} }
public void register(boolean voiceVerification, String captcha) throws IOException { public void register(boolean voiceVerification, String captcha) throws IOException {