1 package org
.asamk
.signal
.commands
;
3 import com
.fasterxml
.jackson
.core
.type
.TypeReference
;
5 import net
.sourceforge
.argparse4j
.impl
.Arguments
;
6 import net
.sourceforge
.argparse4j
.inf
.Namespace
;
7 import net
.sourceforge
.argparse4j
.inf
.Subparser
;
9 import org
.asamk
.signal
.OutputType
;
10 import org
.asamk
.signal
.ReceiveMessageHandler
;
11 import org
.asamk
.signal
.commands
.exceptions
.CommandException
;
12 import org
.asamk
.signal
.commands
.exceptions
.IOErrorException
;
13 import org
.asamk
.signal
.commands
.exceptions
.UserErrorException
;
14 import org
.asamk
.signal
.json
.JsonReceiveMessageHandler
;
15 import org
.asamk
.signal
.manager
.Manager
;
16 import org
.asamk
.signal
.manager
.api
.AlreadyReceivingException
;
17 import org
.asamk
.signal
.manager
.api
.ReceiveConfig
;
18 import org
.asamk
.signal
.output
.JsonWriter
;
19 import org
.asamk
.signal
.output
.OutputWriter
;
20 import org
.asamk
.signal
.output
.PlainTextWriter
;
21 import org
.slf4j
.Logger
;
22 import org
.slf4j
.LoggerFactory
;
24 import java
.io
.IOException
;
25 import java
.time
.Duration
;
26 import java
.util
.ArrayList
;
27 import java
.util
.List
;
28 import java
.util
.Optional
;
30 public class ReceiveCommand
implements LocalCommand
, JsonRpcSingleCommand
<ReceiveCommand
.ReceiveParams
> {
32 private final static Logger logger
= LoggerFactory
.getLogger(ReceiveCommand
.class);
35 public String
getName() {
40 public void attachToSubparser(final Subparser subparser
) {
41 subparser
.help("Query the server for new messages.");
42 subparser
.addArgument("-t", "--timeout")
45 .help("Number of seconds to wait for new messages (negative values disable timeout)");
46 subparser
.addArgument("--max-messages")
49 .help("Maximum number of messages to receive, before returning.");
50 subparser
.addArgument("--ignore-attachments")
51 .help("Don’t download attachments of received messages.")
52 .action(Arguments
.storeTrue());
53 subparser
.addArgument("--ignore-stories")
54 .help("Don’t receive story messages from the server.")
55 .action(Arguments
.storeTrue());
56 subparser
.addArgument("--send-read-receipts")
57 .help("Send read receipts for all incoming data messages (in addition to the default delivery receipts)")
58 .action(Arguments
.storeTrue());
62 public List
<OutputType
> getSupportedOutputTypes() {
63 return List
.of(OutputType
.PLAIN_TEXT
, OutputType
.JSON
);
67 public void handleCommand(
68 final Namespace ns
, final Manager m
, final OutputWriter outputWriter
69 ) throws CommandException
{
70 final var timeout
= ns
.getDouble("timeout");
71 final var maxMessagesRaw
= ns
.getInt("max-messages");
72 final var ignoreAttachments
= Boolean
.TRUE
.equals(ns
.getBoolean("ignore-attachments"));
73 final var ignoreStories
= Boolean
.TRUE
.equals(ns
.getBoolean("ignore-stories"));
74 final var sendReadReceipts
= Boolean
.TRUE
.equals(ns
.getBoolean("send-read-receipts"));
75 m
.setReceiveConfig(new ReceiveConfig(ignoreAttachments
, ignoreStories
, sendReadReceipts
));
77 final var handler
= outputWriter
instanceof JsonWriter ?
new JsonReceiveMessageHandler(m
,
78 (JsonWriter
) outputWriter
) : new ReceiveMessageHandler(m
, (PlainTextWriter
) outputWriter
);
79 final var duration
= timeout
< 0 ?
null : Duration
.ofMillis((long) (timeout
* 1000));
80 final var maxMessages
= maxMessagesRaw
< 0 ?
null : maxMessagesRaw
;
81 m
.receiveMessages(Optional
.ofNullable(duration
), Optional
.ofNullable(maxMessages
), handler
);
82 } catch (IOException e
) {
83 throw new IOErrorException("Error while receiving messages: " + e
.getMessage(), e
);
84 } catch (AlreadyReceivingException e
) {
85 throw new UserErrorException("Receive command cannot be used if messages are already being received.", e
);
90 public TypeReference
<ReceiveParams
> getRequestType() {
91 return new TypeReference
<>() {};
95 public void handleCommand(
96 final ReceiveParams request
, final Manager m
, final JsonWriter jsonWriter
97 ) throws CommandException
{
98 final var timeout
= request
.timeout() == null ?
3.0 : request
.timeout();
99 final var maxMessagesRaw
= request
.maxMessages() == null ?
-1 : request
.maxMessages();
102 final var messages
= new ArrayList
<>();
103 final var handler
= new JsonReceiveMessageHandler(m
, messages
::add
);
104 final var duration
= timeout
< 0 ?
null : Duration
.ofMillis((long) (timeout
* 1000));
105 final var maxMessages
= maxMessagesRaw
< 0 ?
null : maxMessagesRaw
;
106 m
.receiveMessages(Optional
.ofNullable(duration
), Optional
.ofNullable(maxMessages
), handler
);
107 jsonWriter
.write(messages
);
108 } catch (IOException e
) {
109 throw new IOErrorException("Error while receiving messages: " + e
.getMessage(), e
);
110 } catch (AlreadyReceivingException e
) {
111 throw new UserErrorException("Receive command cannot be used if messages are already being received.", e
);
115 public record ReceiveParams(Double timeout
, Integer maxMessages
) {}