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
= switch (outputWriter
) {
78 case JsonWriter writer
-> new JsonReceiveMessageHandler(m
, writer
);
79 case PlainTextWriter writer
-> new ReceiveMessageHandler(m
, writer
);
81 final var duration
= timeout
< 0 ?
null : Duration
.ofMillis((long) (timeout
* 1000));
82 final var maxMessages
= maxMessagesRaw
< 0 ?
null : maxMessagesRaw
;
83 m
.receiveMessages(Optional
.ofNullable(duration
), Optional
.ofNullable(maxMessages
), handler
);
84 } catch (IOException e
) {
85 throw new IOErrorException("Error while receiving messages: " + e
.getMessage(), e
);
86 } catch (AlreadyReceivingException e
) {
87 throw new UserErrorException("Receive command cannot be used if messages are already being received.", e
);
92 public TypeReference
<ReceiveParams
> getRequestType() {
93 return new TypeReference
<>() {};
97 public void handleCommand(
98 final ReceiveParams request
, final Manager m
, final JsonWriter jsonWriter
99 ) throws CommandException
{
100 final var timeout
= request
.timeout() == null ?
3.0 : request
.timeout();
101 final var maxMessagesRaw
= request
.maxMessages() == null ?
-1 : request
.maxMessages();
104 final var messages
= new ArrayList
<>();
105 final var handler
= new JsonReceiveMessageHandler(m
, messages
::add
);
106 final var duration
= timeout
< 0 ?
null : Duration
.ofMillis((long) (timeout
* 1000));
107 final var maxMessages
= maxMessagesRaw
< 0 ?
null : maxMessagesRaw
;
108 m
.receiveMessages(Optional
.ofNullable(duration
), Optional
.ofNullable(maxMessages
), handler
);
109 jsonWriter
.write(messages
);
110 } catch (IOException e
) {
111 throw new IOErrorException("Error while receiving messages: " + e
.getMessage(), e
);
112 } catch (AlreadyReceivingException e
) {
113 throw new UserErrorException("Receive command cannot be used if messages are already being received.", e
);
117 public record ReceiveParams(Double timeout
, Integer maxMessages
) {}