Add CommandException to abstract cli return codes for errors

This commit is contained in:
AsamK 2021-02-22 20:43:08 +01:00
parent c6395b9f35
commit 221d937eec
47 changed files with 538 additions and 572 deletions

View file

@ -14,6 +14,9 @@ import org.asamk.signal.commands.LocalCommand;
import org.asamk.signal.commands.MultiLocalCommand; import org.asamk.signal.commands.MultiLocalCommand;
import org.asamk.signal.commands.ProvisioningCommand; import org.asamk.signal.commands.ProvisioningCommand;
import org.asamk.signal.commands.RegistrationCommand; import org.asamk.signal.commands.RegistrationCommand;
import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.UnexpectedErrorException;
import org.asamk.signal.commands.exceptions.UserErrorException;
import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.NotRegisteredException; import org.asamk.signal.manager.NotRegisteredException;
import org.asamk.signal.manager.ProvisioningManager; import org.asamk.signal.manager.ProvisioningManager;
@ -29,9 +32,8 @@ import org.whispersystems.signalservice.api.util.PhoneNumberFormatter;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
public class App { public class App {
@ -79,18 +81,16 @@ public class App {
this.ns = ns; this.ns = ns;
} }
public int init() { public void init() throws CommandException {
var commandKey = ns.getString("command"); var commandKey = ns.getString("command");
var command = Commands.getCommand(commandKey); var command = Commands.getCommand(commandKey);
if (command == null) { if (command == null) {
logger.error("Command not implemented!"); throw new UserErrorException("Command not implemented!");
return 1;
} }
OutputType outputType = ns.get("output"); OutputType outputType = ns.get("output");
if (!command.getSupportedOutputTypes().contains(outputType)) { if (!command.getSupportedOutputTypes().contains(outputType)) {
logger.error("Command doesn't support output type {}", outputType.toString()); throw new UserErrorException("Command doesn't support output type " + outputType.toString());
return 1;
} }
var username = ns.getString("username"); var username = ns.getString("username");
@ -99,7 +99,8 @@ public class App {
final boolean useDbusSystem = ns.getBoolean("dbus_system"); final boolean useDbusSystem = ns.getBoolean("dbus_system");
if (useDbus || useDbusSystem) { if (useDbus || useDbusSystem) {
// If username is null, it will connect to the default object path // If username is null, it will connect to the default object path
return initDbusClient(command, username, useDbusSystem); initDbusClient(command, username, useDbusSystem);
return;
} }
final File dataPath; final File dataPath;
@ -118,111 +119,102 @@ public class App {
} }
if (!ServiceConfig.isSignalClientAvailable()) { if (!ServiceConfig.isSignalClientAvailable()) {
logger.error("Missing required native library dependency: libsignal-client"); throw new UserErrorException("Missing required native library dependency: libsignal-client");
return 1;
} }
if (command instanceof ProvisioningCommand) { if (command instanceof ProvisioningCommand) {
if (username != null) { if (username != null) {
System.err.println("You cannot specify a username (phone number) when linking"); throw new UserErrorException("You cannot specify a username (phone number) when linking");
return 1;
} }
return handleProvisioningCommand((ProvisioningCommand) command, dataPath, serviceEnvironment); handleProvisioningCommand((ProvisioningCommand) command, dataPath, serviceEnvironment);
return;
} }
if (username == null) { if (username == null) {
var usernames = Manager.getAllLocalUsernames(dataPath); var usernames = Manager.getAllLocalUsernames(dataPath);
if (usernames.size() == 0) { if (usernames.size() == 0) {
System.err.println("No local users found, you first need to register or link an account"); throw new UserErrorException("No local users found, you first need to register or link an account");
return 1;
} }
if (command instanceof MultiLocalCommand) { if (command instanceof MultiLocalCommand) {
return handleMultiLocalCommand((MultiLocalCommand) command, dataPath, serviceEnvironment, usernames); handleMultiLocalCommand((MultiLocalCommand) command, dataPath, serviceEnvironment, usernames);
return;
} }
if (usernames.size() > 1) { if (usernames.size() > 1) {
System.err.println("Multiple users found, you need to specify a username (phone number) with -u"); throw new UserErrorException(
return 1; "Multiple users found, you need to specify a username (phone number) with -u");
} }
username = usernames.get(0); username = usernames.get(0);
} else if (!PhoneNumberFormatter.isValidNumber(username, null)) { } else if (!PhoneNumberFormatter.isValidNumber(username, null)) {
System.err.println("Invalid username (phone number), make sure you include the country code."); throw new UserErrorException("Invalid username (phone number), make sure you include the country code.");
return 1;
} }
if (command instanceof RegistrationCommand) { if (command instanceof RegistrationCommand) {
return handleRegistrationCommand((RegistrationCommand) command, username, dataPath, serviceEnvironment); handleRegistrationCommand((RegistrationCommand) command, username, dataPath, serviceEnvironment);
return;
} }
if (!(command instanceof LocalCommand)) { if (!(command instanceof LocalCommand)) {
System.err.println("Command only works via dbus"); throw new UserErrorException("Command only works via dbus");
return 1;
} }
return handleLocalCommand((LocalCommand) command, username, dataPath, serviceEnvironment); handleLocalCommand((LocalCommand) command, username, dataPath, serviceEnvironment);
} }
private int handleProvisioningCommand( private void handleProvisioningCommand(
final ProvisioningCommand command, final File dataPath, final ServiceEnvironment serviceEnvironment final ProvisioningCommand command, final File dataPath, final ServiceEnvironment serviceEnvironment
) { ) throws CommandException {
var pm = ProvisioningManager.init(dataPath, serviceEnvironment, BaseConfig.USER_AGENT); var pm = ProvisioningManager.init(dataPath, serviceEnvironment, BaseConfig.USER_AGENT);
return command.handleCommand(ns, pm); command.handleCommand(ns, pm);
} }
private int handleRegistrationCommand( private void handleRegistrationCommand(
final RegistrationCommand command, final RegistrationCommand command,
final String username, final String username,
final File dataPath, final File dataPath,
final ServiceEnvironment serviceEnvironment final ServiceEnvironment serviceEnvironment
) { ) throws CommandException {
final RegistrationManager manager; final RegistrationManager manager;
try { try {
manager = RegistrationManager.init(username, dataPath, serviceEnvironment, BaseConfig.USER_AGENT); manager = RegistrationManager.init(username, dataPath, serviceEnvironment, BaseConfig.USER_AGENT);
} catch (Throwable e) { } catch (Throwable e) {
logger.error("Error loading or creating state file: {}", e.getMessage()); throw new UnexpectedErrorException("Error loading or creating state file: " + e.getMessage());
return 2;
} }
try (var m = manager) { try (var m = manager) {
return command.handleCommand(ns, m); command.handleCommand(ns, m);
} catch (IOException e) { } catch (IOException e) {
logger.error("Cleanup failed", e); logger.warn("Cleanup failed", e);
return 2;
} }
} }
private int handleLocalCommand( private void handleLocalCommand(
final LocalCommand command, final LocalCommand command,
final String username, final String username,
final File dataPath, final File dataPath,
final ServiceEnvironment serviceEnvironment final ServiceEnvironment serviceEnvironment
) { ) throws CommandException {
try (var m = loadManager(username, dataPath, serviceEnvironment)) { try (var m = loadManager(username, dataPath, serviceEnvironment)) {
if (m == null) { command.handleCommand(ns, m);
return 2;
}
return command.handleCommand(ns, m);
} catch (IOException e) { } catch (IOException e) {
logger.error("Cleanup failed", e); logger.warn("Cleanup failed", e);
return 2;
} }
} }
private int handleMultiLocalCommand( private void handleMultiLocalCommand(
final MultiLocalCommand command, final MultiLocalCommand command,
final File dataPath, final File dataPath,
final ServiceEnvironment serviceEnvironment, final ServiceEnvironment serviceEnvironment,
final List<String> usernames final List<String> usernames
) { ) throws CommandException {
final var managers = usernames.stream() final var managers = new ArrayList<Manager>();
.map(u -> loadManager(u, dataPath, serviceEnvironment)) for (String u : usernames) {
.filter(Objects::nonNull) managers.add(loadManager(u, dataPath, serviceEnvironment));
.collect(Collectors.toList()); }
var result = command.handleCommand(ns, managers); command.handleCommand(ns, managers);
for (var m : managers) { for (var m : managers) {
try { try {
@ -231,34 +223,32 @@ public class App {
logger.warn("Cleanup failed", e); logger.warn("Cleanup failed", e);
} }
} }
return result;
} }
private Manager loadManager( private Manager loadManager(
final String username, final File dataPath, final ServiceEnvironment serviceEnvironment final String username, final File dataPath, final ServiceEnvironment serviceEnvironment
) { ) throws CommandException {
Manager manager; Manager manager;
try { try {
manager = Manager.init(username, dataPath, serviceEnvironment, BaseConfig.USER_AGENT); manager = Manager.init(username, dataPath, serviceEnvironment, BaseConfig.USER_AGENT);
} catch (NotRegisteredException e) { } catch (NotRegisteredException e) {
logger.error("User " + username + " is not registered."); throw new UserErrorException("User " + username + " is not registered.");
return null;
} catch (Throwable e) { } catch (Throwable e) {
logger.error("Error loading state file for user " + username + ": {}", e.getMessage()); throw new UnexpectedErrorException("Error loading state file for user " + username + ": " + e.getMessage());
return null;
} }
try { try {
manager.checkAccountState(); manager.checkAccountState();
} catch (IOException e) { } catch (IOException e) {
logger.error("Error while checking account " + username + ": {}", e.getMessage()); throw new UnexpectedErrorException("Error while checking account " + username + ": " + e.getMessage());
return null;
} }
return manager; return manager;
} }
private int initDbusClient(final Command command, final String username, final boolean systemBus) { private void initDbusClient(
final Command command, final String username, final boolean systemBus
) throws CommandException {
try { try {
DBusConnection.DBusBusType busType; DBusConnection.DBusBusType busType;
if (systemBus) { if (systemBus) {
@ -271,22 +261,21 @@ public class App {
DbusConfig.getObjectPath(username), DbusConfig.getObjectPath(username),
Signal.class); Signal.class);
return handleCommand(command, ts, dBusConn); handleCommand(command, ts, dBusConn);
} }
} catch (DBusException | IOException e) { } catch (DBusException | IOException e) {
logger.error("Dbus client failed", e); logger.error("Dbus client failed", e);
return 2; throw new UnexpectedErrorException("Dbus client failed");
} }
} }
private int handleCommand(Command command, Signal ts, DBusConnection dBusConn) { private void handleCommand(Command command, Signal ts, DBusConnection dBusConn) throws CommandException {
if (command instanceof ExtendedDbusCommand) { if (command instanceof ExtendedDbusCommand) {
return ((ExtendedDbusCommand) command).handleCommand(ns, ts, dBusConn); ((ExtendedDbusCommand) command).handleCommand(ns, ts, dBusConn);
} else if (command instanceof DbusCommand) { } else if (command instanceof DbusCommand) {
return ((DbusCommand) command).handleCommand(ns, ts); ((DbusCommand) command).handleCommand(ns, ts);
} else { } else {
System.err.println("Command is not yet implemented via dbus"); throw new UserErrorException("Command is not yet implemented via dbus");
return 1;
} }
} }

View file

@ -8,7 +8,6 @@ import org.slf4j.LoggerFactory;
import org.whispersystems.signalservice.api.messages.SignalServiceContent; import org.whispersystems.signalservice.api.messages.SignalServiceContent;
import org.whispersystems.signalservice.api.messages.SignalServiceEnvelope; import org.whispersystems.signalservice.api.messages.SignalServiceEnvelope;
import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
public class JsonReceiveMessageHandler implements Manager.ReceiveMessageHandler { public class JsonReceiveMessageHandler implements Manager.ReceiveMessageHandler {
@ -32,10 +31,7 @@ public class JsonReceiveMessageHandler implements Manager.ReceiveMessageHandler
if (envelope != null) { if (envelope != null) {
object.put("envelope", new JsonMessageEnvelope(envelope, content, m)); object.put("envelope", new JsonMessageEnvelope(envelope, content, m));
} }
try {
jsonWriter.write(object); jsonWriter.write(object);
} catch (IOException e) {
logger.error("Failed to write json object: {}", e.getMessage());
}
} }
} }

View file

@ -26,7 +26,8 @@ public class JsonWriter {
objectMapper.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET); objectMapper.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
} }
public void write(final Object object) throws IOException { public void write(final Object object) {
try {
try { try {
objectMapper.writeValue(writer, object); objectMapper.writeValue(writer, object);
} catch (JsonProcessingException e) { } catch (JsonProcessingException e) {
@ -35,5 +36,8 @@ public class JsonWriter {
} }
writer.write(System.lineSeparator()); writer.write(System.lineSeparator());
writer.flush(); writer.flush();
} catch (IOException e) {
throw new AssertionError(e);
}
} }
} }

