Added the isRegistered command

This commit is contained in:
david-harley 2020-12-24 15:21:20 +10:30
parent 67f62947c6
commit 938b2a2b9d
4 changed files with 93 additions and 0 deletions

View file

@ -124,6 +124,15 @@ 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
Checks if a list of users are registered on Signal or not.
[NUMBER [NUMBER ...]]::
One or more numbers to check.
*--json*::
Output received messages in json format, one object per line.
=== send === send
Send a message to another user or group. Send a message to another user or group.

View file

@ -11,6 +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("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

@ -0,0 +1,67 @@
package org.asamk.signal.commands;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import net.sourceforge.argparse4j.impl.Arguments;
import net.sourceforge.argparse4j.inf.Namespace;
import net.sourceforge.argparse4j.inf.Subparser;
import org.asamk.signal.manager.Manager;
import java.io.IOException;
public class IsRegisteredCommand implements LocalCommand {
@Override
public void attachToSubparser(final Subparser subparser) {
subparser.addArgument("number").help("Phone number").nargs("+");
subparser.help("Check if the specified phone number/s have been registered");
subparser.addArgument("--json")
.help("Output received messages in json format, one json object per line.")
.action(Arguments.storeTrue());
}
@Override
public int handleCommand(final Namespace ns, final Manager m) {
if (!m.isRegistered()) {
System.err.println("User is not registered.");
return 1;
}
ObjectMapper jsonProcessor = new ObjectMapper();
jsonProcessor.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); // disable autodetect
jsonProcessor.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
jsonProcessor.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
for (String phone_number : ns.<String>getList("number")) {
try {
boolean isRegistered = m.isUserRegistered(phone_number);
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) {
System.err.println(e.getMessage());
}
}
return 0;
}
private class JsonIsReceived {
String name;
boolean isRegistered;
public JsonIsReceived(String name, boolean isRegistered) {
this.name = name;
this.isRegistered = isRegistered;
}
}
}

View file

@ -345,6 +345,22 @@ public class Manager implements Closeable {
return account.isRegistered(); return account.isRegistered();
} }
/**
* This is used for checking if someone else's number is registered on Signal
*
* @param number The phone number in question
* @return True if registered, false otherwise
* @throws IOException if its unable to check if the user is registered
*/
public boolean isUserRegistered(String number) throws IOException {
ContactTokenDetails details = this.accountManager.getContact(number).orNull();
if (details == null) {
return false;
}
return true;
}
public void register(boolean voiceVerification, String captcha) throws IOException { public void register(boolean voiceVerification, String captcha) throws IOException {
account.setPassword(KeyUtils.createPassword()); account.setPassword(KeyUtils.createPassword());