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) {
229 System
.err
.println("removeDevice is not yet implemented via dbus");
232 if (!m
.isRegistered()) {
233 System
.err
.println("User is not registered.");
237 int deviceId
= ns
.getInt("deviceId");
238 m
.removeLinkedDevices(deviceId
);
239 } catch (IOException e
) {
245 if (dBusConn
== null && !m
.isRegistered()) {
246 System
.err
.println("User is not registered.");
250 if (ns
.getBoolean("endsession")) {
251 if (ns
.getList("recipient") == null) {
252 System
.err
.println("No recipients given");
253 System
.err
.println("Aborting sending.");
257 ts
.sendEndSessionMessage(ns
.<String
>getList("recipient"));
258 } catch (IOException e
) {
259 handleIOException(e
);
260 } catch (EncapsulatedExceptions e
) {
261 handleEncapsulatedExceptions(e
);
262 } catch (AssertionError e
) {
263 handleAssertionError(e
);
264 } catch (DBusExecutionException e
) {
265 handleDBusExecutionException(e
);
266 } catch (UntrustedIdentityException e
) {
270 String messageText
= ns
.getString("message");
271 if (messageText
== null) {
273 messageText
= IOUtils
.toString(System
.in);
274 } catch (IOException e
) {
275 System
.err
.println("Failed to read message from stdin: " + e
.getMessage());
276 System
.err
.println("Aborting sending.");
282 List
<String
> attachments
= ns
.getList("attachment");
283 if (attachments
== null) {
284 attachments
= new ArrayList
<>();
286 if (ns
.getString("group") != null) {
287 byte[] groupId
= decodeGroupId(ns
.getString("group"));
288 ts
.sendGroupMessage(messageText
, attachments
, groupId
);
290 ts
.sendMessage(messageText
, attachments
, ns
.<String
>getList("recipient"));
292 } catch (IOException e
) {
293 handleIOException(e
);
294 } catch (EncapsulatedExceptions e
) {
295 handleEncapsulatedExceptions(e
);
296 } catch (AssertionError e
) {
297 handleAssertionError(e
);
298 } catch (GroupNotFoundException e
) {
299 handleGroupNotFoundException(e
);
300 } catch (AttachmentInvalidException e
) {
301 System
.err
.println("Failed to add attachment: " + e
.getMessage());
302 System
.err
.println("Aborting sending.");
304 } catch (DBusExecutionException e
) {
305 handleDBusExecutionException(e
);
306 } catch (UntrustedIdentityException e
) {
313 if (dBusConn
!= null) {
315 dBusConn
.addSigHandler(Signal
.MessageReceived
.class, new DBusSigHandler
<Signal
.MessageReceived
>() {
317 public void handle(Signal
.MessageReceived s
) {
318 System
.out
.print(String
.format("Envelope from: %s\nTimestamp: %d\nBody: %s\n",
319 s
.getSender(), s
.getTimestamp(), s
.getMessage()));
320 if (s
.getGroupId().length
> 0) {
321 System
.out
.println("Group info:");
322 System
.out
.println(" Id: " + Base64
.encodeBytes(s
.getGroupId()));
324 if (s
.getAttachments().size() > 0) {
325 System
.out
.println("Attachments: ");
326 for (String attachment
: s
.getAttachments()) {
327 System
.out
.println("- Stored plaintext in: " + attachment
);
330 System
.out
.println();
333 } catch (DBusException e
) {
339 } catch (InterruptedException e
) {
344 if (!m
.isRegistered()) {
345 System
.err
.println("User is not registered.");
349 if (ns
.getInt("timeout") != null) {
350 timeout
= ns
.getInt("timeout");
352 boolean returnOnTimeout
= true;
354 returnOnTimeout
= false;
358 m
.receiveMessages(timeout
, returnOnTimeout
, new ReceiveMessageHandler(m
));
359 } catch (IOException e
) {
360 System
.err
.println("Error while receiving messages: " + e
.getMessage());
362 } catch (AssertionError e
) {
363 handleAssertionError(e
);
367 if (dBusConn
!= null) {
368 System
.err
.println("quitGroup is not yet implemented via dbus");
371 if (!m
.isRegistered()) {
372 System
.err
.println("User is not registered.");
377 m
.sendQuitGroupMessage(decodeGroupId(ns
.getString("group")));
378 } catch (IOException e
) {
379 handleIOException(e
);
380 } catch (EncapsulatedExceptions e
) {
381 handleEncapsulatedExceptions(e
);
382 } catch (AssertionError e
) {
383 handleAssertionError(e
);
384 } catch (GroupNotFoundException e
) {
385 handleGroupNotFoundException(e
);
386 } catch (UntrustedIdentityException e
) {
392 if (dBusConn
!= null) {
393 System
.err
.println("updateGroup is not yet implemented via dbus");
396 if (!m
.isRegistered()) {
397 System
.err
.println("User is not registered.");
402 byte[] groupId
= null;
403 if (ns
.getString("group") != null) {
404 groupId
= decodeGroupId(ns
.getString("group"));
406 byte[] newGroupId
= m
.sendUpdateGroupMessage(groupId
, ns
.getString("name"), ns
.<String
>getList("member"), ns
.getString("avatar"));
407 if (groupId
== null) {
408 System
.out
.println("Creating new group \"" + Base64
.encodeBytes(newGroupId
) + "\" …");
410 } catch (IOException e
) {
411 handleIOException(e
);
412 } catch (AttachmentInvalidException e
) {
413 System
.err
.println("Failed to add avatar attachment for group\": " + e
.getMessage());
414 System
.err
.println("Aborting sending.");
416 } catch (GroupNotFoundException e
) {
417 handleGroupNotFoundException(e
);
418 } catch (EncapsulatedExceptions e
) {
419 handleEncapsulatedExceptions(e
);
420 } catch (UntrustedIdentityException e
) {
426 if (dBusConn
!= null) {
427 System
.err
.println("Stop it.");
430 if (!m
.isRegistered()) {
431 System
.err
.println("User is not registered.");
434 DBusConnection conn
= null;
438 if (ns
.getBoolean("system")) {
439 busType
= DBusConnection
.SYSTEM
;
441 busType
= DBusConnection
.SESSION
;
443 conn
= DBusConnection
.getConnection(busType
);
444 conn
.exportObject(SIGNAL_OBJECTPATH
, m
);
445 conn
.requestBusName(SIGNAL_BUSNAME
);
446 } catch (DBusException e
) {
451 m
.receiveMessages(3600, false, new DbusReceiveMessageHandler(m
, conn
));
452 } catch (IOException e
) {
453 System
.err
.println("Error while receiving messages: " + e
.getMessage());
455 } catch (AssertionError e
) {
456 handleAssertionError(e
);
468 if (dBusConn
!= null) {
469 dBusConn
.disconnect();
474 private static void handleGroupNotFoundException(GroupNotFoundException e
) {
475 System
.err
.println("Failed to send to group: " + e
.getMessage());
476 System
.err
.println("Aborting sending.");
480 private static void handleDBusExecutionException(DBusExecutionException e
) {
481 System
.err
.println("Cannot connect to dbus: " + e
.getMessage());
482 System
.err
.println("Aborting.");
486 private static byte[] decodeGroupId(String groupId
) {
488 return Base64
.decode(groupId
);
489 } catch (IOException e
) {
490 System
.err
.println("Failed to decode groupId (must be base64) \"" + groupId
+ "\": " + e
.getMessage());
491 System
.err
.println("Aborting sending.");
497 private static Namespace
parseArgs(String
[] args
) {
498 ArgumentParser parser
= ArgumentParsers
.newArgumentParser("signal-cli")
500 .description("Commandline interface for Signal.")
501 .version(Manager
.PROJECT_NAME
+ " " + Manager
.PROJECT_VERSION
);
503 parser
.addArgument("-v", "--version")
504 .help("Show package version.")
505 .action(Arguments
.version());
506 parser
.addArgument("--config")
507 .help("Set the path, where to store the config (Default: $HOME/.config/signal-cli).");
509 MutuallyExclusiveGroup mut
= parser
.addMutuallyExclusiveGroup();
510 mut
.addArgument("-u", "--username")
511 .help("Specify your phone number, that will be used for verification.");
512 mut
.addArgument("--dbus")
513 .help("Make request via user dbus.")
514 .action(Arguments
.storeTrue());
515 mut
.addArgument("--dbus-system")
516 .help("Make request via system dbus.")
517 .action(Arguments
.storeTrue());
519 Subparsers subparsers
= parser
.addSubparsers()
520 .title("subcommands")
522 .description("valid subcommands")
523 .help("additional help");
525 Subparser parserLink
= subparsers
.addParser("link");
526 parserLink
.addArgument("-n", "--name")
527 .help("Specify a name to describe this new device.");
529 Subparser parserAddDevice
= subparsers
.addParser("addDevice");
530 parserAddDevice
.addArgument("--uri")
532 .help("Specify the uri contained in the QR code shown by the new device.");
534 Subparser parserDevices
= subparsers
.addParser("listDevices");
536 Subparser parserRemoveDevice
= subparsers
.addParser("removeDevice");
537 parserRemoveDevice
.addArgument("-d", "--deviceId")
540 .help("Specify the device you want to remove. Use listDevices to see the deviceIds.");
542 Subparser parserRegister
= subparsers
.addParser("register");
543 parserRegister
.addArgument("-v", "--voice")
544 .help("The verification should be done over voice, not sms.")
545 .action(Arguments
.storeTrue());
547 Subparser parserVerify
= subparsers
.addParser("verify");
548 parserVerify
.addArgument("verificationCode")
549 .help("The verification code you received via sms or voice call.");
551 Subparser parserSend
= subparsers
.addParser("send");
552 parserSend
.addArgument("-g", "--group")
553 .help("Specify the recipient group ID.");
554 parserSend
.addArgument("recipient")
555 .help("Specify the recipients' phone number.")
557 parserSend
.addArgument("-m", "--message")
558 .help("Specify the message, if missing standard input is used.");
559 parserSend
.addArgument("-a", "--attachment")
561 .help("Add file as attachment");
562 parserSend
.addArgument("-e", "--endsession")
563 .help("Clear session state and send end session message.")
564 .action(Arguments
.storeTrue());
566 Subparser parserLeaveGroup
= subparsers
.addParser("quitGroup");
567 parserLeaveGroup
.addArgument("-g", "--group")
569 .help("Specify the recipient group ID.");
571 Subparser parserUpdateGroup
= subparsers
.addParser("updateGroup");
572 parserUpdateGroup
.addArgument("-g", "--group")
573 .help("Specify the recipient group ID.");
574 parserUpdateGroup
.addArgument("-n", "--name")
575 .help("Specify the new group name.");
576 parserUpdateGroup
.addArgument("-a", "--avatar")
577 .help("Specify a new group avatar image file");
578 parserUpdateGroup
.addArgument("-m", "--member")
580 .help("Specify one or more members to add to the group");
582 Subparser parserReceive
= subparsers
.addParser("receive");
583 parserReceive
.addArgument("-t", "--timeout")
585 .help("Number of seconds to wait for new messages (negative values disable timeout)");
587 Subparser parserDaemon
= subparsers
.addParser("daemon");
588 parserDaemon
.addArgument("--system")
589 .action(Arguments
.storeTrue())
590 .help("Use DBus system bus instead of user bus.");
593 Namespace ns
= parser
.parseArgs(args
);
594 if ("link".equals(ns
.getString("command"))) {
595 if (ns
.getString("username") != null) {
597 System
.err
.println("You cannot specify a username (phone number) when linking");
600 } else if (!ns
.getBoolean("dbus") && !ns
.getBoolean("dbus_system")) {
601 if (ns
.getString("username") == null) {
603 System
.err
.println("You need to specify a username (phone number)");
606 if (!PhoneNumberFormatter
.isValidNumber(ns
.getString("username"))) {
607 System
.err
.println("Invalid username (phone number), make sure you include the country code.");
611 if (ns
.getList("recipient") != null && !ns
.getList("recipient").isEmpty() && ns
.getString("group") != null) {
612 System
.err
.println("You cannot specify recipients by phone number and groups a the same time");
616 } catch (ArgumentParserException e
) {
617 parser
.handleError(e
);
622 private static void handleAssertionError(AssertionError e
) {
623 System
.err
.println("Failed to send/receive message (Assertion): " + e
.getMessage());
625 System
.err
.println("If you use an Oracle JRE please check if you have unlimited strength crypto enabled, see README");
629 private static void handleEncapsulatedExceptions(EncapsulatedExceptions e
) {
630 System
.err
.println("Failed to send (some) messages:");
631 for (NetworkFailureException n
: e
.getNetworkExceptions()) {
632 System
.err
.println("Network failure for \"" + n
.getE164number() + "\": " + n
.getMessage());
634 for (UnregisteredUserException n
: e
.getUnregisteredUserExceptions()) {
635 System
.err
.println("Unregistered user \"" + n
.getE164Number() + "\": " + n
.getMessage());
637 for (UntrustedIdentityException n
: e
.getUntrustedIdentityExceptions()) {
638 System
.err
.println("Untrusted Identity for \"" + n
.getE164Number() + "\": " + n
.getMessage());
642 private static void handleIOException(IOException e
) {
643 System
.err
.println("Failed to send message: " + e
.getMessage());
646 private static class ReceiveMessageHandler
implements Manager
.ReceiveMessageHandler
{
649 public ReceiveMessageHandler(Manager m
) {
654 public void handleMessage(SignalServiceEnvelope envelope
, SignalServiceContent content
, GroupInfo group
) {
655 SignalServiceAddress source
= envelope
.getSourceAddress();
656 System
.out
.println(String
.format("Envelope from: %s (device: %d)", source
.getNumber(), envelope
.getSourceDevice()));
657 if (source
.getRelay().isPresent()) {
658 System
.out
.println("Relayed by: " + source
.getRelay().get());
660 System
.out
.println("Timestamp: " + envelope
.getTimestamp());
662 if (envelope
.isReceipt()) {
663 System
.out
.println("Got receipt.");
664 } else if (envelope
.isSignalMessage() | envelope
.isPreKeySignalMessage()) {
665 if (content
== null) {
666 System
.out
.println("Failed to decrypt message.");
668 if (content
.getDataMessage().isPresent()) {
669 SignalServiceDataMessage message
= content
.getDataMessage().get();
670 handleSignalServiceDataMessage(message
, group
);
672 if (content
.getSyncMessage().isPresent()) {
673 System
.out
.println("Received a sync message");
674 SignalServiceSyncMessage syncMessage
= content
.getSyncMessage().get();
676 if (syncMessage
.getContacts().isPresent()) {
677 System
.out
.println("Received sync contacts");
678 printAttachment(syncMessage
.getContacts().get());
680 if (syncMessage
.getGroups().isPresent()) {
681 System
.out
.println("Received sync groups");
682 printAttachment(syncMessage
.getGroups().get());
684 if (syncMessage
.getRead().isPresent()) {
685 System
.out
.println("Received sync read messages list");
686 for (ReadMessage rm
: syncMessage
.getRead().get()) {
687 System
.out
.println("From: " + rm
.getSender() + " Message timestamp: " + rm
.getTimestamp());
690 if (syncMessage
.getRequest().isPresent()) {
691 System
.out
.println("Received sync request");
692 if (syncMessage
.getRequest().get().isContactsRequest()) {
693 System
.out
.println(" - contacts request");
695 if (syncMessage
.getRequest().get().isGroupsRequest()) {
696 System
.out
.println(" - groups request");
699 if (syncMessage
.getSent().isPresent()) {
700 System
.out
.println("Received sync sent message");
701 final SentTranscriptMessage sentTranscriptMessage
= syncMessage
.getSent().get();
702 System
.out
.println("To: " + (sentTranscriptMessage
.getDestination().isPresent() ? sentTranscriptMessage
.getDestination().get() : "Unknown") + " , Message timestamp: " + sentTranscriptMessage
.getTimestamp());
703 SignalServiceDataMessage message
= sentTranscriptMessage
.getMessage();
704 handleSignalServiceDataMessage(message
, null);
709 System
.out
.println("Unknown message received.");
711 System
.out
.println();
714 // TODO remove group parameter
715 private void handleSignalServiceDataMessage(SignalServiceDataMessage message
, GroupInfo group
) {
716 System
.out
.println("Message timestamp: " + message
.getTimestamp());
718 if (message
.getBody().isPresent()) {
719 System
.out
.println("Body: " + message
.getBody().get());
721 if (message
.getGroupInfo().isPresent()) {
722 SignalServiceGroup groupInfo
= message
.getGroupInfo().get();
723 System
.out
.println("Group info:");
724 System
.out
.println(" Id: " + Base64
.encodeBytes(groupInfo
.getGroupId()));
725 if (groupInfo
.getName().isPresent()) {
726 System
.out
.println(" Name: " + groupInfo
.getName().get());
727 } else if (group
!= null) {
728 System
.out
.println(" Name: " + group
.name
);
730 System
.out
.println(" Name: <Unknown group>");
732 System
.out
.println(" Type: " + groupInfo
.getType());
733 if (groupInfo
.getMembers().isPresent()) {
734 for (String member
: groupInfo
.getMembers().get()) {
735 System
.out
.println(" Member: " + member
);
738 if (groupInfo
.getAvatar().isPresent()) {
739 System
.out
.println(" Avatar:");
740 printAttachment(groupInfo
.getAvatar().get());
743 if (message
.isEndSession()) {
744 System
.out
.println("Is end session");
747 if (message
.getAttachments().isPresent()) {
748 System
.out
.println("Attachments: ");
749 for (SignalServiceAttachment attachment
: message
.getAttachments().get()) {
750 printAttachment(attachment
);
755 private void printAttachment(SignalServiceAttachment attachment
) {
756 System
.out
.println("- " + attachment
.getContentType() + " (" + (attachment
.isPointer() ?
"Pointer" : "") + (attachment
.isStream() ?
"Stream" : "") + ")");
757 if (attachment
.isPointer()) {
758 final SignalServiceAttachmentPointer pointer
= attachment
.asPointer();
759 System
.out
.println(" Id: " + pointer
.getId() + " Key length: " + pointer
.getKey().length
+ (pointer
.getRelay().isPresent() ?
" Relay: " + pointer
.getRelay().get() : ""));
760 System
.out
.println(" Size: " + (pointer
.getSize().isPresent() ? pointer
.getSize().get() + " bytes" : "<unavailable>") + (pointer
.getPreview().isPresent() ?
" (Preview is available: " + pointer
.getPreview().get().length
+ " bytes)" : ""));
761 File file
= m
.getAttachmentFile(pointer
.getId());
763 System
.out
.println(" Stored plaintext in: " + file
);
769 private static class DbusReceiveMessageHandler
extends ReceiveMessageHandler
{
770 final DBusConnection conn
;
772 public DbusReceiveMessageHandler(Manager m
, DBusConnection conn
) {
778 public void handleMessage(SignalServiceEnvelope envelope
, SignalServiceContent content
, GroupInfo group
) {
779 super.handleMessage(envelope
, content
, group
);
781 if (!envelope
.isReceipt() && content
!= null && content
.getDataMessage().isPresent()) {
782 SignalServiceDataMessage message
= content
.getDataMessage().get();
784 if (!message
.isEndSession() &&
785 !(message
.getGroupInfo().isPresent() &&
786 message
.getGroupInfo().get().getType() != SignalServiceGroup
.Type
.DELIVER
)) {
787 List
<String
> attachments
= new ArrayList
<>();
788 if (message
.getAttachments().isPresent()) {
789 for (SignalServiceAttachment attachment
: message
.getAttachments().get()) {
790 if (attachment
.isPointer()) {
791 attachments
.add(m
.getAttachmentFile(attachment
.asPointer().getId()).getAbsolutePath());
797 conn
.sendSignal(new Signal
.MessageReceived(
799 message
.getTimestamp(),
800 envelope
.getSource(),
801 message
.getGroupInfo().isPresent() ? message
.getGroupInfo().get().getGroupId() : new byte[0],
802 message
.getBody().isPresent() ? message
.getBody().get() : "",
804 } catch (DBusException e
) {
811 private void printAttachment(SignalServiceAttachment attachment
) {
812 System
.out
.println("- " + attachment
.getContentType() + " (" + (attachment
.isPointer() ?
"Pointer" : "") + (attachment
.isStream() ?
"Stream" : "") + ")");
813 if (attachment
.isPointer()) {
814 final SignalServiceAttachmentPointer pointer
= attachment
.asPointer();
815 System
.out
.println(" Id: " + pointer
.getId() + " Key length: " + pointer
.getKey().length
+ (pointer
.getRelay().isPresent() ?
" Relay: " + pointer
.getRelay().get() : ""));
816 System
.out
.println(" Size: " + (pointer
.getSize().isPresent() ? pointer
.getSize().get() + " bytes" : "<unavailable>") + (pointer
.getPreview().isPresent() ?
" (Preview is available: " + pointer
.getPreview().get().length
+ " bytes)" : ""));
817 File file
= m
.getAttachmentFile(pointer
.getId());
819 System
.out
.println(" Stored plaintext in: " + file
);