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
.InvalidUsernameException
;
14 import org
.asamk
.signal
.manager
.api
.Message
;
15 import org
.asamk
.signal
.manager
.api
.MessageEnvelope
;
16 import org
.asamk
.signal
.manager
.api
.NotPrimaryDeviceException
;
17 import org
.asamk
.signal
.manager
.api
.Pair
;
18 import org
.asamk
.signal
.manager
.api
.ReceiveConfig
;
19 import org
.asamk
.signal
.manager
.api
.Recipient
;
20 import org
.asamk
.signal
.manager
.api
.RecipientAddress
;
21 import org
.asamk
.signal
.manager
.api
.RecipientIdentifier
;
22 import org
.asamk
.signal
.manager
.api
.SendGroupMessageResults
;
23 import org
.asamk
.signal
.manager
.api
.SendMessageResults
;
24 import org
.asamk
.signal
.manager
.api
.StickerPack
;
25 import org
.asamk
.signal
.manager
.api
.StickerPackInvalidException
;
26 import org
.asamk
.signal
.manager
.api
.StickerPackUrl
;
27 import org
.asamk
.signal
.manager
.api
.TypingAction
;
28 import org
.asamk
.signal
.manager
.api
.UpdateGroup
;
29 import org
.asamk
.signal
.manager
.api
.UpdateProfile
;
30 import org
.asamk
.signal
.manager
.api
.UserStatus
;
31 import org
.asamk
.signal
.manager
.groups
.GroupId
;
32 import org
.asamk
.signal
.manager
.groups
.GroupInviteLinkUrl
;
33 import org
.asamk
.signal
.manager
.groups
.GroupNotFoundException
;
34 import org
.asamk
.signal
.manager
.groups
.GroupPermission
;
35 import org
.asamk
.signal
.manager
.groups
.GroupSendingNotAllowedException
;
36 import org
.asamk
.signal
.manager
.groups
.LastGroupAdminException
;
37 import org
.asamk
.signal
.manager
.groups
.NotAGroupMemberException
;
38 import org
.asamk
.signal
.manager
.storage
.recipients
.Contact
;
39 import org
.asamk
.signal
.manager
.storage
.recipients
.Profile
;
40 import org
.freedesktop
.dbus
.DBusMap
;
41 import org
.freedesktop
.dbus
.DBusPath
;
42 import org
.freedesktop
.dbus
.connections
.impl
.DBusConnection
;
43 import org
.freedesktop
.dbus
.exceptions
.DBusException
;
44 import org
.freedesktop
.dbus
.interfaces
.DBusInterface
;
45 import org
.freedesktop
.dbus
.interfaces
.DBusSigHandler
;
46 import org
.freedesktop
.dbus
.types
.Variant
;
49 import java
.io
.IOException
;
50 import java
.io
.InputStream
;
52 import java
.net
.URISyntaxException
;
53 import java
.time
.Duration
;
54 import java
.util
.ArrayList
;
55 import java
.util
.Collection
;
56 import java
.util
.HashMap
;
57 import java
.util
.HashSet
;
58 import java
.util
.List
;
60 import java
.util
.Objects
;
61 import java
.util
.Optional
;
63 import java
.util
.concurrent
.atomic
.AtomicInteger
;
64 import java
.util
.concurrent
.atomic
.AtomicLong
;
65 import java
.util
.function
.Function
;
66 import java
.util
.function
.Supplier
;
67 import java
.util
.stream
.Collectors
;
68 import java
.util
.stream
.Stream
;
71 * This class implements the Manager interface using the DBus Signal interface, where possible.
72 * It's used for the signal-cli dbus client mode (--dbus, --dbus-system)
74 public class DbusManagerImpl
implements Manager
{
76 private final Signal signal
;
77 private final DBusConnection connection
;
79 private final Set
<ReceiveMessageHandler
> weakHandlers
= new HashSet
<>();
80 private final Set
<ReceiveMessageHandler
> messageHandlers
= new HashSet
<>();
81 private final List
<Runnable
> closedListeners
= new ArrayList
<>();
82 private DBusSigHandler
<Signal
.MessageReceivedV2
> dbusMsgHandler
;
83 private DBusSigHandler
<Signal
.ReceiptReceivedV2
> dbusRcptHandler
;
84 private DBusSigHandler
<Signal
.SyncMessageReceivedV2
> dbusSyncHandler
;
86 public DbusManagerImpl(final Signal signal
, DBusConnection connection
) {
88 this.connection
= connection
;
92 public String
getSelfNumber() {
93 return signal
.getSelfNumber();
97 public Map
<String
, UserStatus
> getUserStatus(final Set
<String
> numbers
) throws IOException
{
98 final var numbersList
= new ArrayList
<>(numbers
);
99 final var registered
= signal
.isRegistered(numbersList
);
101 final var result
= new HashMap
<String
, UserStatus
>();
102 for (var i
= 0; i
< numbersList
.size(); i
++) {
103 result
.put(numbersList
.get(i
),
104 new UserStatus(numbersList
.get(i
),
105 registered
.get(i
) ? RecipientAddress
.UNKNOWN_UUID
: null,
112 public void updateAccountAttributes(final String deviceName
) throws IOException
{
113 if (deviceName
!= null) {
114 final var devicePath
= signal
.getThisDevice();
115 getRemoteObject(devicePath
, Signal
.Device
.class).Set("org.asamk.Signal.Device", "Name", deviceName
);
120 public Configuration
getConfiguration() {
121 final var configuration
= getRemoteObject(new DBusPath(signal
.getObjectPath() + "/Configuration"),
122 Signal
.Configuration
.class).GetAll("org.asamk.Signal.Configuration");
123 return new Configuration(Optional
.of((Boolean
) configuration
.get("ReadReceipts").getValue()),
124 Optional
.of((Boolean
) configuration
.get("UnidentifiedDeliveryIndicators").getValue()),
125 Optional
.of((Boolean
) configuration
.get("TypingIndicators").getValue()),
126 Optional
.of((Boolean
) configuration
.get("LinkPreviews").getValue()));
130 public void updateConfiguration(Configuration newConfiguration
) throws IOException
{
131 final var configuration
= getRemoteObject(new DBusPath(signal
.getObjectPath() + "/Configuration"),
132 Signal
.Configuration
.class);
133 newConfiguration
.readReceipts()
134 .ifPresent(v
-> configuration
.Set("org.asamk.Signal.Configuration", "ReadReceipts", v
));
135 newConfiguration
.unidentifiedDeliveryIndicators()
136 .ifPresent(v
-> configuration
.Set("org.asamk.Signal.Configuration",
137 "UnidentifiedDeliveryIndicators",
139 newConfiguration
.typingIndicators()
140 .ifPresent(v
-> configuration
.Set("org.asamk.Signal.Configuration", "TypingIndicators", v
));
141 newConfiguration
.linkPreviews()
142 .ifPresent(v
-> configuration
.Set("org.asamk.Signal.Configuration", "LinkPreviews", v
));
146 public void updateProfile(UpdateProfile updateProfile
) throws IOException
{
147 signal
.updateProfile(emptyIfNull(updateProfile
.getGivenName()),
148 emptyIfNull(updateProfile
.getFamilyName()),
149 emptyIfNull(updateProfile
.getAbout()),
150 emptyIfNull(updateProfile
.getAboutEmoji()),
151 updateProfile
.getAvatar() == null ?
"" : updateProfile
.getAvatar(),
152 updateProfile
.isDeleteAvatar());
156 public String
setUsername(final String username
) throws IOException
, InvalidUsernameException
{
157 throw new UnsupportedOperationException();
161 public void deleteUsername() throws IOException
{
162 throw new UnsupportedOperationException();
166 public void unregister() throws IOException
{
171 public void deleteAccount() throws IOException
{
172 signal
.deleteAccount();
176 public void submitRateLimitRecaptchaChallenge(final String challenge
, final String captcha
) throws IOException
{
177 signal
.submitRateLimitChallenge(challenge
, captcha
);
181 public List
<Device
> getLinkedDevices() throws IOException
{
182 final var thisDevice
= signal
.getThisDevice();
183 return signal
.listDevices().stream().map(d
-> {
184 final var device
= getRemoteObject(d
.getObjectPath(),
185 Signal
.Device
.class).GetAll("org.asamk.Signal.Device");
186 return new Device((Integer
) device
.get("Id").getValue(),
187 (String
) device
.get("Name").getValue(),
188 (long) device
.get("Created").getValue(),
189 (long) device
.get("LastSeen").getValue(),
190 thisDevice
.equals(d
.getObjectPath()));
195 public void removeLinkedDevices(final int deviceId
) throws IOException
{
196 final var devicePath
= signal
.getDevice(deviceId
);
197 getRemoteObject(devicePath
, Signal
.Device
.class).removeDevice();
201 public void addDeviceLink(final URI linkUri
) throws IOException
, InvalidDeviceLinkException
{
202 signal
.addDevice(linkUri
.toString());
206 public void setRegistrationLockPin(final Optional
<String
> pin
) throws IOException
{
207 if (pin
.isPresent()) {
208 signal
.setPin(pin
.get());
215 public Profile
getRecipientProfile(final RecipientIdentifier
.Single recipient
) {
216 throw new UnsupportedOperationException();
220 public List
<Group
> getGroups() {
221 final var groups
= signal
.listGroups();
222 return groups
.stream().map(Signal
.StructGroup
::getObjectPath
).map(this::getGroup
).toList();
226 public SendGroupMessageResults
quitGroup(
227 final GroupId groupId
, final Set
<RecipientIdentifier
.Single
> groupAdmins
228 ) throws GroupNotFoundException
, IOException
, NotAGroupMemberException
, LastGroupAdminException
{
229 if (groupAdmins
.size() > 0) {
230 throw new UnsupportedOperationException();
232 final var group
= getRemoteObject(signal
.getGroup(groupId
.serialize()), Signal
.Group
.class);
235 } catch (Signal
.Error
.GroupNotFound e
) {
236 throw new GroupNotFoundException(groupId
);
237 } catch (Signal
.Error
.NotAGroupMember e
) {
238 throw new NotAGroupMemberException(groupId
, group
.Get("org.asamk.Signal.Group", "Name"));
239 } catch (Signal
.Error
.LastGroupAdmin e
) {
240 throw new LastGroupAdminException(groupId
, group
.Get("org.asamk.Signal.Group", "Name"));
242 return new SendGroupMessageResults(0, List
.of());
246 public void deleteGroup(final GroupId groupId
) throws IOException
{
247 final var group
= getRemoteObject(signal
.getGroup(groupId
.serialize()), Signal
.Group
.class);
252 public Pair
<GroupId
, SendGroupMessageResults
> createGroup(
253 final String name
, final Set
<RecipientIdentifier
.Single
> members
, final String avatarFile
254 ) throws IOException
, AttachmentInvalidException
{
255 final var newGroupId
= signal
.createGroup(emptyIfNull(name
),
256 members
.stream().map(RecipientIdentifier
.Single
::getIdentifier
).toList(),
257 avatarFile
== null ?
"" : avatarFile
);
258 return new Pair
<>(GroupId
.unknownVersion(newGroupId
), new SendGroupMessageResults(0, List
.of()));
262 public SendGroupMessageResults
updateGroup(
263 final GroupId groupId
, final UpdateGroup updateGroup
264 ) throws IOException
, GroupNotFoundException
, AttachmentInvalidException
, NotAGroupMemberException
, GroupSendingNotAllowedException
{
265 final var group
= getRemoteObject(signal
.getGroup(groupId
.serialize()), Signal
.Group
.class);
266 if (updateGroup
.getName() != null) {
267 group
.Set("org.asamk.Signal.Group", "Name", updateGroup
.getName());
269 if (updateGroup
.getDescription() != null) {
270 group
.Set("org.asamk.Signal.Group", "Description", updateGroup
.getDescription());
272 if (updateGroup
.getAvatarFile() != null) {
273 group
.Set("org.asamk.Signal.Group",
275 updateGroup
.getAvatarFile() == null ?
"" : updateGroup
.getAvatarFile());
277 if (updateGroup
.getExpirationTimer() != null) {
278 group
.Set("org.asamk.Signal.Group", "MessageExpirationTimer", updateGroup
.getExpirationTimer());
280 if (updateGroup
.getAddMemberPermission() != null) {
281 group
.Set("org.asamk.Signal.Group", "PermissionAddMember", updateGroup
.getAddMemberPermission().name());
283 if (updateGroup
.getEditDetailsPermission() != null) {
284 group
.Set("org.asamk.Signal.Group", "PermissionEditDetails", updateGroup
.getEditDetailsPermission().name());
286 if (updateGroup
.getIsAnnouncementGroup() != null) {
287 group
.Set("org.asamk.Signal.Group",
288 "PermissionSendMessage",
289 updateGroup
.getIsAnnouncementGroup()
290 ? GroupPermission
.ONLY_ADMINS
.name()
291 : GroupPermission
.EVERY_MEMBER
.name());
293 if (updateGroup
.getMembers() != null) {
294 group
.addMembers(updateGroup
.getMembers().stream().map(RecipientIdentifier
.Single
::getIdentifier
).toList());
296 if (updateGroup
.getRemoveMembers() != null) {
297 group
.removeMembers(updateGroup
.getRemoveMembers()
299 .map(RecipientIdentifier
.Single
::getIdentifier
)
302 if (updateGroup
.getAdmins() != null) {
303 group
.addAdmins(updateGroup
.getAdmins().stream().map(RecipientIdentifier
.Single
::getIdentifier
).toList());
305 if (updateGroup
.getRemoveAdmins() != null) {
306 group
.removeAdmins(updateGroup
.getRemoveAdmins()
308 .map(RecipientIdentifier
.Single
::getIdentifier
)
311 if (updateGroup
.isResetGroupLink()) {
314 if (updateGroup
.getGroupLinkState() != null) {
315 switch (updateGroup
.getGroupLinkState()) {
316 case DISABLED
-> group
.disableLink();
317 case ENABLED
-> group
.enableLink(false);
318 case ENABLED_WITH_APPROVAL
-> group
.enableLink(true);
321 return new SendGroupMessageResults(0, List
.of());
325 public Pair
<GroupId
, SendGroupMessageResults
> joinGroup(final GroupInviteLinkUrl inviteLinkUrl
) throws IOException
, InactiveGroupLinkException
{
326 final var newGroupId
= signal
.joinGroup(inviteLinkUrl
.getUrl());
327 return new Pair
<>(GroupId
.unknownVersion(newGroupId
), new SendGroupMessageResults(0, List
.of()));
331 public SendMessageResults
sendTypingMessage(
332 final TypingAction action
, final Set
<RecipientIdentifier
> recipients
333 ) throws IOException
, NotAGroupMemberException
, GroupNotFoundException
, GroupSendingNotAllowedException
{
334 return handleMessage(recipients
, numbers
-> {
335 numbers
.forEach(n
-> signal
.sendTyping(n
, action
== TypingAction
.STOP
));
338 signal
.sendTyping(signal
.getSelfNumber(), action
== TypingAction
.STOP
);
341 signal
.sendGroupTyping(groupId
, action
== TypingAction
.STOP
);
347 public SendMessageResults
sendReadReceipt(
348 final RecipientIdentifier
.Single sender
, final List
<Long
> messageIds
350 signal
.sendReadReceipt(sender
.getIdentifier(), messageIds
);
351 return new SendMessageResults(0, Map
.of());
355 public SendMessageResults
sendViewedReceipt(
356 final RecipientIdentifier
.Single sender
, final List
<Long
> messageIds
358 signal
.sendViewedReceipt(sender
.getIdentifier(), messageIds
);
359 return new SendMessageResults(0, Map
.of());
363 public SendMessageResults
sendMessage(
364 final Message message
, final Set
<RecipientIdentifier
> recipients
365 ) throws IOException
, AttachmentInvalidException
, NotAGroupMemberException
, GroupNotFoundException
, GroupSendingNotAllowedException
{
366 return handleMessage(recipients
,
367 numbers
-> signal
.sendMessage(message
.messageText(), message
.attachments(), numbers
),
368 () -> signal
.sendNoteToSelfMessage(message
.messageText(), message
.attachments()),
369 groupId
-> signal
.sendGroupMessage(message
.messageText(), message
.attachments(), groupId
));
373 public SendMessageResults
sendRemoteDeleteMessage(
374 final long targetSentTimestamp
, final Set
<RecipientIdentifier
> recipients
375 ) throws IOException
, NotAGroupMemberException
, GroupNotFoundException
, GroupSendingNotAllowedException
{
376 return handleMessage(recipients
,
377 numbers
-> signal
.sendRemoteDeleteMessage(targetSentTimestamp
, numbers
),
378 () -> signal
.sendRemoteDeleteMessage(targetSentTimestamp
, signal
.getSelfNumber()),
379 groupId
-> signal
.sendGroupRemoteDeleteMessage(targetSentTimestamp
, groupId
));
383 public SendMessageResults
sendMessageReaction(
385 final boolean remove
,
386 final RecipientIdentifier
.Single targetAuthor
,
387 final long targetSentTimestamp
,
388 final Set
<RecipientIdentifier
> recipients
,
389 final boolean isStory
390 ) throws IOException
, NotAGroupMemberException
, GroupNotFoundException
, GroupSendingNotAllowedException
{
391 return handleMessage(recipients
,
392 numbers
-> signal
.sendMessageReaction(emoji
,
394 targetAuthor
.getIdentifier(),
397 () -> signal
.sendMessageReaction(emoji
,
399 targetAuthor
.getIdentifier(),
401 signal
.getSelfNumber()),
402 groupId
-> signal
.sendGroupMessageReaction(emoji
,
404 targetAuthor
.getIdentifier(),
410 public SendMessageResults
sendPaymentNotificationMessage(
411 final byte[] receipt
, final String note
, final RecipientIdentifier
.Single recipient
412 ) throws IOException
{
413 final var timestamp
= signal
.sendPaymentNotification(receipt
, note
, recipient
.getIdentifier());
414 return new SendMessageResults(timestamp
, Map
.of());
418 public SendMessageResults
sendEndSessionMessage(final Set
<RecipientIdentifier
.Single
> recipients
) throws IOException
{
419 signal
.sendEndSessionMessage(recipients
.stream().map(RecipientIdentifier
.Single
::getIdentifier
).toList());
420 return new SendMessageResults(0, Map
.of());
424 public void deleteRecipient(final RecipientIdentifier
.Single recipient
) {
425 signal
.deleteRecipient(recipient
.getIdentifier());
429 public void deleteContact(final RecipientIdentifier
.Single recipient
) {
430 signal
.deleteContact(recipient
.getIdentifier());
434 public void setContactName(
435 final RecipientIdentifier
.Single recipient
, final String givenName
, final String familyName
436 ) throws NotPrimaryDeviceException
{
437 signal
.setContactName(recipient
.getIdentifier(), givenName
);
441 public void setContactsBlocked(
442 final Collection
<RecipientIdentifier
.Single
> recipients
, final boolean blocked
443 ) throws NotPrimaryDeviceException
, IOException
{
444 for (final var recipient
: recipients
) {
445 signal
.setContactBlocked(recipient
.getIdentifier(), blocked
);
450 public void setGroupsBlocked(
451 final Collection
<GroupId
> groupIds
, final boolean blocked
452 ) throws GroupNotFoundException
, IOException
{
453 for (final var groupId
: groupIds
) {
454 setGroupProperty(groupId
, "IsBlocked", blocked
);
458 private void setGroupProperty(final GroupId groupId
, final String propertyName
, final boolean blocked
) {
459 final var group
= getRemoteObject(signal
.getGroup(groupId
.serialize()), Signal
.Group
.class);
460 group
.Set("org.asamk.Signal.Group", propertyName
, blocked
);
464 public void setExpirationTimer(
465 final RecipientIdentifier
.Single recipient
, final int messageExpirationTimer
466 ) throws IOException
{
467 signal
.setExpirationTimer(recipient
.getIdentifier(), messageExpirationTimer
);
471 public StickerPackUrl
uploadStickerPack(final File path
) throws IOException
, StickerPackInvalidException
{
473 return StickerPackUrl
.fromUri(new URI(signal
.uploadStickerPack(path
.getPath())));
474 } catch (URISyntaxException
| StickerPackUrl
.InvalidStickerPackLinkException e
) {
475 throw new AssertionError(e
);
480 public List
<StickerPack
> getStickerPacks() {
481 throw new UnsupportedOperationException();
485 public void requestAllSyncData() throws IOException
{
486 signal
.sendSyncRequest();
490 public void addReceiveHandler(final ReceiveMessageHandler handler
, final boolean isWeakListener
) {
491 synchronized (messageHandlers
) {
492 if (isWeakListener
) {
493 weakHandlers
.add(handler
);
495 if (messageHandlers
.size() == 0) {
496 installMessageHandlers();
498 messageHandlers
.add(handler
);
504 public void removeReceiveHandler(final ReceiveMessageHandler handler
) {
505 synchronized (messageHandlers
) {
506 weakHandlers
.remove(handler
);
507 messageHandlers
.remove(handler
);
508 if (messageHandlers
.size() == 0) {
509 uninstallMessageHandlers();
515 public boolean isReceiving() {
516 synchronized (messageHandlers
) {
517 return messageHandlers
.size() > 0;
522 public void receiveMessages(
523 Optional
<Duration
> timeout
, Optional
<Integer
> maxMessages
, ReceiveMessageHandler handler
524 ) throws IOException
{
525 final var remainingMessages
= new AtomicInteger(maxMessages
.orElse(-1));
526 final var lastMessage
= new AtomicLong(System
.currentTimeMillis());
527 final var thread
= Thread
.currentThread();
529 final ReceiveMessageHandler receiveHandler
= (envelope
, e
) -> {
530 lastMessage
.set(System
.currentTimeMillis());
531 handler
.handleMessage(envelope
, e
);
532 if (remainingMessages
.get() > 0) {
533 if (remainingMessages
.decrementAndGet() <= 0) {
534 remainingMessages
.set(0);
539 addReceiveHandler(receiveHandler
);
540 if (timeout
.isPresent()) {
541 while (remainingMessages
.get() != 0) {
543 final var passedTime
= System
.currentTimeMillis() - lastMessage
.get();
544 final var sleepTimeRemaining
= timeout
.get().toMillis() - passedTime
;
545 if (sleepTimeRemaining
< 0) {
548 Thread
.sleep(sleepTimeRemaining
);
549 } catch (InterruptedException ignored
) {
554 synchronized (this) {
557 } catch (InterruptedException ignored
) {
561 removeReceiveHandler(receiveHandler
);
565 public void setReceiveConfig(final ReceiveConfig receiveConfig
) {
569 public boolean hasCaughtUpWithOldMessages() {
574 public boolean isContactBlocked(final RecipientIdentifier
.Single recipient
) {
575 return signal
.isContactBlocked(recipient
.getIdentifier());
579 public void sendContacts() throws IOException
{
580 signal
.sendContacts();
584 public List
<Recipient
> getRecipients(
585 final boolean onlyContacts
,
586 final Optional
<Boolean
> blocked
,
587 final Collection
<RecipientIdentifier
.Single
> addresses
,
588 final Optional
<String
> name
590 final var numbers
= addresses
.stream()
591 .filter(s
-> s
instanceof RecipientIdentifier
.Number
)
592 .map(s
-> ((RecipientIdentifier
.Number
) s
).number())
593 .collect(Collectors
.toSet());
594 return signal
.listNumbers().stream().filter(n
-> addresses
.isEmpty() || numbers
.contains(n
)).map(n
-> {
595 final var contactBlocked
= signal
.isContactBlocked(n
);
596 if (blocked
.isPresent() && blocked
.get() != contactBlocked
) {
599 final var contactName
= signal
.getContactName(n
);
600 if (onlyContacts
&& contactName
.length() == 0) {
603 if (name
.isPresent() && !name
.get().equals(contactName
)) {
606 return Recipient
.newBuilder()
607 .withAddress(new RecipientAddress(null, n
))
608 .withContact(new Contact(contactName
, null, null, 0, contactBlocked
, false, false))
610 }).filter(Objects
::nonNull
).toList();
614 public String
getContactOrProfileName(final RecipientIdentifier
.Single recipient
) {
615 return signal
.getContactName(recipient
.getIdentifier());
619 public Group
getGroup(final GroupId groupId
) {
620 final var groupPath
= signal
.getGroup(groupId
.serialize());
621 return getGroup(groupPath
);
624 @SuppressWarnings("unchecked")
625 private Group
getGroup(final DBusPath groupPath
) {
626 final var group
= getRemoteObject(groupPath
, Signal
.Group
.class).GetAll("org.asamk.Signal.Group");
627 final var id
= (byte[]) group
.get("Id").getValue();
629 return new Group(GroupId
.unknownVersion(id
),
630 (String
) group
.get("Name").getValue(),
631 (String
) group
.get("Description").getValue(),
632 GroupInviteLinkUrl
.fromUri((String
) group
.get("GroupInviteLink").getValue()),
633 ((List
<String
>) group
.get("Members").getValue()).stream()
634 .map(m
-> new RecipientAddress(null, m
))
635 .collect(Collectors
.toSet()),
636 ((List
<String
>) group
.get("PendingMembers").getValue()).stream()
637 .map(m
-> new RecipientAddress(null, m
))
638 .collect(Collectors
.toSet()),
639 ((List
<String
>) group
.get("RequestingMembers").getValue()).stream()
640 .map(m
-> new RecipientAddress(null, m
))
641 .collect(Collectors
.toSet()),
642 ((List
<String
>) group
.get("Admins").getValue()).stream()
643 .map(m
-> new RecipientAddress(null, m
))
644 .collect(Collectors
.toSet()),
645 ((List
<String
>) group
.get("Banned").getValue()).stream()
646 .map(m
-> new RecipientAddress(null, m
))
647 .collect(Collectors
.toSet()),
648 (boolean) group
.get("IsBlocked").getValue(),
649 (int) group
.get("MessageExpirationTimer").getValue(),
650 GroupPermission
.valueOf((String
) group
.get("PermissionAddMember").getValue()),
651 GroupPermission
.valueOf((String
) group
.get("PermissionEditDetails").getValue()),
652 GroupPermission
.valueOf((String
) group
.get("PermissionSendMessage").getValue()),
653 (boolean) group
.get("IsMember").getValue(),
654 (boolean) group
.get("IsAdmin").getValue());
655 } catch (GroupInviteLinkUrl
.InvalidGroupLinkException
| GroupInviteLinkUrl
.UnknownGroupLinkVersionException e
) {
656 throw new AssertionError(e
);
661 public List
<Identity
> getIdentities() {
662 throw new UnsupportedOperationException();
666 public List
<Identity
> getIdentities(final RecipientIdentifier
.Single recipient
) {
667 throw new UnsupportedOperationException();
671 public boolean trustIdentityVerified(final RecipientIdentifier
.Single recipient
, final byte[] fingerprint
) {
672 throw new UnsupportedOperationException();
676 public boolean trustIdentityVerifiedSafetyNumber(
677 final RecipientIdentifier
.Single recipient
, final String safetyNumber
679 throw new UnsupportedOperationException();
683 public boolean trustIdentityVerifiedSafetyNumber(
684 final RecipientIdentifier
.Single recipient
, final byte[] safetyNumber
686 throw new UnsupportedOperationException();
690 public boolean trustIdentityAllKeys(final RecipientIdentifier
.Single recipient
) {
691 throw new UnsupportedOperationException();
695 public void addAddressChangedListener(final Runnable listener
) {
699 public void addClosedListener(final Runnable listener
) {
700 synchronized (closedListeners
) {
701 closedListeners
.add(listener
);
706 public void close() {
707 synchronized (this) {
710 synchronized (messageHandlers
) {
711 if (messageHandlers
.size() > 0) {
712 uninstallMessageHandlers();
714 weakHandlers
.clear();
715 messageHandlers
.clear();
717 synchronized (closedListeners
) {
718 closedListeners
.forEach(Runnable
::run
);
719 closedListeners
.clear();
723 private SendMessageResults
handleMessage(
724 Set
<RecipientIdentifier
> recipients
,
725 Function
<List
<String
>, Long
> recipientsHandler
,
726 Supplier
<Long
> noteToSelfHandler
,
727 Function
<byte[], Long
> groupHandler
730 final var singleRecipients
= recipients
.stream()
731 .filter(r
-> r
instanceof RecipientIdentifier
.Single
)
732 .map(RecipientIdentifier
.Single
.class::cast
)
733 .map(RecipientIdentifier
.Single
::getIdentifier
)
735 if (singleRecipients
.size() > 0) {
736 timestamp
= recipientsHandler
.apply(singleRecipients
);
739 if (recipients
.contains(RecipientIdentifier
.NoteToSelf
.INSTANCE
)) {
740 timestamp
= noteToSelfHandler
.get();
742 final var groupRecipients
= recipients
.stream()
743 .filter(r
-> r
instanceof RecipientIdentifier
.Group
)
744 .map(RecipientIdentifier
.Group
.class::cast
)
745 .map(RecipientIdentifier
.Group
::groupId
)
747 for (final var groupId
: groupRecipients
) {
748 timestamp
= groupHandler
.apply(groupId
.serialize());
750 return new SendMessageResults(timestamp
, Map
.of());
753 private String
emptyIfNull(final String string
) {
754 return string
== null ?
"" : string
;
757 private <T
extends DBusInterface
> T
getRemoteObject(final DBusPath path
, final Class
<T
> type
) {
759 return connection
.getRemoteObject(DbusConfig
.getBusname(), path
.getPath(), type
);
760 } catch (DBusException e
) {
761 throw new AssertionError(e
);
765 private void installMessageHandlers() {
767 this.dbusMsgHandler
= messageReceived
-> {
768 final var extras
= messageReceived
.getExtras();
769 final var envelope
= new MessageEnvelope(Optional
.of(new RecipientAddress(null,
770 messageReceived
.getSender())),
772 messageReceived
.getTimestamp(),
778 Optional
.of(new MessageEnvelope
.Data(messageReceived
.getTimestamp(),
779 messageReceived
.getGroupId().length
> 0
780 ? Optional
.of(new MessageEnvelope
.Data
.GroupContext(GroupId
.unknownVersion(
781 messageReceived
.getGroupId()), false, 0))
785 Optional
.of(messageReceived
.getMessage()),
795 getAttachments(extras
),
805 notifyMessageHandlers(envelope
);
807 connection
.addSigHandler(Signal
.MessageReceivedV2
.class, signal
, this.dbusMsgHandler
);
809 this.dbusRcptHandler
= receiptReceived
-> {
810 final var type
= switch (receiptReceived
.getReceiptType()) {
811 case "read" -> MessageEnvelope
.Receipt
.Type
.READ
;
812 case "viewed" -> MessageEnvelope
.Receipt
.Type
.VIEWED
;
813 case "delivery" -> MessageEnvelope
.Receipt
.Type
.DELIVERY
;
814 default -> MessageEnvelope
.Receipt
.Type
.UNKNOWN
;
816 final var envelope
= new MessageEnvelope(Optional
.of(new RecipientAddress(null,
817 receiptReceived
.getSender())),
819 receiptReceived
.getTimestamp(),
823 Optional
.of(new MessageEnvelope
.Receipt(receiptReceived
.getTimestamp(),
825 List
.of(receiptReceived
.getTimestamp()))),
831 notifyMessageHandlers(envelope
);
833 connection
.addSigHandler(Signal
.ReceiptReceivedV2
.class, signal
, this.dbusRcptHandler
);
835 this.dbusSyncHandler
= syncReceived
-> {
836 final var extras
= syncReceived
.getExtras();
837 final var envelope
= new MessageEnvelope(Optional
.of(new RecipientAddress(null,
838 syncReceived
.getSource())),
840 syncReceived
.getTimestamp(),
847 Optional
.of(new MessageEnvelope
.Sync(Optional
.of(new MessageEnvelope
.Sync
.Sent(syncReceived
.getTimestamp(),
848 syncReceived
.getTimestamp(),
849 syncReceived
.getDestination().isEmpty()
851 : Optional
.of(new RecipientAddress(null, syncReceived
.getDestination())),
853 Optional
.of(new MessageEnvelope
.Data(syncReceived
.getTimestamp(),
854 syncReceived
.getGroupId().length
> 0
855 ? Optional
.of(new MessageEnvelope
.Data
.GroupContext(GroupId
.unknownVersion(
856 syncReceived
.getGroupId()), false, 0))
860 Optional
.of(syncReceived
.getMessage()),
870 getAttachments(extras
),
887 notifyMessageHandlers(envelope
);
889 connection
.addSigHandler(Signal
.SyncMessageReceivedV2
.class, signal
, this.dbusSyncHandler
);
890 } catch (DBusException e
) {
893 signal
.subscribeReceive();
896 private void notifyMessageHandlers(final MessageEnvelope envelope
) {
897 synchronized (messageHandlers
) {
898 Stream
.concat(messageHandlers
.stream(), weakHandlers
.stream())
899 .forEach(h
-> h
.handleMessage(envelope
, null));
903 private void uninstallMessageHandlers() {
905 signal
.unsubscribeReceive();
906 connection
.removeSigHandler(Signal
.MessageReceivedV2
.class, signal
, this.dbusMsgHandler
);
907 connection
.removeSigHandler(Signal
.ReceiptReceivedV2
.class, signal
, this.dbusRcptHandler
);
908 connection
.removeSigHandler(Signal
.SyncMessageReceivedV2
.class, signal
, this.dbusSyncHandler
);
909 } catch (DBusException e
) {
914 private List
<MessageEnvelope
.Data
.Attachment
> getAttachments(final Map
<String
, Variant
<?
>> extras
) {
915 if (!extras
.containsKey("attachments")) {
919 final List
<DBusMap
<String
, Variant
<?
>>> attachments
= getValue(extras
, "attachments");
920 return attachments
.stream().map(a
-> {
921 final String file
= a
.containsKey("file") ?
getValue(a
, "file") : null;
922 return new MessageEnvelope
.Data
.Attachment(a
.containsKey("remoteId")
923 ? Optional
.of(getValue(a
, "remoteId"))
925 file
!= null ? Optional
.of(new File(file
)) : Optional
.empty(),
927 getValue(a
, "contentType"),
935 getValue(a
, "isVoiceNote"),
936 getValue(a
, "isGif"),
937 getValue(a
, "isBorderless"));
942 public InputStream
retrieveAttachment(final String id
) throws IOException
{
943 throw new UnsupportedOperationException();
946 @SuppressWarnings("unchecked")
947 private <T
> T
getValue(
948 final Map
<String
, Variant
<?
>> stringVariantMap
, final String field
950 return (T
) stringVariantMap
.get(field
).getValue();