1 package org
.asamk
.signal
.dbus
;
3 import org
.asamk
.Signal
;
4 import org
.asamk
.signal
.BaseConfig
;
5 import org
.asamk
.signal
.commands
.exceptions
.CommandException
;
6 import org
.asamk
.signal
.commands
.exceptions
.IOErrorException
;
7 import org
.asamk
.signal
.manager
.AttachmentInvalidException
;
8 import org
.asamk
.signal
.manager
.Manager
;
9 import org
.asamk
.signal
.manager
.NotMasterDeviceException
;
10 import org
.asamk
.signal
.manager
.StickerPackInvalidException
;
11 import org
.asamk
.signal
.manager
.UntrustedIdentityException
;
12 import org
.asamk
.signal
.manager
.api
.Identity
;
13 import org
.asamk
.signal
.manager
.api
.Message
;
14 import org
.asamk
.signal
.manager
.api
.RecipientIdentifier
;
15 import org
.asamk
.signal
.manager
.api
.TypingAction
;
16 import org
.asamk
.signal
.manager
.api
.UpdateGroup
;
17 import org
.asamk
.signal
.manager
.groups
.GroupId
;
18 import org
.asamk
.signal
.manager
.groups
.GroupInviteLinkUrl
;
19 import org
.asamk
.signal
.manager
.groups
.GroupLinkState
;
20 import org
.asamk
.signal
.manager
.groups
.GroupNotFoundException
;
21 import org
.asamk
.signal
.manager
.groups
.GroupPermission
;
22 import org
.asamk
.signal
.manager
.groups
.GroupSendingNotAllowedException
;
23 import org
.asamk
.signal
.manager
.groups
.LastGroupAdminException
;
24 import org
.asamk
.signal
.manager
.groups
.NotAGroupMemberException
;
25 import org
.asamk
.signal
.manager
.storage
.recipients
.Profile
;
26 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientAddress
;
27 import org
.asamk
.signal
.util
.ErrorUtils
;
28 import org
.freedesktop
.dbus
.DBusPath
;
29 import org
.freedesktop
.dbus
.connections
.impl
.DBusConnection
;
30 import org
.freedesktop
.dbus
.exceptions
.DBusException
;
31 import org
.freedesktop
.dbus
.exceptions
.DBusExecutionException
;
32 import org
.freedesktop
.dbus
.types
.Variant
;
33 import org
.slf4j
.Logger
;
34 import org
.slf4j
.LoggerFactory
;
35 import org
.whispersystems
.libsignal
.InvalidKeyException
;
36 import org
.whispersystems
.libsignal
.util
.Pair
;
37 import org
.whispersystems
.libsignal
.util
.guava
.Optional
;
38 import org
.whispersystems
.signalservice
.api
.groupsv2
.GroupLinkNotActiveException
;
39 import org
.whispersystems
.signalservice
.api
.messages
.SendMessageResult
;
40 import org
.whispersystems
.signalservice
.api
.push
.exceptions
.UnregisteredUserException
;
41 import org
.whispersystems
.signalservice
.api
.util
.InvalidNumberException
;
42 import org
.whispersystems
.signalservice
.internal
.contacts
.crypto
.UnauthenticatedResponseException
;
45 import java
.io
.IOException
;
47 import java
.net
.URISyntaxException
;
48 import java
.util
.ArrayList
;
49 import java
.util
.Arrays
;
50 import java
.util
.Base64
;
51 import java
.util
.Collection
;
52 import java
.util
.HashSet
;
53 import java
.util
.List
;
55 import java
.util
.Objects
;
57 import java
.util
.UUID
;
58 import java
.util
.stream
.Collectors
;
59 import java
.util
.stream
.Stream
;
61 public class DbusSignalImpl
implements Signal
{
63 private final Manager m
;
64 private final DBusConnection connection
;
65 private final String objectPath
;
67 private DBusPath thisDevice
;
68 private final List
<StructDevice
> devices
= new ArrayList
<>();
69 private final List
<StructGroup
> groups
= new ArrayList
<>();
71 private final static Logger logger
= LoggerFactory
.getLogger(DbusSignalImpl
.class);
73 public DbusSignalImpl(final Manager m
, DBusConnection connection
, final String objectPath
) {
75 this.connection
= connection
;
76 this.objectPath
= objectPath
;
79 public void initObjects() {
82 updateConfiguration();
88 unExportConfiguration();
92 public String
getObjectPath() {
97 public String
getSelfNumber() {
98 return m
.getSelfNumber();
102 public void submitRateLimitChallenge(String challenge
, String captchaString
) throws IOErrorException
{
103 final var captcha
= captchaString
== null ?
null : captchaString
.replace("signalcaptcha://", "");
106 m
.submitRateLimitRecaptchaChallenge(challenge
, captcha
);
107 } catch (IOException e
) {
108 throw new IOErrorException("Submit challenge error: " + e
.getMessage(), e
);
114 public void addDevice(String uri
) {
116 m
.addDeviceLink(new URI(uri
));
117 } catch (IOException
| InvalidKeyException e
) {
118 throw new Error
.Failure(e
.getClass().getSimpleName() + " Add device link failed. " + e
.getMessage());
119 } catch (URISyntaxException e
) {
120 throw new Error
.InvalidUri(e
.getClass().getSimpleName()
121 + " Device link uri has invalid format: "
127 public DBusPath
getDevice(long deviceId
) {
129 final var deviceOptional
= devices
.stream().filter(g
-> g
.getId().equals(deviceId
)).findFirst();
130 if (deviceOptional
.isEmpty()) {
131 throw new Error
.DeviceNotFound("Device not found");
133 return deviceOptional
.get().getObjectPath();
137 public List
<StructDevice
> listDevices() {
143 public DBusPath
getThisDevice() {
149 public long sendMessage(final String message
, final List
<String
> attachments
, final String recipient
) {
150 var recipients
= new ArrayList
<String
>(1);
151 recipients
.add(recipient
);
152 return sendMessage(message
, attachments
, recipients
);
156 public long sendMessage(final String message
, final List
<String
> attachments
, final List
<String
> recipients
) {
158 final var results
= m
.sendMessage(new Message(message
, attachments
),
159 getSingleRecipientIdentifiers(recipients
, m
.getSelfNumber()).stream()
160 .map(RecipientIdentifier
.class::cast
)
161 .collect(Collectors
.toSet()));
163 checkSendMessageResults(results
.getTimestamp(), results
.getResults());
164 return results
.getTimestamp();
165 } catch (AttachmentInvalidException e
) {
166 throw new Error
.AttachmentInvalid(e
.getMessage());
167 } catch (IOException e
) {
168 throw new Error
.Failure(e
.getMessage());
169 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
170 throw new Error
.GroupNotFound(e
.getMessage());
175 public long sendRemoteDeleteMessage(
176 final long targetSentTimestamp
, final String recipient
178 var recipients
= new ArrayList
<String
>(1);
179 recipients
.add(recipient
);
180 return sendRemoteDeleteMessage(targetSentTimestamp
, recipients
);
184 public long sendRemoteDeleteMessage(
185 final long targetSentTimestamp
, final List
<String
> recipients
188 final var results
= m
.sendRemoteDeleteMessage(targetSentTimestamp
,
189 getSingleRecipientIdentifiers(recipients
, m
.getSelfNumber()).stream()
190 .map(RecipientIdentifier
.class::cast
)
191 .collect(Collectors
.toSet()));
192 checkSendMessageResults(results
.getTimestamp(), results
.getResults());
193 return results
.getTimestamp();
194 } catch (IOException e
) {
195 throw new Error
.Failure(e
.getMessage());
196 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
197 throw new Error
.GroupNotFound(e
.getMessage());
202 public long sendGroupRemoteDeleteMessage(
203 final long targetSentTimestamp
, final byte[] groupId
206 final var results
= m
.sendRemoteDeleteMessage(targetSentTimestamp
,
207 Set
.of(new RecipientIdentifier
.Group(getGroupId(groupId
))));
208 checkSendMessageResults(results
.getTimestamp(), results
.getResults());
209 return results
.getTimestamp();
210 } catch (IOException e
) {
211 throw new Error
.Failure(e
.getMessage());
212 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
213 throw new Error
.GroupNotFound(e
.getMessage());
218 public long sendMessageReaction(
220 final boolean remove
,
221 final String targetAuthor
,
222 final long targetSentTimestamp
,
223 final String recipient
225 var recipients
= new ArrayList
<String
>(1);
226 recipients
.add(recipient
);
227 return sendMessageReaction(emoji
, remove
, targetAuthor
, targetSentTimestamp
, recipients
);
231 public long sendMessageReaction(
233 final boolean remove
,
234 final String targetAuthor
,
235 final long targetSentTimestamp
,
236 final List
<String
> recipients
239 final var results
= m
.sendMessageReaction(emoji
,
241 getSingleRecipientIdentifier(targetAuthor
, m
.getSelfNumber()),
243 getSingleRecipientIdentifiers(recipients
, m
.getSelfNumber()).stream()
244 .map(RecipientIdentifier
.class::cast
)
245 .collect(Collectors
.toSet()));
246 checkSendMessageResults(results
.getTimestamp(), results
.getResults());
247 return results
.getTimestamp();
248 } catch (IOException e
) {
249 throw new Error
.Failure(e
.getMessage());
250 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
251 throw new Error
.GroupNotFound(e
.getMessage());
256 public void sendTyping(
257 final String recipient
, final boolean stop
258 ) throws Error
.Failure
, Error
.GroupNotFound
, Error
.UntrustedIdentity
{
260 var recipients
= new ArrayList
<String
>(1);
261 recipients
.add(recipient
);
262 m
.sendTypingMessage(stop ? TypingAction
.STOP
: TypingAction
.START
,
263 getSingleRecipientIdentifiers(recipients
, m
.getSelfNumber()).stream()
264 .map(RecipientIdentifier
.class::cast
)
265 .collect(Collectors
.toSet()));
266 } catch (IOException e
) {
267 throw new Error
.Failure(e
.getMessage());
268 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
269 throw new Error
.GroupNotFound(e
.getMessage());
270 } catch (UntrustedIdentityException e
) {
271 throw new Error
.UntrustedIdentity(e
.getMessage());
276 public void sendReadReceipt(
277 final String recipient
, final List
<Long
> messageIds
278 ) throws Error
.Failure
, Error
.UntrustedIdentity
{
280 m
.sendReadReceipt(getSingleRecipientIdentifier(recipient
, m
.getSelfNumber()), messageIds
);
281 } catch (IOException e
) {
282 throw new Error
.Failure(e
.getMessage());
283 } catch (UntrustedIdentityException e
) {
284 throw new Error
.UntrustedIdentity(e
.getMessage());
289 public void sendContacts() {
292 } catch (IOException e
) {
293 throw new Error
.Failure("SendContacts error: " + e
.getMessage());
298 public void sendSyncRequest() {
300 m
.requestAllSyncData();
301 } catch (IOException e
) {
302 throw new Error
.Failure("Request sync data error: " + e
.getMessage());
307 public long sendNoteToSelfMessage(
308 final String message
, final List
<String
> attachments
309 ) throws Error
.AttachmentInvalid
, Error
.Failure
, Error
.UntrustedIdentity
{
311 final var results
= m
.sendMessage(new Message(message
, attachments
),
312 Set
.of(RecipientIdentifier
.NoteToSelf
.INSTANCE
));
313 checkSendMessageResults(results
.getTimestamp(), results
.getResults());
314 return results
.getTimestamp();
315 } catch (AttachmentInvalidException e
) {
316 throw new Error
.AttachmentInvalid(e
.getMessage());
317 } catch (IOException e
) {
318 throw new Error
.Failure(e
.getMessage());
319 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
320 throw new Error
.GroupNotFound(e
.getMessage());
325 public void sendEndSessionMessage(final List
<String
> recipients
) {
327 final var results
= m
.sendEndSessionMessage(getSingleRecipientIdentifiers(recipients
, m
.getSelfNumber()));
328 checkSendMessageResults(results
.getTimestamp(), results
.getResults());
329 } catch (IOException e
) {
330 throw new Error
.Failure(e
.getMessage());
335 public long sendGroupMessage(final String message
, final List
<String
> attachments
, final byte[] groupId
) {
337 var results
= m
.sendMessage(new Message(message
, attachments
),
338 Set
.of(new RecipientIdentifier
.Group(getGroupId(groupId
))));
339 checkSendMessageResults(results
.getTimestamp(), results
.getResults());
340 return results
.getTimestamp();
341 } catch (IOException e
) {
342 throw new Error
.Failure(e
.getMessage());
343 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
344 throw new Error
.GroupNotFound(e
.getMessage());
345 } catch (AttachmentInvalidException e
) {
346 throw new Error
.AttachmentInvalid(e
.getMessage());
351 public long sendGroupMessageReaction(
353 final boolean remove
,
354 final String targetAuthor
,
355 final long targetSentTimestamp
,
359 final var results
= m
.sendMessageReaction(emoji
,
361 getSingleRecipientIdentifier(targetAuthor
, m
.getSelfNumber()),
363 Set
.of(new RecipientIdentifier
.Group(getGroupId(groupId
))));
364 checkSendMessageResults(results
.getTimestamp(), results
.getResults());
365 return results
.getTimestamp();
366 } catch (IOException e
) {
367 throw new Error
.Failure(e
.getMessage());
368 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
369 throw new Error
.GroupNotFound(e
.getMessage());
373 // Since contact names might be empty if not defined, also potentially return
376 public String
getContactName(final String number
) {
377 final var name
= m
.getContactOrProfileName(getSingleRecipientIdentifier(number
, m
.getSelfNumber()));
378 return name
== null ?
"" : name
;
382 public void setContactName(final String number
, final String name
) {
384 m
.setContactName(getSingleRecipientIdentifier(number
, m
.getSelfNumber()), name
);
385 } catch (NotMasterDeviceException e
) {
386 throw new Error
.Failure("This command doesn't work on linked devices.");
387 } catch (UnregisteredUserException e
) {
388 throw new Error
.Failure("Contact is not registered.");
393 public void setExpirationTimer(final String number
, final int expiration
) {
395 m
.setExpirationTimer(getSingleRecipientIdentifier(number
, m
.getSelfNumber()), expiration
);
396 } catch (IOException e
) {
397 throw new Error
.Failure(e
.getMessage());
402 public void setContactBlocked(final String number
, final boolean blocked
) {
404 m
.setContactBlocked(getSingleRecipientIdentifier(number
, m
.getSelfNumber()), blocked
);
405 } catch (NotMasterDeviceException e
) {
406 throw new Error
.Failure("This command doesn't work on linked devices.");
407 } catch (IOException e
) {
408 throw new Error
.Failure(e
.getMessage());
413 public void setGroupBlocked(final byte[] groupId
, final boolean blocked
) {
415 m
.setGroupBlocked(getGroupId(groupId
), blocked
);
416 } catch (NotMasterDeviceException e
) {
417 throw new Error
.Failure("This command doesn't work on linked devices.");
418 } catch (GroupNotFoundException e
) {
419 throw new Error
.GroupNotFound(e
.getMessage());
420 } catch (IOException e
) {
421 throw new Error
.Failure(e
.getMessage());
426 public List
<byte[]> getGroupIds() {
427 var groups
= m
.getGroups();
428 var ids
= new ArrayList
<byte[]>(groups
.size());
429 for (var group
: groups
) {
430 ids
.add(group
.getGroupId().serialize());
436 public DBusPath
getGroup(final byte[] groupId
) {
438 final var groupOptional
= groups
.stream().filter(g
-> Arrays
.equals(g
.getId(), groupId
)).findFirst();
439 if (groupOptional
.isEmpty()) {
440 throw new Error
.GroupNotFound("Group not found");
442 return groupOptional
.get().getObjectPath();
446 public List
<StructGroup
> listGroups() {
452 public String
getGroupName(final byte[] groupId
) {
453 var group
= m
.getGroup(getGroupId(groupId
));
454 if (group
== null || group
.getTitle() == null) {
457 return group
.getTitle();
462 public List
<String
> getGroupMembers(final byte[] groupId
) {
463 var group
= m
.getGroup(getGroupId(groupId
));
467 final var members
= group
.getMembers();
468 return getRecipientStrings(members
);
473 public byte[] createGroup(
474 final String name
, final List
<String
> members
, final String avatar
475 ) throws Error
.AttachmentInvalid
, Error
.Failure
, Error
.InvalidNumber
{
476 return updateGroup(new byte[0], name
, members
, avatar
);
480 public byte[] updateGroup(byte[] groupId
, String name
, List
<String
> members
, String avatar
) {
482 groupId
= nullIfEmpty(groupId
);
483 name
= nullIfEmpty(name
);
484 avatar
= nullIfEmpty(avatar
);
485 final var memberIdentifiers
= getSingleRecipientIdentifiers(members
, m
.getSelfNumber());
486 if (groupId
== null) {
487 final var results
= m
.createGroup(name
, memberIdentifiers
, avatar
== null ?
null : new File(avatar
));
488 checkSendMessageResults(results
.second().getTimestamp(), results
.second().getResults());
489 return results
.first().serialize();
491 final var results
= m
.updateGroup(getGroupId(groupId
),
492 UpdateGroup
.newBuilder()
494 .withMembers(memberIdentifiers
)
495 .withAvatarFile(avatar
== null ?
null : new File(avatar
))
497 if (results
!= null) {
498 checkSendMessageResults(results
.getTimestamp(), results
.getResults());
502 } catch (IOException e
) {
503 throw new Error
.Failure(e
.getMessage());
504 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
505 throw new Error
.GroupNotFound(e
.getMessage());
506 } catch (AttachmentInvalidException e
) {
507 throw new Error
.AttachmentInvalid(e
.getMessage());
512 public boolean isRegistered() {
517 public boolean isRegistered(String number
) {
518 var result
= isRegistered(List
.of(number
));
519 return result
.get(0);
523 public List
<Boolean
> isRegistered(List
<String
> numbers
) {
524 var results
= new ArrayList
<Boolean
>();
525 if (numbers
.isEmpty()) {
529 Map
<String
, Pair
<String
, UUID
>> registered
;
531 registered
= m
.areUsersRegistered(new HashSet
<>(numbers
));
532 } catch (IOException e
) {
533 throw new Error
.Failure(e
.getMessage());
536 return numbers
.stream().map(number
-> {
537 var uuid
= registered
.get(number
).second();
539 }).collect(Collectors
.toList());
543 public void updateProfile(
549 final boolean removeAvatar
552 givenName
= nullIfEmpty(givenName
);
553 familyName
= nullIfEmpty(familyName
);
554 about
= nullIfEmpty(about
);
555 aboutEmoji
= nullIfEmpty(aboutEmoji
);
556 avatarPath
= nullIfEmpty(avatarPath
);
557 Optional
<File
> avatarFile
= removeAvatar
559 : avatarPath
== null ?
null : Optional
.of(new File(avatarPath
));
560 m
.setProfile(givenName
, familyName
, about
, aboutEmoji
, avatarFile
);
561 } catch (IOException e
) {
562 throw new Error
.Failure(e
.getMessage());
567 public void updateProfile(
570 final String aboutEmoji
,
572 final boolean removeAvatar
574 updateProfile(name
, "", about
, aboutEmoji
, avatarPath
, removeAvatar
);
578 public void removePin() {
580 m
.setRegistrationLockPin(Optional
.absent());
581 } catch (UnauthenticatedResponseException e
) {
582 throw new Error
.Failure("Remove pin failed with unauthenticated response: " + e
.getMessage());
583 } catch (IOException e
) {
584 throw new Error
.Failure("Remove pin error: " + e
.getMessage());
589 public void setPin(String registrationLockPin
) {
591 m
.setRegistrationLockPin(Optional
.of(registrationLockPin
));
592 } catch (UnauthenticatedResponseException e
) {
593 throw new Error
.Failure("Set pin error failed with unauthenticated response: " + e
.getMessage());
594 } catch (IOException e
) {
595 throw new Error
.Failure("Set pin error: " + e
.getMessage());
599 // Provide option to query a version string in order to react on potential
600 // future interface changes
602 public String
version() {
603 return BaseConfig
.PROJECT_VERSION
;
606 // Create a unique list of Numbers from Identities and Contacts to really get
607 // all numbers the system knows
609 public List
<String
> listNumbers() {
610 return Stream
.concat(m
.getIdentities().stream().map(Identity
::getRecipient
),
611 m
.getContacts().stream().map(Pair
::first
))
612 .map(a
-> a
.getNumber().orElse(null))
613 .filter(Objects
::nonNull
)
615 .collect(Collectors
.toList());
619 public List
<String
> getContactNumber(final String name
) {
620 // Contact names have precedence.
621 var numbers
= new ArrayList
<String
>();
622 var contacts
= m
.getContacts();
623 for (var c
: contacts
) {
624 if (name
.equals(c
.second().getName())) {
625 numbers
.add(c
.first().getLegacyIdentifier());
628 // Try profiles if no contact name was found
629 for (var identity
: m
.getIdentities()) {
630 final var address
= identity
.getRecipient();
631 var number
= address
.getNumber().orElse(null);
632 if (number
!= null) {
633 Profile profile
= null;
635 profile
= m
.getRecipientProfile(RecipientIdentifier
.Single
.fromAddress(address
));
636 } catch (UnregisteredUserException ignored
) {
638 if (profile
!= null && profile
.getDisplayName().equals(name
)) {
647 public void quitGroup(final byte[] groupId
) {
648 var group
= getGroupId(groupId
);
650 m
.quitGroup(group
, Set
.of());
651 } catch (GroupNotFoundException
| NotAGroupMemberException e
) {
652 throw new Error
.GroupNotFound(e
.getMessage());
653 } catch (IOException
| LastGroupAdminException e
) {
654 throw new Error
.Failure(e
.getMessage());
659 public byte[] joinGroup(final String groupLink
) {
661 final var linkUrl
= GroupInviteLinkUrl
.fromUri(groupLink
);
662 if (linkUrl
== null) {
663 throw new Error
.Failure("Group link is invalid:");
665 final var result
= m
.joinGroup(linkUrl
);
666 return result
.first().serialize();
667 } catch (GroupInviteLinkUrl
.InvalidGroupLinkException
| GroupLinkNotActiveException e
) {
668 throw new Error
.Failure("Group link is invalid: " + e
.getMessage());
669 } catch (GroupInviteLinkUrl
.UnknownGroupLinkVersionException e
) {
670 throw new Error
.Failure("Group link was created with an incompatible version: " + e
.getMessage());
671 } catch (IOException e
) {
672 throw new Error
.Failure(e
.getMessage());
677 public boolean isContactBlocked(final String number
) {
678 return m
.isContactBlocked(getSingleRecipientIdentifier(number
, m
.getSelfNumber()));
682 public boolean isGroupBlocked(final byte[] groupId
) {
683 var group
= m
.getGroup(getGroupId(groupId
));
687 return group
.isBlocked();
692 public boolean isMember(final byte[] groupId
) {
693 var group
= m
.getGroup(getGroupId(groupId
));
697 return group
.isMember();
702 public String
uploadStickerPack(String stickerPackPath
) {
703 File path
= new File(stickerPackPath
);
705 return m
.uploadStickerPack(path
).toString();
706 } catch (IOException e
) {
707 throw new Error
.IOError("Upload error (maybe image size is too large):" + e
.getMessage());
708 } catch (StickerPackInvalidException e
) {
709 throw new Error
.Failure("Invalid sticker pack: " + e
.getMessage());
713 private static void checkSendMessageResult(long timestamp
, SendMessageResult result
) throws DBusExecutionException
{
714 var error
= ErrorUtils
.getErrorMessageFromSendMessageResult(result
);
720 final var message
= timestamp
+ "\nFailed to send message:\n" + error
+ '\n';
722 if (result
.getIdentityFailure() != null) {
723 throw new Error
.UntrustedIdentity(message
);
725 throw new Error
.Failure(message
);
729 private static void checkSendMessageResults(
730 long timestamp
, Map
<RecipientIdentifier
, List
<SendMessageResult
>> results
731 ) throws DBusExecutionException
{
732 final var sendMessageResults
= results
.values().stream().findFirst();
733 if (results
.size() == 1 && sendMessageResults
.get().size() == 1) {
734 checkSendMessageResult(timestamp
, sendMessageResults
.get().stream().findFirst().get());
738 var errors
= ErrorUtils
.getErrorMessagesFromSendMessageResults(results
);
739 if (errors
.size() == 0) {
743 var message
= new StringBuilder();
744 message
.append(timestamp
).append('\n');
745 message
.append("Failed to send (some) messages:\n");
746 for (var error
: errors
) {
747 message
.append(error
).append('\n');
750 throw new Error
.Failure(message
.toString());
753 private static void checkSendMessageResults(
754 long timestamp
, Collection
<SendMessageResult
> results
755 ) throws DBusExecutionException
{
756 if (results
.size() == 1) {
757 checkSendMessageResult(timestamp
, results
.stream().findFirst().get());
761 var errors
= ErrorUtils
.getErrorMessagesFromSendMessageResults(results
);
762 if (errors
.size() == 0) {
766 var message
= new StringBuilder();
767 message
.append(timestamp
).append('\n');
768 message
.append("Failed to send (some) messages:\n");
769 for (var error
: errors
) {
770 message
.append(error
).append('\n');
773 throw new Error
.Failure(message
.toString());
776 private static List
<String
> getRecipientStrings(final Set
<RecipientAddress
> members
) {
777 return members
.stream().map(RecipientAddress
::getLegacyIdentifier
).collect(Collectors
.toList());
780 private static Set
<RecipientIdentifier
.Single
> getSingleRecipientIdentifiers(
781 final Collection
<String
> recipientStrings
, final String localNumber
782 ) throws DBusExecutionException
{
783 final var identifiers
= new HashSet
<RecipientIdentifier
.Single
>();
784 for (var recipientString
: recipientStrings
) {
785 identifiers
.add(getSingleRecipientIdentifier(recipientString
, localNumber
));
790 private static RecipientIdentifier
.Single
getSingleRecipientIdentifier(
791 final String recipientString
, final String localNumber
792 ) throws DBusExecutionException
{
794 return RecipientIdentifier
.Single
.fromString(recipientString
, localNumber
);
795 } catch (InvalidNumberException e
) {
796 throw new Error
.InvalidNumber(e
.getMessage());
800 private static GroupId
getGroupId(byte[] groupId
) throws DBusExecutionException
{
802 return GroupId
.unknownVersion(groupId
);
803 } catch (Throwable e
) {
804 throw new Error
.InvalidGroupId("Invalid group id: " + e
.getMessage());
808 private byte[] nullIfEmpty(final byte[] array
) {
809 return array
.length
== 0 ?
null : array
;
812 private String
nullIfEmpty(final String name
) {
813 return name
.isEmpty() ?
null : name
;
816 private String
emptyIfNull(final String string
) {
817 return string
== null ?
"" : string
;
820 private static String
getDeviceObjectPath(String basePath
, long deviceId
) {
821 return basePath
+ "/Devices/" + deviceId
;
824 private void updateDevices() {
825 List
<org
.asamk
.signal
.manager
.api
.Device
> linkedDevices
;
827 linkedDevices
= m
.getLinkedDevices();
828 } catch (IOException e
) {
829 throw new Error
.Failure("Failed to get linked devices: " + e
.getMessage());
834 linkedDevices
.forEach(d
-> {
835 final var object
= new DbusSignalDeviceImpl(d
);
836 final var deviceObjectPath
= object
.getObjectPath();
838 connection
.exportObject(object
);
839 logger
.debug("Exported dbus object: " + deviceObjectPath
);
840 } catch (DBusException e
) {
843 if (d
.isThisDevice()) {
844 thisDevice
= new DBusPath(deviceObjectPath
);
846 this.devices
.add(new StructDevice(new DBusPath(deviceObjectPath
), d
.getId(), emptyIfNull(d
.getName())));
850 private void unExportDevices() {
851 this.devices
.stream()
852 .map(StructDevice
::getObjectPath
)
853 .map(DBusPath
::getPath
)
854 .forEach(connection
::unExportObject
);
855 this.devices
.clear();
858 private static String
getGroupObjectPath(String basePath
, byte[] groupId
) {
859 return basePath
+ "/Groups/" + Base64
.getEncoder()
860 .encodeToString(groupId
)
866 private void updateGroups() {
867 List
<org
.asamk
.signal
.manager
.api
.Group
> groups
;
868 groups
= m
.getGroups();
872 groups
.forEach(g
-> {
873 final var object
= new DbusSignalGroupImpl(g
.getGroupId());
875 connection
.exportObject(object
);
876 logger
.debug("Exported dbus object: " + object
.getObjectPath());
877 } catch (DBusException e
) {
880 this.groups
.add(new StructGroup(new DBusPath(object
.getObjectPath()),
881 g
.getGroupId().serialize(),
882 emptyIfNull(g
.getTitle())));
886 private void unExportGroups() {
887 this.groups
.stream().map(StructGroup
::getObjectPath
).map(DBusPath
::getPath
).forEach(connection
::unExportObject
);
891 private void unExportConfiguration() {
892 final var object
= getConfigurationObjectPath(objectPath
);
893 connection
.unExportObject(object
);
896 private static String
getConfigurationObjectPath(String basePath
) {
897 return basePath
+ "/Configuration";
900 private void updateConfiguration() {
902 Boolean readReceipts
= null;
903 Boolean unidentifiedDeliveryIndicators
= null;
904 Boolean typingIndicators
= null;
905 Boolean linkPreviews
= null;
906 List
<Boolean
> configuration
= new ArrayList
<>(4);
909 configuration
= m
.getConfiguration();
910 } catch (NotMasterDeviceException e
) {
911 logger
.debug("Not exporting Configuration for " + m
.getSelfNumber() + ": " + e
.getMessage());
913 } catch (IOException e
) {
914 throw new Error
.IOError(objectPath
+ e
.getMessage());
915 } catch (NullPointerException e
) {
916 logger
.info("No configuration found, creating one for " + m
.getSelfNumber() + ": " + e
.getMessage());
918 unidentifiedDeliveryIndicators
= true;
919 typingIndicators
= true;
922 if (readReceipts
== null) {
924 readReceipts
= configuration
.get(0);
925 } catch (NullPointerException e
) {
929 if (unidentifiedDeliveryIndicators
== null) {
931 unidentifiedDeliveryIndicators
= configuration
.get(1);
932 } catch (NullPointerException e
) {
933 unidentifiedDeliveryIndicators
= true;
936 if (typingIndicators
== null) {
938 typingIndicators
= configuration
.get(2);
939 } catch (NullPointerException e
) {
940 typingIndicators
= true;
943 if (linkPreviews
== null) {
945 linkPreviews
= configuration
.get(3);
946 } catch (NullPointerException e
) {
952 unExportConfiguration();
953 m
.updateConfiguration(readReceipts
, unidentifiedDeliveryIndicators
, typingIndicators
, linkPreviews
);
954 final var object
= new DbusSignalConfigurationImpl(readReceipts
, unidentifiedDeliveryIndicators
, typingIndicators
, linkPreviews
);
955 connection
.exportObject(object
);
956 logger
.debug("Exported dbus object: " + objectPath
+ "/Configuration");
957 } catch (NotMasterDeviceException ignore
) {
959 } catch (IOException e
) {
960 throw new Error
.IOError(objectPath
+ e
.getMessage());
961 } catch (DBusException e
) {
967 public class DbusSignalDeviceImpl
extends DbusProperties
implements Signal
.Device
{
969 private final org
.asamk
.signal
.manager
.api
.Device device
;
971 public DbusSignalDeviceImpl(final org
.asamk
.signal
.manager
.api
.Device device
) {
972 super.addPropertiesHandler(new DbusInterfacePropertiesHandler("org.asamk.Signal.Device",
973 List
.of(new DbusProperty
<>("Id", device
::getId
),
974 new DbusProperty
<>("Name", () -> emptyIfNull(device
.getName()), this::setDeviceName
),
975 new DbusProperty
<>("Created", device
::getCreated
),
976 new DbusProperty
<>("LastSeen", device
::getLastSeen
))));
977 this.device
= device
;
981 public String
getObjectPath() {
982 return getDeviceObjectPath(objectPath
, device
.getId());
986 public void removeDevice() throws Error
.Failure
{
988 m
.removeLinkedDevices(device
.getId());
990 } catch (IOException e
) {
991 throw new Error
.Failure(e
.getMessage());
995 private void setDeviceName(String name
) {
996 if (!device
.isThisDevice()) {
997 throw new Error
.Failure("Only the name of this device can be changed");
1000 m
.updateAccountAttributes(name
);
1001 // update device list
1003 } catch (IOException e
) {
1004 throw new Error
.Failure(e
.getMessage());
1009 public class DbusSignalConfigurationImpl
extends DbusProperties
implements Signal
.Configuration
{
1011 private final Boolean readReceipts
;
1012 private final Boolean unidentifiedDeliveryIndicators
;
1013 private final Boolean typingIndicators
;
1014 private final Boolean linkPreviews
;
1016 public DbusSignalConfigurationImpl(final Boolean readReceipts
, final Boolean unidentifiedDeliveryIndicators
, final Boolean typingIndicators
, final Boolean linkPreviews
) {
1017 this.readReceipts
= readReceipts
;
1018 this.unidentifiedDeliveryIndicators
= unidentifiedDeliveryIndicators
;
1019 this.typingIndicators
= typingIndicators
;
1020 this.linkPreviews
= linkPreviews
;
1021 super.addPropertiesHandler(new DbusInterfacePropertiesHandler("org.asamk.Signal.Configuration",
1022 List
.of(new DbusProperty
<>("ConfigurationReadReceipts", () -> getReadReceipts(), this::setReadReceipts
),
1023 new DbusProperty
<>("ConfigurationUnidentifiedDeliveryIndicators", () -> getUnidentifiedDeliveryIndicators(), this::setUnidentifiedDeliveryIndicators
),
1024 new DbusProperty
<>("ConfigurationTypingIndicators", () -> getTypingIndicators(), this::setTypingIndicators
),
1025 new DbusProperty
<>("ConfigurationLinkPreviews", () -> getLinkPreviews(), this::setLinkPreviews
)
1032 public String
getObjectPath() {
1033 return getConfigurationObjectPath(objectPath
);
1036 public void setReadReceipts(Boolean readReceipts
) {
1037 setConfiguration(readReceipts
, null, null, null);
1040 public void setUnidentifiedDeliveryIndicators(Boolean unidentifiedDeliveryIndicators
) {
1041 setConfiguration(null, unidentifiedDeliveryIndicators
, null, null);
1044 public void setTypingIndicators(Boolean typingIndicators
) {
1045 setConfiguration(null, null, typingIndicators
, null);
1048 public void setLinkPreviews(Boolean linkPreviews
) {
1049 setConfiguration(null, null, null, linkPreviews
);
1052 private void setConfiguration(Boolean readReceipts
, Boolean unidentifiedDeliveryIndicators
, Boolean typingIndicators
, Boolean linkPreviews
) {
1054 if (readReceipts
== null) {
1055 readReceipts
= m
.getConfiguration().get(0);
1057 if (unidentifiedDeliveryIndicators
== null) {
1058 unidentifiedDeliveryIndicators
= m
.getConfiguration().get(1);
1060 if (typingIndicators
== null) {
1061 typingIndicators
= m
.getConfiguration().get(2);
1063 if (linkPreviews
== null) {
1064 linkPreviews
= m
.getConfiguration().get(3);
1066 m
.updateConfiguration(readReceipts
, unidentifiedDeliveryIndicators
, typingIndicators
, linkPreviews
);
1067 } catch (IOException e
) {
1068 throw new Error
.IOError("UpdateAccount error: " + e
.getMessage());
1069 } catch (NotMasterDeviceException e
) {
1070 throw new Error
.UserError("This command doesn't work on linked devices.");
1074 public List
<Boolean
> getConfiguration() {
1075 List
<Boolean
> config
= new ArrayList
<>(4);
1077 config
= m
.getConfiguration();
1078 } catch (IOException e
) {
1079 throw new Error
.IOError("Configuration storage error: " + e
.getMessage());
1080 } catch (NotMasterDeviceException e
) {
1081 throw new Error
.UserError("This command doesn't work on linked devices.");
1086 public Boolean
getReadReceipts() {
1088 return m
.getConfiguration().get(0);
1089 } catch (IOException e
) {
1090 throw new Error
.IOError("Configuration storage error: " + e
.getMessage());
1091 } catch (NotMasterDeviceException e
) {
1092 throw new Error
.UserError("This command doesn't work on linked devices.");
1096 public Boolean
getUnidentifiedDeliveryIndicators() {
1098 return m
.getConfiguration().get(1);
1099 } catch (IOException e
) {
1100 throw new Error
.IOError("Configuration storage error: " + e
.getMessage());
1101 } catch (NotMasterDeviceException e
) {
1102 throw new Error
.UserError("This command doesn't work on linked devices.");
1106 public Boolean
getTypingIndicators() {
1108 return m
.getConfiguration().get(2);
1109 } catch (IOException e
) {
1110 throw new Error
.IOError("Configuration storage error: " + e
.getMessage());
1111 } catch (NotMasterDeviceException e
) {
1112 throw new Error
.UserError("This command doesn't work on linked devices.");
1116 public Boolean
getLinkPreviews() {
1118 return m
.getConfiguration().get(3);
1119 } catch (IOException e
) {
1120 throw new Error
.IOError("Configuration storage error: " + e
.getMessage());
1121 } catch (NotMasterDeviceException e
) {
1122 throw new Error
.UserError("This command doesn't work on linked devices.");
1127 public class DbusSignalGroupImpl
extends DbusProperties
implements Signal
.Group
{
1129 private final GroupId groupId
;
1131 public DbusSignalGroupImpl(final GroupId groupId
) {
1132 this.groupId
= groupId
;
1133 super.addPropertiesHandler(new DbusInterfacePropertiesHandler("org.asamk.Signal.Group",
1134 List
.of(new DbusProperty
<>("Id", groupId
::serialize
),
1135 new DbusProperty
<>("Name", () -> emptyIfNull(getGroup().getTitle()), this::setGroupName
),
1136 new DbusProperty
<>("Description",
1137 () -> emptyIfNull(getGroup().getDescription()),
1138 this::setGroupDescription
),
1139 new DbusProperty
<>("Avatar", this::setGroupAvatar
),
1140 new DbusProperty
<>("IsBlocked", () -> getGroup().isBlocked(), this::setIsBlocked
),
1141 new DbusProperty
<>("IsMember", () -> getGroup().isMember()),
1142 new DbusProperty
<>("IsAdmin", () -> getGroup().isAdmin()),
1143 new DbusProperty
<>("MessageExpirationTimer",
1144 () -> getGroup().getMessageExpirationTimer(),
1145 this::setMessageExpirationTime
),
1146 new DbusProperty
<>("Members",
1147 () -> new Variant
<>(getRecipientStrings(getGroup().getMembers()), "as")),
1148 new DbusProperty
<>("PendingMembers",
1149 () -> new Variant
<>(getRecipientStrings(getGroup().getPendingMembers()), "as")),
1150 new DbusProperty
<>("RequestingMembers",
1151 () -> new Variant
<>(getRecipientStrings(getGroup().getRequestingMembers()), "as")),
1152 new DbusProperty
<>("Admins",
1153 () -> new Variant
<>(getRecipientStrings(getGroup().getAdminMembers()), "as")),
1154 new DbusProperty
<>("PermissionAddMember",
1155 () -> getGroup().getPermissionAddMember().name(),
1156 this::setGroupPermissionAddMember
),
1157 new DbusProperty
<>("PermissionEditDetails",
1158 () -> getGroup().getPermissionEditDetails().name(),
1159 this::setGroupPermissionEditDetails
),
1160 new DbusProperty
<>("PermissionSendMessage",
1161 () -> getGroup().getPermissionSendMessage().name(),
1162 this::setGroupPermissionSendMessage
),
1163 new DbusProperty
<>("GroupInviteLink", () -> {
1164 final var groupInviteLinkUrl
= getGroup().getGroupInviteLinkUrl();
1165 return groupInviteLinkUrl
== null ?
"" : groupInviteLinkUrl
.getUrl();
1170 public String
getObjectPath() {
1171 return getGroupObjectPath(objectPath
, groupId
.serialize());
1175 public void quitGroup() throws Error
.Failure
{
1177 m
.quitGroup(groupId
, Set
.of());
1178 } catch (GroupNotFoundException
| NotAGroupMemberException e
) {
1179 throw new Error
.GroupNotFound(e
.getMessage());
1180 } catch (IOException e
) {
1181 throw new Error
.Failure(e
.getMessage());
1182 } catch (LastGroupAdminException e
) {
1183 throw new Error
.LastGroupAdmin(e
.getMessage());
1188 public void addMembers(final List
<String
> recipients
) throws Error
.Failure
{
1189 final var memberIdentifiers
= getSingleRecipientIdentifiers(recipients
, m
.getSelfNumber());
1190 updateGroup(UpdateGroup
.newBuilder().withMembers(memberIdentifiers
).build());
1194 public void removeMembers(final List
<String
> recipients
) throws Error
.Failure
{
1195 final var memberIdentifiers
= getSingleRecipientIdentifiers(recipients
, m
.getSelfNumber());
1196 updateGroup(UpdateGroup
.newBuilder().withRemoveMembers(memberIdentifiers
).build());
1200 public void addAdmins(final List
<String
> recipients
) throws Error
.Failure
{
1201 final var memberIdentifiers
= getSingleRecipientIdentifiers(recipients
, m
.getSelfNumber());
1202 updateGroup(UpdateGroup
.newBuilder().withAdmins(memberIdentifiers
).build());
1206 public void removeAdmins(final List
<String
> recipients
) throws Error
.Failure
{
1207 final var memberIdentifiers
= getSingleRecipientIdentifiers(recipients
, m
.getSelfNumber());
1208 updateGroup(UpdateGroup
.newBuilder().withRemoveAdmins(memberIdentifiers
).build());
1212 public void resetLink() throws Error
.Failure
{
1213 updateGroup(UpdateGroup
.newBuilder().withResetGroupLink(true).build());
1217 public void disableLink() throws Error
.Failure
{
1218 updateGroup(UpdateGroup
.newBuilder().withGroupLinkState(GroupLinkState
.DISABLED
).build());
1222 public void enableLink(final boolean requiresApproval
) throws Error
.Failure
{
1223 updateGroup(UpdateGroup
.newBuilder()
1224 .withGroupLinkState(requiresApproval
1225 ? GroupLinkState
.ENABLED_WITH_APPROVAL
1226 : GroupLinkState
.ENABLED
)
1230 private org
.asamk
.signal
.manager
.api
.Group
getGroup() {
1231 return m
.getGroup(groupId
);
1234 private void setGroupName(final String name
) {
1235 updateGroup(UpdateGroup
.newBuilder().withName(name
).build());
1238 private void setGroupDescription(final String description
) {
1239 updateGroup(UpdateGroup
.newBuilder().withDescription(description
).build());
1242 private void setGroupAvatar(final String avatar
) {
1243 updateGroup(UpdateGroup
.newBuilder().withAvatarFile(new File(avatar
)).build());
1246 private void setMessageExpirationTime(final int expirationTime
) {
1247 updateGroup(UpdateGroup
.newBuilder().withExpirationTimer(expirationTime
).build());
1250 private void setGroupPermissionAddMember(final String permission
) {
1251 updateGroup(UpdateGroup
.newBuilder().withAddMemberPermission(GroupPermission
.valueOf(permission
)).build());
1254 private void setGroupPermissionEditDetails(final String permission
) {
1255 updateGroup(UpdateGroup
.newBuilder()
1256 .withEditDetailsPermission(GroupPermission
.valueOf(permission
))
1260 private void setGroupPermissionSendMessage(final String permission
) {
1261 updateGroup(UpdateGroup
.newBuilder()
1262 .withIsAnnouncementGroup(GroupPermission
.valueOf(permission
) == GroupPermission
.ONLY_ADMINS
)
1266 private void setIsBlocked(final boolean isBlocked
) {
1268 m
.setGroupBlocked(groupId
, isBlocked
);
1269 } catch (NotMasterDeviceException e
) {
1270 throw new Error
.Failure("This command doesn't work on linked devices.");
1271 } catch (GroupNotFoundException e
) {
1272 throw new Error
.GroupNotFound(e
.getMessage());
1273 } catch (IOException e
) {
1274 throw new Error
.Failure(e
.getMessage());
1278 private void updateGroup(final UpdateGroup updateGroup
) {
1280 m
.updateGroup(groupId
, updateGroup
);
1281 } catch (IOException e
) {
1282 throw new Error
.Failure(e
.getMessage());
1283 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
1284 throw new Error
.GroupNotFound(e
.getMessage());
1285 } catch (AttachmentInvalidException e
) {
1286 throw new Error
.AttachmentInvalid(e
.getMessage());