]> nmode's Git Repositories - signal-cli/blob - src/main/java/org/asamk/signal/commands/ReceiveCommand.java
f7a491570722cf60dc63553f22e192533d409a1a
[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 // TODO delete later when "json" variable is removed
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 import java.io.IOException;
28 import java.util.concurrent.TimeUnit;
29
30 import static org.asamk.signal.util.ErrorUtils.handleAssertionError;
31
32 public class ReceiveCommand implements ExtendedDbusCommand, LocalCommand {
33
34 // TODO delete later when "json" variable is removed
35 final static Logger logger = LoggerFactory.getLogger(ReceiveCommand.class);
36
37 @Override
38 public void attachToSubparser(final Subparser subparser) {
39 subparser.addArgument("-t", "--timeout")
40 .type(double.class)
41 .help("Number of seconds to wait for new messages (negative values disable timeout)");
42 subparser.addArgument("--ignore-attachments")
43 .help("Don’t download attachments of received messages.")
44 .action(Arguments.storeTrue());
45 subparser.addArgument("--json")
46 .help("WARNING: This parameter is now deprecated! Please use the \"output\" option instead.\n\nOutput received messages in json format, one json object per line.")
47 .action(Arguments.storeTrue());
48 }
49
50 public int handleCommand(final Namespace ns, final Signal signal, DBusConnection dbusconnection) {
51 final ObjectMapper jsonProcessor;
52
53 boolean inJson = ns.getString("output").equals("json") || ns.getBoolean("json");
54
55 // TODO delete later when "json" variable is removed
56 if (ns.getBoolean("json")) {
57 logger.warn("\"--json\" option has been deprecated, please use \"output\" instead.");
58 }
59
60 if (inJson) {
61 jsonProcessor = new ObjectMapper();
62 jsonProcessor.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
63 jsonProcessor.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
64 } else {
65 jsonProcessor = null;
66 }
67 try {
68 dbusconnection.addSigHandler(Signal.MessageReceived.class, messageReceived -> {
69 if (jsonProcessor != null) {
70 JsonMessageEnvelope envelope = new JsonMessageEnvelope(messageReceived);
71 ObjectNode result = jsonProcessor.createObjectNode();
72 result.putPOJO("envelope", envelope);
73 try {
74 jsonProcessor.writeValue(System.out, result);
75 System.out.println();
76 } catch (IOException e) {
77 e.printStackTrace();
78 }
79 } else {
80 System.out.print(String.format("Envelope from: %s\nTimestamp: %s\nBody: %s\n",
81 messageReceived.getSender(),
82 DateUtils.formatTimestamp(messageReceived.getTimestamp()),
83 messageReceived.getMessage()));
84 if (messageReceived.getGroupId().length > 0) {
85 System.out.println("Group info:");
86 System.out.println(" Id: " + Base64.encodeBytes(messageReceived.getGroupId()));
87 }
88 if (messageReceived.getAttachments().size() > 0) {
89 System.out.println("Attachments: ");
90 for (String attachment : messageReceived.getAttachments()) {
91 System.out.println("- Stored plaintext in: " + attachment);
92 }
93 }
94 System.out.println();
95 }
96 });
97
98 dbusconnection.addSigHandler(Signal.ReceiptReceived.class, receiptReceived -> {
99 if (jsonProcessor != null) {
100 JsonMessageEnvelope envelope = new JsonMessageEnvelope(receiptReceived);
101 ObjectNode result = jsonProcessor.createObjectNode();
102 result.putPOJO("envelope", envelope);
103 try {
104 jsonProcessor.writeValue(System.out, result);
105 System.out.println();
106 } catch (IOException e) {
107 e.printStackTrace();
108 }
109 } else {
110 System.out.print(String.format("Receipt from: %s\nTimestamp: %s\n",
111 receiptReceived.getSender(),
112 DateUtils.formatTimestamp(receiptReceived.getTimestamp())));
113 }
114 });
115
116 dbusconnection.addSigHandler(Signal.SyncMessageReceived.class, syncReceived -> {
117 if (jsonProcessor != null) {
118 JsonMessageEnvelope envelope = new JsonMessageEnvelope(syncReceived);
119 ObjectNode result = jsonProcessor.createObjectNode();
120 result.putPOJO("envelope", envelope);
121 try {
122 jsonProcessor.writeValue(System.out, result);
123 System.out.println();
124 } catch (IOException e) {
125 e.printStackTrace();
126 }
127 } else {
128 System.out.print(String.format("Sync Envelope from: %s to: %s\nTimestamp: %s\nBody: %s\n",
129 syncReceived.getSource(),
130 syncReceived.getDestination(),
131 DateUtils.formatTimestamp(syncReceived.getTimestamp()),
132 syncReceived.getMessage()));
133 if (syncReceived.getGroupId().length > 0) {
134 System.out.println("Group info:");
135 System.out.println(" Id: " + Base64.encodeBytes(syncReceived.getGroupId()));
136 }
137 if (syncReceived.getAttachments().size() > 0) {
138 System.out.println("Attachments: ");
139 for (String attachment : syncReceived.getAttachments()) {
140 System.out.println("- Stored plaintext in: " + attachment);
141 }
142 }
143 System.out.println();
144 }
145 });
146 } catch (UnsatisfiedLinkError e) {
147 System.err.println("Missing native library dependency for dbus service: " + e.getMessage());
148 return 1;
149 } catch (DBusException e) {
150 e.printStackTrace();
151 return 1;
152 }
153 while (true) {
154 try {
155 Thread.sleep(10000);
156 } catch (InterruptedException e) {
157 return 0;
158 }
159 }
160 }
161
162 @Override
163 public int handleCommand(final Namespace ns, final Manager m) {
164 boolean inJson = ns.getString("output").equals("json") || ns.getBoolean("json");
165
166 // TODO delete later when "json" variable is removed
167 if (ns.getBoolean("json")) {
168 logger.warn("\"--json\" option has been deprecated, please use \"output\" instead.");
169 }
170
171 double timeout = 5;
172 if (ns.getDouble("timeout") != null) {
173 timeout = ns.getDouble("timeout");
174 }
175 boolean returnOnTimeout = true;
176 if (timeout < 0) {
177 returnOnTimeout = false;
178 timeout = 3600;
179 }
180 boolean ignoreAttachments = ns.getBoolean("ignore_attachments");
181 try {
182 final Manager.ReceiveMessageHandler handler = inJson
183 ? new JsonReceiveMessageHandler(m)
184 : new ReceiveMessageHandler(m);
185 m.receiveMessages((long) (timeout * 1000),
186 TimeUnit.MILLISECONDS,
187 returnOnTimeout,
188 ignoreAttachments,
189 handler);
190 return 0;
191 } catch (IOException e) {
192 System.err.println("Error while receiving messages: " + e.getMessage());
193 return 3;
194 } catch (AssertionError e) {
195 handleAssertionError(e);
196 return 1;
197 }
198 }
199 }