1 package org
.asamk
.signal
.dbus
;
3 import org
.asamk
.Signal
;
4 import org
.asamk
.signal
.BaseConfig
;
5 import org
.asamk
.signal
.DbusReceiveMessageHandler
;
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
.InactiveGroupLinkException
;
13 import org
.asamk
.signal
.manager
.api
.InvalidDeviceLinkException
;
14 import org
.asamk
.signal
.manager
.api
.InvalidNumberException
;
15 import org
.asamk
.signal
.manager
.api
.Message
;
16 import org
.asamk
.signal
.manager
.api
.Pair
;
17 import org
.asamk
.signal
.manager
.api
.RecipientIdentifier
;
18 import org
.asamk
.signal
.manager
.api
.SendMessageResult
;
19 import org
.asamk
.signal
.manager
.api
.TypingAction
;
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
.ErrorUtils
;
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(m
, 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 var recipients
= new ArrayList
<String
>(1);
197 recipients
.add(recipient
);
198 return sendMessage(message
, attachments
, recipients
);
202 public long sendMessage(final String message
, final List
<String
> attachments
, final List
<String
> recipients
) {
204 final var results
= m
.sendMessage(new Message(message
, attachments
, List
.of()),
205 getSingleRecipientIdentifiers(recipients
, m
.getSelfNumber()).stream()
206 .map(RecipientIdentifier
.class::cast
)
207 .collect(Collectors
.toSet()));
209 checkSendMessageResults(results
.timestamp(), results
.results());
210 return results
.timestamp();
211 } catch (AttachmentInvalidException e
) {
212 throw new Error
.AttachmentInvalid(e
.getMessage());
213 } catch (IOException e
) {
214 throw new Error
.Failure(e
);
215 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
216 throw new Error
.GroupNotFound(e
.getMessage());
221 public long sendRemoteDeleteMessage(
222 final long targetSentTimestamp
, final String recipient
224 var recipients
= new ArrayList
<String
>(1);
225 recipients
.add(recipient
);
226 return sendRemoteDeleteMessage(targetSentTimestamp
, recipients
);
230 public long sendRemoteDeleteMessage(
231 final long targetSentTimestamp
, final List
<String
> recipients
234 final var results
= m
.sendRemoteDeleteMessage(targetSentTimestamp
,
235 getSingleRecipientIdentifiers(recipients
, m
.getSelfNumber()).stream()
236 .map(RecipientIdentifier
.class::cast
)
237 .collect(Collectors
.toSet()));
238 checkSendMessageResults(results
.timestamp(), results
.results());
239 return results
.timestamp();
240 } catch (IOException e
) {
241 throw new Error
.Failure(e
.getMessage());
242 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
243 throw new Error
.GroupNotFound(e
.getMessage());
248 public long sendGroupRemoteDeleteMessage(
249 final long targetSentTimestamp
, final byte[] groupId
252 final var results
= m
.sendRemoteDeleteMessage(targetSentTimestamp
,
253 Set
.of(new RecipientIdentifier
.Group(getGroupId(groupId
))));
254 checkSendMessageResults(results
.timestamp(), results
.results());
255 return results
.timestamp();
256 } catch (IOException e
) {
257 throw new Error
.Failure(e
.getMessage());
258 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
259 throw new Error
.GroupNotFound(e
.getMessage());
264 public long sendMessageReaction(
266 final boolean remove
,
267 final String targetAuthor
,
268 final long targetSentTimestamp
,
269 final String recipient
271 var recipients
= new ArrayList
<String
>(1);
272 recipients
.add(recipient
);
273 return sendMessageReaction(emoji
, remove
, targetAuthor
, targetSentTimestamp
, recipients
);
277 public long sendMessageReaction(
279 final boolean remove
,
280 final String targetAuthor
,
281 final long targetSentTimestamp
,
282 final List
<String
> recipients
285 final var results
= m
.sendMessageReaction(emoji
,
287 getSingleRecipientIdentifier(targetAuthor
, m
.getSelfNumber()),
289 getSingleRecipientIdentifiers(recipients
, m
.getSelfNumber()).stream()
290 .map(RecipientIdentifier
.class::cast
)
291 .collect(Collectors
.toSet()));
292 checkSendMessageResults(results
.timestamp(), results
.results());
293 return results
.timestamp();
294 } catch (IOException e
) {
295 throw new Error
.Failure(e
.getMessage());
296 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
297 throw new Error
.GroupNotFound(e
.getMessage());
302 public void sendTyping(
303 final String recipient
, final boolean stop
304 ) throws Error
.Failure
, Error
.GroupNotFound
, Error
.UntrustedIdentity
{
306 var recipients
= new ArrayList
<String
>(1);
307 recipients
.add(recipient
);
308 m
.sendTypingMessage(stop ? TypingAction
.STOP
: TypingAction
.START
,
309 getSingleRecipientIdentifiers(recipients
, m
.getSelfNumber()).stream()
310 .map(RecipientIdentifier
.class::cast
)
311 .collect(Collectors
.toSet()));
312 } catch (IOException e
) {
313 throw new Error
.Failure(e
.getMessage());
314 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
315 throw new Error
.GroupNotFound(e
.getMessage());
316 } catch (UntrustedIdentityException e
) {
317 throw new Error
.UntrustedIdentity(e
.getMessage());
322 public void sendReadReceipt(
323 final String recipient
, final List
<Long
> messageIds
324 ) throws Error
.Failure
, Error
.UntrustedIdentity
{
326 m
.sendReadReceipt(getSingleRecipientIdentifier(recipient
, m
.getSelfNumber()), messageIds
);
327 } catch (IOException e
) {
328 throw new Error
.Failure(e
.getMessage());
329 } catch (UntrustedIdentityException e
) {
330 throw new Error
.UntrustedIdentity(e
.getMessage());
335 public void sendViewedReceipt(
336 final String recipient
, final List
<Long
> messageIds
337 ) throws Error
.Failure
, Error
.UntrustedIdentity
{
339 m
.sendViewedReceipt(getSingleRecipientIdentifier(recipient
, m
.getSelfNumber()), messageIds
);
340 } catch (IOException e
) {
341 throw new Error
.Failure(e
.getMessage());
342 } catch (UntrustedIdentityException e
) {
343 throw new Error
.UntrustedIdentity(e
.getMessage());
348 public void sendContacts() {
351 } catch (IOException e
) {
352 throw new Error
.Failure("SendContacts error: " + e
.getMessage());
357 public void sendSyncRequest() {
359 m
.requestAllSyncData();
360 } catch (IOException e
) {
361 throw new Error
.Failure("Request sync data error: " + e
.getMessage());
366 public long sendNoteToSelfMessage(
367 final String message
, final List
<String
> attachments
368 ) throws Error
.AttachmentInvalid
, Error
.Failure
, Error
.UntrustedIdentity
{
370 final var results
= m
.sendMessage(new Message(message
, attachments
, List
.of()),
371 Set
.of(RecipientIdentifier
.NoteToSelf
.INSTANCE
));
372 checkSendMessageResults(results
.timestamp(), results
.results());
373 return results
.timestamp();
374 } catch (AttachmentInvalidException e
) {
375 throw new Error
.AttachmentInvalid(e
.getMessage());
376 } catch (IOException e
) {
377 throw new Error
.Failure(e
.getMessage());
378 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
379 throw new Error
.GroupNotFound(e
.getMessage());
384 public void sendEndSessionMessage(final List
<String
> recipients
) {
386 final var results
= m
.sendEndSessionMessage(getSingleRecipientIdentifiers(recipients
, m
.getSelfNumber()));
387 checkSendMessageResults(results
.timestamp(), results
.results());
388 } catch (IOException e
) {
389 throw new Error
.Failure(e
.getMessage());
394 public long sendGroupMessage(final String message
, final List
<String
> attachments
, final byte[] groupId
) {
396 var results
= m
.sendMessage(new Message(message
, attachments
, List
.of()),
397 Set
.of(new RecipientIdentifier
.Group(getGroupId(groupId
))));
398 checkSendMessageResults(results
.timestamp(), results
.results());
399 return results
.timestamp();
400 } catch (IOException e
) {
401 throw new Error
.Failure(e
.getMessage());
402 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
403 throw new Error
.GroupNotFound(e
.getMessage());
404 } catch (AttachmentInvalidException e
) {
405 throw new Error
.AttachmentInvalid(e
.getMessage());
410 public long sendGroupMessageReaction(
412 final boolean remove
,
413 final String targetAuthor
,
414 final long targetSentTimestamp
,
418 final var results
= m
.sendMessageReaction(emoji
,
420 getSingleRecipientIdentifier(targetAuthor
, m
.getSelfNumber()),
422 Set
.of(new RecipientIdentifier
.Group(getGroupId(groupId
))));
423 checkSendMessageResults(results
.timestamp(), results
.results());
424 return results
.timestamp();
425 } catch (IOException e
) {
426 throw new Error
.Failure(e
.getMessage());
427 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
428 throw new Error
.GroupNotFound(e
.getMessage());
432 // Since contact names might be empty if not defined, also potentially return
435 public String
getContactName(final String number
) {
436 final var name
= m
.getContactOrProfileName(getSingleRecipientIdentifier(number
, m
.getSelfNumber()));
437 return name
== null ?
"" : name
;
441 public void setContactName(final String number
, final String name
) {
443 m
.setContactName(getSingleRecipientIdentifier(number
, m
.getSelfNumber()), name
);
444 } catch (NotMasterDeviceException e
) {
445 throw new Error
.Failure("This command doesn't work on linked devices.");
446 } catch (IOException e
) {
447 throw new Error
.Failure("Contact is not registered.");
452 public void setExpirationTimer(final String number
, final int expiration
) {
454 m
.setExpirationTimer(getSingleRecipientIdentifier(number
, m
.getSelfNumber()), expiration
);
455 } catch (IOException e
) {
456 throw new Error
.Failure(e
.getMessage());
461 public void setContactBlocked(final String number
, final boolean blocked
) {
463 m
.setContactBlocked(getSingleRecipientIdentifier(number
, m
.getSelfNumber()), blocked
);
464 } catch (NotMasterDeviceException e
) {
465 throw new Error
.Failure("This command doesn't work on linked devices.");
466 } catch (IOException e
) {
467 throw new Error
.Failure(e
.getMessage());
472 public void setGroupBlocked(final byte[] groupId
, final boolean blocked
) {
474 m
.setGroupBlocked(getGroupId(groupId
), blocked
);
475 } catch (NotMasterDeviceException e
) {
476 throw new Error
.Failure("This command doesn't work on linked devices.");
477 } catch (GroupNotFoundException e
) {
478 throw new Error
.GroupNotFound(e
.getMessage());
479 } catch (IOException e
) {
480 throw new Error
.Failure(e
.getMessage());
485 public List
<byte[]> getGroupIds() {
486 var groups
= m
.getGroups();
487 var ids
= new ArrayList
<byte[]>(groups
.size());
488 for (var group
: groups
) {
489 ids
.add(group
.groupId().serialize());
495 public DBusPath
getGroup(final byte[] groupId
) {
497 final var groupOptional
= groups
.stream().filter(g
-> Arrays
.equals(g
.getId(), groupId
)).findFirst();
498 if (groupOptional
.isEmpty()) {
499 throw new Error
.GroupNotFound("Group not found");
501 return groupOptional
.get().getObjectPath();
505 public List
<StructGroup
> listGroups() {
511 public String
getGroupName(final byte[] groupId
) {
512 var group
= m
.getGroup(getGroupId(groupId
));
513 if (group
== null || group
.title() == null) {
516 return group
.title();
521 public List
<String
> getGroupMembers(final byte[] groupId
) {
522 var group
= m
.getGroup(getGroupId(groupId
));
526 final var members
= group
.members();
527 return getRecipientStrings(members
);
532 public byte[] createGroup(
533 final String name
, final List
<String
> members
, final String avatar
534 ) throws Error
.AttachmentInvalid
, Error
.Failure
, Error
.InvalidNumber
{
535 return updateGroup(new byte[0], name
, members
, avatar
);
539 public byte[] updateGroup(byte[] groupId
, String name
, List
<String
> members
, String avatar
) {
541 groupId
= nullIfEmpty(groupId
);
542 name
= nullIfEmpty(name
);
543 avatar
= nullIfEmpty(avatar
);
544 final var memberIdentifiers
= getSingleRecipientIdentifiers(members
, m
.getSelfNumber());
545 if (groupId
== null) {
546 final var results
= m
.createGroup(name
, memberIdentifiers
, avatar
== null ?
null : new File(avatar
));
547 checkSendMessageResults(results
.second().timestamp(), results
.second().results());
548 return results
.first().serialize();
550 final var results
= m
.updateGroup(getGroupId(groupId
),
551 UpdateGroup
.newBuilder()
553 .withMembers(memberIdentifiers
)
554 .withAvatarFile(avatar
== null ?
null : new File(avatar
))
556 if (results
!= null) {
557 checkSendMessageResults(results
.timestamp(), results
.results());
561 } catch (IOException e
) {
562 throw new Error
.Failure(e
.getMessage());
563 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
564 throw new Error
.GroupNotFound(e
.getMessage());
565 } catch (AttachmentInvalidException e
) {
566 throw new Error
.AttachmentInvalid(e
.getMessage());
571 public boolean isRegistered() {
576 public boolean isRegistered(String number
) {
577 var result
= isRegistered(List
.of(number
));
578 return result
.get(0);
582 public List
<Boolean
> isRegistered(List
<String
> numbers
) {
583 var results
= new ArrayList
<Boolean
>();
584 if (numbers
.isEmpty()) {
588 Map
<String
, Pair
<String
, UUID
>> registered
;
590 registered
= m
.areUsersRegistered(new HashSet
<>(numbers
));
591 } catch (IOException e
) {
592 throw new Error
.Failure(e
.getMessage());
595 return numbers
.stream().map(number
-> {
596 var uuid
= registered
.get(number
).second();
598 }).collect(Collectors
.toList());
602 public void updateProfile(
608 final boolean removeAvatar
611 givenName
= nullIfEmpty(givenName
);
612 familyName
= nullIfEmpty(familyName
);
613 about
= nullIfEmpty(about
);
614 aboutEmoji
= nullIfEmpty(aboutEmoji
);
615 avatarPath
= nullIfEmpty(avatarPath
);
616 Optional
<File
> avatarFile
= removeAvatar
618 : avatarPath
== null ?
null : Optional
.of(new File(avatarPath
));
619 m
.setProfile(givenName
, familyName
, about
, aboutEmoji
, avatarFile
);
620 } catch (IOException e
) {
621 throw new Error
.Failure(e
.getMessage());
626 public void updateProfile(
629 final String aboutEmoji
,
631 final boolean removeAvatar
633 updateProfile(name
, "", about
, aboutEmoji
, avatarPath
, removeAvatar
);
637 public void removePin() {
639 m
.setRegistrationLockPin(Optional
.empty());
640 } catch (IOException e
) {
641 throw new Error
.Failure("Remove pin error: " + e
.getMessage());
646 public void setPin(String registrationLockPin
) {
648 m
.setRegistrationLockPin(Optional
.of(registrationLockPin
));
649 } catch (IOException e
) {
650 throw new Error
.Failure("Set pin error: " + e
.getMessage());
654 // Provide option to query a version string in order to react on potential
655 // future interface changes
657 public String
version() {
658 return BaseConfig
.PROJECT_VERSION
;
661 // Create a unique list of Numbers from Identities and Contacts to really get
662 // all numbers the system knows
664 public List
<String
> listNumbers() {
665 return Stream
.concat(m
.getIdentities().stream().map(Identity
::recipient
),
666 m
.getContacts().stream().map(Pair
::first
))
667 .map(a
-> a
.getNumber().orElse(null))
668 .filter(Objects
::nonNull
)
670 .collect(Collectors
.toList());
674 public List
<String
> getContactNumber(final String name
) {
675 // Contact names have precedence.
676 var numbers
= new ArrayList
<String
>();
677 var contacts
= m
.getContacts();
678 for (var c
: contacts
) {
679 if (name
.equals(c
.second().getName())) {
680 numbers
.add(c
.first().getLegacyIdentifier());
683 // Try profiles if no contact name was found
684 for (var identity
: m
.getIdentities()) {
685 final var address
= identity
.recipient();
686 var number
= address
.getNumber().orElse(null);
687 if (number
!= null) {
688 Profile profile
= null;
690 profile
= m
.getRecipientProfile(RecipientIdentifier
.Single
.fromAddress(address
));
691 } catch (IOException ignored
) {
693 if (profile
!= null && profile
.getDisplayName().equals(name
)) {
702 public void quitGroup(final byte[] groupId
) {
703 var group
= getGroupId(groupId
);
705 m
.quitGroup(group
, Set
.of());
706 } catch (GroupNotFoundException
| NotAGroupMemberException e
) {
707 throw new Error
.GroupNotFound(e
.getMessage());
708 } catch (IOException
| LastGroupAdminException e
) {
709 throw new Error
.Failure(e
.getMessage());
714 public byte[] joinGroup(final String groupLink
) {
716 final var linkUrl
= GroupInviteLinkUrl
.fromUri(groupLink
);
717 if (linkUrl
== null) {
718 throw new Error
.Failure("Group link is invalid:");
720 final var result
= m
.joinGroup(linkUrl
);
721 return result
.first().serialize();
722 } catch (GroupInviteLinkUrl
.InvalidGroupLinkException
| InactiveGroupLinkException e
) {
723 throw new Error
.Failure("Group link is invalid: " + e
.getMessage());
724 } catch (GroupInviteLinkUrl
.UnknownGroupLinkVersionException e
) {
725 throw new Error
.Failure("Group link was created with an incompatible version: " + e
.getMessage());
726 } catch (IOException e
) {
727 throw new Error
.Failure(e
.getMessage());
732 public boolean isContactBlocked(final String number
) {
733 return m
.isContactBlocked(getSingleRecipientIdentifier(number
, m
.getSelfNumber()));
737 public boolean isGroupBlocked(final byte[] groupId
) {
738 var group
= m
.getGroup(getGroupId(groupId
));
742 return group
.isBlocked();
747 public boolean isMember(final byte[] groupId
) {
748 var group
= m
.getGroup(getGroupId(groupId
));
752 return group
.isMember();
757 public String
uploadStickerPack(String stickerPackPath
) {
758 File path
= new File(stickerPackPath
);
760 return m
.uploadStickerPack(path
).toString();
761 } catch (IOException e
) {
762 throw new Error
.Failure("Upload error (maybe image size is too large):" + e
.getMessage());
763 } catch (StickerPackInvalidException e
) {
764 throw new Error
.Failure("Invalid sticker pack: " + e
.getMessage());
768 private static void checkSendMessageResult(long timestamp
, SendMessageResult result
) throws DBusExecutionException
{
769 var error
= ErrorUtils
.getErrorMessageFromSendMessageResult(result
);
775 final var message
= timestamp
+ "\nFailed to send message:\n" + error
+ '\n';
777 if (result
.isIdentityFailure()) {
778 throw new Error
.UntrustedIdentity(message
);
780 throw new Error
.Failure(message
);
784 private static void checkSendMessageResults(
785 long timestamp
, Map
<RecipientIdentifier
, List
<SendMessageResult
>> results
786 ) throws DBusExecutionException
{
787 final var sendMessageResults
= results
.values().stream().findFirst();
788 if (results
.size() == 1 && sendMessageResults
.get().size() == 1) {
789 checkSendMessageResult(timestamp
, sendMessageResults
.get().stream().findFirst().get());
793 var errors
= ErrorUtils
.getErrorMessagesFromSendMessageResults(results
);
794 if (errors
.size() == 0) {
798 var message
= new StringBuilder();
799 message
.append(timestamp
).append('\n');
800 message
.append("Failed to send (some) messages:\n");
801 for (var error
: errors
) {
802 message
.append(error
).append('\n');
805 throw new Error
.Failure(message
.toString());
808 private static void checkSendMessageResults(
809 long timestamp
, Collection
<SendMessageResult
> results
810 ) throws DBusExecutionException
{
811 if (results
.size() == 1) {
812 checkSendMessageResult(timestamp
, results
.stream().findFirst().get());
816 var errors
= ErrorUtils
.getErrorMessagesFromSendMessageResults(results
);
817 if (errors
.size() == 0) {
821 var message
= new StringBuilder();
822 message
.append(timestamp
).append('\n');
823 message
.append("Failed to send (some) messages:\n");
824 for (var error
: errors
) {
825 message
.append(error
).append('\n');
828 throw new Error
.Failure(message
.toString());
831 private static List
<String
> getRecipientStrings(final Set
<RecipientAddress
> members
) {
832 return members
.stream().map(RecipientAddress
::getLegacyIdentifier
).collect(Collectors
.toList());
835 private static Set
<RecipientIdentifier
.Single
> getSingleRecipientIdentifiers(
836 final Collection
<String
> recipientStrings
, final String localNumber
837 ) throws DBusExecutionException
{
838 final var identifiers
= new HashSet
<RecipientIdentifier
.Single
>();
839 for (var recipientString
: recipientStrings
) {
840 identifiers
.add(getSingleRecipientIdentifier(recipientString
, localNumber
));
845 private static RecipientIdentifier
.Single
getSingleRecipientIdentifier(
846 final String recipientString
, final String localNumber
847 ) throws DBusExecutionException
{
849 return RecipientIdentifier
.Single
.fromString(recipientString
, localNumber
);
850 } catch (InvalidNumberException e
) {
851 throw new Error
.InvalidNumber(e
.getMessage());
855 private static GroupId
getGroupId(byte[] groupId
) throws DBusExecutionException
{
857 return GroupId
.unknownVersion(groupId
);
858 } catch (Throwable e
) {
859 throw new Error
.InvalidGroupId("Invalid group id: " + e
.getMessage());
863 private byte[] nullIfEmpty(final byte[] array
) {
864 return array
.length
== 0 ?
null : array
;
867 private String
nullIfEmpty(final String name
) {
868 return name
.isEmpty() ?
null : name
;
871 private String
emptyIfNull(final String string
) {
872 return string
== null ?
"" : string
;
875 private static String
getDeviceObjectPath(String basePath
, long deviceId
) {
876 return basePath
+ "/Devices/" + deviceId
;
879 private void updateDevices() {
880 List
<org
.asamk
.signal
.manager
.api
.Device
> linkedDevices
;
882 linkedDevices
= m
.getLinkedDevices();
883 } catch (IOException e
) {
884 throw new Error
.Failure("Failed to get linked devices: " + e
.getMessage());
889 linkedDevices
.forEach(d
-> {
890 final var object
= new DbusSignalDeviceImpl(d
);
891 final var deviceObjectPath
= object
.getObjectPath();
892 exportObject(object
);
893 if (d
.isThisDevice()) {
894 thisDevice
= new DBusPath(deviceObjectPath
);
896 this.devices
.add(new StructDevice(new DBusPath(deviceObjectPath
), d
.id(), emptyIfNull(d
.name())));
900 private void unExportDevices() {
901 this.devices
.stream()
902 .map(StructDevice
::getObjectPath
)
903 .map(DBusPath
::getPath
)
904 .forEach(connection
::unExportObject
);
905 this.devices
.clear();
908 private static String
getGroupObjectPath(String basePath
, byte[] groupId
) {
909 return basePath
+ "/Groups/" + Base64
.getEncoder()
910 .encodeToString(groupId
)
916 private void updateGroups() {
917 List
<org
.asamk
.signal
.manager
.api
.Group
> groups
;
918 groups
= m
.getGroups();
922 groups
.forEach(g
-> {
923 final var object
= new DbusSignalGroupImpl(g
.groupId());
924 exportObject(object
);
925 this.groups
.add(new StructGroup(new DBusPath(object
.getObjectPath()),
926 g
.groupId().serialize(),
927 emptyIfNull(g
.title())));
931 private void unExportGroups() {
932 this.groups
.stream().map(StructGroup
::getObjectPath
).map(DBusPath
::getPath
).forEach(connection
::unExportObject
);
936 private static String
getConfigurationObjectPath(String basePath
) {
937 return basePath
+ "/Configuration";
940 private void updateConfiguration() {
941 unExportConfiguration();
942 final var object
= new DbusSignalConfigurationImpl();
943 exportObject(object
);
946 private void unExportConfiguration() {
947 final var objectPath
= getConfigurationObjectPath(this.objectPath
);
948 connection
.unExportObject(objectPath
);
951 private void exportObject(final DBusInterface object
) {
953 connection
.exportObject(object
);
954 logger
.debug("Exported dbus object: " + object
.getObjectPath());
955 } catch (DBusException e
) {
960 public class DbusSignalDeviceImpl
extends DbusProperties
implements Signal
.Device
{
962 private final org
.asamk
.signal
.manager
.api
.Device device
;
964 public DbusSignalDeviceImpl(final org
.asamk
.signal
.manager
.api
.Device device
) {
965 super.addPropertiesHandler(new DbusInterfacePropertiesHandler("org.asamk.Signal.Device",
966 List
.of(new DbusProperty
<>("Id", device
::id
),
967 new DbusProperty
<>("Name", () -> emptyIfNull(device
.name()), this::setDeviceName
),
968 new DbusProperty
<>("Created", device
::created
),
969 new DbusProperty
<>("LastSeen", device
::lastSeen
))));
970 this.device
= device
;
974 public String
getObjectPath() {
975 return getDeviceObjectPath(objectPath
, device
.id());
979 public void removeDevice() throws Error
.Failure
{
981 m
.removeLinkedDevices(device
.id());
983 } catch (IOException e
) {
984 throw new Error
.Failure(e
.getMessage());
988 private void setDeviceName(String name
) {
989 if (!device
.isThisDevice()) {
990 throw new Error
.Failure("Only the name of this device can be changed");
993 m
.updateAccountAttributes(name
);
994 // update device list
996 } catch (IOException e
) {
997 throw new Error
.Failure(e
.getMessage());
1002 public class DbusSignalConfigurationImpl
extends DbusProperties
implements Signal
.Configuration
{
1004 public DbusSignalConfigurationImpl(
1006 super.addPropertiesHandler(new DbusInterfacePropertiesHandler("org.asamk.Signal.Configuration",
1007 List
.of(new DbusProperty
<>("ReadReceipts", this::getReadReceipts
, this::setReadReceipts
),
1008 new DbusProperty
<>("UnidentifiedDeliveryIndicators",
1009 this::getUnidentifiedDeliveryIndicators
,
1010 this::setUnidentifiedDeliveryIndicators
),
1011 new DbusProperty
<>("TypingIndicators",
1012 this::getTypingIndicators
,
1013 this::setTypingIndicators
),
1014 new DbusProperty
<>("LinkPreviews", this::getLinkPreviews
, this::setLinkPreviews
))));
1019 public String
getObjectPath() {
1020 return getConfigurationObjectPath(objectPath
);
1023 public void setReadReceipts(Boolean readReceipts
) {
1024 setConfiguration(readReceipts
, null, null, null);
1027 public void setUnidentifiedDeliveryIndicators(Boolean unidentifiedDeliveryIndicators
) {
1028 setConfiguration(null, unidentifiedDeliveryIndicators
, null, null);
1031 public void setTypingIndicators(Boolean typingIndicators
) {
1032 setConfiguration(null, null, typingIndicators
, null);
1035 public void setLinkPreviews(Boolean linkPreviews
) {
1036 setConfiguration(null, null, null, linkPreviews
);
1039 private void setConfiguration(
1040 Boolean readReceipts
,
1041 Boolean unidentifiedDeliveryIndicators
,
1042 Boolean typingIndicators
,
1043 Boolean linkPreviews
1046 m
.updateConfiguration(new org
.asamk
.signal
.manager
.api
.Configuration(Optional
.ofNullable(readReceipts
),
1047 Optional
.ofNullable(unidentifiedDeliveryIndicators
),
1048 Optional
.ofNullable(typingIndicators
),
1049 Optional
.ofNullable(linkPreviews
)));
1050 } catch (IOException e
) {
1051 throw new Error
.Failure("UpdateAccount error: " + e
.getMessage());
1052 } catch (NotMasterDeviceException e
) {
1053 throw new Error
.Failure("This command doesn't work on linked devices.");
1057 private boolean getReadReceipts() {
1058 return m
.getConfiguration().readReceipts().orElse(false);
1061 private boolean getUnidentifiedDeliveryIndicators() {
1062 return m
.getConfiguration().unidentifiedDeliveryIndicators().orElse(false);
1065 private boolean getTypingIndicators() {
1066 return m
.getConfiguration().typingIndicators().orElse(false);
1069 private boolean getLinkPreviews() {
1070 return m
.getConfiguration().linkPreviews().orElse(false);
1074 public class DbusSignalGroupImpl
extends DbusProperties
implements Signal
.Group
{
1076 private final GroupId groupId
;
1078 public DbusSignalGroupImpl(final GroupId groupId
) {
1079 this.groupId
= groupId
;
1080 super.addPropertiesHandler(new DbusInterfacePropertiesHandler("org.asamk.Signal.Group",
1081 List
.of(new DbusProperty
<>("Id", groupId
::serialize
),
1082 new DbusProperty
<>("Name", () -> emptyIfNull(getGroup().title()), this::setGroupName
),
1083 new DbusProperty
<>("Description",
1084 () -> emptyIfNull(getGroup().description()),
1085 this::setGroupDescription
),
1086 new DbusProperty
<>("Avatar", this::setGroupAvatar
),
1087 new DbusProperty
<>("IsBlocked", () -> getGroup().isBlocked(), this::setIsBlocked
),
1088 new DbusProperty
<>("IsMember", () -> getGroup().isMember()),
1089 new DbusProperty
<>("IsAdmin", () -> getGroup().isAdmin()),
1090 new DbusProperty
<>("MessageExpirationTimer",
1091 () -> getGroup().messageExpirationTimer(),
1092 this::setMessageExpirationTime
),
1093 new DbusProperty
<>("Members",
1094 () -> new Variant
<>(getRecipientStrings(getGroup().members()), "as")),
1095 new DbusProperty
<>("PendingMembers",
1096 () -> new Variant
<>(getRecipientStrings(getGroup().pendingMembers()), "as")),
1097 new DbusProperty
<>("RequestingMembers",
1098 () -> new Variant
<>(getRecipientStrings(getGroup().requestingMembers()), "as")),
1099 new DbusProperty
<>("Admins",
1100 () -> new Variant
<>(getRecipientStrings(getGroup().adminMembers()), "as")),
1101 new DbusProperty
<>("PermissionAddMember",
1102 () -> getGroup().permissionAddMember().name(),
1103 this::setGroupPermissionAddMember
),
1104 new DbusProperty
<>("PermissionEditDetails",
1105 () -> getGroup().permissionEditDetails().name(),
1106 this::setGroupPermissionEditDetails
),
1107 new DbusProperty
<>("PermissionSendMessage",
1108 () -> getGroup().permissionSendMessage().name(),
1109 this::setGroupPermissionSendMessage
),
1110 new DbusProperty
<>("GroupInviteLink", () -> {
1111 final var groupInviteLinkUrl
= getGroup().groupInviteLinkUrl();
1112 return groupInviteLinkUrl
== null ?
"" : groupInviteLinkUrl
.getUrl();
1117 public String
getObjectPath() {
1118 return getGroupObjectPath(objectPath
, groupId
.serialize());
1122 public void quitGroup() throws Error
.Failure
{
1124 m
.quitGroup(groupId
, Set
.of());
1125 } catch (GroupNotFoundException
| NotAGroupMemberException e
) {
1126 throw new Error
.GroupNotFound(e
.getMessage());
1127 } catch (IOException e
) {
1128 throw new Error
.Failure(e
.getMessage());
1129 } catch (LastGroupAdminException e
) {
1130 throw new Error
.LastGroupAdmin(e
.getMessage());
1135 public void addMembers(final List
<String
> recipients
) throws Error
.Failure
{
1136 final var memberIdentifiers
= getSingleRecipientIdentifiers(recipients
, m
.getSelfNumber());
1137 updateGroup(UpdateGroup
.newBuilder().withMembers(memberIdentifiers
).build());
1141 public void removeMembers(final List
<String
> recipients
) throws Error
.Failure
{
1142 final var memberIdentifiers
= getSingleRecipientIdentifiers(recipients
, m
.getSelfNumber());
1143 updateGroup(UpdateGroup
.newBuilder().withRemoveMembers(memberIdentifiers
).build());
1147 public void addAdmins(final List
<String
> recipients
) throws Error
.Failure
{
1148 final var memberIdentifiers
= getSingleRecipientIdentifiers(recipients
, m
.getSelfNumber());
1149 updateGroup(UpdateGroup
.newBuilder().withAdmins(memberIdentifiers
).build());
1153 public void removeAdmins(final List
<String
> recipients
) throws Error
.Failure
{
1154 final var memberIdentifiers
= getSingleRecipientIdentifiers(recipients
, m
.getSelfNumber());
1155 updateGroup(UpdateGroup
.newBuilder().withRemoveAdmins(memberIdentifiers
).build());
1159 public void resetLink() throws Error
.Failure
{
1160 updateGroup(UpdateGroup
.newBuilder().withResetGroupLink(true).build());
1164 public void disableLink() throws Error
.Failure
{
1165 updateGroup(UpdateGroup
.newBuilder().withGroupLinkState(GroupLinkState
.DISABLED
).build());
1169 public void enableLink(final boolean requiresApproval
) throws Error
.Failure
{
1170 updateGroup(UpdateGroup
.newBuilder()
1171 .withGroupLinkState(requiresApproval
1172 ? GroupLinkState
.ENABLED_WITH_APPROVAL
1173 : GroupLinkState
.ENABLED
)
1177 private org
.asamk
.signal
.manager
.api
.Group
getGroup() {
1178 return m
.getGroup(groupId
);
1181 private void setGroupName(final String name
) {
1182 updateGroup(UpdateGroup
.newBuilder().withName(name
).build());
1185 private void setGroupDescription(final String description
) {
1186 updateGroup(UpdateGroup
.newBuilder().withDescription(description
).build());
1189 private void setGroupAvatar(final String avatar
) {
1190 updateGroup(UpdateGroup
.newBuilder().withAvatarFile(new File(avatar
)).build());
1193 private void setMessageExpirationTime(final int expirationTime
) {
1194 updateGroup(UpdateGroup
.newBuilder().withExpirationTimer(expirationTime
).build());
1197 private void setGroupPermissionAddMember(final String permission
) {
1198 updateGroup(UpdateGroup
.newBuilder().withAddMemberPermission(GroupPermission
.valueOf(permission
)).build());
1201 private void setGroupPermissionEditDetails(final String permission
) {
1202 updateGroup(UpdateGroup
.newBuilder()
1203 .withEditDetailsPermission(GroupPermission
.valueOf(permission
))
1207 private void setGroupPermissionSendMessage(final String permission
) {
1208 updateGroup(UpdateGroup
.newBuilder()
1209 .withIsAnnouncementGroup(GroupPermission
.valueOf(permission
) == GroupPermission
.ONLY_ADMINS
)
1213 private void setIsBlocked(final boolean isBlocked
) {
1215 m
.setGroupBlocked(groupId
, isBlocked
);
1216 } catch (NotMasterDeviceException e
) {
1217 throw new Error
.Failure("This command doesn't work on linked devices.");
1218 } catch (GroupNotFoundException e
) {
1219 throw new Error
.GroupNotFound(e
.getMessage());
1220 } catch (IOException e
) {
1221 throw new Error
.Failure(e
.getMessage());
1225 private void updateGroup(final UpdateGroup updateGroup
) {
1227 m
.updateGroup(groupId
, updateGroup
);
1228 } catch (IOException e
) {
1229 throw new Error
.Failure(e
.getMessage());
1230 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
1231 throw new Error
.GroupNotFound(e
.getMessage());
1232 } catch (AttachmentInvalidException e
) {
1233 throw new Error
.AttachmentInvalid(e
.getMessage());