View file

@ -21,6 +21,11 @@ import net.sourceforge.argparse4j.impl.Arguments;
import net.sourceforge.argparse4j.inf.ArgumentParserException; import net.sourceforge.argparse4j.inf.ArgumentParserException;
import net.sourceforge.argparse4j.inf.Namespace; import net.sourceforge.argparse4j.inf.Namespace;
import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.IOErrorException;
import org.asamk.signal.commands.exceptions.UnexpectedErrorException;
import org.asamk.signal.commands.exceptions.UntrustedKeyErrorException;
import org.asamk.signal.commands.exceptions.UserErrorException;
import org.asamk.signal.manager.LibSignalLogger; import org.asamk.signal.manager.LibSignalLogger;
import org.asamk.signal.util.SecurityProvider; import org.asamk.signal.util.SecurityProvider;
import org.bouncycastle.jce.provider.BouncyCastleProvider; import org.bouncycastle.jce.provider.BouncyCastleProvider;
@ -39,8 +44,14 @@ public class Main {
var ns = parser.parseArgsOrFail(args); var ns = parser.parseArgsOrFail(args);
var res = new App(ns).init(); int status = 0;
System.exit(res); try {
new App(ns).init();
} catch (CommandException e) {
System.err.println(e.getMessage());
status = getStatusForError(e);
}
System.exit(status);
} }
private static void installSecurityProviderWorkaround() { private static void installSecurityProviderWorkaround() {
@ -78,4 +89,18 @@ public class Main {
System.setProperty("org.slf4j.simpleLogger.showDateTime", "false"); System.setProperty("org.slf4j.simpleLogger.showDateTime", "false");
} }
} }
private static int getStatusForError(final CommandException e) {
if (e instanceof UserErrorException) {
return 1;
} else if (e instanceof UnexpectedErrorException) {
return 2;
} else if (e instanceof IOErrorException) {
return 3;
} else if (e instanceof UntrustedKeyErrorException) {
return 4;
} else {
return 2;
}
}
} }

View file

@ -1,23 +1,21 @@
package org.asamk.signal; package org.asamk.signal;
import java.io.IOException;
public interface PlainTextWriter { public interface PlainTextWriter {
void println(String format, Object... args) throws IOException; void println(String format, Object... args);
PlainTextWriter indentedWriter(); PlainTextWriter indentedWriter();
default void println() throws IOException { default void println() {
println(""); println("");
} }
default void indent(final WriterConsumer subWriter) throws IOException { default void indent(final WriterConsumer subWriter) {
subWriter.consume(indentedWriter()); subWriter.consume(indentedWriter());
} }
interface WriterConsumer { interface WriterConsumer {
void consume(PlainTextWriter writer) throws IOException; void consume(PlainTextWriter writer);
} }
} }

View file

@ -19,12 +19,16 @@ public final class PlainTextWriterImpl implements PlainTextWriter {
} }
@Override @Override
public void println(String format, Object... args) throws IOException { public void println(String format, Object... args) {
final var message = MessageFormatter.arrayFormat(format, args).getMessage(); final var message = MessageFormatter.arrayFormat(format, args).getMessage();
try {
writer.write(message); writer.write(message);
writer.write(System.lineSeparator()); writer.write(System.lineSeparator());
writer.flush(); writer.flush();
} catch (IOException e) {
throw new AssertionError(e);
}
} }
@Override @Override
@ -51,8 +55,12 @@ public final class PlainTextWriterImpl implements PlainTextWriter {
} }
@Override @Override
public void println(final String format, final Object... args) throws IOException { public void println(final String format, final Object... args) {
try {
writer.write(spaces); writer.write(spaces);
} catch (IOException e) {
throw new AssertionError(e);
}
plainTextWriter.println(format, args); plainTextWriter.println(format, args);
} }

View file

@ -630,7 +630,7 @@ public class ReceiveMessageHandler implements Manager.ReceiveMessageHandler {
writer.println("- {}: {} (length: {})", formatContact(address), mention.getStart(), mention.getLength()); writer.println("- {}: {} (length: {})", formatContact(address), mention.getStart(), mention.getLength());
} }
private void printAttachment(PlainTextWriter writer, SignalServiceAttachment attachment) throws IOException { private void printAttachment(PlainTextWriter writer, SignalServiceAttachment attachment) {
writer.println("Content-Type: {}", attachment.getContentType()); writer.println("Content-Type: {}", attachment.getContentType());
writer.println("Type: {}", attachment.isPointer() ? "Pointer" : attachment.isStream() ? "Stream" : "<unknown>"); writer.println("Type: {}", attachment.isPointer() ? "Pointer" : attachment.isStream() ? "Stream" : "<unknown>");
if (attachment.isPointer()) { if (attachment.isPointer()) {

View file

@ -3,7 +3,13 @@ package org.asamk.signal.commands;
import net.sourceforge.argparse4j.inf.Namespace; import net.sourceforge.argparse4j.inf.Namespace;
import net.sourceforge.argparse4j.inf.Subparser; import net.sourceforge.argparse4j.inf.Subparser;
import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.IOErrorException;
import org.asamk.signal.commands.exceptions.UnexpectedErrorException;
import org.asamk.signal.commands.exceptions.UserErrorException;
import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.Manager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.whispersystems.libsignal.InvalidKeyException; import org.whispersystems.libsignal.InvalidKeyException;
import java.io.IOException; import java.io.IOException;
@ -14,6 +20,8 @@ import static org.asamk.signal.util.ErrorUtils.handleAssertionError;
public class AddDeviceCommand implements LocalCommand { public class AddDeviceCommand implements LocalCommand {
private final static Logger logger = LoggerFactory.getLogger(AddDeviceCommand.class);
@Override @Override
public void attachToSubparser(final Subparser subparser) { public void attachToSubparser(final Subparser subparser) {
subparser.addArgument("--uri") subparser.addArgument("--uri")
@ -22,19 +30,20 @@ public class AddDeviceCommand implements LocalCommand {
} }
@Override @Override
public int handleCommand(final Namespace ns, final Manager m) { public void handleCommand(final Namespace ns, final Manager m) throws CommandException {
try { try {
m.addDeviceLink(new URI(ns.getString("uri"))); m.addDeviceLink(new URI(ns.getString("uri")));
return 0;
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); logger.error("Add device link failed", e);
return 3; throw new IOErrorException("Add device link failed");
} catch (InvalidKeyException | URISyntaxException e) { } catch (URISyntaxException e) {
e.printStackTrace(); throw new UserErrorException("Device link uri has invalid format: {}" + e.getMessage());
return 2; } catch (InvalidKeyException e) {
logger.error("Add device link failed", e);
throw new UnexpectedErrorException("Add device link failed.");
} catch (AssertionError e) { } catch (AssertionError e) {
handleAssertionError(e); handleAssertionError(e);
return 1; throw e;
} }
} }
} }

View file

@ -7,10 +7,14 @@ import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.groups.GroupIdFormatException; import org.asamk.signal.manager.groups.GroupIdFormatException;
import org.asamk.signal.manager.groups.GroupNotFoundException; import org.asamk.signal.manager.groups.GroupNotFoundException;
import org.asamk.signal.util.Util; import org.asamk.signal.util.Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.whispersystems.signalservice.api.util.InvalidNumberException; import org.whispersystems.signalservice.api.util.InvalidNumberException;
public class BlockCommand implements LocalCommand { public class BlockCommand implements LocalCommand {
private final static Logger logger = LoggerFactory.getLogger(BlockCommand.class);
@Override @Override
public void attachToSubparser(final Subparser subparser) { public void attachToSubparser(final Subparser subparser) {
subparser.addArgument("contact").help("Contact number").nargs("*"); subparser.addArgument("contact").help("Contact number").nargs("*");
@ -19,12 +23,12 @@ public class BlockCommand implements LocalCommand {
} }
@Override @Override
public int handleCommand(final Namespace ns, final Manager m) { public void handleCommand(final Namespace ns, final Manager m) {
for (var contact_number : ns.<String>getList("contact")) { for (var contact_number : ns.<String>getList("contact")) {
try { try {
m.setContactBlocked(contact_number, true); m.setContactBlocked(contact_number, true);
} catch (InvalidNumberException e) { } catch (InvalidNumberException e) {
System.err.println(e.getMessage()); logger.warn("Invalid number {}: {}", contact_number, e.getMessage());
} }
} }
@ -34,11 +38,9 @@ public class BlockCommand implements LocalCommand {
var groupId = Util.decodeGroupId(groupIdString); var groupId = Util.decodeGroupId(groupIdString);
m.setGroupBlocked(groupId, true); m.setGroupBlocked(groupId, true);
} catch (GroupIdFormatException | GroupNotFoundException e) { } catch (GroupIdFormatException | GroupNotFoundException e) {
System.err.println(e.getMessage()); logger.warn("Invalid group id {}: {}", groupIdString, e.getMessage());
} }
} }
} }
return 0;
} }
} }

View file

@ -8,6 +8,8 @@ import org.asamk.signal.DbusConfig;
import org.asamk.signal.DbusReceiveMessageHandler; import org.asamk.signal.DbusReceiveMessageHandler;
import org.asamk.signal.JsonDbusReceiveMessageHandler; import org.asamk.signal.JsonDbusReceiveMessageHandler;
import org.asamk.signal.OutputType; import org.asamk.signal.OutputType;
import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.UnexpectedErrorException;
import org.asamk.signal.dbus.DbusSignalImpl; import org.asamk.signal.dbus.DbusSignalImpl;
import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.Manager;
import org.freedesktop.dbus.connections.impl.DBusConnection; import org.freedesktop.dbus.connections.impl.DBusConnection;
@ -44,7 +46,7 @@ public class DaemonCommand implements MultiLocalCommand {
} }
@Override @Override
public int handleCommand(final Namespace ns, final Manager m) { public void handleCommand(final Namespace ns, final Manager m) throws CommandException {
var inJson = ns.get("output") == OutputType.JSON || ns.getBoolean("json"); var inJson = ns.get("output") == OutputType.JSON || ns.getBoolean("json");
// TODO delete later when "json" variable is removed // TODO delete later when "json" variable is removed
@ -71,15 +73,14 @@ public class DaemonCommand implements MultiLocalCommand {
t.join(); t.join();
} catch (InterruptedException ignored) { } catch (InterruptedException ignored) {
} }
return 0;
} catch (DBusException | IOException e) { } catch (DBusException | IOException e) {
logger.error("Dbus command failed", e); logger.error("Dbus command failed", e);
return 2; throw new UnexpectedErrorException("Dbus command failed");
} }
} }
@Override @Override
public int handleCommand(final Namespace ns, final List<Manager> managers) { public void handleCommand(final Namespace ns, final List<Manager> managers) throws CommandException {
var inJson = ns.get("output") == OutputType.JSON || ns.getBoolean("json"); var inJson = ns.get("output") == OutputType.JSON || ns.getBoolean("json");
// TODO delete later when "json" variable is removed // TODO delete later when "json" variable is removed
@ -112,10 +113,9 @@ public class DaemonCommand implements MultiLocalCommand {
} catch (InterruptedException ignored) { } catch (InterruptedException ignored) {
} }
} }
return 0;
} catch (DBusException | IOException e) { } catch (DBusException | IOException e) {
logger.error("Dbus command failed", e); logger.error("Dbus command failed", e);
return 2; throw new UnexpectedErrorException("Dbus command failed");
} }
} }

View file

@ -3,14 +3,15 @@ package org.asamk.signal.commands;
import net.sourceforge.argparse4j.inf.Namespace; import net.sourceforge.argparse4j.inf.Namespace;
import org.asamk.Signal; import org.asamk.Signal;
import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.dbus.DbusSignalImpl; import org.asamk.signal.dbus.DbusSignalImpl;
import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.Manager;
public interface DbusCommand extends LocalCommand { public interface DbusCommand extends LocalCommand {
int handleCommand(Namespace ns, Signal signal); void handleCommand(Namespace ns, Signal signal) throws CommandException;
default int handleCommand(final Namespace ns, final Manager m) { default void handleCommand(final Namespace ns, final Manager m) throws CommandException {
return handleCommand(ns, new DbusSignalImpl(m)); handleCommand(ns, new DbusSignalImpl(m));
} }
} }

View file

