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
.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, messageReceived
-> {
41 System
.out
.print(String
.format("Envelope from: %s\nTimestamp: %s\nBody: %s\n",
42 messageReceived
.getSender(), DateUtils
.formatTimestamp(messageReceived
.getTimestamp()), messageReceived
.getMessage()));
43 if (messageReceived
.getGroupId().length
> 0) {
44 System
.out
.println("Group info:");
45 System
.out
.println(" Id: " + Base64
.encodeBytes(messageReceived
.getGroupId()));
47 if (messageReceived
.getAttachments().size() > 0) {
48 System
.out
.println("Attachments: ");
49 for (String attachment
: messageReceived
.getAttachments()) {
50 System
.out
.println("- Stored plaintext in: " + attachment
);
55 dbusconnection
.addSigHandler(Signal
.ReceiptReceived
.class,
56 receiptReceived
-> System
.out
.print(String
.format("Receipt from: %s\nTimestamp: %s\n",
57 receiptReceived
.getSender(), DateUtils
.formatTimestamp(receiptReceived
.getTimestamp()))));
58 } catch (UnsatisfiedLinkError e
) {
59 System
.err
.println("Missing native library dependency for dbus service: " + e
.getMessage());
61 } catch (DBusException e
) {
68 } catch (InterruptedException e
) {
77 public int handleCommand(final Namespace ns
, final Manager m
) {
78 if (!m
.isRegistered()) {
79 System
.err
.println("User is not registered.");
83 if (ns
.getDouble("timeout") != null) {
84 timeout
= ns
.getDouble("timeout");
86 boolean returnOnTimeout
= true;
88 returnOnTimeout
= false;
91 boolean ignoreAttachments
= ns
.getBoolean("ignore_attachments");
93 final Manager
.ReceiveMessageHandler handler
= ns
.getBoolean("json") ?
new JsonReceiveMessageHandler(m
) : new ReceiveMessageHandler(m
);
94 m
.receiveMessages((long) (timeout
* 1000), TimeUnit
.MILLISECONDS
, returnOnTimeout
, ignoreAttachments
, handler
);
96 } catch (IOException e
) {
97 System
.err
.println("Error while receiving messages: " + e
.getMessage());
99 } catch (AssertionError e
) {
100 handleAssertionError(e
);