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
.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
.groups
.GroupId
;
17 import org
.asamk
.signal
.manager
.groups
.GroupInviteLinkUrl
;
18 import org
.asamk
.signal
.manager
.groups
.GroupNotFoundException
;
19 import org
.asamk
.signal
.manager
.groups
.GroupSendingNotAllowedException
;
20 import org
.asamk
.signal
.manager
.groups
.LastGroupAdminException
;
21 import org
.asamk
.signal
.manager
.groups
.NotAGroupMemberException
;
22 import org
.asamk
.signal
.manager
.storage
.recipients
.Profile
;
23 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientAddress
;
24 import org
.asamk
.signal
.util
.ErrorUtils
;
25 import org
.freedesktop
.dbus
.DBusPath
;
26 import org
.freedesktop
.dbus
.connections
.impl
.DBusConnection
;
27 import org
.freedesktop
.dbus
.exceptions
.DBusException
;
28 import org
.freedesktop
.dbus
.exceptions
.DBusExecutionException
;
29 import org
.whispersystems
.libsignal
.InvalidKeyException
;
30 import org
.whispersystems
.libsignal
.util
.Pair
;
31 import org
.whispersystems
.libsignal
.util
.guava
.Optional
;
32 import org
.whispersystems
.signalservice
.api
.groupsv2
.GroupLinkNotActiveException
;
33 import org
.whispersystems
.signalservice
.api
.messages
.SendMessageResult
;
34 import org
.whispersystems
.signalservice
.api
.push
.exceptions
.UnregisteredUserException
;
35 import org
.whispersystems
.signalservice
.api
.util
.InvalidNumberException
;
36 import org
.whispersystems
.signalservice
.internal
.contacts
.crypto
.UnauthenticatedResponseException
;
39 import java
.io
.IOException
;
41 import java
.net
.URISyntaxException
;
42 import java
.util
.ArrayList
;
43 import java
.util
.Collection
;
44 import java
.util
.HashSet
;
45 import java
.util
.List
;
47 import java
.util
.Objects
;
49 import java
.util
.UUID
;
50 import java
.util
.stream
.Collectors
;
51 import java
.util
.stream
.Stream
;
53 public class DbusSignalImpl
implements Signal
{
55 private final Manager m
;
56 private final DBusConnection connection
;
57 private final String objectPath
;
59 private DBusPath thisDevice
;
60 private final List
<DBusPath
> devices
= new ArrayList
<>();
62 public DbusSignalImpl(final Manager m
, DBusConnection connection
, final String objectPath
) {
64 this.connection
= connection
;
65 this.objectPath
= objectPath
;
68 public void initObjects() {
77 public String
getObjectPath() {
82 public String
getSelfNumber() {
83 return m
.getSelfNumber();
87 public void submitRateLimitChallenge(String challenge
, String captchaString
) throws IOErrorException
{
88 final var captcha
= captchaString
== null ?
null : captchaString
.replace("signalcaptcha://", "");
91 m
.submitRateLimitRecaptchaChallenge(challenge
, captcha
);
92 } catch (IOException e
) {
93 throw new IOErrorException("Submit challenge error: " + e
.getMessage(), e
);
99 public void addDevice(String uri
) {
101 m
.addDeviceLink(new URI(uri
));
102 } catch (IOException
| InvalidKeyException e
) {
103 throw new Error
.Failure(e
.getClass().getSimpleName() + " Add device link failed. " + e
.getMessage());
104 } catch (URISyntaxException e
) {
105 throw new Error
.InvalidUri(e
.getClass().getSimpleName()
106 + " Device link uri has invalid format: "
112 public DBusPath
getDevice(long deviceId
) {
114 return new DBusPath(getDeviceObjectPath(objectPath
, deviceId
));
118 public List
<DBusPath
> listDevices() {
123 private void updateDevices() {
124 List
<org
.asamk
.signal
.manager
.api
.Device
> linkedDevices
;
126 linkedDevices
= m
.getLinkedDevices();
127 } catch (IOException
| Error
.Failure e
) {
128 throw new Error
.Failure("Failed to get linked devices: " + e
.getMessage());
133 linkedDevices
.forEach(d
-> {
134 final var object
= new DbusSignalDeviceImpl(d
);
135 final var deviceObjectPath
= object
.getObjectPath();
137 connection
.exportObject(object
);
138 } catch (DBusException e
) {
141 if (d
.isThisDevice()) {
142 thisDevice
= new DBusPath(deviceObjectPath
);
144 this.devices
.add(new DBusPath(deviceObjectPath
));
148 private void unExportDevices() {
149 this.devices
.stream().map(DBusPath
::getPath
).forEach(connection
::unExportObject
);
150 this.devices
.clear();
154 public DBusPath
getThisDevice() {
160 public long sendMessage(final String message
, final List
<String
> attachments
, final String recipient
) {
161 var recipients
= new ArrayList
<String
>(1);
162 recipients
.add(recipient
);
163 return sendMessage(message
, attachments
, recipients
);
167 public long sendMessage(final String message
, final List
<String
> attachments
, final List
<String
> recipients
) {
169 final var results
= m
.sendMessage(new Message(message
, attachments
),
170 getSingleRecipientIdentifiers(recipients
, m
.getSelfNumber()).stream()
171 .map(RecipientIdentifier
.class::cast
)
172 .collect(Collectors
.toSet()));
174 checkSendMessageResults(results
.getTimestamp(), results
.getResults());
175 return results
.getTimestamp();
176 } catch (AttachmentInvalidException e
) {
177 throw new Error
.AttachmentInvalid(e
.getMessage());
178 } catch (IOException e
) {
179 throw new Error
.Failure(e
.getMessage());
180 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
181 throw new Error
.GroupNotFound(e
.getMessage());
186 public long sendRemoteDeleteMessage(
187 final long targetSentTimestamp
, final String recipient
189 var recipients
= new ArrayList
<String
>(1);
190 recipients
.add(recipient
);
191 return sendRemoteDeleteMessage(targetSentTimestamp
, recipients
);
195 public long sendRemoteDeleteMessage(
196 final long targetSentTimestamp
, final List
<String
> recipients
199 final var results
= m
.sendRemoteDeleteMessage(targetSentTimestamp
,
200 getSingleRecipientIdentifiers(recipients
, m
.getSelfNumber()).stream()
201 .map(RecipientIdentifier
.class::cast
)
202 .collect(Collectors
.toSet()));
203 checkSendMessageResults(results
.getTimestamp(), results
.getResults());
204 return results
.getTimestamp();
205 } catch (IOException e
) {
206 throw new Error
.Failure(e
.getMessage());
207 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
208 throw new Error
.GroupNotFound(e
.getMessage());
213 public long sendGroupRemoteDeleteMessage(
214 final long targetSentTimestamp
, final byte[] groupId
217 final var results
= m
.sendRemoteDeleteMessage(targetSentTimestamp
,
218 Set
.of(new RecipientIdentifier
.Group(getGroupId(groupId
))));
219 checkSendMessageResults(results
.getTimestamp(), results
.getResults());
220 return results
.getTimestamp();
221 } catch (IOException e
) {
222 throw new Error
.Failure(e
.getMessage());
223 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
224 throw new Error
.GroupNotFound(e
.getMessage());
229 public long sendMessageReaction(
231 final boolean remove
,
232 final String targetAuthor
,
233 final long targetSentTimestamp
,
234 final String recipient
236 var recipients
= new ArrayList
<String
>(1);
237 recipients
.add(recipient
);
238 return sendMessageReaction(emoji
, remove
, targetAuthor
, targetSentTimestamp
, recipients
);
242 public long sendMessageReaction(
244 final boolean remove
,
245 final String targetAuthor
,
246 final long targetSentTimestamp
,
247 final List
<String
> recipients
250 final var results
= m
.sendMessageReaction(emoji
,
252 getSingleRecipientIdentifier(targetAuthor
, m
.getSelfNumber()),
254 getSingleRecipientIdentifiers(recipients
, m
.getSelfNumber()).stream()
255 .map(RecipientIdentifier
.class::cast
)
256 .collect(Collectors
.toSet()));
257 checkSendMessageResults(results
.getTimestamp(), results
.getResults());
258 return results
.getTimestamp();
259 } catch (IOException e
) {
260 throw new Error
.Failure(e
.getMessage());
261 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
262 throw new Error
.GroupNotFound(e
.getMessage());
267 public void sendTyping(
268 final String recipient
, final boolean stop
269 ) throws Error
.Failure
, Error
.GroupNotFound
, Error
.UntrustedIdentity
{
271 var recipients
= new ArrayList
<String
>(1);
272 recipients
.add(recipient
);
273 m
.sendTypingMessage(stop ? TypingAction
.STOP
: TypingAction
.START
,
274 getSingleRecipientIdentifiers(recipients
, m
.getSelfNumber()).stream()
275 .map(RecipientIdentifier
.class::cast
)
276 .collect(Collectors
.toSet()));
277 } catch (IOException e
) {
278 throw new Error
.Failure(e
.getMessage());
279 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
280 throw new Error
.GroupNotFound(e
.getMessage());
281 } catch (UntrustedIdentityException e
) {
282 throw new Error
.UntrustedIdentity(e
.getMessage());
287 public void sendReadReceipt(
288 final String recipient
, final List
<Long
> messageIds
289 ) throws Error
.Failure
, Error
.UntrustedIdentity
{
291 m
.sendReadReceipt(getSingleRecipientIdentifier(recipient
, m
.getSelfNumber()), messageIds
);
292 } catch (IOException e
) {
293 throw new Error
.Failure(e
.getMessage());
294 } catch (UntrustedIdentityException e
) {
295 throw new Error
.UntrustedIdentity(e
.getMessage());
300 public void sendContacts() {
303 } catch (IOException e
) {
304 throw new Error
.Failure("SendContacts error: " + e
.getMessage());
309 public void sendSyncRequest() {
311 m
.requestAllSyncData();
312 } catch (IOException e
) {
313 throw new Error
.Failure("Request sync data error: " + e
.getMessage());
318 public long sendNoteToSelfMessage(
319 final String message
, final List
<String
> attachments
320 ) throws Error
.AttachmentInvalid
, Error
.Failure
, Error
.UntrustedIdentity
{
322 final var results
= m
.sendMessage(new Message(message
, attachments
),
323 Set
.of(RecipientIdentifier
.NoteToSelf
.INSTANCE
));
324 checkSendMessageResults(results
.getTimestamp(), results
.getResults());
325 return results
.getTimestamp();
326 } catch (AttachmentInvalidException e
) {
327 throw new Error
.AttachmentInvalid(e
.getMessage());
328 } catch (IOException e
) {
329 throw new Error
.Failure(e
.getMessage());
330 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
331 throw new Error
.GroupNotFound(e
.getMessage());
336 public void sendEndSessionMessage(final List
<String
> recipients
) {
338 final var results
= m
.sendEndSessionMessage(getSingleRecipientIdentifiers(recipients
, m
.getSelfNumber()));
339 checkSendMessageResults(results
.getTimestamp(), results
.getResults());
340 } catch (IOException e
) {
341 throw new Error
.Failure(e
.getMessage());
346 public long sendGroupMessage(final String message
, final List
<String
> attachments
, final byte[] groupId
) {
348 var results
= m
.sendMessage(new Message(message
, attachments
),
349 Set
.of(new RecipientIdentifier
.Group(getGroupId(groupId
))));
350 checkSendMessageResults(results
.getTimestamp(), results
.getResults());
351 return results
.getTimestamp();
352 } catch (IOException e
) {
353 throw new Error
.Failure(e
.getMessage());
354 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
355 throw new Error
.GroupNotFound(e
.getMessage());
356 } catch (AttachmentInvalidException e
) {
357 throw new Error
.AttachmentInvalid(e
.getMessage());
362 public long sendGroupMessageReaction(
364 final boolean remove
,
365 final String targetAuthor
,
366 final long targetSentTimestamp
,
370 final var results
= m
.sendMessageReaction(emoji
,
372 getSingleRecipientIdentifier(targetAuthor
, m
.getSelfNumber()),
374 Set
.of(new RecipientIdentifier
.Group(getGroupId(groupId
))));
375 checkSendMessageResults(results
.getTimestamp(), results
.getResults());
376 return results
.getTimestamp();
377 } catch (IOException e
) {
378 throw new Error
.Failure(e
.getMessage());
379 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
380 throw new Error
.GroupNotFound(e
.getMessage());
384 // Since contact names might be empty if not defined, also potentially return
387 public String
getContactName(final String number
) {
388 final var name
= m
.getContactOrProfileName(getSingleRecipientIdentifier(number
, m
.getSelfNumber()));
389 return name
== null ?
"" : name
;
393 public void setContactName(final String number
, final String name
) {
395 m
.setContactName(getSingleRecipientIdentifier(number
, m
.getSelfNumber()), name
);
396 } catch (NotMasterDeviceException e
) {
397 throw new Error
.Failure("This command doesn't work on linked devices.");
398 } catch (UnregisteredUserException e
) {
399 throw new Error
.Failure("Contact is not registered.");
404 public void setExpirationTimer(final String number
, final int expiration
) {
406 m
.setExpirationTimer(getSingleRecipientIdentifier(number
, m
.getSelfNumber()), expiration
);
407 } catch (IOException e
) {
408 throw new Error
.Failure(e
.getMessage());
413 public void setContactBlocked(final String number
, final boolean blocked
) {
415 m
.setContactBlocked(getSingleRecipientIdentifier(number
, m
.getSelfNumber()), blocked
);
416 } catch (NotMasterDeviceException e
) {
417 throw new Error
.Failure("This command doesn't work on linked devices.");
418 } catch (IOException e
) {
419 throw new Error
.Failure(e
.getMessage());
424 public void setGroupBlocked(final byte[] groupId
, final boolean blocked
) {
426 m
.setGroupBlocked(getGroupId(groupId
), blocked
);
427 } catch (GroupNotFoundException e
) {
428 throw new Error
.GroupNotFound(e
.getMessage());
429 } catch (IOException e
) {
430 throw new Error
.Failure(e
.getMessage());
435 public List
<byte[]> getGroupIds() {
436 var groups
= m
.getGroups();
437 var ids
= new ArrayList
<byte[]>(groups
.size());
438 for (var group
: groups
) {
439 ids
.add(group
.getGroupId().serialize());
445 public String
getGroupName(final byte[] groupId
) {
446 var group
= m
.getGroup(getGroupId(groupId
));
447 if (group
== null || group
.getTitle() == null) {
450 return group
.getTitle();
455 public List
<String
> getGroupMembers(final byte[] groupId
) {
456 var group
= m
.getGroup(getGroupId(groupId
));
460 return group
.getMembers().stream().map(RecipientAddress
::getLegacyIdentifier
).collect(Collectors
.toList());
465 public byte[] updateGroup(byte[] groupId
, String name
, List
<String
> members
, String avatar
) {
467 groupId
= nullIfEmpty(groupId
);
468 name
= nullIfEmpty(name
);
469 avatar
= nullIfEmpty(avatar
);
470 final var memberIdentifiers
= getSingleRecipientIdentifiers(members
, m
.getSelfNumber());
471 if (groupId
== null) {
472 final var results
= m
.createGroup(name
, memberIdentifiers
, avatar
== null ?
null : new File(avatar
));
473 checkSendMessageResults(results
.second().getTimestamp(), results
.second().getResults());
474 return results
.first().serialize();
476 final var results
= m
.updateGroup(getGroupId(groupId
),
487 avatar
== null ?
null : new File(avatar
),
490 if (results
!= null) {
491 checkSendMessageResults(results
.getTimestamp(), results
.getResults());
495 } catch (IOException e
) {
496 throw new Error
.Failure(e
.getMessage());
497 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
498 throw new Error
.GroupNotFound(e
.getMessage());
499 } catch (AttachmentInvalidException e
) {
500 throw new Error
.AttachmentInvalid(e
.getMessage());
505 public boolean isRegistered() {
510 public boolean isRegistered(String number
) {
511 var result
= isRegistered(List
.of(number
));
512 return result
.get(0);
516 public List
<Boolean
> isRegistered(List
<String
> numbers
) {
517 var results
= new ArrayList
<Boolean
>();
518 if (numbers
.isEmpty()) {
522 Map
<String
, Pair
<String
, UUID
>> registered
;
524 registered
= m
.areUsersRegistered(new HashSet
<>(numbers
));
525 } catch (IOException e
) {
526 throw new Error
.Failure(e
.getMessage());
529 return numbers
.stream().map(number
-> {
530 var uuid
= registered
.get(number
).second();
532 }).collect(Collectors
.toList());
536 public void updateProfile(
542 final boolean removeAvatar
545 givenName
= nullIfEmpty(givenName
);
546 familyName
= nullIfEmpty(familyName
);
547 about
= nullIfEmpty(about
);
548 aboutEmoji
= nullIfEmpty(aboutEmoji
);
549 avatarPath
= nullIfEmpty(avatarPath
);
550 Optional
<File
> avatarFile
= removeAvatar
552 : avatarPath
== null ?
null : Optional
.of(new File(avatarPath
));
553 m
.setProfile(givenName
, familyName
, about
, aboutEmoji
, avatarFile
);
554 } catch (IOException e
) {
555 throw new Error
.Failure(e
.getMessage());
560 public void updateProfile(
563 final String aboutEmoji
,
565 final boolean removeAvatar
567 updateProfile(name
, "", about
, aboutEmoji
, avatarPath
, removeAvatar
);
571 public void removePin() {
573 m
.setRegistrationLockPin(Optional
.absent());
574 } catch (UnauthenticatedResponseException e
) {
575 throw new Error
.Failure("Remove pin failed with unauthenticated response: " + e
.getMessage());
576 } catch (IOException e
) {
577 throw new Error
.Failure("Remove pin error: " + e
.getMessage());
582 public void setPin(String registrationLockPin
) {
584 m
.setRegistrationLockPin(Optional
.of(registrationLockPin
));
585 } catch (UnauthenticatedResponseException e
) {
586 throw new Error
.Failure("Set pin error failed with unauthenticated response: " + e
.getMessage());
587 } catch (IOException e
) {
588 throw new Error
.Failure("Set pin error: " + e
.getMessage());
592 // Provide option to query a version string in order to react on potential
593 // future interface changes
595 public String
version() {
596 return BaseConfig
.PROJECT_VERSION
;
599 // Create a unique list of Numbers from Identities and Contacts to really get
600 // all numbers the system knows
602 public List
<String
> listNumbers() {
603 return Stream
.concat(m
.getIdentities().stream().map(Identity
::getRecipient
),
604 m
.getContacts().stream().map(Pair
::first
))
605 .map(a
-> a
.getNumber().orElse(null))
606 .filter(Objects
::nonNull
)
608 .collect(Collectors
.toList());
612 public List
<String
> getContactNumber(final String name
) {
613 // Contact names have precedence.
614 var numbers
= new ArrayList
<String
>();
615 var contacts
= m
.getContacts();
616 for (var c
: contacts
) {
617 if (name
.equals(c
.second().getName())) {
618 numbers
.add(c
.first().getLegacyIdentifier());
621 // Try profiles if no contact name was found
622 for (var identity
: m
.getIdentities()) {
623 final var address
= identity
.getRecipient();
624 var number
= address
.getNumber().orElse(null);
625 if (number
!= null) {
626 Profile profile
= null;
628 profile
= m
.getRecipientProfile(RecipientIdentifier
.Single
.fromAddress(address
));
629 } catch (UnregisteredUserException ignored
) {
631 if (profile
!= null && profile
.getDisplayName().equals(name
)) {
640 public void quitGroup(final byte[] groupId
) {
641 var group
= getGroupId(groupId
);
643 m
.quitGroup(group
, Set
.of());
644 } catch (GroupNotFoundException
| NotAGroupMemberException e
) {
645 throw new Error
.GroupNotFound(e
.getMessage());
646 } catch (IOException
| LastGroupAdminException e
) {
647 throw new Error
.Failure(e
.getMessage());
652 public byte[] joinGroup(final String groupLink
) {
654 final var linkUrl
= GroupInviteLinkUrl
.fromUri(groupLink
);
655 if (linkUrl
== null) {
656 throw new Error
.Failure("Group link is invalid:");
658 final var result
= m
.joinGroup(linkUrl
);
659 return result
.first().serialize();
660 } catch (GroupInviteLinkUrl
.InvalidGroupLinkException
| GroupLinkNotActiveException e
) {
661 throw new Error
.Failure("Group link is invalid: " + e
.getMessage());
662 } catch (GroupInviteLinkUrl
.UnknownGroupLinkVersionException e
) {
663 throw new Error
.Failure("Group link was created with an incompatible version: " + e
.getMessage());
664 } catch (IOException e
) {
665 throw new Error
.Failure(e
.getMessage());
670 public boolean isContactBlocked(final String number
) {
671 return m
.isContactBlocked(getSingleRecipientIdentifier(number
, m
.getSelfNumber()));
675 public boolean isGroupBlocked(final byte[] groupId
) {
676 var group
= m
.getGroup(getGroupId(groupId
));
680 return group
.isBlocked();
685 public boolean isMember(final byte[] groupId
) {
686 var group
= m
.getGroup(getGroupId(groupId
));
690 return group
.isMember();
695 public String
uploadStickerPack(String stickerPackPath
) {
696 File path
= new File(stickerPackPath
);
698 return m
.uploadStickerPack(path
).toString();
699 } catch (IOException e
) {
700 throw new Error
.Failure("Upload error (maybe image size is too large):" + e
.getMessage());
701 } catch (StickerPackInvalidException e
) {
702 throw new Error
.Failure("Invalid sticker pack: " + e
.getMessage());
706 private static void checkSendMessageResult(long timestamp
, SendMessageResult result
) throws DBusExecutionException
{
707 var error
= ErrorUtils
.getErrorMessageFromSendMessageResult(result
);
713 final var message
= timestamp
+ "\nFailed to send message:\n" + error
+ '\n';
715 if (result
.getIdentityFailure() != null) {
716 throw new Error
.UntrustedIdentity(message
);
718 throw new Error
.Failure(message
);
722 private static void checkSendMessageResults(
723 long timestamp
, Map
<RecipientIdentifier
, List
<SendMessageResult
>> results
724 ) throws DBusExecutionException
{
725 final var sendMessageResults
= results
.values().stream().findFirst();
726 if (results
.size() == 1 && sendMessageResults
.get().size() == 1) {
727 checkSendMessageResult(timestamp
, sendMessageResults
.get().stream().findFirst().get());
731 var errors
= ErrorUtils
.getErrorMessagesFromSendMessageResults(results
);
732 if (errors
.size() == 0) {
736 var message
= new StringBuilder();
737 message
.append(timestamp
).append('\n');
738 message
.append("Failed to send (some) messages:\n");
739 for (var error
: errors
) {
740 message
.append(error
).append('\n');
743 throw new Error
.Failure(message
.toString());
746 private static void checkSendMessageResults(
747 long timestamp
, Collection
<SendMessageResult
> results
748 ) throws DBusExecutionException
{
749 if (results
.size() == 1) {
750 checkSendMessageResult(timestamp
, results
.stream().findFirst().get());
754 var errors
= ErrorUtils
.getErrorMessagesFromSendMessageResults(results
);
755 if (errors
.size() == 0) {
759 var message
= new StringBuilder();
760 message
.append(timestamp
).append('\n');
761 message
.append("Failed to send (some) messages:\n");
762 for (var error
: errors
) {
763 message
.append(error
).append('\n');
766 throw new Error
.Failure(message
.toString());
769 private static Set
<RecipientIdentifier
.Single
> getSingleRecipientIdentifiers(
770 final Collection
<String
> recipientStrings
, final String localNumber
771 ) throws DBusExecutionException
{
772 final var identifiers
= new HashSet
<RecipientIdentifier
.Single
>();
773 for (var recipientString
: recipientStrings
) {
774 identifiers
.add(getSingleRecipientIdentifier(recipientString
, localNumber
));
779 private static RecipientIdentifier
.Single
getSingleRecipientIdentifier(
780 final String recipientString
, final String localNumber
781 ) throws DBusExecutionException
{
783 return RecipientIdentifier
.Single
.fromString(recipientString
, localNumber
);
784 } catch (InvalidNumberException e
) {
785 throw new Error
.InvalidNumber(e
.getMessage());
789 private static GroupId
getGroupId(byte[] groupId
) throws DBusExecutionException
{
791 return GroupId
.unknownVersion(groupId
);
792 } catch (Throwable e
) {
793 throw new Error
.InvalidGroupId("Invalid group id: " + e
.getMessage());
797 private byte[] nullIfEmpty(final byte[] array
) {
798 return array
.length
== 0 ?
null : array
;
801 private String
nullIfEmpty(final String name
) {
802 return name
.isEmpty() ?
null : name
;
805 private static String
getDeviceObjectPath(String basePath
, long deviceId
) {
806 return basePath
+ "/Devices/" + deviceId
;
809 public class DbusSignalDeviceImpl
extends DbusProperties
implements Signal
.Device
{
811 private final org
.asamk
.signal
.manager
.api
.Device device
;
813 public DbusSignalDeviceImpl(final org
.asamk
.signal
.manager
.api
.Device device
) {
815 super.addPropertiesHandler(new DbusInterfacePropertiesHandler("org.asamk.Signal.Device",
816 List
.of(new DbusProperty
<>("Id", device
::getId
),
817 new DbusProperty
<>("Name",
818 () -> device
.getName() == null ?
"" : device
.getName(),
819 this::setDeviceName
),
820 new DbusProperty
<>("Created", device
::getCreated
),
821 new DbusProperty
<>("LastSeen", device
::getLastSeen
))));
822 this.device
= device
;
826 public String
getObjectPath() {
827 return getDeviceObjectPath(objectPath
, device
.getId());
831 public void removeDevice() throws Error
.Failure
{
833 m
.removeLinkedDevices(device
.getId());
835 } catch (IOException e
) {
836 throw new Error
.Failure(e
.getMessage());
840 private void setDeviceName(String name
) {
841 if (!device
.isThisDevice()) {
842 throw new Error
.Failure("Only the name of this device can be changed");
845 m
.updateAccountAttributes(name
);
846 // update device list
848 } catch (IOException e
) {
849 throw new Error
.Failure(e
.getMessage());