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
.DBusSigHandler
;
14 import org
.freedesktop
.dbus
.exceptions
.DBusException
;
15 import org
.whispersystems
.signalservice
.internal
.util
.Base64
;
17 import java
.io
.IOException
;
18 import java
.util
.concurrent
.TimeUnit
;
20 import static org
.asamk
.signal
.util
.ErrorUtils
.handleAssertionError
;
22 public class ReceiveCommand
implements ExtendedDbusCommand
, LocalCommand
{
25 public void attachToSubparser(final Subparser subparser
) {
26 subparser
.addArgument("-t", "--timeout")
28 .help("Number of seconds to wait for new messages (negative values disable timeout)");
29 subparser
.addArgument("--ignore-attachments")
30 .help("Don’t download attachments of received messages.")
31 .action(Arguments
.storeTrue());
32 subparser
.addArgument("--json")
33 .help("Output received messages in json format, one json object per line.")
34 .action(Arguments
.storeTrue());
37 public int handleCommand(final Namespace ns
, final Signal signal
, DBusConnection dbusconnection
) {
38 if (dbusconnection
!= null) {
40 dbusconnection
.addSigHandler(Signal
.MessageReceived
.class, new DBusSigHandler
<Signal
.MessageReceived
>() {
42 public void handle(Signal
.MessageReceived s
) {
43 System
.out
.print(String
.format("Envelope from: %s\nTimestamp: %s\nBody: %s\n",
44 s
.getSender(), DateUtils
.formatTimestamp(s
.getTimestamp()), s
.getMessage()));
45 if (s
.getGroupId().length
> 0) {
46 System
.out
.println("Group info:");
47 System
.out
.println(" Id: " + Base64
.encodeBytes(s
.getGroupId()));
49 if (s
.getAttachments().size() > 0) {
50 System
.out
.println("Attachments: ");
51 for (String attachment
: s
.getAttachments()) {
52 System
.out
.println("- Stored plaintext in: " + attachment
);
58 dbusconnection
.addSigHandler(Signal
.ReceiptReceived
.class, new DBusSigHandler
<Signal
.ReceiptReceived
>() {
60 public void handle(Signal
.ReceiptReceived s
) {
61 System
.out
.print(String
.format("Receipt from: %s\nTimestamp: %s\n",
62 s
.getSender(), DateUtils
.formatTimestamp(s
.getTimestamp())));
65 } catch (UnsatisfiedLinkError e
) {
66 System
.err
.println("Missing native library dependency for dbus service: " + e
.getMessage());
68 } catch (DBusException e
) {
75 } catch (InterruptedException e
) {
84 public int handleCommand(final Namespace ns
, final Manager m
) {
85 if (!m
.isRegistered()) {
86 System
.err
.println("User is not registered.");
90 if (ns
.getDouble("timeout") != null) {
91 timeout
= ns
.getDouble("timeout");
93 boolean returnOnTimeout
= true;
95 returnOnTimeout
= false;
98 boolean ignoreAttachments
= ns
.getBoolean("ignore_attachments");
100 final Manager
.ReceiveMessageHandler handler
= ns
.getBoolean("json") ?
new JsonReceiveMessageHandler(m
) : new ReceiveMessageHandler(m
);
101 m
.receiveMessages((long) (timeout
* 1000), TimeUnit
.MILLISECONDS
, returnOnTimeout
, ignoreAttachments
, handler
);
103 } catch (IOException e
) {
104 System
.err
.println("Error while receiving messages: " + e
.getMessage());
106 } catch (AssertionError e
) {
107 handleAssertionError(e
);