]> nmode's Git Repositories - signal-cli/blob - src/main/java/org/asamk/signal/commands/ReceiveCommand.java
42ab732704b92c81c015b0bade1bd40731d48ce4
[signal-cli] / src / main / java / org / asamk / signal / commands / ReceiveCommand.java
1 package org.asamk.signal.commands;
2
3 import net.sourceforge.argparse4j.impl.Arguments;
4 import net.sourceforge.argparse4j.inf.Namespace;
5 import net.sourceforge.argparse4j.inf.Subparser;
6
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;
15
16 import java.io.IOException;
17 import java.util.concurrent.TimeUnit;
18
19 import static org.asamk.signal.util.ErrorUtils.handleAssertionError;
20
21 public class ReceiveCommand implements ExtendedDbusCommand, LocalCommand {
22
23 @Override
24 public void attachToSubparser(final Subparser subparser) {
25 subparser.addArgument("-t", "--timeout")
26 .type(double.class)
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());
34 }
35
36 public int handleCommand(final Namespace ns, final Signal signal, DBusConnection dbusconnection) {
37 if (dbusconnection != null) {
38 try {
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()));
45 }
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);
50 }
51 }
52 System.out.println();
53 });
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()));
63 }
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);
68 }
69 }
70 System.out.println();
71 });
72 } catch (UnsatisfiedLinkError e) {
73 System.err.println("Missing native library dependency for dbus service: " + e.getMessage());
74 return 1;
75 } catch (DBusException e) {
76 e.printStackTrace();
77 return 1;
78 }
79 while (true) {
80 try {
81 Thread.sleep(10000);
82 } catch (InterruptedException e) {
83 return 0;
84 }
85 }
86 }
87 return 0;
88 }
89
90 @Override
91 public int handleCommand(final Namespace ns, final Manager m) {
92 if (!m.isRegistered()) {
93 System.err.println("User is not registered.");
94 return 1;
95 }
96 double timeout = 5;
97 if (ns.getDouble("timeout") != null) {
98 timeout = ns.getDouble("timeout");
99 }
100 boolean returnOnTimeout = true;
101 if (timeout < 0) {
102 returnOnTimeout = false;
103 timeout = 3600;
104 }
105 boolean ignoreAttachments = ns.getBoolean("ignore_attachments");
106 try {
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);
109 return 0;
110 } catch (IOException e) {
111 System.err.println("Error while receiving messages: " + e.getMessage());
112 return 3;
113 } catch (AssertionError e) {
114 handleAssertionError(e);
115 return 1;
116 }
117 }
118 }