@ -3,9 +3,10 @@ package org.asamk.signal.commands;
import net.sourceforge.argparse4j.inf.Namespace; import net.sourceforge.argparse4j.inf.Namespace;
import org.asamk.Signal; import org.asamk.Signal;
import org.asamk.signal.commands.exceptions.CommandException;
import org.freedesktop.dbus.connections.impl.DBusConnection; import org.freedesktop.dbus.connections.impl.DBusConnection;
public interface ExtendedDbusCommand extends Command { public interface ExtendedDbusCommand extends Command {
int handleCommand(Namespace ns, Signal signal, DBusConnection dbusconnection); void handleCommand(Namespace ns, Signal signal, DBusConnection dbusconnection) throws CommandException;
} }

View file

@ -7,6 +7,8 @@ import net.sourceforge.argparse4j.inf.Subparser;
import org.asamk.signal.JsonWriter; import org.asamk.signal.JsonWriter;
import org.asamk.signal.OutputType; import org.asamk.signal.OutputType;
import org.asamk.signal.PlainTextWriterImpl; import org.asamk.signal.PlainTextWriterImpl;
import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.IOErrorException;
import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.Manager;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -36,7 +38,7 @@ public class GetUserStatusCommand implements LocalCommand {
} }
@Override @Override
public int handleCommand(final Namespace ns, final Manager m) { public void handleCommand(final Namespace ns, final Manager m) throws CommandException {
// Setup the json object mapper // Setup the json object mapper
var inJson = ns.get("output") == OutputType.JSON || ns.getBoolean("json"); var inJson = ns.get("output") == OutputType.JSON || ns.getBoolean("json");
@ -50,8 +52,8 @@ public class GetUserStatusCommand implements LocalCommand {
try { try {
registered = m.areUsersRegistered(new HashSet<>(ns.getList("number"))); registered = m.areUsersRegistered(new HashSet<>(ns.getList("number")));
} catch (IOException e) { } catch (IOException e) {
System.err.println("Unable to check if users are registered"); logger.debug("Failed to check registered users", e);
return 3; throw new IOErrorException("Unable to check if users are registered");
} }
// Output // Output
@ -63,28 +65,16 @@ public class GetUserStatusCommand implements LocalCommand {
.map(entry -> new JsonUserStatus(entry.getKey(), entry.getValue())) .map(entry -> new JsonUserStatus(entry.getKey(), entry.getValue()))
.collect(Collectors.toList()); .collect(Collectors.toList());
try {
jsonWriter.write(jsonUserStatuses); jsonWriter.write(jsonUserStatuses);
} catch (IOException e) {
logger.error("Failed to write json object: {}", e.getMessage());
return 3;
}
} else { } else {
final var writer = new PlainTextWriterImpl(System.out); final var writer = new PlainTextWriterImpl(System.out);
try {
for (var entry : registered.entrySet()) { for (var entry : registered.entrySet()) {
writer.println("{}: {}", entry.getKey(), entry.getValue()); writer.println("{}: {}", entry.getKey(), entry.getValue());
} }
} catch (IOException e) {
e.printStackTrace();
return 3;
} }
} }
return 0;
}
private static final class JsonUserStatus { private static final class JsonUserStatus {
public String name; public String name;

View file

@ -4,46 +4,42 @@ import net.sourceforge.argparse4j.inf.Namespace;
import net.sourceforge.argparse4j.inf.Subparser; import net.sourceforge.argparse4j.inf.Subparser;
import org.asamk.signal.PlainTextWriterImpl; import org.asamk.signal.PlainTextWriterImpl;
import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.IOErrorException;
import org.asamk.signal.commands.exceptions.UnexpectedErrorException;
import org.asamk.signal.commands.exceptions.UserErrorException;
import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.groups.GroupInviteLinkUrl; import org.asamk.signal.manager.groups.GroupInviteLinkUrl;
import org.freedesktop.dbus.exceptions.DBusExecutionException; import org.freedesktop.dbus.exceptions.DBusExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.whispersystems.signalservice.api.groupsv2.GroupLinkNotActiveException; import org.whispersystems.signalservice.api.groupsv2.GroupLinkNotActiveException;
import org.whispersystems.signalservice.internal.push.exceptions.GroupPatchNotAcceptedException; import org.whispersystems.signalservice.internal.push.exceptions.GroupPatchNotAcceptedException;
import java.io.IOException; import java.io.IOException;
import static org.asamk.signal.util.ErrorUtils.handleAssertionError; import static org.asamk.signal.util.ErrorUtils.handleAssertionError;
import static org.asamk.signal.util.ErrorUtils.handleIOException;
import static org.asamk.signal.util.ErrorUtils.handleTimestampAndSendMessageResults; import static org.asamk.signal.util.ErrorUtils.handleTimestampAndSendMessageResults;
public class JoinGroupCommand implements LocalCommand { public class JoinGroupCommand implements LocalCommand {
private final static Logger logger = LoggerFactory.getLogger(JoinGroupCommand.class);
@Override @Override
public void attachToSubparser(final Subparser subparser) { public void attachToSubparser(final Subparser subparser) {
subparser.addArgument("--uri").required(true).help("Specify the uri with the group invitation link."); subparser.addArgument("--uri").required(true).help("Specify the uri with the group invitation link.");
} }
@Override @Override
public int handleCommand(final Namespace ns, final Manager m) { public void handleCommand(final Namespace ns, final Manager m) throws CommandException {
final GroupInviteLinkUrl linkUrl; final GroupInviteLinkUrl linkUrl;
var uri = ns.getString("uri"); var uri = ns.getString("uri");
try { try {
linkUrl = GroupInviteLinkUrl.fromUri(uri); linkUrl = GroupInviteLinkUrl.fromUri(uri);
} catch (GroupInviteLinkUrl.InvalidGroupLinkException e) { } catch (GroupInviteLinkUrl.InvalidGroupLinkException e) {
System.err.println("Group link is invalid: " + e.getMessage()); throw new UserErrorException("Group link is invalid: " + e.getMessage());
return 1;
} catch (GroupInviteLinkUrl.UnknownGroupLinkVersionException e) { } catch (GroupInviteLinkUrl.UnknownGroupLinkVersionException e) {
System.err.println("Group link was created with an incompatible version: " + e.getMessage()); throw new UserErrorException("Group link was created with an incompatible version: " + e.getMessage());
return 1;
} }
if (linkUrl == null) { if (linkUrl == null) {
System.err.println("Link is not a signal group invitation link"); throw new UserErrorException("Link is not a signal group invitation link");
return 1;
} }
try { try {
@ -56,23 +52,18 @@ public class JoinGroupCommand implements LocalCommand {
} else { } else {
writer.println("Joined group \"{}\"", newGroupId.toBase64()); writer.println("Joined group \"{}\"", newGroupId.toBase64());
} }
return handleTimestampAndSendMessageResults(writer, 0, results.second()); handleTimestampAndSendMessageResults(writer, 0, results.second());
} catch (AssertionError e) { } catch (AssertionError e) {
handleAssertionError(e); handleAssertionError(e);
return 1; throw e;
} catch (GroupPatchNotAcceptedException e) { } catch (GroupPatchNotAcceptedException e) {
System.err.println("Failed to join group, maybe already a member"); throw new UserErrorException("Failed to join group, maybe already a member");
return 1;
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); throw new IOErrorException("Failed to send message: " + e.getMessage());
handleIOException(e);
return 3;
} catch (DBusExecutionException e) { } catch (DBusExecutionException e) {
System.err.println("Failed to send message: " + e.getMessage()); throw new UnexpectedErrorException("Failed to send message: " + e.getMessage());
return 2;
} catch (GroupLinkNotActiveException e) { } catch (GroupLinkNotActiveException e) {
System.err.println("Group link is not valid: " + e.getMessage()); throw new UserErrorException("Group link is not valid: " + e.getMessage());
return 1;
} }
} }
} }

View file

@ -4,6 +4,10 @@ import net.sourceforge.argparse4j.inf.Namespace;
import net.sourceforge.argparse4j.inf.Subparser; import net.sourceforge.argparse4j.inf.Subparser;
import org.asamk.signal.PlainTextWriterImpl; import org.asamk.signal.PlainTextWriterImpl;
import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.IOErrorException;
import org.asamk.signal.commands.exceptions.UnexpectedErrorException;
import org.asamk.signal.commands.exceptions.UserErrorException;
import org.asamk.signal.manager.ProvisioningManager; import org.asamk.signal.manager.ProvisioningManager;
import org.asamk.signal.manager.UserAlreadyExists; import org.asamk.signal.manager.UserAlreadyExists;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -25,7 +29,7 @@ public class LinkCommand implements ProvisioningCommand {
} }
@Override @Override
public int handleCommand(final Namespace ns, final ProvisioningManager m) { public void handleCommand(final Namespace ns, final ProvisioningManager m) throws CommandException {
final var writer = new PlainTextWriterImpl(System.out); final var writer = new PlainTextWriterImpl(System.out);
var deviceName = ns.getString("name"); var deviceName = ns.getString("name");
@ -37,25 +41,21 @@ public class LinkCommand implements ProvisioningCommand {
var username = m.finishDeviceLink(deviceName); var username = m.finishDeviceLink(deviceName);
writer.println("Associated with: {}", username); writer.println("Associated with: {}", username);
} catch (TimeoutException e) { } catch (TimeoutException e) {
System.err.println("Link request timed out, please try again."); throw new UserErrorException("Link request timed out, please try again.");
return 3;
} catch (IOException e) { } catch (IOException e) {
System.err.println("Link request error: " + e.getMessage()); throw new IOErrorException("Link request error: " + e.getMessage());
return 3;
} catch (AssertionError e) { } catch (AssertionError e) {
handleAssertionError(e); handleAssertionError(e);
return 1; throw e;
} catch (InvalidKeyException e) { } catch (InvalidKeyException e) {
e.printStackTrace(); logger.debug("Finish device link failed", e);
return 2; throw new UnexpectedErrorException("Invalid key: " + e.getMessage());
} catch (UserAlreadyExists e) { } catch (UserAlreadyExists e) {
System.err.println("The user " throw new UserErrorException("The user "
+ e.getUsername() + e.getUsername()
+ " already exists\nDelete \"" + " already exists\nDelete \""
+ e.getFileName() + e.getFileName()
+ "\" before trying again."); + "\" before trying again.");
return 1;
} }
return 0;
} }
} }

View file

@ -6,8 +6,6 @@ import net.sourceforge.argparse4j.inf.Subparser;
import org.asamk.signal.PlainTextWriterImpl; import org.asamk.signal.PlainTextWriterImpl;
import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.Manager;
import java.io.IOException;
public class ListContactsCommand implements LocalCommand { public class ListContactsCommand implements LocalCommand {
@Override @Override
@ -15,18 +13,12 @@ public class ListContactsCommand implements LocalCommand {
} }
@Override @Override
public int handleCommand(final Namespace ns, final Manager m) { public void handleCommand(final Namespace ns, final Manager m) {
final var writer = new PlainTextWriterImpl(System.out); final var writer = new PlainTextWriterImpl(System.out);
var contacts = m.getContacts(); var contacts = m.getContacts();
try {
for (var c : contacts) { for (var c : contacts) {
writer.println("Number: {} Name: {} Blocked: {}", c.number, c.name, c.blocked); writer.println("Number: {} Name: {} Blocked: {}", c.number, c.name, c.blocked);
} }
} catch (IOException e) {
e.printStackTrace();
return 3;
}
return 0;
} }
} }

View file

