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
.whispersystems
.util
.Base64
;
23 // TODO delete later when "json" variable is removed
24 import org
.slf4j
.Logger
;
25 import org
.slf4j
.LoggerFactory
;
27 import java
.io
.IOException
;
28 import java
.util
.concurrent
.TimeUnit
;
30 import static org
.asamk
.signal
.util
.ErrorUtils
.handleAssertionError
;
32 public class ReceiveCommand
implements ExtendedDbusCommand
, LocalCommand
{
34 // TODO delete later when "json" variable is removed
35 final static Logger logger
= LoggerFactory
.getLogger(ReceiveCommand
.class);
38 public void attachToSubparser(final Subparser subparser
) {
39 subparser
.addArgument("-t", "--timeout")
41 .help("Number of seconds to wait for new messages (negative values disable timeout)");
42 subparser
.addArgument("--ignore-attachments")
43 .help("Don’t download attachments of received messages.")
44 .action(Arguments
.storeTrue());
45 subparser
.addArgument("--json")
46 .help("WARNING: This parameter is now deprecated! Please use the \"output\" option instead.\n\nOutput received messages in json format, one json object per line.")
47 .action(Arguments
.storeTrue());
50 public int handleCommand(final Namespace ns
, final Signal signal
, DBusConnection dbusconnection
) {
51 final ObjectMapper jsonProcessor
;
53 boolean inJson
= ns
.getString("output").equals("json") || ns
.getBoolean("json");
55 // TODO delete later when "json" variable is removed
56 if (ns
.getBoolean("json")) {
57 logger
.warn("\"--json\" option has been deprecated, please use \"output\" instead.");
61 jsonProcessor
= new ObjectMapper();
62 jsonProcessor
.setVisibility(PropertyAccessor
.ALL
, JsonAutoDetect
.Visibility
.ANY
);
63 jsonProcessor
.disable(JsonGenerator
.Feature
.AUTO_CLOSE_TARGET
);
68 dbusconnection
.addSigHandler(Signal
.MessageReceived
.class, messageReceived
-> {
69 if (jsonProcessor
!= null) {
70 JsonMessageEnvelope envelope
= new JsonMessageEnvelope(messageReceived
);
71 ObjectNode result
= jsonProcessor
.createObjectNode();
72 result
.putPOJO("envelope", envelope
);
74 jsonProcessor
.writeValue(System
.out
, result
);
76 } catch (IOException e
) {
80 System
.out
.print(String
.format("Envelope from: %s\nTimestamp: %s\nBody: %s\n",
81 messageReceived
.getSender(),
82 DateUtils
.formatTimestamp(messageReceived
.getTimestamp()),
83 messageReceived
.getMessage()));
84 if (messageReceived
.getGroupId().length
> 0) {
85 System
.out
.println("Group info:");
86 System
.out
.println(" Id: " + Base64
.encodeBytes(messageReceived
.getGroupId()));
88 if (messageReceived
.getAttachments().size() > 0) {
89 System
.out
.println("Attachments: ");
90 for (String attachment
: messageReceived
.getAttachments()) {
91 System
.out
.println("- Stored plaintext in: " + attachment
);
98 dbusconnection
.addSigHandler(Signal
.ReceiptReceived
.class, receiptReceived
-> {
99 if (jsonProcessor
!= null) {
100 JsonMessageEnvelope envelope
= new JsonMessageEnvelope(receiptReceived
);
101 ObjectNode result
= jsonProcessor
.createObjectNode();
102 result
.putPOJO("envelope", envelope
);
104 jsonProcessor
.writeValue(System
.out
, result
);
105 System
.out
.println();
106 } catch (IOException e
) {
110 System
.out
.print(String
.format("Receipt from: %s\nTimestamp: %s\n",
111 receiptReceived
.getSender(),
112 DateUtils
.formatTimestamp(receiptReceived
.getTimestamp())));
116 dbusconnection
.addSigHandler(Signal
.SyncMessageReceived
.class, syncReceived
-> {
117 if (jsonProcessor
!= null) {
118 JsonMessageEnvelope envelope
= new JsonMessageEnvelope(syncReceived
);
119 ObjectNode result
= jsonProcessor
.createObjectNode();
120 result
.putPOJO("envelope", envelope
);
122 jsonProcessor
.writeValue(System
.out
, result
);
123 System
.out
.println();
124 } catch (IOException e
) {
128 System
.out
.print(String
.format("Sync Envelope from: %s to: %s\nTimestamp: %s\nBody: %s\n",
129 syncReceived
.getSource(),
130 syncReceived
.getDestination(),
131 DateUtils
.formatTimestamp(syncReceived
.getTimestamp()),
132 syncReceived
.getMessage()));
133 if (syncReceived
.getGroupId().length
> 0) {
134 System
.out
.println("Group info:");
135 System
.out
.println(" Id: " + Base64
.encodeBytes(syncReceived
.getGroupId()));
137 if (syncReceived
.getAttachments().size() > 0) {
138 System
.out
.println("Attachments: ");
139 for (String attachment
: syncReceived
.getAttachments()) {
140 System
.out
.println("- Stored plaintext in: " + attachment
);
143 System
.out
.println();
146 } catch (UnsatisfiedLinkError e
) {
147 System
.err
.println("Missing native library dependency for dbus service: " + e
.getMessage());
149 } catch (DBusException e
) {
156 } catch (InterruptedException e
) {
163 public int handleCommand(final Namespace ns
, final Manager m
) {
164 boolean inJson
= ns
.getString("output").equals("json") || ns
.getBoolean("json");
166 // TODO delete later when "json" variable is removed
167 if (ns
.getBoolean("json")) {
168 logger
.warn("\"--json\" option has been deprecated, please use \"output\" instead.");
172 if (ns
.getDouble("timeout") != null) {
173 timeout
= ns
.getDouble("timeout");
175 boolean returnOnTimeout
= true;
177 returnOnTimeout
= false;
180 boolean ignoreAttachments
= ns
.getBoolean("ignore_attachments");
182 final Manager
.ReceiveMessageHandler handler
= inJson
183 ?
new JsonReceiveMessageHandler(m
)
184 : 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
);