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
.Device
;
11 import org
.asamk
.signal
.manager
.api
.Group
;
12 import org
.asamk
.signal
.manager
.api
.Identity
;
13 import org
.asamk
.signal
.manager
.api
.Message
;
14 import org
.asamk
.signal
.manager
.api
.RecipientIdentifier
;
15 import org
.asamk
.signal
.manager
.api
.SendGroupMessageResults
;
16 import org
.asamk
.signal
.manager
.api
.SendMessageResults
;
17 import org
.asamk
.signal
.manager
.api
.TypingAction
;
18 import org
.asamk
.signal
.manager
.api
.UpdateGroup
;
19 import org
.asamk
.signal
.manager
.groups
.GroupId
;
20 import org
.asamk
.signal
.manager
.groups
.GroupInviteLinkUrl
;
21 import org
.asamk
.signal
.manager
.groups
.GroupNotFoundException
;
22 import org
.asamk
.signal
.manager
.groups
.GroupPermission
;
23 import org
.asamk
.signal
.manager
.groups
.GroupSendingNotAllowedException
;
24 import org
.asamk
.signal
.manager
.groups
.LastGroupAdminException
;
25 import org
.asamk
.signal
.manager
.groups
.NotAGroupMemberException
;
26 import org
.asamk
.signal
.manager
.storage
.recipients
.Contact
;
27 import org
.asamk
.signal
.manager
.storage
.recipients
.Profile
;
28 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientAddress
;
29 import org
.freedesktop
.dbus
.DBusPath
;
30 import org
.freedesktop
.dbus
.connections
.impl
.DBusConnection
;
31 import org
.freedesktop
.dbus
.exceptions
.DBusException
;
32 import org
.freedesktop
.dbus
.interfaces
.DBusInterface
;
33 import org
.whispersystems
.libsignal
.IdentityKey
;
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
);
105 public void updateConfiguration(
106 final Boolean readReceipts
,
107 final Boolean unidentifiedDeliveryIndicators
,
108 final Boolean typingIndicators
,
109 final Boolean linkPreviews
110 ) throws IOException
{
111 throw new UnsupportedOperationException();
115 public void setProfile(
116 final String givenName
,
117 final String familyName
,
119 final String aboutEmoji
,
120 final Optional
<File
> avatar
121 ) throws IOException
{
122 signal
.updateProfile(emptyIfNull(givenName
),
123 emptyIfNull(familyName
),
125 emptyIfNull(aboutEmoji
),
126 avatar
== null ?
"" : avatar
.transform(File
::getPath
).or(""),
127 avatar
!= null && !avatar
.isPresent());
131 public void unregister() throws IOException
{
132 throw new UnsupportedOperationException();
136 public void deleteAccount() throws IOException
{
137 throw new UnsupportedOperationException();
141 public void submitRateLimitRecaptchaChallenge(final String challenge
, final String captcha
) throws IOException
{
142 throw new UnsupportedOperationException();
146 public List
<Device
> getLinkedDevices() throws IOException
{
147 final var thisDevice
= signal
.getThisDevice();
148 return signal
.listDevices().stream().map(d
-> {
149 final var device
= getRemoteObject(d
.getObjectPath(),
150 Signal
.Device
.class).GetAll("org.asamk.Signal.Device");
151 return new Device((long) device
.get("Id").getValue(),
152 (String
) device
.get("Name").getValue(),
153 (long) device
.get("Created").getValue(),
154 (long) device
.get("LastSeen").getValue(),
155 thisDevice
.equals(d
.getObjectPath()));
156 }).collect(Collectors
.toList());
160 public void removeLinkedDevices(final long deviceId
) throws IOException
{
161 final var devicePath
= signal
.getDevice(deviceId
);
162 getRemoteObject(devicePath
, Signal
.Device
.class).removeDevice();
166 public void addDeviceLink(final URI linkUri
) throws IOException
, InvalidKeyException
{
167 signal
.addDevice(linkUri
.toString());
171 public void setRegistrationLockPin(final Optional
<String
> pin
) throws IOException
, UnauthenticatedResponseException
{
172 if (pin
.isPresent()) {
173 signal
.setPin(pin
.get());
180 public Profile
getRecipientProfile(final RecipientIdentifier
.Single recipient
) throws UnregisteredUserException
{
181 throw new UnsupportedOperationException();
185 public List
<Group
> getGroups() {
186 final var groups
= signal
.listGroups();
187 return groups
.stream().map(Signal
.StructGroup
::getObjectPath
).map(this::getGroup
).collect(Collectors
.toList());
191 public SendGroupMessageResults
quitGroup(
192 final GroupId groupId
, final Set
<RecipientIdentifier
.Single
> groupAdmins
193 ) throws GroupNotFoundException
, IOException
, NotAGroupMemberException
, LastGroupAdminException
{
194 if (groupAdmins
.size() > 0) {
195 throw new UnsupportedOperationException();
197 final var group
= getRemoteObject(signal
.getGroup(groupId
.serialize()), Signal
.Group
.class);
199 return new SendGroupMessageResults(0, List
.of());
203 public void deleteGroup(final GroupId groupId
) throws IOException
{
204 throw new UnsupportedOperationException();
208 public Pair
<GroupId
, SendGroupMessageResults
> createGroup(
209 final String name
, final Set
<RecipientIdentifier
.Single
> members
, final File avatarFile
210 ) throws IOException
, AttachmentInvalidException
{
211 final var newGroupId
= signal
.createGroup(emptyIfNull(name
),
212 members
.stream().map(RecipientIdentifier
.Single
::getIdentifier
).collect(Collectors
.toList()),
213 avatarFile
== null ?
"" : avatarFile
.getPath());
214 return new Pair
<>(GroupId
.unknownVersion(newGroupId
), new SendGroupMessageResults(0, List
.of()));
218 public SendGroupMessageResults
updateGroup(
219 final GroupId groupId
, final UpdateGroup updateGroup
220 ) throws IOException
, GroupNotFoundException
, AttachmentInvalidException
, NotAGroupMemberException
, GroupSendingNotAllowedException
{
221 final var group
= getRemoteObject(signal
.getGroup(groupId
.serialize()), Signal
.Group
.class);
222 if (updateGroup
.getName() != null) {
223 group
.Set("org.asamk.Signal.Group", "Name", updateGroup
.getName());
225 if (updateGroup
.getDescription() != null) {
226 group
.Set("org.asamk.Signal.Group", "Description", updateGroup
.getDescription());
228 if (updateGroup
.getAvatarFile() != null) {
229 group
.Set("org.asamk.Signal.Group",
231 updateGroup
.getAvatarFile() == null ?
"" : updateGroup
.getAvatarFile().getPath());
233 if (updateGroup
.getExpirationTimer() != null) {
234 group
.Set("org.asamk.Signal.Group", "MessageExpirationTimer", updateGroup
.getExpirationTimer());
236 if (updateGroup
.getAddMemberPermission() != null) {
237 group
.Set("org.asamk.Signal.Group", "PermissionAddMember", updateGroup
.getAddMemberPermission().name());
239 if (updateGroup
.getEditDetailsPermission() != null) {
240 group
.Set("org.asamk.Signal.Group", "PermissionEditDetails", updateGroup
.getEditDetailsPermission().name());
242 if (updateGroup
.getIsAnnouncementGroup() != null) {
243 group
.Set("org.asamk.Signal.Group",
244 "PermissionSendMessage",
245 updateGroup
.getIsAnnouncementGroup()
246 ? GroupPermission
.ONLY_ADMINS
.name()
247 : GroupPermission
.EVERY_MEMBER
.name());
249 if (updateGroup
.getMembers() != null) {
250 group
.addMembers(updateGroup
.getMembers()
252 .map(RecipientIdentifier
.Single
::getIdentifier
)
253 .collect(Collectors
.toList()));
255 if (updateGroup
.getRemoveMembers() != null) {
256 group
.removeMembers(updateGroup
.getRemoveMembers()
258 .map(RecipientIdentifier
.Single
::getIdentifier
)
259 .collect(Collectors
.toList()));
261 if (updateGroup
.getAdmins() != null) {
262 group
.addAdmins(updateGroup
.getAdmins()
264 .map(RecipientIdentifier
.Single
::getIdentifier
)
265 .collect(Collectors
.toList()));
267 if (updateGroup
.getRemoveAdmins() != null) {
268 group
.removeAdmins(updateGroup
.getRemoveAdmins()
270 .map(RecipientIdentifier
.Single
::getIdentifier
)
271 .collect(Collectors
.toList()));
273 if (updateGroup
.isResetGroupLink()) {
276 if (updateGroup
.getGroupLinkState() != null) {
277 switch (updateGroup
.getGroupLinkState()) {
282 group
.enableLink(false);
284 case ENABLED_WITH_APPROVAL
:
285 group
.enableLink(true);
289 return new SendGroupMessageResults(0, List
.of());
293 public Pair
<GroupId
, SendGroupMessageResults
> joinGroup(final GroupInviteLinkUrl inviteLinkUrl
) throws IOException
, GroupLinkNotActiveException
{
294 final var newGroupId
= signal
.joinGroup(inviteLinkUrl
.getUrl());
295 return new Pair
<>(GroupId
.unknownVersion(newGroupId
), new SendGroupMessageResults(0, List
.of()));
299 public void sendTypingMessage(
300 final TypingAction action
, final Set
<RecipientIdentifier
> recipients
301 ) throws IOException
, UntrustedIdentityException
, NotAGroupMemberException
, GroupNotFoundException
, GroupSendingNotAllowedException
{
302 for (final var recipient
: recipients
) {
303 if (recipient
instanceof RecipientIdentifier
.Single
) {
304 signal
.sendTyping(((RecipientIdentifier
.Single
) recipient
).getIdentifier(),
305 action
== TypingAction
.STOP
);
306 } else if (recipient
instanceof RecipientIdentifier
.Group
) {
307 throw new UnsupportedOperationException();
313 public void sendReadReceipt(
314 final RecipientIdentifier
.Single sender
, final List
<Long
> messageIds
315 ) throws IOException
, UntrustedIdentityException
{
316 signal
.sendReadReceipt(sender
.getIdentifier(), messageIds
);
320 public void sendViewedReceipt(
321 final RecipientIdentifier
.Single sender
, final List
<Long
> messageIds
322 ) throws IOException
, UntrustedIdentityException
{
323 throw new UnsupportedOperationException();
327 public SendMessageResults
sendMessage(
328 final Message message
, final Set
<RecipientIdentifier
> recipients
329 ) throws IOException
, AttachmentInvalidException
, NotAGroupMemberException
, GroupNotFoundException
, GroupSendingNotAllowedException
{
330 return handleMessage(recipients
,
331 numbers
-> signal
.sendMessage(message
.getMessageText(), message
.getAttachments(), numbers
),
332 () -> signal
.sendNoteToSelfMessage(message
.getMessageText(), message
.getAttachments()),
333 groupId
-> signal
.sendGroupMessage(message
.getMessageText(), message
.getAttachments(), groupId
));
337 public SendMessageResults
sendRemoteDeleteMessage(
338 final long targetSentTimestamp
, final Set
<RecipientIdentifier
> recipients
339 ) throws IOException
, NotAGroupMemberException
, GroupNotFoundException
, GroupSendingNotAllowedException
{
340 return handleMessage(recipients
,
341 numbers
-> signal
.sendRemoteDeleteMessage(targetSentTimestamp
, numbers
),
342 () -> signal
.sendRemoteDeleteMessage(targetSentTimestamp
, signal
.getSelfNumber()),
343 groupId
-> signal
.sendGroupRemoteDeleteMessage(targetSentTimestamp
, groupId
));
347 public SendMessageResults
sendMessageReaction(
349 final boolean remove
,
350 final RecipientIdentifier
.Single targetAuthor
,
351 final long targetSentTimestamp
,
352 final Set
<RecipientIdentifier
> recipients
353 ) throws IOException
, NotAGroupMemberException
, GroupNotFoundException
, GroupSendingNotAllowedException
{
354 return handleMessage(recipients
,
355 numbers
-> signal
.sendMessageReaction(emoji
,
357 targetAuthor
.getIdentifier(),
360 () -> signal
.sendMessageReaction(emoji
,
362 targetAuthor
.getIdentifier(),
364 signal
.getSelfNumber()),
365 groupId
-> signal
.sendGroupMessageReaction(emoji
,
367 targetAuthor
.getIdentifier(),
373 public SendMessageResults
sendEndSessionMessage(final Set
<RecipientIdentifier
.Single
> recipients
) throws IOException
{
374 signal
.sendEndSessionMessage(recipients
.stream()
375 .map(RecipientIdentifier
.Single
::getIdentifier
)
376 .collect(Collectors
.toList()));
377 return new SendMessageResults(0, Map
.of());
381 public void setContactName(
382 final RecipientIdentifier
.Single recipient
, final String name
383 ) throws NotMasterDeviceException
, UnregisteredUserException
{
384 signal
.setContactName(recipient
.getIdentifier(), name
);
388 public void setContactBlocked(
389 final RecipientIdentifier
.Single recipient
, final boolean blocked
390 ) throws NotMasterDeviceException
, IOException
{
391 signal
.setContactBlocked(recipient
.getIdentifier(), blocked
);
395 public void setGroupBlocked(
396 final GroupId groupId
, final boolean blocked
397 ) throws GroupNotFoundException
, IOException
{
398 setGroupProperty(groupId
, "IsBlocked", blocked
);
401 private void setGroupProperty(final GroupId groupId
, final String propertyName
, final boolean blocked
) {
402 final var group
= getRemoteObject(signal
.getGroup(groupId
.serialize()), Signal
.Group
.class);
403 group
.Set("org.asamk.Signal.Group", propertyName
, blocked
);
407 public void setExpirationTimer(
408 final RecipientIdentifier
.Single recipient
, final int messageExpirationTimer
409 ) throws IOException
{
410 signal
.setExpirationTimer(recipient
.getIdentifier(), messageExpirationTimer
);
414 public URI
uploadStickerPack(final File path
) throws IOException
, StickerPackInvalidException
{
416 return new URI(signal
.uploadStickerPack(path
.getPath()));
417 } catch (URISyntaxException e
) {
418 throw new AssertionError(e
);
423 public void requestAllSyncData() throws IOException
{
424 signal
.sendSyncRequest();
428 public void receiveMessages(
431 final boolean returnOnTimeout
,
432 final boolean ignoreAttachments
,
433 final ReceiveMessageHandler handler
434 ) throws IOException
{
435 throw new UnsupportedOperationException();
439 public boolean hasCaughtUpWithOldMessages() {
440 throw new UnsupportedOperationException();
444 public boolean isContactBlocked(final RecipientIdentifier
.Single recipient
) {
445 return signal
.isContactBlocked(recipient
.getIdentifier());
449 public File
getAttachmentFile(final SignalServiceAttachmentRemoteId attachmentId
) {
450 throw new UnsupportedOperationException();
454 public void sendContacts() throws IOException
{
455 signal
.sendContacts();
459 public List
<Pair
<RecipientAddress
, Contact
>> getContacts() {
460 throw new UnsupportedOperationException();
464 public String
getContactOrProfileName(final RecipientIdentifier
.Single recipient
) {
465 return signal
.getContactName(recipient
.getIdentifier());
469 public Group
getGroup(final GroupId groupId
) {
470 final var groupPath
= signal
.getGroup(groupId
.serialize());
471 return getGroup(groupPath
);
474 @SuppressWarnings("unchecked")
475 private Group
getGroup(final DBusPath groupPath
) {
476 final var group
= getRemoteObject(groupPath
, Signal
.Group
.class).GetAll("org.asamk.Signal.Group");
477 final var id
= (byte[]) group
.get("Id").getValue();
479 return new Group(GroupId
.unknownVersion(id
),
480 (String
) group
.get("Name").getValue(),
481 (String
) group
.get("Description").getValue(),
482 GroupInviteLinkUrl
.fromUri((String
) group
.get("GroupInviteLink").getValue()),
483 ((List
<String
>) group
.get("Members").getValue()).stream()
484 .map(m
-> new RecipientAddress(null, m
))
485 .collect(Collectors
.toSet()),
486 ((List
<String
>) group
.get("PendingMembers").getValue()).stream()
487 .map(m
-> new RecipientAddress(null, m
))
488 .collect(Collectors
.toSet()),
489 ((List
<String
>) group
.get("RequestingMembers").getValue()).stream()
490 .map(m
-> new RecipientAddress(null, m
))
491 .collect(Collectors
.toSet()),
492 ((List
<String
>) group
.get("Admins").getValue()).stream()
493 .map(m
-> new RecipientAddress(null, m
))
494 .collect(Collectors
.toSet()),
495 (boolean) group
.get("IsBlocked").getValue(),
496 (int) group
.get("MessageExpirationTimer").getValue(),
497 GroupPermission
.valueOf((String
) group
.get("PermissionAddMember").getValue()),
498 GroupPermission
.valueOf((String
) group
.get("PermissionEditDetails").getValue()),
499 GroupPermission
.valueOf((String
) group
.get("PermissionSendMessage").getValue()),
500 (boolean) group
.get("IsMember").getValue(),
501 (boolean) group
.get("IsAdmin").getValue());
502 } catch (GroupInviteLinkUrl
.InvalidGroupLinkException
| GroupInviteLinkUrl
.UnknownGroupLinkVersionException e
) {
503 throw new AssertionError(e
);
508 public List
<Identity
> getIdentities() {
509 throw new UnsupportedOperationException();
513 public List
<Identity
> getIdentities(final RecipientIdentifier
.Single recipient
) {
514 throw new UnsupportedOperationException();
518 public boolean trustIdentityVerified(final RecipientIdentifier
.Single recipient
, final byte[] fingerprint
) {
519 throw new UnsupportedOperationException();
523 public boolean trustIdentityVerifiedSafetyNumber(
524 final RecipientIdentifier
.Single recipient
, final String safetyNumber
526 throw new UnsupportedOperationException();
530 public boolean trustIdentityVerifiedSafetyNumber(
531 final RecipientIdentifier
.Single recipient
, final byte[] safetyNumber
533 throw new UnsupportedOperationException();
537 public boolean trustIdentityAllKeys(final RecipientIdentifier
.Single recipient
) {
538 throw new UnsupportedOperationException();
542 public String
computeSafetyNumber(
543 final SignalServiceAddress theirAddress
, final IdentityKey theirIdentityKey
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
);