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
.ReceiveMessageHandler
;
16 import org
.asamk
.signal
.json
.JsonMessageEnvelope
;
17 import org
.asamk
.signal
.manager
.Manager
;
18 import org
.asamk
.signal
.util
.DateUtils
;
19 import org
.freedesktop
.dbus
.connections
.impl
.DBusConnection
;
20 import org
.freedesktop
.dbus
.exceptions
.DBusException
;
21 import org
.slf4j
.Logger
;
22 import org
.slf4j
.LoggerFactory
;
23 import org
.whispersystems
.util
.Base64
;
25 import java
.io
.IOException
;
26 import java
.util
.concurrent
.TimeUnit
;
28 import static org
.asamk
.signal
.util
.ErrorUtils
.handleAssertionError
;
30 public class ReceiveCommand
implements ExtendedDbusCommand
, LocalCommand
{
32 // TODO delete later when "json" variable is removed
33 final static Logger logger
= LoggerFactory
.getLogger(ReceiveCommand
.class);
36 public void attachToSubparser(final Subparser subparser
) {
37 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("--json")
44 .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.")
45 .action(Arguments
.storeTrue());
48 public int handleCommand(final Namespace ns
, final Signal signal
, DBusConnection dbusconnection
) {
49 final ObjectMapper jsonProcessor
;
51 boolean inJson
= ns
.getString("output").equals("json") || ns
.getBoolean("json");
53 // TODO delete later when "json" variable is removed
54 if (ns
.getBoolean("json")) {
55 logger
.warn("\"--json\" option has been deprecated, please use the global \"--output=json\" instead.");
59 jsonProcessor
= new ObjectMapper();
60 jsonProcessor
.setVisibility(PropertyAccessor
.ALL
, JsonAutoDetect
.Visibility
.ANY
);
61 jsonProcessor
.disable(JsonGenerator
.Feature
.AUTO_CLOSE_TARGET
);
66 dbusconnection
.addSigHandler(Signal
.MessageReceived
.class, messageReceived
-> {
67 if (jsonProcessor
!= null) {
68 JsonMessageEnvelope envelope
= new JsonMessageEnvelope(messageReceived
);
69 ObjectNode result
= jsonProcessor
.createObjectNode();
70 result
.putPOJO("envelope", envelope
);
72 jsonProcessor
.writeValue(System
.out
, result
);
74 } catch (IOException e
) {
78 System
.out
.print(String
.format("Envelope from: %s\nTimestamp: %s\nBody: %s\n",
79 messageReceived
.getSender(),
80 DateUtils
.formatTimestamp(messageReceived
.getTimestamp()),
81 messageReceived
.getMessage()));
82 if (messageReceived
.getGroupId().length
> 0) {
83 System
.out
.println("Group info:");
84 System
.out
.println(" Id: " + Base64
.encodeBytes(messageReceived
.getGroupId()));
86 if (messageReceived
.getAttachments().size() > 0) {
87 System
.out
.println("Attachments: ");
88 for (String attachment
: messageReceived
.getAttachments()) {
89 System
.out
.println("- Stored plaintext in: " + attachment
);
96 dbusconnection
.addSigHandler(Signal
.ReceiptReceived
.class, receiptReceived
-> {
97 if (jsonProcessor
!= null) {
98 JsonMessageEnvelope envelope
= new JsonMessageEnvelope(receiptReceived
);
99 ObjectNode result
= jsonProcessor
.createObjectNode();
100 result
.putPOJO("envelope", envelope
);
102 jsonProcessor
.writeValue(System
.out
, result
);
103 System
.out
.println();
104 } catch (IOException e
) {
108 System
.out
.print(String
.format("Receipt from: %s\nTimestamp: %s\n",
109 receiptReceived
.getSender(),
110 DateUtils
.formatTimestamp(receiptReceived
.getTimestamp())));
114 dbusconnection
.addSigHandler(Signal
.SyncMessageReceived
.class, syncReceived
-> {
115 if (jsonProcessor
!= null) {
116 JsonMessageEnvelope envelope
= new JsonMessageEnvelope(syncReceived
);
117 ObjectNode result
= jsonProcessor
.createObjectNode();
118 result
.putPOJO("envelope", envelope
);
120 jsonProcessor
.writeValue(System
.out
, result
);
121 System
.out
.println();
122 } catch (IOException e
) {
126 System
.out
.print(String
.format("Sync Envelope from: %s to: %s\nTimestamp: %s\nBody: %s\n",
127 syncReceived
.getSource(),
128 syncReceived
.getDestination(),
129 DateUtils
.formatTimestamp(syncReceived
.getTimestamp()),
130 syncReceived
.getMessage()));
131 if (syncReceived
.getGroupId().length
> 0) {
132 System
.out
.println("Group info:");
133 System
.out
.println(" Id: " + Base64
.encodeBytes(syncReceived
.getGroupId()));
135 if (syncReceived
.getAttachments().size() > 0) {
136 System
.out
.println("Attachments: ");
137 for (String attachment
: syncReceived
.getAttachments()) {
138 System
.out
.println("- Stored plaintext in: " + attachment
);
141 System
.out
.println();
144 } catch (UnsatisfiedLinkError e
) {
145 System
.err
.println("Missing native library dependency for dbus service: " + e
.getMessage());
147 } catch (DBusException e
) {
154 } catch (InterruptedException e
) {
161 public int handleCommand(final Namespace ns
, final Manager m
) {
162 boolean inJson
= ns
.getString("output").equals("json") || ns
.getBoolean("json");
164 // TODO delete later when "json" variable is removed
165 if (ns
.getBoolean("json")) {
166 logger
.warn("\"--json\" option has been deprecated, please use the global \"--output=json\" instead.");
170 if (ns
.getDouble("timeout") != null) {
171 timeout
= ns
.getDouble("timeout");
173 boolean returnOnTimeout
= true;
175 returnOnTimeout
= false;
178 boolean ignoreAttachments
= ns
.getBoolean("ignore_attachments");
180 final Manager
.ReceiveMessageHandler handler
= inJson
181 ?
new JsonReceiveMessageHandler(m
)
182 : new ReceiveMessageHandler(m
);
183 m
.receiveMessages((long) (timeout
* 1000),
184 TimeUnit
.MILLISECONDS
,
189 } catch (IOException e
) {
190 System
.err
.println("Error while receiving messages: " + e
.getMessage());
192 } catch (AssertionError e
) {
193 handleAssertionError(e
);