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
;
24 public class ReceiveCommand
implements LocalCommand
{
26 private final static Logger logger
= LoggerFactory
.getLogger(ReceiveCommand
.class);
29 public String
getName() {
34 public void attachToSubparser(final Subparser subparser
) {
35 subparser
.help("Query the server for new messages.");
36 subparser
.addArgument("-t", "--timeout")
39 .help("Number of seconds to wait for new messages (negative values disable timeout)");
40 subparser
.addArgument("--ignore-attachments")
41 .help("Don’t download attachments of received messages.")
42 .action(Arguments
.storeTrue());
43 subparser
.addArgument("--ignore-stories")
44 .help("Don’t receive story messages from the server.")
45 .action(Arguments
.storeTrue());
46 subparser
.addArgument("--send-read-receipts")
47 .help("Send read receipts for all incoming data messages (in addition to the default delivery receipts)")
48 .action(Arguments
.storeTrue());
52 public List
<OutputType
> getSupportedOutputTypes() {
53 return List
.of(OutputType
.PLAIN_TEXT
, OutputType
.JSON
);
57 public void handleCommand(
58 final Namespace ns
, final Manager m
, final OutputWriter outputWriter
59 ) throws CommandException
{
60 final var timeout
= ns
.getDouble("timeout");
61 final var ignoreAttachments
= Boolean
.TRUE
.equals(ns
.getBoolean("ignore-attachments"));
62 final var ignoreStories
= Boolean
.TRUE
.equals(ns
.getBoolean("ignore-stories"));
63 final var sendReadReceipts
= Boolean
.TRUE
.equals(ns
.getBoolean("send-read-receipts"));
64 m
.setReceiveConfig(new ReceiveConfig(ignoreAttachments
, ignoreStories
, sendReadReceipts
));
66 final var handler
= outputWriter
instanceof JsonWriter ?
new JsonReceiveMessageHandler(m
,
67 (JsonWriter
) outputWriter
) : new ReceiveMessageHandler(m
, (PlainTextWriter
) outputWriter
);
69 m
.receiveMessages(handler
);
71 m
.receiveMessages(Duration
.ofMillis((long) (timeout
* 1000)), handler
);
73 } catch (IOException e
) {
74 throw new IOErrorException("Error while receiving messages: " + e
.getMessage(), e
);