1 package org
.asamk
.signal
.dbus
;
3 import org
.asamk
.Signal
;
4 import org
.asamk
.signal
.DbusConfig
;
5 import org
.asamk
.signal
.manager
.AttachmentInvalidException
;
6 import org
.asamk
.signal
.manager
.Manager
;
7 import org
.asamk
.signal
.manager
.NotMasterDeviceException
;
8 import org
.asamk
.signal
.manager
.StickerPackInvalidException
;
9 import org
.asamk
.signal
.manager
.UntrustedIdentityException
;
10 import org
.asamk
.signal
.manager
.api
.Configuration
;
11 import org
.asamk
.signal
.manager
.api
.Device
;
12 import org
.asamk
.signal
.manager
.api
.Group
;
13 import org
.asamk
.signal
.manager
.api
.Identity
;
14 import org
.asamk
.signal
.manager
.api
.Message
;
15 import org
.asamk
.signal
.manager
.api
.RecipientIdentifier
;
16 import org
.asamk
.signal
.manager
.api
.SendGroupMessageResults
;
17 import org
.asamk
.signal
.manager
.api
.SendMessageResults
;
18 import org
.asamk
.signal
.manager
.api
.TypingAction
;
19 import org
.asamk
.signal
.manager
.api
.UpdateGroup
;
20 import org
.asamk
.signal
.manager
.groups
.GroupId
;
21 import org
.asamk
.signal
.manager
.groups
.GroupInviteLinkUrl
;
22 import org
.asamk
.signal
.manager
.groups
.GroupNotFoundException
;
23 import org
.asamk
.signal
.manager
.groups
.GroupPermission
;
24 import org
.asamk
.signal
.manager
.groups
.GroupSendingNotAllowedException
;
25 import org
.asamk
.signal
.manager
.groups
.LastGroupAdminException
;
26 import org
.asamk
.signal
.manager
.groups
.NotAGroupMemberException
;
27 import org
.asamk
.signal
.manager
.storage
.recipients
.Contact
;
28 import org
.asamk
.signal
.manager
.storage
.recipients
.Profile
;
29 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientAddress
;
30 import org
.freedesktop
.dbus
.DBusPath
;
31 import org
.freedesktop
.dbus
.connections
.impl
.DBusConnection
;
32 import org
.freedesktop
.dbus
.exceptions
.DBusException
;
33 import org
.freedesktop
.dbus
.interfaces
.DBusInterface
;
34 import org
.whispersystems
.libsignal
.InvalidKeyException
;
35 import org
.whispersystems
.libsignal
.util
.Pair
;
36 import org
.whispersystems
.libsignal
.util
.guava
.Optional
;
37 import org
.whispersystems
.signalservice
.api
.groupsv2
.GroupLinkNotActiveException
;
38 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceAttachmentRemoteId
;
39 import org
.whispersystems
.signalservice
.api
.push
.SignalServiceAddress
;
40 import org
.whispersystems
.signalservice
.api
.push
.exceptions
.UnregisteredUserException
;
41 import org
.whispersystems
.signalservice
.api
.util
.UuidUtil
;
42 import org
.whispersystems
.signalservice
.internal
.contacts
.crypto
.UnauthenticatedResponseException
;
45 import java
.io
.IOException
;
47 import java
.net
.URISyntaxException
;
48 import java
.util
.ArrayList
;
49 import java
.util
.HashMap
;
50 import java
.util
.List
;
53 import java
.util
.UUID
;
54 import java
.util
.concurrent
.TimeUnit
;
55 import java
.util
.function
.Function
;
56 import java
.util
.function
.Supplier
;
57 import java
.util
.stream
.Collectors
;
60 * This class implements the Manager interface using the DBus Signal interface, where possible.
61 * It's used for the signal-cli dbus client mode (--dbus, --dbus-system)
63 public class DbusManagerImpl
implements Manager
{
65 private final Signal signal
;
66 private final DBusConnection connection
;
68 public DbusManagerImpl(final Signal signal
, DBusConnection connection
) {
70 this.connection
= connection
;
74 public String
getSelfNumber() {
75 return signal
.getSelfNumber();
79 public void checkAccountState() throws IOException
{
80 throw new UnsupportedOperationException();
84 public Map
<String
, Pair
<String
, UUID
>> areUsersRegistered(final Set
<String
> numbers
) throws IOException
{
85 final var numbersList
= new ArrayList
<>(numbers
);
86 final var registered
= signal
.isRegistered(numbersList
);
88 final var result
= new HashMap
<String
, Pair
<String
, UUID
>>();
89 for (var i
= 0; i
< numbersList
.size(); i
++) {
90 result
.put(numbersList
.get(i
),
91 new Pair
<>(numbersList
.get(i
), registered
.get(i
) ? UuidUtil
.UNKNOWN_UUID
: null));
97 public void updateAccountAttributes(final String deviceName
) throws IOException
{
98 if (deviceName
!= null) {
99 final var devicePath
= signal
.getThisDevice();
100 getRemoteObject(devicePath
, Signal
.Device
.class).Set("org.asamk.Signal.Device", "Name", deviceName
);
106 public List
<Boolean
> getConfiguration() {
107 throw new UnsupportedOperationException();
111 public void updateConfiguration(
112 final Boolean readReceipts
,
113 final Boolean unidentifiedDeliveryIndicators
,
114 final Boolean typingIndicators
,
115 final Boolean linkPreviews
118 throw new UnsupportedOperationException();
122 public void setProfile(
123 final String givenName
,
124 final String familyName
,
126 final String aboutEmoji
,
127 final Optional
<File
> avatar
128 ) throws IOException
{
129 signal
.updateProfile(emptyIfNull(givenName
),
130 emptyIfNull(familyName
),
132 emptyIfNull(aboutEmoji
),
133 avatar
== null ?
"" : avatar
.transform(File
::getPath
).or(""),
134 avatar
!= null && !avatar
.isPresent());
138 public void unregister() throws IOException
{
139 throw new UnsupportedOperationException();
143 public void deleteAccount() throws IOException
{
144 throw new UnsupportedOperationException();
148 public void submitRateLimitRecaptchaChallenge(final String challenge
, final String captcha
) throws IOException
{
149 throw new UnsupportedOperationException();
153 public List
<Device
> getLinkedDevices() throws IOException
{
154 final var thisDevice
= signal
.getThisDevice();
155 return signal
.listDevices().stream().map(d
-> {
156 final var device
= getRemoteObject(d
.getObjectPath(),
157 Signal
.Device
.class).GetAll("org.asamk.Signal.Device");
158 return new Device((long) device
.get("Id").getValue(),
159 (String
) device
.get("Name").getValue(),
160 (long) device
.get("Created").getValue(),
161 (long) device
.get("LastSeen").getValue(),
162 thisDevice
.equals(d
.getObjectPath()));
163 }).collect(Collectors
.toList());
167 public void removeLinkedDevices(final long deviceId
) throws IOException
{
168 final var devicePath
= signal
.getDevice(deviceId
);
169 getRemoteObject(devicePath
, Signal
.Device
.class).removeDevice();
173 public void addDeviceLink(final URI linkUri
) throws IOException
, InvalidKeyException
{
174 signal
.addDevice(linkUri
.toString());
178 public void setRegistrationLockPin(final Optional
<String
> pin
) throws IOException
, UnauthenticatedResponseException
{
179 if (pin
.isPresent()) {
180 signal
.setPin(pin
.get());
187 public Profile
getRecipientProfile(final RecipientIdentifier
.Single recipient
) throws UnregisteredUserException
{
188 throw new UnsupportedOperationException();
192 public List
<Group
> getGroups() {
193 final var groups
= signal
.listGroups();
194 return groups
.stream().map(Signal
.StructGroup
::getObjectPath
).map(this::getGroup
).collect(Collectors
.toList());
198 public SendGroupMessageResults
quitGroup(
199 final GroupId groupId
, final Set
<RecipientIdentifier
.Single
> groupAdmins
200 ) throws GroupNotFoundException
, IOException
, NotAGroupMemberException
, LastGroupAdminException
{
201 if (groupAdmins
.size() > 0) {
202 throw new UnsupportedOperationException();
204 final var group
= getRemoteObject(signal
.getGroup(groupId
.serialize()), Signal
.Group
.class);
206 return new SendGroupMessageResults(0, List
.of());
210 public void deleteGroup(final GroupId groupId
) throws IOException
{
211 throw new UnsupportedOperationException();
215 public Pair
<GroupId
, SendGroupMessageResults
> createGroup(
216 final String name
, final Set
<RecipientIdentifier
.Single
> members
, final File avatarFile
217 ) throws IOException
, AttachmentInvalidException
{
218 final var newGroupId
= signal
.createGroup(emptyIfNull(name
),
219 members
.stream().map(RecipientIdentifier
.Single
::getIdentifier
).collect(Collectors
.toList()),
220 avatarFile
== null ?
"" : avatarFile
.getPath());
221 return new Pair
<>(GroupId
.unknownVersion(newGroupId
), new SendGroupMessageResults(0, List
.of()));
225 public SendGroupMessageResults
updateGroup(
226 final GroupId groupId
, final UpdateGroup updateGroup
227 ) throws IOException
, GroupNotFoundException
, AttachmentInvalidException
, NotAGroupMemberException
, GroupSendingNotAllowedException
{
228 final var group
= getRemoteObject(signal
.getGroup(groupId
.serialize()), Signal
.Group
.class);
229 if (updateGroup
.getName() != null) {
230 group
.Set("org.asamk.Signal.Group", "Name", updateGroup
.getName());
232 if (updateGroup
.getDescription() != null) {
233 group
.Set("org.asamk.Signal.Group", "Description", updateGroup
.getDescription());
235 if (updateGroup
.getAvatarFile() != null) {
236 group
.Set("org.asamk.Signal.Group",
238 updateGroup
.getAvatarFile() == null ?
"" : updateGroup
.getAvatarFile().getPath());
240 if (updateGroup
.getExpirationTimer() != null) {
241 group
.Set("org.asamk.Signal.Group", "MessageExpirationTimer", updateGroup
.getExpirationTimer());
243 if (updateGroup
.getAddMemberPermission() != null) {
244 group
.Set("org.asamk.Signal.Group", "PermissionAddMember", updateGroup
.getAddMemberPermission().name());
246 if (updateGroup
.getEditDetailsPermission() != null) {
247 group
.Set("org.asamk.Signal.Group", "PermissionEditDetails", updateGroup
.getEditDetailsPermission().name());
249 if (updateGroup
.getIsAnnouncementGroup() != null) {
250 group
.Set("org.asamk.Signal.Group",
251 "PermissionSendMessage",
252 updateGroup
.getIsAnnouncementGroup()
253 ? GroupPermission
.ONLY_ADMINS
.name()
254 : GroupPermission
.EVERY_MEMBER
.name());
256 if (updateGroup
.getMembers() != null) {
257 group
.addMembers(updateGroup
.getMembers()
259 .map(RecipientIdentifier
.Single
::getIdentifier
)
260 .collect(Collectors
.toList()));
262 if (updateGroup
.getRemoveMembers() != null) {
263 group
.removeMembers(updateGroup
.getRemoveMembers()
265 .map(RecipientIdentifier
.Single
::getIdentifier
)
266 .collect(Collectors
.toList()));
268 if (updateGroup
.getAdmins() != null) {
269 group
.addAdmins(updateGroup
.getAdmins()
271 .map(RecipientIdentifier
.Single
::getIdentifier
)
272 .collect(Collectors
.toList()));
274 if (updateGroup
.getRemoveAdmins() != null) {
275 group
.removeAdmins(updateGroup
.getRemoveAdmins()
277 .map(RecipientIdentifier
.Single
::getIdentifier
)
278 .collect(Collectors
.toList()));
280 if (updateGroup
.isResetGroupLink()) {
283 if (updateGroup
.getGroupLinkState() != null) {
284 switch (updateGroup
.getGroupLinkState()) {
289 group
.enableLink(false);
291 case ENABLED_WITH_APPROVAL
:
292 group
.enableLink(true);
296 return new SendGroupMessageResults(0, List
.of());
300 public Pair
<GroupId
, SendGroupMessageResults
> joinGroup(final GroupInviteLinkUrl inviteLinkUrl
) throws IOException
, GroupLinkNotActiveException
{
301 final var newGroupId
= signal
.joinGroup(inviteLinkUrl
.getUrl());
302 return new Pair
<>(GroupId
.unknownVersion(newGroupId
), new SendGroupMessageResults(0, List
.of()));
306 public void sendTypingMessage(
307 final TypingAction action
, final Set
<RecipientIdentifier
> recipients
308 ) throws IOException
, UntrustedIdentityException
, NotAGroupMemberException
, GroupNotFoundException
, GroupSendingNotAllowedException
{
309 for (final var recipient
: recipients
) {
310 if (recipient
instanceof RecipientIdentifier
.Single
) {
311 signal
.sendTyping(((RecipientIdentifier
.Single
) recipient
).getIdentifier(),
312 action
== TypingAction
.STOP
);
313 } else if (recipient
instanceof RecipientIdentifier
.Group
) {
314 throw new UnsupportedOperationException();
320 public void sendReadReceipt(
321 final RecipientIdentifier
.Single sender
, final List
<Long
> messageIds
322 ) throws IOException
, UntrustedIdentityException
{
323 signal
.sendReadReceipt(sender
.getIdentifier(), messageIds
);
327 public void sendViewedReceipt(
328 final RecipientIdentifier
.Single sender
, final List
<Long
> messageIds
329 ) throws IOException
, UntrustedIdentityException
{
330 throw new UnsupportedOperationException();
334 public SendMessageResults
sendMessage(
335 final Message message
, final Set
<RecipientIdentifier
> recipients
336 ) throws IOException
, AttachmentInvalidException
, NotAGroupMemberException
, GroupNotFoundException
, GroupSendingNotAllowedException
{
337 return handleMessage(recipients
,
338 numbers
-> signal
.sendMessage(message
.getMessageText(), message
.getAttachments(), numbers
),
339 () -> signal
.sendNoteToSelfMessage(message
.getMessageText(), message
.getAttachments()),
340 groupId
-> signal
.sendGroupMessage(message
.getMessageText(), message
.getAttachments(), groupId
));
344 public SendMessageResults
sendRemoteDeleteMessage(
345 final long targetSentTimestamp
, final Set
<RecipientIdentifier
> recipients
346 ) throws IOException
, NotAGroupMemberException
, GroupNotFoundException
, GroupSendingNotAllowedException
{
347 return handleMessage(recipients
,
348 numbers
-> signal
.sendRemoteDeleteMessage(targetSentTimestamp
, numbers
),
349 () -> signal
.sendRemoteDeleteMessage(targetSentTimestamp
, signal
.getSelfNumber()),
350 groupId
-> signal
.sendGroupRemoteDeleteMessage(targetSentTimestamp
, groupId
));
354 public SendMessageResults
sendMessageReaction(
356 final boolean remove
,
357 final RecipientIdentifier
.Single targetAuthor
,
358 final long targetSentTimestamp
,
359 final Set
<RecipientIdentifier
> recipients
360 ) throws IOException
, NotAGroupMemberException
, GroupNotFoundException
, GroupSendingNotAllowedException
{
361 return handleMessage(recipients
,
362 numbers
-> signal
.sendMessageReaction(emoji
,
364 targetAuthor
.getIdentifier(),
367 () -> signal
.sendMessageReaction(emoji
,
369 targetAuthor
.getIdentifier(),
371 signal
.getSelfNumber()),
372 groupId
-> signal
.sendGroupMessageReaction(emoji
,
374 targetAuthor
.getIdentifier(),
380 public SendMessageResults
sendEndSessionMessage(final Set
<RecipientIdentifier
.Single
> recipients
) throws IOException
{
381 signal
.sendEndSessionMessage(recipients
.stream()
382 .map(RecipientIdentifier
.Single
::getIdentifier
)
383 .collect(Collectors
.toList()));
384 return new SendMessageResults(0, Map
.of());
388 public void setContactName(
389 final RecipientIdentifier
.Single recipient
, final String name
390 ) throws NotMasterDeviceException
, UnregisteredUserException
{
391 signal
.setContactName(recipient
.getIdentifier(), name
);
395 public void setContactBlocked(
396 final RecipientIdentifier
.Single recipient
, final boolean blocked
397 ) throws NotMasterDeviceException
, IOException
{
398 signal
.setContactBlocked(recipient
.getIdentifier(), blocked
);
402 public void setGroupBlocked(
403 final GroupId groupId
, final boolean blocked
404 ) throws GroupNotFoundException
, IOException
{
405 setGroupProperty(groupId
, "IsBlocked", blocked
);
408 private void setGroupProperty(final GroupId groupId
, final String propertyName
, final boolean blocked
) {
409 final var group
= getRemoteObject(signal
.getGroup(groupId
.serialize()), Signal
.Group
.class);
410 group
.Set("org.asamk.Signal.Group", propertyName
, blocked
);
414 public void setExpirationTimer(
415 final RecipientIdentifier
.Single recipient
, final int messageExpirationTimer
416 ) throws IOException
{
417 signal
.setExpirationTimer(recipient
.getIdentifier(), messageExpirationTimer
);
421 public URI
uploadStickerPack(final File path
) throws IOException
, StickerPackInvalidException
{
423 return new URI(signal
.uploadStickerPack(path
.getPath()));
424 } catch (URISyntaxException e
) {
425 throw new AssertionError(e
);
430 public void requestAllSyncData() throws IOException
{
431 signal
.sendSyncRequest();
435 public void receiveMessages(
438 final boolean returnOnTimeout
,
439 final boolean ignoreAttachments
,
440 final ReceiveMessageHandler handler
441 ) throws IOException
{
442 throw new UnsupportedOperationException();
446 public boolean hasCaughtUpWithOldMessages() {
447 throw new UnsupportedOperationException();
451 public boolean isContactBlocked(final RecipientIdentifier
.Single recipient
) {
452 return signal
.isContactBlocked(recipient
.getIdentifier());
456 public File
getAttachmentFile(final SignalServiceAttachmentRemoteId attachmentId
) {
457 throw new UnsupportedOperationException();
461 public void sendContacts() throws IOException
{
462 signal
.sendContacts();
466 public List
<Pair
<RecipientAddress
, Contact
>> getContacts() {
467 throw new UnsupportedOperationException();
471 public String
getContactOrProfileName(final RecipientIdentifier
.Single recipient
) {
472 return signal
.getContactName(recipient
.getIdentifier());
476 public Group
getGroup(final GroupId groupId
) {
477 final var groupPath
= signal
.getGroup(groupId
.serialize());
478 return getGroup(groupPath
);
481 @SuppressWarnings("unchecked")
482 private Group
getGroup(final DBusPath groupPath
) {
483 final var group
= getRemoteObject(groupPath
, Signal
.Group
.class).GetAll("org.asamk.Signal.Group");
484 final var id
= (byte[]) group
.get("Id").getValue();
486 return new Group(GroupId
.unknownVersion(id
),
487 (String
) group
.get("Name").getValue(),
488 (String
) group
.get("Description").getValue(),
489 GroupInviteLinkUrl
.fromUri((String
) group
.get("GroupInviteLink").getValue()),
490 ((List
<String
>) group
.get("Members").getValue()).stream()
491 .map(m
-> new RecipientAddress(null, m
))
492 .collect(Collectors
.toSet()),
493 ((List
<String
>) group
.get("PendingMembers").getValue()).stream()
494 .map(m
-> new RecipientAddress(null, m
))
495 .collect(Collectors
.toSet()),
496 ((List
<String
>) group
.get("RequestingMembers").getValue()).stream()
497 .map(m
-> new RecipientAddress(null, m
))
498 .collect(Collectors
.toSet()),
499 ((List
<String
>) group
.get("Admins").getValue()).stream()
500 .map(m
-> new RecipientAddress(null, m
))
501 .collect(Collectors
.toSet()),
502 (boolean) group
.get("IsBlocked").getValue(),
503 (int) group
.get("MessageExpirationTimer").getValue(),
504 GroupPermission
.valueOf((String
) group
.get("PermissionAddMember").getValue()),
505 GroupPermission
.valueOf((String
) group
.get("PermissionEditDetails").getValue()),
506 GroupPermission
.valueOf((String
) group
.get("PermissionSendMessage").getValue()),
507 (boolean) group
.get("IsMember").getValue(),
508 (boolean) group
.get("IsAdmin").getValue());
509 } catch (GroupInviteLinkUrl
.InvalidGroupLinkException
| GroupInviteLinkUrl
.UnknownGroupLinkVersionException e
) {
510 throw new AssertionError(e
);
515 public List
<Identity
> getIdentities() {
516 throw new UnsupportedOperationException();
520 public List
<Identity
> getIdentities(final RecipientIdentifier
.Single recipient
) {
521 throw new UnsupportedOperationException();
525 public boolean trustIdentityVerified(final RecipientIdentifier
.Single recipient
, final byte[] fingerprint
) {
526 throw new UnsupportedOperationException();
530 public boolean trustIdentityVerifiedSafetyNumber(
531 final RecipientIdentifier
.Single recipient
, final String safetyNumber
533 throw new UnsupportedOperationException();
537 public boolean trustIdentityVerifiedSafetyNumber(
538 final RecipientIdentifier
.Single recipient
, final byte[] safetyNumber
540 throw new UnsupportedOperationException();
544 public boolean trustIdentityAllKeys(final RecipientIdentifier
.Single recipient
) {
545 throw new UnsupportedOperationException();
549 public SignalServiceAddress
resolveSignalServiceAddress(final SignalServiceAddress address
) {
554 public void close() throws IOException
{
557 private SendMessageResults
handleMessage(
558 Set
<RecipientIdentifier
> recipients
,
559 Function
<List
<String
>, Long
> recipientsHandler
,
560 Supplier
<Long
> noteToSelfHandler
,
561 Function
<byte[], Long
> groupHandler
564 final var singleRecipients
= recipients
.stream()
565 .filter(r
-> r
instanceof RecipientIdentifier
.Single
)
566 .map(RecipientIdentifier
.Single
.class::cast
)
567 .map(RecipientIdentifier
.Single
::getIdentifier
)
568 .collect(Collectors
.toList());
569 if (singleRecipients
.size() > 0) {
570 timestamp
= recipientsHandler
.apply(singleRecipients
);
573 if (recipients
.contains(RecipientIdentifier
.NoteToSelf
.INSTANCE
)) {
574 timestamp
= noteToSelfHandler
.get();
576 final var groupRecipients
= recipients
.stream()
577 .filter(r
-> r
instanceof RecipientIdentifier
.Group
)
578 .map(RecipientIdentifier
.Group
.class::cast
)
580 .collect(Collectors
.toList());
581 for (final var groupId
: groupRecipients
) {
582 timestamp
= groupHandler
.apply(groupId
.serialize());
584 return new SendMessageResults(timestamp
, Map
.of());
587 private String
emptyIfNull(final String string
) {
588 return string
== null ?
"" : string
;
591 private <T
extends DBusInterface
> T
getRemoteObject(final DBusPath devicePath
, final Class
<T
> type
) {
593 return connection
.getRemoteObject(DbusConfig
.getBusname(), devicePath
.getPath(), type
);
594 } catch (DBusException e
) {
595 throw new AssertionError(e
);