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
.JsonReceiveMessageHandler
;
8 import org
.asamk
.signal
.JsonWriter
;
9 import org
.asamk
.signal
.OutputType
;
10 import org
.asamk
.signal
.OutputWriter
;
11 import org
.asamk
.signal
.PlainTextWriter
;
12 import org
.asamk
.signal
.ReceiveMessageHandler
;
13 import org
.asamk
.signal
.commands
.exceptions
.CommandException
;
14 import org
.asamk
.signal
.commands
.exceptions
.IOErrorException
;
15 import org
.asamk
.signal
.manager
.Manager
;
16 import org
.slf4j
.Logger
;
17 import org
.slf4j
.LoggerFactory
;
19 import java
.io
.IOException
;
20 import java
.util
.List
;
21 import java
.util
.concurrent
.TimeUnit
;
23 public class ReceiveCommand
implements LocalCommand
{
25 private final static Logger logger
= LoggerFactory
.getLogger(ReceiveCommand
.class);
28 public String
getName() {
33 public void attachToSubparser(final Subparser subparser
) {
34 subparser
.help("Query the server for new messages.");
35 subparser
.addArgument("-t", "--timeout")
38 .help("Number of seconds to wait for new messages (negative values disable timeout)");
39 subparser
.addArgument("--ignore-attachments")
40 .help("Don’t download attachments of received messages.")
41 .action(Arguments
.storeTrue());
45 public List
<OutputType
> getSupportedOutputTypes() {
46 return List
.of(OutputType
.PLAIN_TEXT
, OutputType
.JSON
);
50 public void handleCommand(
51 final Namespace ns
, final Manager m
, final OutputWriter outputWriter
52 ) throws CommandException
{
53 double timeout
= ns
.getDouble("timeout");
54 boolean ignoreAttachments
= Boolean
.TRUE
.equals(ns
.getBoolean("ignore-attachments"));
55 m
.setIgnoreAttachments(ignoreAttachments
);
57 final var handler
= outputWriter
instanceof JsonWriter ?
new JsonReceiveMessageHandler(m
,
58 (JsonWriter
) outputWriter
) : new ReceiveMessageHandler(m
, (PlainTextWriter
) outputWriter
);
60 m
.receiveMessages(handler
);
62 m
.receiveMessages((long) (timeout
* 1000), TimeUnit
.MILLISECONDS
, handler
);
64 } catch (IOException e
) {
65 throw new IOErrorException("Error while receiving messages: " + e
.getMessage(), e
);