]> nmode's Git Repositories - signal-cli/blob - src/main/java/org/asamk/signal/commands/ReceiveCommand.java
Switch to hypfvieh dbus-java
[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(), DateUtils.formatTimestamp(messageReceived.getTimestamp()), messageReceived.getMessage()));
67 if (messageReceived.getGroupId().length > 0) {
68 System.out.println("Group info:");
69 System.out.println(" Id: " + Base64.encodeBytes(messageReceived.getGroupId()));
70 }
71 if (messageReceived.getAttachments().size() > 0) {
72 System.out.println("Attachments: ");
73 for (String attachment : messageReceived.getAttachments()) {
74 System.out.println("- Stored plaintext in: " + attachment);
75 }
76 }
77 System.out.println();
78 }
79 });
80
81 dbusconnection.addSigHandler(Signal.ReceiptReceived.class,
82 receiptReceived -> {
83 if (jsonProcessor != null) {
84 JsonMessageEnvelope envelope = new JsonMessageEnvelope(receiptReceived);
85 ObjectNode result = jsonProcessor.createObjectNode();
86 result.putPOJO("envelope", envelope);
87 try {
88 jsonProcessor.writeValue(System.out, result);
89 System.out.println();
90 } catch (IOException e) {
91 e.printStackTrace();
92 }
93 } else {
94 System.out.print(String.format("Receipt from: %s\nTimestamp: %s\n",
95 receiptReceived.getSender(), DateUtils.formatTimestamp(receiptReceived.getTimestamp())));
96 }
97 });
98
99 dbusconnection.addSigHandler(Signal.SyncMessageReceived.class, syncReceived -> {
100 if (jsonProcessor != null) {
101 JsonMessageEnvelope envelope = new JsonMessageEnvelope(syncReceived);
102 ObjectNode result = jsonProcessor.createObjectNode();
103 result.putPOJO("envelope", envelope);
104 try {
105 jsonProcessor.writeValue(System.out, result);
106 System.out.println();
107 } catch (IOException e) {
108 e.printStackTrace();
109 }
110 } else {
111 System.out.print(String.format("Sync Envelope from: %s to: %s\nTimestamp: %s\nBody: %s\n",
112 syncReceived.getSource(), syncReceived.getDestination(), DateUtils.formatTimestamp(syncReceived.getTimestamp()), syncReceived.getMessage()));
113 if (syncReceived.getGroupId().length > 0) {
114 System.out.println("Group info:");
115 System.out.println(" Id: " + Base64.encodeBytes(syncReceived.getGroupId()));
116 }
117 if (syncReceived.getAttachments().size() > 0) {
118 System.out.println("Attachments: ");
119 for (String attachment : syncReceived.getAttachments()) {
120 System.out.println("- Stored plaintext in: " + attachment);
121 }
122 }
123 System.out.println();
124 }
125 });
126 } catch (UnsatisfiedLinkError e) {
127 System.err.println("Missing native library dependency for dbus service: " + e.getMessage());
128 return 1;
129 } catch (DBusException e) {
130 e.printStackTrace();
131 return 1;
132 }
133 while (true) {
134 try {
135 Thread.sleep(10000);
136 } catch (InterruptedException e) {
137 return 0;
138 }
139 }
140 }
141
142 @Override
143 public int handleCommand(final Namespace ns, final Manager m) {
144 if (!m.isRegistered()) {
145 System.err.println("User is not registered.");
146 return 1;
147 }
148 double timeout = 5;
149 if (ns.getDouble("timeout") != null) {
150 timeout = ns.getDouble("timeout");
151 }
152 boolean returnOnTimeout = true;
153 if (timeout < 0) {
154 returnOnTimeout = false;
155 timeout = 3600;
156 }
157 boolean ignoreAttachments = ns.getBoolean("ignore_attachments");
158 try {
159 final Manager.ReceiveMessageHandler handler = ns.getBoolean("json") ? new JsonReceiveMessageHandler(m) : new ReceiveMessageHandler(m);
160 m.receiveMessages((long) (timeout * 1000), TimeUnit.MILLISECONDS, returnOnTimeout, ignoreAttachments, handler);
161 return 0;
162 } catch (IOException e) {
163 System.err.println("Error while receiving messages: " + e.getMessage());
164 return 3;
165 } catch (AssertionError e) {
166 handleAssertionError(e);
167 return 1;
168 }
169 }
170 }