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
); // disable autodetect
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(), DateUtils
.formatTimestamp(messageReceived
.getTimestamp()), messageReceived
.getMessage()));
67 if (messageReceived
.getGroupId().length
> 0) {
68 System
.out
.println("Group info:");
69 System
.out
.println(" Id: " + Base64
.encodeBytes(messageReceived
.getGroupId()));
71 if (messageReceived
.getAttachments().size() > 0) {
72 System
.out
.println("Attachments: ");
73 for (String attachment
: messageReceived
.getAttachments()) {
74 System
.out
.println("- Stored plaintext in: " + attachment
);
81 dbusconnection
.addSigHandler(Signal
.ReceiptReceived
.class,
83 if (jsonProcessor
!= null) {
84 JsonMessageEnvelope envelope
= new JsonMessageEnvelope(receiptReceived
);
85 ObjectNode result
= jsonProcessor
.createObjectNode();
86 result
.putPOJO("envelope", envelope
);
88 jsonProcessor
.writeValue(System
.out
, result
);
90 } catch (IOException e
) {
94 System
.out
.print(String
.format("Receipt from: %s\nTimestamp: %s\n",
95 receiptReceived
.getSender(), DateUtils
.formatTimestamp(receiptReceived
.getTimestamp())));
99 dbusconnection
.addSigHandler(Signal
.SyncMessageReceived
.class, syncReceived
-> {
100 if (jsonProcessor
!= null) {
101 JsonMessageEnvelope envelope
= new JsonMessageEnvelope(syncReceived
);
102 ObjectNode result
= jsonProcessor
.createObjectNode();
103 result
.putPOJO("envelope", envelope
);
105 jsonProcessor
.writeValue(System
.out
, result
);
106 System
.out
.println();
107 } catch (IOException e
) {
111 System
.out
.print(String
.format("Sync Envelope from: %s to: %s\nTimestamp: %s\nBody: %s\n",
112 syncReceived
.getSource(), syncReceived
.getDestination(), DateUtils
.formatTimestamp(syncReceived
.getTimestamp()), syncReceived
.getMessage()));
113 if (syncReceived
.getGroupId().length
> 0) {
114 System
.out
.println("Group info:");
115 System
.out
.println(" Id: " + Base64
.encodeBytes(syncReceived
.getGroupId()));
117 if (syncReceived
.getAttachments().size() > 0) {
118 System
.out
.println("Attachments: ");
119 for (String attachment
: syncReceived
.getAttachments()) {
120 System
.out
.println("- Stored plaintext in: " + attachment
);
123 System
.out
.println();
126 } catch (UnsatisfiedLinkError e
) {
127 System
.err
.println("Missing native library dependency for dbus service: " + e
.getMessage());
129 } catch (DBusException e
) {
136 } catch (InterruptedException e
) {
143 public int handleCommand(final Namespace ns
, final Manager m
) {
144 if (!m
.isRegistered()) {
145 System
.err
.println("User is not registered.");
149 if (ns
.getDouble("timeout") != null) {
150 timeout
= ns
.getDouble("timeout");
152 boolean returnOnTimeout
= true;
154 returnOnTimeout
= false;
157 boolean ignoreAttachments
= ns
.getBoolean("ignore_attachments");
159 final Manager
.ReceiveMessageHandler handler
= ns
.getBoolean("json") ?
new JsonReceiveMessageHandler(m
) : new ReceiveMessageHandler(m
);
160 m
.receiveMessages((long) (timeout
* 1000), TimeUnit
.MILLISECONDS
, returnOnTimeout
, ignoreAttachments
, handler
);
162 } catch (IOException e
) {
163 System
.err
.println("Error while receiving messages: " + e
.getMessage());
165 } catch (AssertionError e
) {
166 handleAssertionError(e
);