1 package org
.asamk
.signal
.commands
;
3 import net
.sourceforge
.argparse4j
.impl
.Arguments
;
4 import net
.sourceforge
.argparse4j
.inf
.Namespace
;
5 import net
.sourceforge
.argparse4j
.inf
.Subparser
;
7 import org
.asamk
.signal
.OutputType
;
8 import org
.asamk
.signal
.ReceiveMessageHandler
;
9 import org
.asamk
.signal
.commands
.exceptions
.CommandException
;
10 import org
.asamk
.signal
.commands
.exceptions
.IOErrorException
;
11 import org
.asamk
.signal
.json
.JsonReceiveMessageHandler
;
12 import org
.asamk
.signal
.manager
.Manager
;
13 import org
.asamk
.signal
.manager
.api
.ReceiveConfig
;
14 import org
.asamk
.signal
.output
.JsonWriter
;
15 import org
.asamk
.signal
.output
.OutputWriter
;
16 import org
.asamk
.signal
.output
.PlainTextWriter
;
17 import org
.slf4j
.Logger
;
18 import org
.slf4j
.LoggerFactory
;
20 import java
.io
.IOException
;
21 import java
.time
.Duration
;
22 import java
.util
.List
;
23 import java
.util
.Optional
;
25 public class ReceiveCommand
implements LocalCommand
{
27 private final static Logger logger
= LoggerFactory
.getLogger(ReceiveCommand
.class);
30 public String
getName() {
35 public void attachToSubparser(final Subparser subparser
) {
36 subparser
.help("Query the server for new messages.");
37 subparser
.addArgument("-t", "--timeout")
40 .help("Number of seconds to wait for new messages (negative values disable timeout)");
41 subparser
.addArgument("--max-messages")
44 .help("Maximum number of messages to receive, before returning.");
45 subparser
.addArgument("--ignore-attachments")
46 .help("Don’t download attachments of received messages.")
47 .action(Arguments
.storeTrue());
48 subparser
.addArgument("--ignore-stories")
49 .help("Don’t receive story messages from the server.")
50 .action(Arguments
.storeTrue());
51 subparser
.addArgument("--send-read-receipts")
52 .help("Send read receipts for all incoming data messages (in addition to the default delivery receipts)")
53 .action(Arguments
.storeTrue());
57 public List
<OutputType
> getSupportedOutputTypes() {
58 return List
.of(OutputType
.PLAIN_TEXT
, OutputType
.JSON
);
62 public void handleCommand(
63 final Namespace ns
, final Manager m
, final OutputWriter outputWriter
64 ) throws CommandException
{
65 final var timeout
= ns
.getDouble("timeout");
66 final var maxMessagesRaw
= ns
.getInt("max-messages");
67 final var ignoreAttachments
= Boolean
.TRUE
.equals(ns
.getBoolean("ignore-attachments"));
68 final var ignoreStories
= Boolean
.TRUE
.equals(ns
.getBoolean("ignore-stories"));
69 final var sendReadReceipts
= Boolean
.TRUE
.equals(ns
.getBoolean("send-read-receipts"));
70 m
.setReceiveConfig(new ReceiveConfig(ignoreAttachments
, ignoreStories
, sendReadReceipts
));
72 final var handler
= outputWriter
instanceof JsonWriter ?
new JsonReceiveMessageHandler(m
,
73 (JsonWriter
) outputWriter
) : new ReceiveMessageHandler(m
, (PlainTextWriter
) outputWriter
);
74 final var duration
= timeout
< 0 ?
null : Duration
.ofMillis((long) (timeout
* 1000));
75 final var maxMessages
= maxMessagesRaw
< 0 ?
null : maxMessagesRaw
;
76 m
.receiveMessages(Optional
.ofNullable(duration
), Optional
.ofNullable(maxMessages
), handler
);
77 } catch (IOException e
) {
78 throw new IOErrorException("Error while receiving messages: " + e
.getMessage(), e
);