]> nmode's Git Repositories - signal-cli/blob - src/main/java/org/asamk/signal/commands/ReceiveCommand.java
Reduce usage of deprecated resolved methods
[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.JsonWriter;
10 import org.asamk.signal.OutputType;
11 import org.asamk.signal.PlainTextWriterImpl;
12 import org.asamk.signal.ReceiveMessageHandler;
13 import org.asamk.signal.commands.exceptions.CommandException;
14 import org.asamk.signal.commands.exceptions.IOErrorException;
15 import org.asamk.signal.commands.exceptions.UnexpectedErrorException;
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.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 import java.io.IOException;
25 import java.util.Base64;
26 import java.util.Map;
27 import java.util.Set;
28 import java.util.concurrent.TimeUnit;
29
30 public class ReceiveCommand implements ExtendedDbusCommand, LocalCommand {
31
32 private final static Logger logger = LoggerFactory.getLogger(ReceiveCommand.class);
33
34 @Override
35 public void attachToSubparser(final Subparser subparser) {
36 subparser.addArgument("-t", "--timeout")
37 .type(double.class)
38 .help("Number of seconds to wait for new messages (negative values disable timeout)");
39 subparser.addArgument("--ignore-attachments")
40 .help("Don’t download attachments of received messages.")
41 .action(Arguments.storeTrue());
42 subparser.addArgument("--json")
43 .help("WARNING: This parameter is now deprecated! Please use the global \"--output=json\" option instead.\n\nOutput received messages in json format, one json object per line.")
44 .action(Arguments.storeTrue());
45 }
46
47 @Override
48 public Set<OutputType> getSupportedOutputTypes() {
49 return Set.of(OutputType.PLAIN_TEXT, OutputType.JSON);
50 }
51
52 public void handleCommand(
53 final Namespace ns, final Signal signal, DBusConnection dbusconnection
54 ) throws CommandException {
55 var inJson = ns.get("output") == OutputType.JSON || ns.getBoolean("json");
56
57 // TODO delete later when "json" variable is removed
58 if (ns.getBoolean("json")) {
59 logger.warn("\"--json\" option has been deprecated, please use the global \"--output=json\" instead.");
60 }
61
62 try {
63 if (inJson) {
64 final var jsonWriter = new JsonWriter(System.out);
65
66 dbusconnection.addSigHandler(Signal.MessageReceived.class, messageReceived -> {
67 var envelope = new JsonMessageEnvelope(messageReceived);
68 final var object = Map.of("envelope", envelope);
69 jsonWriter.write(object);
70 });
71
72 dbusconnection.addSigHandler(Signal.ReceiptReceived.class, receiptReceived -> {
73 var envelope = new JsonMessageEnvelope(receiptReceived);
74 final var object = Map.of("envelope", envelope);
75 jsonWriter.write(object);
76 });
77
78 dbusconnection.addSigHandler(Signal.SyncMessageReceived.class, syncReceived -> {
79 var envelope = new JsonMessageEnvelope(syncReceived);
80 final var object = Map.of("envelope", envelope);
81 jsonWriter.write(object);
82 });
83 } else {
84 final var writer = new PlainTextWriterImpl(System.out);
85
86 dbusconnection.addSigHandler(Signal.MessageReceived.class, messageReceived -> {
87 writer.println("Envelope from: {}", messageReceived.getSender());
88 writer.println("Timestamp: {}", DateUtils.formatTimestamp(messageReceived.getTimestamp()));
89 writer.println("Body: {}", messageReceived.getMessage());
90 if (messageReceived.getGroupId().length > 0) {
91 writer.println("Group info:");
92 writer.indentedWriter()
93 .println("Id: {}", Base64.getEncoder().encodeToString(messageReceived.getGroupId()));
94 }
95 if (messageReceived.getAttachments().size() > 0) {
96 writer.println("Attachments:");
97 for (var attachment : messageReceived.getAttachments()) {
98 writer.println("- Stored plaintext in: {}", attachment);
99 }
100 }
101 writer.println();
102 });
103
104 dbusconnection.addSigHandler(Signal.ReceiptReceived.class, receiptReceived -> {
105 writer.println("Receipt from: {}", receiptReceived.getSender());
106 writer.println("Timestamp: {}", DateUtils.formatTimestamp(receiptReceived.getTimestamp()));
107 });
108
109 dbusconnection.addSigHandler(Signal.SyncMessageReceived.class, syncReceived -> {
110 writer.println("Sync Envelope from: {} to: {}",
111 syncReceived.getSource(),
112 syncReceived.getDestination());
113 writer.println("Timestamp: {}", DateUtils.formatTimestamp(syncReceived.getTimestamp()));
114 writer.println("Body: {}", syncReceived.getMessage());
115 if (syncReceived.getGroupId().length > 0) {
116 writer.println("Group info:");
117 writer.indentedWriter()
118 .println("Id: {}", Base64.getEncoder().encodeToString(syncReceived.getGroupId()));
119 }
120 if (syncReceived.getAttachments().size() > 0) {
121 writer.println("Attachments:");
122 for (var attachment : syncReceived.getAttachments()) {
123 writer.println("- Stored plaintext in: {}", attachment);
124 }
125 }
126 writer.println();
127 });
128 }
129 } catch (DBusException e) {
130 logger.error("Dbus client failed", e);
131 throw new UnexpectedErrorException("Dbus client failed");
132 }
133 while (true) {
134 try {
135 Thread.sleep(10000);
136 } catch (InterruptedException ignored) {
137 return;
138 }
139 }
140 }
141
142 @Override
143 public void handleCommand(final Namespace ns, final Manager m) throws CommandException {
144 var inJson = ns.get("output") == OutputType.JSON || ns.getBoolean("json");
145
146 // TODO delete later when "json" variable is removed
147 if (ns.getBoolean("json")) {
148 logger.warn("\"--json\" option has been deprecated, please use the global \"--output=json\" instead.");
149 }
150
151 double timeout = 5;
152 if (ns.getDouble("timeout") != null) {
153 timeout = ns.getDouble("timeout");
154 }
155 var returnOnTimeout = true;
156 if (timeout < 0) {
157 returnOnTimeout = false;
158 timeout = 3600;
159 }
160 boolean ignoreAttachments = ns.getBoolean("ignore_attachments");
161 try {
162 final var handler = inJson ? new JsonReceiveMessageHandler(m) : new ReceiveMessageHandler(m);
163 m.receiveMessages((long) (timeout * 1000),
164 TimeUnit.MILLISECONDS,
165 returnOnTimeout,
166 ignoreAttachments,
167 handler);
168 } catch (IOException e) {
169 throw new IOErrorException("Error while receiving messages: " + e.getMessage());
170 }
171 }
172 }