]> nmode's Git Repositories - signal-cli/blob - src/main/java/org/asamk/signal/commands/ReceiveCommand.java
bc68565ad09e2e84ab74fe972065d65da852c18e
[signal-cli] / src / main / java / org / asamk / signal / commands / ReceiveCommand.java
1 package org.asamk.signal.commands;
2
3 import com.fasterxml.jackson.annotation.JsonAutoDetect;
4 import com.fasterxml.jackson.annotation.PropertyAccessor;
5 import com.fasterxml.jackson.core.JsonGenerator;
6 import com.fasterxml.jackson.databind.ObjectMapper;
7 import com.fasterxml.jackson.databind.node.ObjectNode;
8
9 import net.sourceforge.argparse4j.impl.Arguments;
10 import net.sourceforge.argparse4j.inf.Namespace;
11 import net.sourceforge.argparse4j.inf.Subparser;
12
13 import org.asamk.Signal;
14 import org.asamk.signal.JsonReceiveMessageHandler;
15 import org.asamk.signal.ReceiveMessageHandler;
16 import org.asamk.signal.json.JsonMessageEnvelope;
17 import org.asamk.signal.manager.Manager;
18 import org.asamk.signal.util.DateUtils;
19 import org.freedesktop.dbus.connections.impl.DBusConnection;
20 import org.freedesktop.dbus.exceptions.DBusException;
21 import org.whispersystems.util.Base64;
22
23 import java.io.IOException;
24 import java.util.concurrent.TimeUnit;
25
26 import static org.asamk.signal.util.ErrorUtils.handleAssertionError;
27
28 public class ReceiveCommand implements ExtendedDbusCommand, LocalCommand {
29
30 @Override
31 public void attachToSubparser(final Subparser subparser) {
32 subparser.addArgument("-t", "--timeout")
33 .type(double.class)
34 .help("Number of seconds to wait for new messages (negative values disable timeout)");
35 subparser.addArgument("--ignore-attachments")
36 .help("Don’t download attachments of received messages.")
37 .action(Arguments.storeTrue());
38 subparser.addArgument("--json")
39 .help("Output received messages in json format, one json object per line.")
40 .action(Arguments.storeTrue());
41 }
42
43 public int handleCommand(final Namespace ns, final Signal signal, DBusConnection dbusconnection) {
44 final ObjectMapper jsonProcessor;
45 if (ns.getBoolean("json")) {
46 jsonProcessor = new ObjectMapper();
47 jsonProcessor.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); // disable autodetect
48 jsonProcessor.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
49 } else {
50 jsonProcessor = null;
51 }
52 try {
53 dbusconnection.addSigHandler(Signal.MessageReceived.class, messageReceived -> {
54 if (jsonProcessor != null) {
55 JsonMessageEnvelope envelope = new JsonMessageEnvelope(messageReceived);
56 ObjectNode result = jsonProcessor.createObjectNode();
57 result.putPOJO("envelope", envelope);
58 try {
59 jsonProcessor.writeValue(System.out, result);
60 System.out.println();
61 } catch (IOException e) {
62 e.printStackTrace();
63 }
64 } else {
65 System.out.print(String.format("Envelope from: %s\nTimestamp: %s\nBody: %s\n",
66 messageReceived.getSender(),
67 DateUtils.formatTimestamp(messageReceived.getTimestamp()),
68 messageReceived.getMessage()));
69 if (messageReceived.getGroupId().length > 0) {
70 System.out.println("Group info:");
71 System.out.println(" Id: " + Base64.encodeBytes(messageReceived.getGroupId()));
72 }
73 if (messageReceived.getAttachments().size() > 0) {
74 System.out.println("Attachments: ");
75 for (String attachment : messageReceived.getAttachments()) {
76 System.out.println("- Stored plaintext in: " + attachment);
77 }
78 }
79 System.out.println();
80 }
81 });
82
83 dbusconnection.addSigHandler(Signal.ReceiptReceived.class, receiptReceived -> {
84 if (jsonProcessor != null) {
85 JsonMessageEnvelope envelope = new JsonMessageEnvelope(receiptReceived);
86 ObjectNode result = jsonProcessor.createObjectNode();
87 result.putPOJO("envelope", envelope);
88 try {
89 jsonProcessor.writeValue(System.out, result);
90 System.out.println();
91 } catch (IOException e) {
92 e.printStackTrace();
93 }
94 } else {
95 System.out.print(String.format("Receipt from: %s\nTimestamp: %s\n",
96 receiptReceived.getSender(),
97 DateUtils.formatTimestamp(receiptReceived.getTimestamp())));
98 }
99 });
100
101 dbusconnection.addSigHandler(Signal.SyncMessageReceived.class, syncReceived -> {
102 if (jsonProcessor != null) {
103 JsonMessageEnvelope envelope = new JsonMessageEnvelope(syncReceived);
104 ObjectNode result = jsonProcessor.createObjectNode();
105 result.putPOJO("envelope", envelope);
106 try {
107 jsonProcessor.writeValue(System.out, result);
108 System.out.println();
109 } catch (IOException e) {
110 e.printStackTrace();
111 }
112 } else {
113 System.out.print(String.format("Sync Envelope from: %s to: %s\nTimestamp: %s\nBody: %s\n",
114 syncReceived.getSource(),
115 syncReceived.getDestination(),
116 DateUtils.formatTimestamp(syncReceived.getTimestamp()),
117 syncReceived.getMessage()));
118 if (syncReceived.getGroupId().length > 0) {
119 System.out.println("Group info:");
120 System.out.println(" Id: " + Base64.encodeBytes(syncReceived.getGroupId()));
121 }
122 if (syncReceived.getAttachments().size() > 0) {
123 System.out.println("Attachments: ");
124 for (String attachment : syncReceived.getAttachments()) {
125 System.out.println("- Stored plaintext in: " + attachment);
126 }
127 }
128 System.out.println();
129 }
130 });
131 } catch (UnsatisfiedLinkError e) {
132 System.err.println("Missing native library dependency for dbus service: " + e.getMessage());
133 return 1;
134 } catch (DBusException e) {
135 e.printStackTrace();
136 return 1;
137 }
138 while (true) {
139 try {
140 Thread.sleep(10000);
141 } catch (InterruptedException e) {
142 return 0;
143 }
144 }
145 }
146
147 @Override
148 public int handleCommand(final Namespace ns, final Manager m) {
149 if (!m.isRegistered()) {
150 System.err.println("User is not registered.");
151 return 1;
152 }
153 double timeout = 5;
154 if (ns.getDouble("timeout") != null) {
155 timeout = ns.getDouble("timeout");
156 }
157 boolean returnOnTimeout = true;
158 if (timeout < 0) {
159 returnOnTimeout = false;
160 timeout = 3600;
161 }
162 boolean ignoreAttachments = ns.getBoolean("ignore_attachments");
163 try {
164 final Manager.ReceiveMessageHandler handler = ns.getBoolean("json")
165 ? new JsonReceiveMessageHandler(m)
166 : new ReceiveMessageHandler(m);
167 m.receiveMessages((long) (timeout * 1000),
168 TimeUnit.MILLISECONDS,
169 returnOnTimeout,
170 ignoreAttachments,
171 handler);
172 return 0;
173 } catch (IOException e) {
174 System.err.println("Error while receiving messages: " + e.getMessage());
175 return 3;
176 } catch (AssertionError e) {
177 handleAssertionError(e);
178 return 1;
179 }
180 }
181 }