@ -4,22 +4,37 @@ import net.sourceforge.argparse4j.inf.Namespace;
import net.sourceforge.argparse4j.inf.Subparser; import net.sourceforge.argparse4j.inf.Subparser;
import org.asamk.signal.PlainTextWriterImpl; import org.asamk.signal.PlainTextWriterImpl;
import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.IOErrorException;
import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.Manager;
import org.asamk.signal.util.DateUtils; import org.asamk.signal.util.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.whispersystems.signalservice.api.messages.multidevice.DeviceInfo;
import java.io.IOException; import java.io.IOException;
import java.util.List;
public class ListDevicesCommand implements LocalCommand { public class ListDevicesCommand implements LocalCommand {
private final static Logger logger = LoggerFactory.getLogger(ListDevicesCommand.class);
@Override @Override
public void attachToSubparser(final Subparser subparser) { public void attachToSubparser(final Subparser subparser) {
} }
@Override @Override
public int handleCommand(final Namespace ns, final Manager m) { public void handleCommand(final Namespace ns, final Manager m) throws CommandException {
final var writer = new PlainTextWriterImpl(System.out); final var writer = new PlainTextWriterImpl(System.out);
List<DeviceInfo> devices;
try { try {
var devices = m.getLinkedDevices(); devices = m.getLinkedDevices();
} catch (IOException e) {
logger.debug("Failed to get linked devices", e);
throw new IOErrorException("Failed to get linked devices: " + e.getMessage());
}
for (var d : devices) { for (var d : devices) {
writer.println("- Device {}{}:", d.getId(), (d.getId() == m.getDeviceId() ? " (this device)" : "")); writer.println("- Device {}{}:", d.getId(), (d.getId() == m.getDeviceId() ? " (this device)" : ""));
writer.indent(w -> { writer.indent(w -> {
@ -28,10 +43,5 @@ public class ListDevicesCommand implements LocalCommand {
w.println("Last seen: {}", DateUtils.formatTimestamp(d.getLastSeen())); w.println("Last seen: {}", DateUtils.formatTimestamp(d.getLastSeen()));
}); });
} }
return 0;
} catch (IOException e) {
e.printStackTrace();
return 3;
}
} }
} }

View file

@ -8,13 +8,13 @@ import org.asamk.signal.JsonWriter;
import org.asamk.signal.OutputType; import org.asamk.signal.OutputType;
import org.asamk.signal.PlainTextWriter; import org.asamk.signal.PlainTextWriter;
import org.asamk.signal.PlainTextWriterImpl; import org.asamk.signal.PlainTextWriterImpl;
import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.storage.groups.GroupInfo; import org.asamk.signal.manager.storage.groups.GroupInfo;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.whispersystems.signalservice.api.push.SignalServiceAddress; import org.whispersystems.signalservice.api.push.SignalServiceAddress;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -32,7 +32,7 @@ public class ListGroupsCommand implements LocalCommand {
private static void printGroupPlainText( private static void printGroupPlainText(
PlainTextWriter writer, Manager m, GroupInfo group, boolean detailed PlainTextWriter writer, Manager m, GroupInfo group, boolean detailed
) throws IOException { ) {
if (detailed) { if (detailed) {
final var groupInviteLink = group.getGroupInviteLink(); final var groupInviteLink = group.getGroupInviteLink();
@ -70,7 +70,7 @@ public class ListGroupsCommand implements LocalCommand {
} }
@Override @Override
public int handleCommand(final Namespace ns, final Manager m) { public void handleCommand(final Namespace ns, final Manager m) throws CommandException {
if (ns.get("output") == OutputType.JSON) { if (ns.get("output") == OutputType.JSON) {
final var jsonWriter = new JsonWriter(System.out); final var jsonWriter = new JsonWriter(System.out);
@ -88,30 +88,16 @@ public class ListGroupsCommand implements LocalCommand {
groupInviteLink == null ? null : groupInviteLink.getUrl())); groupInviteLink == null ? null : groupInviteLink.getUrl()));
} }
try {
jsonWriter.write(jsonGroups); jsonWriter.write(jsonGroups);
} catch (IOException e) {
logger.error("Failed to write json object: {}", e.getMessage());
return 3;
}
return 0;
} else { } else {
final var writer = new PlainTextWriterImpl(System.out); final var writer = new PlainTextWriterImpl(System.out);
boolean detailed = ns.getBoolean("detailed"); boolean detailed = ns.getBoolean("detailed");
try {
for (var group : m.getGroups()) { for (var group : m.getGroups()) {
printGroupPlainText(writer, m, group, detailed); printGroupPlainText(writer, m, group, detailed);
} }
} catch (IOException e) {
e.printStackTrace();
return 3;
} }
} }
return 0;
}
private static final class JsonGroup { private static final class JsonGroup {
public String id; public String id;

View file

@ -5,6 +5,8 @@ import net.sourceforge.argparse4j.inf.Subparser;
import org.asamk.signal.PlainTextWriter; import org.asamk.signal.PlainTextWriter;
import org.asamk.signal.PlainTextWriterImpl; import org.asamk.signal.PlainTextWriterImpl;
import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.UserErrorException;
import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.storage.protocol.IdentityInfo; import org.asamk.signal.manager.storage.protocol.IdentityInfo;
import org.asamk.signal.util.Hex; import org.asamk.signal.util.Hex;
@ -13,7 +15,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.whispersystems.signalservice.api.util.InvalidNumberException; import org.whispersystems.signalservice.api.util.InvalidNumberException;
import java.io.IOException; import java.util.List;
public class ListIdentitiesCommand implements LocalCommand { public class ListIdentitiesCommand implements LocalCommand {
@ -21,16 +23,12 @@ public class ListIdentitiesCommand implements LocalCommand {
private static void printIdentityFingerprint(PlainTextWriter writer, Manager m, IdentityInfo theirId) { private static void printIdentityFingerprint(PlainTextWriter writer, Manager m, IdentityInfo theirId) {
var digits = Util.formatSafetyNumber(m.computeSafetyNumber(theirId.getAddress(), theirId.getIdentityKey())); var digits = Util.formatSafetyNumber(m.computeSafetyNumber(theirId.getAddress(), theirId.getIdentityKey()));
try {
writer.println("{}: {} Added: {} Fingerprint: {} Safety Number: {}", writer.println("{}: {} Added: {} Fingerprint: {} Safety Number: {}",
theirId.getAddress().getNumber().orNull(), theirId.getAddress().getNumber().orNull(),
theirId.getTrustLevel(), theirId.getTrustLevel(),
theirId.getDateAdded(), theirId.getDateAdded(),
Hex.toString(theirId.getFingerprint()), Hex.toString(theirId.getFingerprint()),
digits); digits);
} catch (IOException e) {
e.printStackTrace();
}
} }
@Override @Override
@ -39,24 +37,27 @@ public class ListIdentitiesCommand implements LocalCommand {
} }
@Override @Override
public int handleCommand(final Namespace ns, final Manager m) { public void handleCommand(final Namespace ns, final Manager m) throws CommandException {
final var writer = new PlainTextWriterImpl(System.out); final var writer = new PlainTextWriterImpl(System.out);
if (ns.get("number") == null) { var number = ns.getString("number");
if (number == null) {
for (var identity : m.getIdentities()) { for (var identity : m.getIdentities()) {
printIdentityFingerprint(writer, m, identity); printIdentityFingerprint(writer, m, identity);
} }
} else { return;
var number = ns.getString("number"); }
List<IdentityInfo> identities;
try { try {
var identities = m.getIdentities(number); identities = m.getIdentities(number);
} catch (InvalidNumberException e) {
throw new UserErrorException("Invalid number: " + e.getMessage());
}
for (var id : identities) { for (var id : identities) {
printIdentityFingerprint(writer, m, id); printIdentityFingerprint(writer, m, id);
} }
} catch (InvalidNumberException e) {
System.err.println("Invalid number: " + e.getMessage());
}
}
return 0;
} }
} }

View file

@ -2,9 +2,10 @@ package org.asamk.signal.commands;
import net.sourceforge.argparse4j.inf.Namespace; import net.sourceforge.argparse4j.inf.Namespace;
import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.Manager;
public interface LocalCommand extends Command { public interface LocalCommand extends Command {
int handleCommand(Namespace ns, Manager m); void handleCommand(Namespace ns, Manager m) throws CommandException;
} }

View file

@ -2,16 +2,17 @@ package org.asamk.signal.commands;
import net.sourceforge.argparse4j.inf.Namespace; import net.sourceforge.argparse4j.inf.Namespace;
import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.Manager;
import java.util.List; import java.util.List;
public interface MultiLocalCommand extends LocalCommand { public interface MultiLocalCommand extends LocalCommand {
int handleCommand(Namespace ns, List<Manager> m); void handleCommand(Namespace ns, List<Manager> m) throws CommandException;
@Override @Override
default int handleCommand(final Namespace ns, final Manager m) { default void handleCommand(final Namespace ns, final Manager m) throws CommandException {
return handleCommand(ns, List.of(m)); handleCommand(ns, List.of(m));
} }
} }

View file

@ -2,9 +2,10 @@ package org.asamk.signal.commands;
import net.sourceforge.argparse4j.inf.Namespace; import net.sourceforge.argparse4j.inf.Namespace;
import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.manager.ProvisioningManager; import org.asamk.signal.manager.ProvisioningManager;
public interface ProvisioningCommand extends Command { public interface ProvisioningCommand extends Command {
int handleCommand(Namespace ns, ProvisioningManager m); void handleCommand(Namespace ns, ProvisioningManager m) throws CommandException;
} }

View file

@ -4,7 +4,11 @@ import net.sourceforge.argparse4j.inf.Namespace;
import net.sourceforge.argparse4j.inf.Subparser; import net.sourceforge.argparse4j.inf.Subparser;
import org.asamk.signal.PlainTextWriterImpl; import org.asamk.signal.PlainTextWriterImpl;
import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.IOErrorException;
import org.asamk.signal.commands.exceptions.UserErrorException;
import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.groups.GroupId;
import org.asamk.signal.manager.groups.GroupIdFormatException; import org.asamk.signal.manager.groups.GroupIdFormatException;
import org.asamk.signal.manager.groups.GroupNotFoundException; import org.asamk.signal.manager.groups.GroupNotFoundException;
import org.asamk.signal.manager.groups.NotAGroupMemberException; import org.asamk.signal.manager.groups.NotAGroupMemberException;
@ -13,10 +17,6 @@ import org.asamk.signal.util.Util;
import java.io.IOException; import java.io.IOException;
import static org.asamk.signal.util.ErrorUtils.handleAssertionError; import static org.asamk.signal.util.ErrorUtils.handleAssertionError;
import static org.asamk.signal.util.ErrorUtils.handleGroupIdFormatException;
import static org.asamk.signal.util.ErrorUtils.handleGroupNotFoundException;
import static org.asamk.signal.util.ErrorUtils.handleIOException;
import static org.asamk.signal.util.ErrorUtils.handleNotAGroupMemberException;
import static org.asamk.signal.util.ErrorUtils.handleTimestampAndSendMessageResults; import static org.asamk.signal.util.ErrorUtils.handleTimestampAndSendMessageResults;
public class QuitGroupCommand implements LocalCommand { public class QuitGroupCommand implements LocalCommand {
@ -27,28 +27,28 @@ public class QuitGroupCommand implements LocalCommand {
} }
@Override @Override
public int handleCommand(final Namespace ns, final Manager m) { public void handleCommand(final Namespace ns, final Manager m) throws CommandException {
try {
final var writer = new PlainTextWriterImpl(System.out); final var writer = new PlainTextWriterImpl(System.out);
final var groupId = Util.decodeGroupId(ns.getString("group")); final GroupId groupId;
try {
groupId = Util.decodeGroupId(ns.getString("group"));
} catch (GroupIdFormatException e) {
throw new UserErrorException("Invalid group id:" + e.getMessage());
}
try {
final var results = m.sendQuitGroupMessage(groupId); final var results = m.sendQuitGroupMessage(groupId);
return handleTimestampAndSendMessageResults(writer, results.first(), results.second()); handleTimestampAndSendMessageResults(writer, results.first(), results.second());
} catch (IOException e) { } catch (IOException e) {
handleIOException(e); throw new IOErrorException("Failed to send message: " + e.getMessage());
return 3;
} catch (AssertionError e) { } catch (AssertionError e) {
handleAssertionError(e); handleAssertionError(e);
return 1; throw e;
} catch (GroupNotFoundException e) { } catch (GroupNotFoundException e) {
handleGroupNotFoundException(e); throw new UserErrorException("Failed to send to group: " + e.getMessage());
return 1;
} catch (NotAGroupMemberException e) { } catch (NotAGroupMemberException e) {
handleNotAGroupMemberException(e); throw new UserErrorException("Failed to send to group: " + e.getMessage());
return 1;
} catch (GroupIdFormatException e) {
handleGroupIdFormatException(e);
return 1;
} }
} }
} }

View file

@ -10,6 +10,9 @@ import org.asamk.signal.JsonWriter;
import org.asamk.signal.OutputType; import org.asamk.signal.OutputType;
import org.asamk.signal.PlainTextWriterImpl; import org.asamk.signal.PlainTextWriterImpl;
import org.asamk.signal.ReceiveMessageHandler; import org.asamk.signal.ReceiveMessageHandler;
import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.IOErrorException;
import org.asamk.signal.commands.exceptions.UnexpectedErrorException;
import org.asamk.signal.json.JsonMessageEnvelope; import org.asamk.signal.json.JsonMessageEnvelope;
import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.Manager;
import org.asamk.signal.util.DateUtils; import org.asamk.signal.util.DateUtils;
@ -48,7 +51,9 @@ public class ReceiveCommand implements ExtendedDbusCommand, LocalCommand {
return Set.of(OutputType.PLAIN_TEXT, OutputType.JSON); return Set.of(OutputType.PLAIN_TEXT, OutputType.JSON);
} }
public int handleCommand(final Namespace ns, final Signal signal, DBusConnection dbusconnection) { public void handleCommand(
final Namespace ns, final Signal signal, DBusConnection dbusconnection
) throws CommandException {
var inJson = ns.get("output") == OutputType.JSON || ns.getBoolean("json"); var inJson = ns.get("output") == OutputType.JSON || ns.getBoolean("json");
// TODO delete later when "json" variable is removed // TODO delete later when "json" variable is removed
@ -63,45 +68,31 @@ public class ReceiveCommand implements ExtendedDbusCommand, LocalCommand {
dbusconnection.addSigHandler(Signal.MessageReceived.class, messageReceived -> { dbusconnection.addSigHandler(Signal.MessageReceived.class, messageReceived -> {
var envelope = new JsonMessageEnvelope(messageReceived); var envelope = new JsonMessageEnvelope(messageReceived);
final var object = Map.of("envelope", envelope); final var object = Map.of("envelope", envelope);
try {
jsonWriter.write(object); jsonWriter.write(object);
} catch (IOException e) {
logger.error("Failed to write json object: {}", e.getMessage());
}
}); });
dbusconnection.addSigHandler(Signal.ReceiptReceived.class, receiptReceived -> { dbusconnection.addSigHandler(Signal.ReceiptReceived.class, receiptReceived -> {
var envelope = new JsonMessageEnvelope(receiptReceived); var envelope = new JsonMessageEnvelope(receiptReceived);
final var object = Map.of("envelope", envelope); final var object = Map.of("envelope", envelope);
try {
jsonWriter.write(object); jsonWriter.write(object);
} catch (IOException e) {
logger.error("Failed to write json object: {}", e.getMessage());
}
}); });
dbusconnection.addSigHandler(Signal.SyncMessageReceived.class, syncReceived -> { dbusconnection.addSigHandler(Signal.SyncMessageReceived.class, syncReceived -> {
var envelope = new JsonMessageEnvelope(syncReceived); var envelope = new JsonMessageEnvelope(syncReceived);
final var object = Map.of("envelope", envelope); final var object = Map.of("envelope", envelope);
try {
jsonWriter.write(object); jsonWriter.write(object);
} catch (IOException e) {
logger.error("Failed to write json object: {}", e.getMessage());
}
}); });
} else { } else {
final var writer = new PlainTextWriterImpl(System.out); final var writer = new PlainTextWriterImpl(System.out);
dbusconnection.addSigHandler(Signal.MessageReceived.class, messageReceived -> { dbusconnection.addSigHandler(Signal.MessageReceived.class, messageReceived -> {
try {
writer.println("Envelope from: {}", messageReceived.getSender()); writer.println("Envelope from: {}", messageReceived.getSender());
writer.println("Timestamp: {}", DateUtils.formatTimestamp(messageReceived.getTimestamp())); writer.println("Timestamp: {}", DateUtils.formatTimestamp(messageReceived.getTimestamp()));
writer.println("Body: {}", messageReceived.getMessage()); writer.println("Body: {}", messageReceived.getMessage());
if (messageReceived.getGroupId().length > 0) { if (messageReceived.getGroupId().length > 0) {
writer.println("Group info:"); writer.println("Group info:");
writer.indentedWriter() writer.indentedWriter()
.println("Id: {}", .println("Id: {}", Base64.getEncoder().encodeToString(messageReceived.getGroupId()));
Base64.getEncoder().encodeToString(messageReceived.getGroupId()));
} }
if (messageReceived.getAttachments().size() > 0) { if (messageReceived.getAttachments().size() > 0) {
writer.println("Attachments:"); writer.println("Attachments:");
@ -110,22 +101,14 @@ public class ReceiveCommand implements ExtendedDbusCommand, LocalCommand {
} }
} }
writer.println(); writer.println();
} catch (IOException e) {
e.printStackTrace();
}
}); });
dbusconnection.addSigHandler(Signal.ReceiptReceived.class, receiptReceived -> { dbusconnection.addSigHandler(Signal.ReceiptReceived.class, receiptReceived -> {
try {
writer.println("Receipt from: {}", receiptReceived.getSender()); writer.println("Receipt from: {}", receiptReceived.getSender());
writer.println("Timestamp: {}", DateUtils.formatTimestamp(receiptReceived.getTimestamp())); writer.println("Timestamp: {}", DateUtils.formatTimestamp(receiptReceived.getTimestamp()));
} catch (IOException e) {
e.printStackTrace();
}
}); });
dbusconnection.addSigHandler(Signal.SyncMessageReceived.class, syncReceived -> { dbusconnection.addSigHandler(Signal.SyncMessageReceived.class, syncReceived -> {
try {
writer.println("Sync Envelope from: {} to: {}", writer.println("Sync Envelope from: {} to: {}",
syncReceived.getSource(), syncReceived.getSource(),
syncReceived.getDestination()); syncReceived.getDestination());
@ -143,26 +126,23 @@ public class ReceiveCommand implements ExtendedDbusCommand, LocalCommand {
} }
} }
writer.println(); writer.println();
} catch (IOException e) {
e.printStackTrace();
}
}); });
} }
} catch (DBusException e) { } catch (DBusException e) {
e.printStackTrace(); logger.error("Dbus client failed", e);
return 2; throw new UnexpectedErrorException("Dbus client failed");
} }
while (true) { while (true) {
try { try {
Thread.sleep(10000); Thread.sleep(10000);
} catch (InterruptedException e) { } catch (InterruptedException ignored) {
return 0; return;
} }
} }
} }
@Override @Override
public int handleCommand(final Namespace ns, final Manager m) { public void handleCommand(final Namespace ns, final Manager m) throws CommandException {
var inJson = ns.get("output") == OutputType.JSON || ns.getBoolean("json"); var inJson = ns.get("output") == OutputType.JSON || ns.getBoolean("json");
// TODO delete later when "json" variable is removed // TODO delete later when "json" variable is removed
@ -187,13 +167,11 @@ public class ReceiveCommand implements ExtendedDbusCommand, LocalCommand {
returnOnTimeout, returnOnTimeout,
ignoreAttachments, ignoreAttachments,
handler); handler);
return 0;
} catch (IOException e) { } catch (IOException e) {
System.err.println("Error while receiving messages: " + e.getMessage()); throw new IOErrorException("Error while receiving messages: " + e.getMessage());
return 3;
} catch (AssertionError e) { } catch (AssertionError e) {
handleAssertionError(e); handleAssertionError(e);
return 1; throw e;
} }
} }
} }

