X-Git-Url: https://git.nmode.ca/signal-cli/blobdiff_plain/79cc225869b9a97bbb5b3f054970d5a6bb2222ee..b7005884fdcfa8d95f54e557ddbf2fe4201962f8:/src/main/java/org/asamk/signal/commands/FinishLinkCommand.java diff --git a/src/main/java/org/asamk/signal/commands/FinishLinkCommand.java b/src/main/java/org/asamk/signal/commands/FinishLinkCommand.java new file mode 100644 index 00000000..40c241a2 --- /dev/null +++ b/src/main/java/org/asamk/signal/commands/FinishLinkCommand.java @@ -0,0 +1,73 @@ +package org.asamk.signal.commands; + +import com.fasterxml.jackson.core.type.TypeReference; + +import org.asamk.signal.JsonWriter; +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.MultiAccountManager; +import org.asamk.signal.manager.UserAlreadyExists; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.concurrent.TimeoutException; + +public class FinishLinkCommand implements JsonRpcMultiCommand { + + private final static Logger logger = LoggerFactory.getLogger(FinishLinkCommand.class); + + @Override + public String getName() { + return "finishLink"; + } + + @Override + public TypeReference getRequestType() { + return new TypeReference<>() {}; + } + + @Override + public void handleCommand( + final FinishLinkParams request, final MultiAccountManager m, final JsonWriter jsonWriter + ) throws CommandException { + final URI deviceLinkUri; + try { + deviceLinkUri = new URI(request.deviceLinkUri()); + } catch (URISyntaxException e) { + throw new UserErrorException("Invalid device link uri."); + } + final var provisioningManager = m.getProvisioningManagerFor(deviceLinkUri); + if (provisioningManager == null) { + throw new UserErrorException("Unknown device link uri."); + } + + var deviceName = request.deviceName(); + if (deviceName == null) { + deviceName = "cli"; + } + final String number; + try { + number = provisioningManager.finishDeviceLink(deviceName); + } catch (TimeoutException e) { + throw new UserErrorException("Link request timed out, please try again."); + } catch (IOException e) { + throw new IOErrorException("Link request error: " + e.getMessage(), e); + } catch (UserAlreadyExists e) { + throw new UserErrorException("The user " + + e.getNumber() + + " already exists\nDelete \"" + + e.getFileName() + + "\" before trying again."); + } + + jsonWriter.write(new JsonFinishLink(number)); + } + + record FinishLinkParams(String deviceLinkUri, String deviceName) {} + + private record JsonFinishLink(String number) {} +}