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 } catch (UnsatisfiedLinkError e
) {
58 System
.err
.println("Missing native library dependency for dbus service: " + e
.getMessage());
60 } catch (DBusException e
) {
67 } catch (InterruptedException e
) {
76 public int handleCommand(final Namespace ns
, final Manager m
) {
77 if (!m
.isRegistered()) {
78 System
.err
.println("User is not registered.");
82 if (ns
.getDouble("timeout") != null) {
83 timeout
= ns
.getDouble("timeout");
85 boolean returnOnTimeout
= true;
87 returnOnTimeout
= false;
90 boolean ignoreAttachments
= ns
.getBoolean("ignore_attachments");
92 final Manager
.ReceiveMessageHandler handler
= ns
.getBoolean("json") ?
new JsonReceiveMessageHandler(m
) : new ReceiveMessageHandler(m
);
93 m
.receiveMessages((long) (timeout
* 1000), TimeUnit
.MILLISECONDS
, returnOnTimeout
, ignoreAttachments
, handler
);
95 } catch (IOException e
) {
96 System
.err
.println("Error while receiving messages: " + e
.getMessage());
98 } catch (AssertionError e
) {
99 handleAssertionError(e
);