View file

@ -4,6 +4,9 @@ import net.sourceforge.argparse4j.impl.Arguments;
import net.sourceforge.argparse4j.inf.Namespace; import net.sourceforge.argparse4j.inf.Namespace;
import net.sourceforge.argparse4j.inf.Subparser; import net.sourceforge.argparse4j.inf.Subparser;
import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.IOErrorException;
import org.asamk.signal.commands.exceptions.UserErrorException;
import org.asamk.signal.manager.RegistrationManager; import org.asamk.signal.manager.RegistrationManager;
import org.whispersystems.signalservice.api.push.exceptions.CaptchaRequiredException; import org.whispersystems.signalservice.api.push.exceptions.CaptchaRequiredException;
@ -21,26 +24,25 @@ public class RegisterCommand implements RegistrationCommand {
} }
@Override @Override
public int handleCommand(final Namespace ns, final RegistrationManager m) { public void handleCommand(final Namespace ns, final RegistrationManager m) throws CommandException {
final boolean voiceVerification = ns.getBoolean("voice"); final boolean voiceVerification = ns.getBoolean("voice");
final var captcha = ns.getString("captcha"); final var captcha = ns.getString("captcha");
try { try {
m.register(voiceVerification, captcha); m.register(voiceVerification, captcha);
return 0;
} catch (CaptchaRequiredException e) { } catch (CaptchaRequiredException e) {
String message;
if (captcha == null) { if (captcha == null) {
System.err.println("Captcha required for verification, use --captcha CAPTCHA"); message = "Captcha required for verification, use --captcha CAPTCHA\n"
System.err.println("To get the token, go to https://signalcaptchas.org/registration/generate.html"); + "To get the token, go to https://signalcaptchas.org/registration/generate.html\n"
System.err.println("Check the developer tools (F12) console for a failed redirect to signalcaptcha://"); + "Check the developer tools (F12) console for a failed redirect to signalcaptcha://\n"
System.err.println("Everything after signalcaptcha:// is the captcha token."); + "Everything after signalcaptcha:// is the captcha token.";
} else { } else {
System.err.println("Invalid captcha given."); message = "Invalid captcha given.";
} }
return 1; throw new UserErrorException(message);
} catch (IOException e) { } catch (IOException e) {
System.err.println("Request verify error: " + e.getMessage()); throw new IOErrorException("Request verify error: " + e.getMessage());
return 3;
} }
} }
} }

View file

@ -2,9 +2,10 @@ package org.asamk.signal.commands;
import net.sourceforge.argparse4j.inf.Namespace; import net.sourceforge.argparse4j.inf.Namespace;
import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.manager.RegistrationManager; import org.asamk.signal.manager.RegistrationManager;
public interface RegistrationCommand extends Command { public interface RegistrationCommand extends Command {
int handleCommand(Namespace ns, RegistrationManager m); void handleCommand(Namespace ns, RegistrationManager m) throws CommandException;
} }

View file

@ -3,6 +3,8 @@ package org.asamk.signal.commands;
import net.sourceforge.argparse4j.inf.Namespace; import net.sourceforge.argparse4j.inf.Namespace;
import net.sourceforge.argparse4j.inf.Subparser; import net.sourceforge.argparse4j.inf.Subparser;
import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.IOErrorException;
import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.Manager;
import java.io.IOException; import java.io.IOException;
@ -18,14 +20,12 @@ public class RemoveDeviceCommand implements LocalCommand {
} }
@Override @Override
public int handleCommand(final Namespace ns, final Manager m) { public void handleCommand(final Namespace ns, final Manager m) throws CommandException {
try { try {
int deviceId = ns.getInt("deviceId"); int deviceId = ns.getInt("deviceId");
m.removeLinkedDevices(deviceId); m.removeLinkedDevices(deviceId);
return 0;
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); throw new IOErrorException("Error while removing device: " + e.getMessage());
return 3;
} }
} }
} }

View file

@ -3,6 +3,9 @@ package org.asamk.signal.commands;
import net.sourceforge.argparse4j.inf.Namespace; import net.sourceforge.argparse4j.inf.Namespace;
import net.sourceforge.argparse4j.inf.Subparser; import net.sourceforge.argparse4j.inf.Subparser;
import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.IOErrorException;
import org.asamk.signal.commands.exceptions.UnexpectedErrorException;
import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.Manager;
import org.whispersystems.libsignal.util.guava.Optional; import org.whispersystems.libsignal.util.guava.Optional;
import org.whispersystems.signalservice.internal.contacts.crypto.UnauthenticatedResponseException; import org.whispersystems.signalservice.internal.contacts.crypto.UnauthenticatedResponseException;
@ -16,16 +19,13 @@ public class RemovePinCommand implements LocalCommand {
} }
@Override @Override
public int handleCommand(final Namespace ns, final Manager m) { public void handleCommand(final Namespace ns, final Manager m) throws CommandException {
try { try {
m.setRegistrationLockPin(Optional.absent()); m.setRegistrationLockPin(Optional.absent());
return 0;
} catch (UnauthenticatedResponseException e) { } catch (UnauthenticatedResponseException e) {
System.err.println("Remove pin error: " + e.getMessage()); throw new UnexpectedErrorException("Remove pin failed with unauthenticated response: " + e.getMessage());
return 2;
} catch (IOException e) { } catch (IOException e) {
System.err.println("Remove pin error: " + e.getMessage()); throw new IOErrorException("Remove pin error: " + e.getMessage());
return 3;
} }
} }
} }

View file

