2 * Copyright (C) 2015 AsamK
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package org
.asamk
.signal
;
19 import net
.sourceforge
.argparse4j
.ArgumentParsers
;
20 import net
.sourceforge
.argparse4j
.impl
.Arguments
;
21 import net
.sourceforge
.argparse4j
.inf
.*;
22 import org
.apache
.commons
.io
.IOUtils
;
23 import org
.apache
.http
.util
.TextUtils
;
24 import org
.asamk
.Signal
;
25 import org
.freedesktop
.dbus
.DBusConnection
;
26 import org
.freedesktop
.dbus
.DBusSigHandler
;
27 import org
.freedesktop
.dbus
.exceptions
.DBusException
;
28 import org
.freedesktop
.dbus
.exceptions
.DBusExecutionException
;
29 import org
.whispersystems
.libsignal
.InvalidKeyException
;
30 import org
.whispersystems
.signalservice
.api
.crypto
.UntrustedIdentityException
;
31 import org
.whispersystems
.signalservice
.api
.messages
.*;
32 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.DeviceInfo
;
33 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.ReadMessage
;
34 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.SentTranscriptMessage
;
35 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.SignalServiceSyncMessage
;
36 import org
.whispersystems
.signalservice
.api
.push
.SignalServiceAddress
;
37 import org
.whispersystems
.signalservice
.api
.push
.exceptions
.EncapsulatedExceptions
;
38 import org
.whispersystems
.signalservice
.api
.push
.exceptions
.NetworkFailureException
;
39 import org
.whispersystems
.signalservice
.api
.push
.exceptions
.UnregisteredUserException
;
40 import org
.whispersystems
.signalservice
.api
.util
.PhoneNumberFormatter
;
43 import java
.io
.IOException
;
45 import java
.net
.URISyntaxException
;
46 import java
.security
.Security
;
47 import java
.util
.ArrayList
;
48 import java
.util
.List
;
49 import java
.util
.concurrent
.TimeoutException
;
53 public static final String SIGNAL_BUSNAME
= "org.asamk.Signal";
54 public static final String SIGNAL_OBJECTPATH
= "/org/asamk/Signal";
56 public static void main(String
[] args
) {
57 // Workaround for BKS truststore
58 Security
.insertProviderAt(new org
.bouncycastle
.jce
.provider
.BouncyCastleProvider(), 1);
60 Namespace ns
= parseArgs(args
);
65 final String username
= ns
.getString("username");
68 DBusConnection dBusConn
= null;
70 if (ns
.getBoolean("dbus") || ns
.getBoolean("dbus_system")) {
74 if (ns
.getBoolean("dbus_system")) {
75 busType
= DBusConnection
.SYSTEM
;
77 busType
= DBusConnection
.SESSION
;
79 dBusConn
= DBusConnection
.getConnection(busType
);
80 ts
= (Signal
) dBusConn
.getRemoteObject(
81 SIGNAL_BUSNAME
, SIGNAL_OBJECTPATH
,
83 } catch (DBusException e
) {
85 if (dBusConn
!= null) {
86 dBusConn
.disconnect();
92 String settingsPath
= ns
.getString("config");
93 if (TextUtils
.isEmpty(settingsPath
)) {
94 settingsPath
= System
.getProperty("user.home") + "/.config/signal";
95 if (!new File(settingsPath
).exists()) {
96 String legacySettingsPath
= System
.getProperty("user.home") + "/.config/textsecure";
97 if (new File(legacySettingsPath
).exists()) {
98 settingsPath
= legacySettingsPath
;
103 m
= new Manager(username
, settingsPath
);
105 if (m
.userExists()) {
108 } catch (Exception e
) {
109 System
.err
.println("Error loading state file \"" + m
.getFileName() + "\": " + e
.getMessage());
116 switch (ns
.getString("command")) {
118 if (dBusConn
!= null) {
119 System
.err
.println("register is not yet implemented via dbus");
122 if (!m
.userHasKeys()) {
123 m
.createNewIdentity();
126 m
.register(ns
.getBoolean("voice"));
127 } catch (IOException e
) {
128 System
.err
.println("Request verify error: " + e
.getMessage());
133 if (dBusConn
!= null) {
134 System
.err
.println("verify is not yet implemented via dbus");
137 if (!m
.userHasKeys()) {
138 System
.err
.println("User has no keys, first call register.");
141 if (m
.isRegistered()) {
142 System
.err
.println("User registration is already verified");
146 m
.verifyAccount(ns
.getString("verificationCode"));
147 } catch (IOException e
) {
148 System
.err
.println("Verify error: " + e
.getMessage());
153 if (dBusConn
!= null) {
154 System
.err
.println("link is not yet implemented via dbus");
158 // When linking, username is null and we always have to create keys
159 m
.createNewIdentity();
161 String deviceName
= ns
.getString("name");
162 if (deviceName
== null) {
166 System
.out
.println(m
.getDeviceLinkUri());
167 m
.finishDeviceLink(deviceName
);
168 System
.out
.println("Associated with: " + m
.getUsername());
169 } catch (TimeoutException e
) {
170 System
.err
.println("Link request timed out, please try again.");
172 } catch (IOException e
) {
173 System
.err
.println("Link request error: " + e
.getMessage());
175 } catch (InvalidKeyException e
) {
178 } catch (UserAlreadyExists e
) {
179 System
.err
.println("The user " + e
.getUsername() + " already exists\nDelete \"" + e
.getFileName() + "\" before trying again.");
184 if (dBusConn
!= null) {
185 System
.err
.println("link is not yet implemented via dbus");
188 if (!m
.isRegistered()) {
189 System
.err
.println("User is not registered.");
193 m
.addDeviceLink(new URI(ns
.getString("uri")));
194 } catch (IOException e
) {
197 } catch (InvalidKeyException e
) {
200 } catch (URISyntaxException e
) {
206 if (dBusConn
!= null) {
207 System
.err
.println("listDevices is not yet implemented via dbus");
210 if (!m
.isRegistered()) {
211 System
.err
.println("User is not registered.");
215 List
<DeviceInfo
> devices
= m
.getLinkedDevices();
216 for (DeviceInfo d
: devices
) {
217 System
.out
.println("Device " + d
.getId() + (d
.getId() == m
.getDeviceId() ?
" (this device)" : "") + ":");
218 System
.out
.println(" Name: " + d
.getName());
219 System
.out
.println(" Created: " + d
.getCreated());
220 System
.out
.println(" Last seen: " + d
.getLastSeen());
222 } catch (IOException e
) {
228 if (dBusConn
== null && !m
.isRegistered()) {
229 System
.err
.println("User is not registered.");
233 if (ns
.getBoolean("endsession")) {
234 if (ns
.getList("recipient") == null) {
235 System
.err
.println("No recipients given");
236 System
.err
.println("Aborting sending.");
240 ts
.sendEndSessionMessage(ns
.<String
>getList("recipient"));
241 } catch (IOException e
) {
242 handleIOException(e
);
243 } catch (EncapsulatedExceptions e
) {
244 handleEncapsulatedExceptions(e
);
245 } catch (AssertionError e
) {
246 handleAssertionError(e
);
247 } catch (DBusExecutionException e
) {
248 handleDBusExecutionException(e
);
249 } catch (UntrustedIdentityException e
) {
253 String messageText
= ns
.getString("message");
254 if (messageText
== null) {
256 messageText
= IOUtils
.toString(System
.in);
257 } catch (IOException e
) {
258 System
.err
.println("Failed to read message from stdin: " + e
.getMessage());
259 System
.err
.println("Aborting sending.");
265 List
<String
> attachments
= ns
.getList("attachment");
266 if (attachments
== null) {
267 attachments
= new ArrayList
<>();
269 if (ns
.getString("group") != null) {
270 byte[] groupId
= decodeGroupId(ns
.getString("group"));
271 ts
.sendGroupMessage(messageText
, attachments
, groupId
);
273 ts
.sendMessage(messageText
, attachments
, ns
.<String
>getList("recipient"));
275 } catch (IOException e
) {
276 handleIOException(e
);
277 } catch (EncapsulatedExceptions e
) {
278 handleEncapsulatedExceptions(e
);
279 } catch (AssertionError e
) {
280 handleAssertionError(e
);
281 } catch (GroupNotFoundException e
) {
282 handleGroupNotFoundException(e
);
283 } catch (AttachmentInvalidException e
) {
284 System
.err
.println("Failed to add attachment: " + e
.getMessage());
285 System
.err
.println("Aborting sending.");
287 } catch (DBusExecutionException e
) {
288 handleDBusExecutionException(e
);
289 } catch (UntrustedIdentityException e
) {
296 if (dBusConn
!= null) {
298 dBusConn
.addSigHandler(Signal
.MessageReceived
.class, new DBusSigHandler
<Signal
.MessageReceived
>() {
300 public void handle(Signal
.MessageReceived s
) {
301 System
.out
.print(String
.format("Envelope from: %s\nTimestamp: %d\nBody: %s\n",
302 s
.getSender(), s
.getTimestamp(), s
.getMessage()));
303 if (s
.getGroupId().length
> 0) {
304 System
.out
.println("Group info:");
305 System
.out
.println(" Id: " + Base64
.encodeBytes(s
.getGroupId()));
307 if (s
.getAttachments().size() > 0) {
308 System
.out
.println("Attachments: ");
309 for (String attachment
: s
.getAttachments()) {
310 System
.out
.println("- Stored plaintext in: " + attachment
);
313 System
.out
.println();
316 } catch (DBusException e
) {
322 } catch (InterruptedException e
) {
327 if (!m
.isRegistered()) {
328 System
.err
.println("User is not registered.");
332 if (ns
.getInt("timeout") != null) {
333 timeout
= ns
.getInt("timeout");
335 boolean returnOnTimeout
= true;
337 returnOnTimeout
= false;
341 m
.receiveMessages(timeout
, returnOnTimeout
, new ReceiveMessageHandler(m
));
342 } catch (IOException e
) {
343 System
.err
.println("Error while receiving messages: " + e
.getMessage());
345 } catch (AssertionError e
) {
346 handleAssertionError(e
);
350 if (dBusConn
!= null) {
351 System
.err
.println("quitGroup is not yet implemented via dbus");
354 if (!m
.isRegistered()) {
355 System
.err
.println("User is not registered.");
360 m
.sendQuitGroupMessage(decodeGroupId(ns
.getString("group")));
361 } catch (IOException e
) {
362 handleIOException(e
);
363 } catch (EncapsulatedExceptions e
) {
364 handleEncapsulatedExceptions(e
);
365 } catch (AssertionError e
) {
366 handleAssertionError(e
);
367 } catch (GroupNotFoundException e
) {
368 handleGroupNotFoundException(e
);
369 } catch (UntrustedIdentityException e
) {
375 if (dBusConn
!= null) {
376 System
.err
.println("updateGroup is not yet implemented via dbus");
379 if (!m
.isRegistered()) {
380 System
.err
.println("User is not registered.");
385 byte[] groupId
= null;
386 if (ns
.getString("group") != null) {
387 groupId
= decodeGroupId(ns
.getString("group"));
389 byte[] newGroupId
= m
.sendUpdateGroupMessage(groupId
, ns
.getString("name"), ns
.<String
>getList("member"), ns
.getString("avatar"));
390 if (groupId
== null) {
391 System
.out
.println("Creating new group \"" + Base64
.encodeBytes(newGroupId
) + "\" …");
393 } catch (IOException e
) {
394 handleIOException(e
);
395 } catch (AttachmentInvalidException e
) {
396 System
.err
.println("Failed to add avatar attachment for group\": " + e
.getMessage());
397 System
.err
.println("Aborting sending.");
399 } catch (GroupNotFoundException e
) {
400 handleGroupNotFoundException(e
);
401 } catch (EncapsulatedExceptions e
) {
402 handleEncapsulatedExceptions(e
);
403 } catch (UntrustedIdentityException e
) {
409 if (dBusConn
!= null) {
410 System
.err
.println("Stop it.");
413 if (!m
.isRegistered()) {
414 System
.err
.println("User is not registered.");
417 DBusConnection conn
= null;
421 if (ns
.getBoolean("system")) {
422 busType
= DBusConnection
.SYSTEM
;
424 busType
= DBusConnection
.SESSION
;
426 conn
= DBusConnection
.getConnection(busType
);
427 conn
.exportObject(SIGNAL_OBJECTPATH
, m
);
428 conn
.requestBusName(SIGNAL_BUSNAME
);
429 } catch (DBusException e
) {
434 m
.receiveMessages(3600, false, new DbusReceiveMessageHandler(m
, conn
));
435 } catch (IOException e
) {
436 System
.err
.println("Error while receiving messages: " + e
.getMessage());
438 } catch (AssertionError e
) {
439 handleAssertionError(e
);
451 if (dBusConn
!= null) {
452 dBusConn
.disconnect();
457 private static void handleGroupNotFoundException(GroupNotFoundException e
) {
458 System
.err
.println("Failed to send to group: " + e
.getMessage());
459 System
.err
.println("Aborting sending.");
463 private static void handleDBusExecutionException(DBusExecutionException e
) {
464 System
.err
.println("Cannot connect to dbus: " + e
.getMessage());
465 System
.err
.println("Aborting.");
469 private static byte[] decodeGroupId(String groupId
) {
471 return Base64
.decode(groupId
);
472 } catch (IOException e
) {
473 System
.err
.println("Failed to decode groupId (must be base64) \"" + groupId
+ "\": " + e
.getMessage());
474 System
.err
.println("Aborting sending.");
480 private static Namespace
parseArgs(String
[] args
) {
481 ArgumentParser parser
= ArgumentParsers
.newArgumentParser("signal-cli")
483 .description("Commandline interface for Signal.")
484 .version(Manager
.PROJECT_NAME
+ " " + Manager
.PROJECT_VERSION
);
486 parser
.addArgument("-v", "--version")
487 .help("Show package version.")
488 .action(Arguments
.version());
489 parser
.addArgument("--config")
490 .help("Set the path, where to store the config (Default: $HOME/.config/signal-cli).");
492 MutuallyExclusiveGroup mut
= parser
.addMutuallyExclusiveGroup();
493 mut
.addArgument("-u", "--username")
494 .help("Specify your phone number, that will be used for verification.");
495 mut
.addArgument("--dbus")
496 .help("Make request via user dbus.")
497 .action(Arguments
.storeTrue());
498 mut
.addArgument("--dbus-system")
499 .help("Make request via system dbus.")
500 .action(Arguments
.storeTrue());
502 Subparsers subparsers
= parser
.addSubparsers()
503 .title("subcommands")
505 .description("valid subcommands")
506 .help("additional help");
508 Subparser parserLink
= subparsers
.addParser("link");
509 parserLink
.addArgument("-n", "--name")
510 .help("Specify a name to describe this new device.");
512 Subparser parserAddDevice
= subparsers
.addParser("addDevice");
513 parserAddDevice
.addArgument("--uri")
515 .help("Specify the uri contained in the QR code shown by the new device.");
517 Subparser parserDevices
= subparsers
.addParser("listDevices");
519 Subparser parserRegister
= subparsers
.addParser("register");
520 parserRegister
.addArgument("-v", "--voice")
521 .help("The verification should be done over voice, not sms.")
522 .action(Arguments
.storeTrue());
524 Subparser parserVerify
= subparsers
.addParser("verify");
525 parserVerify
.addArgument("verificationCode")
526 .help("The verification code you received via sms or voice call.");
528 Subparser parserSend
= subparsers
.addParser("send");
529 parserSend
.addArgument("-g", "--group")
530 .help("Specify the recipient group ID.");
531 parserSend
.addArgument("recipient")
532 .help("Specify the recipients' phone number.")
534 parserSend
.addArgument("-m", "--message")
535 .help("Specify the message, if missing standard input is used.");
536 parserSend
.addArgument("-a", "--attachment")
538 .help("Add file as attachment");
539 parserSend
.addArgument("-e", "--endsession")
540 .help("Clear session state and send end session message.")
541 .action(Arguments
.storeTrue());
543 Subparser parserLeaveGroup
= subparsers
.addParser("quitGroup");
544 parserLeaveGroup
.addArgument("-g", "--group")
546 .help("Specify the recipient group ID.");
548 Subparser parserUpdateGroup
= subparsers
.addParser("updateGroup");
549 parserUpdateGroup
.addArgument("-g", "--group")
550 .help("Specify the recipient group ID.");
551 parserUpdateGroup
.addArgument("-n", "--name")
552 .help("Specify the new group name.");
553 parserUpdateGroup
.addArgument("-a", "--avatar")
554 .help("Specify a new group avatar image file");
555 parserUpdateGroup
.addArgument("-m", "--member")
557 .help("Specify one or more members to add to the group");
559 Subparser parserReceive
= subparsers
.addParser("receive");
560 parserReceive
.addArgument("-t", "--timeout")
562 .help("Number of seconds to wait for new messages (negative values disable timeout)");
564 Subparser parserDaemon
= subparsers
.addParser("daemon");
565 parserDaemon
.addArgument("--system")
566 .action(Arguments
.storeTrue())
567 .help("Use DBus system bus instead of user bus.");
570 Namespace ns
= parser
.parseArgs(args
);
571 if ("link".equals(ns
.getString("command"))) {
572 if (ns
.getString("username") != null) {
574 System
.err
.println("You cannot specify a username (phone number) when linking");
577 } else if (!ns
.getBoolean("dbus") && !ns
.getBoolean("dbus_system")) {
578 if (ns
.getString("username") == null) {
580 System
.err
.println("You need to specify a username (phone number)");
583 if (!PhoneNumberFormatter
.isValidNumber(ns
.getString("username"))) {
584 System
.err
.println("Invalid username (phone number), make sure you include the country code.");
588 if (ns
.getList("recipient") != null && !ns
.getList("recipient").isEmpty() && ns
.getString("group") != null) {
589 System
.err
.println("You cannot specify recipients by phone number and groups a the same time");
593 } catch (ArgumentParserException e
) {
594 parser
.handleError(e
);
599 private static void handleAssertionError(AssertionError e
) {
600 System
.err
.println("Failed to send/receive message (Assertion): " + e
.getMessage());
602 System
.err
.println("If you use an Oracle JRE please check if you have unlimited strength crypto enabled, see README");
606 private static void handleEncapsulatedExceptions(EncapsulatedExceptions e
) {
607 System
.err
.println("Failed to send (some) messages:");
608 for (NetworkFailureException n
: e
.getNetworkExceptions()) {
609 System
.err
.println("Network failure for \"" + n
.getE164number() + "\": " + n
.getMessage());
611 for (UnregisteredUserException n
: e
.getUnregisteredUserExceptions()) {
612 System
.err
.println("Unregistered user \"" + n
.getE164Number() + "\": " + n
.getMessage());
614 for (UntrustedIdentityException n
: e
.getUntrustedIdentityExceptions()) {
615 System
.err
.println("Untrusted Identity for \"" + n
.getE164Number() + "\": " + n
.getMessage());
619 private static void handleIOException(IOException e
) {
620 System
.err
.println("Failed to send message: " + e
.getMessage());
623 private static class ReceiveMessageHandler
implements Manager
.ReceiveMessageHandler
{
626 public ReceiveMessageHandler(Manager m
) {
631 public void handleMessage(SignalServiceEnvelope envelope
, SignalServiceContent content
, GroupInfo group
) {
632 SignalServiceAddress source
= envelope
.getSourceAddress();
633 System
.out
.println(String
.format("Envelope from: %s (device: %d)", source
.getNumber(), envelope
.getSourceDevice()));
634 if (source
.getRelay().isPresent()) {
635 System
.out
.println("Relayed by: " + source
.getRelay().get());
637 System
.out
.println("Timestamp: " + envelope
.getTimestamp());
639 if (envelope
.isReceipt()) {
640 System
.out
.println("Got receipt.");
641 } else if (envelope
.isSignalMessage() | envelope
.isPreKeySignalMessage()) {
642 if (content
== null) {
643 System
.out
.println("Failed to decrypt message.");
645 if (content
.getDataMessage().isPresent()) {
646 SignalServiceDataMessage message
= content
.getDataMessage().get();
647 handleSignalServiceDataMessage(message
, group
);
649 if (content
.getSyncMessage().isPresent()) {
650 System
.out
.println("Received a sync message");
651 SignalServiceSyncMessage syncMessage
= content
.getSyncMessage().get();
653 if (syncMessage
.getContacts().isPresent()) {
654 System
.out
.println("Received sync contacts");
655 printAttachment(syncMessage
.getContacts().get());
657 if (syncMessage
.getGroups().isPresent()) {
658 System
.out
.println("Received sync groups");
659 printAttachment(syncMessage
.getGroups().get());
661 if (syncMessage
.getRead().isPresent()) {
662 System
.out
.println("Received sync read messages list");
663 for (ReadMessage rm
: syncMessage
.getRead().get()) {
664 System
.out
.println("From: " + rm
.getSender() + " Message timestamp: " + rm
.getTimestamp());
667 if (syncMessage
.getRequest().isPresent()) {
668 System
.out
.println("Received sync request");
669 if (syncMessage
.getRequest().get().isContactsRequest()) {
670 System
.out
.println(" - contacts request");
672 if (syncMessage
.getRequest().get().isGroupsRequest()) {
673 System
.out
.println(" - groups request");
676 if (syncMessage
.getSent().isPresent()) {
677 System
.out
.println("Received sync sent message");
678 final SentTranscriptMessage sentTranscriptMessage
= syncMessage
.getSent().get();
679 System
.out
.println("To: " + (sentTranscriptMessage
.getDestination().isPresent() ? sentTranscriptMessage
.getDestination().get() : "Unknown") + " , Message timestamp: " + sentTranscriptMessage
.getTimestamp());
680 SignalServiceDataMessage message
= sentTranscriptMessage
.getMessage();
681 handleSignalServiceDataMessage(message
, null);
686 System
.out
.println("Unknown message received.");
688 System
.out
.println();
691 // TODO remove group parameter
692 private void handleSignalServiceDataMessage(SignalServiceDataMessage message
, GroupInfo group
) {
693 System
.out
.println("Message timestamp: " + message
.getTimestamp());
695 if (message
.getBody().isPresent()) {
696 System
.out
.println("Body: " + message
.getBody().get());
698 if (message
.getGroupInfo().isPresent()) {
699 SignalServiceGroup groupInfo
= message
.getGroupInfo().get();
700 System
.out
.println("Group info:");
701 System
.out
.println(" Id: " + Base64
.encodeBytes(groupInfo
.getGroupId()));
702 if (groupInfo
.getName().isPresent()) {
703 System
.out
.println(" Name: " + groupInfo
.getName().get());
704 } else if (group
!= null) {
705 System
.out
.println(" Name: " + group
.name
);
707 System
.out
.println(" Name: <Unknown group>");
709 System
.out
.println(" Type: " + groupInfo
.getType());
710 if (groupInfo
.getMembers().isPresent()) {
711 for (String member
: groupInfo
.getMembers().get()) {
712 System
.out
.println(" Member: " + member
);
715 if (groupInfo
.getAvatar().isPresent()) {
716 System
.out
.println(" Avatar:");
717 printAttachment(groupInfo
.getAvatar().get());
720 if (message
.isEndSession()) {
721 System
.out
.println("Is end session");
724 if (message
.getAttachments().isPresent()) {
725 System
.out
.println("Attachments: ");
726 for (SignalServiceAttachment attachment
: message
.getAttachments().get()) {
727 printAttachment(attachment
);
732 private void printAttachment(SignalServiceAttachment attachment
) {
733 System
.out
.println("- " + attachment
.getContentType() + " (" + (attachment
.isPointer() ?
"Pointer" : "") + (attachment
.isStream() ?
"Stream" : "") + ")");
734 if (attachment
.isPointer()) {
735 final SignalServiceAttachmentPointer pointer
= attachment
.asPointer();
736 System
.out
.println(" Id: " + pointer
.getId() + " Key length: " + pointer
.getKey().length
+ (pointer
.getRelay().isPresent() ?
" Relay: " + pointer
.getRelay().get() : ""));
737 System
.out
.println(" Size: " + (pointer
.getSize().isPresent() ? pointer
.getSize().get() + " bytes" : "<unavailable>") + (pointer
.getPreview().isPresent() ?
" (Preview is available: " + pointer
.getPreview().get().length
+ " bytes)" : ""));
738 File file
= m
.getAttachmentFile(pointer
.getId());
740 System
.out
.println(" Stored plaintext in: " + file
);
746 private static class DbusReceiveMessageHandler
extends ReceiveMessageHandler
{
747 final DBusConnection conn
;
749 public DbusReceiveMessageHandler(Manager m
, DBusConnection conn
) {
755 public void handleMessage(SignalServiceEnvelope envelope
, SignalServiceContent content
, GroupInfo group
) {
756 super.handleMessage(envelope
, content
, group
);
758 if (!envelope
.isReceipt() && content
!= null && content
.getDataMessage().isPresent()) {
759 SignalServiceDataMessage message
= content
.getDataMessage().get();
761 if (!message
.isEndSession() &&
762 !(message
.getGroupInfo().isPresent() &&
763 message
.getGroupInfo().get().getType() != SignalServiceGroup
.Type
.DELIVER
)) {
764 List
<String
> attachments
= new ArrayList
<>();
765 if (message
.getAttachments().isPresent()) {
766 for (SignalServiceAttachment attachment
: message
.getAttachments().get()) {
767 if (attachment
.isPointer()) {
768 attachments
.add(m
.getAttachmentFile(attachment
.asPointer().getId()).getAbsolutePath());
774 conn
.sendSignal(new Signal
.MessageReceived(
776 message
.getTimestamp(),
777 envelope
.getSource(),
778 message
.getGroupInfo().isPresent() ? message
.getGroupInfo().get().getGroupId() : new byte[0],
779 message
.getBody().isPresent() ? message
.getBody().get() : "",
781 } catch (DBusException e
) {
788 private void printAttachment(SignalServiceAttachment attachment
) {
789 System
.out
.println("- " + attachment
.getContentType() + " (" + (attachment
.isPointer() ?
"Pointer" : "") + (attachment
.isStream() ?
"Stream" : "") + ")");
790 if (attachment
.isPointer()) {
791 final SignalServiceAttachmentPointer pointer
= attachment
.asPointer();
792 System
.out
.println(" Id: " + pointer
.getId() + " Key length: " + pointer
.getKey().length
+ (pointer
.getRelay().isPresent() ?
" Relay: " + pointer
.getRelay().get() : ""));
793 System
.out
.println(" Size: " + (pointer
.getSize().isPresent() ? pointer
.getSize().get() + " bytes" : "<unavailable>") + (pointer
.getPreview().isPresent() ?
" (Preview is available: " + pointer
.getPreview().get().length
+ " bytes)" : ""));
794 File file
= m
.getAttachmentFile(pointer
.getId());
796 System
.out
.println(" Stored plaintext in: " + file
);