]> nmode's Git Repositories - signal-cli/blob - src/main/java/org/asamk/signal/commands/ReceiveCommand.java
Refactor sticker upload
[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.DBusSigHandler;
14 import org.freedesktop.dbus.exceptions.DBusException;
15 import org.whispersystems.util.Base64;
16
17 import java.io.IOException;
18 import java.util.concurrent.TimeUnit;
19
20 import static org.asamk.signal.util.ErrorUtils.handleAssertionError;
21
22 public class ReceiveCommand implements ExtendedDbusCommand, LocalCommand {
23
24 @Override
25 public void attachToSubparser(final Subparser subparser) {
26 subparser.addArgument("-t", "--timeout")
27 .type(double.class)
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());
35 }
36
37 public int handleCommand(final Namespace ns, final Signal signal, DBusConnection dbusconnection) {
38 if (dbusconnection != null) {
39 try {
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()));
46 }
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);
51 }
52 }
53 System.out.println();
54 });
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());
60 return 1;
61 } catch (DBusException e) {
62 e.printStackTrace();
63 return 1;
64 }
65 while (true) {
66 try {
67 Thread.sleep(10000);
68 } catch (InterruptedException e) {
69 return 0;
70 }
71 }
72 }
73 return 0;
74 }
75
76 @Override
77 public int handleCommand(final Namespace ns, final Manager m) {
78 if (!m.isRegistered()) {
79 System.err.println("User is not registered.");
80 return 1;
81 }
82 double timeout = 5;
83 if (ns.getDouble("timeout") != null) {
84 timeout = ns.getDouble("timeout");
85 }
86 boolean returnOnTimeout = true;
87 if (timeout < 0) {
88 returnOnTimeout = false;
89 timeout = 3600;
90 }
91 boolean ignoreAttachments = ns.getBoolean("ignore_attachments");
92 try {
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);
95 return 0;
96 } catch (IOException e) {
97 System.err.println("Error while receiving messages: " + e.getMessage());
98 return 3;
99 } catch (AssertionError e) {
100 handleAssertionError(e);
101 return 1;
102 }
103 }
104 }