]> nmode's Git Repositories - signal-cli/blob - src/main/java/org/asamk/signal/commands/ReceiveCommand.java
Mark our own identity key as trusted initially
[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 } catch (UnsatisfiedLinkError e) {
58 System.err.println("Missing native library dependency for dbus service: " + e.getMessage());
59 return 1;
60 } catch (DBusException e) {
61 e.printStackTrace();
62 return 1;
63 }
64 while (true) {
65 try {
66 Thread.sleep(10000);
67 } catch (InterruptedException e) {
68 return 0;
69 }
70 }
71 }
72 return 0;
73 }
74
75 @Override
76 public int handleCommand(final Namespace ns, final Manager m) {
77 if (!m.isRegistered()) {
78 System.err.println("User is not registered.");
79 return 1;
80 }
81 double timeout = 5;
82 if (ns.getDouble("timeout") != null) {
83 timeout = ns.getDouble("timeout");
84 }
85 boolean returnOnTimeout = true;
86 if (timeout < 0) {
87 returnOnTimeout = false;
88 timeout = 3600;
89 }
90 boolean ignoreAttachments = ns.getBoolean("ignore_attachments");
91 try {
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);
94 return 0;
95 } catch (IOException e) {
96 System.err.println("Error while receiving messages: " + e.getMessage());
97 return 3;
98 } catch (AssertionError e) {
99 handleAssertionError(e);
100 return 1;
101 }
102 }
103 }