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 import java
.io
.IOException
;
24 import java
.util
.concurrent
.TimeUnit
;
26 import static org
.asamk
.signal
.util
.ErrorUtils
.handleAssertionError
;
28 public class ReceiveCommand
implements ExtendedDbusCommand
, LocalCommand
{
31 public void attachToSubparser(final Subparser subparser
) {
32 subparser
.addArgument("-t", "--timeout")
34 .help("Number of seconds to wait for new messages (negative values disable timeout)");
35 subparser
.addArgument("--ignore-attachments")
36 .help("Don’t download attachments of received messages.")
37 .action(Arguments
.storeTrue());
38 subparser
.addArgument("--json")
39 .help("Output received messages in json format, one json object per line.")
40 .action(Arguments
.storeTrue());
43 public int handleCommand(final Namespace ns
, final Signal signal
, DBusConnection dbusconnection
) {
44 final ObjectMapper jsonProcessor
;
45 if (ns
.getBoolean("json")) {
46 jsonProcessor
= new ObjectMapper();
47 jsonProcessor
.setVisibility(PropertyAccessor
.ALL
, JsonAutoDetect
.Visibility
.ANY
);
48 jsonProcessor
.disable(JsonGenerator
.Feature
.AUTO_CLOSE_TARGET
);
53 dbusconnection
.addSigHandler(Signal
.MessageReceived
.class, messageReceived
-> {
54 if (jsonProcessor
!= null) {
55 JsonMessageEnvelope envelope
= new JsonMessageEnvelope(messageReceived
);
56 ObjectNode result
= jsonProcessor
.createObjectNode();
57 result
.putPOJO("envelope", envelope
);
59 jsonProcessor
.writeValue(System
.out
, result
);
61 } catch (IOException e
) {
65 System
.out
.print(String
.format("Envelope from: %s\nTimestamp: %s\nBody: %s\n",
66 messageReceived
.getSender(),
67 DateUtils
.formatTimestamp(messageReceived
.getTimestamp()),
68 messageReceived
.getMessage()));
69 if (messageReceived
.getGroupId().length
> 0) {
70 System
.out
.println("Group info:");
71 System
.out
.println(" Id: " + Base64
.encodeBytes(messageReceived
.getGroupId()));
73 if (messageReceived
.getAttachments().size() > 0) {
74 System
.out
.println("Attachments: ");
75 for (String attachment
: messageReceived
.getAttachments()) {
76 System
.out
.println("- Stored plaintext in: " + attachment
);
83 dbusconnection
.addSigHandler(Signal
.ReceiptReceived
.class, receiptReceived
-> {
84 if (jsonProcessor
!= null) {
85 JsonMessageEnvelope envelope
= new JsonMessageEnvelope(receiptReceived
);
86 ObjectNode result
= jsonProcessor
.createObjectNode();
87 result
.putPOJO("envelope", envelope
);
89 jsonProcessor
.writeValue(System
.out
, result
);
91 } catch (IOException e
) {
95 System
.out
.print(String
.format("Receipt from: %s\nTimestamp: %s\n",
96 receiptReceived
.getSender(),
97 DateUtils
.formatTimestamp(receiptReceived
.getTimestamp())));
101 dbusconnection
.addSigHandler(Signal
.SyncMessageReceived
.class, syncReceived
-> {
102 if (jsonProcessor
!= null) {
103 JsonMessageEnvelope envelope
= new JsonMessageEnvelope(syncReceived
);
104 ObjectNode result
= jsonProcessor
.createObjectNode();
105 result
.putPOJO("envelope", envelope
);
107 jsonProcessor
.writeValue(System
.out
, result
);
108 System
.out
.println();
109 } catch (IOException e
) {
113 System
.out
.print(String
.format("Sync Envelope from: %s to: %s\nTimestamp: %s\nBody: %s\n",
114 syncReceived
.getSource(),
115 syncReceived
.getDestination(),
116 DateUtils
.formatTimestamp(syncReceived
.getTimestamp()),
117 syncReceived
.getMessage()));
118 if (syncReceived
.getGroupId().length
> 0) {
119 System
.out
.println("Group info:");
120 System
.out
.println(" Id: " + Base64
.encodeBytes(syncReceived
.getGroupId()));
122 if (syncReceived
.getAttachments().size() > 0) {
123 System
.out
.println("Attachments: ");
124 for (String attachment
: syncReceived
.getAttachments()) {
125 System
.out
.println("- Stored plaintext in: " + attachment
);
128 System
.out
.println();
131 } catch (UnsatisfiedLinkError e
) {
132 System
.err
.println("Missing native library dependency for dbus service: " + e
.getMessage());
134 } catch (DBusException e
) {
141 } catch (InterruptedException e
) {
148 public int handleCommand(final Namespace ns
, final Manager m
) {
150 if (ns
.getDouble("timeout") != null) {
151 timeout
= ns
.getDouble("timeout");
153 boolean returnOnTimeout
= true;
155 returnOnTimeout
= false;
158 boolean ignoreAttachments
= ns
.getBoolean("ignore_attachments");
160 final Manager
.ReceiveMessageHandler handler
= ns
.getBoolean("json")
161 ?
new JsonReceiveMessageHandler(m
)
162 : new ReceiveMessageHandler(m
);
163 m
.receiveMessages((long) (timeout
* 1000),
164 TimeUnit
.MILLISECONDS
,
169 } catch (IOException e
) {
170 System
.err
.println("Error while receiving messages: " + e
.getMessage());
172 } catch (AssertionError e
) {
173 handleAssertionError(e
);