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
.AlreadyReceivingException
;
7 import org
.asamk
.signal
.manager
.api
.AttachmentInvalidException
;
8 import org
.asamk
.signal
.manager
.api
.CaptchaRequiredException
;
9 import org
.asamk
.signal
.manager
.api
.Configuration
;
10 import org
.asamk
.signal
.manager
.api
.Contact
;
11 import org
.asamk
.signal
.manager
.api
.Device
;
12 import org
.asamk
.signal
.manager
.api
.DeviceLinkUrl
;
13 import org
.asamk
.signal
.manager
.api
.Group
;
14 import org
.asamk
.signal
.manager
.api
.GroupId
;
15 import org
.asamk
.signal
.manager
.api
.GroupInviteLinkUrl
;
16 import org
.asamk
.signal
.manager
.api
.GroupNotFoundException
;
17 import org
.asamk
.signal
.manager
.api
.GroupPermission
;
18 import org
.asamk
.signal
.manager
.api
.GroupSendingNotAllowedException
;
19 import org
.asamk
.signal
.manager
.api
.Identity
;
20 import org
.asamk
.signal
.manager
.api
.IdentityVerificationCode
;
21 import org
.asamk
.signal
.manager
.api
.InactiveGroupLinkException
;
22 import org
.asamk
.signal
.manager
.api
.IncorrectPinException
;
23 import org
.asamk
.signal
.manager
.api
.InvalidDeviceLinkException
;
24 import org
.asamk
.signal
.manager
.api
.InvalidStickerException
;
25 import org
.asamk
.signal
.manager
.api
.InvalidUsernameException
;
26 import org
.asamk
.signal
.manager
.api
.LastGroupAdminException
;
27 import org
.asamk
.signal
.manager
.api
.Message
;
28 import org
.asamk
.signal
.manager
.api
.MessageEnvelope
;
29 import org
.asamk
.signal
.manager
.api
.NonNormalizedPhoneNumberException
;
30 import org
.asamk
.signal
.manager
.api
.NotAGroupMemberException
;
31 import org
.asamk
.signal
.manager
.api
.NotPrimaryDeviceException
;
32 import org
.asamk
.signal
.manager
.api
.Pair
;
33 import org
.asamk
.signal
.manager
.api
.PinLockedException
;
34 import org
.asamk
.signal
.manager
.api
.RateLimitException
;
35 import org
.asamk
.signal
.manager
.api
.ReceiveConfig
;
36 import org
.asamk
.signal
.manager
.api
.Recipient
;
37 import org
.asamk
.signal
.manager
.api
.RecipientAddress
;
38 import org
.asamk
.signal
.manager
.api
.RecipientIdentifier
;
39 import org
.asamk
.signal
.manager
.api
.SendGroupMessageResults
;
40 import org
.asamk
.signal
.manager
.api
.SendMessageResults
;
41 import org
.asamk
.signal
.manager
.api
.StickerPack
;
42 import org
.asamk
.signal
.manager
.api
.StickerPackInvalidException
;
43 import org
.asamk
.signal
.manager
.api
.StickerPackUrl
;
44 import org
.asamk
.signal
.manager
.api
.TypingAction
;
45 import org
.asamk
.signal
.manager
.api
.UnregisteredRecipientException
;
46 import org
.asamk
.signal
.manager
.api
.UpdateGroup
;
47 import org
.asamk
.signal
.manager
.api
.UpdateProfile
;
48 import org
.asamk
.signal
.manager
.api
.UserStatus
;
49 import org
.asamk
.signal
.manager
.api
.UsernameLinkUrl
;
50 import org
.freedesktop
.dbus
.DBusMap
;
51 import org
.freedesktop
.dbus
.DBusPath
;
52 import org
.freedesktop
.dbus
.connections
.impl
.DBusConnection
;
53 import org
.freedesktop
.dbus
.exceptions
.DBusException
;
54 import org
.freedesktop
.dbus
.exceptions
.DBusExecutionException
;
55 import org
.freedesktop
.dbus
.interfaces
.DBusInterface
;
56 import org
.freedesktop
.dbus
.interfaces
.DBusSigHandler
;
57 import org
.freedesktop
.dbus
.types
.Variant
;
60 import java
.io
.IOException
;
61 import java
.io
.InputStream
;
63 import java
.net
.URISyntaxException
;
64 import java
.time
.Duration
;
65 import java
.util
.ArrayList
;
66 import java
.util
.Collection
;
67 import java
.util
.HashMap
;
68 import java
.util
.HashSet
;
69 import java
.util
.List
;
71 import java
.util
.Objects
;
72 import java
.util
.Optional
;
74 import java
.util
.concurrent
.atomic
.AtomicInteger
;
75 import java
.util
.concurrent
.atomic
.AtomicLong
;
76 import java
.util
.function
.Function
;
77 import java
.util
.function
.Supplier
;
78 import java
.util
.stream
.Collectors
;
79 import java
.util
.stream
.Stream
;
82 * This class implements the Manager interface using the DBus Signal interface, where possible.
83 * It's used for the signal-cli dbus client mode (--dbus, --dbus-system)
85 public class DbusManagerImpl
implements Manager
{
87 private final Signal signal
;
88 private final DBusConnection connection
;
90 private final Set
<ReceiveMessageHandler
> weakHandlers
= new HashSet
<>();
91 private final Set
<ReceiveMessageHandler
> messageHandlers
= new HashSet
<>();
92 private final List
<Runnable
> closedListeners
= new ArrayList
<>();
93 private DBusSigHandler
<Signal
.MessageReceivedV2
> dbusMsgHandler
;
94 private DBusSigHandler
<Signal
.EditMessageReceived
> dbusEditMsgHandler
;
95 private DBusSigHandler
<Signal
.ReceiptReceivedV2
> dbusRcptHandler
;
96 private DBusSigHandler
<Signal
.SyncMessageReceivedV2
> dbusSyncHandler
;
98 public DbusManagerImpl(final Signal signal
, DBusConnection connection
) {
100 this.connection
= connection
;
104 public String
getSelfNumber() {
105 return signal
.getSelfNumber();
109 public Map
<String
, UserStatus
> getUserStatus(final Set
<String
> numbers
) throws IOException
{
110 final var numbersList
= new ArrayList
<>(numbers
);
111 final var registered
= signal
.isRegistered(numbersList
);
113 final var result
= new HashMap
<String
, UserStatus
>();
114 for (var i
= 0; i
< numbersList
.size(); i
++) {
115 result
.put(numbersList
.get(i
),
116 new UserStatus(numbersList
.get(i
),
117 registered
.get(i
) ? RecipientAddress
.UNKNOWN_UUID
: null,
124 public void updateAccountAttributes(
125 final String deviceName
, final Boolean unrestrictedUnidentifiedSender
126 ) throws IOException
{
127 if (deviceName
!= null) {
128 final var devicePath
= signal
.getThisDevice();
129 getRemoteObject(devicePath
, Signal
.Device
.class).Set("org.asamk.Signal.Device", "Name", deviceName
);
134 public Configuration
getConfiguration() {
135 final var configuration
= getRemoteObject(new DBusPath(signal
.getObjectPath() + "/Configuration"),
136 Signal
.Configuration
.class).GetAll("org.asamk.Signal.Configuration");
137 return new Configuration(Optional
.of((Boolean
) configuration
.get("ReadReceipts").getValue()),
138 Optional
.of((Boolean
) configuration
.get("UnidentifiedDeliveryIndicators").getValue()),
139 Optional
.of((Boolean
) configuration
.get("TypingIndicators").getValue()),
140 Optional
.of((Boolean
) configuration
.get("LinkPreviews").getValue()));
144 public void updateConfiguration(Configuration newConfiguration
) {
145 final var configuration
= getRemoteObject(new DBusPath(signal
.getObjectPath() + "/Configuration"),
146 Signal
.Configuration
.class);
147 newConfiguration
.readReceipts()
148 .ifPresent(v
-> configuration
.Set("org.asamk.Signal.Configuration", "ReadReceipts", v
));
149 newConfiguration
.unidentifiedDeliveryIndicators()
150 .ifPresent(v
-> configuration
.Set("org.asamk.Signal.Configuration",
151 "UnidentifiedDeliveryIndicators",
153 newConfiguration
.typingIndicators()
154 .ifPresent(v
-> configuration
.Set("org.asamk.Signal.Configuration", "TypingIndicators", v
));
155 newConfiguration
.linkPreviews()
156 .ifPresent(v
-> configuration
.Set("org.asamk.Signal.Configuration", "LinkPreviews", v
));
160 public void updateProfile(UpdateProfile updateProfile
) throws IOException
{
161 signal
.updateProfile(emptyIfNull(updateProfile
.getGivenName()),
162 emptyIfNull(updateProfile
.getFamilyName()),
163 emptyIfNull(updateProfile
.getAbout()),
164 emptyIfNull(updateProfile
.getAboutEmoji()),
165 updateProfile
.getAvatar() == null ?
"" : updateProfile
.getAvatar(),
166 updateProfile
.isDeleteAvatar());
170 public String
getUsername() {
171 throw new UnsupportedOperationException();
175 public UsernameLinkUrl
getUsernameLink() {
176 throw new UnsupportedOperationException();
180 public void setUsername(final String username
) throws IOException
, InvalidUsernameException
{
181 throw new UnsupportedOperationException();
185 public void deleteUsername() throws IOException
{
186 throw new UnsupportedOperationException();
190 public void startChangeNumber(
191 final String newNumber
, final boolean voiceVerification
, final String captcha
192 ) throws RateLimitException
, IOException
, CaptchaRequiredException
, NonNormalizedPhoneNumberException
{
193 throw new UnsupportedOperationException();
197 public void finishChangeNumber(
198 final String newNumber
, final String verificationCode
, final String pin
199 ) throws IncorrectPinException
, PinLockedException
, IOException
{
200 throw new UnsupportedOperationException();
204 public void unregister() throws IOException
{
209 public void deleteAccount() throws IOException
{
210 signal
.deleteAccount();
214 public void submitRateLimitRecaptchaChallenge(final String challenge
, final String captcha
) throws IOException
{
215 signal
.submitRateLimitChallenge(challenge
, captcha
);
219 public List
<Device
> getLinkedDevices() throws IOException
{
220 final var thisDevice
= signal
.getThisDevice();
221 return signal
.listDevices().stream().map(d
-> {
222 final var device
= getRemoteObject(d
.getObjectPath(),
223 Signal
.Device
.class).GetAll("org.asamk.Signal.Device");
224 return new Device((Integer
) device
.get("Id").getValue(),
225 (String
) device
.get("Name").getValue(),
226 (long) device
.get("Created").getValue(),
227 (long) device
.get("LastSeen").getValue(),
228 thisDevice
.equals(d
.getObjectPath()));
233 public void removeLinkedDevices(final int deviceId
) throws IOException
{
234 final var devicePath
= signal
.getDevice(deviceId
);
235 getRemoteObject(devicePath
, Signal
.Device
.class).removeDevice();
239 public void addDeviceLink(final DeviceLinkUrl linkUri
) throws IOException
, InvalidDeviceLinkException
{
240 signal
.addDevice(linkUri
.createDeviceLinkUri().toString());
244 public void setRegistrationLockPin(final Optional
<String
> pin
) throws IOException
{
245 if (pin
.isPresent()) {
246 signal
.setPin(pin
.get());
253 public List
<Group
> getGroups() {
254 final var groups
= signal
.listGroups();
255 return groups
.stream().map(Signal
.StructGroup
::getObjectPath
).map(this::getGroup
).toList();
259 public SendGroupMessageResults
quitGroup(
260 final GroupId groupId
, final Set
<RecipientIdentifier
.Single
> groupAdmins
261 ) throws GroupNotFoundException
, IOException
, NotAGroupMemberException
, LastGroupAdminException
{
262 if (!groupAdmins
.isEmpty()) {
263 throw new UnsupportedOperationException();
265 final var group
= getRemoteObject(signal
.getGroup(groupId
.serialize()), Signal
.Group
.class);
268 } catch (Signal
.Error
.GroupNotFound e
) {
269 throw new GroupNotFoundException(groupId
);
270 } catch (Signal
.Error
.NotAGroupMember e
) {
271 throw new NotAGroupMemberException(groupId
, group
.Get("org.asamk.Signal.Group", "Name"));
272 } catch (Signal
.Error
.LastGroupAdmin e
) {
273 throw new LastGroupAdminException(groupId
, group
.Get("org.asamk.Signal.Group", "Name"));
275 return new SendGroupMessageResults(0, List
.of());
279 public void deleteGroup(final GroupId groupId
) throws IOException
{
280 final var group
= getRemoteObject(signal
.getGroup(groupId
.serialize()), Signal
.Group
.class);
285 public Pair
<GroupId
, SendGroupMessageResults
> createGroup(
286 final String name
, final Set
<RecipientIdentifier
.Single
> members
, final String avatarFile
287 ) throws IOException
, AttachmentInvalidException
{
288 final var newGroupId
= signal
.createGroup(emptyIfNull(name
),
289 members
.stream().map(RecipientIdentifier
.Single
::getIdentifier
).toList(),
290 avatarFile
== null ?
"" : avatarFile
);
291 return new Pair
<>(GroupId
.unknownVersion(newGroupId
), new SendGroupMessageResults(0, List
.of()));
295 public SendGroupMessageResults
updateGroup(
296 final GroupId groupId
, final UpdateGroup updateGroup
297 ) throws IOException
, GroupNotFoundException
, AttachmentInvalidException
, NotAGroupMemberException
, GroupSendingNotAllowedException
{
298 final var group
= getRemoteObject(signal
.getGroup(groupId
.serialize()), Signal
.Group
.class);
299 if (updateGroup
.getName() != null) {
300 group
.Set("org.asamk.Signal.Group", "Name", updateGroup
.getName());
302 if (updateGroup
.getDescription() != null) {
303 group
.Set("org.asamk.Signal.Group", "Description", updateGroup
.getDescription());
305 if (updateGroup
.getAvatarFile() != null) {
306 group
.Set("org.asamk.Signal.Group",
308 updateGroup
.getAvatarFile() == null ?
"" : updateGroup
.getAvatarFile());
310 if (updateGroup
.getExpirationTimer() != null) {
311 group
.Set("org.asamk.Signal.Group", "MessageExpirationTimer", updateGroup
.getExpirationTimer());
313 if (updateGroup
.getAddMemberPermission() != null) {
314 group
.Set("org.asamk.Signal.Group", "PermissionAddMember", updateGroup
.getAddMemberPermission().name());
316 if (updateGroup
.getEditDetailsPermission() != null) {
317 group
.Set("org.asamk.Signal.Group", "PermissionEditDetails", updateGroup
.getEditDetailsPermission().name());
319 if (updateGroup
.getIsAnnouncementGroup() != null) {
320 group
.Set("org.asamk.Signal.Group",
321 "PermissionSendMessage",
322 updateGroup
.getIsAnnouncementGroup()
323 ? GroupPermission
.ONLY_ADMINS
.name()
324 : GroupPermission
.EVERY_MEMBER
.name());
326 if (updateGroup
.getMembers() != null) {
327 group
.addMembers(updateGroup
.getMembers().stream().map(RecipientIdentifier
.Single
::getIdentifier
).toList());
329 if (updateGroup
.getRemoveMembers() != null) {
330 group
.removeMembers(updateGroup
.getRemoveMembers()
332 .map(RecipientIdentifier
.Single
::getIdentifier
)
335 if (updateGroup
.getAdmins() != null) {
336 group
.addAdmins(updateGroup
.getAdmins().stream().map(RecipientIdentifier
.Single
::getIdentifier
).toList());
338 if (updateGroup
.getRemoveAdmins() != null) {
339 group
.removeAdmins(updateGroup
.getRemoveAdmins()
341 .map(RecipientIdentifier
.Single
::getIdentifier
)
344 if (updateGroup
.isResetGroupLink()) {
347 if (updateGroup
.getGroupLinkState() != null) {
348 switch (updateGroup
.getGroupLinkState()) {
349 case DISABLED
-> group
.disableLink();
350 case ENABLED
-> group
.enableLink(false);
351 case ENABLED_WITH_APPROVAL
-> group
.enableLink(true);
354 return new SendGroupMessageResults(0, List
.of());
358 public Pair
<GroupId
, SendGroupMessageResults
> joinGroup(final GroupInviteLinkUrl inviteLinkUrl
) throws IOException
, InactiveGroupLinkException
{
360 final var newGroupId
= signal
.joinGroup(inviteLinkUrl
.getUrl());
361 return new Pair
<>(GroupId
.unknownVersion(newGroupId
), new SendGroupMessageResults(0, List
.of()));
362 } catch (DBusExecutionException e
) {
363 throw new IOException("Failed to join group: " + e
.getMessage() + " (" + e
.getClass().getSimpleName() + ")",
369 public SendMessageResults
sendTypingMessage(
370 final TypingAction action
, final Set
<RecipientIdentifier
> recipients
371 ) throws IOException
, NotAGroupMemberException
, GroupNotFoundException
, GroupSendingNotAllowedException
{
372 return handleMessage(recipients
, numbers
-> {
373 numbers
.forEach(n
-> signal
.sendTyping(n
, action
== TypingAction
.STOP
));
376 signal
.sendTyping(signal
.getSelfNumber(), action
== TypingAction
.STOP
);
379 signal
.sendGroupTyping(groupId
, action
== TypingAction
.STOP
);
385 public SendMessageResults
sendReadReceipt(
386 final RecipientIdentifier
.Single sender
, final List
<Long
> messageIds
388 signal
.sendReadReceipt(sender
.getIdentifier(), messageIds
);
389 return new SendMessageResults(0, Map
.of());
393 public SendMessageResults
sendViewedReceipt(
394 final RecipientIdentifier
.Single sender
, final List
<Long
> messageIds
396 signal
.sendViewedReceipt(sender
.getIdentifier(), messageIds
);
397 return new SendMessageResults(0, Map
.of());
401 public SendMessageResults
sendMessage(
402 final Message message
, final Set
<RecipientIdentifier
> recipients
, final boolean notifySelf
403 ) throws IOException
, AttachmentInvalidException
, NotAGroupMemberException
, GroupNotFoundException
, GroupSendingNotAllowedException
{
404 return handleMessage(recipients
,
405 numbers
-> signal
.sendMessage(message
.messageText(), message
.attachments(), numbers
),
406 () -> signal
.sendNoteToSelfMessage(message
.messageText(), message
.attachments()),
407 groupId
-> signal
.sendGroupMessage(message
.messageText(), message
.attachments(), groupId
));
411 public SendMessageResults
sendEditMessage(
412 final Message message
, final Set
<RecipientIdentifier
> recipients
, final long editTargetTimestamp
413 ) throws IOException
, AttachmentInvalidException
, NotAGroupMemberException
, GroupNotFoundException
, GroupSendingNotAllowedException
, UnregisteredRecipientException
, InvalidStickerException
{
414 throw new UnsupportedOperationException();
418 public SendMessageResults
sendRemoteDeleteMessage(
419 final long targetSentTimestamp
, final Set
<RecipientIdentifier
> recipients
420 ) throws IOException
, NotAGroupMemberException
, GroupNotFoundException
, GroupSendingNotAllowedException
{
421 return handleMessage(recipients
,
422 numbers
-> signal
.sendRemoteDeleteMessage(targetSentTimestamp
, numbers
),
423 () -> signal
.sendRemoteDeleteMessage(targetSentTimestamp
, signal
.getSelfNumber()),
424 groupId
-> signal
.sendGroupRemoteDeleteMessage(targetSentTimestamp
, groupId
));
428 public SendMessageResults
sendMessageReaction(
430 final boolean remove
,
431 final RecipientIdentifier
.Single targetAuthor
,
432 final long targetSentTimestamp
,
433 final Set
<RecipientIdentifier
> recipients
,
434 final boolean isStory
435 ) throws IOException
, NotAGroupMemberException
, GroupNotFoundException
, GroupSendingNotAllowedException
{
436 return handleMessage(recipients
,
437 numbers
-> signal
.sendMessageReaction(emoji
,
439 targetAuthor
.getIdentifier(),
442 () -> signal
.sendMessageReaction(emoji
,
444 targetAuthor
.getIdentifier(),
446 signal
.getSelfNumber()),
447 groupId
-> signal
.sendGroupMessageReaction(emoji
,
449 targetAuthor
.getIdentifier(),
455 public SendMessageResults
sendPaymentNotificationMessage(
456 final byte[] receipt
, final String note
, final RecipientIdentifier
.Single recipient
457 ) throws IOException
{
458 final var timestamp
= signal
.sendPaymentNotification(receipt
, note
, recipient
.getIdentifier());
459 return new SendMessageResults(timestamp
, Map
.of());
463 public SendMessageResults
sendEndSessionMessage(final Set
<RecipientIdentifier
.Single
> recipients
) throws IOException
{
464 signal
.sendEndSessionMessage(recipients
.stream().map(RecipientIdentifier
.Single
::getIdentifier
).toList());
465 return new SendMessageResults(0, Map
.of());
468 public void hideRecipient(final RecipientIdentifier
.Single recipient
) {
469 throw new UnsupportedOperationException();
473 public void deleteRecipient(final RecipientIdentifier
.Single recipient
) {
474 signal
.deleteRecipient(recipient
.getIdentifier());
478 public void deleteContact(final RecipientIdentifier
.Single recipient
) {
479 signal
.deleteContact(recipient
.getIdentifier());
483 public void setContactName(
484 final RecipientIdentifier
.Single recipient
, final String givenName
, final String familyName
485 ) throws NotPrimaryDeviceException
{
486 signal
.setContactName(recipient
.getIdentifier(), givenName
);
490 public void setContactsBlocked(
491 final Collection
<RecipientIdentifier
.Single
> recipients
, final boolean blocked
492 ) throws NotPrimaryDeviceException
, IOException
{
493 for (final var recipient
: recipients
) {
494 signal
.setContactBlocked(recipient
.getIdentifier(), blocked
);
499 public void setGroupsBlocked(
500 final Collection
<GroupId
> groupIds
, final boolean blocked
501 ) throws GroupNotFoundException
, IOException
{
502 for (final var groupId
: groupIds
) {
503 setGroupProperty(groupId
, "IsBlocked", blocked
);
507 private void setGroupProperty(final GroupId groupId
, final String propertyName
, final boolean blocked
) {
508 final var group
= getRemoteObject(signal
.getGroup(groupId
.serialize()), Signal
.Group
.class);
509 group
.Set("org.asamk.Signal.Group", propertyName
, blocked
);
513 public void setExpirationTimer(
514 final RecipientIdentifier
.Single recipient
, final int messageExpirationTimer
515 ) throws IOException
{
516 signal
.setExpirationTimer(recipient
.getIdentifier(), messageExpirationTimer
);
520 public StickerPackUrl
uploadStickerPack(final File path
) throws IOException
, StickerPackInvalidException
{
522 return StickerPackUrl
.fromUri(new URI(signal
.uploadStickerPack(path
.getPath())));
523 } catch (URISyntaxException
| StickerPackUrl
.InvalidStickerPackLinkException e
) {
524 throw new AssertionError(e
);
529 public void installStickerPack(final StickerPackUrl url
) throws IOException
{
530 throw new UnsupportedOperationException();
534 public List
<StickerPack
> getStickerPacks() {
535 throw new UnsupportedOperationException();
539 public void requestAllSyncData() throws IOException
{
540 signal
.sendSyncRequest();
544 public void addReceiveHandler(final ReceiveMessageHandler handler
, final boolean isWeakListener
) {
545 synchronized (messageHandlers
) {
546 if (isWeakListener
) {
547 weakHandlers
.add(handler
);
549 if (messageHandlers
.isEmpty()) {
550 installMessageHandlers();
552 messageHandlers
.add(handler
);
558 public void removeReceiveHandler(final ReceiveMessageHandler handler
) {
559 synchronized (messageHandlers
) {
560 weakHandlers
.remove(handler
);
561 messageHandlers
.remove(handler
);
562 if (messageHandlers
.isEmpty()) {
563 uninstallMessageHandlers();
569 public boolean isReceiving() {
570 synchronized (messageHandlers
) {
571 return !messageHandlers
.isEmpty();
575 private Thread receiveThread
;
578 public void receiveMessages(
579 Optional
<Duration
> timeout
, Optional
<Integer
> maxMessages
, ReceiveMessageHandler handler
580 ) throws IOException
, AlreadyReceivingException
{
581 if (receiveThread
!= null) {
582 throw new AlreadyReceivingException("Already receiving message.");
584 receiveThread
= Thread
.currentThread();
586 final var remainingMessages
= new AtomicInteger(maxMessages
.orElse(-1));
587 final var lastMessage
= new AtomicLong(System
.currentTimeMillis());
588 final var thread
= Thread
.currentThread();
590 final ReceiveMessageHandler receiveHandler
= (envelope
, e
) -> {
591 lastMessage
.set(System
.currentTimeMillis());
592 handler
.handleMessage(envelope
, e
);
593 if (remainingMessages
.get() > 0) {
594 if (remainingMessages
.decrementAndGet() <= 0) {
595 remainingMessages
.set(0);
600 addReceiveHandler(receiveHandler
);
601 if (timeout
.isPresent()) {
602 while (remainingMessages
.get() != 0) {
604 final var passedTime
= System
.currentTimeMillis() - lastMessage
.get();
605 final var sleepTimeRemaining
= timeout
.get().toMillis() - passedTime
;
606 if (sleepTimeRemaining
< 0) {
609 Thread
.sleep(sleepTimeRemaining
);
610 } catch (InterruptedException ignored
) {
616 synchronized (this) {
619 } catch (InterruptedException ignored
) {
623 removeReceiveHandler(receiveHandler
);
624 receiveThread
= null;
628 public void stopReceiveMessages() {
629 if (receiveThread
!= null) {
630 receiveThread
.interrupt();
635 public void setReceiveConfig(final ReceiveConfig receiveConfig
) {
639 public boolean isContactBlocked(final RecipientIdentifier
.Single recipient
) {
640 return signal
.isContactBlocked(recipient
.getIdentifier());
644 public void sendContacts() throws IOException
{
645 signal
.sendContacts();
649 public List
<Recipient
> getRecipients(
650 final boolean onlyContacts
,
651 final Optional
<Boolean
> blocked
,
652 final Collection
<RecipientIdentifier
.Single
> addresses
,
653 final Optional
<String
> name
655 final var numbers
= addresses
.stream()
656 .filter(s
-> s
instanceof RecipientIdentifier
.Number
)
657 .map(s
-> ((RecipientIdentifier
.Number
) s
).number())
658 .collect(Collectors
.toSet());
659 return signal
.listNumbers().stream().filter(n
-> addresses
.isEmpty() || numbers
.contains(n
)).map(n
-> {
660 final var contactBlocked
= signal
.isContactBlocked(n
);
661 if (blocked
.isPresent() && blocked
.get() != contactBlocked
) {
664 final var contactName
= signal
.getContactName(n
);
665 if (onlyContacts
&& contactName
.isEmpty()) {
668 if (name
.isPresent() && !name
.get().equals(contactName
)) {
671 return Recipient
.newBuilder()
672 .withAddress(new RecipientAddress(null, n
))
673 .withContact(new Contact(contactName
, null, null, 0, contactBlocked
, false, false, false))
675 }).filter(Objects
::nonNull
).toList();
679 public String
getContactOrProfileName(final RecipientIdentifier
.Single recipient
) {
680 return signal
.getContactName(recipient
.getIdentifier());
684 public Group
getGroup(final GroupId groupId
) {
685 final var groupPath
= signal
.getGroup(groupId
.serialize());
686 return getGroup(groupPath
);
689 @SuppressWarnings("unchecked")
690 private Group
getGroup(final DBusPath groupPath
) {
691 final var group
= getRemoteObject(groupPath
, Signal
.Group
.class).GetAll("org.asamk.Signal.Group");
692 final var id
= (byte[]) group
.get("Id").getValue();
694 return new Group(GroupId
.unknownVersion(id
),
695 (String
) group
.get("Name").getValue(),
696 (String
) group
.get("Description").getValue(),
697 GroupInviteLinkUrl
.fromUri((String
) group
.get("GroupInviteLink").getValue()),
698 ((List
<String
>) group
.get("Members").getValue()).stream()
699 .map(m
-> new RecipientAddress(null, m
))
700 .collect(Collectors
.toSet()),
701 ((List
<String
>) group
.get("PendingMembers").getValue()).stream()
702 .map(m
-> new RecipientAddress(null, m
))
703 .collect(Collectors
.toSet()),
704 ((List
<String
>) group
.get("RequestingMembers").getValue()).stream()
705 .map(m
-> new RecipientAddress(null, m
))
706 .collect(Collectors
.toSet()),
707 ((List
<String
>) group
.get("Admins").getValue()).stream()
708 .map(m
-> new RecipientAddress(null, m
))
709 .collect(Collectors
.toSet()),
710 ((List
<String
>) group
.get("Banned").getValue()).stream()
711 .map(m
-> new RecipientAddress(null, m
))
712 .collect(Collectors
.toSet()),
713 (boolean) group
.get("IsBlocked").getValue(),
714 (int) group
.get("MessageExpirationTimer").getValue(),
715 GroupPermission
.valueOf((String
) group
.get("PermissionAddMember").getValue()),
716 GroupPermission
.valueOf((String
) group
.get("PermissionEditDetails").getValue()),
717 GroupPermission
.valueOf((String
) group
.get("PermissionSendMessage").getValue()),
718 (boolean) group
.get("IsMember").getValue(),
719 (boolean) group
.get("IsAdmin").getValue());
720 } catch (GroupInviteLinkUrl
.InvalidGroupLinkException
| GroupInviteLinkUrl
.UnknownGroupLinkVersionException e
) {
721 throw new AssertionError(e
);
726 public List
<Identity
> getIdentities() {
727 throw new UnsupportedOperationException();
731 public List
<Identity
> getIdentities(final RecipientIdentifier
.Single recipient
) {
732 throw new UnsupportedOperationException();
736 public boolean trustIdentityVerified(
737 final RecipientIdentifier
.Single recipient
, final IdentityVerificationCode verificationCode
739 throw new UnsupportedOperationException();
743 public boolean trustIdentityAllKeys(final RecipientIdentifier
.Single recipient
) {
744 throw new UnsupportedOperationException();
748 public void addAddressChangedListener(final Runnable listener
) {
752 public void addClosedListener(final Runnable listener
) {
753 synchronized (closedListeners
) {
754 closedListeners
.add(listener
);
759 public void close() {
760 synchronized (this) {
763 synchronized (messageHandlers
) {
764 if (!messageHandlers
.isEmpty()) {
765 uninstallMessageHandlers();
767 weakHandlers
.clear();
768 messageHandlers
.clear();
770 synchronized (closedListeners
) {
771 closedListeners
.forEach(Runnable
::run
);
772 closedListeners
.clear();
776 private SendMessageResults
handleMessage(
777 Set
<RecipientIdentifier
> recipients
,
778 Function
<List
<String
>, Long
> recipientsHandler
,
779 Supplier
<Long
> noteToSelfHandler
,
780 Function
<byte[], Long
> groupHandler
783 final var singleRecipients
= recipients
.stream()
784 .filter(r
-> r
instanceof RecipientIdentifier
.Single
)
785 .map(RecipientIdentifier
.Single
.class::cast
)
786 .map(RecipientIdentifier
.Single
::getIdentifier
)
788 if (!singleRecipients
.isEmpty()) {
789 timestamp
= recipientsHandler
.apply(singleRecipients
);
792 if (recipients
.contains(RecipientIdentifier
.NoteToSelf
.INSTANCE
)) {
793 timestamp
= noteToSelfHandler
.get();
795 final var groupRecipients
= recipients
.stream()
796 .filter(r
-> r
instanceof RecipientIdentifier
.Group
)
797 .map(RecipientIdentifier
.Group
.class::cast
)
798 .map(RecipientIdentifier
.Group
::groupId
)
800 for (final var groupId
: groupRecipients
) {
801 timestamp
= groupHandler
.apply(groupId
.serialize());
803 return new SendMessageResults(timestamp
, Map
.of());
806 private String
emptyIfNull(final String string
) {
807 return string
== null ?
"" : string
;
810 private <T
extends DBusInterface
> T
getRemoteObject(final DBusPath path
, final Class
<T
> type
) {
812 return connection
.getRemoteObject(DbusConfig
.getBusname(), path
.getPath(), type
);
813 } catch (DBusException e
) {
814 throw new AssertionError(e
);
818 private void installMessageHandlers() {
820 this.dbusMsgHandler
= messageReceived
-> {
821 final var extras
= messageReceived
.getExtras();
822 final var envelope
= new MessageEnvelope(Optional
.of(new RecipientAddress(null,
823 messageReceived
.getSender())),
825 messageReceived
.getTimestamp(),
831 Optional
.of(new MessageEnvelope
.Data(messageReceived
.getTimestamp(),
832 messageReceived
.getGroupId().length
> 0
833 ? Optional
.of(new MessageEnvelope
.Data
.GroupContext(GroupId
.unknownVersion(
834 messageReceived
.getGroupId()), false, 0))
838 Optional
.of(messageReceived
.getMessage()),
848 getAttachments(extras
),
859 notifyMessageHandlers(envelope
);
861 connection
.addSigHandler(Signal
.MessageReceivedV2
.class, signal
, this.dbusMsgHandler
);
862 this.dbusEditMsgHandler
= messageReceived
-> {
863 final var extras
= messageReceived
.getExtras();
864 final var envelope
= new MessageEnvelope(Optional
.of(new RecipientAddress(null,
865 messageReceived
.getSender())),
867 messageReceived
.getTimestamp(),
874 Optional
.of(new MessageEnvelope
.Edit(messageReceived
.getTargetSentTimestamp(),
875 new MessageEnvelope
.Data(messageReceived
.getTimestamp(),
876 messageReceived
.getGroupId().length
> 0
877 ? Optional
.of(new MessageEnvelope
.Data
.GroupContext(GroupId
.unknownVersion(
878 messageReceived
.getGroupId()), false, 0))
882 Optional
.of(messageReceived
.getMessage()),
892 getAttachments(extras
),
902 notifyMessageHandlers(envelope
);
904 connection
.addSigHandler(Signal
.EditMessageReceived
.class, signal
, this.dbusEditMsgHandler
);
906 this.dbusRcptHandler
= receiptReceived
-> {
907 final var type
= switch (receiptReceived
.getReceiptType()) {
908 case "read" -> MessageEnvelope
.Receipt
.Type
.READ
;
909 case "viewed" -> MessageEnvelope
.Receipt
.Type
.VIEWED
;
910 case "delivery" -> MessageEnvelope
.Receipt
.Type
.DELIVERY
;
911 default -> MessageEnvelope
.Receipt
.Type
.UNKNOWN
;
913 final var envelope
= new MessageEnvelope(Optional
.of(new RecipientAddress(null,
914 receiptReceived
.getSender())),
916 receiptReceived
.getTimestamp(),
920 Optional
.of(new MessageEnvelope
.Receipt(receiptReceived
.getTimestamp(),
922 List
.of(receiptReceived
.getTimestamp()))),
929 notifyMessageHandlers(envelope
);
931 connection
.addSigHandler(Signal
.ReceiptReceivedV2
.class, signal
, this.dbusRcptHandler
);
933 this.dbusSyncHandler
= syncReceived
-> {
934 final var extras
= syncReceived
.getExtras();
935 final var envelope
= new MessageEnvelope(Optional
.of(new RecipientAddress(null,
936 syncReceived
.getSource())),
938 syncReceived
.getTimestamp(),
946 Optional
.of(new MessageEnvelope
.Sync(Optional
.of(new MessageEnvelope
.Sync
.Sent(syncReceived
.getTimestamp(),
947 syncReceived
.getTimestamp(),
948 syncReceived
.getDestination().isEmpty()
950 : Optional
.of(new RecipientAddress(null, syncReceived
.getDestination())),
952 Optional
.of(new MessageEnvelope
.Data(syncReceived
.getTimestamp(),
953 syncReceived
.getGroupId().length
> 0
954 ? Optional
.of(new MessageEnvelope
.Data
.GroupContext(GroupId
.unknownVersion(
955 syncReceived
.getGroupId()), false, 0))
959 Optional
.of(syncReceived
.getMessage()),
969 getAttachments(extras
),
987 notifyMessageHandlers(envelope
);
989 connection
.addSigHandler(Signal
.SyncMessageReceivedV2
.class, signal
, this.dbusSyncHandler
);
990 } catch (DBusException e
) {
991 throw new RuntimeException(e
);
993 signal
.subscribeReceive();
996 private void notifyMessageHandlers(final MessageEnvelope envelope
) {
997 synchronized (messageHandlers
) {
998 Stream
.concat(messageHandlers
.stream(), weakHandlers
.stream())
999 .forEach(h
-> h
.handleMessage(envelope
, null));
1003 private void uninstallMessageHandlers() {
1005 signal
.unsubscribeReceive();
1006 connection
.removeSigHandler(Signal
.MessageReceivedV2
.class, signal
, this.dbusMsgHandler
);
1007 connection
.removeSigHandler(Signal
.EditMessageReceived
.class, signal
, this.dbusEditMsgHandler
);
1008 connection
.removeSigHandler(Signal
.ReceiptReceivedV2
.class, signal
, this.dbusRcptHandler
);
1009 connection
.removeSigHandler(Signal
.SyncMessageReceivedV2
.class, signal
, this.dbusSyncHandler
);
1010 } catch (DBusException e
) {
1011 throw new RuntimeException(e
);
1015 private List
<MessageEnvelope
.Data
.Attachment
> getAttachments(final Map
<String
, Variant
<?
>> extras
) {
1016 if (!extras
.containsKey("attachments")) {
1020 final List
<DBusMap
<String
, Variant
<?
>>> attachments
= getValue(extras
, "attachments");
1021 return attachments
.stream().map(a
-> {
1022 final String file
= a
.containsKey("file") ?
getValue(a
, "file") : null;
1023 return new MessageEnvelope
.Data
.Attachment(a
.containsKey("remoteId")
1024 ? Optional
.of(getValue(a
, "remoteId"))
1026 file
!= null ? Optional
.of(new File(file
)) : Optional
.empty(),
1028 getValue(a
, "contentType"),
1036 getValue(a
, "isVoiceNote"),
1037 getValue(a
, "isGif"),
1038 getValue(a
, "isBorderless"));
1043 public InputStream
retrieveAttachment(final String id
) throws IOException
{
1044 throw new UnsupportedOperationException();
1047 @SuppressWarnings("unchecked")
1048 private <T
> T
getValue(
1049 final Map
<String
, Variant
<?
>> stringVariantMap
, final String field
1051 return (T
) stringVariantMap
.get(field
).getValue();