1 package org
.asamk
.signal
.dbus
;
3 import org
.asamk
.Signal
;
4 import org
.asamk
.signal
.DbusConfig
;
5 import org
.asamk
.signal
.manager
.Manager
;
6 import org
.asamk
.signal
.manager
.api
.AttachmentInvalidException
;
7 import org
.asamk
.signal
.manager
.api
.Configuration
;
8 import org
.asamk
.signal
.manager
.api
.Device
;
9 import org
.asamk
.signal
.manager
.api
.Group
;
10 import org
.asamk
.signal
.manager
.api
.Identity
;
11 import org
.asamk
.signal
.manager
.api
.InactiveGroupLinkException
;
12 import org
.asamk
.signal
.manager
.api
.InvalidDeviceLinkException
;
13 import org
.asamk
.signal
.manager
.api
.Message
;
14 import org
.asamk
.signal
.manager
.api
.MessageEnvelope
;
15 import org
.asamk
.signal
.manager
.api
.NotPrimaryDeviceException
;
16 import org
.asamk
.signal
.manager
.api
.Pair
;
17 import org
.asamk
.signal
.manager
.api
.RecipientIdentifier
;
18 import org
.asamk
.signal
.manager
.api
.SendGroupMessageResults
;
19 import org
.asamk
.signal
.manager
.api
.SendMessageResults
;
20 import org
.asamk
.signal
.manager
.api
.StickerPack
;
21 import org
.asamk
.signal
.manager
.api
.StickerPackInvalidException
;
22 import org
.asamk
.signal
.manager
.api
.StickerPackUrl
;
23 import org
.asamk
.signal
.manager
.api
.TypingAction
;
24 import org
.asamk
.signal
.manager
.api
.UpdateGroup
;
25 import org
.asamk
.signal
.manager
.api
.UpdateProfile
;
26 import org
.asamk
.signal
.manager
.api
.UserStatus
;
27 import org
.asamk
.signal
.manager
.groups
.GroupId
;
28 import org
.asamk
.signal
.manager
.groups
.GroupInviteLinkUrl
;
29 import org
.asamk
.signal
.manager
.groups
.GroupNotFoundException
;
30 import org
.asamk
.signal
.manager
.groups
.GroupPermission
;
31 import org
.asamk
.signal
.manager
.groups
.GroupSendingNotAllowedException
;
32 import org
.asamk
.signal
.manager
.groups
.LastGroupAdminException
;
33 import org
.asamk
.signal
.manager
.groups
.NotAGroupMemberException
;
34 import org
.asamk
.signal
.manager
.storage
.recipients
.Contact
;
35 import org
.asamk
.signal
.manager
.storage
.recipients
.Profile
;
36 import org
.asamk
.signal
.manager
.storage
.recipients
.Recipient
;
37 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientAddress
;
38 import org
.freedesktop
.dbus
.DBusMap
;
39 import org
.freedesktop
.dbus
.DBusPath
;
40 import org
.freedesktop
.dbus
.connections
.impl
.DBusConnection
;
41 import org
.freedesktop
.dbus
.exceptions
.DBusException
;
42 import org
.freedesktop
.dbus
.interfaces
.DBusInterface
;
43 import org
.freedesktop
.dbus
.interfaces
.DBusSigHandler
;
44 import org
.freedesktop
.dbus
.types
.Variant
;
47 import java
.io
.IOException
;
49 import java
.net
.URISyntaxException
;
50 import java
.time
.Duration
;
51 import java
.util
.ArrayList
;
52 import java
.util
.Collection
;
53 import java
.util
.HashMap
;
54 import java
.util
.HashSet
;
55 import java
.util
.List
;
57 import java
.util
.Objects
;
58 import java
.util
.Optional
;
60 import java
.util
.concurrent
.atomic
.AtomicLong
;
61 import java
.util
.function
.Function
;
62 import java
.util
.function
.Supplier
;
63 import java
.util
.stream
.Collectors
;
64 import java
.util
.stream
.Stream
;
67 * This class implements the Manager interface using the DBus Signal interface, where possible.
68 * It's used for the signal-cli dbus client mode (--dbus, --dbus-system)
70 public class DbusManagerImpl
implements Manager
{
72 private final Signal signal
;
73 private final DBusConnection connection
;
75 private final Set
<ReceiveMessageHandler
> weakHandlers
= new HashSet
<>();
76 private final Set
<ReceiveMessageHandler
> messageHandlers
= new HashSet
<>();
77 private final List
<Runnable
> closedListeners
= new ArrayList
<>();
78 private DBusSigHandler
<Signal
.MessageReceivedV2
> dbusMsgHandler
;
79 private DBusSigHandler
<Signal
.ReceiptReceivedV2
> dbusRcptHandler
;
80 private DBusSigHandler
<Signal
.SyncMessageReceivedV2
> dbusSyncHandler
;
82 public DbusManagerImpl(final Signal signal
, DBusConnection connection
) {
84 this.connection
= connection
;
88 public String
getSelfNumber() {
89 return signal
.getSelfNumber();
93 public Map
<String
, UserStatus
> getUserStatus(final Set
<String
> numbers
) throws IOException
{
94 final var numbersList
= new ArrayList
<>(numbers
);
95 final var registered
= signal
.isRegistered(numbersList
);
97 final var result
= new HashMap
<String
, UserStatus
>();
98 for (var i
= 0; i
< numbersList
.size(); i
++) {
99 result
.put(numbersList
.get(i
),
100 new UserStatus(numbersList
.get(i
),
101 registered
.get(i
) ? RecipientAddress
.UNKNOWN_UUID
: null,
108 public void updateAccountAttributes(final String deviceName
) throws IOException
{
109 if (deviceName
!= null) {
110 final var devicePath
= signal
.getThisDevice();
111 getRemoteObject(devicePath
, Signal
.Device
.class).Set("org.asamk.Signal.Device", "Name", deviceName
);
116 public Configuration
getConfiguration() {
117 final var configuration
= getRemoteObject(new DBusPath(signal
.getObjectPath() + "/Configuration"),
118 Signal
.Configuration
.class).GetAll("org.asamk.Signal.Configuration");
119 return new Configuration(Optional
.of((Boolean
) configuration
.get("ReadReceipts").getValue()),
120 Optional
.of((Boolean
) configuration
.get("UnidentifiedDeliveryIndicators").getValue()),
121 Optional
.of((Boolean
) configuration
.get("TypingIndicators").getValue()),
122 Optional
.of((Boolean
) configuration
.get("LinkPreviews").getValue()));
126 public void updateConfiguration(Configuration newConfiguration
) throws IOException
{
127 final var configuration
= getRemoteObject(new DBusPath(signal
.getObjectPath() + "/Configuration"),
128 Signal
.Configuration
.class);
129 newConfiguration
.readReceipts()
130 .ifPresent(v
-> configuration
.Set("org.asamk.Signal.Configuration", "ReadReceipts", v
));
131 newConfiguration
.unidentifiedDeliveryIndicators()
132 .ifPresent(v
-> configuration
.Set("org.asamk.Signal.Configuration",
133 "UnidentifiedDeliveryIndicators",
135 newConfiguration
.typingIndicators()
136 .ifPresent(v
-> configuration
.Set("org.asamk.Signal.Configuration", "TypingIndicators", v
));
137 newConfiguration
.linkPreviews()
138 .ifPresent(v
-> configuration
.Set("org.asamk.Signal.Configuration", "LinkPreviews", v
));
142 public void updateProfile(UpdateProfile updateProfile
) throws IOException
{
143 signal
.updateProfile(emptyIfNull(updateProfile
.getGivenName()),
144 emptyIfNull(updateProfile
.getFamilyName()),
145 emptyIfNull(updateProfile
.getAbout()),
146 emptyIfNull(updateProfile
.getAboutEmoji()),
147 updateProfile
.getAvatar() == null ?
"" : updateProfile
.getAvatar().getPath(),
148 updateProfile
.isDeleteAvatar());
152 public void unregister() throws IOException
{
157 public void deleteAccount() throws IOException
{
158 signal
.deleteAccount();
162 public void submitRateLimitRecaptchaChallenge(final String challenge
, final String captcha
) throws IOException
{
163 signal
.submitRateLimitChallenge(challenge
, captcha
);
167 public List
<Device
> getLinkedDevices() throws IOException
{
168 final var thisDevice
= signal
.getThisDevice();
169 return signal
.listDevices().stream().map(d
-> {
170 final var device
= getRemoteObject(d
.getObjectPath(),
171 Signal
.Device
.class).GetAll("org.asamk.Signal.Device");
172 return new Device((Integer
) device
.get("Id").getValue(),
173 (String
) device
.get("Name").getValue(),
174 (long) device
.get("Created").getValue(),
175 (long) device
.get("LastSeen").getValue(),
176 thisDevice
.equals(d
.getObjectPath()));
181 public void removeLinkedDevices(final int deviceId
) throws IOException
{
182 final var devicePath
= signal
.getDevice(deviceId
);
183 getRemoteObject(devicePath
, Signal
.Device
.class).removeDevice();
187 public void addDeviceLink(final URI linkUri
) throws IOException
, InvalidDeviceLinkException
{
188 signal
.addDevice(linkUri
.toString());
192 public void setRegistrationLockPin(final Optional
<String
> pin
) throws IOException
{
193 if (pin
.isPresent()) {
194 signal
.setPin(pin
.get());
201 public Profile
getRecipientProfile(final RecipientIdentifier
.Single recipient
) {
202 throw new UnsupportedOperationException();
206 public List
<Group
> getGroups() {
207 final var groups
= signal
.listGroups();
208 return groups
.stream().map(Signal
.StructGroup
::getObjectPath
).map(this::getGroup
).toList();
212 public SendGroupMessageResults
quitGroup(
213 final GroupId groupId
, final Set
<RecipientIdentifier
.Single
> groupAdmins
214 ) throws GroupNotFoundException
, IOException
, NotAGroupMemberException
, LastGroupAdminException
{
215 if (groupAdmins
.size() > 0) {
216 throw new UnsupportedOperationException();
218 final var group
= getRemoteObject(signal
.getGroup(groupId
.serialize()), Signal
.Group
.class);
220 return new SendGroupMessageResults(0, List
.of());
224 public void deleteGroup(final GroupId groupId
) throws IOException
{
225 final var group
= getRemoteObject(signal
.getGroup(groupId
.serialize()), Signal
.Group
.class);
230 public Pair
<GroupId
, SendGroupMessageResults
> createGroup(
231 final String name
, final Set
<RecipientIdentifier
.Single
> members
, final File avatarFile
232 ) throws IOException
, AttachmentInvalidException
{
233 final var newGroupId
= signal
.createGroup(emptyIfNull(name
),
234 members
.stream().map(RecipientIdentifier
.Single
::getIdentifier
).toList(),
235 avatarFile
== null ?
"" : avatarFile
.getPath());
236 return new Pair
<>(GroupId
.unknownVersion(newGroupId
), new SendGroupMessageResults(0, List
.of()));
240 public SendGroupMessageResults
updateGroup(
241 final GroupId groupId
, final UpdateGroup updateGroup
242 ) throws IOException
, GroupNotFoundException
, AttachmentInvalidException
, NotAGroupMemberException
, GroupSendingNotAllowedException
{
243 final var group
= getRemoteObject(signal
.getGroup(groupId
.serialize()), Signal
.Group
.class);
244 if (updateGroup
.getName() != null) {
245 group
.Set("org.asamk.Signal.Group", "Name", updateGroup
.getName());
247 if (updateGroup
.getDescription() != null) {
248 group
.Set("org.asamk.Signal.Group", "Description", updateGroup
.getDescription());
250 if (updateGroup
.getAvatarFile() != null) {
251 group
.Set("org.asamk.Signal.Group",
253 updateGroup
.getAvatarFile() == null ?
"" : updateGroup
.getAvatarFile().getPath());
255 if (updateGroup
.getExpirationTimer() != null) {
256 group
.Set("org.asamk.Signal.Group", "MessageExpirationTimer", updateGroup
.getExpirationTimer());
258 if (updateGroup
.getAddMemberPermission() != null) {
259 group
.Set("org.asamk.Signal.Group", "PermissionAddMember", updateGroup
.getAddMemberPermission().name());
261 if (updateGroup
.getEditDetailsPermission() != null) {
262 group
.Set("org.asamk.Signal.Group", "PermissionEditDetails", updateGroup
.getEditDetailsPermission().name());
264 if (updateGroup
.getIsAnnouncementGroup() != null) {
265 group
.Set("org.asamk.Signal.Group",
266 "PermissionSendMessage",
267 updateGroup
.getIsAnnouncementGroup()
268 ? GroupPermission
.ONLY_ADMINS
.name()
269 : GroupPermission
.EVERY_MEMBER
.name());
271 if (updateGroup
.getMembers() != null) {
272 group
.addMembers(updateGroup
.getMembers().stream().map(RecipientIdentifier
.Single
::getIdentifier
).toList());
274 if (updateGroup
.getRemoveMembers() != null) {
275 group
.removeMembers(updateGroup
.getRemoveMembers()
277 .map(RecipientIdentifier
.Single
::getIdentifier
)
280 if (updateGroup
.getAdmins() != null) {
281 group
.addAdmins(updateGroup
.getAdmins().stream().map(RecipientIdentifier
.Single
::getIdentifier
).toList());
283 if (updateGroup
.getRemoveAdmins() != null) {
284 group
.removeAdmins(updateGroup
.getRemoveAdmins()
286 .map(RecipientIdentifier
.Single
::getIdentifier
)
289 if (updateGroup
.isResetGroupLink()) {
292 if (updateGroup
.getGroupLinkState() != null) {
293 switch (updateGroup
.getGroupLinkState()) {
294 case DISABLED
-> group
.disableLink();
295 case ENABLED
-> group
.enableLink(false);
296 case ENABLED_WITH_APPROVAL
-> group
.enableLink(true);
299 return new SendGroupMessageResults(0, List
.of());
303 public Pair
<GroupId
, SendGroupMessageResults
> joinGroup(final GroupInviteLinkUrl inviteLinkUrl
) throws IOException
, InactiveGroupLinkException
{
304 final var newGroupId
= signal
.joinGroup(inviteLinkUrl
.getUrl());
305 return new Pair
<>(GroupId
.unknownVersion(newGroupId
), new SendGroupMessageResults(0, List
.of()));
309 public SendMessageResults
sendTypingMessage(
310 final TypingAction action
, final Set
<RecipientIdentifier
> recipients
311 ) throws IOException
, NotAGroupMemberException
, GroupNotFoundException
, GroupSendingNotAllowedException
{
312 return handleMessage(recipients
, numbers
-> {
313 numbers
.forEach(n
-> signal
.sendTyping(n
, action
== TypingAction
.STOP
));
316 signal
.sendTyping(signal
.getSelfNumber(), action
== TypingAction
.STOP
);
319 signal
.sendGroupTyping(groupId
, action
== TypingAction
.STOP
);
325 public SendMessageResults
sendReadReceipt(
326 final RecipientIdentifier
.Single sender
, final List
<Long
> messageIds
328 signal
.sendReadReceipt(sender
.getIdentifier(), messageIds
);
329 return new SendMessageResults(0, Map
.of());
333 public SendMessageResults
sendViewedReceipt(
334 final RecipientIdentifier
.Single sender
, final List
<Long
> messageIds
336 signal
.sendViewedReceipt(sender
.getIdentifier(), messageIds
);
337 return new SendMessageResults(0, Map
.of());
341 public SendMessageResults
sendMessage(
342 final Message message
, final Set
<RecipientIdentifier
> recipients
343 ) throws IOException
, AttachmentInvalidException
, NotAGroupMemberException
, GroupNotFoundException
, GroupSendingNotAllowedException
{
344 return handleMessage(recipients
,
345 numbers
-> signal
.sendMessage(message
.messageText(), message
.attachments(), numbers
),
346 () -> signal
.sendNoteToSelfMessage(message
.messageText(), message
.attachments()),
347 groupId
-> signal
.sendGroupMessage(message
.messageText(), message
.attachments(), groupId
));
351 public SendMessageResults
sendRemoteDeleteMessage(
352 final long targetSentTimestamp
, final Set
<RecipientIdentifier
> recipients
353 ) throws IOException
, NotAGroupMemberException
, GroupNotFoundException
, GroupSendingNotAllowedException
{
354 return handleMessage(recipients
,
355 numbers
-> signal
.sendRemoteDeleteMessage(targetSentTimestamp
, numbers
),
356 () -> signal
.sendRemoteDeleteMessage(targetSentTimestamp
, signal
.getSelfNumber()),
357 groupId
-> signal
.sendGroupRemoteDeleteMessage(targetSentTimestamp
, groupId
));
361 public SendMessageResults
sendMessageReaction(
363 final boolean remove
,
364 final RecipientIdentifier
.Single targetAuthor
,
365 final long targetSentTimestamp
,
366 final Set
<RecipientIdentifier
> recipients
367 ) throws IOException
, NotAGroupMemberException
, GroupNotFoundException
, GroupSendingNotAllowedException
{
368 return handleMessage(recipients
,
369 numbers
-> signal
.sendMessageReaction(emoji
,
371 targetAuthor
.getIdentifier(),
374 () -> signal
.sendMessageReaction(emoji
,
376 targetAuthor
.getIdentifier(),
378 signal
.getSelfNumber()),
379 groupId
-> signal
.sendGroupMessageReaction(emoji
,
381 targetAuthor
.getIdentifier(),
387 public SendMessageResults
sendPaymentNotificationMessage(
388 final byte[] receipt
, final String note
, final RecipientIdentifier
.Single recipient
389 ) throws IOException
{
390 throw new UnsupportedOperationException();
394 public SendMessageResults
sendEndSessionMessage(final Set
<RecipientIdentifier
.Single
> recipients
) throws IOException
{
395 signal
.sendEndSessionMessage(recipients
.stream().map(RecipientIdentifier
.Single
::getIdentifier
).toList());
396 return new SendMessageResults(0, Map
.of());
400 public void deleteRecipient(final RecipientIdentifier
.Single recipient
) {
401 signal
.deleteRecipient(recipient
.getIdentifier());
405 public void deleteContact(final RecipientIdentifier
.Single recipient
) {
406 signal
.deleteContact(recipient
.getIdentifier());
410 public void setContactName(
411 final RecipientIdentifier
.Single recipient
, final String name
412 ) throws NotPrimaryDeviceException
{
413 signal
.setContactName(recipient
.getIdentifier(), name
);
417 public void setContactsBlocked(
418 final Collection
<RecipientIdentifier
.Single
> recipients
, final boolean blocked
419 ) throws NotPrimaryDeviceException
, IOException
{
420 for (final var recipient
: recipients
) {
421 signal
.setContactBlocked(recipient
.getIdentifier(), blocked
);
426 public void setGroupsBlocked(
427 final Collection
<GroupId
> groupIds
, final boolean blocked
428 ) throws GroupNotFoundException
, IOException
{
429 for (final var groupId
: groupIds
) {
430 setGroupProperty(groupId
, "IsBlocked", blocked
);
434 private void setGroupProperty(final GroupId groupId
, final String propertyName
, final boolean blocked
) {
435 final var group
= getRemoteObject(signal
.getGroup(groupId
.serialize()), Signal
.Group
.class);
436 group
.Set("org.asamk.Signal.Group", propertyName
, blocked
);
440 public void setExpirationTimer(
441 final RecipientIdentifier
.Single recipient
, final int messageExpirationTimer
442 ) throws IOException
{
443 signal
.setExpirationTimer(recipient
.getIdentifier(), messageExpirationTimer
);
447 public StickerPackUrl
uploadStickerPack(final File path
) throws IOException
, StickerPackInvalidException
{
449 return StickerPackUrl
.fromUri(new URI(signal
.uploadStickerPack(path
.getPath())));
450 } catch (URISyntaxException
| StickerPackUrl
.InvalidStickerPackLinkException e
) {
451 throw new AssertionError(e
);
456 public List
<StickerPack
> getStickerPacks() {
457 throw new UnsupportedOperationException();
461 public void requestAllSyncData() throws IOException
{
462 signal
.sendSyncRequest();
466 public void addReceiveHandler(final ReceiveMessageHandler handler
, final boolean isWeakListener
) {
467 synchronized (messageHandlers
) {
468 if (isWeakListener
) {
469 weakHandlers
.add(handler
);
471 if (messageHandlers
.size() == 0) {
472 installMessageHandlers();
474 messageHandlers
.add(handler
);
480 public void removeReceiveHandler(final ReceiveMessageHandler handler
) {
481 synchronized (messageHandlers
) {
482 weakHandlers
.remove(handler
);
483 messageHandlers
.remove(handler
);
484 if (messageHandlers
.size() == 0) {
485 uninstallMessageHandlers();
491 public boolean isReceiving() {
492 synchronized (messageHandlers
) {
493 return messageHandlers
.size() > 0;
498 public void receiveMessages(final ReceiveMessageHandler handler
) throws IOException
{
499 addReceiveHandler(handler
);
501 synchronized (this) {
504 } catch (InterruptedException ignored
) {
506 removeReceiveHandler(handler
);
510 public void receiveMessages(
511 final Duration timeout
, final ReceiveMessageHandler handler
512 ) throws IOException
{
513 final var lastMessage
= new AtomicLong(System
.currentTimeMillis());
515 final ReceiveMessageHandler receiveHandler
= (envelope
, e
) -> {
516 lastMessage
.set(System
.currentTimeMillis());
517 handler
.handleMessage(envelope
, e
);
519 addReceiveHandler(receiveHandler
);
522 final var sleepTimeRemaining
= timeout
.toMillis() - (System
.currentTimeMillis() - lastMessage
.get());
523 if (sleepTimeRemaining
< 0) {
526 Thread
.sleep(sleepTimeRemaining
);
527 } catch (InterruptedException ignored
) {
530 removeReceiveHandler(receiveHandler
);
534 public void setIgnoreAttachments(final boolean ignoreAttachments
) {
538 public boolean hasCaughtUpWithOldMessages() {
543 public boolean isContactBlocked(final RecipientIdentifier
.Single recipient
) {
544 return signal
.isContactBlocked(recipient
.getIdentifier());
548 public void sendContacts() throws IOException
{
549 signal
.sendContacts();
553 public List
<Recipient
> getRecipients(
554 final boolean onlyContacts
,
555 final Optional
<Boolean
> blocked
,
556 final Collection
<RecipientIdentifier
.Single
> addresses
,
557 final Optional
<String
> name
559 final var numbers
= addresses
.stream()
560 .filter(s
-> s
instanceof RecipientIdentifier
.Number
)
561 .map(s
-> ((RecipientIdentifier
.Number
) s
).number())
562 .collect(Collectors
.toSet());
563 return signal
.listNumbers().stream().filter(n
-> addresses
.isEmpty() || numbers
.contains(n
)).map(n
-> {
564 final var contactBlocked
= signal
.isContactBlocked(n
);
565 if (blocked
.isPresent() && blocked
.get() != contactBlocked
) {
568 final var contactName
= signal
.getContactName(n
);
569 if (onlyContacts
&& contactName
.length() == 0) {
572 if (name
.isPresent() && !name
.get().equals(contactName
)) {
575 return Recipient
.newBuilder()
576 .withAddress(new RecipientAddress(null, n
))
577 .withContact(new Contact(contactName
, null, null, 0, contactBlocked
, false, false))
579 }).filter(Objects
::nonNull
).toList();
583 public String
getContactOrProfileName(final RecipientIdentifier
.Single recipient
) {
584 return signal
.getContactName(recipient
.getIdentifier());
588 public Group
getGroup(final GroupId groupId
) {
589 final var groupPath
= signal
.getGroup(groupId
.serialize());
590 return getGroup(groupPath
);
593 @SuppressWarnings("unchecked")
594 private Group
getGroup(final DBusPath groupPath
) {
595 final var group
= getRemoteObject(groupPath
, Signal
.Group
.class).GetAll("org.asamk.Signal.Group");
596 final var id
= (byte[]) group
.get("Id").getValue();
598 return new Group(GroupId
.unknownVersion(id
),
599 (String
) group
.get("Name").getValue(),
600 (String
) group
.get("Description").getValue(),
601 GroupInviteLinkUrl
.fromUri((String
) group
.get("GroupInviteLink").getValue()),
602 ((List
<String
>) group
.get("Members").getValue()).stream()
603 .map(m
-> new RecipientAddress(null, m
))
604 .collect(Collectors
.toSet()),
605 ((List
<String
>) group
.get("PendingMembers").getValue()).stream()
606 .map(m
-> new RecipientAddress(null, m
))
607 .collect(Collectors
.toSet()),
608 ((List
<String
>) group
.get("RequestingMembers").getValue()).stream()
609 .map(m
-> new RecipientAddress(null, m
))
610 .collect(Collectors
.toSet()),
611 ((List
<String
>) group
.get("Admins").getValue()).stream()
612 .map(m
-> new RecipientAddress(null, m
))
613 .collect(Collectors
.toSet()),
614 ((List
<String
>) group
.get("Banned").getValue()).stream()
615 .map(m
-> new RecipientAddress(null, m
))
616 .collect(Collectors
.toSet()),
617 (boolean) group
.get("IsBlocked").getValue(),
618 (int) group
.get("MessageExpirationTimer").getValue(),
619 GroupPermission
.valueOf((String
) group
.get("PermissionAddMember").getValue()),
620 GroupPermission
.valueOf((String
) group
.get("PermissionEditDetails").getValue()),
621 GroupPermission
.valueOf((String
) group
.get("PermissionSendMessage").getValue()),
622 (boolean) group
.get("IsMember").getValue(),
623 (boolean) group
.get("IsAdmin").getValue());
624 } catch (GroupInviteLinkUrl
.InvalidGroupLinkException
| GroupInviteLinkUrl
.UnknownGroupLinkVersionException e
) {
625 throw new AssertionError(e
);
630 public List
<Identity
> getIdentities() {
631 throw new UnsupportedOperationException();
635 public List
<Identity
> getIdentities(final RecipientIdentifier
.Single recipient
) {
636 throw new UnsupportedOperationException();
640 public boolean trustIdentityVerified(final RecipientIdentifier
.Single recipient
, final byte[] fingerprint
) {
641 throw new UnsupportedOperationException();
645 public boolean trustIdentityVerifiedSafetyNumber(
646 final RecipientIdentifier
.Single recipient
, final String safetyNumber
648 throw new UnsupportedOperationException();
652 public boolean trustIdentityVerifiedSafetyNumber(
653 final RecipientIdentifier
.Single recipient
, final byte[] safetyNumber
655 throw new UnsupportedOperationException();
659 public boolean trustIdentityAllKeys(final RecipientIdentifier
.Single recipient
) {
660 throw new UnsupportedOperationException();
664 public void addAddressChangedListener(final Runnable listener
) {
668 public void addClosedListener(final Runnable listener
) {
669 synchronized (closedListeners
) {
670 closedListeners
.add(listener
);
675 public void close() {
676 synchronized (this) {
679 synchronized (messageHandlers
) {
680 if (messageHandlers
.size() > 0) {
681 uninstallMessageHandlers();
683 weakHandlers
.clear();
684 messageHandlers
.clear();
686 synchronized (closedListeners
) {
687 closedListeners
.forEach(Runnable
::run
);
688 closedListeners
.clear();
692 private SendMessageResults
handleMessage(
693 Set
<RecipientIdentifier
> recipients
,
694 Function
<List
<String
>, Long
> recipientsHandler
,
695 Supplier
<Long
> noteToSelfHandler
,
696 Function
<byte[], Long
> groupHandler
699 final var singleRecipients
= recipients
.stream()
700 .filter(r
-> r
instanceof RecipientIdentifier
.Single
)
701 .map(RecipientIdentifier
.Single
.class::cast
)
702 .map(RecipientIdentifier
.Single
::getIdentifier
)
704 if (singleRecipients
.size() > 0) {
705 timestamp
= recipientsHandler
.apply(singleRecipients
);
708 if (recipients
.contains(RecipientIdentifier
.NoteToSelf
.INSTANCE
)) {
709 timestamp
= noteToSelfHandler
.get();
711 final var groupRecipients
= recipients
.stream()
712 .filter(r
-> r
instanceof RecipientIdentifier
.Group
)
713 .map(RecipientIdentifier
.Group
.class::cast
)
714 .map(RecipientIdentifier
.Group
::groupId
)
716 for (final var groupId
: groupRecipients
) {
717 timestamp
= groupHandler
.apply(groupId
.serialize());
719 return new SendMessageResults(timestamp
, Map
.of());
722 private String
emptyIfNull(final String string
) {
723 return string
== null ?
"" : string
;
726 private <T
extends DBusInterface
> T
getRemoteObject(final DBusPath path
, final Class
<T
> type
) {
728 return connection
.getRemoteObject(DbusConfig
.getBusname(), path
.getPath(), type
);
729 } catch (DBusException e
) {
730 throw new AssertionError(e
);
734 private void installMessageHandlers() {
736 this.dbusMsgHandler
= messageReceived
-> {
737 final var extras
= messageReceived
.getExtras();
738 final var envelope
= new MessageEnvelope(Optional
.of(new RecipientAddress(null,
739 messageReceived
.getSender())),
741 messageReceived
.getTimestamp(),
747 Optional
.of(new MessageEnvelope
.Data(messageReceived
.getTimestamp(),
748 messageReceived
.getGroupId().length
> 0
749 ? Optional
.of(new MessageEnvelope
.Data
.GroupContext(GroupId
.unknownVersion(
750 messageReceived
.getGroupId()), false, 0))
753 Optional
.of(messageReceived
.getMessage()),
762 getAttachments(extras
),
770 notifyMessageHandlers(envelope
);
772 connection
.addSigHandler(Signal
.MessageReceivedV2
.class, signal
, this.dbusMsgHandler
);
774 this.dbusRcptHandler
= receiptReceived
-> {
775 final var type
= switch (receiptReceived
.getReceiptType()) {
776 case "read" -> MessageEnvelope
.Receipt
.Type
.READ
;
777 case "viewed" -> MessageEnvelope
.Receipt
.Type
.VIEWED
;
778 case "delivery" -> MessageEnvelope
.Receipt
.Type
.DELIVERY
;
779 default -> MessageEnvelope
.Receipt
.Type
.UNKNOWN
;
781 final var envelope
= new MessageEnvelope(Optional
.of(new RecipientAddress(null,
782 receiptReceived
.getSender())),
784 receiptReceived
.getTimestamp(),
788 Optional
.of(new MessageEnvelope
.Receipt(receiptReceived
.getTimestamp(),
790 List
.of(receiptReceived
.getTimestamp()))),
795 notifyMessageHandlers(envelope
);
797 connection
.addSigHandler(Signal
.ReceiptReceivedV2
.class, signal
, this.dbusRcptHandler
);
799 this.dbusSyncHandler
= syncReceived
-> {
800 final var extras
= syncReceived
.getExtras();
801 final var envelope
= new MessageEnvelope(Optional
.of(new RecipientAddress(null,
802 syncReceived
.getSource())),
804 syncReceived
.getTimestamp(),
811 Optional
.of(new MessageEnvelope
.Sync(Optional
.of(new MessageEnvelope
.Sync
.Sent(syncReceived
.getTimestamp(),
812 syncReceived
.getTimestamp(),
813 syncReceived
.getDestination().isEmpty()
815 : Optional
.of(new RecipientAddress(null, syncReceived
.getDestination())),
817 Optional
.of(new MessageEnvelope
.Data(syncReceived
.getTimestamp(),
818 syncReceived
.getGroupId().length
> 0
819 ? Optional
.of(new MessageEnvelope
.Data
.GroupContext(GroupId
.unknownVersion(
820 syncReceived
.getGroupId()), false, 0))
823 Optional
.of(syncReceived
.getMessage()),
832 getAttachments(extras
),
846 notifyMessageHandlers(envelope
);
848 connection
.addSigHandler(Signal
.SyncMessageReceivedV2
.class, signal
, this.dbusSyncHandler
);
849 } catch (DBusException e
) {
852 signal
.subscribeReceive();
855 private void notifyMessageHandlers(final MessageEnvelope envelope
) {
856 synchronized (messageHandlers
) {
857 Stream
.concat(messageHandlers
.stream(), weakHandlers
.stream())
858 .forEach(h
-> h
.handleMessage(envelope
, null));
862 private void uninstallMessageHandlers() {
864 signal
.unsubscribeReceive();
865 connection
.removeSigHandler(Signal
.MessageReceivedV2
.class, signal
, this.dbusMsgHandler
);
866 connection
.removeSigHandler(Signal
.ReceiptReceivedV2
.class, signal
, this.dbusRcptHandler
);
867 connection
.removeSigHandler(Signal
.SyncMessageReceivedV2
.class, signal
, this.dbusSyncHandler
);
868 } catch (DBusException e
) {
873 private List
<MessageEnvelope
.Data
.Attachment
> getAttachments(final Map
<String
, Variant
<?
>> extras
) {
874 if (!extras
.containsKey("attachments")) {
878 final List
<DBusMap
<String
, Variant
<?
>>> attachments
= getValue(extras
, "attachments");
879 return attachments
.stream().map(a
-> {
880 final String file
= a
.containsKey("file") ?
getValue(a
, "file") : null;
881 return new MessageEnvelope
.Data
.Attachment(a
.containsKey("remoteId")
882 ? Optional
.of(getValue(a
, "remoteId"))
884 file
!= null ? Optional
.of(new File(file
)) : Optional
.empty(),
886 getValue(a
, "contentType"),
894 getValue(a
, "isVoiceNote"),
895 getValue(a
, "isGif"),
896 getValue(a
, "isBorderless"));
900 @SuppressWarnings("unchecked")
901 private <T
> T
getValue(
902 final Map
<String
, Variant
<?
>> stringVariantMap
, final String field
904 return (T
) stringVariantMap
.get(field
).getValue();