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
.ReceiveConfig
;
18 import org
.asamk
.signal
.manager
.api
.Recipient
;
19 import org
.asamk
.signal
.manager
.api
.RecipientAddress
;
20 import org
.asamk
.signal
.manager
.api
.RecipientIdentifier
;
21 import org
.asamk
.signal
.manager
.api
.SendGroupMessageResults
;
22 import org
.asamk
.signal
.manager
.api
.SendMessageResults
;
23 import org
.asamk
.signal
.manager
.api
.StickerPack
;
24 import org
.asamk
.signal
.manager
.api
.StickerPackInvalidException
;
25 import org
.asamk
.signal
.manager
.api
.StickerPackUrl
;
26 import org
.asamk
.signal
.manager
.api
.TypingAction
;
27 import org
.asamk
.signal
.manager
.api
.UpdateGroup
;
28 import org
.asamk
.signal
.manager
.api
.UpdateProfile
;
29 import org
.asamk
.signal
.manager
.api
.UserStatus
;
30 import org
.asamk
.signal
.manager
.groups
.GroupId
;
31 import org
.asamk
.signal
.manager
.groups
.GroupInviteLinkUrl
;
32 import org
.asamk
.signal
.manager
.groups
.GroupNotFoundException
;
33 import org
.asamk
.signal
.manager
.groups
.GroupPermission
;
34 import org
.asamk
.signal
.manager
.groups
.GroupSendingNotAllowedException
;
35 import org
.asamk
.signal
.manager
.groups
.LastGroupAdminException
;
36 import org
.asamk
.signal
.manager
.groups
.NotAGroupMemberException
;
37 import org
.asamk
.signal
.manager
.storage
.recipients
.Contact
;
38 import org
.asamk
.signal
.manager
.storage
.recipients
.Profile
;
39 import org
.freedesktop
.dbus
.DBusMap
;
40 import org
.freedesktop
.dbus
.DBusPath
;
41 import org
.freedesktop
.dbus
.connections
.impl
.DBusConnection
;
42 import org
.freedesktop
.dbus
.exceptions
.DBusException
;
43 import org
.freedesktop
.dbus
.interfaces
.DBusInterface
;
44 import org
.freedesktop
.dbus
.interfaces
.DBusSigHandler
;
45 import org
.freedesktop
.dbus
.types
.Variant
;
48 import java
.io
.IOException
;
50 import java
.net
.URISyntaxException
;
51 import java
.time
.Duration
;
52 import java
.util
.ArrayList
;
53 import java
.util
.Collection
;
54 import java
.util
.HashMap
;
55 import java
.util
.HashSet
;
56 import java
.util
.List
;
58 import java
.util
.Objects
;
59 import java
.util
.Optional
;
61 import java
.util
.concurrent
.atomic
.AtomicInteger
;
62 import java
.util
.concurrent
.atomic
.AtomicLong
;
63 import java
.util
.function
.Function
;
64 import java
.util
.function
.Supplier
;
65 import java
.util
.stream
.Collectors
;
66 import java
.util
.stream
.Stream
;
69 * This class implements the Manager interface using the DBus Signal interface, where possible.
70 * It's used for the signal-cli dbus client mode (--dbus, --dbus-system)
72 public class DbusManagerImpl
implements Manager
{
74 private final Signal signal
;
75 private final DBusConnection connection
;
77 private final Set
<ReceiveMessageHandler
> weakHandlers
= new HashSet
<>();
78 private final Set
<ReceiveMessageHandler
> messageHandlers
= new HashSet
<>();
79 private final List
<Runnable
> closedListeners
= new ArrayList
<>();
80 private DBusSigHandler
<Signal
.MessageReceivedV2
> dbusMsgHandler
;
81 private DBusSigHandler
<Signal
.ReceiptReceivedV2
> dbusRcptHandler
;
82 private DBusSigHandler
<Signal
.SyncMessageReceivedV2
> dbusSyncHandler
;
84 public DbusManagerImpl(final Signal signal
, DBusConnection connection
) {
86 this.connection
= connection
;
90 public String
getSelfNumber() {
91 return signal
.getSelfNumber();
95 public Map
<String
, UserStatus
> getUserStatus(final Set
<String
> numbers
) throws IOException
{
96 final var numbersList
= new ArrayList
<>(numbers
);
97 final var registered
= signal
.isRegistered(numbersList
);
99 final var result
= new HashMap
<String
, UserStatus
>();
100 for (var i
= 0; i
< numbersList
.size(); i
++) {
101 result
.put(numbersList
.get(i
),
102 new UserStatus(numbersList
.get(i
),
103 registered
.get(i
) ? RecipientAddress
.UNKNOWN_UUID
: null,
110 public void updateAccountAttributes(final String deviceName
) throws IOException
{
111 if (deviceName
!= null) {
112 final var devicePath
= signal
.getThisDevice();
113 getRemoteObject(devicePath
, Signal
.Device
.class).Set("org.asamk.Signal.Device", "Name", deviceName
);
118 public Configuration
getConfiguration() {
119 final var configuration
= getRemoteObject(new DBusPath(signal
.getObjectPath() + "/Configuration"),
120 Signal
.Configuration
.class).GetAll("org.asamk.Signal.Configuration");
121 return new Configuration(Optional
.of((Boolean
) configuration
.get("ReadReceipts").getValue()),
122 Optional
.of((Boolean
) configuration
.get("UnidentifiedDeliveryIndicators").getValue()),
123 Optional
.of((Boolean
) configuration
.get("TypingIndicators").getValue()),
124 Optional
.of((Boolean
) configuration
.get("LinkPreviews").getValue()));
128 public void updateConfiguration(Configuration newConfiguration
) throws IOException
{
129 final var configuration
= getRemoteObject(new DBusPath(signal
.getObjectPath() + "/Configuration"),
130 Signal
.Configuration
.class);
131 newConfiguration
.readReceipts()
132 .ifPresent(v
-> configuration
.Set("org.asamk.Signal.Configuration", "ReadReceipts", v
));
133 newConfiguration
.unidentifiedDeliveryIndicators()
134 .ifPresent(v
-> configuration
.Set("org.asamk.Signal.Configuration",
135 "UnidentifiedDeliveryIndicators",
137 newConfiguration
.typingIndicators()
138 .ifPresent(v
-> configuration
.Set("org.asamk.Signal.Configuration", "TypingIndicators", v
));
139 newConfiguration
.linkPreviews()
140 .ifPresent(v
-> configuration
.Set("org.asamk.Signal.Configuration", "LinkPreviews", v
));
144 public void updateProfile(UpdateProfile updateProfile
) throws IOException
{
145 signal
.updateProfile(emptyIfNull(updateProfile
.getGivenName()),
146 emptyIfNull(updateProfile
.getFamilyName()),
147 emptyIfNull(updateProfile
.getAbout()),
148 emptyIfNull(updateProfile
.getAboutEmoji()),
149 updateProfile
.getAvatar() == null ?
"" : updateProfile
.getAvatar().getPath(),
150 updateProfile
.isDeleteAvatar());
154 public void unregister() throws IOException
{
159 public void deleteAccount() throws IOException
{
160 signal
.deleteAccount();
164 public void submitRateLimitRecaptchaChallenge(final String challenge
, final String captcha
) throws IOException
{
165 signal
.submitRateLimitChallenge(challenge
, captcha
);
169 public List
<Device
> getLinkedDevices() throws IOException
{
170 final var thisDevice
= signal
.getThisDevice();
171 return signal
.listDevices().stream().map(d
-> {
172 final var device
= getRemoteObject(d
.getObjectPath(),
173 Signal
.Device
.class).GetAll("org.asamk.Signal.Device");
174 return new Device((Integer
) device
.get("Id").getValue(),
175 (String
) device
.get("Name").getValue(),
176 (long) device
.get("Created").getValue(),
177 (long) device
.get("LastSeen").getValue(),
178 thisDevice
.equals(d
.getObjectPath()));
183 public void removeLinkedDevices(final int deviceId
) throws IOException
{
184 final var devicePath
= signal
.getDevice(deviceId
);
185 getRemoteObject(devicePath
, Signal
.Device
.class).removeDevice();
189 public void addDeviceLink(final URI linkUri
) throws IOException
, InvalidDeviceLinkException
{
190 signal
.addDevice(linkUri
.toString());
194 public void setRegistrationLockPin(final Optional
<String
> pin
) throws IOException
{
195 if (pin
.isPresent()) {
196 signal
.setPin(pin
.get());
203 public Profile
getRecipientProfile(final RecipientIdentifier
.Single recipient
) {
204 throw new UnsupportedOperationException();
208 public List
<Group
> getGroups() {
209 final var groups
= signal
.listGroups();
210 return groups
.stream().map(Signal
.StructGroup
::getObjectPath
).map(this::getGroup
).toList();
214 public SendGroupMessageResults
quitGroup(
215 final GroupId groupId
, final Set
<RecipientIdentifier
.Single
> groupAdmins
216 ) throws GroupNotFoundException
, IOException
, NotAGroupMemberException
, LastGroupAdminException
{
217 if (groupAdmins
.size() > 0) {
218 throw new UnsupportedOperationException();
220 final var group
= getRemoteObject(signal
.getGroup(groupId
.serialize()), Signal
.Group
.class);
222 return new SendGroupMessageResults(0, List
.of());
226 public void deleteGroup(final GroupId groupId
) throws IOException
{
227 final var group
= getRemoteObject(signal
.getGroup(groupId
.serialize()), Signal
.Group
.class);
232 public Pair
<GroupId
, SendGroupMessageResults
> createGroup(
233 final String name
, final Set
<RecipientIdentifier
.Single
> members
, final File avatarFile
234 ) throws IOException
, AttachmentInvalidException
{
235 final var newGroupId
= signal
.createGroup(emptyIfNull(name
),
236 members
.stream().map(RecipientIdentifier
.Single
::getIdentifier
).toList(),
237 avatarFile
== null ?
"" : avatarFile
.getPath());
238 return new Pair
<>(GroupId
.unknownVersion(newGroupId
), new SendGroupMessageResults(0, List
.of()));
242 public SendGroupMessageResults
updateGroup(
243 final GroupId groupId
, final UpdateGroup updateGroup
244 ) throws IOException
, GroupNotFoundException
, AttachmentInvalidException
, NotAGroupMemberException
, GroupSendingNotAllowedException
{
245 final var group
= getRemoteObject(signal
.getGroup(groupId
.serialize()), Signal
.Group
.class);
246 if (updateGroup
.getName() != null) {
247 group
.Set("org.asamk.Signal.Group", "Name", updateGroup
.getName());
249 if (updateGroup
.getDescription() != null) {
250 group
.Set("org.asamk.Signal.Group", "Description", updateGroup
.getDescription());
252 if (updateGroup
.getAvatarFile() != null) {
253 group
.Set("org.asamk.Signal.Group",
255 updateGroup
.getAvatarFile() == null ?
"" : updateGroup
.getAvatarFile().getPath());
257 if (updateGroup
.getExpirationTimer() != null) {
258 group
.Set("org.asamk.Signal.Group", "MessageExpirationTimer", updateGroup
.getExpirationTimer());
260 if (updateGroup
.getAddMemberPermission() != null) {
261 group
.Set("org.asamk.Signal.Group", "PermissionAddMember", updateGroup
.getAddMemberPermission().name());
263 if (updateGroup
.getEditDetailsPermission() != null) {
264 group
.Set("org.asamk.Signal.Group", "PermissionEditDetails", updateGroup
.getEditDetailsPermission().name());
266 if (updateGroup
.getIsAnnouncementGroup() != null) {
267 group
.Set("org.asamk.Signal.Group",
268 "PermissionSendMessage",
269 updateGroup
.getIsAnnouncementGroup()
270 ? GroupPermission
.ONLY_ADMINS
.name()
271 : GroupPermission
.EVERY_MEMBER
.name());
273 if (updateGroup
.getMembers() != null) {
274 group
.addMembers(updateGroup
.getMembers().stream().map(RecipientIdentifier
.Single
::getIdentifier
).toList());
276 if (updateGroup
.getRemoveMembers() != null) {
277 group
.removeMembers(updateGroup
.getRemoveMembers()
279 .map(RecipientIdentifier
.Single
::getIdentifier
)
282 if (updateGroup
.getAdmins() != null) {
283 group
.addAdmins(updateGroup
.getAdmins().stream().map(RecipientIdentifier
.Single
::getIdentifier
).toList());
285 if (updateGroup
.getRemoveAdmins() != null) {
286 group
.removeAdmins(updateGroup
.getRemoveAdmins()
288 .map(RecipientIdentifier
.Single
::getIdentifier
)
291 if (updateGroup
.isResetGroupLink()) {
294 if (updateGroup
.getGroupLinkState() != null) {
295 switch (updateGroup
.getGroupLinkState()) {
296 case DISABLED
-> group
.disableLink();
297 case ENABLED
-> group
.enableLink(false);
298 case ENABLED_WITH_APPROVAL
-> group
.enableLink(true);
301 return new SendGroupMessageResults(0, List
.of());
305 public Pair
<GroupId
, SendGroupMessageResults
> joinGroup(final GroupInviteLinkUrl inviteLinkUrl
) throws IOException
, InactiveGroupLinkException
{
306 final var newGroupId
= signal
.joinGroup(inviteLinkUrl
.getUrl());
307 return new Pair
<>(GroupId
.unknownVersion(newGroupId
), new SendGroupMessageResults(0, List
.of()));
311 public SendMessageResults
sendTypingMessage(
312 final TypingAction action
, final Set
<RecipientIdentifier
> recipients
313 ) throws IOException
, NotAGroupMemberException
, GroupNotFoundException
, GroupSendingNotAllowedException
{
314 return handleMessage(recipients
, numbers
-> {
315 numbers
.forEach(n
-> signal
.sendTyping(n
, action
== TypingAction
.STOP
));
318 signal
.sendTyping(signal
.getSelfNumber(), action
== TypingAction
.STOP
);
321 signal
.sendGroupTyping(groupId
, action
== TypingAction
.STOP
);
327 public SendMessageResults
sendReadReceipt(
328 final RecipientIdentifier
.Single sender
, final List
<Long
> messageIds
330 signal
.sendReadReceipt(sender
.getIdentifier(), messageIds
);
331 return new SendMessageResults(0, Map
.of());
335 public SendMessageResults
sendViewedReceipt(
336 final RecipientIdentifier
.Single sender
, final List
<Long
> messageIds
338 signal
.sendViewedReceipt(sender
.getIdentifier(), messageIds
);
339 return new SendMessageResults(0, Map
.of());
343 public SendMessageResults
sendMessage(
344 final Message message
, final Set
<RecipientIdentifier
> recipients
345 ) throws IOException
, AttachmentInvalidException
, NotAGroupMemberException
, GroupNotFoundException
, GroupSendingNotAllowedException
{
346 return handleMessage(recipients
,
347 numbers
-> signal
.sendMessage(message
.messageText(), message
.attachments(), numbers
),
348 () -> signal
.sendNoteToSelfMessage(message
.messageText(), message
.attachments()),
349 groupId
-> signal
.sendGroupMessage(message
.messageText(), message
.attachments(), groupId
));
353 public SendMessageResults
sendRemoteDeleteMessage(
354 final long targetSentTimestamp
, final Set
<RecipientIdentifier
> recipients
355 ) throws IOException
, NotAGroupMemberException
, GroupNotFoundException
, GroupSendingNotAllowedException
{
356 return handleMessage(recipients
,
357 numbers
-> signal
.sendRemoteDeleteMessage(targetSentTimestamp
, numbers
),
358 () -> signal
.sendRemoteDeleteMessage(targetSentTimestamp
, signal
.getSelfNumber()),
359 groupId
-> signal
.sendGroupRemoteDeleteMessage(targetSentTimestamp
, groupId
));
363 public SendMessageResults
sendMessageReaction(
365 final boolean remove
,
366 final RecipientIdentifier
.Single targetAuthor
,
367 final long targetSentTimestamp
,
368 final Set
<RecipientIdentifier
> recipients
,
369 final boolean isStory
370 ) throws IOException
, NotAGroupMemberException
, GroupNotFoundException
, GroupSendingNotAllowedException
{
371 return handleMessage(recipients
,
372 numbers
-> signal
.sendMessageReaction(emoji
,
374 targetAuthor
.getIdentifier(),
377 () -> signal
.sendMessageReaction(emoji
,
379 targetAuthor
.getIdentifier(),
381 signal
.getSelfNumber()),
382 groupId
-> signal
.sendGroupMessageReaction(emoji
,
384 targetAuthor
.getIdentifier(),
390 public SendMessageResults
sendPaymentNotificationMessage(
391 final byte[] receipt
, final String note
, final RecipientIdentifier
.Single recipient
392 ) throws IOException
{
393 final var timestamp
= signal
.sendPaymentNotification(receipt
, note
, recipient
.getIdentifier());
394 return new SendMessageResults(timestamp
, Map
.of());
398 public SendMessageResults
sendEndSessionMessage(final Set
<RecipientIdentifier
.Single
> recipients
) throws IOException
{
399 signal
.sendEndSessionMessage(recipients
.stream().map(RecipientIdentifier
.Single
::getIdentifier
).toList());
400 return new SendMessageResults(0, Map
.of());
404 public void deleteRecipient(final RecipientIdentifier
.Single recipient
) {
405 signal
.deleteRecipient(recipient
.getIdentifier());
409 public void deleteContact(final RecipientIdentifier
.Single recipient
) {
410 signal
.deleteContact(recipient
.getIdentifier());
414 public void setContactName(
415 final RecipientIdentifier
.Single recipient
, final String givenName
, final String familyName
416 ) throws NotPrimaryDeviceException
{
417 signal
.setContactName(recipient
.getIdentifier(), givenName
);
421 public void setContactsBlocked(
422 final Collection
<RecipientIdentifier
.Single
> recipients
, final boolean blocked
423 ) throws NotPrimaryDeviceException
, IOException
{
424 for (final var recipient
: recipients
) {
425 signal
.setContactBlocked(recipient
.getIdentifier(), blocked
);
430 public void setGroupsBlocked(
431 final Collection
<GroupId
> groupIds
, final boolean blocked
432 ) throws GroupNotFoundException
, IOException
{
433 for (final var groupId
: groupIds
) {
434 setGroupProperty(groupId
, "IsBlocked", blocked
);
438 private void setGroupProperty(final GroupId groupId
, final String propertyName
, final boolean blocked
) {
439 final var group
= getRemoteObject(signal
.getGroup(groupId
.serialize()), Signal
.Group
.class);
440 group
.Set("org.asamk.Signal.Group", propertyName
, blocked
);
444 public void setExpirationTimer(
445 final RecipientIdentifier
.Single recipient
, final int messageExpirationTimer
446 ) throws IOException
{
447 signal
.setExpirationTimer(recipient
.getIdentifier(), messageExpirationTimer
);
451 public StickerPackUrl
uploadStickerPack(final File path
) throws IOException
, StickerPackInvalidException
{
453 return StickerPackUrl
.fromUri(new URI(signal
.uploadStickerPack(path
.getPath())));
454 } catch (URISyntaxException
| StickerPackUrl
.InvalidStickerPackLinkException e
) {
455 throw new AssertionError(e
);
460 public List
<StickerPack
> getStickerPacks() {
461 throw new UnsupportedOperationException();
465 public void requestAllSyncData() throws IOException
{
466 signal
.sendSyncRequest();
470 public void addReceiveHandler(final ReceiveMessageHandler handler
, final boolean isWeakListener
) {
471 synchronized (messageHandlers
) {
472 if (isWeakListener
) {
473 weakHandlers
.add(handler
);
475 if (messageHandlers
.size() == 0) {
476 installMessageHandlers();
478 messageHandlers
.add(handler
);
484 public void removeReceiveHandler(final ReceiveMessageHandler handler
) {
485 synchronized (messageHandlers
) {
486 weakHandlers
.remove(handler
);
487 messageHandlers
.remove(handler
);
488 if (messageHandlers
.size() == 0) {
489 uninstallMessageHandlers();
495 public boolean isReceiving() {
496 synchronized (messageHandlers
) {
497 return messageHandlers
.size() > 0;
502 public void receiveMessages(
503 Optional
<Duration
> timeout
, Optional
<Integer
> maxMessages
, ReceiveMessageHandler handler
504 ) throws IOException
{
505 final var remainingMessages
= new AtomicInteger(maxMessages
.orElse(-1));
506 final var lastMessage
= new AtomicLong(System
.currentTimeMillis());
507 final var thread
= Thread
.currentThread();
509 final ReceiveMessageHandler receiveHandler
= (envelope
, e
) -> {
510 lastMessage
.set(System
.currentTimeMillis());
511 handler
.handleMessage(envelope
, e
);
512 if (remainingMessages
.get() > 0) {
513 if (remainingMessages
.decrementAndGet() <= 0) {
514 remainingMessages
.set(0);
519 addReceiveHandler(receiveHandler
);
520 if (timeout
.isPresent()) {
521 while (remainingMessages
.get() != 0) {
523 final var passedTime
= System
.currentTimeMillis() - lastMessage
.get();
524 final var sleepTimeRemaining
= timeout
.get().toMillis() - passedTime
;
525 if (sleepTimeRemaining
< 0) {
528 Thread
.sleep(sleepTimeRemaining
);
529 } catch (InterruptedException ignored
) {
534 synchronized (this) {
537 } catch (InterruptedException ignored
) {
541 removeReceiveHandler(receiveHandler
);
545 public void setReceiveConfig(final ReceiveConfig receiveConfig
) {
549 public boolean hasCaughtUpWithOldMessages() {
554 public boolean isContactBlocked(final RecipientIdentifier
.Single recipient
) {
555 return signal
.isContactBlocked(recipient
.getIdentifier());
559 public void sendContacts() throws IOException
{
560 signal
.sendContacts();
564 public List
<Recipient
> getRecipients(
565 final boolean onlyContacts
,
566 final Optional
<Boolean
> blocked
,
567 final Collection
<RecipientIdentifier
.Single
> addresses
,
568 final Optional
<String
> name
570 final var numbers
= addresses
.stream()
571 .filter(s
-> s
instanceof RecipientIdentifier
.Number
)
572 .map(s
-> ((RecipientIdentifier
.Number
) s
).number())
573 .collect(Collectors
.toSet());
574 return signal
.listNumbers().stream().filter(n
-> addresses
.isEmpty() || numbers
.contains(n
)).map(n
-> {
575 final var contactBlocked
= signal
.isContactBlocked(n
);
576 if (blocked
.isPresent() && blocked
.get() != contactBlocked
) {
579 final var contactName
= signal
.getContactName(n
);
580 if (onlyContacts
&& contactName
.length() == 0) {
583 if (name
.isPresent() && !name
.get().equals(contactName
)) {
586 return Recipient
.newBuilder()
587 .withAddress(new RecipientAddress(null, n
))
588 .withContact(new Contact(contactName
, null, null, 0, contactBlocked
, false, false))
590 }).filter(Objects
::nonNull
).toList();
594 public String
getContactOrProfileName(final RecipientIdentifier
.Single recipient
) {
595 return signal
.getContactName(recipient
.getIdentifier());
599 public Group
getGroup(final GroupId groupId
) {
600 final var groupPath
= signal
.getGroup(groupId
.serialize());
601 return getGroup(groupPath
);
604 @SuppressWarnings("unchecked")
605 private Group
getGroup(final DBusPath groupPath
) {
606 final var group
= getRemoteObject(groupPath
, Signal
.Group
.class).GetAll("org.asamk.Signal.Group");
607 final var id
= (byte[]) group
.get("Id").getValue();
609 return new Group(GroupId
.unknownVersion(id
),
610 (String
) group
.get("Name").getValue(),
611 (String
) group
.get("Description").getValue(),
612 GroupInviteLinkUrl
.fromUri((String
) group
.get("GroupInviteLink").getValue()),
613 ((List
<String
>) group
.get("Members").getValue()).stream()
614 .map(m
-> new RecipientAddress(null, m
))
615 .collect(Collectors
.toSet()),
616 ((List
<String
>) group
.get("PendingMembers").getValue()).stream()
617 .map(m
-> new RecipientAddress(null, m
))
618 .collect(Collectors
.toSet()),
619 ((List
<String
>) group
.get("RequestingMembers").getValue()).stream()
620 .map(m
-> new RecipientAddress(null, m
))
621 .collect(Collectors
.toSet()),
622 ((List
<String
>) group
.get("Admins").getValue()).stream()
623 .map(m
-> new RecipientAddress(null, m
))
624 .collect(Collectors
.toSet()),
625 ((List
<String
>) group
.get("Banned").getValue()).stream()
626 .map(m
-> new RecipientAddress(null, m
))
627 .collect(Collectors
.toSet()),
628 (boolean) group
.get("IsBlocked").getValue(),
629 (int) group
.get("MessageExpirationTimer").getValue(),
630 GroupPermission
.valueOf((String
) group
.get("PermissionAddMember").getValue()),
631 GroupPermission
.valueOf((String
) group
.get("PermissionEditDetails").getValue()),
632 GroupPermission
.valueOf((String
) group
.get("PermissionSendMessage").getValue()),
633 (boolean) group
.get("IsMember").getValue(),
634 (boolean) group
.get("IsAdmin").getValue());
635 } catch (GroupInviteLinkUrl
.InvalidGroupLinkException
| GroupInviteLinkUrl
.UnknownGroupLinkVersionException e
) {
636 throw new AssertionError(e
);
641 public List
<Identity
> getIdentities() {
642 throw new UnsupportedOperationException();
646 public List
<Identity
> getIdentities(final RecipientIdentifier
.Single recipient
) {
647 throw new UnsupportedOperationException();
651 public boolean trustIdentityVerified(final RecipientIdentifier
.Single recipient
, final byte[] fingerprint
) {
652 throw new UnsupportedOperationException();
656 public boolean trustIdentityVerifiedSafetyNumber(
657 final RecipientIdentifier
.Single recipient
, final String safetyNumber
659 throw new UnsupportedOperationException();
663 public boolean trustIdentityVerifiedSafetyNumber(
664 final RecipientIdentifier
.Single recipient
, final byte[] safetyNumber
666 throw new UnsupportedOperationException();
670 public boolean trustIdentityAllKeys(final RecipientIdentifier
.Single recipient
) {
671 throw new UnsupportedOperationException();
675 public void addAddressChangedListener(final Runnable listener
) {
679 public void addClosedListener(final Runnable listener
) {
680 synchronized (closedListeners
) {
681 closedListeners
.add(listener
);
686 public void close() {
687 synchronized (this) {
690 synchronized (messageHandlers
) {
691 if (messageHandlers
.size() > 0) {
692 uninstallMessageHandlers();
694 weakHandlers
.clear();
695 messageHandlers
.clear();
697 synchronized (closedListeners
) {
698 closedListeners
.forEach(Runnable
::run
);
699 closedListeners
.clear();
703 private SendMessageResults
handleMessage(
704 Set
<RecipientIdentifier
> recipients
,
705 Function
<List
<String
>, Long
> recipientsHandler
,
706 Supplier
<Long
> noteToSelfHandler
,
707 Function
<byte[], Long
> groupHandler
710 final var singleRecipients
= recipients
.stream()
711 .filter(r
-> r
instanceof RecipientIdentifier
.Single
)
712 .map(RecipientIdentifier
.Single
.class::cast
)
713 .map(RecipientIdentifier
.Single
::getIdentifier
)
715 if (singleRecipients
.size() > 0) {
716 timestamp
= recipientsHandler
.apply(singleRecipients
);
719 if (recipients
.contains(RecipientIdentifier
.NoteToSelf
.INSTANCE
)) {
720 timestamp
= noteToSelfHandler
.get();
722 final var groupRecipients
= recipients
.stream()
723 .filter(r
-> r
instanceof RecipientIdentifier
.Group
)
724 .map(RecipientIdentifier
.Group
.class::cast
)
725 .map(RecipientIdentifier
.Group
::groupId
)
727 for (final var groupId
: groupRecipients
) {
728 timestamp
= groupHandler
.apply(groupId
.serialize());
730 return new SendMessageResults(timestamp
, Map
.of());
733 private String
emptyIfNull(final String string
) {
734 return string
== null ?
"" : string
;
737 private <T
extends DBusInterface
> T
getRemoteObject(final DBusPath path
, final Class
<T
> type
) {
739 return connection
.getRemoteObject(DbusConfig
.getBusname(), path
.getPath(), type
);
740 } catch (DBusException e
) {
741 throw new AssertionError(e
);
745 private void installMessageHandlers() {
747 this.dbusMsgHandler
= messageReceived
-> {
748 final var extras
= messageReceived
.getExtras();
749 final var envelope
= new MessageEnvelope(Optional
.of(new RecipientAddress(null,
750 messageReceived
.getSender())),
752 messageReceived
.getTimestamp(),
758 Optional
.of(new MessageEnvelope
.Data(messageReceived
.getTimestamp(),
759 messageReceived
.getGroupId().length
> 0
760 ? Optional
.of(new MessageEnvelope
.Data
.GroupContext(GroupId
.unknownVersion(
761 messageReceived
.getGroupId()), false, 0))
765 Optional
.of(messageReceived
.getMessage()),
775 getAttachments(extras
),
784 notifyMessageHandlers(envelope
);
786 connection
.addSigHandler(Signal
.MessageReceivedV2
.class, signal
, this.dbusMsgHandler
);
788 this.dbusRcptHandler
= receiptReceived
-> {
789 final var type
= switch (receiptReceived
.getReceiptType()) {
790 case "read" -> MessageEnvelope
.Receipt
.Type
.READ
;
791 case "viewed" -> MessageEnvelope
.Receipt
.Type
.VIEWED
;
792 case "delivery" -> MessageEnvelope
.Receipt
.Type
.DELIVERY
;
793 default -> MessageEnvelope
.Receipt
.Type
.UNKNOWN
;
795 final var envelope
= new MessageEnvelope(Optional
.of(new RecipientAddress(null,
796 receiptReceived
.getSender())),
798 receiptReceived
.getTimestamp(),
802 Optional
.of(new MessageEnvelope
.Receipt(receiptReceived
.getTimestamp(),
804 List
.of(receiptReceived
.getTimestamp()))),
810 notifyMessageHandlers(envelope
);
812 connection
.addSigHandler(Signal
.ReceiptReceivedV2
.class, signal
, this.dbusRcptHandler
);
814 this.dbusSyncHandler
= syncReceived
-> {
815 final var extras
= syncReceived
.getExtras();
816 final var envelope
= new MessageEnvelope(Optional
.of(new RecipientAddress(null,
817 syncReceived
.getSource())),
819 syncReceived
.getTimestamp(),
826 Optional
.of(new MessageEnvelope
.Sync(Optional
.of(new MessageEnvelope
.Sync
.Sent(syncReceived
.getTimestamp(),
827 syncReceived
.getTimestamp(),
828 syncReceived
.getDestination().isEmpty()
830 : Optional
.of(new RecipientAddress(null, syncReceived
.getDestination())),
832 Optional
.of(new MessageEnvelope
.Data(syncReceived
.getTimestamp(),
833 syncReceived
.getGroupId().length
> 0
834 ? Optional
.of(new MessageEnvelope
.Data
.GroupContext(GroupId
.unknownVersion(
835 syncReceived
.getGroupId()), false, 0))
839 Optional
.of(syncReceived
.getMessage()),
849 getAttachments(extras
),
865 notifyMessageHandlers(envelope
);
867 connection
.addSigHandler(Signal
.SyncMessageReceivedV2
.class, signal
, this.dbusSyncHandler
);
868 } catch (DBusException e
) {
871 signal
.subscribeReceive();
874 private void notifyMessageHandlers(final MessageEnvelope envelope
) {
875 synchronized (messageHandlers
) {
876 Stream
.concat(messageHandlers
.stream(), weakHandlers
.stream())
877 .forEach(h
-> h
.handleMessage(envelope
, null));
881 private void uninstallMessageHandlers() {
883 signal
.unsubscribeReceive();
884 connection
.removeSigHandler(Signal
.MessageReceivedV2
.class, signal
, this.dbusMsgHandler
);
885 connection
.removeSigHandler(Signal
.ReceiptReceivedV2
.class, signal
, this.dbusRcptHandler
);
886 connection
.removeSigHandler(Signal
.SyncMessageReceivedV2
.class, signal
, this.dbusSyncHandler
);
887 } catch (DBusException e
) {
892 private List
<MessageEnvelope
.Data
.Attachment
> getAttachments(final Map
<String
, Variant
<?
>> extras
) {
893 if (!extras
.containsKey("attachments")) {
897 final List
<DBusMap
<String
, Variant
<?
>>> attachments
= getValue(extras
, "attachments");
898 return attachments
.stream().map(a
-> {
899 final String file
= a
.containsKey("file") ?
getValue(a
, "file") : null;
900 return new MessageEnvelope
.Data
.Attachment(a
.containsKey("remoteId")
901 ? Optional
.of(getValue(a
, "remoteId"))
903 file
!= null ? Optional
.of(new File(file
)) : Optional
.empty(),
905 getValue(a
, "contentType"),
913 getValue(a
, "isVoiceNote"),
914 getValue(a
, "isGif"),
915 getValue(a
, "isBorderless"));
919 @SuppressWarnings("unchecked")
920 private <T
> T
getValue(
921 final Map
<String
, Variant
<?
>> stringVariantMap
, final String field
923 return (T
) stringVariantMap
.get(field
).getValue();