@ -6,6 +6,10 @@ import net.sourceforge.argparse4j.inf.Subparser;
import org.asamk.Signal; import org.asamk.Signal;
import org.asamk.signal.PlainTextWriterImpl; import org.asamk.signal.PlainTextWriterImpl;
import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.UnexpectedErrorException;
import org.asamk.signal.commands.exceptions.UntrustedKeyErrorException;
import org.asamk.signal.commands.exceptions.UserErrorException;
import org.asamk.signal.manager.groups.GroupIdFormatException; import org.asamk.signal.manager.groups.GroupIdFormatException;
import org.asamk.signal.util.IOUtils; import org.asamk.signal.util.IOUtils;
import org.asamk.signal.util.Util; import org.asamk.signal.util.Util;
@ -19,7 +23,6 @@ import java.nio.charset.Charset;
import java.util.List; import java.util.List;
import static org.asamk.signal.util.ErrorUtils.handleAssertionError; import static org.asamk.signal.util.ErrorUtils.handleAssertionError;
import static org.asamk.signal.util.ErrorUtils.handleGroupIdFormatException;
public class SendCommand implements DbusCommand { public class SendCommand implements DbusCommand {
@ -42,7 +45,7 @@ public class SendCommand implements DbusCommand {
} }
@Override @Override
public int handleCommand(final Namespace ns, final Signal signal) { public void handleCommand(final Namespace ns, final Signal signal) throws CommandException {
final List<String> recipients = ns.getList("recipient"); final List<String> recipients = ns.getList("recipient");
final var isEndSession = ns.getBoolean("endsession"); final var isEndSession = ns.getBoolean("endsession");
final var groupIdString = ns.getString("group"); final var groupIdString = ns.getString("group");
@ -50,32 +53,27 @@ public class SendCommand implements DbusCommand {
final var noRecipients = recipients == null || recipients.isEmpty(); final var noRecipients = recipients == null || recipients.isEmpty();
if ((noRecipients && isEndSession) || (noRecipients && groupIdString == null && !isNoteToSelf)) { if ((noRecipients && isEndSession) || (noRecipients && groupIdString == null && !isNoteToSelf)) {
System.err.println("No recipients given"); throw new UserErrorException("No recipients given");
System.err.println("Aborting sending.");
return 1;
} }
if (!noRecipients && groupIdString != null) { if (!noRecipients && groupIdString != null) {
System.err.println("You cannot specify recipients by phone number and groups at the same time"); throw new UserErrorException("You cannot specify recipients by phone number and groups at the same time");
return 1;
} }
if (!noRecipients && isNoteToSelf) { if (!noRecipients && isNoteToSelf) {
System.err.println("You cannot specify recipients by phone number and not to self at the same time"); throw new UserErrorException(
return 1; "You cannot specify recipients by phone number and not to self at the same time");
} }
if (isEndSession) { if (isEndSession) {
try { try {
signal.sendEndSessionMessage(recipients); signal.sendEndSessionMessage(recipients);
return 0; return;
} catch (AssertionError e) { } catch (AssertionError e) {
handleAssertionError(e); handleAssertionError(e);
return 1; throw e;
} catch (Signal.Error.UntrustedIdentity e) { } catch (Signal.Error.UntrustedIdentity e) {
System.err.println("Failed to send message: " + e.getMessage()); throw new UntrustedKeyErrorException("Failed to send message: " + e.getMessage());
return 4;
} catch (DBusExecutionException e) { } catch (DBusExecutionException e) {
System.err.println("Failed to send message: " + e.getMessage()); throw new UnexpectedErrorException("Failed to send message: " + e.getMessage());
return 2;
} }
} }
@ -84,9 +82,7 @@ public class SendCommand implements DbusCommand {
try { try {
messageText = IOUtils.readAll(System.in, Charset.defaultCharset()); messageText = IOUtils.readAll(System.in, Charset.defaultCharset());
} catch (IOException e) { } catch (IOException e) {
System.err.println("Failed to read message from stdin: " + e.getMessage()); throw new UserErrorException("Failed to read message from stdin: " + e.getMessage());
System.err.println("Aborting sending.");
return 1;
} }
} }
@ -102,23 +98,18 @@ public class SendCommand implements DbusCommand {
try { try {
groupId = Util.decodeGroupId(groupIdString).serialize(); groupId = Util.decodeGroupId(groupIdString).serialize();
} catch (GroupIdFormatException e) { } catch (GroupIdFormatException e) {
handleGroupIdFormatException(e); throw new UserErrorException("Invalid group id:" + e.getMessage());
return 1;
} }
try { try {
var timestamp = signal.sendGroupMessage(messageText, attachments, groupId); var timestamp = signal.sendGroupMessage(messageText, attachments, groupId);
writer.println("{}", timestamp); writer.println("{}", timestamp);
return 0; return;
} catch (AssertionError e) { } catch (AssertionError e) {
handleAssertionError(e); handleAssertionError(e);
return 1; throw e;
} catch (DBusExecutionException e) { } catch (DBusExecutionException e) {
System.err.println("Failed to send group message: " + e.getMessage()); throw new UnexpectedErrorException("Failed to send group message: " + e.getMessage());
return 2;
} catch (IOException e) {
e.printStackTrace();
return 3;
} }
} }
@ -126,41 +117,29 @@ public class SendCommand implements DbusCommand {
try { try {
var timestamp = signal.sendNoteToSelfMessage(messageText, attachments); var timestamp = signal.sendNoteToSelfMessage(messageText, attachments);
writer.println("{}", timestamp); writer.println("{}", timestamp);
return 0; return;
} catch (AssertionError e) { } catch (AssertionError e) {
handleAssertionError(e); handleAssertionError(e);
return 1; throw e;
} catch (Signal.Error.UntrustedIdentity e) { } catch (Signal.Error.UntrustedIdentity e) {
System.err.println("Failed to send message: " + e.getMessage()); throw new UntrustedKeyErrorException("Failed to send message: " + e.getMessage());
return 4;
} catch (DBusExecutionException e) { } catch (DBusExecutionException e) {
System.err.println("Failed to send note to self message: " + e.getMessage()); throw new UnexpectedErrorException("Failed to send note to self message: " + e.getMessage());
return 2;
} catch (IOException e) {
e.printStackTrace();
return 3;
} }
} }
try { try {
var timestamp = signal.sendMessage(messageText, attachments, recipients); var timestamp = signal.sendMessage(messageText, attachments, recipients);
writer.println("{}", timestamp); writer.println("{}", timestamp);
return 0;
} catch (AssertionError e) { } catch (AssertionError e) {
handleAssertionError(e); handleAssertionError(e);
return 1; throw e;
} catch (UnknownObject e) { } catch (UnknownObject e) {
System.err.println("Failed to find dbus object, maybe missing the -u flag: " + e.getMessage()); throw new UserErrorException("Failed to find dbus object, maybe missing the -u flag: " + e.getMessage());
return 1;
} catch (Signal.Error.UntrustedIdentity e) { } catch (Signal.Error.UntrustedIdentity e) {
System.err.println("Failed to send message: " + e.getMessage()); throw new UntrustedKeyErrorException("Failed to send message: " + e.getMessage());
return 4;
} catch (DBusExecutionException e) { } catch (DBusExecutionException e) {
System.err.println("Failed to send message: " + e.getMessage()); throw new UnexpectedErrorException("Failed to send message: " + e.getMessage());
return 2;
} catch (IOException e) {
e.printStackTrace();
return 3;
} }
} }
} }

View file

@ -3,6 +3,9 @@ package org.asamk.signal.commands;
import net.sourceforge.argparse4j.inf.Namespace; import net.sourceforge.argparse4j.inf.Namespace;
import net.sourceforge.argparse4j.inf.Subparser; import net.sourceforge.argparse4j.inf.Subparser;
import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.IOErrorException;
import org.asamk.signal.commands.exceptions.UntrustedKeyErrorException;
import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.Manager;
import org.whispersystems.signalservice.api.crypto.UntrustedIdentityException; import org.whispersystems.signalservice.api.crypto.UntrustedIdentityException;
@ -16,16 +19,13 @@ public class SendContactsCommand implements LocalCommand {
} }
@Override @Override
public int handleCommand(final Namespace ns, final Manager m) { public void handleCommand(final Namespace ns, final Manager m) throws CommandException {
try { try {
m.sendContacts(); m.sendContacts();
return 0;
} catch (UntrustedIdentityException e) { } catch (UntrustedIdentityException e) {
System.err.println("SendContacts error: " + e.getMessage()); throw new UntrustedKeyErrorException("SendContacts error: " + e.getMessage());
return 2;
} catch (IOException e) { } catch (IOException e) {
System.err.println("SendContacts error: " + e.getMessage()); throw new IOErrorException("SendContacts error: " + e.getMessage());
return 3;
} }
} }
} }

View file

@ -5,7 +5,11 @@ import net.sourceforge.argparse4j.inf.Namespace;
import net.sourceforge.argparse4j.inf.Subparser; import net.sourceforge.argparse4j.inf.Subparser;
import org.asamk.signal.PlainTextWriterImpl; import org.asamk.signal.PlainTextWriterImpl;
import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.IOErrorException;
import org.asamk.signal.commands.exceptions.UserErrorException;
import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.groups.GroupId;
import org.asamk.signal.manager.groups.GroupIdFormatException; import org.asamk.signal.manager.groups.GroupIdFormatException;
import org.asamk.signal.manager.groups.GroupNotFoundException; import org.asamk.signal.manager.groups.GroupNotFoundException;
import org.asamk.signal.manager.groups.NotAGroupMemberException; import org.asamk.signal.manager.groups.NotAGroupMemberException;
@ -18,11 +22,6 @@ import java.io.IOException;
import java.util.List; import java.util.List;
import static org.asamk.signal.util.ErrorUtils.handleAssertionError; import static org.asamk.signal.util.ErrorUtils.handleAssertionError;
import static org.asamk.signal.util.ErrorUtils.handleGroupIdFormatException;
import static org.asamk.signal.util.ErrorUtils.handleGroupNotFoundException;
import static org.asamk.signal.util.ErrorUtils.handleIOException;
import static org.asamk.signal.util.ErrorUtils.handleInvalidNumberException;
import static org.asamk.signal.util.ErrorUtils.handleNotAGroupMemberException;
import static org.asamk.signal.util.ErrorUtils.handleTimestampAndSendMessageResults; import static org.asamk.signal.util.ErrorUtils.handleTimestampAndSendMessageResults;
public class SendReactionCommand implements LocalCommand { public class SendReactionCommand implements LocalCommand {
@ -46,19 +45,16 @@ public class SendReactionCommand implements LocalCommand {
} }
@Override @Override
public int handleCommand(final Namespace ns, final Manager m) { public void handleCommand(final Namespace ns, final Manager m) throws CommandException {
final List<String> recipients = ns.getList("recipient"); final List<String> recipients = ns.getList("recipient");
final var groupIdString = ns.getString("group"); final var groupIdString = ns.getString("group");
final var noRecipients = recipients == null || recipients.isEmpty(); final var noRecipients = recipients == null || recipients.isEmpty();
if (noRecipients && groupIdString == null) { if (noRecipients && groupIdString == null) {
System.err.println("No recipients given"); throw new UserErrorException("No recipients given");
System.err.println("Aborting sending.");
return 1;
} }
if (!noRecipients && groupIdString != null) { if (!noRecipients && groupIdString != null) {
System.err.println("You cannot specify recipients by phone number and groups at the same time"); throw new UserErrorException("You cannot specify recipients by phone number and groups at the same time");
return 1;
} }
final var emoji = ns.getString("emoji"); final var emoji = ns.getString("emoji");
@ -66,35 +62,37 @@ public class SendReactionCommand implements LocalCommand {
final var targetAuthor = ns.getString("target_author"); final var targetAuthor = ns.getString("target_author");
final long targetTimestamp = ns.getLong("target_timestamp"); final long targetTimestamp = ns.getLong("target_timestamp");
try {
final var writer = new PlainTextWriterImpl(System.out); final var writer = new PlainTextWriterImpl(System.out);
final Pair<Long, List<SendMessageResult>> results; final Pair<Long, List<SendMessageResult>> results;
if (groupIdString != null) {
var groupId = Util.decodeGroupId(groupIdString); GroupId groupId = null;
if (groupId != null) {
try {
groupId = Util.decodeGroupId(groupIdString);
} catch (GroupIdFormatException e) {
throw new UserErrorException("Invalid group id:" + e.getMessage());
}
}
try {
if (groupId != null) {
results = m.sendGroupMessageReaction(emoji, isRemove, targetAuthor, targetTimestamp, groupId); results = m.sendGroupMessageReaction(emoji, isRemove, targetAuthor, targetTimestamp, groupId);
} else { } else {
results = m.sendMessageReaction(emoji, isRemove, targetAuthor, targetTimestamp, recipients); results = m.sendMessageReaction(emoji, isRemove, targetAuthor, targetTimestamp, recipients);
} }
return handleTimestampAndSendMessageResults(writer, results.first(), results.second()); handleTimestampAndSendMessageResults(writer, results.first(), results.second());
} catch (IOException e) { } catch (IOException e) {
handleIOException(e); throw new IOErrorException("Failed to send message: " + e.getMessage());
return 3;
} catch (AssertionError e) { } catch (AssertionError e) {
handleAssertionError(e); handleAssertionError(e);
return 1; throw e;
} catch (GroupNotFoundException e) { } catch (GroupNotFoundException e) {
handleGroupNotFoundException(e); throw new UserErrorException("Failed to send to group: " + e.getMessage());
return 1;
} catch (NotAGroupMemberException e) { } catch (NotAGroupMemberException e) {
handleNotAGroupMemberException(e); throw new UserErrorException("Failed to send to group: " + e.getMessage());
return 1;
} catch (GroupIdFormatException e) {
handleGroupIdFormatException(e);
return 1;
} catch (InvalidNumberException e) { } catch (InvalidNumberException e) {
handleInvalidNumberException(e); throw new UserErrorException("Invalid number: " + e.getMessage());
return 1;
} }
} }
} }

