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
.http
.util
.TextUtils
;
23 import org
.asamk
.Signal
;
24 import org
.freedesktop
.dbus
.DBusConnection
;
25 import org
.freedesktop
.dbus
.DBusSigHandler
;
26 import org
.freedesktop
.dbus
.exceptions
.DBusException
;
27 import org
.freedesktop
.dbus
.exceptions
.DBusExecutionException
;
28 import org
.whispersystems
.libsignal
.InvalidKeyException
;
29 import org
.whispersystems
.signalservice
.api
.crypto
.UntrustedIdentityException
;
30 import org
.whispersystems
.signalservice
.api
.messages
.*;
31 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.DeviceInfo
;
32 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.ReadMessage
;
33 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.SentTranscriptMessage
;
34 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.SignalServiceSyncMessage
;
35 import org
.whispersystems
.signalservice
.api
.push
.SignalServiceAddress
;
36 import org
.whispersystems
.signalservice
.api
.push
.exceptions
.EncapsulatedExceptions
;
37 import org
.whispersystems
.signalservice
.api
.push
.exceptions
.NetworkFailureException
;
38 import org
.whispersystems
.signalservice
.api
.push
.exceptions
.UnregisteredUserException
;
39 import org
.whispersystems
.signalservice
.api
.util
.PhoneNumberFormatter
;
42 import java
.io
.IOException
;
43 import java
.io
.InputStream
;
44 import java
.io
.StringWriter
;
46 import java
.net
.URISyntaxException
;
47 import java
.nio
.charset
.Charset
;
48 import java
.security
.Security
;
49 import java
.util
.ArrayList
;
50 import java
.util
.List
;
51 import java
.util
.concurrent
.TimeoutException
;
55 public static final String SIGNAL_BUSNAME
= "org.asamk.Signal";
56 public static final String SIGNAL_OBJECTPATH
= "/org/asamk/Signal";
58 public static void main(String
[] args
) {
59 // Workaround for BKS truststore
60 Security
.insertProviderAt(new org
.bouncycastle
.jce
.provider
.BouncyCastleProvider(), 1);
62 Namespace ns
= parseArgs(args
);
67 final String username
= ns
.getString("username");
70 DBusConnection dBusConn
= null;
72 if (ns
.getBoolean("dbus") || ns
.getBoolean("dbus_system")) {
76 if (ns
.getBoolean("dbus_system")) {
77 busType
= DBusConnection
.SYSTEM
;
79 busType
= DBusConnection
.SESSION
;
81 dBusConn
= DBusConnection
.getConnection(busType
);
82 ts
= (Signal
) dBusConn
.getRemoteObject(
83 SIGNAL_BUSNAME
, SIGNAL_OBJECTPATH
,
85 } catch (DBusException e
) {
87 if (dBusConn
!= null) {
88 dBusConn
.disconnect();
94 String settingsPath
= ns
.getString("config");
95 if (TextUtils
.isEmpty(settingsPath
)) {
96 settingsPath
= System
.getProperty("user.home") + "/.config/signal";
97 if (!new File(settingsPath
).exists()) {
98 String legacySettingsPath
= System
.getProperty("user.home") + "/.config/textsecure";
99 if (new File(legacySettingsPath
).exists()) {
100 settingsPath
= legacySettingsPath
;
105 m
= new Manager(username
, settingsPath
);
107 if (m
.userExists()) {
110 } catch (Exception e
) {
111 System
.err
.println("Error loading state file \"" + m
.getFileName() + "\": " + e
.getMessage());
118 switch (ns
.getString("command")) {
120 if (dBusConn
!= null) {
121 System
.err
.println("register is not yet implemented via dbus");
124 if (!m
.userHasKeys()) {
125 m
.createNewIdentity();
128 m
.register(ns
.getBoolean("voice"));
129 } catch (IOException e
) {
130 System
.err
.println("Request verify error: " + e
.getMessage());
135 if (dBusConn
!= null) {
136 System
.err
.println("verify is not yet implemented via dbus");
139 if (!m
.userHasKeys()) {
140 System
.err
.println("User has no keys, first call register.");
143 if (m
.isRegistered()) {
144 System
.err
.println("User registration is already verified");
148 m
.verifyAccount(ns
.getString("verificationCode"));
149 } catch (IOException e
) {
150 System
.err
.println("Verify error: " + e
.getMessage());
155 if (dBusConn
!= null) {
156 System
.err
.println("link is not yet implemented via dbus");
160 // When linking, username is null and we always have to create keys
161 m
.createNewIdentity();
163 String deviceName
= ns
.getString("name");
164 if (deviceName
== null) {
168 System
.out
.println(m
.getDeviceLinkUri());
169 m
.finishDeviceLink(deviceName
);
170 System
.out
.println("Associated with: " + m
.getUsername());
171 } catch (TimeoutException e
) {
172 System
.err
.println("Link request timed out, please try again.");
174 } catch (IOException e
) {
175 System
.err
.println("Link request error: " + e
.getMessage());
177 } catch (InvalidKeyException e
) {
180 } catch (UserAlreadyExists e
) {
181 System
.err
.println("The user " + e
.getUsername() + " already exists\nDelete \"" + e
.getFileName() + "\" before trying again.");
186 if (dBusConn
!= null) {
187 System
.err
.println("link is not yet implemented via dbus");
190 if (!m
.isRegistered()) {
191 System
.err
.println("User is not registered.");
195 m
.addDeviceLink(new URI(ns
.getString("uri")));
196 } catch (IOException e
) {
199 } catch (InvalidKeyException e
) {
202 } catch (URISyntaxException e
) {
208 if (dBusConn
!= null) {
209 System
.err
.println("listDevices is not yet implemented via dbus");
212 if (!m
.isRegistered()) {
213 System
.err
.println("User is not registered.");
217 List
<DeviceInfo
> devices
= m
.getLinkedDevices();
218 for (DeviceInfo d
: devices
) {
219 System
.out
.println("Device " + d
.getId() + (d
.getId() == m
.getDeviceId() ?
" (this device)" : "") + ":");
220 System
.out
.println(" Name: " + d
.getName());
221 System
.out
.println(" Created: " + d
.getCreated());
222 System
.out
.println(" Last seen: " + d
.getLastSeen());
224 } catch (IOException e
) {
230 if (dBusConn
!= null) {
231 System
.err
.println("removeDevice is not yet implemented via dbus");
234 if (!m
.isRegistered()) {
235 System
.err
.println("User is not registered.");
239 int deviceId
= ns
.getInt("deviceId");
240 m
.removeLinkedDevices(deviceId
);
241 } catch (IOException e
) {
247 if (dBusConn
== null && !m
.isRegistered()) {
248 System
.err
.println("User is not registered.");
252 if (ns
.getBoolean("endsession")) {
253 if (ns
.getList("recipient") == null) {
254 System
.err
.println("No recipients given");
255 System
.err
.println("Aborting sending.");
259 ts
.sendEndSessionMessage(ns
.<String
>getList("recipient"));
260 } catch (IOException e
) {
261 handleIOException(e
);
262 } catch (EncapsulatedExceptions e
) {
263 handleEncapsulatedExceptions(e
);
264 } catch (AssertionError e
) {
265 handleAssertionError(e
);
266 } catch (DBusExecutionException e
) {
267 handleDBusExecutionException(e
);
270 String messageText
= ns
.getString("message");
271 if (messageText
== null) {
273 messageText
= readAll(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
);
311 if (dBusConn
!= null) {
313 dBusConn
.addSigHandler(Signal
.MessageReceived
.class, new DBusSigHandler
<Signal
.MessageReceived
>() {
315 public void handle(Signal
.MessageReceived s
) {
316 System
.out
.print(String
.format("Envelope from: %s\nTimestamp: %d\nBody: %s\n",
317 s
.getSender(), s
.getTimestamp(), s
.getMessage()));
318 if (s
.getGroupId().length
> 0) {
319 System
.out
.println("Group info:");
320 System
.out
.println(" Id: " + Base64
.encodeBytes(s
.getGroupId()));
322 if (s
.getAttachments().size() > 0) {
323 System
.out
.println("Attachments: ");
324 for (String attachment
: s
.getAttachments()) {
325 System
.out
.println("- Stored plaintext in: " + attachment
);
328 System
.out
.println();
331 } catch (DBusException e
) {
337 } catch (InterruptedException e
) {
342 if (!m
.isRegistered()) {
343 System
.err
.println("User is not registered.");
347 if (ns
.getInt("timeout") != null) {
348 timeout
= ns
.getInt("timeout");
350 boolean returnOnTimeout
= true;
352 returnOnTimeout
= false;
356 m
.receiveMessages(timeout
, returnOnTimeout
, new ReceiveMessageHandler(m
));
357 } catch (IOException e
) {
358 System
.err
.println("Error while receiving messages: " + e
.getMessage());
360 } catch (AssertionError e
) {
361 handleAssertionError(e
);
365 if (dBusConn
!= null) {
366 System
.err
.println("quitGroup is not yet implemented via dbus");
369 if (!m
.isRegistered()) {
370 System
.err
.println("User is not registered.");
375 m
.sendQuitGroupMessage(decodeGroupId(ns
.getString("group")));
376 } catch (IOException e
) {
377 handleIOException(e
);
378 } catch (EncapsulatedExceptions e
) {
379 handleEncapsulatedExceptions(e
);
380 } catch (AssertionError e
) {
381 handleAssertionError(e
);
382 } catch (GroupNotFoundException e
) {
383 handleGroupNotFoundException(e
);
388 if (dBusConn
!= null) {
389 System
.err
.println("updateGroup is not yet implemented via dbus");
392 if (!m
.isRegistered()) {
393 System
.err
.println("User is not registered.");
398 byte[] groupId
= null;
399 if (ns
.getString("group") != null) {
400 groupId
= decodeGroupId(ns
.getString("group"));
402 byte[] newGroupId
= m
.sendUpdateGroupMessage(groupId
, ns
.getString("name"), ns
.<String
>getList("member"), ns
.getString("avatar"));
403 if (groupId
== null) {
404 System
.out
.println("Creating new group \"" + Base64
.encodeBytes(newGroupId
) + "\" …");
406 } catch (IOException e
) {
407 handleIOException(e
);
408 } catch (AttachmentInvalidException e
) {
409 System
.err
.println("Failed to add avatar attachment for group\": " + e
.getMessage());
410 System
.err
.println("Aborting sending.");
412 } catch (GroupNotFoundException e
) {
413 handleGroupNotFoundException(e
);
414 } catch (EncapsulatedExceptions e
) {
415 handleEncapsulatedExceptions(e
);
420 if (dBusConn
!= null) {
421 System
.err
.println("Stop it.");
424 if (!m
.isRegistered()) {
425 System
.err
.println("User is not registered.");
428 DBusConnection conn
= null;
432 if (ns
.getBoolean("system")) {
433 busType
= DBusConnection
.SYSTEM
;
435 busType
= DBusConnection
.SESSION
;
437 conn
= DBusConnection
.getConnection(busType
);
438 conn
.exportObject(SIGNAL_OBJECTPATH
, m
);
439 conn
.requestBusName(SIGNAL_BUSNAME
);
440 } catch (DBusException e
) {
445 m
.receiveMessages(3600, false, new DbusReceiveMessageHandler(m
, conn
));
446 } catch (IOException e
) {
447 System
.err
.println("Error while receiving messages: " + e
.getMessage());
449 } catch (AssertionError e
) {
450 handleAssertionError(e
);
462 if (dBusConn
!= null) {
463 dBusConn
.disconnect();
468 private static void handleGroupNotFoundException(GroupNotFoundException e
) {
469 System
.err
.println("Failed to send to group: " + e
.getMessage());
470 System
.err
.println("Aborting sending.");
474 private static void handleDBusExecutionException(DBusExecutionException e
) {
475 System
.err
.println("Cannot connect to dbus: " + e
.getMessage());
476 System
.err
.println("Aborting.");
480 private static byte[] decodeGroupId(String groupId
) {
482 return Base64
.decode(groupId
);
483 } catch (IOException e
) {
484 System
.err
.println("Failed to decode groupId (must be base64) \"" + groupId
+ "\": " + e
.getMessage());
485 System
.err
.println("Aborting sending.");
491 private static Namespace
parseArgs(String
[] args
) {
492 ArgumentParser parser
= ArgumentParsers
.newArgumentParser("signal-cli")
494 .description("Commandline interface for Signal.")
495 .version(Manager
.PROJECT_NAME
+ " " + Manager
.PROJECT_VERSION
);
497 parser
.addArgument("-v", "--version")
498 .help("Show package version.")
499 .action(Arguments
.version());
500 parser
.addArgument("--config")
501 .help("Set the path, where to store the config (Default: $HOME/.config/signal).");
503 MutuallyExclusiveGroup mut
= parser
.addMutuallyExclusiveGroup();
504 mut
.addArgument("-u", "--username")
505 .help("Specify your phone number, that will be used for verification.");
506 mut
.addArgument("--dbus")
507 .help("Make request via user dbus.")
508 .action(Arguments
.storeTrue());
509 mut
.addArgument("--dbus-system")
510 .help("Make request via system dbus.")
511 .action(Arguments
.storeTrue());
513 Subparsers subparsers
= parser
.addSubparsers()
514 .title("subcommands")
516 .description("valid subcommands")
517 .help("additional help");
519 Subparser parserLink
= subparsers
.addParser("link");
520 parserLink
.addArgument("-n", "--name")
521 .help("Specify a name to describe this new device.");
523 Subparser parserAddDevice
= subparsers
.addParser("addDevice");
524 parserAddDevice
.addArgument("--uri")
526 .help("Specify the uri contained in the QR code shown by the new device.");
528 Subparser parserDevices
= subparsers
.addParser("listDevices");
530 Subparser parserRemoveDevice
= subparsers
.addParser("removeDevice");
531 parserRemoveDevice
.addArgument("-d", "--deviceId")
534 .help("Specify the device you want to remove. Use listDevices to see the deviceIds.");
536 Subparser parserRegister
= subparsers
.addParser("register");
537 parserRegister
.addArgument("-v", "--voice")
538 .help("The verification should be done over voice, not sms.")
539 .action(Arguments
.storeTrue());
541 Subparser parserVerify
= subparsers
.addParser("verify");
542 parserVerify
.addArgument("verificationCode")
543 .help("The verification code you received via sms or voice call.");
545 Subparser parserSend
= subparsers
.addParser("send");
546 parserSend
.addArgument("-g", "--group")
547 .help("Specify the recipient group ID.");
548 parserSend
.addArgument("recipient")
549 .help("Specify the recipients' phone number.")
551 parserSend
.addArgument("-m", "--message")
552 .help("Specify the message, if missing standard input is used.");
553 parserSend
.addArgument("-a", "--attachment")
555 .help("Add file as attachment");
556 parserSend
.addArgument("-e", "--endsession")
557 .help("Clear session state and send end session message.")
558 .action(Arguments
.storeTrue());
560 Subparser parserLeaveGroup
= subparsers
.addParser("quitGroup");
561 parserLeaveGroup
.addArgument("-g", "--group")
563 .help("Specify the recipient group ID.");
565 Subparser parserUpdateGroup
= subparsers
.addParser("updateGroup");
566 parserUpdateGroup
.addArgument("-g", "--group")
567 .help("Specify the recipient group ID.");
568 parserUpdateGroup
.addArgument("-n", "--name")
569 .help("Specify the new group name.");
570 parserUpdateGroup
.addArgument("-a", "--avatar")
571 .help("Specify a new group avatar image file");
572 parserUpdateGroup
.addArgument("-m", "--member")
574 .help("Specify one or more members to add to the group");
576 Subparser parserReceive
= subparsers
.addParser("receive");
577 parserReceive
.addArgument("-t", "--timeout")
579 .help("Number of seconds to wait for new messages (negative values disable timeout)");
581 Subparser parserDaemon
= subparsers
.addParser("daemon");
582 parserDaemon
.addArgument("--system")
583 .action(Arguments
.storeTrue())
584 .help("Use DBus system bus instead of user bus.");
587 Namespace ns
= parser
.parseArgs(args
);
588 if ("link".equals(ns
.getString("command"))) {
589 if (ns
.getString("username") != null) {
591 System
.err
.println("You cannot specify a username (phone number) when linking");
594 } else if (!ns
.getBoolean("dbus") && !ns
.getBoolean("dbus_system")) {
595 if (ns
.getString("username") == null) {
597 System
.err
.println("You need to specify a username (phone number)");
600 if (!PhoneNumberFormatter
.isValidNumber(ns
.getString("username"))) {
601 System
.err
.println("Invalid username (phone number), make sure you include the country code.");
605 if (ns
.getList("recipient") != null && !ns
.getList("recipient").isEmpty() && ns
.getString("group") != null) {
606 System
.err
.println("You cannot specify recipients by phone number and groups a the same time");
610 } catch (ArgumentParserException e
) {
611 parser
.handleError(e
);
616 private static void handleAssertionError(AssertionError e
) {
617 System
.err
.println("Failed to send/receive message (Assertion): " + e
.getMessage());
619 System
.err
.println("If you use an Oracle JRE please check if you have unlimited strength crypto enabled, see README");
623 private static void handleEncapsulatedExceptions(EncapsulatedExceptions e
) {
624 System
.err
.println("Failed to send (some) messages:");
625 for (NetworkFailureException n
: e
.getNetworkExceptions()) {
626 System
.err
.println("Network failure for \"" + n
.getE164number() + "\": " + n
.getMessage());
628 for (UnregisteredUserException n
: e
.getUnregisteredUserExceptions()) {
629 System
.err
.println("Unregistered user \"" + n
.getE164Number() + "\": " + n
.getMessage());
631 for (UntrustedIdentityException n
: e
.getUntrustedIdentityExceptions()) {
632 System
.err
.println("Untrusted Identity for \"" + n
.getE164Number() + "\": " + n
.getMessage());
636 private static void handleIOException(IOException e
) {
637 System
.err
.println("Failed to send message: " + e
.getMessage());
640 private static String
readAll(InputStream
in) throws IOException
{
641 StringWriter output
= new StringWriter();
642 byte[] buffer
= new byte[4096];
645 while (-1 != (n
= System
.in.read(buffer
))) {
646 output
.write(new String(buffer
, 0, n
, Charset
.defaultCharset()));
649 return output
.toString();
652 private static class ReceiveMessageHandler
implements Manager
.ReceiveMessageHandler
{
655 public ReceiveMessageHandler(Manager m
) {
660 public void handleMessage(SignalServiceEnvelope envelope
, SignalServiceContent content
) {
661 SignalServiceAddress source
= envelope
.getSourceAddress();
662 ContactInfo sourceContact
= m
.getContact(source
.getNumber());
663 System
.out
.println(String
.format("Envelope from: %s (device: %d)", (sourceContact
== null ?
"" : "“" + sourceContact
.name
+ "” ") + source
.getNumber(), envelope
.getSourceDevice()));
664 if (source
.getRelay().isPresent()) {
665 System
.out
.println("Relayed by: " + source
.getRelay().get());
667 System
.out
.println("Timestamp: " + envelope
.getTimestamp());
669 if (envelope
.isReceipt()) {
670 System
.out
.println("Got receipt.");
671 } else if (envelope
.isSignalMessage() | envelope
.isPreKeySignalMessage()) {
672 if (content
== null) {
673 System
.out
.println("Failed to decrypt message.");
675 if (content
.getDataMessage().isPresent()) {
676 SignalServiceDataMessage message
= content
.getDataMessage().get();
677 handleSignalServiceDataMessage(message
);
679 if (content
.getSyncMessage().isPresent()) {
680 System
.out
.println("Received a sync message");
681 SignalServiceSyncMessage syncMessage
= content
.getSyncMessage().get();
683 if (syncMessage
.getContacts().isPresent()) {
684 System
.out
.println("Received sync contacts");
685 printAttachment(syncMessage
.getContacts().get());
687 if (syncMessage
.getGroups().isPresent()) {
688 System
.out
.println("Received sync groups");
689 printAttachment(syncMessage
.getGroups().get());
691 if (syncMessage
.getRead().isPresent()) {
692 System
.out
.println("Received sync read messages list");
693 for (ReadMessage rm
: syncMessage
.getRead().get()) {
694 ContactInfo fromContact
= m
.getContact(rm
.getSender());
695 System
.out
.println("From: " + (fromContact
== null ?
"" : "“" + fromContact
.name
+ "” ") + rm
.getSender() + " Message timestamp: " + rm
.getTimestamp());
698 if (syncMessage
.getRequest().isPresent()) {
699 System
.out
.println("Received sync request");
700 if (syncMessage
.getRequest().get().isContactsRequest()) {
701 System
.out
.println(" - contacts request");
703 if (syncMessage
.getRequest().get().isGroupsRequest()) {
704 System
.out
.println(" - groups request");
707 if (syncMessage
.getSent().isPresent()) {
708 System
.out
.println("Received sync sent message");
709 final SentTranscriptMessage sentTranscriptMessage
= syncMessage
.getSent().get();
711 if (sentTranscriptMessage
.getDestination().isPresent()) {
712 String dest
= sentTranscriptMessage
.getDestination().get();
713 ContactInfo destContact
= m
.getContact(dest
);
714 to = (destContact
== null ?
"" : "“" + destContact
.name
+ "” ") + dest
;
718 System
.out
.println("To: " + to + " , Message timestamp: " + sentTranscriptMessage
.getTimestamp());
719 SignalServiceDataMessage message
= sentTranscriptMessage
.getMessage();
720 handleSignalServiceDataMessage(message
);
725 System
.out
.println("Unknown message received.");
727 System
.out
.println();
730 private void handleSignalServiceDataMessage(SignalServiceDataMessage message
) {
731 System
.out
.println("Message timestamp: " + message
.getTimestamp());
733 if (message
.getBody().isPresent()) {
734 System
.out
.println("Body: " + message
.getBody().get());
736 if (message
.getGroupInfo().isPresent()) {
737 SignalServiceGroup groupInfo
= message
.getGroupInfo().get();
738 System
.out
.println("Group info:");
739 System
.out
.println(" Id: " + Base64
.encodeBytes(groupInfo
.getGroupId()));
740 if (groupInfo
.getType() == SignalServiceGroup
.Type
.UPDATE
&& groupInfo
.getName().isPresent()) {
741 System
.out
.println(" Name: " + groupInfo
.getName().get());
743 GroupInfo group
= m
.getGroup(groupInfo
.getGroupId());
745 System
.out
.println(" Name: " + group
.name
);
747 System
.out
.println(" Name: <Unknown group>");
750 System
.out
.println(" Type: " + groupInfo
.getType());
751 if (groupInfo
.getMembers().isPresent()) {
752 for (String member
: groupInfo
.getMembers().get()) {
753 System
.out
.println(" Member: " + member
);
756 if (groupInfo
.getAvatar().isPresent()) {
757 System
.out
.println(" Avatar:");
758 printAttachment(groupInfo
.getAvatar().get());
761 if (message
.isEndSession()) {
762 System
.out
.println("Is end session");
765 if (message
.getAttachments().isPresent()) {
766 System
.out
.println("Attachments: ");
767 for (SignalServiceAttachment attachment
: message
.getAttachments().get()) {
768 printAttachment(attachment
);
773 private void printAttachment(SignalServiceAttachment attachment
) {
774 System
.out
.println("- " + attachment
.getContentType() + " (" + (attachment
.isPointer() ?
"Pointer" : "") + (attachment
.isStream() ?
"Stream" : "") + ")");
775 if (attachment
.isPointer()) {
776 final SignalServiceAttachmentPointer pointer
= attachment
.asPointer();
777 System
.out
.println(" Id: " + pointer
.getId() + " Key length: " + pointer
.getKey().length
+ (pointer
.getRelay().isPresent() ?
" Relay: " + pointer
.getRelay().get() : ""));
778 System
.out
.println(" Size: " + (pointer
.getSize().isPresent() ? pointer
.getSize().get() + " bytes" : "<unavailable>") + (pointer
.getPreview().isPresent() ?
" (Preview is available: " + pointer
.getPreview().get().length
+ " bytes)" : ""));
779 File file
= m
.getAttachmentFile(pointer
.getId());
781 System
.out
.println(" Stored plaintext in: " + file
);
787 private static class DbusReceiveMessageHandler
extends ReceiveMessageHandler
{
788 final DBusConnection conn
;
790 public DbusReceiveMessageHandler(Manager m
, DBusConnection conn
) {
796 public void handleMessage(SignalServiceEnvelope envelope
, SignalServiceContent content
) {
797 super.handleMessage(envelope
, content
);
799 if (!envelope
.isReceipt() && content
!= null && content
.getDataMessage().isPresent()) {
800 SignalServiceDataMessage message
= content
.getDataMessage().get();
802 if (!message
.isEndSession() &&
803 !(message
.getGroupInfo().isPresent() &&
804 message
.getGroupInfo().get().getType() != SignalServiceGroup
.Type
.DELIVER
)) {
805 List
<String
> attachments
= new ArrayList
<>();
806 if (message
.getAttachments().isPresent()) {
807 for (SignalServiceAttachment attachment
: message
.getAttachments().get()) {
808 if (attachment
.isPointer()) {
809 attachments
.add(m
.getAttachmentFile(attachment
.asPointer().getId()).getAbsolutePath());
815 conn
.sendSignal(new Signal
.MessageReceived(
817 message
.getTimestamp(),
818 envelope
.getSource(),
819 message
.getGroupInfo().isPresent() ? message
.getGroupInfo().get().getGroupId() : new byte[0],
820 message
.getBody().isPresent() ? message
.getBody().get() : "",
822 } catch (DBusException e
) {