1 package org
.asamk
.signal
.commands
;
3 import net
.sourceforge
.argparse4j
.impl
.Arguments
;
4 import net
.sourceforge
.argparse4j
.inf
.Namespace
;
5 import net
.sourceforge
.argparse4j
.inf
.Subparser
;
7 import org
.asamk
.Signal
;
8 import org
.asamk
.signal
.JsonReceiveMessageHandler
;
9 import org
.asamk
.signal
.ReceiveMessageHandler
;
10 import org
.asamk
.signal
.manager
.Manager
;
11 import org
.asamk
.signal
.util
.DateUtils
;
12 import org
.freedesktop
.dbus
.DBusConnection
;
13 import org
.freedesktop
.dbus
.exceptions
.DBusException
;
14 import org
.whispersystems
.util
.Base64
;
16 import java
.io
.IOException
;
17 import java
.util
.concurrent
.TimeUnit
;
19 import static org
.asamk
.signal
.util
.ErrorUtils
.handleAssertionError
;
21 public class ReceiveCommand
implements ExtendedDbusCommand
, LocalCommand
{
24 public void attachToSubparser(final Subparser subparser
) {
25 subparser
.addArgument("-t", "--timeout")
27 .help("Number of seconds to wait for new messages (negative values disable timeout)");
28 subparser
.addArgument("--ignore-attachments")
29 .help("Don’t download attachments of received messages.")
30 .action(Arguments
.storeTrue());
31 subparser
.addArgument("--json")
32 .help("Output received messages in json format, one json object per line.")
33 .action(Arguments
.storeTrue());
36 public int handleCommand(final Namespace ns
, final Signal signal
, DBusConnection dbusconnection
) {
37 if (dbusconnection
!= null) {
39 dbusconnection
.addSigHandler(Signal
.MessageReceived
.class, messageReceived
-> {
40 System
.out
.print(String
.format("Envelope from: %s\nTimestamp: %s\nBody: %s\n",
41 messageReceived
.getSender(), DateUtils
.formatTimestamp(messageReceived
.getTimestamp()), messageReceived
.getMessage()));
42 if (messageReceived
.getGroupId().length
> 0) {
43 System
.out
.println("Group info:");
44 System
.out
.println(" Id: " + Base64
.encodeBytes(messageReceived
.getGroupId()));
46 if (messageReceived
.getAttachments().size() > 0) {
47 System
.out
.println("Attachments: ");
48 for (String attachment
: messageReceived
.getAttachments()) {
49 System
.out
.println("- Stored plaintext in: " + attachment
);
54 dbusconnection
.addSigHandler(Signal
.ReceiptReceived
.class,
55 receiptReceived
-> System
.out
.print(String
.format("Receipt from: %s\nTimestamp: %s\n",
56 receiptReceived
.getSender(), DateUtils
.formatTimestamp(receiptReceived
.getTimestamp()))));
57 dbusconnection
.addSigHandler(Signal
.SyncMessageReceived
.class, syncReceived
-> {
58 System
.out
.print(String
.format("Sync Envelope from: %s to: %s\nTimestamp: %s\nBody: %s\n",
59 syncReceived
.getSource(), syncReceived
.getDestination(), DateUtils
.formatTimestamp(syncReceived
.getTimestamp()), syncReceived
.getMessage()));
60 if (syncReceived
.getGroupId().length
> 0) {
61 System
.out
.println("Group info:");
62 System
.out
.println(" Id: " + Base64
.encodeBytes(syncReceived
.getGroupId()));
64 if (syncReceived
.getAttachments().size() > 0) {
65 System
.out
.println("Attachments: ");
66 for (String attachment
: syncReceived
.getAttachments()) {
67 System
.out
.println("- Stored plaintext in: " + attachment
);
72 } catch (UnsatisfiedLinkError e
) {
73 System
.err
.println("Missing native library dependency for dbus service: " + e
.getMessage());
75 } catch (DBusException e
) {
82 } catch (InterruptedException e
) {
91 public int handleCommand(final Namespace ns
, final Manager m
) {
92 if (!m
.isRegistered()) {
93 System
.err
.println("User is not registered.");
97 if (ns
.getDouble("timeout") != null) {
98 timeout
= ns
.getDouble("timeout");
100 boolean returnOnTimeout
= true;
102 returnOnTimeout
= false;
105 boolean ignoreAttachments
= ns
.getBoolean("ignore_attachments");
107 final Manager
.ReceiveMessageHandler handler
= ns
.getBoolean("json") ?
new JsonReceiveMessageHandler(m
) : new ReceiveMessageHandler(m
);
108 m
.receiveMessages((long) (timeout
* 1000), TimeUnit
.MILLISECONDS
, returnOnTimeout
, ignoreAttachments
, handler
);
110 } catch (IOException e
) {
111 System
.err
.println("Error while receiving messages: " + e
.getMessage());
113 } catch (AssertionError e
) {
114 handleAssertionError(e
);