View file

@ -3,6 +3,9 @@ package org.asamk.signal.commands;
import net.sourceforge.argparse4j.inf.Namespace; import net.sourceforge.argparse4j.inf.Namespace;
import net.sourceforge.argparse4j.inf.Subparser; import net.sourceforge.argparse4j.inf.Subparser;
import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.IOErrorException;
import org.asamk.signal.commands.exceptions.UnexpectedErrorException;
import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.Manager;
import org.whispersystems.libsignal.util.guava.Optional; import org.whispersystems.libsignal.util.guava.Optional;
import org.whispersystems.signalservice.internal.contacts.crypto.UnauthenticatedResponseException; import org.whispersystems.signalservice.internal.contacts.crypto.UnauthenticatedResponseException;
@ -18,17 +21,14 @@ public class SetPinCommand implements LocalCommand {
} }
@Override @Override
public int handleCommand(final Namespace ns, final Manager m) { public void handleCommand(final Namespace ns, final Manager m) throws CommandException {
try { try {
var registrationLockPin = ns.getString("registrationLockPin"); var registrationLockPin = ns.getString("registrationLockPin");
m.setRegistrationLockPin(Optional.of(registrationLockPin)); m.setRegistrationLockPin(Optional.of(registrationLockPin));
return 0;
} catch (UnauthenticatedResponseException e) { } catch (UnauthenticatedResponseException e) {
System.err.println("Set pin error: " + e.getMessage()); throw new UnexpectedErrorException("Set pin error failed with unauthenticated response: " + e.getMessage());
return 2;
} catch (IOException e) { } catch (IOException e) {
System.err.println("Set pin error: " + e.getMessage()); throw new IOErrorException("Set pin error: " + e.getMessage());
return 3;
} }
} }
} }

View file

@ -4,8 +4,9 @@ import net.sourceforge.argparse4j.impl.Arguments;
import net.sourceforge.argparse4j.inf.Namespace; import net.sourceforge.argparse4j.inf.Namespace;
import net.sourceforge.argparse4j.inf.Subparser; import net.sourceforge.argparse4j.inf.Subparser;
import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.UserErrorException;
import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.Manager;
import org.asamk.signal.util.ErrorUtils;
import org.asamk.signal.util.Hex; import org.asamk.signal.util.Hex;
import org.whispersystems.signalservice.api.util.InvalidNumberException; import org.whispersystems.signalservice.api.util.InvalidNumberException;
@ -25,13 +26,12 @@ public class TrustCommand implements LocalCommand {
} }
@Override @Override
public int handleCommand(final Namespace ns, final Manager m) { public void handleCommand(final Namespace ns, final Manager m) throws CommandException {
var number = ns.getString("number"); var number = ns.getString("number");
if (ns.getBoolean("trust_all_known_keys")) { if (ns.getBoolean("trust_all_known_keys")) {
var res = m.trustIdentityAllKeys(number); var res = m.trustIdentityAllKeys(number);
if (!res) { if (!res) {
System.err.println("Failed to set the trust for this number, make sure the number is correct."); throw new UserErrorException("Failed to set the trust for this number, make sure the number is correct.");
return 1;
} }
} else { } else {
var safetyNumber = ns.getString("verified_safety_number"); var safetyNumber = ns.getString("verified_safety_number");
@ -42,46 +42,38 @@ public class TrustCommand implements LocalCommand {
try { try {
fingerprintBytes = Hex.toByteArray(safetyNumber.toLowerCase(Locale.ROOT)); fingerprintBytes = Hex.toByteArray(safetyNumber.toLowerCase(Locale.ROOT));
} catch (Exception e) { } catch (Exception e) {
System.err.println( throw new UserErrorException(
"Failed to parse the fingerprint, make sure the fingerprint is a correctly encoded hex string without additional characters."); "Failed to parse the fingerprint, make sure the fingerprint is a correctly encoded hex string without additional characters.");
return 1;
} }
boolean res; boolean res;
try { try {
res = m.trustIdentityVerified(number, fingerprintBytes); res = m.trustIdentityVerified(number, fingerprintBytes);
} catch (InvalidNumberException e) { } catch (InvalidNumberException e) {
ErrorUtils.handleInvalidNumberException(e); throw new UserErrorException("Failed to parse recipient: " + e.getMessage());
return 1;
} }
if (!res) { if (!res) {
System.err.println( throw new UserErrorException(
"Failed to set the trust for the fingerprint of this number, make sure the number and the fingerprint are correct."); "Failed to set the trust for the fingerprint of this number, make sure the number and the fingerprint are correct.");
return 1;
} }
} else if (safetyNumber.length() == 60) { } else if (safetyNumber.length() == 60) {
boolean res; boolean res;
try { try {
res = m.trustIdentityVerifiedSafetyNumber(number, safetyNumber); res = m.trustIdentityVerifiedSafetyNumber(number, safetyNumber);
} catch (InvalidNumberException e) { } catch (InvalidNumberException e) {
ErrorUtils.handleInvalidNumberException(e); throw new UserErrorException("Failed to parse recipient: " + e.getMessage());
return 1;
} }
if (!res) { if (!res) {
System.err.println( throw new UserErrorException(
"Failed to set the trust for the safety number of this phone number, make sure the phone number and the safety number are correct."); "Failed to set the trust for the safety number of this phone number, make sure the phone number and the safety number are correct.");
return 1;
} }
} else { } else {
System.err.println( throw new UserErrorException(
"Safety number has invalid format, either specify the old hex fingerprint or the new safety number"); "Safety number has invalid format, either specify the old hex fingerprint or the new safety number");
return 1;
} }
} else { } else {
System.err.println( throw new UserErrorException(
"You need to specify the fingerprint/safety number you have verified with -v SAFETY_NUMBER"); "You need to specify the fingerprint/safety number you have verified with -v SAFETY_NUMBER");
return 1;
} }
} }
return 0;
} }
} }

View file

@ -3,14 +3,19 @@ package org.asamk.signal.commands;
import net.sourceforge.argparse4j.inf.Namespace; import net.sourceforge.argparse4j.inf.Namespace;
import net.sourceforge.argparse4j.inf.Subparser; import net.sourceforge.argparse4j.inf.Subparser;
import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.groups.GroupIdFormatException; import org.asamk.signal.manager.groups.GroupIdFormatException;
import org.asamk.signal.manager.groups.GroupNotFoundException; import org.asamk.signal.manager.groups.GroupNotFoundException;
import org.asamk.signal.util.Util; import org.asamk.signal.util.Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.whispersystems.signalservice.api.util.InvalidNumberException; import org.whispersystems.signalservice.api.util.InvalidNumberException;
public class UnblockCommand implements LocalCommand { public class UnblockCommand implements LocalCommand {
private final static Logger logger = LoggerFactory.getLogger(UnblockCommand.class);
@Override @Override
public void attachToSubparser(final Subparser subparser) { public void attachToSubparser(final Subparser subparser) {
subparser.addArgument("contact").help("Contact number").nargs("*"); subparser.addArgument("contact").help("Contact number").nargs("*");
@ -19,12 +24,12 @@ public class UnblockCommand implements LocalCommand {
} }
@Override @Override
public int handleCommand(final Namespace ns, final Manager m) { public void handleCommand(final Namespace ns, final Manager m) throws CommandException {
for (var contact_number : ns.<String>getList("contact")) { for (var contactNumber : ns.<String>getList("contact")) {
try { try {
m.setContactBlocked(contact_number, false); m.setContactBlocked(contactNumber, false);
} catch (InvalidNumberException e) { } catch (InvalidNumberException e) {
System.err.println(e.getMessage()); logger.warn("Invalid number: {}", contactNumber);
} }
} }
@ -33,12 +38,12 @@ public class UnblockCommand implements LocalCommand {
try { try {
var groupId = Util.decodeGroupId(groupIdString); var groupId = Util.decodeGroupId(groupIdString);
m.setGroupBlocked(groupId, false); m.setGroupBlocked(groupId, false);
} catch (GroupIdFormatException | GroupNotFoundException e) { } catch (GroupIdFormatException e) {
System.err.println(e.getMessage()); logger.warn("Invalid group id: {}", groupIdString);
} catch (GroupNotFoundException e) {
logger.warn("Unknown group id: {}", groupIdString);
} }
} }
} }
return 0;
} }
} }

View file

@ -3,6 +3,8 @@ package org.asamk.signal.commands;
import net.sourceforge.argparse4j.inf.Namespace; import net.sourceforge.argparse4j.inf.Namespace;
import net.sourceforge.argparse4j.inf.Subparser; import net.sourceforge.argparse4j.inf.Subparser;
import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.IOErrorException;
import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.Manager;
import java.io.IOException; import java.io.IOException;
@ -15,13 +17,11 @@ public class UnregisterCommand implements LocalCommand {
} }
@Override @Override
public int handleCommand(final Namespace ns, final Manager m) { public void handleCommand(final Namespace ns, final Manager m) throws CommandException {
try { try {
m.unregister(); m.unregister();
return 0;
} catch (IOException e) { } catch (IOException e) {
System.err.println("Unregister error: " + e.getMessage()); throw new IOErrorException("Unregister error: " + e.getMessage());
return 3;
} }
} }
} }

View file

@ -3,6 +3,8 @@ package org.asamk.signal.commands;
import net.sourceforge.argparse4j.inf.Namespace; import net.sourceforge.argparse4j.inf.Namespace;
import net.sourceforge.argparse4j.inf.Subparser; import net.sourceforge.argparse4j.inf.Subparser;
import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.IOErrorException;
import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.Manager;
import java.io.IOException; import java.io.IOException;
@ -15,13 +17,11 @@ public class UpdateAccountCommand implements LocalCommand {
} }
@Override @Override
public int handleCommand(final Namespace ns, final Manager m) { public void handleCommand(final Namespace ns, final Manager m) throws CommandException {
try { try {
m.updateAccountAttributes(); m.updateAccountAttributes();
return 0;
} catch (IOException e) { } catch (IOException e) {
System.err.println("UpdateAccount error: " + e.getMessage()); throw new IOErrorException("UpdateAccount error: " + e.getMessage());
return 3;
} }
} }
} }

View file

@ -3,6 +3,9 @@ package org.asamk.signal.commands;
import net.sourceforge.argparse4j.inf.Namespace; import net.sourceforge.argparse4j.inf.Namespace;
import net.sourceforge.argparse4j.inf.Subparser; import net.sourceforge.argparse4j.inf.Subparser;
import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.IOErrorException;
import org.asamk.signal.commands.exceptions.UserErrorException;
import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.Manager;
import org.whispersystems.signalservice.api.util.InvalidNumberException; import org.whispersystems.signalservice.api.util.InvalidNumberException;
@ -22,7 +25,7 @@ public class UpdateContactCommand implements LocalCommand {
} }
@Override @Override
public int handleCommand(final Namespace ns, final Manager m) { public void handleCommand(final Namespace ns, final Manager m) throws CommandException {
var number = ns.getString("number"); var number = ns.getString("number");
var name = ns.getString("name"); var name = ns.getString("name");
@ -34,13 +37,9 @@ public class UpdateContactCommand implements LocalCommand {
m.setExpirationTimer(number, expiration); m.setExpirationTimer(number, expiration);
} }
} catch (InvalidNumberException e) { } catch (InvalidNumberException e) {
System.err.println("Invalid contact number: " + e.getMessage()); throw new UserErrorException("Invalid contact number: " + e.getMessage());
return 1;
} catch (IOException e) { } catch (IOException e) {
System.err.println("Update contact error: " + e.getMessage()); throw new IOErrorException("Update contact error: " + e.getMessage());
return 3;
} }
return 0;
} }
} }

View file

