1 package org
.asamk
.signal
.commands
;
3 import com
.fasterxml
.jackson
.annotation
.JsonAutoDetect
;
4 import com
.fasterxml
.jackson
.annotation
.PropertyAccessor
;
5 import com
.fasterxml
.jackson
.core
.JsonGenerator
;
6 import com
.fasterxml
.jackson
.databind
.ObjectMapper
;
7 import com
.fasterxml
.jackson
.databind
.node
.ObjectNode
;
9 import net
.sourceforge
.argparse4j
.impl
.Arguments
;
10 import net
.sourceforge
.argparse4j
.inf
.Namespace
;
11 import net
.sourceforge
.argparse4j
.inf
.Subparser
;
13 import org
.asamk
.Signal
;
14 import org
.asamk
.signal
.JsonReceiveMessageHandler
;
15 import org
.asamk
.signal
.OutputType
;
16 import org
.asamk
.signal
.ReceiveMessageHandler
;
17 import org
.asamk
.signal
.json
.JsonMessageEnvelope
;
18 import org
.asamk
.signal
.manager
.Manager
;
19 import org
.asamk
.signal
.util
.DateUtils
;
20 import org
.freedesktop
.dbus
.connections
.impl
.DBusConnection
;
21 import org
.freedesktop
.dbus
.exceptions
.DBusException
;
22 import org
.slf4j
.Logger
;
23 import org
.slf4j
.LoggerFactory
;
25 import java
.io
.IOException
;
26 import java
.util
.Base64
;
27 import java
.util
.concurrent
.TimeUnit
;
29 import static org
.asamk
.signal
.util
.ErrorUtils
.handleAssertionError
;
31 public class ReceiveCommand
implements ExtendedDbusCommand
, LocalCommand
{
33 // TODO delete later when "json" variable is removed
34 private final static Logger logger
= LoggerFactory
.getLogger(ReceiveCommand
.class);
37 public void attachToSubparser(final Subparser subparser
) {
38 subparser
.addArgument("-t", "--timeout")
40 .help("Number of seconds to wait for new messages (negative values disable timeout)");
41 subparser
.addArgument("--ignore-attachments")
42 .help("Don’t download attachments of received messages.")
43 .action(Arguments
.storeTrue());
44 subparser
.addArgument("--json")
45 .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.")
46 .action(Arguments
.storeTrue());
49 public int handleCommand(final Namespace ns
, final Signal signal
, DBusConnection dbusconnection
) {
50 final ObjectMapper jsonProcessor
;
52 boolean 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.");
60 jsonProcessor
= new ObjectMapper();
61 jsonProcessor
.setVisibility(PropertyAccessor
.ALL
, JsonAutoDetect
.Visibility
.ANY
);
62 jsonProcessor
.disable(JsonGenerator
.Feature
.AUTO_CLOSE_TARGET
);
67 dbusconnection
.addSigHandler(Signal
.MessageReceived
.class, messageReceived
-> {
68 if (jsonProcessor
!= null) {
69 JsonMessageEnvelope envelope
= new JsonMessageEnvelope(messageReceived
);
70 ObjectNode result
= jsonProcessor
.createObjectNode();
71 result
.putPOJO("envelope", envelope
);
73 jsonProcessor
.writeValue(System
.out
, result
);
75 } catch (IOException e
) {
79 System
.out
.print(String
.format("Envelope from: %s\nTimestamp: %s\nBody: %s\n",
80 messageReceived
.getSender(),
81 DateUtils
.formatTimestamp(messageReceived
.getTimestamp()),
82 messageReceived
.getMessage()));
83 if (messageReceived
.getGroupId().length
> 0) {
84 System
.out
.println("Group info:");
85 System
.out
.println(" Id: " + Base64
.getEncoder().encodeToString(messageReceived
.getGroupId()));
87 if (messageReceived
.getAttachments().size() > 0) {
88 System
.out
.println("Attachments: ");
89 for (String attachment
: messageReceived
.getAttachments()) {
90 System
.out
.println("- Stored plaintext in: " + attachment
);
97 dbusconnection
.addSigHandler(Signal
.ReceiptReceived
.class, receiptReceived
-> {
98 if (jsonProcessor
!= null) {
99 JsonMessageEnvelope envelope
= new JsonMessageEnvelope(receiptReceived
);
100 ObjectNode result
= jsonProcessor
.createObjectNode();
101 result
.putPOJO("envelope", envelope
);
103 jsonProcessor
.writeValue(System
.out
, result
);
104 System
.out
.println();
105 } catch (IOException e
) {
109 System
.out
.print(String
.format("Receipt from: %s\nTimestamp: %s\n",
110 receiptReceived
.getSender(),
111 DateUtils
.formatTimestamp(receiptReceived
.getTimestamp())));
115 dbusconnection
.addSigHandler(Signal
.SyncMessageReceived
.class, syncReceived
-> {
116 if (jsonProcessor
!= null) {
117 JsonMessageEnvelope envelope
= new JsonMessageEnvelope(syncReceived
);
118 ObjectNode result
= jsonProcessor
.createObjectNode();
119 result
.putPOJO("envelope", envelope
);
121 jsonProcessor
.writeValue(System
.out
, result
);
122 System
.out
.println();
123 } catch (IOException e
) {
127 System
.out
.print(String
.format("Sync Envelope from: %s to: %s\nTimestamp: %s\nBody: %s\n",
128 syncReceived
.getSource(),
129 syncReceived
.getDestination(),
130 DateUtils
.formatTimestamp(syncReceived
.getTimestamp()),
131 syncReceived
.getMessage()));
132 if (syncReceived
.getGroupId().length
> 0) {
133 System
.out
.println("Group info:");
134 System
.out
.println(" Id: " + Base64
.getEncoder().encodeToString(syncReceived
.getGroupId()));
136 if (syncReceived
.getAttachments().size() > 0) {
137 System
.out
.println("Attachments: ");
138 for (String attachment
: syncReceived
.getAttachments()) {
139 System
.out
.println("- Stored plaintext in: " + attachment
);
142 System
.out
.println();
145 } catch (DBusException e
) {
152 } catch (InterruptedException e
) {
159 public int handleCommand(final Namespace ns
, final Manager m
) {
160 boolean inJson
= ns
.get("output") == OutputType
.JSON
|| ns
.getBoolean("json");
162 // TODO delete later when "json" variable is removed
163 if (ns
.getBoolean("json")) {
164 logger
.warn("\"--json\" option has been deprecated, please use the global \"--output=json\" instead.");
168 if (ns
.getDouble("timeout") != null) {
169 timeout
= ns
.getDouble("timeout");
171 boolean returnOnTimeout
= true;
173 returnOnTimeout
= false;
176 boolean ignoreAttachments
= ns
.getBoolean("ignore_attachments");
178 final Manager
.ReceiveMessageHandler handler
= inJson
179 ?
new JsonReceiveMessageHandler(m
)
180 : new ReceiveMessageHandler(m
);
181 m
.receiveMessages((long) (timeout
* 1000),
182 TimeUnit
.MILLISECONDS
,
187 } catch (IOException e
) {
188 System
.err
.println("Error while receiving messages: " + e
.getMessage());
190 } catch (AssertionError e
) {
191 handleAssertionError(e
);