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
;
6 import org
.asamk
.Signal
;
7 import org
.asamk
.signal
.JsonReceiveMessageHandler
;
8 import org
.asamk
.signal
.ReceiveMessageHandler
;
9 import org
.asamk
.signal
.manager
.Manager
;
10 import org
.asamk
.signal
.util
.DateUtils
;
11 import org
.freedesktop
.dbus
.DBusConnection
;
12 import org
.freedesktop
.dbus
.DBusSigHandler
;
13 import org
.freedesktop
.dbus
.exceptions
.DBusException
;
14 import org
.whispersystems
.signalservice
.internal
.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, new DBusSigHandler
<Signal
.MessageReceived
>() {
41 public void handle(Signal
.MessageReceived s
) {
42 System
.out
.print(String
.format("Envelope from: %s\nTimestamp: %s\nBody: %s\n",
43 s
.getSender(), DateUtils
.formatTimestamp(s
.getTimestamp()), s
.getMessage()));
44 if (s
.getGroupId().length
> 0) {
45 System
.out
.println("Group info:");
46 System
.out
.println(" Id: " + Base64
.encodeBytes(s
.getGroupId()));
48 if (s
.getAttachments().size() > 0) {
49 System
.out
.println("Attachments: ");
50 for (String attachment
: s
.getAttachments()) {
51 System
.out
.println("- Stored plaintext in: " + attachment
);
57 dbusconnection
.addSigHandler(Signal
.ReceiptReceived
.class, new DBusSigHandler
<Signal
.ReceiptReceived
>() {
59 public void handle(Signal
.ReceiptReceived s
) {
60 System
.out
.print(String
.format("Receipt from: %s\nTimestamp: %s\n",
61 s
.getSender(), DateUtils
.formatTimestamp(s
.getTimestamp())));
64 } catch (UnsatisfiedLinkError e
) {
65 System
.err
.println("Missing native library dependency for dbus service: " + e
.getMessage());
67 } catch (DBusException e
) {
74 } catch (InterruptedException e
) {
83 public int handleCommand(final Namespace ns
, final Manager m
) {
84 if (!m
.isRegistered()) {
85 System
.err
.println("User is not registered.");
89 if (ns
.getDouble("timeout") != null) {
90 timeout
= ns
.getDouble("timeout");
92 boolean returnOnTimeout
= true;
94 returnOnTimeout
= false;
97 boolean ignoreAttachments
= ns
.getBoolean("ignore_attachments");
99 final Manager
.ReceiveMessageHandler handler
= ns
.getBoolean("json") ?
new JsonReceiveMessageHandler(m
) : new ReceiveMessageHandler(m
);
100 m
.receiveMessages((long) (timeout
* 1000), TimeUnit
.MILLISECONDS
, returnOnTimeout
, ignoreAttachments
, handler
);
102 } catch (IOException e
) {
103 System
.err
.println("Error while receiving messages: " + e
.getMessage());
105 } catch (AssertionError e
) {
106 handleAssertionError(e
);