@ -5,19 +5,20 @@ import net.sourceforge.argparse4j.inf.Subparser;
import org.asamk.Signal; import org.asamk.Signal;
import org.asamk.signal.PlainTextWriterImpl; import org.asamk.signal.PlainTextWriterImpl;
import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.UnexpectedErrorException;
import org.asamk.signal.commands.exceptions.UserErrorException;
import org.asamk.signal.manager.groups.GroupIdFormatException; import org.asamk.signal.manager.groups.GroupIdFormatException;
import org.asamk.signal.util.Util; import org.asamk.signal.util.Util;
import org.freedesktop.dbus.exceptions.DBusExecutionException; import org.freedesktop.dbus.exceptions.DBusExecutionException;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Base64; import java.util.Base64;
import java.util.List; import java.util.List;
import static org.asamk.signal.util.ErrorUtils.handleAssertionError; import static org.asamk.signal.util.ErrorUtils.handleAssertionError;
import static org.asamk.signal.util.ErrorUtils.handleGroupIdFormatException;
public class UpdateGroupCommand implements DbusCommand { public class UpdateGroupCommand implements DbusCommand {
@ -32,15 +33,14 @@ public class UpdateGroupCommand implements DbusCommand {
} }
@Override @Override
public int handleCommand(final Namespace ns, final Signal signal) { public void handleCommand(final Namespace ns, final Signal signal) throws CommandException {
final var writer = new PlainTextWriterImpl(System.out); final var writer = new PlainTextWriterImpl(System.out);
byte[] groupId = null; byte[] groupId = null;
if (ns.getString("group") != null) { if (ns.getString("group") != null) {
try { try {
groupId = Util.decodeGroupId(ns.getString("group")).serialize(); groupId = Util.decodeGroupId(ns.getString("group")).serialize();
} catch (GroupIdFormatException e) { } catch (GroupIdFormatException e) {
handleGroupIdFormatException(e); throw new UserErrorException("Invalid group id:" + e.getMessage());
return 1;
} }
} }
if (groupId == null) { if (groupId == null) {
@ -65,23 +65,15 @@ public class UpdateGroupCommand implements DbusCommand {
try { try {
var newGroupId = signal.updateGroup(groupId, groupName, groupMembers, groupAvatar); var newGroupId = signal.updateGroup(groupId, groupName, groupMembers, groupAvatar);
if (groupId.length != newGroupId.length) { if (groupId.length != newGroupId.length) {
try { writer.println("Created new group: \"{}\"", Base64.getEncoder().encodeToString(newGroupId));
writer.println("Creating new group \"{}\"", Base64.getEncoder().encodeToString(newGroupId));
} catch (IOException e) {
e.printStackTrace();
return 3;
} }
}
return 0;
} catch (AssertionError e) { } catch (AssertionError e) {
handleAssertionError(e); handleAssertionError(e);
return 1; throw e;
} catch (Signal.Error.AttachmentInvalid e) { } catch (Signal.Error.AttachmentInvalid e) {
System.err.println("Failed to add avatar attachment for group\": " + e.getMessage()); throw new UserErrorException("Failed to add avatar attachment for group\": " + e.getMessage());
return 1;
} catch (DBusExecutionException e) { } catch (DBusExecutionException e) {
System.err.println("Failed to send message: " + e.getMessage()); throw new UnexpectedErrorException("Failed to send message: " + e.getMessage());
return 2;
} }
} }
} }

View file

@ -4,6 +4,8 @@ import net.sourceforge.argparse4j.impl.Arguments;
import net.sourceforge.argparse4j.inf.Namespace; import net.sourceforge.argparse4j.inf.Namespace;
import net.sourceforge.argparse4j.inf.Subparser; import net.sourceforge.argparse4j.inf.Subparser;
import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.IOErrorException;
import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.Manager;
import org.whispersystems.libsignal.util.guava.Optional; import org.whispersystems.libsignal.util.guava.Optional;
@ -26,23 +28,21 @@ public class UpdateProfileCommand implements LocalCommand {
} }
@Override @Override
public int handleCommand(final Namespace ns, final Manager m) { public void handleCommand(final Namespace ns, final Manager m) throws CommandException {
var name = ns.getString("name"); var name = ns.getString("name");
var about = ns.getString("about"); var about = ns.getString("about");
var aboutEmoji = ns.getString("about_emoji"); var aboutEmoji = ns.getString("about_emoji");
var avatarPath = ns.getString("avatar"); var avatarPath = ns.getString("avatar");
boolean removeAvatar = ns.getBoolean("remove_avatar"); boolean removeAvatar = ns.getBoolean("remove_avatar");
try {
Optional<File> avatarFile = removeAvatar Optional<File> avatarFile = removeAvatar
? Optional.absent() ? Optional.absent()
: avatarPath == null ? null : Optional.of(new File(avatarPath)); : avatarPath == null ? null : Optional.of(new File(avatarPath));
try {
m.setProfile(name, about, aboutEmoji, avatarFile); m.setProfile(name, about, aboutEmoji, avatarFile);
} catch (IOException e) { } catch (IOException e) {
System.err.println("Update profile error: " + e.getMessage()); throw new IOErrorException("Update profile error: " + e.getMessage());
return 3;
} }
return 0;
} }
} }

View file

@ -4,6 +4,9 @@ import net.sourceforge.argparse4j.inf.Namespace;
import net.sourceforge.argparse4j.inf.Subparser; import net.sourceforge.argparse4j.inf.Subparser;
import org.asamk.signal.PlainTextWriterImpl; import org.asamk.signal.PlainTextWriterImpl;
import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.IOErrorException;
import org.asamk.signal.commands.exceptions.UserErrorException;
import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.StickerPackInvalidException; import org.asamk.signal.manager.StickerPackInvalidException;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -23,19 +26,17 @@ public class UploadStickerPackCommand implements LocalCommand {
} }
@Override @Override
public int handleCommand(final Namespace ns, final Manager m) { public void handleCommand(final Namespace ns, final Manager m) throws CommandException {
final var writer = new PlainTextWriterImpl(System.out); final var writer = new PlainTextWriterImpl(System.out);
try {
var path = new File(ns.getString("path")); var path = new File(ns.getString("path"));
try {
var url = m.uploadStickerPack(path); var url = m.uploadStickerPack(path);
writer.println("{}", url); writer.println("{}", url);
return 0;
} catch (IOException e) { } catch (IOException e) {
System.err.println("Upload error: " + e.getMessage()); throw new IOErrorException("Upload error: " + e.getMessage());
return 3;
} catch (StickerPackInvalidException e) { } catch (StickerPackInvalidException e) {
System.err.println("Invalid sticker pack: " + e.getMessage()); throw new UserErrorException("Invalid sticker pack: " + e.getMessage());
return 1;
} }
} }
} }

View file

@ -3,6 +3,10 @@ package org.asamk.signal.commands;
import net.sourceforge.argparse4j.inf.Namespace; import net.sourceforge.argparse4j.inf.Namespace;
import net.sourceforge.argparse4j.inf.Subparser; import net.sourceforge.argparse4j.inf.Subparser;
import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.IOErrorException;
import org.asamk.signal.commands.exceptions.UnexpectedErrorException;
import org.asamk.signal.commands.exceptions.UserErrorException;
import org.asamk.signal.manager.RegistrationManager; import org.asamk.signal.manager.RegistrationManager;
import org.whispersystems.signalservice.api.KeyBackupServicePinException; import org.whispersystems.signalservice.api.KeyBackupServicePinException;
import org.whispersystems.signalservice.api.KeyBackupSystemNoDataException; import org.whispersystems.signalservice.api.KeyBackupSystemNoDataException;
@ -19,26 +23,23 @@ public class VerifyCommand implements RegistrationCommand {
} }
@Override @Override
public int handleCommand(final Namespace ns, final RegistrationManager m) { public void handleCommand(final Namespace ns, final RegistrationManager m) throws CommandException {
try {
var verificationCode = ns.getString("verificationCode"); var verificationCode = ns.getString("verificationCode");
var pin = ns.getString("pin"); var pin = ns.getString("pin");
try {
m.verifyAccount(verificationCode, pin); m.verifyAccount(verificationCode, pin);
return 0;
} catch (LockedException e) { } catch (LockedException e) {
System.err.println("Verification failed! This number is locked with a pin. Hours remaining until reset: " throw new UserErrorException(
+ (e.getTimeRemaining() / 1000 / 60 / 60)); "Verification failed! This number is locked with a pin. Hours remaining until reset: "
System.err.println("Use '--pin PIN_CODE' to specify the registration lock PIN"); + (e.getTimeRemaining() / 1000 / 60 / 60)
return 1; + "\nUse '--pin PIN_CODE' to specify the registration lock PIN");
} catch (KeyBackupServicePinException e) { } catch (KeyBackupServicePinException e) {
System.err.println("Verification failed! Invalid pin, tries remaining: " + e.getTriesRemaining()); throw new UserErrorException("Verification failed! Invalid pin, tries remaining: " + e.getTriesRemaining());
return 1;
} catch (KeyBackupSystemNoDataException e) { } catch (KeyBackupSystemNoDataException e) {
System.err.println("Verification failed! No KBS data."); throw new UnexpectedErrorException("Verification failed! No KBS data.");
return 3;
} catch (IOException e) { } catch (IOException e) {
System.err.println("Verify error: " + e.getMessage()); throw new IOErrorException("Verify error: " + e.getMessage());
return 3;
} }
} }
} }

View file

@ -0,0 +1,8 @@
package org.asamk.signal.commands.exceptions;
public class CommandException extends Exception {
public CommandException(final String message) {
super(message);
}
}

View file

@ -0,0 +1,8 @@
package org.asamk.signal.commands.exceptions;
public final class IOErrorException extends CommandException {
public IOErrorException(final String message) {
super(message);
}
}

View file

@ -0,0 +1,8 @@
package org.asamk.signal.commands.exceptions;
public final class UnexpectedErrorException extends CommandException {
public UnexpectedErrorException(final String message) {
super(message);
}
}

View file

@ -0,0 +1,8 @@
package org.asamk.signal.commands.exceptions;
public final class UntrustedKeyErrorException extends CommandException {
public UntrustedKeyErrorException(final String message) {
super(message);
}
}

View file

@ -0,0 +1,8 @@
package org.asamk.signal.commands.exceptions;
public final class UserErrorException extends CommandException {
public UserErrorException(final String message) {
super(message);
}
}

View file

@ -1,38 +1,34 @@
package org.asamk.signal.util; package org.asamk.signal.util;
import org.asamk.signal.PlainTextWriter; import org.asamk.signal.PlainTextWriter;
import org.asamk.signal.manager.groups.GroupIdFormatException; import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.manager.groups.GroupNotFoundException; import org.asamk.signal.commands.exceptions.IOErrorException;
import org.asamk.signal.manager.groups.NotAGroupMemberException; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.whispersystems.signalservice.api.messages.SendMessageResult; import org.whispersystems.signalservice.api.messages.SendMessageResult;
import org.whispersystems.signalservice.api.util.InvalidNumberException;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class ErrorUtils { public class ErrorUtils {
private final static Logger logger = LoggerFactory.getLogger(ErrorUtils.class);
private ErrorUtils() { private ErrorUtils() {
} }
public static void handleAssertionError(AssertionError e) { public static void handleAssertionError(AssertionError e) {
System.err.println("Failed to send/receive message (Assertion): " + e.getMessage()); logger.warn("If you use an Oracle JRE please check if you have unlimited strength crypto enabled, see README");
e.printStackTrace();
System.err.println(
"If you use an Oracle JRE please check if you have unlimited strength crypto enabled, see README");
} }
public static int handleTimestampAndSendMessageResults( public static void handleTimestampAndSendMessageResults(
PlainTextWriter writer, PlainTextWriter writer, long timestamp, List<SendMessageResult> results
long timestamp, ) throws CommandException {
List<SendMessageResult> results
) throws IOException {
if (timestamp != 0) { if (timestamp != 0) {
writer.println("{}", timestamp); writer.println("{}", timestamp);
} }
var errors = getErrorMessagesFromSendMessageResults(results); var errors = getErrorMessagesFromSendMessageResults(results);
return handleSendMessageResultErrors(errors); handleSendMessageResultErrors(errors);
} }
public static List<String> getErrorMessagesFromSendMessageResults(List<SendMessageResult> results) { public static List<String> getErrorMessagesFromSendMessageResults(List<SendMessageResult> results) {
@ -58,39 +54,15 @@ public class ErrorUtils {
return null; return null;
} }
private static int handleSendMessageResultErrors(List<String> errors) { private static void handleSendMessageResultErrors(List<String> errors) throws CommandException {
if (errors.size() == 0) { if (errors.size() == 0) {
return 0; return;
} }
System.err.println("Failed to send (some) messages:"); var message = new StringBuilder();
message.append("Failed to send (some) messages:\n");
for (var error : errors) { for (var error : errors) {
System.err.println(error); message.append(error).append("\n");
} }
return 3; throw new IOErrorException(message.toString());
}
public static void handleIOException(IOException e) {
System.err.println("Failed to send message: " + e.getMessage());
}
public static void handleGroupNotFoundException(GroupNotFoundException e) {
System.err.println("Failed to send to group: " + e.getMessage());
System.err.println("Aborting sending.");
}
public static void handleNotAGroupMemberException(NotAGroupMemberException e) {
System.err.println("Failed to send to group: " + e.getMessage());
System.err.println("Update the group on another device to readd the user to this group.");
System.err.println("Aborting sending.");
}
public static void handleGroupIdFormatException(GroupIdFormatException e) {
System.err.println(e.getMessage());
System.err.println("Aborting sending.");
}
public static void handleInvalidNumberException(InvalidNumberException e) {
System.err.println("Failed to parse recipient: " + e.getMessage());
System.err.println("Aborting sending.");
} }
} }