1 package org
.asamk
.signal
.dbus
;
3 import org
.asamk
.Signal
;
4 import org
.asamk
.signal
.BaseConfig
;
5 import org
.asamk
.signal
.commands
.exceptions
.IOErrorException
;
6 import org
.asamk
.signal
.manager
.AttachmentInvalidException
;
7 import org
.asamk
.signal
.manager
.Manager
;
8 import org
.asamk
.signal
.manager
.NotMasterDeviceException
;
9 import org
.asamk
.signal
.manager
.StickerPackInvalidException
;
10 import org
.asamk
.signal
.manager
.UntrustedIdentityException
;
11 import org
.asamk
.signal
.manager
.api
.Identity
;
12 import org
.asamk
.signal
.manager
.api
.Message
;
13 import org
.asamk
.signal
.manager
.api
.RecipientIdentifier
;
14 import org
.asamk
.signal
.manager
.api
.TypingAction
;
15 import org
.asamk
.signal
.manager
.api
.UpdateGroup
;
16 import org
.asamk
.signal
.manager
.groups
.GroupId
;
17 import org
.asamk
.signal
.manager
.groups
.GroupInviteLinkUrl
;
18 import org
.asamk
.signal
.manager
.groups
.GroupLinkState
;
19 import org
.asamk
.signal
.manager
.groups
.GroupNotFoundException
;
20 import org
.asamk
.signal
.manager
.groups
.GroupPermission
;
21 import org
.asamk
.signal
.manager
.groups
.GroupSendingNotAllowedException
;
22 import org
.asamk
.signal
.manager
.groups
.LastGroupAdminException
;
23 import org
.asamk
.signal
.manager
.groups
.NotAGroupMemberException
;
24 import org
.asamk
.signal
.manager
.storage
.recipients
.Profile
;
25 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientAddress
;
26 import org
.asamk
.signal
.util
.ErrorUtils
;
27 import org
.freedesktop
.dbus
.DBusPath
;
28 import org
.freedesktop
.dbus
.connections
.impl
.DBusConnection
;
29 import org
.freedesktop
.dbus
.exceptions
.DBusException
;
30 import org
.freedesktop
.dbus
.exceptions
.DBusExecutionException
;
31 import org
.freedesktop
.dbus
.types
.Variant
;
32 import org
.whispersystems
.libsignal
.InvalidKeyException
;
33 import org
.whispersystems
.libsignal
.util
.Pair
;
34 import org
.whispersystems
.libsignal
.util
.guava
.Optional
;
35 import org
.whispersystems
.signalservice
.api
.groupsv2
.GroupLinkNotActiveException
;
36 import org
.whispersystems
.signalservice
.api
.messages
.SendMessageResult
;
37 import org
.whispersystems
.signalservice
.api
.push
.exceptions
.UnregisteredUserException
;
38 import org
.whispersystems
.signalservice
.api
.util
.InvalidNumberException
;
39 import org
.whispersystems
.signalservice
.internal
.contacts
.crypto
.UnauthenticatedResponseException
;
42 import java
.io
.IOException
;
44 import java
.net
.URISyntaxException
;
45 import java
.util
.ArrayList
;
46 import java
.util
.Arrays
;
47 import java
.util
.Base64
;
48 import java
.util
.Collection
;
49 import java
.util
.HashSet
;
50 import java
.util
.List
;
52 import java
.util
.Objects
;
54 import java
.util
.UUID
;
55 import java
.util
.stream
.Collectors
;
56 import java
.util
.stream
.Stream
;
58 public class DbusSignalImpl
implements Signal
{
60 private final Manager m
;
61 private final DBusConnection connection
;
62 private final String objectPath
;
64 private DBusPath thisDevice
;
65 private final List
<StructDevice
> devices
= new ArrayList
<>();
66 private final List
<StructGroup
> groups
= new ArrayList
<>();
68 public DbusSignalImpl(final Manager m
, DBusConnection connection
, final String objectPath
) {
70 this.connection
= connection
;
71 this.objectPath
= objectPath
;
74 public void initObjects() {
84 public String
getObjectPath() {
89 public String
getSelfNumber() {
90 return m
.getSelfNumber();
94 public void submitRateLimitChallenge(String challenge
, String captchaString
) throws IOErrorException
{
95 final var captcha
= captchaString
== null ?
null : captchaString
.replace("signalcaptcha://", "");
98 m
.submitRateLimitRecaptchaChallenge(challenge
, captcha
);
99 } catch (IOException e
) {
100 throw new IOErrorException("Submit challenge error: " + e
.getMessage(), e
);
106 public void addDevice(String uri
) {
108 m
.addDeviceLink(new URI(uri
));
109 } catch (IOException
| InvalidKeyException e
) {
110 throw new Error
.Failure(e
.getClass().getSimpleName() + " Add device link failed. " + e
.getMessage());
111 } catch (URISyntaxException e
) {
112 throw new Error
.InvalidUri(e
.getClass().getSimpleName()
113 + " Device link uri has invalid format: "
119 public DBusPath
getDevice(long deviceId
) {
121 final var deviceOptional
= devices
.stream().filter(g
-> g
.getId().equals(deviceId
)).findFirst();
122 if (deviceOptional
.isEmpty()) {
123 throw new Error
.DeviceNotFound("Device not found");
125 return deviceOptional
.get().getObjectPath();
129 public List
<StructDevice
> listDevices() {
135 public DBusPath
getThisDevice() {
141 public long sendMessage(final String message
, final List
<String
> attachments
, final String recipient
) {
142 var recipients
= new ArrayList
<String
>(1);
143 recipients
.add(recipient
);
144 return sendMessage(message
, attachments
, recipients
);
148 public long sendMessage(final String message
, final List
<String
> attachments
, final List
<String
> recipients
) {
150 final var results
= m
.sendMessage(new Message(message
, attachments
),
151 getSingleRecipientIdentifiers(recipients
, m
.getSelfNumber()).stream()
152 .map(RecipientIdentifier
.class::cast
)
153 .collect(Collectors
.toSet()));
155 checkSendMessageResults(results
.getTimestamp(), results
.getResults());
156 return results
.getTimestamp();
157 } catch (AttachmentInvalidException e
) {
158 throw new Error
.AttachmentInvalid(e
.getMessage());
159 } catch (IOException e
) {
160 throw new Error
.Failure(e
.getMessage());
161 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
162 throw new Error
.GroupNotFound(e
.getMessage());
167 public long sendRemoteDeleteMessage(
168 final long targetSentTimestamp
, final String recipient
170 var recipients
= new ArrayList
<String
>(1);
171 recipients
.add(recipient
);
172 return sendRemoteDeleteMessage(targetSentTimestamp
, recipients
);
176 public long sendRemoteDeleteMessage(
177 final long targetSentTimestamp
, final List
<String
> recipients
180 final var results
= m
.sendRemoteDeleteMessage(targetSentTimestamp
,
181 getSingleRecipientIdentifiers(recipients
, m
.getSelfNumber()).stream()
182 .map(RecipientIdentifier
.class::cast
)
183 .collect(Collectors
.toSet()));
184 checkSendMessageResults(results
.getTimestamp(), results
.getResults());
185 return results
.getTimestamp();
186 } catch (IOException e
) {
187 throw new Error
.Failure(e
.getMessage());
188 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
189 throw new Error
.GroupNotFound(e
.getMessage());
194 public long sendGroupRemoteDeleteMessage(
195 final long targetSentTimestamp
, final byte[] groupId
198 final var results
= m
.sendRemoteDeleteMessage(targetSentTimestamp
,
199 Set
.of(new RecipientIdentifier
.Group(getGroupId(groupId
))));
200 checkSendMessageResults(results
.getTimestamp(), results
.getResults());
201 return results
.getTimestamp();
202 } catch (IOException e
) {
203 throw new Error
.Failure(e
.getMessage());
204 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
205 throw new Error
.GroupNotFound(e
.getMessage());
210 public long sendMessageReaction(
212 final boolean remove
,
213 final String targetAuthor
,
214 final long targetSentTimestamp
,
215 final String recipient
217 var recipients
= new ArrayList
<String
>(1);
218 recipients
.add(recipient
);
219 return sendMessageReaction(emoji
, remove
, targetAuthor
, targetSentTimestamp
, recipients
);
223 public long sendMessageReaction(
225 final boolean remove
,
226 final String targetAuthor
,
227 final long targetSentTimestamp
,
228 final List
<String
> recipients
231 final var results
= m
.sendMessageReaction(emoji
,
233 getSingleRecipientIdentifier(targetAuthor
, m
.getSelfNumber()),
235 getSingleRecipientIdentifiers(recipients
, m
.getSelfNumber()).stream()
236 .map(RecipientIdentifier
.class::cast
)
237 .collect(Collectors
.toSet()));
238 checkSendMessageResults(results
.getTimestamp(), results
.getResults());
239 return results
.getTimestamp();
240 } catch (IOException e
) {
241 throw new Error
.Failure(e
.getMessage());
242 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
243 throw new Error
.GroupNotFound(e
.getMessage());
248 public void sendTyping(
249 final String recipient
, final boolean stop
250 ) throws Error
.Failure
, Error
.GroupNotFound
, Error
.UntrustedIdentity
{
252 var recipients
= new ArrayList
<String
>(1);
253 recipients
.add(recipient
);
254 m
.sendTypingMessage(stop ? TypingAction
.STOP
: TypingAction
.START
,
255 getSingleRecipientIdentifiers(recipients
, m
.getSelfNumber()).stream()
256 .map(RecipientIdentifier
.class::cast
)
257 .collect(Collectors
.toSet()));
258 } catch (IOException e
) {
259 throw new Error
.Failure(e
.getMessage());
260 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
261 throw new Error
.GroupNotFound(e
.getMessage());
262 } catch (UntrustedIdentityException e
) {
263 throw new Error
.UntrustedIdentity(e
.getMessage());
268 public void sendReadReceipt(
269 final String recipient
, final List
<Long
> messageIds
270 ) throws Error
.Failure
, Error
.UntrustedIdentity
{
272 m
.sendReadReceipt(getSingleRecipientIdentifier(recipient
, m
.getSelfNumber()), messageIds
);
273 } catch (IOException e
) {
274 throw new Error
.Failure(e
.getMessage());
275 } catch (UntrustedIdentityException e
) {
276 throw new Error
.UntrustedIdentity(e
.getMessage());
281 public void sendContacts() {
284 } catch (IOException e
) {
285 throw new Error
.Failure("SendContacts error: " + e
.getMessage());
290 public void sendSyncRequest() {
292 m
.requestAllSyncData();
293 } catch (IOException e
) {
294 throw new Error
.Failure("Request sync data error: " + e
.getMessage());
299 public long sendNoteToSelfMessage(
300 final String message
, final List
<String
> attachments
301 ) throws Error
.AttachmentInvalid
, Error
.Failure
, Error
.UntrustedIdentity
{
303 final var results
= m
.sendMessage(new Message(message
, attachments
),
304 Set
.of(RecipientIdentifier
.NoteToSelf
.INSTANCE
));
305 checkSendMessageResults(results
.getTimestamp(), results
.getResults());
306 return results
.getTimestamp();
307 } catch (AttachmentInvalidException e
) {
308 throw new Error
.AttachmentInvalid(e
.getMessage());
309 } catch (IOException e
) {
310 throw new Error
.Failure(e
.getMessage());
311 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
312 throw new Error
.GroupNotFound(e
.getMessage());
317 public void sendEndSessionMessage(final List
<String
> recipients
) {
319 final var results
= m
.sendEndSessionMessage(getSingleRecipientIdentifiers(recipients
, m
.getSelfNumber()));
320 checkSendMessageResults(results
.getTimestamp(), results
.getResults());
321 } catch (IOException e
) {
322 throw new Error
.Failure(e
.getMessage());
327 public long sendGroupMessage(final String message
, final List
<String
> attachments
, final byte[] groupId
) {
329 var results
= m
.sendMessage(new Message(message
, attachments
),
330 Set
.of(new RecipientIdentifier
.Group(getGroupId(groupId
))));
331 checkSendMessageResults(results
.getTimestamp(), results
.getResults());
332 return results
.getTimestamp();
333 } catch (IOException e
) {
334 throw new Error
.Failure(e
.getMessage());
335 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
336 throw new Error
.GroupNotFound(e
.getMessage());
337 } catch (AttachmentInvalidException e
) {
338 throw new Error
.AttachmentInvalid(e
.getMessage());
343 public long sendGroupMessageReaction(
345 final boolean remove
,
346 final String targetAuthor
,
347 final long targetSentTimestamp
,
351 final var results
= m
.sendMessageReaction(emoji
,
353 getSingleRecipientIdentifier(targetAuthor
, m
.getSelfNumber()),
355 Set
.of(new RecipientIdentifier
.Group(getGroupId(groupId
))));
356 checkSendMessageResults(results
.getTimestamp(), results
.getResults());
357 return results
.getTimestamp();
358 } catch (IOException e
) {
359 throw new Error
.Failure(e
.getMessage());
360 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
361 throw new Error
.GroupNotFound(e
.getMessage());
365 // Since contact names might be empty if not defined, also potentially return
368 public String
getContactName(final String number
) {
369 final var name
= m
.getContactOrProfileName(getSingleRecipientIdentifier(number
, m
.getSelfNumber()));
370 return name
== null ?
"" : name
;
374 public void setContactName(final String number
, final String name
) {
376 m
.setContactName(getSingleRecipientIdentifier(number
, m
.getSelfNumber()), name
);
377 } catch (NotMasterDeviceException e
) {
378 throw new Error
.Failure("This command doesn't work on linked devices.");
379 } catch (UnregisteredUserException e
) {
380 throw new Error
.Failure("Contact is not registered.");
385 public void setExpirationTimer(final String number
, final int expiration
) {
387 m
.setExpirationTimer(getSingleRecipientIdentifier(number
, m
.getSelfNumber()), expiration
);
388 } catch (IOException e
) {
389 throw new Error
.Failure(e
.getMessage());
394 public void setContactBlocked(final String number
, final boolean blocked
) {
396 m
.setContactBlocked(getSingleRecipientIdentifier(number
, m
.getSelfNumber()), blocked
);
397 } catch (NotMasterDeviceException e
) {
398 throw new Error
.Failure("This command doesn't work on linked devices.");
399 } catch (IOException e
) {
400 throw new Error
.Failure(e
.getMessage());
405 public void setGroupBlocked(final byte[] groupId
, final boolean blocked
) {
407 m
.setGroupBlocked(getGroupId(groupId
), blocked
);
408 } catch (GroupNotFoundException e
) {
409 throw new Error
.GroupNotFound(e
.getMessage());
410 } catch (IOException e
) {
411 throw new Error
.Failure(e
.getMessage());
416 public List
<byte[]> getGroupIds() {
417 var groups
= m
.getGroups();
418 var ids
= new ArrayList
<byte[]>(groups
.size());
419 for (var group
: groups
) {
420 ids
.add(group
.getGroupId().serialize());
426 public DBusPath
getGroup(final byte[] groupId
) {
428 final var groupOptional
= groups
.stream().filter(g
-> Arrays
.equals(g
.getId(), groupId
)).findFirst();
429 if (groupOptional
.isEmpty()) {
430 throw new Error
.GroupNotFound("Group not found");
432 return groupOptional
.get().getObjectPath();
436 public List
<StructGroup
> listGroups() {
442 public String
getGroupName(final byte[] groupId
) {
443 var group
= m
.getGroup(getGroupId(groupId
));
444 if (group
== null || group
.getTitle() == null) {
447 return group
.getTitle();
452 public List
<String
> getGroupMembers(final byte[] groupId
) {
453 var group
= m
.getGroup(getGroupId(groupId
));
457 final var members
= group
.getMembers();
458 return getRecipientStrings(members
);
463 public byte[] createGroup(
464 final String name
, final List
<String
> members
, final String avatar
465 ) throws Error
.AttachmentInvalid
, Error
.Failure
, Error
.InvalidNumber
{
466 return updateGroup(new byte[0], name
, members
, avatar
);
470 public byte[] updateGroup(byte[] groupId
, String name
, List
<String
> members
, String avatar
) {
472 groupId
= nullIfEmpty(groupId
);
473 name
= nullIfEmpty(name
);
474 avatar
= nullIfEmpty(avatar
);
475 final var memberIdentifiers
= getSingleRecipientIdentifiers(members
, m
.getSelfNumber());
476 if (groupId
== null) {
477 final var results
= m
.createGroup(name
, memberIdentifiers
, avatar
== null ?
null : new File(avatar
));
478 checkSendMessageResults(results
.second().getTimestamp(), results
.second().getResults());
479 return results
.first().serialize();
481 final var results
= m
.updateGroup(getGroupId(groupId
),
482 UpdateGroup
.newBuilder()
484 .withMembers(memberIdentifiers
)
485 .withAvatarFile(avatar
== null ?
null : new File(avatar
))
487 if (results
!= null) {
488 checkSendMessageResults(results
.getTimestamp(), results
.getResults());
492 } catch (IOException e
) {
493 throw new Error
.Failure(e
.getMessage());
494 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
495 throw new Error
.GroupNotFound(e
.getMessage());
496 } catch (AttachmentInvalidException e
) {
497 throw new Error
.AttachmentInvalid(e
.getMessage());
502 public boolean isRegistered() {
507 public boolean isRegistered(String number
) {
508 var result
= isRegistered(List
.of(number
));
509 return result
.get(0);
513 public List
<Boolean
> isRegistered(List
<String
> numbers
) {
514 var results
= new ArrayList
<Boolean
>();
515 if (numbers
.isEmpty()) {
519 Map
<String
, Pair
<String
, UUID
>> registered
;
521 registered
= m
.areUsersRegistered(new HashSet
<>(numbers
));
522 } catch (IOException e
) {
523 throw new Error
.Failure(e
.getMessage());
526 return numbers
.stream().map(number
-> {
527 var uuid
= registered
.get(number
).second();
529 }).collect(Collectors
.toList());
533 public void updateProfile(
539 final boolean removeAvatar
542 givenName
= nullIfEmpty(givenName
);
543 familyName
= nullIfEmpty(familyName
);
544 about
= nullIfEmpty(about
);
545 aboutEmoji
= nullIfEmpty(aboutEmoji
);
546 avatarPath
= nullIfEmpty(avatarPath
);
547 Optional
<File
> avatarFile
= removeAvatar
549 : avatarPath
== null ?
null : Optional
.of(new File(avatarPath
));
550 m
.setProfile(givenName
, familyName
, about
, aboutEmoji
, avatarFile
);
551 } catch (IOException e
) {
552 throw new Error
.Failure(e
.getMessage());
557 public void updateProfile(
560 final String aboutEmoji
,
562 final boolean removeAvatar
564 updateProfile(name
, "", about
, aboutEmoji
, avatarPath
, removeAvatar
);
568 public void removePin() {
570 m
.setRegistrationLockPin(Optional
.absent());
571 } catch (UnauthenticatedResponseException e
) {
572 throw new Error
.Failure("Remove pin failed with unauthenticated response: " + e
.getMessage());
573 } catch (IOException e
) {
574 throw new Error
.Failure("Remove pin error: " + e
.getMessage());
579 public void setPin(String registrationLockPin
) {
581 m
.setRegistrationLockPin(Optional
.of(registrationLockPin
));
582 } catch (UnauthenticatedResponseException e
) {
583 throw new Error
.Failure("Set pin error failed with unauthenticated response: " + e
.getMessage());
584 } catch (IOException e
) {
585 throw new Error
.Failure("Set pin error: " + e
.getMessage());
589 // Provide option to query a version string in order to react on potential
590 // future interface changes
592 public String
version() {
593 return BaseConfig
.PROJECT_VERSION
;
596 // Create a unique list of Numbers from Identities and Contacts to really get
597 // all numbers the system knows
599 public List
<String
> listNumbers() {
600 return Stream
.concat(m
.getIdentities().stream().map(Identity
::getRecipient
),
601 m
.getContacts().stream().map(Pair
::first
))
602 .map(a
-> a
.getNumber().orElse(null))
603 .filter(Objects
::nonNull
)
605 .collect(Collectors
.toList());
609 public List
<String
> getContactNumber(final String name
) {
610 // Contact names have precedence.
611 var numbers
= new ArrayList
<String
>();
612 var contacts
= m
.getContacts();
613 for (var c
: contacts
) {
614 if (name
.equals(c
.second().getName())) {
615 numbers
.add(c
.first().getLegacyIdentifier());
618 // Try profiles if no contact name was found
619 for (var identity
: m
.getIdentities()) {
620 final var address
= identity
.getRecipient();
621 var number
= address
.getNumber().orElse(null);
622 if (number
!= null) {
623 Profile profile
= null;
625 profile
= m
.getRecipientProfile(RecipientIdentifier
.Single
.fromAddress(address
));
626 } catch (UnregisteredUserException ignored
) {
628 if (profile
!= null && profile
.getDisplayName().equals(name
)) {
637 public void quitGroup(final byte[] groupId
) {
638 var group
= getGroupId(groupId
);
640 m
.quitGroup(group
, Set
.of());
641 } catch (GroupNotFoundException
| NotAGroupMemberException e
) {
642 throw new Error
.GroupNotFound(e
.getMessage());
643 } catch (IOException
| LastGroupAdminException e
) {
644 throw new Error
.Failure(e
.getMessage());
649 public byte[] joinGroup(final String groupLink
) {
651 final var linkUrl
= GroupInviteLinkUrl
.fromUri(groupLink
);
652 if (linkUrl
== null) {
653 throw new Error
.Failure("Group link is invalid:");
655 final var result
= m
.joinGroup(linkUrl
);
656 return result
.first().serialize();
657 } catch (GroupInviteLinkUrl
.InvalidGroupLinkException
| GroupLinkNotActiveException e
) {
658 throw new Error
.Failure("Group link is invalid: " + e
.getMessage());
659 } catch (GroupInviteLinkUrl
.UnknownGroupLinkVersionException e
) {
660 throw new Error
.Failure("Group link was created with an incompatible version: " + e
.getMessage());
661 } catch (IOException e
) {
662 throw new Error
.Failure(e
.getMessage());
667 public boolean isContactBlocked(final String number
) {
668 return m
.isContactBlocked(getSingleRecipientIdentifier(number
, m
.getSelfNumber()));
672 public boolean isGroupBlocked(final byte[] groupId
) {
673 var group
= m
.getGroup(getGroupId(groupId
));
677 return group
.isBlocked();
682 public boolean isMember(final byte[] groupId
) {
683 var group
= m
.getGroup(getGroupId(groupId
));
687 return group
.isMember();
692 public String
uploadStickerPack(String stickerPackPath
) {
693 File path
= new File(stickerPackPath
);
695 return m
.uploadStickerPack(path
).toString();
696 } catch (IOException e
) {
697 throw new Error
.Failure("Upload error (maybe image size is too large):" + e
.getMessage());
698 } catch (StickerPackInvalidException e
) {
699 throw new Error
.Failure("Invalid sticker pack: " + e
.getMessage());
703 private static void checkSendMessageResult(long timestamp
, SendMessageResult result
) throws DBusExecutionException
{
704 var error
= ErrorUtils
.getErrorMessageFromSendMessageResult(result
);
710 final var message
= timestamp
+ "\nFailed to send message:\n" + error
+ '\n';
712 if (result
.getIdentityFailure() != null) {
713 throw new Error
.UntrustedIdentity(message
);
715 throw new Error
.Failure(message
);
719 private static void checkSendMessageResults(
720 long timestamp
, Map
<RecipientIdentifier
, List
<SendMessageResult
>> results
721 ) throws DBusExecutionException
{
722 final var sendMessageResults
= results
.values().stream().findFirst();
723 if (results
.size() == 1 && sendMessageResults
.get().size() == 1) {
724 checkSendMessageResult(timestamp
, sendMessageResults
.get().stream().findFirst().get());
728 var errors
= ErrorUtils
.getErrorMessagesFromSendMessageResults(results
);
729 if (errors
.size() == 0) {
733 var message
= new StringBuilder();
734 message
.append(timestamp
).append('\n');
735 message
.append("Failed to send (some) messages:\n");
736 for (var error
: errors
) {
737 message
.append(error
).append('\n');
740 throw new Error
.Failure(message
.toString());
743 private static void checkSendMessageResults(
744 long timestamp
, Collection
<SendMessageResult
> results
745 ) throws DBusExecutionException
{
746 if (results
.size() == 1) {
747 checkSendMessageResult(timestamp
, results
.stream().findFirst().get());
751 var errors
= ErrorUtils
.getErrorMessagesFromSendMessageResults(results
);
752 if (errors
.size() == 0) {
756 var message
= new StringBuilder();
757 message
.append(timestamp
).append('\n');
758 message
.append("Failed to send (some) messages:\n");
759 for (var error
: errors
) {
760 message
.append(error
).append('\n');
763 throw new Error
.Failure(message
.toString());
766 private static List
<String
> getRecipientStrings(final Set
<RecipientAddress
> members
) {
767 return members
.stream().map(RecipientAddress
::getLegacyIdentifier
).collect(Collectors
.toList());
770 private static Set
<RecipientIdentifier
.Single
> getSingleRecipientIdentifiers(
771 final Collection
<String
> recipientStrings
, final String localNumber
772 ) throws DBusExecutionException
{
773 final var identifiers
= new HashSet
<RecipientIdentifier
.Single
>();
774 for (var recipientString
: recipientStrings
) {
775 identifiers
.add(getSingleRecipientIdentifier(recipientString
, localNumber
));
780 private static RecipientIdentifier
.Single
getSingleRecipientIdentifier(
781 final String recipientString
, final String localNumber
782 ) throws DBusExecutionException
{
784 return RecipientIdentifier
.Single
.fromString(recipientString
, localNumber
);
785 } catch (InvalidNumberException e
) {
786 throw new Error
.InvalidNumber(e
.getMessage());
790 private static GroupId
getGroupId(byte[] groupId
) throws DBusExecutionException
{
792 return GroupId
.unknownVersion(groupId
);
793 } catch (Throwable e
) {
794 throw new Error
.InvalidGroupId("Invalid group id: " + e
.getMessage());
798 private byte[] nullIfEmpty(final byte[] array
) {
799 return array
.length
== 0 ?
null : array
;
802 private String
nullIfEmpty(final String name
) {
803 return name
.isEmpty() ?
null : name
;
806 private String
emptyIfNull(final String string
) {
807 return string
== null ?
"" : string
;
810 private static String
getDeviceObjectPath(String basePath
, long deviceId
) {
811 return basePath
+ "/Devices/" + deviceId
;
814 private void updateDevices() {
815 List
<org
.asamk
.signal
.manager
.api
.Device
> linkedDevices
;
817 linkedDevices
= m
.getLinkedDevices();
818 } catch (IOException e
) {
819 throw new Error
.Failure("Failed to get linked devices: " + e
.getMessage());
824 linkedDevices
.forEach(d
-> {
825 final var object
= new DbusSignalDeviceImpl(d
);
826 final var deviceObjectPath
= object
.getObjectPath();
828 connection
.exportObject(object
);
829 } catch (DBusException e
) {
832 if (d
.isThisDevice()) {
833 thisDevice
= new DBusPath(deviceObjectPath
);
835 this.devices
.add(new StructDevice(new DBusPath(deviceObjectPath
), d
.getId(), emptyIfNull(d
.getName())));
839 private void unExportDevices() {
840 this.devices
.stream()
841 .map(StructDevice
::getObjectPath
)
842 .map(DBusPath
::getPath
)
843 .forEach(connection
::unExportObject
);
844 this.devices
.clear();
847 private static String
getGroupObjectPath(String basePath
, byte[] groupId
) {
848 return basePath
+ "/Groups/" + Base64
.getEncoder()
849 .encodeToString(groupId
)
855 private void updateGroups() {
856 List
<org
.asamk
.signal
.manager
.api
.Group
> groups
;
857 groups
= m
.getGroups();
861 groups
.forEach(g
-> {
862 final var object
= new DbusSignalGroupImpl(g
.getGroupId());
864 connection
.exportObject(object
);
865 } catch (DBusException e
) {
868 this.groups
.add(new StructGroup(new DBusPath(object
.getObjectPath()),
869 g
.getGroupId().serialize(),
870 emptyIfNull(g
.getTitle())));
874 private void unExportGroups() {
875 this.groups
.stream().map(StructGroup
::getObjectPath
).map(DBusPath
::getPath
).forEach(connection
::unExportObject
);
879 public class DbusSignalDeviceImpl
extends DbusProperties
implements Signal
.Device
{
881 private final org
.asamk
.signal
.manager
.api
.Device device
;
883 public DbusSignalDeviceImpl(final org
.asamk
.signal
.manager
.api
.Device device
) {
884 super.addPropertiesHandler(new DbusInterfacePropertiesHandler("org.asamk.Signal.Device",
885 List
.of(new DbusProperty
<>("Id", device
::getId
),
886 new DbusProperty
<>("Name", () -> emptyIfNull(device
.getName()), this::setDeviceName
),
887 new DbusProperty
<>("Created", device
::getCreated
),
888 new DbusProperty
<>("LastSeen", device
::getLastSeen
))));
889 this.device
= device
;
893 public String
getObjectPath() {
894 return getDeviceObjectPath(objectPath
, device
.getId());
898 public void removeDevice() throws Error
.Failure
{
900 m
.removeLinkedDevices(device
.getId());
902 } catch (IOException e
) {
903 throw new Error
.Failure(e
.getMessage());
907 private void setDeviceName(String name
) {
908 if (!device
.isThisDevice()) {
909 throw new Error
.Failure("Only the name of this device can be changed");
912 m
.updateAccountAttributes(name
);
913 // update device list
915 } catch (IOException e
) {
916 throw new Error
.Failure(e
.getMessage());
921 public class DbusSignalGroupImpl
extends DbusProperties
implements Signal
.Group
{
923 private final GroupId groupId
;
925 public DbusSignalGroupImpl(final GroupId groupId
) {
926 this.groupId
= groupId
;
927 super.addPropertiesHandler(new DbusInterfacePropertiesHandler("org.asamk.Signal.Group",
928 List
.of(new DbusProperty
<>("Id", groupId
::serialize
),
929 new DbusProperty
<>("Name", () -> emptyIfNull(getGroup().getTitle()), this::setGroupName
),
930 new DbusProperty
<>("Description",
931 () -> emptyIfNull(getGroup().getDescription()),
932 this::setGroupDescription
),
933 new DbusProperty
<>("Avatar", this::setGroupAvatar
),
934 new DbusProperty
<>("IsBlocked", () -> getGroup().isBlocked(), this::setIsBlocked
),
935 new DbusProperty
<>("IsMember", () -> getGroup().isMember()),
936 new DbusProperty
<>("IsAdmin", () -> getGroup().isAdmin()),
937 new DbusProperty
<>("MessageExpirationTimer",
938 () -> getGroup().getMessageExpirationTimer(),
939 this::setMessageExpirationTime
),
940 new DbusProperty
<>("Members",
941 () -> new Variant
<>(getRecipientStrings(getGroup().getMembers()), "as")),
942 new DbusProperty
<>("PendingMembers",
943 () -> new Variant
<>(getRecipientStrings(getGroup().getPendingMembers()), "as")),
944 new DbusProperty
<>("RequestingMembers",
945 () -> new Variant
<>(getRecipientStrings(getGroup().getRequestingMembers()), "as")),
946 new DbusProperty
<>("Admins",
947 () -> new Variant
<>(getRecipientStrings(getGroup().getAdminMembers()), "as")),
948 new DbusProperty
<>("PermissionAddMember",
949 () -> getGroup().getPermissionAddMember().name(),
950 this::setGroupPermissionAddMember
),
951 new DbusProperty
<>("PermissionEditDetails",
952 () -> getGroup().getPermissionEditDetails().name(),
953 this::setGroupPermissionEditDetails
),
954 new DbusProperty
<>("PermissionSendMessage",
955 () -> getGroup().getPermissionSendMessage().name(),
956 this::setGroupPermissionSendMessage
),
957 new DbusProperty
<>("GroupInviteLink", () -> {
958 final var groupInviteLinkUrl
= getGroup().getGroupInviteLinkUrl();
959 return groupInviteLinkUrl
== null ?
"" : groupInviteLinkUrl
.getUrl();
964 public String
getObjectPath() {
965 return getGroupObjectPath(objectPath
, groupId
.serialize());
969 public void quitGroup() throws Error
.Failure
{
971 m
.quitGroup(groupId
, Set
.of());
972 } catch (GroupNotFoundException
| NotAGroupMemberException e
) {
973 throw new Error
.GroupNotFound(e
.getMessage());
974 } catch (IOException e
) {
975 throw new Error
.Failure(e
.getMessage());
976 } catch (LastGroupAdminException e
) {
977 throw new Error
.LastGroupAdmin(e
.getMessage());
982 public void addMembers(final List
<String
> recipients
) throws Error
.Failure
{
983 final var memberIdentifiers
= getSingleRecipientIdentifiers(recipients
, m
.getSelfNumber());
984 updateGroup(UpdateGroup
.newBuilder().withMembers(memberIdentifiers
).build());
988 public void removeMembers(final List
<String
> recipients
) throws Error
.Failure
{
989 final var memberIdentifiers
= getSingleRecipientIdentifiers(recipients
, m
.getSelfNumber());
990 updateGroup(UpdateGroup
.newBuilder().withRemoveMembers(memberIdentifiers
).build());
994 public void addAdmins(final List
<String
> recipients
) throws Error
.Failure
{
995 final var memberIdentifiers
= getSingleRecipientIdentifiers(recipients
, m
.getSelfNumber());
996 updateGroup(UpdateGroup
.newBuilder().withAdmins(memberIdentifiers
).build());
1000 public void removeAdmins(final List
<String
> recipients
) throws Error
.Failure
{
1001 final var memberIdentifiers
= getSingleRecipientIdentifiers(recipients
, m
.getSelfNumber());
1002 updateGroup(UpdateGroup
.newBuilder().withRemoveAdmins(memberIdentifiers
).build());
1006 public void resetLink() throws Error
.Failure
{
1007 updateGroup(UpdateGroup
.newBuilder().withResetGroupLink(true).build());
1011 public void disableLink() throws Error
.Failure
{
1012 updateGroup(UpdateGroup
.newBuilder().withGroupLinkState(GroupLinkState
.DISABLED
).build());
1016 public void enableLink(final boolean requiresApproval
) throws Error
.Failure
{
1017 updateGroup(UpdateGroup
.newBuilder()
1018 .withGroupLinkState(requiresApproval
1019 ? GroupLinkState
.ENABLED_WITH_APPROVAL
1020 : GroupLinkState
.ENABLED
)
1024 private org
.asamk
.signal
.manager
.api
.Group
getGroup() {
1025 return m
.getGroup(groupId
);
1028 private void setGroupName(final String name
) {
1029 updateGroup(UpdateGroup
.newBuilder().withName(name
).build());
1032 private void setGroupDescription(final String description
) {
1033 updateGroup(UpdateGroup
.newBuilder().withDescription(description
).build());
1036 private void setGroupAvatar(final String avatar
) {
1037 updateGroup(UpdateGroup
.newBuilder().withAvatarFile(new File(avatar
)).build());
1040 private void setMessageExpirationTime(final int expirationTime
) {
1041 updateGroup(UpdateGroup
.newBuilder().withExpirationTimer(expirationTime
).build());
1044 private void setGroupPermissionAddMember(final String permission
) {
1045 updateGroup(UpdateGroup
.newBuilder().withAddMemberPermission(GroupPermission
.valueOf(permission
)).build());
1048 private void setGroupPermissionEditDetails(final String permission
) {
1049 updateGroup(UpdateGroup
.newBuilder()
1050 .withEditDetailsPermission(GroupPermission
.valueOf(permission
))
1054 private void setGroupPermissionSendMessage(final String permission
) {
1055 updateGroup(UpdateGroup
.newBuilder()
1056 .withIsAnnouncementGroup(GroupPermission
.valueOf(permission
) == GroupPermission
.ONLY_ADMINS
)
1060 private void setIsBlocked(final boolean isBlocked
) {
1062 m
.setGroupBlocked(groupId
, isBlocked
);
1063 } catch (GroupNotFoundException e
) {
1064 throw new Error
.GroupNotFound(e
.getMessage());
1065 } catch (IOException e
) {
1066 throw new Error
.Failure(e
.getMessage());
1070 private void updateGroup(final UpdateGroup updateGroup
) {
1072 m
.updateGroup(groupId
, updateGroup
);
1073 } catch (IOException e
) {
1074 throw new Error
.Failure(e
.getMessage());
1075 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
1076 throw new Error
.GroupNotFound(e
.getMessage());
1077 } catch (AttachmentInvalidException e
) {
1078 throw new Error
.AttachmentInvalid(e
.getMessage());