mirror of
https://github.com/AsamK/signal-cli
synced 2025-08-29 18:40:39 +00:00
259 lines
9.2 KiB
Java
259 lines
9.2 KiB
Java
package org.asamk.signal.jsonrpc;
|
|
|
|
import com.fasterxml.jackson.core.TreeNode;
|
|
import com.fasterxml.jackson.core.type.TypeReference;
|
|
import com.fasterxml.jackson.databind.JsonMappingException;
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import com.fasterxml.jackson.databind.node.ContainerNode;
|
|
|
|
import org.asamk.signal.JsonReceiveMessageHandler;
|
|
import org.asamk.signal.JsonWriter;
|
|
import org.asamk.signal.OutputWriter;
|
|
import org.asamk.signal.commands.Command;
|
|
import org.asamk.signal.commands.Commands;
|
|
import org.asamk.signal.commands.JsonRpcCommand;
|
|
import org.asamk.signal.commands.SignalCreator;
|
|
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.commands.exceptions.UserErrorException;
|
|
import org.asamk.signal.manager.Manager;
|
|
import org.asamk.signal.util.Util;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import java.io.IOException;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
import java.util.Objects;
|
|
import java.util.function.Supplier;
|
|
|
|
public class SignalJsonRpcDispatcherHandler {
|
|
|
|
private final static Logger logger = LoggerFactory.getLogger(SignalJsonRpcDispatcherHandler.class);
|
|
|
|
private static final int USER_ERROR = -1;
|
|
private static final int IO_ERROR = -3;
|
|
private static final int UNTRUSTED_KEY_ERROR = -4;
|
|
|
|
private final ObjectMapper objectMapper;
|
|
private final JsonRpcSender jsonRpcSender;
|
|
private final JsonRpcReader jsonRpcReader;
|
|
private final boolean noReceiveOnStart;
|
|
|
|
private SignalCreator c;
|
|
private final Map<Manager, Manager.ReceiveMessageHandler> receiveHandlers = new HashMap<>();
|
|
|
|
private Manager m;
|
|
|
|
public SignalJsonRpcDispatcherHandler(
|
|
final JsonWriter outputWriter, final Supplier<String> lineSupplier, final boolean noReceiveOnStart
|
|
) {
|
|
this.noReceiveOnStart = noReceiveOnStart;
|
|
this.objectMapper = Util.createJsonObjectMapper();
|
|
this.jsonRpcSender = new JsonRpcSender(outputWriter);
|
|
this.jsonRpcReader = new JsonRpcReader(jsonRpcSender, lineSupplier);
|
|
}
|
|
|
|
public void handleConnection(final SignalCreator c) {
|
|
this.c = c;
|
|
|
|
if (!noReceiveOnStart) {
|
|
c.getAccountNumbers().stream().map(c::getManager).filter(Objects::nonNull).forEach(this::subscribeReceive);
|
|
}
|
|
|
|
handleConnection();
|
|
}
|
|
|
|
public void handleConnection(final Manager m) {
|
|
this.m = m;
|
|
|
|
if (!noReceiveOnStart) {
|
|
subscribeReceive(m);
|
|
}
|
|
|
|
handleConnection();
|
|
}
|
|
|
|
private void subscribeReceive(final Manager m) {
|
|
if (receiveHandlers.containsKey(m)) {
|
|
return;
|
|
}
|
|
|
|
final var receiveMessageHandler = new JsonReceiveMessageHandler(m,
|
|
s -> jsonRpcSender.sendRequest(JsonRpcRequest.forNotification("receive",
|
|
objectMapper.valueToTree(s),
|
|
null)));
|
|
m.addReceiveHandler(receiveMessageHandler);
|
|
receiveHandlers.put(m, receiveMessageHandler);
|
|
|
|
while (!m.hasCaughtUpWithOldMessages()) {
|
|
try {
|
|
synchronized (m) {
|
|
m.wait();
|
|
}
|
|
} catch (InterruptedException ignored) {
|
|
}
|
|
}
|
|
}
|
|
|
|
void unsubscribeReceive(final Manager m) {
|
|
final var receiveMessageHandler = receiveHandlers.remove(m);
|
|
if (receiveMessageHandler != null) {
|
|
m.removeReceiveHandler(receiveMessageHandler);
|
|
}
|
|
}
|
|
|
|
private void handleConnection() {
|
|
try {
|
|
jsonRpcReader.readMessages((method, params) -> handleRequest(objectMapper, method, params),
|
|
response -> logger.debug("Received unexpected response for id {}", response.getId()));
|
|
} finally {
|
|
receiveHandlers.forEach(Manager::removeReceiveHandler);
|
|
receiveHandlers.clear();
|
|
}
|
|
}
|
|
|
|
private JsonNode handleRequest(
|
|
final ObjectMapper objectMapper, final String method, ContainerNode<?> params
|
|
) throws JsonRpcException {
|
|
var command = getCommand(method);
|
|
// TODO implement listAccounts, register, verify, link
|
|
if (command instanceof JsonRpcCommand<?> jsonRpcCommand) {
|
|
if (m != null) {
|
|
return runCommand(objectMapper, params, new CommandRunnerImpl<>(m, jsonRpcCommand));
|
|
}
|
|
|
|
if (params.has("account")) {
|
|
Manager manager = c.getManager(params.get("account").asText());
|
|
if (manager != null) {
|
|
return runCommand(objectMapper, params, new CommandRunnerImpl<>(manager, jsonRpcCommand));
|
|
}
|
|
} else {
|
|
throw new JsonRpcException(new JsonRpcResponse.Error(JsonRpcResponse.Error.INVALID_PARAMS,
|
|
"Method requires valid account parameter",
|
|
null));
|
|
}
|
|
}
|
|
|
|
throw new JsonRpcException(new JsonRpcResponse.Error(JsonRpcResponse.Error.METHOD_NOT_FOUND,
|
|
"Method not implemented",
|
|
null));
|
|
}
|
|
|
|
private Command getCommand(final String method) {
|
|
if ("subscribeReceive".equals(method)) {
|
|
return new SubscribeReceiveCommand();
|
|
}
|
|
if ("unsubscribeReceive".equals(method)) {
|
|
return new UnsubscribeReceiveCommand();
|
|
}
|
|
return Commands.getCommand(method);
|
|
}
|
|
|
|
private record CommandRunnerImpl<T>(Manager m, JsonRpcCommand<T> command) implements CommandRunner<T> {
|
|
|
|
@Override
|
|
public void handleCommand(final T request, final OutputWriter outputWriter) throws CommandException {
|
|
command.handleCommand(request, m, outputWriter);
|
|
}
|
|
|
|
@Override
|
|
public TypeReference<T> getRequestType() {
|
|
return command.getRequestType();
|
|
}
|
|
}
|
|
|
|
interface CommandRunner<T> {
|
|
|
|
void handleCommand(T request, OutputWriter outputWriter) throws CommandException;
|
|
|
|
TypeReference<T> getRequestType();
|
|
}
|
|
|
|
private JsonNode runCommand(
|
|
final ObjectMapper objectMapper, final ContainerNode<?> params, final CommandRunner<?> command
|
|
) throws JsonRpcException {
|
|
final Object[] result = {null};
|
|
final JsonWriter commandOutputWriter = s -> {
|
|
if (result[0] != null) {
|
|
throw new AssertionError("Command may only write one json result");
|
|
}
|
|
|
|
result[0] = s;
|
|
};
|
|
|
|
try {
|
|
parseParamsAndRunCommand(objectMapper, params, commandOutputWriter, command);
|
|
} catch (JsonMappingException e) {
|
|
throw new JsonRpcException(new JsonRpcResponse.Error(JsonRpcResponse.Error.INVALID_REQUEST,
|
|
e.getMessage(),
|
|
null));
|
|
} catch (UserErrorException e) {
|
|
throw new JsonRpcException(new JsonRpcResponse.Error(USER_ERROR, e.getMessage(), null));
|
|
} catch (IOErrorException e) {
|
|
throw new JsonRpcException(new JsonRpcResponse.Error(IO_ERROR, e.getMessage(), null));
|
|
} catch (UntrustedKeyErrorException e) {
|
|
throw new JsonRpcException(new JsonRpcResponse.Error(UNTRUSTED_KEY_ERROR, e.getMessage(), null));
|
|
} catch (Throwable e) {
|
|
logger.error("Command execution failed", e);
|
|
throw new JsonRpcException(new JsonRpcResponse.Error(JsonRpcResponse.Error.INTERNAL_ERROR,
|
|
e.getMessage(),
|
|
null));
|
|
}
|
|
|
|
Object output = result[0] == null ? Map.of() : result[0];
|
|
return objectMapper.valueToTree(output);
|
|
}
|
|
|
|
private <T> void parseParamsAndRunCommand(
|
|
final ObjectMapper objectMapper,
|
|
final TreeNode params,
|
|
final OutputWriter outputWriter,
|
|
final CommandRunner<T> command
|
|
) throws CommandException, JsonMappingException {
|
|
T requestParams = null;
|
|
final var requestType = command.getRequestType();
|
|
if (params != null && requestType != null) {
|
|
try {
|
|
requestParams = objectMapper.readValue(objectMapper.treeAsTokens(params), requestType);
|
|
} catch (JsonMappingException e) {
|
|
throw e;
|
|
} catch (IOException e) {
|
|
throw new AssertionError(e);
|
|
}
|
|
}
|
|
command.handleCommand(requestParams, outputWriter);
|
|
}
|
|
|
|
private class SubscribeReceiveCommand implements JsonRpcCommand<Void> {
|
|
|
|
@Override
|
|
public String getName() {
|
|
return "subscribeReceive";
|
|
}
|
|
|
|
@Override
|
|
public void handleCommand(
|
|
final Void request, final Manager m, final OutputWriter outputWriter
|
|
) throws CommandException {
|
|
subscribeReceive(m);
|
|
}
|
|
}
|
|
|
|
private class UnsubscribeReceiveCommand implements JsonRpcCommand<Void> {
|
|
|
|
@Override
|
|
public String getName() {
|
|
return "unsubscribeReceive";
|
|
}
|
|
|
|
@Override
|
|
public void handleCommand(
|
|
final Void request, final Manager m, final OutputWriter outputWriter
|
|
) throws CommandException {
|
|
unsubscribeReceive(m);
|
|
}
|
|
}
|
|
}
|