1 package org
.asamk
.signal
.dbus
;
3 import org
.asamk
.Signal
;
4 import org
.asamk
.signal
.BaseConfig
;
5 import org
.asamk
.signal
.manager
.AttachmentInvalidException
;
6 import org
.asamk
.signal
.manager
.Manager
;
7 import org
.asamk
.signal
.manager
.NotMasterDeviceException
;
8 import org
.asamk
.signal
.manager
.StickerPackInvalidException
;
9 import org
.asamk
.signal
.manager
.api
.Identity
;
10 import org
.asamk
.signal
.manager
.api
.InactiveGroupLinkException
;
11 import org
.asamk
.signal
.manager
.api
.InvalidDeviceLinkException
;
12 import org
.asamk
.signal
.manager
.api
.InvalidNumberException
;
13 import org
.asamk
.signal
.manager
.api
.Message
;
14 import org
.asamk
.signal
.manager
.api
.Pair
;
15 import org
.asamk
.signal
.manager
.api
.RecipientIdentifier
;
16 import org
.asamk
.signal
.manager
.api
.SendMessageResult
;
17 import org
.asamk
.signal
.manager
.api
.SendMessageResults
;
18 import org
.asamk
.signal
.manager
.api
.TypingAction
;
19 import org
.asamk
.signal
.manager
.api
.UnregisteredRecipientException
;
20 import org
.asamk
.signal
.manager
.api
.UpdateGroup
;
21 import org
.asamk
.signal
.manager
.groups
.GroupId
;
22 import org
.asamk
.signal
.manager
.groups
.GroupInviteLinkUrl
;
23 import org
.asamk
.signal
.manager
.groups
.GroupLinkState
;
24 import org
.asamk
.signal
.manager
.groups
.GroupNotFoundException
;
25 import org
.asamk
.signal
.manager
.groups
.GroupPermission
;
26 import org
.asamk
.signal
.manager
.groups
.GroupSendingNotAllowedException
;
27 import org
.asamk
.signal
.manager
.groups
.LastGroupAdminException
;
28 import org
.asamk
.signal
.manager
.groups
.NotAGroupMemberException
;
29 import org
.asamk
.signal
.manager
.storage
.recipients
.Profile
;
30 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientAddress
;
31 import org
.asamk
.signal
.util
.SendMessageResultUtils
;
32 import org
.freedesktop
.dbus
.DBusPath
;
33 import org
.freedesktop
.dbus
.connections
.impl
.DBusConnection
;
34 import org
.freedesktop
.dbus
.exceptions
.DBusException
;
35 import org
.freedesktop
.dbus
.exceptions
.DBusExecutionException
;
36 import org
.freedesktop
.dbus
.interfaces
.DBusInterface
;
37 import org
.freedesktop
.dbus
.types
.Variant
;
38 import org
.slf4j
.Logger
;
39 import org
.slf4j
.LoggerFactory
;
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
;
53 import java
.util
.Optional
;
55 import java
.util
.UUID
;
56 import java
.util
.stream
.Collectors
;
57 import java
.util
.stream
.Stream
;
59 public class DbusSignalImpl
implements Signal
{
61 private final Manager m
;
62 private final DBusConnection connection
;
63 private final String objectPath
;
64 private final boolean noReceiveOnStart
;
66 private DBusPath thisDevice
;
67 private final List
<StructDevice
> devices
= new ArrayList
<>();
68 private final List
<StructGroup
> groups
= new ArrayList
<>();
69 private DbusReceiveMessageHandler dbusMessageHandler
;
70 private int subscriberCount
;
72 private final static Logger logger
= LoggerFactory
.getLogger(DbusSignalImpl
.class);
74 public DbusSignalImpl(
75 final Manager m
, DBusConnection connection
, final String objectPath
, final boolean noReceiveOnStart
78 this.connection
= connection
;
79 this.objectPath
= objectPath
;
80 this.noReceiveOnStart
= noReceiveOnStart
;
83 public void initObjects() {
84 if (!noReceiveOnStart
) {
90 updateConfiguration();
94 if (dbusMessageHandler
!= null) {
95 m
.removeReceiveHandler(dbusMessageHandler
);
96 dbusMessageHandler
= null;
100 unExportConfiguration();
104 public String
getObjectPath() {
109 public String
getSelfNumber() {
110 return m
.getSelfNumber();
114 public void subscribeReceive() {
115 if (dbusMessageHandler
== null) {
116 dbusMessageHandler
= new DbusReceiveMessageHandler(connection
, objectPath
);
117 m
.addReceiveHandler(dbusMessageHandler
);
123 public void unsubscribeReceive() {
124 subscriberCount
= Math
.max(0, subscriberCount
- 1);
125 if (subscriberCount
== 0 && dbusMessageHandler
!= null) {
126 m
.removeReceiveHandler(dbusMessageHandler
);
127 dbusMessageHandler
= null;
132 public void submitRateLimitChallenge(String challenge
, String captcha
) {
134 m
.submitRateLimitRecaptchaChallenge(challenge
, captcha
);
135 } catch (IOException e
) {
136 throw new Error
.Failure("Submit challenge error: " + e
.getMessage());
142 public void unregister() throws Error
.Failure
{
145 } catch (IOException e
) {
146 throw new Error
.Failure("Failed to unregister: " + e
.getMessage());
151 public void deleteAccount() throws Error
.Failure
{
154 } catch (IOException e
) {
155 throw new Error
.Failure("Failed to delete account: " + e
.getMessage());
160 public void addDevice(String uri
) {
162 m
.addDeviceLink(new URI(uri
));
163 } catch (IOException
| InvalidDeviceLinkException e
) {
164 throw new Error
.Failure(e
.getClass().getSimpleName() + " Add device link failed. " + e
.getMessage());
165 } catch (URISyntaxException e
) {
166 throw new Error
.InvalidUri(e
.getClass().getSimpleName()
167 + " Device link uri has invalid format: "
173 public DBusPath
getDevice(long deviceId
) {
175 final var deviceOptional
= devices
.stream().filter(g
-> g
.getId().equals(deviceId
)).findFirst();
176 if (deviceOptional
.isEmpty()) {
177 throw new Error
.DeviceNotFound("Device not found");
179 return deviceOptional
.get().getObjectPath();
183 public List
<StructDevice
> listDevices() {
189 public DBusPath
getThisDevice() {
195 public long sendMessage(final String message
, final List
<String
> attachments
, final String recipient
) {
196 return sendMessage(message
, attachments
, List
.of(recipient
));
200 public long sendMessage(final String message
, final List
<String
> attachments
, final List
<String
> recipients
) {
202 final var results
= m
.sendMessage(new Message(message
, attachments
, List
.of(), Optional
.empty()),
203 getSingleRecipientIdentifiers(recipients
, m
.getSelfNumber()).stream()
204 .map(RecipientIdentifier
.class::cast
)
205 .collect(Collectors
.toSet()));
207 checkSendMessageResults(results
);
208 return results
.timestamp();
209 } catch (AttachmentInvalidException e
) {
210 throw new Error
.AttachmentInvalid(e
.getMessage());
211 } catch (IOException e
) {
212 throw new Error
.Failure(e
);
213 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
214 throw new Error
.GroupNotFound(e
.getMessage());
215 } catch (UnregisteredRecipientException e
) {
216 throw new Error
.UntrustedIdentity(e
.getSender().getIdentifier() + " is not registered.");
221 public long sendRemoteDeleteMessage(
222 final long targetSentTimestamp
, final String recipient
224 return sendRemoteDeleteMessage(targetSentTimestamp
, List
.of(recipient
));
228 public long sendRemoteDeleteMessage(
229 final long targetSentTimestamp
, final List
<String
> recipients
232 final var results
= m
.sendRemoteDeleteMessage(targetSentTimestamp
,
233 getSingleRecipientIdentifiers(recipients
, m
.getSelfNumber()).stream()
234 .map(RecipientIdentifier
.class::cast
)
235 .collect(Collectors
.toSet()));
236 checkSendMessageResults(results
);
237 return results
.timestamp();
238 } catch (IOException e
) {
239 throw new Error
.Failure(e
.getMessage());
240 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
241 throw new Error
.GroupNotFound(e
.getMessage());
246 public long sendMessageReaction(
248 final boolean remove
,
249 final String targetAuthor
,
250 final long targetSentTimestamp
,
251 final String recipient
253 return sendMessageReaction(emoji
, remove
, targetAuthor
, targetSentTimestamp
, List
.of(recipient
));
257 public long sendMessageReaction(
259 final boolean remove
,
260 final String targetAuthor
,
261 final long targetSentTimestamp
,
262 final List
<String
> recipients
265 final var results
= m
.sendMessageReaction(emoji
,
267 getSingleRecipientIdentifier(targetAuthor
, m
.getSelfNumber()),
269 getSingleRecipientIdentifiers(recipients
, m
.getSelfNumber()).stream()
270 .map(RecipientIdentifier
.class::cast
)
271 .collect(Collectors
.toSet()));
272 checkSendMessageResults(results
);
273 return results
.timestamp();
274 } catch (IOException e
) {
275 throw new Error
.Failure(e
.getMessage());
276 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
277 throw new Error
.GroupNotFound(e
.getMessage());
278 } catch (UnregisteredRecipientException e
) {
279 throw new Error
.UntrustedIdentity(e
.getSender().getIdentifier() + " is not registered.");
284 public void sendTyping(
285 final String recipient
, final boolean stop
286 ) throws Error
.Failure
, Error
.GroupNotFound
, Error
.UntrustedIdentity
{
288 final var results
= m
.sendTypingMessage(stop ? TypingAction
.STOP
: TypingAction
.START
,
289 getSingleRecipientIdentifiers(List
.of(recipient
), m
.getSelfNumber()).stream()
290 .map(RecipientIdentifier
.class::cast
)
291 .collect(Collectors
.toSet()));
292 checkSendMessageResults(results
);
293 } catch (IOException e
) {
294 throw new Error
.Failure(e
.getMessage());
295 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
296 throw new Error
.GroupNotFound(e
.getMessage());
301 public void sendReadReceipt(
302 final String recipient
, final List
<Long
> messageIds
303 ) throws Error
.Failure
, Error
.UntrustedIdentity
{
305 final var results
= m
.sendReadReceipt(getSingleRecipientIdentifier(recipient
, m
.getSelfNumber()),
307 checkSendMessageResults(results
);
308 } catch (IOException e
) {
309 throw new Error
.Failure(e
.getMessage());
314 public void sendViewedReceipt(
315 final String recipient
, final List
<Long
> messageIds
316 ) throws Error
.Failure
, Error
.UntrustedIdentity
{
318 final var results
= m
.sendViewedReceipt(getSingleRecipientIdentifier(recipient
, m
.getSelfNumber()),
320 checkSendMessageResults(results
);
321 } catch (IOException e
) {
322 throw new Error
.Failure(e
.getMessage());
327 public void sendContacts() {
330 } catch (IOException e
) {
331 throw new Error
.Failure("SendContacts error: " + e
.getMessage());
336 public void sendSyncRequest() {
338 m
.requestAllSyncData();
339 } catch (IOException e
) {
340 throw new Error
.Failure("Request sync data error: " + e
.getMessage());
345 public long sendNoteToSelfMessage(
346 final String message
, final List
<String
> attachments
347 ) throws Error
.AttachmentInvalid
, Error
.Failure
, Error
.UntrustedIdentity
{
349 final var results
= m
.sendMessage(new Message(message
, attachments
, List
.of(), Optional
.empty()),
350 Set
.of(RecipientIdentifier
.NoteToSelf
.INSTANCE
));
351 checkSendMessageResults(results
);
352 return results
.timestamp();
353 } catch (AttachmentInvalidException e
) {
354 throw new Error
.AttachmentInvalid(e
.getMessage());
355 } catch (IOException e
) {
356 throw new Error
.Failure(e
.getMessage());
357 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
358 throw new Error
.GroupNotFound(e
.getMessage());
359 } catch (UnregisteredRecipientException e
) {
360 throw new Error
.UntrustedIdentity(e
.getSender().getIdentifier() + " is not registered.");
365 public void sendEndSessionMessage(final List
<String
> recipients
) {
367 final var results
= m
.sendEndSessionMessage(getSingleRecipientIdentifiers(recipients
, m
.getSelfNumber()));
368 checkSendMessageResults(results
);
369 } catch (IOException e
) {
370 throw new Error
.Failure(e
.getMessage());
375 public void deleteRecipient(final String recipient
) throws Error
.Failure
{
376 m
.deleteRecipient(getSingleRecipientIdentifier(recipient
, m
.getSelfNumber()));
380 public void deleteContact(final String recipient
) throws Error
.Failure
{
381 m
.deleteContact(getSingleRecipientIdentifier(recipient
, m
.getSelfNumber()));
385 public long sendGroupMessage(final String message
, final List
<String
> attachments
, final byte[] groupId
) {
387 var results
= m
.sendMessage(new Message(message
, attachments
, List
.of(), Optional
.empty()),
388 Set
.of(getGroupRecipientIdentifier(groupId
)));
389 checkSendMessageResults(results
);
390 return results
.timestamp();
391 } catch (IOException e
) {
392 throw new Error
.Failure(e
.getMessage());
393 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
394 throw new Error
.GroupNotFound(e
.getMessage());
395 } catch (AttachmentInvalidException e
) {
396 throw new Error
.AttachmentInvalid(e
.getMessage());
397 } catch (UnregisteredRecipientException e
) {
398 throw new Error
.UntrustedIdentity(e
.getSender().getIdentifier() + " is not registered.");
403 public void sendGroupTyping(
404 final byte[] groupId
, final boolean stop
405 ) throws Error
.Failure
, Error
.GroupNotFound
, Error
.UntrustedIdentity
{
407 final var results
= m
.sendTypingMessage(stop ? TypingAction
.STOP
: TypingAction
.START
,
408 Set
.of(getGroupRecipientIdentifier(groupId
)));
409 checkSendMessageResults(results
);
410 } catch (IOException e
) {
411 throw new Error
.Failure(e
.getMessage());
412 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
413 throw new Error
.GroupNotFound(e
.getMessage());
418 public long sendGroupRemoteDeleteMessage(
419 final long targetSentTimestamp
, final byte[] groupId
422 final var results
= m
.sendRemoteDeleteMessage(targetSentTimestamp
,
423 Set
.of(getGroupRecipientIdentifier(groupId
)));
424 checkSendMessageResults(results
);
425 return results
.timestamp();
426 } catch (IOException e
) {
427 throw new Error
.Failure(e
.getMessage());
428 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
429 throw new Error
.GroupNotFound(e
.getMessage());
434 public long sendGroupMessageReaction(
436 final boolean remove
,
437 final String targetAuthor
,
438 final long targetSentTimestamp
,
442 final var results
= m
.sendMessageReaction(emoji
,
444 getSingleRecipientIdentifier(targetAuthor
, m
.getSelfNumber()),
446 Set
.of(getGroupRecipientIdentifier(groupId
)));
447 checkSendMessageResults(results
);
448 return results
.timestamp();
449 } catch (IOException e
) {
450 throw new Error
.Failure(e
.getMessage());
451 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
452 throw new Error
.GroupNotFound(e
.getMessage());
453 } catch (UnregisteredRecipientException e
) {
454 throw new Error
.UntrustedIdentity(e
.getSender().getIdentifier() + " is not registered.");
458 // Since contact names might be empty if not defined, also potentially return
461 public String
getContactName(final String number
) {
462 final var name
= m
.getContactOrProfileName(getSingleRecipientIdentifier(number
, m
.getSelfNumber()));
463 return name
== null ?
"" : name
;
467 public void setContactName(final String number
, final String name
) {
469 m
.setContactName(getSingleRecipientIdentifier(number
, m
.getSelfNumber()), name
);
470 } catch (NotMasterDeviceException e
) {
471 throw new Error
.Failure("This command doesn't work on linked devices.");
472 } catch (IOException e
) {
473 throw new Error
.Failure("Contact is not registered.");
474 } catch (UnregisteredRecipientException e
) {
475 throw new Error
.UntrustedIdentity(e
.getSender().getIdentifier() + " is not registered.");
480 public void setExpirationTimer(final String number
, final int expiration
) {
482 m
.setExpirationTimer(getSingleRecipientIdentifier(number
, m
.getSelfNumber()), expiration
);
483 } catch (IOException e
) {
484 throw new Error
.Failure(e
.getMessage());
485 } catch (UnregisteredRecipientException e
) {
486 throw new Error
.UntrustedIdentity(e
.getSender().getIdentifier() + " is not registered.");
491 public void setContactBlocked(final String number
, final boolean blocked
) {
493 m
.setContactBlocked(getSingleRecipientIdentifier(number
, m
.getSelfNumber()), blocked
);
494 } catch (NotMasterDeviceException e
) {
495 throw new Error
.Failure("This command doesn't work on linked devices.");
496 } catch (IOException e
) {
497 throw new Error
.Failure(e
.getMessage());
498 } catch (UnregisteredRecipientException e
) {
499 throw new Error
.UntrustedIdentity(e
.getSender().getIdentifier() + " is not registered.");
504 public void setGroupBlocked(final byte[] groupId
, final boolean blocked
) {
506 m
.setGroupBlocked(getGroupId(groupId
), blocked
);
507 } catch (NotMasterDeviceException e
) {
508 throw new Error
.Failure("This command doesn't work on linked devices.");
509 } catch (GroupNotFoundException e
) {
510 throw new Error
.GroupNotFound(e
.getMessage());
511 } catch (IOException e
) {
512 throw new Error
.Failure(e
.getMessage());
517 public List
<byte[]> getGroupIds() {
518 var groups
= m
.getGroups();
519 return groups
.stream().map(g
-> g
.groupId().serialize()).toList();
523 public DBusPath
getGroup(final byte[] groupId
) {
525 final var groupOptional
= groups
.stream().filter(g
-> Arrays
.equals(g
.getId(), groupId
)).findFirst();
526 if (groupOptional
.isEmpty()) {
527 throw new Error
.GroupNotFound("Group not found");
529 return groupOptional
.get().getObjectPath();
533 public List
<StructGroup
> listGroups() {
539 public String
getGroupName(final byte[] groupId
) {
540 var group
= m
.getGroup(getGroupId(groupId
));
541 if (group
== null || group
.title() == null) {
544 return group
.title();
549 public List
<String
> getGroupMembers(final byte[] groupId
) {
550 var group
= m
.getGroup(getGroupId(groupId
));
554 final var members
= group
.members();
555 return getRecipientStrings(members
);
560 public byte[] createGroup(
561 final String name
, final List
<String
> members
, final String avatar
562 ) throws Error
.AttachmentInvalid
, Error
.Failure
, Error
.InvalidNumber
{
563 return updateGroup(new byte[0], name
, members
, avatar
);
567 public byte[] updateGroup(byte[] groupId
, String name
, List
<String
> members
, String avatar
) {
569 groupId
= nullIfEmpty(groupId
);
570 name
= nullIfEmpty(name
);
571 avatar
= nullIfEmpty(avatar
);
572 final var memberIdentifiers
= getSingleRecipientIdentifiers(members
, m
.getSelfNumber());
573 if (groupId
== null) {
574 final var results
= m
.createGroup(name
, memberIdentifiers
, avatar
== null ?
null : new File(avatar
));
576 checkGroupSendMessageResults(results
.second().timestamp(), results
.second().results());
577 return results
.first().serialize();
579 final var results
= m
.updateGroup(getGroupId(groupId
),
580 UpdateGroup
.newBuilder()
582 .withMembers(memberIdentifiers
)
583 .withAvatarFile(avatar
== null ?
null : new File(avatar
))
585 if (results
!= null) {
586 checkGroupSendMessageResults(results
.timestamp(), results
.results());
590 } catch (IOException e
) {
591 throw new Error
.Failure(e
.getMessage());
592 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
593 throw new Error
.GroupNotFound(e
.getMessage());
594 } catch (AttachmentInvalidException e
) {
595 throw new Error
.AttachmentInvalid(e
.getMessage());
596 } catch (UnregisteredRecipientException e
) {
597 throw new Error
.UntrustedIdentity(e
.getSender().getIdentifier() + " is not registered.");
602 public boolean isRegistered() {
607 public boolean isRegistered(String number
) {
608 var result
= isRegistered(List
.of(number
));
609 return result
.get(0);
613 public List
<Boolean
> isRegistered(List
<String
> numbers
) {
614 if (numbers
.isEmpty()) {
618 Map
<String
, Pair
<String
, UUID
>> registered
;
620 registered
= m
.areUsersRegistered(new HashSet
<>(numbers
));
621 } catch (IOException e
) {
622 throw new Error
.Failure(e
.getMessage());
625 return numbers
.stream().map(number
-> {
626 var uuid
= registered
.get(number
).second();
632 public void updateProfile(
638 final boolean removeAvatar
641 givenName
= nullIfEmpty(givenName
);
642 familyName
= nullIfEmpty(familyName
);
643 about
= nullIfEmpty(about
);
644 aboutEmoji
= nullIfEmpty(aboutEmoji
);
645 avatarPath
= nullIfEmpty(avatarPath
);
646 Optional
<File
> avatarFile
= removeAvatar
648 : avatarPath
== null ?
null : Optional
.of(new File(avatarPath
));
649 m
.setProfile(givenName
, familyName
, about
, aboutEmoji
, avatarFile
);
650 } catch (IOException e
) {
651 throw new Error
.Failure(e
.getMessage());
656 public void updateProfile(
659 final String aboutEmoji
,
661 final boolean removeAvatar
663 updateProfile(name
, "", about
, aboutEmoji
, avatarPath
, removeAvatar
);
667 public void removePin() {
669 m
.setRegistrationLockPin(Optional
.empty());
670 } catch (IOException e
) {
671 throw new Error
.Failure("Remove pin error: " + e
.getMessage());
672 } catch (NotMasterDeviceException e
) {
673 throw new Error
.Failure("This command doesn't work on linked devices.");
678 public void setPin(String registrationLockPin
) {
680 m
.setRegistrationLockPin(Optional
.of(registrationLockPin
));
681 } catch (IOException e
) {
682 throw new Error
.Failure("Set pin error: " + e
.getMessage());
683 } catch (NotMasterDeviceException e
) {
684 throw new Error
.Failure("This command doesn't work on linked devices.");
688 // Provide option to query a version string in order to react on potential
689 // future interface changes
691 public String
version() {
692 return BaseConfig
.PROJECT_VERSION
;
695 // Create a unique list of Numbers from Identities and Contacts to really get
696 // all numbers the system knows
698 public List
<String
> listNumbers() {
699 return Stream
.concat(m
.getIdentities().stream().map(Identity
::recipient
),
700 m
.getContacts().stream().map(Pair
::first
))
701 .map(a
-> a
.number().orElse(null))
702 .filter(Objects
::nonNull
)
708 public List
<String
> getContactNumber(final String name
) {
709 // Contact names have precedence.
710 var numbers
= new ArrayList
<String
>();
711 var contacts
= m
.getContacts();
712 for (var c
: contacts
) {
713 if (name
.equals(c
.second().getName())) {
714 numbers
.add(c
.first().getLegacyIdentifier());
717 // Try profiles if no contact name was found
718 for (var identity
: m
.getIdentities()) {
719 final var address
= identity
.recipient();
720 var number
= address
.number().orElse(null);
721 if (number
!= null) {
722 Profile profile
= null;
724 profile
= m
.getRecipientProfile(RecipientIdentifier
.Single
.fromAddress(address
));
725 } catch (IOException
| UnregisteredRecipientException ignored
) {
727 if (profile
!= null && profile
.getDisplayName().equals(name
)) {
736 public void quitGroup(final byte[] groupId
) {
737 var group
= getGroupId(groupId
);
739 m
.quitGroup(group
, Set
.of());
740 } catch (GroupNotFoundException
| NotAGroupMemberException e
) {
741 throw new Error
.GroupNotFound(e
.getMessage());
742 } catch (IOException
| LastGroupAdminException e
) {
743 throw new Error
.Failure(e
.getMessage());
744 } catch (UnregisteredRecipientException e
) {
745 throw new Error
.UntrustedIdentity(e
.getSender().getIdentifier() + " is not registered.");
750 public byte[] joinGroup(final String groupLink
) {
752 final var linkUrl
= GroupInviteLinkUrl
.fromUri(groupLink
);
753 if (linkUrl
== null) {
754 throw new Error
.Failure("Group link is invalid:");
756 final var result
= m
.joinGroup(linkUrl
);
757 return result
.first().serialize();
758 } catch (GroupInviteLinkUrl
.InvalidGroupLinkException
| InactiveGroupLinkException e
) {
759 throw new Error
.Failure("Group link is invalid: " + e
.getMessage());
760 } catch (GroupInviteLinkUrl
.UnknownGroupLinkVersionException e
) {
761 throw new Error
.Failure("Group link was created with an incompatible version: " + e
.getMessage());
762 } catch (IOException e
) {
763 throw new Error
.Failure(e
.getMessage());
768 public boolean isContactBlocked(final String number
) {
769 return m
.isContactBlocked(getSingleRecipientIdentifier(number
, m
.getSelfNumber()));
773 public boolean isGroupBlocked(final byte[] groupId
) {
774 var group
= m
.getGroup(getGroupId(groupId
));
778 return group
.isBlocked();
783 public boolean isMember(final byte[] groupId
) {
784 var group
= m
.getGroup(getGroupId(groupId
));
788 return group
.isMember();
793 public String
uploadStickerPack(String stickerPackPath
) {
794 File path
= new File(stickerPackPath
);
796 return m
.uploadStickerPack(path
).toString();
797 } catch (IOException e
) {
798 throw new Error
.Failure("Upload error (maybe image size is too large):" + e
.getMessage());
799 } catch (StickerPackInvalidException e
) {
800 throw new Error
.Failure("Invalid sticker pack: " + e
.getMessage());
804 private static void checkSendMessageResult(long timestamp
, SendMessageResult result
) throws DBusExecutionException
{
805 var error
= SendMessageResultUtils
.getErrorMessageFromSendMessageResult(result
);
811 final var message
= "\nFailed to send message:\n" + error
+ '\n' + timestamp
;
813 if (result
.isIdentityFailure()) {
814 throw new Error
.UntrustedIdentity(message
);
816 throw new Error
.Failure(message
);
820 private void checkSendMessageResults(final SendMessageResults results
) {
821 final var sendMessageResults
= results
.results().values().stream().findFirst();
822 if (results
.results().size() == 1 && sendMessageResults
.get().size() == 1) {
823 checkSendMessageResult(results
.timestamp(), sendMessageResults
.get().stream().findFirst().get());
827 if (results
.hasSuccess()) {
831 var message
= new StringBuilder();
832 message
.append("Failed to send messages:\n");
833 var errors
= SendMessageResultUtils
.getErrorMessagesFromSendMessageResults(results
.results());
834 for (var error
: errors
) {
835 message
.append(error
).append('\n');
837 message
.append(results
.timestamp());
839 throw new Error
.Failure(message
.toString());
842 private static void checkGroupSendMessageResults(
843 long timestamp
, Collection
<SendMessageResult
> results
844 ) throws DBusExecutionException
{
845 if (results
.size() == 1) {
846 checkSendMessageResult(timestamp
, results
.stream().findFirst().get());
850 var errors
= SendMessageResultUtils
.getErrorMessagesFromSendMessageResults(results
);
851 if (errors
.size() < results
.size()) {
855 var message
= new StringBuilder();
856 message
.append("Failed to send message:\n");
857 for (var error
: errors
) {
858 message
.append(error
).append('\n');
860 message
.append(timestamp
);
862 throw new Error
.Failure(message
.toString());
865 private static List
<String
> getRecipientStrings(final Set
<RecipientAddress
> members
) {
866 return members
.stream().map(RecipientAddress
::getLegacyIdentifier
).toList();
869 private static Set
<RecipientIdentifier
.Single
> getSingleRecipientIdentifiers(
870 final Collection
<String
> recipientStrings
, final String localNumber
871 ) throws DBusExecutionException
{
872 final var identifiers
= new HashSet
<RecipientIdentifier
.Single
>();
873 for (var recipientString
: recipientStrings
) {
874 identifiers
.add(getSingleRecipientIdentifier(recipientString
, localNumber
));
879 private static RecipientIdentifier
.Single
getSingleRecipientIdentifier(
880 final String recipientString
, final String localNumber
881 ) throws DBusExecutionException
{
883 return RecipientIdentifier
.Single
.fromString(recipientString
, localNumber
);
884 } catch (InvalidNumberException e
) {
885 throw new Error
.InvalidNumber(e
.getMessage());
889 private RecipientIdentifier
.Group
getGroupRecipientIdentifier(final byte[] groupId
) {
890 return new RecipientIdentifier
.Group(getGroupId(groupId
));
893 private static GroupId
getGroupId(byte[] groupId
) throws DBusExecutionException
{
895 return GroupId
.unknownVersion(groupId
);
896 } catch (Throwable e
) {
897 throw new Error
.InvalidGroupId("Invalid group id: " + e
.getMessage());
901 private byte[] nullIfEmpty(final byte[] array
) {
902 return array
.length
== 0 ?
null : array
;
905 private String
nullIfEmpty(final String name
) {
906 return name
.isEmpty() ?
null : name
;
909 private String
emptyIfNull(final String string
) {
910 return string
== null ?
"" : string
;
913 private static String
getDeviceObjectPath(String basePath
, long deviceId
) {
914 return basePath
+ "/Devices/" + deviceId
;
917 private void updateDevices() {
918 List
<org
.asamk
.signal
.manager
.api
.Device
> linkedDevices
;
920 linkedDevices
= m
.getLinkedDevices();
921 } catch (IOException e
) {
922 throw new Error
.Failure("Failed to get linked devices: " + e
.getMessage());
927 linkedDevices
.forEach(d
-> {
928 final var object
= new DbusSignalDeviceImpl(d
);
929 final var deviceObjectPath
= object
.getObjectPath();
930 exportObject(object
);
931 if (d
.isThisDevice()) {
932 thisDevice
= new DBusPath(deviceObjectPath
);
934 this.devices
.add(new StructDevice(new DBusPath(deviceObjectPath
), d
.id(), emptyIfNull(d
.name())));
938 private void unExportDevices() {
939 this.devices
.stream()
940 .map(StructDevice
::getObjectPath
)
941 .map(DBusPath
::getPath
)
942 .forEach(connection
::unExportObject
);
943 this.devices
.clear();
946 private static String
getGroupObjectPath(String basePath
, byte[] groupId
) {
947 return basePath
+ "/Groups/" + Base64
.getEncoder()
948 .encodeToString(groupId
)
954 private void updateGroups() {
955 List
<org
.asamk
.signal
.manager
.api
.Group
> groups
;
956 groups
= m
.getGroups();
960 groups
.forEach(g
-> {
961 final var object
= new DbusSignalGroupImpl(g
.groupId());
962 exportObject(object
);
963 this.groups
.add(new StructGroup(new DBusPath(object
.getObjectPath()),
964 g
.groupId().serialize(),
965 emptyIfNull(g
.title())));
969 private void unExportGroups() {
970 this.groups
.stream().map(StructGroup
::getObjectPath
).map(DBusPath
::getPath
).forEach(connection
::unExportObject
);
974 private static String
getConfigurationObjectPath(String basePath
) {
975 return basePath
+ "/Configuration";
978 private void updateConfiguration() {
979 unExportConfiguration();
980 final var object
= new DbusSignalConfigurationImpl();
981 exportObject(object
);
984 private void unExportConfiguration() {
985 final var objectPath
= getConfigurationObjectPath(this.objectPath
);
986 connection
.unExportObject(objectPath
);
989 private void exportObject(final DBusInterface object
) {
991 connection
.exportObject(object
);
992 logger
.debug("Exported dbus object: " + object
.getObjectPath());
993 } catch (DBusException e
) {
998 public class DbusSignalDeviceImpl
extends DbusProperties
implements Signal
.Device
{
1000 private final org
.asamk
.signal
.manager
.api
.Device device
;
1002 public DbusSignalDeviceImpl(final org
.asamk
.signal
.manager
.api
.Device device
) {
1003 super.addPropertiesHandler(new DbusInterfacePropertiesHandler("org.asamk.Signal.Device",
1004 List
.of(new DbusProperty
<>("Id", device
::id
),
1005 new DbusProperty
<>("Name", () -> emptyIfNull(device
.name()), this::setDeviceName
),
1006 new DbusProperty
<>("Created", device
::created
),
1007 new DbusProperty
<>("LastSeen", device
::lastSeen
))));
1008 this.device
= device
;
1012 public String
getObjectPath() {
1013 return getDeviceObjectPath(objectPath
, device
.id());
1017 public void removeDevice() throws Error
.Failure
{
1019 m
.removeLinkedDevices(device
.id());
1021 } catch (IOException e
) {
1022 throw new Error
.Failure(e
.getMessage());
1026 private void setDeviceName(String name
) {
1027 if (!device
.isThisDevice()) {
1028 throw new Error
.Failure("Only the name of this device can be changed");
1031 m
.updateAccountAttributes(name
);
1032 // update device list
1034 } catch (IOException e
) {
1035 throw new Error
.Failure(e
.getMessage());
1040 public class DbusSignalConfigurationImpl
extends DbusProperties
implements Signal
.Configuration
{
1042 public DbusSignalConfigurationImpl(
1044 super.addPropertiesHandler(new DbusInterfacePropertiesHandler("org.asamk.Signal.Configuration",
1045 List
.of(new DbusProperty
<>("ReadReceipts", this::getReadReceipts
, this::setReadReceipts
),
1046 new DbusProperty
<>("UnidentifiedDeliveryIndicators",
1047 this::getUnidentifiedDeliveryIndicators
,
1048 this::setUnidentifiedDeliveryIndicators
),
1049 new DbusProperty
<>("TypingIndicators",
1050 this::getTypingIndicators
,
1051 this::setTypingIndicators
),
1052 new DbusProperty
<>("LinkPreviews", this::getLinkPreviews
, this::setLinkPreviews
))));
1057 public String
getObjectPath() {
1058 return getConfigurationObjectPath(objectPath
);
1061 public void setReadReceipts(Boolean readReceipts
) {
1062 setConfiguration(readReceipts
, null, null, null);
1065 public void setUnidentifiedDeliveryIndicators(Boolean unidentifiedDeliveryIndicators
) {
1066 setConfiguration(null, unidentifiedDeliveryIndicators
, null, null);
1069 public void setTypingIndicators(Boolean typingIndicators
) {
1070 setConfiguration(null, null, typingIndicators
, null);
1073 public void setLinkPreviews(Boolean linkPreviews
) {
1074 setConfiguration(null, null, null, linkPreviews
);
1077 private void setConfiguration(
1078 Boolean readReceipts
,
1079 Boolean unidentifiedDeliveryIndicators
,
1080 Boolean typingIndicators
,
1081 Boolean linkPreviews
1084 m
.updateConfiguration(new org
.asamk
.signal
.manager
.api
.Configuration(Optional
.ofNullable(readReceipts
),
1085 Optional
.ofNullable(unidentifiedDeliveryIndicators
),
1086 Optional
.ofNullable(typingIndicators
),
1087 Optional
.ofNullable(linkPreviews
)));
1088 } catch (IOException e
) {
1089 throw new Error
.Failure("UpdateAccount error: " + e
.getMessage());
1090 } catch (NotMasterDeviceException e
) {
1091 throw new Error
.Failure("This command doesn't work on linked devices.");
1095 private boolean getReadReceipts() {
1096 return m
.getConfiguration().readReceipts().orElse(false);
1099 private boolean getUnidentifiedDeliveryIndicators() {
1100 return m
.getConfiguration().unidentifiedDeliveryIndicators().orElse(false);
1103 private boolean getTypingIndicators() {
1104 return m
.getConfiguration().typingIndicators().orElse(false);
1107 private boolean getLinkPreviews() {
1108 return m
.getConfiguration().linkPreviews().orElse(false);
1112 public class DbusSignalGroupImpl
extends DbusProperties
implements Signal
.Group
{
1114 private final GroupId groupId
;
1116 public DbusSignalGroupImpl(final GroupId groupId
) {
1117 this.groupId
= groupId
;
1118 super.addPropertiesHandler(new DbusInterfacePropertiesHandler("org.asamk.Signal.Group",
1119 List
.of(new DbusProperty
<>("Id", groupId
::serialize
),
1120 new DbusProperty
<>("Name", () -> emptyIfNull(getGroup().title()), this::setGroupName
),
1121 new DbusProperty
<>("Description",
1122 () -> emptyIfNull(getGroup().description()),
1123 this::setGroupDescription
),
1124 new DbusProperty
<>("Avatar", this::setGroupAvatar
),
1125 new DbusProperty
<>("IsBlocked", () -> getGroup().isBlocked(), this::setIsBlocked
),
1126 new DbusProperty
<>("IsMember", () -> getGroup().isMember()),
1127 new DbusProperty
<>("IsAdmin", () -> getGroup().isAdmin()),
1128 new DbusProperty
<>("MessageExpirationTimer",
1129 () -> getGroup().messageExpirationTimer(),
1130 this::setMessageExpirationTime
),
1131 new DbusProperty
<>("Members",
1132 () -> new Variant
<>(getRecipientStrings(getGroup().members()), "as")),
1133 new DbusProperty
<>("PendingMembers",
1134 () -> new Variant
<>(getRecipientStrings(getGroup().pendingMembers()), "as")),
1135 new DbusProperty
<>("RequestingMembers",
1136 () -> new Variant
<>(getRecipientStrings(getGroup().requestingMembers()), "as")),
1137 new DbusProperty
<>("Admins",
1138 () -> new Variant
<>(getRecipientStrings(getGroup().adminMembers()), "as")),
1139 new DbusProperty
<>("PermissionAddMember",
1140 () -> getGroup().permissionAddMember().name(),
1141 this::setGroupPermissionAddMember
),
1142 new DbusProperty
<>("PermissionEditDetails",
1143 () -> getGroup().permissionEditDetails().name(),
1144 this::setGroupPermissionEditDetails
),
1145 new DbusProperty
<>("PermissionSendMessage",
1146 () -> getGroup().permissionSendMessage().name(),
1147 this::setGroupPermissionSendMessage
),
1148 new DbusProperty
<>("GroupInviteLink", () -> {
1149 final var groupInviteLinkUrl
= getGroup().groupInviteLinkUrl();
1150 return groupInviteLinkUrl
== null ?
"" : groupInviteLinkUrl
.getUrl();
1155 public String
getObjectPath() {
1156 return getGroupObjectPath(objectPath
, groupId
.serialize());
1160 public void quitGroup() throws Error
.Failure
{
1162 m
.quitGroup(groupId
, Set
.of());
1163 } catch (GroupNotFoundException
| NotAGroupMemberException e
) {
1164 throw new Error
.GroupNotFound(e
.getMessage());
1165 } catch (IOException e
) {
1166 throw new Error
.Failure(e
.getMessage());
1167 } catch (LastGroupAdminException e
) {
1168 throw new Error
.LastGroupAdmin(e
.getMessage());
1169 } catch (UnregisteredRecipientException e
) {
1170 throw new Error
.UntrustedIdentity(e
.getSender().getIdentifier() + " is not registered.");
1175 public void deleteGroup() throws Error
.Failure
, Error
.LastGroupAdmin
{
1177 m
.deleteGroup(groupId
);
1178 } catch (IOException e
) {
1179 throw new Error
.Failure(e
.getMessage());
1185 public void addMembers(final List
<String
> recipients
) throws Error
.Failure
{
1186 final var memberIdentifiers
= getSingleRecipientIdentifiers(recipients
, m
.getSelfNumber());
1187 updateGroup(UpdateGroup
.newBuilder().withMembers(memberIdentifiers
).build());
1191 public void removeMembers(final List
<String
> recipients
) throws Error
.Failure
{
1192 final var memberIdentifiers
= getSingleRecipientIdentifiers(recipients
, m
.getSelfNumber());
1193 updateGroup(UpdateGroup
.newBuilder().withRemoveMembers(memberIdentifiers
).build());
1197 public void addAdmins(final List
<String
> recipients
) throws Error
.Failure
{
1198 final var memberIdentifiers
= getSingleRecipientIdentifiers(recipients
, m
.getSelfNumber());
1199 updateGroup(UpdateGroup
.newBuilder().withAdmins(memberIdentifiers
).build());
1203 public void removeAdmins(final List
<String
> recipients
) throws Error
.Failure
{
1204 final var memberIdentifiers
= getSingleRecipientIdentifiers(recipients
, m
.getSelfNumber());
1205 updateGroup(UpdateGroup
.newBuilder().withRemoveAdmins(memberIdentifiers
).build());
1209 public void resetLink() throws Error
.Failure
{
1210 updateGroup(UpdateGroup
.newBuilder().withResetGroupLink(true).build());
1214 public void disableLink() throws Error
.Failure
{
1215 updateGroup(UpdateGroup
.newBuilder().withGroupLinkState(GroupLinkState
.DISABLED
).build());
1219 public void enableLink(final boolean requiresApproval
) throws Error
.Failure
{
1220 updateGroup(UpdateGroup
.newBuilder()
1221 .withGroupLinkState(requiresApproval
1222 ? GroupLinkState
.ENABLED_WITH_APPROVAL
1223 : GroupLinkState
.ENABLED
)
1227 private org
.asamk
.signal
.manager
.api
.Group
getGroup() {
1228 return m
.getGroup(groupId
);
1231 private void setGroupName(final String name
) {
1232 updateGroup(UpdateGroup
.newBuilder().withName(name
).build());
1235 private void setGroupDescription(final String description
) {
1236 updateGroup(UpdateGroup
.newBuilder().withDescription(description
).build());
1239 private void setGroupAvatar(final String avatar
) {
1240 updateGroup(UpdateGroup
.newBuilder().withAvatarFile(new File(avatar
)).build());
1243 private void setMessageExpirationTime(final int expirationTime
) {
1244 updateGroup(UpdateGroup
.newBuilder().withExpirationTimer(expirationTime
).build());
1247 private void setGroupPermissionAddMember(final String permission
) {
1248 updateGroup(UpdateGroup
.newBuilder().withAddMemberPermission(GroupPermission
.valueOf(permission
)).build());
1251 private void setGroupPermissionEditDetails(final String permission
) {
1252 updateGroup(UpdateGroup
.newBuilder()
1253 .withEditDetailsPermission(GroupPermission
.valueOf(permission
))
1257 private void setGroupPermissionSendMessage(final String permission
) {
1258 updateGroup(UpdateGroup
.newBuilder()
1259 .withIsAnnouncementGroup(GroupPermission
.valueOf(permission
) == GroupPermission
.ONLY_ADMINS
)
1263 private void setIsBlocked(final boolean isBlocked
) {
1265 m
.setGroupBlocked(groupId
, isBlocked
);
1266 } catch (NotMasterDeviceException e
) {
1267 throw new Error
.Failure("This command doesn't work on linked devices.");
1268 } catch (GroupNotFoundException e
) {
1269 throw new Error
.GroupNotFound(e
.getMessage());
1270 } catch (IOException e
) {
1271 throw new Error
.Failure(e
.getMessage());
1275 private void updateGroup(final UpdateGroup updateGroup
) {
1277 m
.updateGroup(groupId
, updateGroup
);
1278 } catch (IOException e
) {
1279 throw new Error
.Failure(e
.getMessage());
1280 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
1281 throw new Error
.GroupNotFound(e
.getMessage());
1282 } catch (AttachmentInvalidException e
) {
1283 throw new Error
.AttachmentInvalid(e
.getMessage());
1284 } catch (UnregisteredRecipientException e
) {
1285 throw new Error
.UntrustedIdentity(e
.getSender().getIdentifier() + " is not registered.");