1 package org
.asamk
.signal
.commands
;
3 import com
.fasterxml
.jackson
.core
.TreeNode
;
4 import com
.fasterxml
.jackson
.databind
.JsonMappingException
;
5 import com
.fasterxml
.jackson
.databind
.JsonNode
;
6 import com
.fasterxml
.jackson
.databind
.ObjectMapper
;
7 import com
.fasterxml
.jackson
.databind
.node
.ContainerNode
;
9 import net
.sourceforge
.argparse4j
.impl
.Arguments
;
10 import net
.sourceforge
.argparse4j
.inf
.Namespace
;
11 import net
.sourceforge
.argparse4j
.inf
.Subparser
;
13 import org
.asamk
.signal
.JsonReceiveMessageHandler
;
14 import org
.asamk
.signal
.JsonWriter
;
15 import org
.asamk
.signal
.OutputType
;
16 import org
.asamk
.signal
.OutputWriter
;
17 import org
.asamk
.signal
.commands
.exceptions
.CommandException
;
18 import org
.asamk
.signal
.commands
.exceptions
.IOErrorException
;
19 import org
.asamk
.signal
.commands
.exceptions
.UntrustedKeyErrorException
;
20 import org
.asamk
.signal
.commands
.exceptions
.UserErrorException
;
21 import org
.asamk
.signal
.jsonrpc
.JsonRpcException
;
22 import org
.asamk
.signal
.jsonrpc
.JsonRpcReader
;
23 import org
.asamk
.signal
.jsonrpc
.JsonRpcRequest
;
24 import org
.asamk
.signal
.jsonrpc
.JsonRpcResponse
;
25 import org
.asamk
.signal
.jsonrpc
.JsonRpcSender
;
26 import org
.asamk
.signal
.manager
.Manager
;
27 import org
.asamk
.signal
.util
.Util
;
28 import org
.slf4j
.Logger
;
29 import org
.slf4j
.LoggerFactory
;
31 import java
.io
.BufferedReader
;
32 import java
.io
.IOException
;
33 import java
.io
.InputStreamReader
;
34 import java
.util
.List
;
37 public class JsonRpcDispatcherCommand
implements LocalCommand
{
39 private final static Logger logger
= LoggerFactory
.getLogger(JsonRpcDispatcherCommand
.class);
41 private static final int USER_ERROR
= -1;
42 private static final int IO_ERROR
= -3;
43 private static final int UNTRUSTED_KEY_ERROR
= -4;
46 public String
getName() {
51 public void attachToSubparser(final Subparser subparser
) {
52 subparser
.help("Take commands from standard input as line-delimited JSON RPC while receiving messages.");
53 subparser
.addArgument("--ignore-attachments")
54 .help("Don’t download attachments of received messages.")
55 .action(Arguments
.storeTrue());
59 public List
<OutputType
> getSupportedOutputTypes() {
60 return List
.of(OutputType
.JSON
);
64 public void handleCommand(
65 final Namespace ns
, final Manager m
, final OutputWriter outputWriter
66 ) throws CommandException
{
67 final boolean ignoreAttachments
= Boolean
.TRUE
.equals(ns
.getBoolean("ignore-attachments"));
68 m
.setIgnoreAttachments(ignoreAttachments
);
70 final var objectMapper
= Util
.createJsonObjectMapper();
71 final var jsonRpcSender
= new JsonRpcSender((JsonWriter
) outputWriter
);
73 final var receiveThread
= receiveMessages(s
-> jsonRpcSender
.sendRequest(JsonRpcRequest
.forNotification(
75 objectMapper
.valueToTree(s
),
78 // Maybe this should be handled inside the Manager
79 while (!m
.hasCaughtUpWithOldMessages()) {
84 } catch (InterruptedException ignored
) {
88 final BufferedReader reader
= new BufferedReader(new InputStreamReader(System
.in));
90 final var jsonRpcReader
= new JsonRpcReader(jsonRpcSender
, () -> {
92 return reader
.readLine();
93 } catch (IOException e
) {
94 throw new AssertionError(e
);
97 jsonRpcReader
.readRequests((method
, params
) -> handleRequest(m
, objectMapper
, method
, params
),
98 response
-> logger
.debug("Received unexpected response for id {}", response
.getId()));
100 receiveThread
.interrupt();
102 receiveThread
.join();
103 } catch (InterruptedException ignored
) {
107 private JsonNode
handleRequest(
108 final Manager m
, final ObjectMapper objectMapper
, final String method
, ContainerNode
<?
> params
109 ) throws JsonRpcException
{
110 final Object
[] result
= {null};
111 final JsonWriter commandOutputWriter
= s
-> {
112 if (result
[0] != null) {
113 throw new AssertionError("Command may only write one json result");
119 var command
= Commands
.getCommand(method
);
120 if (!(command
instanceof JsonRpcCommand
)) {
121 throw new JsonRpcException(new JsonRpcResponse
.Error(JsonRpcResponse
.Error
.METHOD_NOT_FOUND
,
122 "Method not implemented",
127 parseParamsAndRunCommand(m
, objectMapper
, params
, commandOutputWriter
, (JsonRpcCommand
<?
>) command
);
128 } catch (JsonMappingException e
) {
129 throw new JsonRpcException(new JsonRpcResponse
.Error(JsonRpcResponse
.Error
.INVALID_REQUEST
,
132 } catch (UserErrorException e
) {
133 throw new JsonRpcException(new JsonRpcResponse
.Error(USER_ERROR
, e
.getMessage(), null));
134 } catch (IOErrorException e
) {
135 throw new JsonRpcException(new JsonRpcResponse
.Error(IO_ERROR
, e
.getMessage(), null));
136 } catch (UntrustedKeyErrorException e
) {
137 throw new JsonRpcException(new JsonRpcResponse
.Error(UNTRUSTED_KEY_ERROR
, e
.getMessage(), null));
138 } catch (Throwable e
) {
139 logger
.error("Command execution failed", e
);
140 throw new JsonRpcException(new JsonRpcResponse
.Error(JsonRpcResponse
.Error
.INTERNAL_ERROR
,
145 Object output
= result
[0] == null ? Map
.of() : result
[0];
146 return objectMapper
.valueToTree(output
);
149 private <T
> void parseParamsAndRunCommand(
151 final ObjectMapper objectMapper
,
152 final TreeNode params
,
153 final OutputWriter outputWriter
,
154 final JsonRpcCommand
<T
> command
155 ) throws CommandException
, JsonMappingException
{
156 T requestParams
= null;
157 final var requestType
= command
.getRequestType();
158 if (params
!= null && requestType
!= null) {
160 requestParams
= objectMapper
.readValue(objectMapper
.treeAsTokens(params
), requestType
);
161 } catch (JsonMappingException e
) {
163 } catch (IOException e
) {
164 throw new AssertionError(e
);
167 command
.handleCommand(requestParams
, m
, outputWriter
);
170 private Thread
receiveMessages(JsonWriter jsonWriter
, Manager m
) {
171 final var thread
= new Thread(() -> {
172 while (!Thread
.interrupted()) {
174 final var receiveMessageHandler
= new JsonReceiveMessageHandler(m
, jsonWriter
);
175 m
.receiveMessages(receiveMessageHandler
);
177 } catch (IOException e
) {
178 logger
.warn("Receiving messages failed, retrying", e
);