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
.AtomicLong
;
62 import java
.util
.function
.Function
;
63 import java
.util
.function
.Supplier
;
64 import java
.util
.stream
.Collectors
;
65 import java
.util
.stream
.Stream
;
68 * This class implements the Manager interface using the DBus Signal interface, where possible.
69 * It's used for the signal-cli dbus client mode (--dbus, --dbus-system)
71 public class DbusManagerImpl
implements Manager
{
73 private final Signal signal
;
74 private final DBusConnection connection
;
76 private final Set
<ReceiveMessageHandler
> weakHandlers
= new HashSet
<>();
77 private final Set
<ReceiveMessageHandler
> messageHandlers
= new HashSet
<>();
78 private final List
<Runnable
> closedListeners
= new ArrayList
<>();
79 private DBusSigHandler
<Signal
.MessageReceivedV2
> dbusMsgHandler
;
80 private DBusSigHandler
<Signal
.ReceiptReceivedV2
> dbusRcptHandler
;
81 private DBusSigHandler
<Signal
.SyncMessageReceivedV2
> dbusSyncHandler
;
83 public DbusManagerImpl(final Signal signal
, DBusConnection connection
) {
85 this.connection
= connection
;
89 public String
getSelfNumber() {
90 return signal
.getSelfNumber();
94 public Map
<String
, UserStatus
> getUserStatus(final Set
<String
> numbers
) throws IOException
{
95 final var numbersList
= new ArrayList
<>(numbers
);
96 final var registered
= signal
.isRegistered(numbersList
);
98 final var result
= new HashMap
<String
, UserStatus
>();
99 for (var i
= 0; i
< numbersList
.size(); i
++) {
100 result
.put(numbersList
.get(i
),
101 new UserStatus(numbersList
.get(i
),
102 registered
.get(i
) ? RecipientAddress
.UNKNOWN_UUID
: null,
109 public void updateAccountAttributes(final String deviceName
) throws IOException
{
110 if (deviceName
!= null) {
111 final var devicePath
= signal
.getThisDevice();
112 getRemoteObject(devicePath
, Signal
.Device
.class).Set("org.asamk.Signal.Device", "Name", deviceName
);
117 public Configuration
getConfiguration() {
118 final var configuration
= getRemoteObject(new DBusPath(signal
.getObjectPath() + "/Configuration"),
119 Signal
.Configuration
.class).GetAll("org.asamk.Signal.Configuration");
120 return new Configuration(Optional
.of((Boolean
) configuration
.get("ReadReceipts").getValue()),
121 Optional
.of((Boolean
) configuration
.get("UnidentifiedDeliveryIndicators").getValue()),
122 Optional
.of((Boolean
) configuration
.get("TypingIndicators").getValue()),
123 Optional
.of((Boolean
) configuration
.get("LinkPreviews").getValue()));
127 public void updateConfiguration(Configuration newConfiguration
) throws IOException
{
128 final var configuration
= getRemoteObject(new DBusPath(signal
.getObjectPath() + "/Configuration"),
129 Signal
.Configuration
.class);
130 newConfiguration
.readReceipts()
131 .ifPresent(v
-> configuration
.Set("org.asamk.Signal.Configuration", "ReadReceipts", v
));
132 newConfiguration
.unidentifiedDeliveryIndicators()
133 .ifPresent(v
-> configuration
.Set("org.asamk.Signal.Configuration",
134 "UnidentifiedDeliveryIndicators",
136 newConfiguration
.typingIndicators()
137 .ifPresent(v
-> configuration
.Set("org.asamk.Signal.Configuration", "TypingIndicators", v
));
138 newConfiguration
.linkPreviews()
139 .ifPresent(v
-> configuration
.Set("org.asamk.Signal.Configuration", "LinkPreviews", v
));
143 public void updateProfile(UpdateProfile updateProfile
) throws IOException
{
144 signal
.updateProfile(emptyIfNull(updateProfile
.getGivenName()),
145 emptyIfNull(updateProfile
.getFamilyName()),
146 emptyIfNull(updateProfile
.getAbout()),
147 emptyIfNull(updateProfile
.getAboutEmoji()),
148 updateProfile
.getAvatar() == null ?
"" : updateProfile
.getAvatar().getPath(),
149 updateProfile
.isDeleteAvatar());
153 public void unregister() throws IOException
{
158 public void deleteAccount() throws IOException
{
159 signal
.deleteAccount();
163 public void submitRateLimitRecaptchaChallenge(final String challenge
, final String captcha
) throws IOException
{
164 signal
.submitRateLimitChallenge(challenge
, captcha
);
168 public List
<Device
> getLinkedDevices() throws IOException
{
169 final var thisDevice
= signal
.getThisDevice();
170 return signal
.listDevices().stream().map(d
-> {
171 final var device
= getRemoteObject(d
.getObjectPath(),
172 Signal
.Device
.class).GetAll("org.asamk.Signal.Device");
173 return new Device((Integer
) device
.get("Id").getValue(),
174 (String
) device
.get("Name").getValue(),
175 (long) device
.get("Created").getValue(),
176 (long) device
.get("LastSeen").getValue(),
177 thisDevice
.equals(d
.getObjectPath()));
182 public void removeLinkedDevices(final int deviceId
) throws IOException
{
183 final var devicePath
= signal
.getDevice(deviceId
);
184 getRemoteObject(devicePath
, Signal
.Device
.class).removeDevice();
188 public void addDeviceLink(final URI linkUri
) throws IOException
, InvalidDeviceLinkException
{
189 signal
.addDevice(linkUri
.toString());
193 public void setRegistrationLockPin(final Optional
<String
> pin
) throws IOException
{
194 if (pin
.isPresent()) {
195 signal
.setPin(pin
.get());
202 public Profile
getRecipientProfile(final RecipientIdentifier
.Single recipient
) {
203 throw new UnsupportedOperationException();
207 public List
<Group
> getGroups() {
208 final var groups
= signal
.listGroups();
209 return groups
.stream().map(Signal
.StructGroup
::getObjectPath
).map(this::getGroup
).toList();
213 public SendGroupMessageResults
quitGroup(
214 final GroupId groupId
, final Set
<RecipientIdentifier
.Single
> groupAdmins
215 ) throws GroupNotFoundException
, IOException
, NotAGroupMemberException
, LastGroupAdminException
{
216 if (groupAdmins
.size() > 0) {
217 throw new UnsupportedOperationException();
219 final var group
= getRemoteObject(signal
.getGroup(groupId
.serialize()), Signal
.Group
.class);
221 return new SendGroupMessageResults(0, List
.of());
225 public void deleteGroup(final GroupId groupId
) throws IOException
{
226 final var group
= getRemoteObject(signal
.getGroup(groupId
.serialize()), Signal
.Group
.class);
231 public Pair
<GroupId
, SendGroupMessageResults
> createGroup(
232 final String name
, final Set
<RecipientIdentifier
.Single
> members
, final File avatarFile
233 ) throws IOException
, AttachmentInvalidException
{
234 final var newGroupId
= signal
.createGroup(emptyIfNull(name
),
235 members
.stream().map(RecipientIdentifier
.Single
::getIdentifier
).toList(),
236 avatarFile
== null ?
"" : avatarFile
.getPath());
237 return new Pair
<>(GroupId
.unknownVersion(newGroupId
), new SendGroupMessageResults(0, List
.of()));
241 public SendGroupMessageResults
updateGroup(
242 final GroupId groupId
, final UpdateGroup updateGroup
243 ) throws IOException
, GroupNotFoundException
, AttachmentInvalidException
, NotAGroupMemberException
, GroupSendingNotAllowedException
{
244 final var group
= getRemoteObject(signal
.getGroup(groupId
.serialize()), Signal
.Group
.class);
245 if (updateGroup
.getName() != null) {
246 group
.Set("org.asamk.Signal.Group", "Name", updateGroup
.getName());
248 if (updateGroup
.getDescription() != null) {
249 group
.Set("org.asamk.Signal.Group", "Description", updateGroup
.getDescription());
251 if (updateGroup
.getAvatarFile() != null) {
252 group
.Set("org.asamk.Signal.Group",
254 updateGroup
.getAvatarFile() == null ?
"" : updateGroup
.getAvatarFile().getPath());
256 if (updateGroup
.getExpirationTimer() != null) {
257 group
.Set("org.asamk.Signal.Group", "MessageExpirationTimer", updateGroup
.getExpirationTimer());
259 if (updateGroup
.getAddMemberPermission() != null) {
260 group
.Set("org.asamk.Signal.Group", "PermissionAddMember", updateGroup
.getAddMemberPermission().name());
262 if (updateGroup
.getEditDetailsPermission() != null) {
263 group
.Set("org.asamk.Signal.Group", "PermissionEditDetails", updateGroup
.getEditDetailsPermission().name());
265 if (updateGroup
.getIsAnnouncementGroup() != null) {
266 group
.Set("org.asamk.Signal.Group",
267 "PermissionSendMessage",
268 updateGroup
.getIsAnnouncementGroup()
269 ? GroupPermission
.ONLY_ADMINS
.name()
270 : GroupPermission
.EVERY_MEMBER
.name());
272 if (updateGroup
.getMembers() != null) {
273 group
.addMembers(updateGroup
.getMembers().stream().map(RecipientIdentifier
.Single
::getIdentifier
).toList());
275 if (updateGroup
.getRemoveMembers() != null) {
276 group
.removeMembers(updateGroup
.getRemoveMembers()
278 .map(RecipientIdentifier
.Single
::getIdentifier
)
281 if (updateGroup
.getAdmins() != null) {
282 group
.addAdmins(updateGroup
.getAdmins().stream().map(RecipientIdentifier
.Single
::getIdentifier
).toList());
284 if (updateGroup
.getRemoveAdmins() != null) {
285 group
.removeAdmins(updateGroup
.getRemoveAdmins()
287 .map(RecipientIdentifier
.Single
::getIdentifier
)
290 if (updateGroup
.isResetGroupLink()) {
293 if (updateGroup
.getGroupLinkState() != null) {
294 switch (updateGroup
.getGroupLinkState()) {
295 case DISABLED
-> group
.disableLink();
296 case ENABLED
-> group
.enableLink(false);
297 case ENABLED_WITH_APPROVAL
-> group
.enableLink(true);
300 return new SendGroupMessageResults(0, List
.of());
304 public Pair
<GroupId
, SendGroupMessageResults
> joinGroup(final GroupInviteLinkUrl inviteLinkUrl
) throws IOException
, InactiveGroupLinkException
{
305 final var newGroupId
= signal
.joinGroup(inviteLinkUrl
.getUrl());
306 return new Pair
<>(GroupId
.unknownVersion(newGroupId
), new SendGroupMessageResults(0, List
.of()));
310 public SendMessageResults
sendTypingMessage(
311 final TypingAction action
, final Set
<RecipientIdentifier
> recipients
312 ) throws IOException
, NotAGroupMemberException
, GroupNotFoundException
, GroupSendingNotAllowedException
{
313 return handleMessage(recipients
, numbers
-> {
314 numbers
.forEach(n
-> signal
.sendTyping(n
, action
== TypingAction
.STOP
));
317 signal
.sendTyping(signal
.getSelfNumber(), action
== TypingAction
.STOP
);
320 signal
.sendGroupTyping(groupId
, action
== TypingAction
.STOP
);
326 public SendMessageResults
sendReadReceipt(
327 final RecipientIdentifier
.Single sender
, final List
<Long
> messageIds
329 signal
.sendReadReceipt(sender
.getIdentifier(), messageIds
);
330 return new SendMessageResults(0, Map
.of());
334 public SendMessageResults
sendViewedReceipt(
335 final RecipientIdentifier
.Single sender
, final List
<Long
> messageIds
337 signal
.sendViewedReceipt(sender
.getIdentifier(), messageIds
);
338 return new SendMessageResults(0, Map
.of());
342 public SendMessageResults
sendMessage(
343 final Message message
, final Set
<RecipientIdentifier
> recipients
344 ) throws IOException
, AttachmentInvalidException
, NotAGroupMemberException
, GroupNotFoundException
, GroupSendingNotAllowedException
{
345 return handleMessage(recipients
,
346 numbers
-> signal
.sendMessage(message
.messageText(), message
.attachments(), numbers
),
347 () -> signal
.sendNoteToSelfMessage(message
.messageText(), message
.attachments()),
348 groupId
-> signal
.sendGroupMessage(message
.messageText(), message
.attachments(), groupId
));
352 public SendMessageResults
sendRemoteDeleteMessage(
353 final long targetSentTimestamp
, final Set
<RecipientIdentifier
> recipients
354 ) throws IOException
, NotAGroupMemberException
, GroupNotFoundException
, GroupSendingNotAllowedException
{
355 return handleMessage(recipients
,
356 numbers
-> signal
.sendRemoteDeleteMessage(targetSentTimestamp
, numbers
),
357 () -> signal
.sendRemoteDeleteMessage(targetSentTimestamp
, signal
.getSelfNumber()),
358 groupId
-> signal
.sendGroupRemoteDeleteMessage(targetSentTimestamp
, groupId
));
362 public SendMessageResults
sendMessageReaction(
364 final boolean remove
,
365 final RecipientIdentifier
.Single targetAuthor
,
366 final long targetSentTimestamp
,
367 final Set
<RecipientIdentifier
> recipients
,
368 final boolean isStory
369 ) throws IOException
, NotAGroupMemberException
, GroupNotFoundException
, GroupSendingNotAllowedException
{
370 return handleMessage(recipients
,
371 numbers
-> signal
.sendMessageReaction(emoji
,
373 targetAuthor
.getIdentifier(),
376 () -> signal
.sendMessageReaction(emoji
,
378 targetAuthor
.getIdentifier(),
380 signal
.getSelfNumber()),
381 groupId
-> signal
.sendGroupMessageReaction(emoji
,
383 targetAuthor
.getIdentifier(),
389 public SendMessageResults
sendPaymentNotificationMessage(
390 final byte[] receipt
, final String note
, final RecipientIdentifier
.Single recipient
391 ) throws IOException
{
392 final var timestamp
= signal
.sendPaymentNotification(receipt
, note
, recipient
.getIdentifier());
393 return new SendMessageResults(timestamp
, Map
.of());
397 public SendMessageResults
sendEndSessionMessage(final Set
<RecipientIdentifier
.Single
> recipients
) throws IOException
{
398 signal
.sendEndSessionMessage(recipients
.stream().map(RecipientIdentifier
.Single
::getIdentifier
).toList());
399 return new SendMessageResults(0, Map
.of());
403 public void deleteRecipient(final RecipientIdentifier
.Single recipient
) {
404 signal
.deleteRecipient(recipient
.getIdentifier());
408 public void deleteContact(final RecipientIdentifier
.Single recipient
) {
409 signal
.deleteContact(recipient
.getIdentifier());
413 public void setContactName(
414 final RecipientIdentifier
.Single recipient
, final String givenName
, final String familyName
415 ) throws NotPrimaryDeviceException
{
416 signal
.setContactName(recipient
.getIdentifier(), givenName
);
420 public void setContactsBlocked(
421 final Collection
<RecipientIdentifier
.Single
> recipients
, final boolean blocked
422 ) throws NotPrimaryDeviceException
, IOException
{
423 for (final var recipient
: recipients
) {
424 signal
.setContactBlocked(recipient
.getIdentifier(), blocked
);
429 public void setGroupsBlocked(
430 final Collection
<GroupId
> groupIds
, final boolean blocked
431 ) throws GroupNotFoundException
, IOException
{
432 for (final var groupId
: groupIds
) {
433 setGroupProperty(groupId
, "IsBlocked", blocked
);
437 private void setGroupProperty(final GroupId groupId
, final String propertyName
, final boolean blocked
) {
438 final var group
= getRemoteObject(signal
.getGroup(groupId
.serialize()), Signal
.Group
.class);
439 group
.Set("org.asamk.Signal.Group", propertyName
, blocked
);
443 public void setExpirationTimer(
444 final RecipientIdentifier
.Single recipient
, final int messageExpirationTimer
445 ) throws IOException
{
446 signal
.setExpirationTimer(recipient
.getIdentifier(), messageExpirationTimer
);
450 public StickerPackUrl
uploadStickerPack(final File path
) throws IOException
, StickerPackInvalidException
{
452 return StickerPackUrl
.fromUri(new URI(signal
.uploadStickerPack(path
.getPath())));
453 } catch (URISyntaxException
| StickerPackUrl
.InvalidStickerPackLinkException e
) {
454 throw new AssertionError(e
);
459 public List
<StickerPack
> getStickerPacks() {
460 throw new UnsupportedOperationException();
464 public void requestAllSyncData() throws IOException
{
465 signal
.sendSyncRequest();
469 public void addReceiveHandler(final ReceiveMessageHandler handler
, final boolean isWeakListener
) {
470 synchronized (messageHandlers
) {
471 if (isWeakListener
) {
472 weakHandlers
.add(handler
);
474 if (messageHandlers
.size() == 0) {
475 installMessageHandlers();
477 messageHandlers
.add(handler
);
483 public void removeReceiveHandler(final ReceiveMessageHandler handler
) {
484 synchronized (messageHandlers
) {
485 weakHandlers
.remove(handler
);
486 messageHandlers
.remove(handler
);
487 if (messageHandlers
.size() == 0) {
488 uninstallMessageHandlers();
494 public boolean isReceiving() {
495 synchronized (messageHandlers
) {
496 return messageHandlers
.size() > 0;
501 public void receiveMessages(final ReceiveMessageHandler handler
) throws IOException
{
502 addReceiveHandler(handler
);
504 synchronized (this) {
507 } catch (InterruptedException ignored
) {
509 removeReceiveHandler(handler
);
513 public void receiveMessages(
514 final Duration timeout
, final ReceiveMessageHandler handler
515 ) throws IOException
{
516 final var lastMessage
= new AtomicLong(System
.currentTimeMillis());
518 final ReceiveMessageHandler receiveHandler
= (envelope
, e
) -> {
519 lastMessage
.set(System
.currentTimeMillis());
520 handler
.handleMessage(envelope
, e
);
522 addReceiveHandler(receiveHandler
);
525 final var sleepTimeRemaining
= timeout
.toMillis() - (System
.currentTimeMillis() - lastMessage
.get());
526 if (sleepTimeRemaining
< 0) {
529 Thread
.sleep(sleepTimeRemaining
);
530 } catch (InterruptedException ignored
) {
533 removeReceiveHandler(receiveHandler
);
537 public void setReceiveConfig(final ReceiveConfig receiveConfig
) {
541 public boolean hasCaughtUpWithOldMessages() {
546 public boolean isContactBlocked(final RecipientIdentifier
.Single recipient
) {
547 return signal
.isContactBlocked(recipient
.getIdentifier());
551 public void sendContacts() throws IOException
{
552 signal
.sendContacts();
556 public List
<Recipient
> getRecipients(
557 final boolean onlyContacts
,
558 final Optional
<Boolean
> blocked
,
559 final Collection
<RecipientIdentifier
.Single
> addresses
,
560 final Optional
<String
> name
562 final var numbers
= addresses
.stream()
563 .filter(s
-> s
instanceof RecipientIdentifier
.Number
)
564 .map(s
-> ((RecipientIdentifier
.Number
) s
).number())
565 .collect(Collectors
.toSet());
566 return signal
.listNumbers().stream().filter(n
-> addresses
.isEmpty() || numbers
.contains(n
)).map(n
-> {
567 final var contactBlocked
= signal
.isContactBlocked(n
);
568 if (blocked
.isPresent() && blocked
.get() != contactBlocked
) {
571 final var contactName
= signal
.getContactName(n
);
572 if (onlyContacts
&& contactName
.length() == 0) {
575 if (name
.isPresent() && !name
.get().equals(contactName
)) {
578 return Recipient
.newBuilder()
579 .withAddress(new RecipientAddress(null, n
))
580 .withContact(new Contact(contactName
, null, null, 0, contactBlocked
, false, false))
582 }).filter(Objects
::nonNull
).toList();
586 public String
getContactOrProfileName(final RecipientIdentifier
.Single recipient
) {
587 return signal
.getContactName(recipient
.getIdentifier());
591 public Group
getGroup(final GroupId groupId
) {
592 final var groupPath
= signal
.getGroup(groupId
.serialize());
593 return getGroup(groupPath
);
596 @SuppressWarnings("unchecked")
597 private Group
getGroup(final DBusPath groupPath
) {
598 final var group
= getRemoteObject(groupPath
, Signal
.Group
.class).GetAll("org.asamk.Signal.Group");
599 final var id
= (byte[]) group
.get("Id").getValue();
601 return new Group(GroupId
.unknownVersion(id
),
602 (String
) group
.get("Name").getValue(),
603 (String
) group
.get("Description").getValue(),
604 GroupInviteLinkUrl
.fromUri((String
) group
.get("GroupInviteLink").getValue()),
605 ((List
<String
>) group
.get("Members").getValue()).stream()
606 .map(m
-> new RecipientAddress(null, m
))
607 .collect(Collectors
.toSet()),
608 ((List
<String
>) group
.get("PendingMembers").getValue()).stream()
609 .map(m
-> new RecipientAddress(null, m
))
610 .collect(Collectors
.toSet()),
611 ((List
<String
>) group
.get("RequestingMembers").getValue()).stream()
612 .map(m
-> new RecipientAddress(null, m
))
613 .collect(Collectors
.toSet()),
614 ((List
<String
>) group
.get("Admins").getValue()).stream()
615 .map(m
-> new RecipientAddress(null, m
))
616 .collect(Collectors
.toSet()),
617 ((List
<String
>) group
.get("Banned").getValue()).stream()
618 .map(m
-> new RecipientAddress(null, m
))
619 .collect(Collectors
.toSet()),
620 (boolean) group
.get("IsBlocked").getValue(),
621 (int) group
.get("MessageExpirationTimer").getValue(),
622 GroupPermission
.valueOf((String
) group
.get("PermissionAddMember").getValue()),
623 GroupPermission
.valueOf((String
) group
.get("PermissionEditDetails").getValue()),
624 GroupPermission
.valueOf((String
) group
.get("PermissionSendMessage").getValue()),
625 (boolean) group
.get("IsMember").getValue(),
626 (boolean) group
.get("IsAdmin").getValue());
627 } catch (GroupInviteLinkUrl
.InvalidGroupLinkException
| GroupInviteLinkUrl
.UnknownGroupLinkVersionException e
) {
628 throw new AssertionError(e
);
633 public List
<Identity
> getIdentities() {
634 throw new UnsupportedOperationException();
638 public List
<Identity
> getIdentities(final RecipientIdentifier
.Single recipient
) {
639 throw new UnsupportedOperationException();
643 public boolean trustIdentityVerified(final RecipientIdentifier
.Single recipient
, final byte[] fingerprint
) {
644 throw new UnsupportedOperationException();
648 public boolean trustIdentityVerifiedSafetyNumber(
649 final RecipientIdentifier
.Single recipient
, final String safetyNumber
651 throw new UnsupportedOperationException();
655 public boolean trustIdentityVerifiedSafetyNumber(
656 final RecipientIdentifier
.Single recipient
, final byte[] safetyNumber
658 throw new UnsupportedOperationException();
662 public boolean trustIdentityAllKeys(final RecipientIdentifier
.Single recipient
) {
663 throw new UnsupportedOperationException();
667 public void addAddressChangedListener(final Runnable listener
) {
671 public void addClosedListener(final Runnable listener
) {
672 synchronized (closedListeners
) {
673 closedListeners
.add(listener
);
678 public void close() {
679 synchronized (this) {
682 synchronized (messageHandlers
) {
683 if (messageHandlers
.size() > 0) {
684 uninstallMessageHandlers();
686 weakHandlers
.clear();
687 messageHandlers
.clear();
689 synchronized (closedListeners
) {
690 closedListeners
.forEach(Runnable
::run
);
691 closedListeners
.clear();
695 private SendMessageResults
handleMessage(
696 Set
<RecipientIdentifier
> recipients
,
697 Function
<List
<String
>, Long
> recipientsHandler
,
698 Supplier
<Long
> noteToSelfHandler
,
699 Function
<byte[], Long
> groupHandler
702 final var singleRecipients
= recipients
.stream()
703 .filter(r
-> r
instanceof RecipientIdentifier
.Single
)
704 .map(RecipientIdentifier
.Single
.class::cast
)
705 .map(RecipientIdentifier
.Single
::getIdentifier
)
707 if (singleRecipients
.size() > 0) {
708 timestamp
= recipientsHandler
.apply(singleRecipients
);
711 if (recipients
.contains(RecipientIdentifier
.NoteToSelf
.INSTANCE
)) {
712 timestamp
= noteToSelfHandler
.get();
714 final var groupRecipients
= recipients
.stream()
715 .filter(r
-> r
instanceof RecipientIdentifier
.Group
)
716 .map(RecipientIdentifier
.Group
.class::cast
)
717 .map(RecipientIdentifier
.Group
::groupId
)
719 for (final var groupId
: groupRecipients
) {
720 timestamp
= groupHandler
.apply(groupId
.serialize());
722 return new SendMessageResults(timestamp
, Map
.of());
725 private String
emptyIfNull(final String string
) {
726 return string
== null ?
"" : string
;
729 private <T
extends DBusInterface
> T
getRemoteObject(final DBusPath path
, final Class
<T
> type
) {
731 return connection
.getRemoteObject(DbusConfig
.getBusname(), path
.getPath(), type
);
732 } catch (DBusException e
) {
733 throw new AssertionError(e
);
737 private void installMessageHandlers() {
739 this.dbusMsgHandler
= messageReceived
-> {
740 final var extras
= messageReceived
.getExtras();
741 final var envelope
= new MessageEnvelope(Optional
.of(new RecipientAddress(null,
742 messageReceived
.getSender())),
744 messageReceived
.getTimestamp(),
750 Optional
.of(new MessageEnvelope
.Data(messageReceived
.getTimestamp(),
751 messageReceived
.getGroupId().length
> 0
752 ? Optional
.of(new MessageEnvelope
.Data
.GroupContext(GroupId
.unknownVersion(
753 messageReceived
.getGroupId()), false, 0))
757 Optional
.of(messageReceived
.getMessage()),
767 getAttachments(extras
),
776 notifyMessageHandlers(envelope
);
778 connection
.addSigHandler(Signal
.MessageReceivedV2
.class, signal
, this.dbusMsgHandler
);
780 this.dbusRcptHandler
= receiptReceived
-> {
781 final var type
= switch (receiptReceived
.getReceiptType()) {
782 case "read" -> MessageEnvelope
.Receipt
.Type
.READ
;
783 case "viewed" -> MessageEnvelope
.Receipt
.Type
.VIEWED
;
784 case "delivery" -> MessageEnvelope
.Receipt
.Type
.DELIVERY
;
785 default -> MessageEnvelope
.Receipt
.Type
.UNKNOWN
;
787 final var envelope
= new MessageEnvelope(Optional
.of(new RecipientAddress(null,
788 receiptReceived
.getSender())),
790 receiptReceived
.getTimestamp(),
794 Optional
.of(new MessageEnvelope
.Receipt(receiptReceived
.getTimestamp(),
796 List
.of(receiptReceived
.getTimestamp()))),
802 notifyMessageHandlers(envelope
);
804 connection
.addSigHandler(Signal
.ReceiptReceivedV2
.class, signal
, this.dbusRcptHandler
);
806 this.dbusSyncHandler
= syncReceived
-> {
807 final var extras
= syncReceived
.getExtras();
808 final var envelope
= new MessageEnvelope(Optional
.of(new RecipientAddress(null,
809 syncReceived
.getSource())),
811 syncReceived
.getTimestamp(),
818 Optional
.of(new MessageEnvelope
.Sync(Optional
.of(new MessageEnvelope
.Sync
.Sent(syncReceived
.getTimestamp(),
819 syncReceived
.getTimestamp(),
820 syncReceived
.getDestination().isEmpty()
822 : Optional
.of(new RecipientAddress(null, syncReceived
.getDestination())),
824 Optional
.of(new MessageEnvelope
.Data(syncReceived
.getTimestamp(),
825 syncReceived
.getGroupId().length
> 0
826 ? Optional
.of(new MessageEnvelope
.Data
.GroupContext(GroupId
.unknownVersion(
827 syncReceived
.getGroupId()), false, 0))
831 Optional
.of(syncReceived
.getMessage()),
841 getAttachments(extras
),
857 notifyMessageHandlers(envelope
);
859 connection
.addSigHandler(Signal
.SyncMessageReceivedV2
.class, signal
, this.dbusSyncHandler
);
860 } catch (DBusException e
) {
863 signal
.subscribeReceive();
866 private void notifyMessageHandlers(final MessageEnvelope envelope
) {
867 synchronized (messageHandlers
) {
868 Stream
.concat(messageHandlers
.stream(), weakHandlers
.stream())
869 .forEach(h
-> h
.handleMessage(envelope
, null));
873 private void uninstallMessageHandlers() {
875 signal
.unsubscribeReceive();
876 connection
.removeSigHandler(Signal
.MessageReceivedV2
.class, signal
, this.dbusMsgHandler
);
877 connection
.removeSigHandler(Signal
.ReceiptReceivedV2
.class, signal
, this.dbusRcptHandler
);
878 connection
.removeSigHandler(Signal
.SyncMessageReceivedV2
.class, signal
, this.dbusSyncHandler
);
879 } catch (DBusException e
) {
884 private List
<MessageEnvelope
.Data
.Attachment
> getAttachments(final Map
<String
, Variant
<?
>> extras
) {
885 if (!extras
.containsKey("attachments")) {
889 final List
<DBusMap
<String
, Variant
<?
>>> attachments
= getValue(extras
, "attachments");
890 return attachments
.stream().map(a
-> {
891 final String file
= a
.containsKey("file") ?
getValue(a
, "file") : null;
892 return new MessageEnvelope
.Data
.Attachment(a
.containsKey("remoteId")
893 ? Optional
.of(getValue(a
, "remoteId"))
895 file
!= null ? Optional
.of(new File(file
)) : Optional
.empty(),
897 getValue(a
, "contentType"),
905 getValue(a
, "isVoiceNote"),
906 getValue(a
, "isGif"),
907 getValue(a
, "isBorderless"));
911 @SuppressWarnings("unchecked")
912 private <T
> T
getValue(
913 final Map
<String
, Variant
<?
>> stringVariantMap
, final String field
915 return (T
) stringVariantMap
.get(field
).getValue();