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());
46 public List
<OutputType
> getSupportedOutputTypes() {
47 return List
.of(OutputType
.PLAIN_TEXT
, OutputType
.JSON
);
51 public void handleCommand(
52 final Namespace ns
, final Manager m
, final OutputWriter outputWriter
53 ) throws CommandException
{
54 double timeout
= ns
.getDouble("timeout");
55 boolean ignoreAttachments
= Boolean
.TRUE
.equals(ns
.getBoolean("ignore-attachments"));
56 m
.setReceiveConfig(new ReceiveConfig(ignoreAttachments
));
58 final var handler
= outputWriter
instanceof JsonWriter ?
new JsonReceiveMessageHandler(m
,
59 (JsonWriter
) outputWriter
) : new ReceiveMessageHandler(m
, (PlainTextWriter
) outputWriter
);
61 m
.receiveMessages(handler
);
63 m
.receiveMessages(Duration
.ofMillis((long) (timeout
* 1000)), handler
);
65 } catch (IOException e
) {
66 throw new IOErrorException("Error while receiving messages: " + e
.getMessage(), e
);