1 package org
.asamk
.signal
.dbus
;
3 import org
.asamk
.Signal
;
4 import org
.asamk
.Signal
.Error
;
5 import org
.asamk
.signal
.BaseConfig
;
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
.groups
.GroupId
;
16 import org
.asamk
.signal
.manager
.groups
.GroupInviteLinkUrl
;
17 import org
.asamk
.signal
.manager
.groups
.GroupNotFoundException
;
18 import org
.asamk
.signal
.manager
.groups
.GroupSendingNotAllowedException
;
19 import org
.asamk
.signal
.manager
.groups
.LastGroupAdminException
;
20 import org
.asamk
.signal
.manager
.groups
.NotAGroupMemberException
;
21 import org
.asamk
.signal
.manager
.storage
.recipients
.Profile
;
22 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientAddress
;
23 import org
.asamk
.signal
.util
.ErrorUtils
;
24 import org
.freedesktop
.dbus
.DBusPath
;
25 import org
.freedesktop
.dbus
.connections
.impl
.DBusConnection
;
26 import org
.freedesktop
.dbus
.exceptions
.DBusException
;
27 import org
.freedesktop
.dbus
.exceptions
.DBusExecutionException
;
28 import org
.whispersystems
.libsignal
.InvalidKeyException
;
29 import org
.whispersystems
.libsignal
.util
.Pair
;
30 import org
.whispersystems
.libsignal
.util
.guava
.Optional
;
31 import org
.whispersystems
.signalservice
.api
.groupsv2
.GroupLinkNotActiveException
;
32 import org
.whispersystems
.signalservice
.api
.messages
.SendMessageResult
;
33 import org
.whispersystems
.signalservice
.api
.push
.exceptions
.UnregisteredUserException
;
34 import org
.whispersystems
.signalservice
.api
.util
.InvalidNumberException
;
35 import org
.whispersystems
.signalservice
.internal
.contacts
.crypto
.UnauthenticatedResponseException
;
38 import java
.io
.IOException
;
40 import java
.net
.URISyntaxException
;
41 import java
.util
.ArrayList
;
42 import java
.util
.Collection
;
43 import java
.util
.HashSet
;
44 import java
.util
.List
;
46 import java
.util
.Objects
;
48 import java
.util
.UUID
;
49 import java
.util
.stream
.Collectors
;
50 import java
.util
.stream
.Stream
;
52 public class DbusSignalImpl
implements Signal
{
54 private final Manager m
;
55 private final DBusConnection connection
;
56 private final String objectPath
;
58 private DBusPath thisDevice
;
59 private final List
<DBusPath
> devices
= new ArrayList
<>();
61 public DbusSignalImpl(final Manager m
, DBusConnection connection
, final String objectPath
) {
63 this.connection
= connection
;
64 this.objectPath
= objectPath
;
67 public void initObjects() {
76 public String
getObjectPath() {
81 public String
getSelfNumber() {
82 return m
.getSelfNumber();
86 public void addDevice(String uri
) {
88 m
.addDeviceLink(new URI(uri
));
89 } catch (IOException
| InvalidKeyException e
) {
90 throw new Error
.Failure(e
.getClass().getSimpleName() + " Add device link failed. " + e
.getMessage());
91 } catch (URISyntaxException e
) {
92 throw new Error
.InvalidUri(e
.getClass().getSimpleName()
93 + " Device link uri has invalid format: "
99 public DBusPath
getDevice(long deviceId
) {
101 return new DBusPath(getDeviceObjectPath(objectPath
, deviceId
));
105 public List
<DBusPath
> listDevices() {
110 private void updateDevices() {
111 List
<org
.asamk
.signal
.manager
.api
.Device
> linkedDevices
;
113 linkedDevices
= m
.getLinkedDevices();
114 } catch (IOException
| Error
.Failure e
) {
115 throw new Error
.Failure("Failed to get linked devices: " + e
.getMessage());
120 linkedDevices
.forEach(d
-> {
121 final var object
= new DbusSignalDeviceImpl(d
);
122 final var deviceObjectPath
= object
.getObjectPath();
124 connection
.exportObject(object
);
125 } catch (DBusException e
) {
128 if (d
.isThisDevice()) {
129 thisDevice
= new DBusPath(deviceObjectPath
);
131 this.devices
.add(new DBusPath(deviceObjectPath
));
135 private void unExportDevices() {
136 this.devices
.stream().map(DBusPath
::getPath
).forEach(connection
::unExportObject
);
137 this.devices
.clear();
141 public DBusPath
getThisDevice() {
147 public long sendMessage(final String message
, final List
<String
> attachments
, final String recipient
) {
148 var recipients
= new ArrayList
<String
>(1);
149 recipients
.add(recipient
);
150 return sendMessage(message
, attachments
, recipients
);
154 public long sendMessage(final String message
, final List
<String
> attachments
, final List
<String
> recipients
) {
156 final var results
= m
.sendMessage(new Message(message
, attachments
),
157 getSingleRecipientIdentifiers(recipients
, m
.getSelfNumber()).stream()
158 .map(RecipientIdentifier
.class::cast
)
159 .collect(Collectors
.toSet()));
161 checkSendMessageResults(results
.getTimestamp(), results
.getResults());
162 return results
.getTimestamp();
163 } catch (AttachmentInvalidException e
) {
164 throw new Error
.AttachmentInvalid(e
.getMessage());
165 } catch (IOException e
) {
166 throw new Error
.Failure(e
.getMessage());
167 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
168 throw new Error
.GroupNotFound(e
.getMessage());
173 public long sendRemoteDeleteMessage(
174 final long targetSentTimestamp
, final String recipient
176 var recipients
= new ArrayList
<String
>(1);
177 recipients
.add(recipient
);
178 return sendRemoteDeleteMessage(targetSentTimestamp
, recipients
);
182 public long sendRemoteDeleteMessage(
183 final long targetSentTimestamp
, final List
<String
> recipients
186 final var results
= m
.sendRemoteDeleteMessage(targetSentTimestamp
,
187 getSingleRecipientIdentifiers(recipients
, m
.getSelfNumber()).stream()
188 .map(RecipientIdentifier
.class::cast
)
189 .collect(Collectors
.toSet()));
190 checkSendMessageResults(results
.getTimestamp(), results
.getResults());
191 return results
.getTimestamp();
192 } catch (IOException e
) {
193 throw new Error
.Failure(e
.getMessage());
194 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
195 throw new Error
.GroupNotFound(e
.getMessage());
200 public long sendGroupRemoteDeleteMessage(
201 final long targetSentTimestamp
, final byte[] groupId
204 final var results
= m
.sendRemoteDeleteMessage(targetSentTimestamp
,
205 Set
.of(new RecipientIdentifier
.Group(getGroupId(groupId
))));
206 checkSendMessageResults(results
.getTimestamp(), results
.getResults());
207 return results
.getTimestamp();
208 } catch (IOException e
) {
209 throw new Error
.Failure(e
.getMessage());
210 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
211 throw new Error
.GroupNotFound(e
.getMessage());
216 public long sendMessageReaction(
218 final boolean remove
,
219 final String targetAuthor
,
220 final long targetSentTimestamp
,
221 final String recipient
223 var recipients
= new ArrayList
<String
>(1);
224 recipients
.add(recipient
);
225 return sendMessageReaction(emoji
, remove
, targetAuthor
, targetSentTimestamp
, recipients
);
229 public long sendMessageReaction(
231 final boolean remove
,
232 final String targetAuthor
,
233 final long targetSentTimestamp
,
234 final List
<String
> recipients
237 final var results
= m
.sendMessageReaction(emoji
,
239 getSingleRecipientIdentifier(targetAuthor
, m
.getSelfNumber()),
241 getSingleRecipientIdentifiers(recipients
, m
.getSelfNumber()).stream()
242 .map(RecipientIdentifier
.class::cast
)
243 .collect(Collectors
.toSet()));
244 checkSendMessageResults(results
.getTimestamp(), results
.getResults());
245 return results
.getTimestamp();
246 } catch (IOException e
) {
247 throw new Error
.Failure(e
.getMessage());
248 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
249 throw new Error
.GroupNotFound(e
.getMessage());
254 public void sendTyping(
255 final String recipient
, final boolean stop
256 ) throws Error
.Failure
, Error
.GroupNotFound
, Error
.UntrustedIdentity
{
258 var recipients
= new ArrayList
<String
>(1);
259 recipients
.add(recipient
);
260 m
.sendTypingMessage(stop ? TypingAction
.STOP
: TypingAction
.START
,
261 getSingleRecipientIdentifiers(recipients
, m
.getSelfNumber()).stream()
262 .map(RecipientIdentifier
.class::cast
)
263 .collect(Collectors
.toSet()));
264 } catch (IOException e
) {
265 throw new Error
.Failure(e
.getMessage());
266 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
267 throw new Error
.GroupNotFound(e
.getMessage());
268 } catch (UntrustedIdentityException e
) {
269 throw new Error
.UntrustedIdentity(e
.getMessage());
274 public void sendReadReceipt(
275 final String recipient
, final List
<Long
> messageIds
276 ) throws Error
.Failure
, Error
.UntrustedIdentity
{
278 m
.sendReadReceipt(getSingleRecipientIdentifier(recipient
, m
.getSelfNumber()), messageIds
);
279 } catch (IOException e
) {
280 throw new Error
.Failure(e
.getMessage());
281 } catch (UntrustedIdentityException e
) {
282 throw new Error
.UntrustedIdentity(e
.getMessage());
287 public void sendContacts() {
290 } catch (IOException e
) {
291 throw new Error
.Failure("SendContacts error: " + e
.getMessage());
296 public void sendSyncRequest() {
298 m
.requestAllSyncData();
299 } catch (IOException e
) {
300 throw new Error
.Failure("Request sync data error: " + e
.getMessage());
305 public long sendNoteToSelfMessage(
306 final String message
, final List
<String
> attachments
307 ) throws Error
.AttachmentInvalid
, Error
.Failure
, Error
.UntrustedIdentity
{
309 final var results
= m
.sendMessage(new Message(message
, attachments
),
310 Set
.of(RecipientIdentifier
.NoteToSelf
.INSTANCE
));
311 checkSendMessageResults(results
.getTimestamp(), results
.getResults());
312 return results
.getTimestamp();
313 } catch (AttachmentInvalidException e
) {
314 throw new Error
.AttachmentInvalid(e
.getMessage());
315 } catch (IOException e
) {
316 throw new Error
.Failure(e
.getMessage());
317 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
318 throw new Error
.GroupNotFound(e
.getMessage());
323 public void sendEndSessionMessage(final List
<String
> recipients
) {
325 final var results
= m
.sendEndSessionMessage(getSingleRecipientIdentifiers(recipients
, m
.getSelfNumber()));
326 checkSendMessageResults(results
.getTimestamp(), results
.getResults());
327 } catch (IOException e
) {
328 throw new Error
.Failure(e
.getMessage());
333 public long sendGroupMessage(final String message
, final List
<String
> attachments
, final byte[] groupId
) {
335 var results
= m
.sendMessage(new Message(message
, attachments
),
336 Set
.of(new RecipientIdentifier
.Group(getGroupId(groupId
))));
337 checkSendMessageResults(results
.getTimestamp(), results
.getResults());
338 return results
.getTimestamp();
339 } catch (IOException e
) {
340 throw new Error
.Failure(e
.getMessage());
341 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
342 throw new Error
.GroupNotFound(e
.getMessage());
343 } catch (AttachmentInvalidException e
) {
344 throw new Error
.AttachmentInvalid(e
.getMessage());
349 public long sendGroupMessageReaction(
351 final boolean remove
,
352 final String targetAuthor
,
353 final long targetSentTimestamp
,
357 final var results
= m
.sendMessageReaction(emoji
,
359 getSingleRecipientIdentifier(targetAuthor
, m
.getSelfNumber()),
361 Set
.of(new RecipientIdentifier
.Group(getGroupId(groupId
))));
362 checkSendMessageResults(results
.getTimestamp(), results
.getResults());
363 return results
.getTimestamp();
364 } catch (IOException e
) {
365 throw new Error
.Failure(e
.getMessage());
366 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
367 throw new Error
.GroupNotFound(e
.getMessage());
371 // Since contact names might be empty if not defined, also potentially return
374 public String
getContactName(final String number
) {
375 final var name
= m
.getContactOrProfileName(getSingleRecipientIdentifier(number
, m
.getSelfNumber()));
376 return name
== null ?
"" : name
;
380 public void setContactName(final String number
, final String name
) {
382 m
.setContactName(getSingleRecipientIdentifier(number
, m
.getSelfNumber()), name
);
383 } catch (NotMasterDeviceException e
) {
384 throw new Error
.Failure("This command doesn't work on linked devices.");
385 } catch (UnregisteredUserException e
) {
386 throw new Error
.Failure("Contact is not registered.");
391 public void setExpirationTimer(final String number
, final int expiration
) {
393 m
.setExpirationTimer(getSingleRecipientIdentifier(number
, m
.getSelfNumber()), expiration
);
394 } catch (IOException e
) {
395 throw new Error
.Failure(e
.getMessage());
400 public void setContactBlocked(final String number
, final boolean blocked
) {
402 m
.setContactBlocked(getSingleRecipientIdentifier(number
, m
.getSelfNumber()), blocked
);
403 } catch (NotMasterDeviceException e
) {
404 throw new Error
.Failure("This command doesn't work on linked devices.");
405 } catch (IOException e
) {
406 throw new Error
.Failure(e
.getMessage());
411 public void setGroupBlocked(final byte[] groupId
, final boolean blocked
) {
413 m
.setGroupBlocked(getGroupId(groupId
), blocked
);
414 } catch (GroupNotFoundException e
) {
415 throw new Error
.GroupNotFound(e
.getMessage());
416 } catch (IOException e
) {
417 throw new Error
.Failure(e
.getMessage());
422 public List
<byte[]> getGroupIds() {
423 var groups
= m
.getGroups();
424 var ids
= new ArrayList
<byte[]>(groups
.size());
425 for (var group
: groups
) {
426 ids
.add(group
.getGroupId().serialize());
432 public String
getGroupName(final byte[] groupId
) {
433 var group
= m
.getGroup(getGroupId(groupId
));
434 if (group
== null || group
.getTitle() == null) {
437 return group
.getTitle();
442 public List
<String
> getGroupMembers(final byte[] groupId
) {
443 var group
= m
.getGroup(getGroupId(groupId
));
447 return group
.getMembers().stream().map(RecipientAddress
::getLegacyIdentifier
).collect(Collectors
.toList());
452 public byte[] updateGroup(byte[] groupId
, String name
, List
<String
> members
, String avatar
) {
454 groupId
= nullIfEmpty(groupId
);
455 name
= nullIfEmpty(name
);
456 avatar
= nullIfEmpty(avatar
);
457 final var memberIdentifiers
= getSingleRecipientIdentifiers(members
, m
.getSelfNumber());
458 if (groupId
== null) {
459 final var results
= m
.createGroup(name
, memberIdentifiers
, avatar
== null ?
null : new File(avatar
));
460 checkSendMessageResults(results
.second().getTimestamp(), results
.second().getResults());
461 return results
.first().serialize();
463 final var results
= m
.updateGroup(getGroupId(groupId
),
474 avatar
== null ?
null : new File(avatar
),
477 if (results
!= null) {
478 checkSendMessageResults(results
.getTimestamp(), results
.getResults());
482 } catch (IOException e
) {
483 throw new Error
.Failure(e
.getMessage());
484 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
485 throw new Error
.GroupNotFound(e
.getMessage());
486 } catch (AttachmentInvalidException e
) {
487 throw new Error
.AttachmentInvalid(e
.getMessage());
492 public boolean isRegistered() {
497 public boolean isRegistered(String number
) {
498 var result
= isRegistered(List
.of(number
));
499 return result
.get(0);
503 public List
<Boolean
> isRegistered(List
<String
> numbers
) {
504 var results
= new ArrayList
<Boolean
>();
505 if (numbers
.isEmpty()) {
509 Map
<String
, Pair
<String
, UUID
>> registered
;
511 registered
= m
.areUsersRegistered(new HashSet
<>(numbers
));
512 } catch (IOException e
) {
513 throw new Error
.Failure(e
.getMessage());
516 return numbers
.stream().map(number
-> {
517 var uuid
= registered
.get(number
).second();
519 }).collect(Collectors
.toList());
523 public void updateProfile(
529 final boolean removeAvatar
532 givenName
= nullIfEmpty(givenName
);
533 familyName
= nullIfEmpty(familyName
);
534 about
= nullIfEmpty(about
);
535 aboutEmoji
= nullIfEmpty(aboutEmoji
);
536 avatarPath
= nullIfEmpty(avatarPath
);
537 Optional
<File
> avatarFile
= removeAvatar
539 : avatarPath
== null ?
null : Optional
.of(new File(avatarPath
));
540 m
.setProfile(givenName
, familyName
, about
, aboutEmoji
, avatarFile
);
541 } catch (IOException e
) {
542 throw new Error
.Failure(e
.getMessage());
547 public void updateProfile(
550 final String aboutEmoji
,
552 final boolean removeAvatar
554 updateProfile(name
, "", about
, aboutEmoji
, avatarPath
, removeAvatar
);
558 public void removePin() {
560 m
.setRegistrationLockPin(Optional
.absent());
561 } catch (UnauthenticatedResponseException e
) {
562 throw new Error
.Failure("Remove pin failed with unauthenticated response: " + e
.getMessage());
563 } catch (IOException e
) {
564 throw new Error
.Failure("Remove pin error: " + e
.getMessage());
569 public void setPin(String registrationLockPin
) {
571 m
.setRegistrationLockPin(Optional
.of(registrationLockPin
));
572 } catch (UnauthenticatedResponseException e
) {
573 throw new Error
.Failure("Set pin error failed with unauthenticated response: " + e
.getMessage());
574 } catch (IOException e
) {
575 throw new Error
.Failure("Set pin error: " + e
.getMessage());
579 // Provide option to query a version string in order to react on potential
580 // future interface changes
582 public String
version() {
583 return BaseConfig
.PROJECT_VERSION
;
586 // Create a unique list of Numbers from Identities and Contacts to really get
587 // all numbers the system knows
589 public List
<String
> listNumbers() {
590 return Stream
.concat(m
.getIdentities().stream().map(Identity
::getRecipient
),
591 m
.getContacts().stream().map(Pair
::first
))
592 .map(a
-> a
.getNumber().orElse(null))
593 .filter(Objects
::nonNull
)
595 .collect(Collectors
.toList());
599 public List
<String
> getContactNumber(final String name
) {
600 // Contact names have precedence.
601 var numbers
= new ArrayList
<String
>();
602 var contacts
= m
.getContacts();
603 for (var c
: contacts
) {
604 if (name
.equals(c
.second().getName())) {
605 numbers
.add(c
.first().getLegacyIdentifier());
608 // Try profiles if no contact name was found
609 for (var identity
: m
.getIdentities()) {
610 final var address
= identity
.getRecipient();
611 var number
= address
.getNumber().orElse(null);
612 if (number
!= null) {
613 Profile profile
= null;
615 profile
= m
.getRecipientProfile(RecipientIdentifier
.Single
.fromAddress(address
));
616 } catch (UnregisteredUserException ignored
) {
618 if (profile
!= null && profile
.getDisplayName().equals(name
)) {
627 public void quitGroup(final byte[] groupId
) {
628 var group
= getGroupId(groupId
);
630 m
.quitGroup(group
, Set
.of());
631 } catch (GroupNotFoundException
| NotAGroupMemberException e
) {
632 throw new Error
.GroupNotFound(e
.getMessage());
633 } catch (IOException
| LastGroupAdminException e
) {
634 throw new Error
.Failure(e
.getMessage());
639 public byte[] joinGroup(final String groupLink
) {
641 final var linkUrl
= GroupInviteLinkUrl
.fromUri(groupLink
);
642 if (linkUrl
== null) {
643 throw new Error
.Failure("Group link is invalid:");
645 final var result
= m
.joinGroup(linkUrl
);
646 return result
.first().serialize();
647 } catch (GroupInviteLinkUrl
.InvalidGroupLinkException
| GroupLinkNotActiveException e
) {
648 throw new Error
.Failure("Group link is invalid: " + e
.getMessage());
649 } catch (GroupInviteLinkUrl
.UnknownGroupLinkVersionException e
) {
650 throw new Error
.Failure("Group link was created with an incompatible version: " + e
.getMessage());
651 } catch (IOException e
) {
652 throw new Error
.Failure(e
.getMessage());
657 public boolean isContactBlocked(final String number
) {
658 return m
.isContactBlocked(getSingleRecipientIdentifier(number
, m
.getSelfNumber()));
662 public boolean isGroupBlocked(final byte[] groupId
) {
663 var group
= m
.getGroup(getGroupId(groupId
));
667 return group
.isBlocked();
672 public boolean isMember(final byte[] groupId
) {
673 var group
= m
.getGroup(getGroupId(groupId
));
677 return group
.isMember();
682 public String
uploadStickerPack(String stickerPackPath
) {
683 File path
= new File(stickerPackPath
);
685 return m
.uploadStickerPack(path
).toString();
686 } catch (IOException e
) {
687 throw new Error
.IOError("Upload error (maybe image size is too large):" + e
.getMessage());
688 } catch (StickerPackInvalidException e
) {
689 throw new Error
.Failure("Invalid sticker pack: " + e
.getMessage());
694 public void setConfiguration(boolean readReceipts
, boolean unidentifiedDeliveryIndicators
, boolean typingIndicators
, boolean linkPreviews
) {
696 m
.updateConfiguration(readReceipts
, unidentifiedDeliveryIndicators
, typingIndicators
, linkPreviews
);
697 } catch (IOException e
) {
698 throw new Error
.IOError("UpdateAccount error: " + e
.getMessage());
699 } catch (NotMasterDeviceException e
) {
700 throw new Error
.UserError("This command doesn't work on linked devices.");
705 public List
<Boolean
> getConfiguration() {
706 List
<Boolean
> config
= new ArrayList
<>(4);
708 config
= m
.getConfiguration();
709 } catch (IOException e
) {
710 throw new Error
.IOError("Configuration storage error: " + e
.getMessage());
711 } catch (NotMasterDeviceException e
) {
712 throw new Error
.UserError("This command doesn't work on linked devices.");
717 private static void checkSendMessageResult(long timestamp
, SendMessageResult result
) throws DBusExecutionException
{
718 var error
= ErrorUtils
.getErrorMessageFromSendMessageResult(result
);
724 final var message
= timestamp
+ "\nFailed to send message:\n" + error
+ '\n';
726 if (result
.getIdentityFailure() != null) {
727 throw new Error
.UntrustedIdentity(message
);
729 throw new Error
.Failure(message
);
733 private static void checkSendMessageResults(
734 long timestamp
, Map
<RecipientIdentifier
, List
<SendMessageResult
>> results
735 ) throws DBusExecutionException
{
736 final var sendMessageResults
= results
.values().stream().findFirst();
737 if (results
.size() == 1 && sendMessageResults
.get().size() == 1) {
738 checkSendMessageResult(timestamp
, sendMessageResults
.get().stream().findFirst().get());
742 var errors
= ErrorUtils
.getErrorMessagesFromSendMessageResults(results
);
743 if (errors
.size() == 0) {
747 var message
= new StringBuilder();
748 message
.append(timestamp
).append('\n');
749 message
.append("Failed to send (some) messages:\n");
750 for (var error
: errors
) {
751 message
.append(error
).append('\n');
754 throw new Error
.Failure(message
.toString());
757 private static void checkSendMessageResults(
758 long timestamp
, Collection
<SendMessageResult
> results
759 ) throws DBusExecutionException
{
760 if (results
.size() == 1) {
761 checkSendMessageResult(timestamp
, results
.stream().findFirst().get());
765 var errors
= ErrorUtils
.getErrorMessagesFromSendMessageResults(results
);
766 if (errors
.size() == 0) {
770 var message
= new StringBuilder();
771 message
.append(timestamp
).append('\n');
772 message
.append("Failed to send (some) messages:\n");
773 for (var error
: errors
) {
774 message
.append(error
).append('\n');
777 throw new Error
.Failure(message
.toString());
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 static String
getDeviceObjectPath(String basePath
, long deviceId
) {
817 return basePath
+ "/Devices/" + deviceId
;
820 public class DbusSignalDeviceImpl
extends DbusProperties
implements Signal
.Device
{
822 private final org
.asamk
.signal
.manager
.api
.Device device
;
824 public DbusSignalDeviceImpl(final org
.asamk
.signal
.manager
.api
.Device device
) {
826 super.addPropertiesHandler(new DbusInterfacePropertiesHandler("org.asamk.Signal.Device",
827 List
.of(new DbusProperty
<>("Id", device
::getId
),
828 new DbusProperty
<>("Name",
829 () -> device
.getName() == null ?
"" : device
.getName(),
830 this::setDeviceName
),
831 new DbusProperty
<>("Created", device
::getCreated
),
832 new DbusProperty
<>("LastSeen", device
::getLastSeen
))));
833 this.device
= device
;
837 public String
getObjectPath() {
838 return getDeviceObjectPath(objectPath
, device
.getId());
842 public void removeDevice() throws Error
.Failure
{
844 m
.removeLinkedDevices(device
.getId());
846 } catch (IOException e
) {
847 throw new Error
.Failure(e
.getMessage());
851 private void setDeviceName(String name
) {
852 if (!device
.isThisDevice()) {
853 throw new Error
.Failure("Only the name of this device can be changed");
856 m
.updateAccountAttributes(name
);
857 // update device list
859 } catch (IOException e
) {
860 throw new Error
.Failure(e
.getMessage());