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
;
8 import org
.asamk
.signal
.JsonReceiveMessageHandler
;
9 import org
.asamk
.signal
.JsonWriter
;
10 import org
.asamk
.signal
.OutputType
;
11 import org
.asamk
.signal
.PlainTextWriterImpl
;
12 import org
.asamk
.signal
.ReceiveMessageHandler
;
13 import org
.asamk
.signal
.json
.JsonMessageEnvelope
;
14 import org
.asamk
.signal
.manager
.Manager
;
15 import org
.asamk
.signal
.util
.DateUtils
;
16 import org
.freedesktop
.dbus
.connections
.impl
.DBusConnection
;
17 import org
.freedesktop
.dbus
.exceptions
.DBusException
;
18 import org
.slf4j
.Logger
;
19 import org
.slf4j
.LoggerFactory
;
21 import java
.io
.IOException
;
22 import java
.util
.Base64
;
25 import java
.util
.concurrent
.TimeUnit
;
27 import static org
.asamk
.signal
.util
.ErrorUtils
.handleAssertionError
;
29 public class ReceiveCommand
implements ExtendedDbusCommand
, LocalCommand
{
31 private final static Logger logger
= LoggerFactory
.getLogger(ReceiveCommand
.class);
34 public void attachToSubparser(final Subparser subparser
) {
35 subparser
.addArgument("-t", "--timeout")
37 .help("Number of seconds to wait for new messages (negative values disable timeout)");
38 subparser
.addArgument("--ignore-attachments")
39 .help("Don’t download attachments of received messages.")
40 .action(Arguments
.storeTrue());
41 subparser
.addArgument("--json")
42 .help("WARNING: This parameter is now deprecated! Please use the global \"--output=json\" option instead.\n\nOutput received messages in json format, one json object per line.")
43 .action(Arguments
.storeTrue());
47 public Set
<OutputType
> getSupportedOutputTypes() {
48 return Set
.of(OutputType
.PLAIN_TEXT
, OutputType
.JSON
);
51 public int handleCommand(final Namespace ns
, final Signal signal
, DBusConnection dbusconnection
) {
52 var inJson
= ns
.get("output") == OutputType
.JSON
|| ns
.getBoolean("json");
54 // TODO delete later when "json" variable is removed
55 if (ns
.getBoolean("json")) {
56 logger
.warn("\"--json\" option has been deprecated, please use the global \"--output=json\" instead.");
61 final var jsonWriter
= new JsonWriter(System
.out
);
63 dbusconnection
.addSigHandler(Signal
.MessageReceived
.class, messageReceived
-> {
64 var envelope
= new JsonMessageEnvelope(messageReceived
);
65 final var object
= Map
.of("envelope", envelope
);
67 jsonWriter
.write(object
);
68 } catch (IOException e
) {
69 logger
.error("Failed to write json object: {}", e
.getMessage());
73 dbusconnection
.addSigHandler(Signal
.ReceiptReceived
.class, receiptReceived
-> {
74 var envelope
= new JsonMessageEnvelope(receiptReceived
);
75 final var object
= Map
.of("envelope", envelope
);
77 jsonWriter
.write(object
);
78 } catch (IOException e
) {
79 logger
.error("Failed to write json object: {}", e
.getMessage());
83 dbusconnection
.addSigHandler(Signal
.SyncMessageReceived
.class, syncReceived
-> {
84 var envelope
= new JsonMessageEnvelope(syncReceived
);
85 final var object
= Map
.of("envelope", envelope
);
87 jsonWriter
.write(object
);
88 } catch (IOException e
) {
89 logger
.error("Failed to write json object: {}", e
.getMessage());
93 final var writer
= new PlainTextWriterImpl(System
.out
);
95 dbusconnection
.addSigHandler(Signal
.MessageReceived
.class, messageReceived
-> {
97 writer
.println("Envelope from: {}", messageReceived
.getSender());
98 writer
.println("Timestamp: {}", DateUtils
.formatTimestamp(messageReceived
.getTimestamp()));
99 writer
.println("Body: {}", messageReceived
.getMessage());
100 if (messageReceived
.getGroupId().length
> 0) {
101 writer
.println("Group info:");
102 writer
.indentedWriter()
104 Base64
.getEncoder().encodeToString(messageReceived
.getGroupId()));
106 if (messageReceived
.getAttachments().size() > 0) {
107 writer
.println("Attachments:");
108 for (var attachment
: messageReceived
.getAttachments()) {
109 writer
.println("- Stored plaintext in: {}", attachment
);
113 } catch (IOException e
) {
118 dbusconnection
.addSigHandler(Signal
.ReceiptReceived
.class, receiptReceived
-> {
120 writer
.println("Receipt from: {}", receiptReceived
.getSender());
121 writer
.println("Timestamp: {}", DateUtils
.formatTimestamp(receiptReceived
.getTimestamp()));
122 } catch (IOException e
) {
127 dbusconnection
.addSigHandler(Signal
.SyncMessageReceived
.class, syncReceived
-> {
129 writer
.println("Sync Envelope from: {} to: {}",
130 syncReceived
.getSource(),
131 syncReceived
.getDestination());
132 writer
.println("Timestamp: {}", DateUtils
.formatTimestamp(syncReceived
.getTimestamp()));
133 writer
.println("Body: {}", syncReceived
.getMessage());
134 if (syncReceived
.getGroupId().length
> 0) {
135 writer
.println("Group info:");
136 writer
.indentedWriter()
137 .println("Id: {}", Base64
.getEncoder().encodeToString(syncReceived
.getGroupId()));
139 if (syncReceived
.getAttachments().size() > 0) {
140 writer
.println("Attachments:");
141 for (var attachment
: syncReceived
.getAttachments()) {
142 writer
.println("- Stored plaintext in: {}", attachment
);
146 } catch (IOException e
) {
151 } catch (DBusException e
) {
158 } catch (InterruptedException e
) {
165 public int handleCommand(final Namespace ns
, final Manager m
) {
166 var inJson
= ns
.get("output") == OutputType
.JSON
|| ns
.getBoolean("json");
168 // TODO delete later when "json" variable is removed
169 if (ns
.getBoolean("json")) {
170 logger
.warn("\"--json\" option has been deprecated, please use the global \"--output=json\" instead.");
174 if (ns
.getDouble("timeout") != null) {
175 timeout
= ns
.getDouble("timeout");
177 var returnOnTimeout
= true;
179 returnOnTimeout
= false;
182 boolean ignoreAttachments
= ns
.getBoolean("ignore_attachments");
184 final var handler
= inJson ?
new JsonReceiveMessageHandler(m
) : new ReceiveMessageHandler(m
);
185 m
.receiveMessages((long) (timeout
* 1000),
186 TimeUnit
.MILLISECONDS
,
191 } catch (IOException e
) {
192 System
.err
.println("Error while receiving messages: " + e
.getMessage());
194 } catch (AssertionError e
) {
195 handleAssertionError(e
);