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
.InactiveGroupLinkException
;
14 import org
.asamk
.signal
.manager
.api
.InvalidDeviceLinkException
;
15 import org
.asamk
.signal
.manager
.api
.Message
;
16 import org
.asamk
.signal
.manager
.api
.Pair
;
17 import org
.asamk
.signal
.manager
.api
.RecipientIdentifier
;
18 import org
.asamk
.signal
.manager
.api
.SendGroupMessageResults
;
19 import org
.asamk
.signal
.manager
.api
.SendMessageResults
;
20 import org
.asamk
.signal
.manager
.api
.TypingAction
;
21 import org
.asamk
.signal
.manager
.api
.UpdateGroup
;
22 import org
.asamk
.signal
.manager
.groups
.GroupId
;
23 import org
.asamk
.signal
.manager
.groups
.GroupInviteLinkUrl
;
24 import org
.asamk
.signal
.manager
.groups
.GroupNotFoundException
;
25 import org
.asamk
.signal
.manager
.groups
.GroupPermission
;
26 import org
.asamk
.signal
.manager
.groups
.GroupSendingNotAllowedException
;
27 import org
.asamk
.signal
.manager
.groups
.LastGroupAdminException
;
28 import org
.asamk
.signal
.manager
.groups
.NotAGroupMemberException
;
29 import org
.asamk
.signal
.manager
.storage
.recipients
.Contact
;
30 import org
.asamk
.signal
.manager
.storage
.recipients
.Profile
;
31 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientAddress
;
32 import org
.freedesktop
.dbus
.DBusPath
;
33 import org
.freedesktop
.dbus
.connections
.impl
.DBusConnection
;
34 import org
.freedesktop
.dbus
.exceptions
.DBusException
;
35 import org
.freedesktop
.dbus
.interfaces
.DBusInterface
;
36 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceAttachmentRemoteId
;
37 import org
.whispersystems
.signalservice
.api
.push
.SignalServiceAddress
;
38 import org
.whispersystems
.signalservice
.api
.push
.exceptions
.UnregisteredUserException
;
39 import org
.whispersystems
.signalservice
.api
.util
.UuidUtil
;
40 import org
.whispersystems
.signalservice
.internal
.contacts
.crypto
.UnauthenticatedResponseException
;
43 import java
.io
.IOException
;
45 import java
.net
.URISyntaxException
;
46 import java
.util
.ArrayList
;
47 import java
.util
.HashMap
;
48 import java
.util
.List
;
50 import java
.util
.Optional
;
52 import java
.util
.UUID
;
53 import java
.util
.concurrent
.TimeUnit
;
54 import java
.util
.function
.Function
;
55 import java
.util
.function
.Supplier
;
56 import java
.util
.stream
.Collectors
;
59 * This class implements the Manager interface using the DBus Signal interface, where possible.
60 * It's used for the signal-cli dbus client mode (--dbus, --dbus-system)
62 public class DbusManagerImpl
implements Manager
{
64 private final Signal signal
;
65 private final DBusConnection connection
;
67 public DbusManagerImpl(final Signal signal
, DBusConnection connection
) {
69 this.connection
= connection
;
73 public String
getSelfNumber() {
74 return signal
.getSelfNumber();
78 public void checkAccountState() throws IOException
{
79 throw new UnsupportedOperationException();
83 public Map
<String
, Pair
<String
, UUID
>> areUsersRegistered(final Set
<String
> numbers
) throws IOException
{
84 final var numbersList
= new ArrayList
<>(numbers
);
85 final var registered
= signal
.isRegistered(numbersList
);
87 final var result
= new HashMap
<String
, Pair
<String
, UUID
>>();
88 for (var i
= 0; i
< numbersList
.size(); i
++) {
89 result
.put(numbersList
.get(i
),
90 new Pair
<>(numbersList
.get(i
), registered
.get(i
) ? UuidUtil
.UNKNOWN_UUID
: null));
96 public void updateAccountAttributes(final String deviceName
) throws IOException
{
97 if (deviceName
!= null) {
98 final var devicePath
= signal
.getThisDevice();
99 getRemoteObject(devicePath
, Signal
.Device
.class).Set("org.asamk.Signal.Device", "Name", deviceName
);
104 public void updateConfiguration(
105 final Boolean readReceipts
,
106 final Boolean unidentifiedDeliveryIndicators
,
107 final Boolean typingIndicators
,
108 final Boolean linkPreviews
109 ) throws IOException
{
110 throw new UnsupportedOperationException();
114 public void setProfile(
115 final String givenName
,
116 final String familyName
,
118 final String aboutEmoji
,
119 final Optional
<File
> avatar
120 ) throws IOException
{
121 signal
.updateProfile(emptyIfNull(givenName
),
122 emptyIfNull(familyName
),
124 emptyIfNull(aboutEmoji
),
125 avatar
== null ?
"" : avatar
.map(File
::getPath
).orElse(""),
126 avatar
!= null && !avatar
.isPresent());
130 public void unregister() throws IOException
{
131 throw new UnsupportedOperationException();
135 public void deleteAccount() throws IOException
{
136 throw new UnsupportedOperationException();
140 public void submitRateLimitRecaptchaChallenge(final String challenge
, final String captcha
) throws IOException
{
141 throw new UnsupportedOperationException();
145 public List
<Device
> getLinkedDevices() throws IOException
{
146 final var thisDevice
= signal
.getThisDevice();
147 return signal
.listDevices().stream().map(d
-> {
148 final var device
= getRemoteObject(d
.getObjectPath(),
149 Signal
.Device
.class).GetAll("org.asamk.Signal.Device");
150 return new Device((long) device
.get("Id").getValue(),
151 (String
) device
.get("Name").getValue(),
152 (long) device
.get("Created").getValue(),
153 (long) device
.get("LastSeen").getValue(),
154 thisDevice
.equals(d
.getObjectPath()));
155 }).collect(Collectors
.toList());
159 public void removeLinkedDevices(final long deviceId
) throws IOException
{
160 final var devicePath
= signal
.getDevice(deviceId
);
161 getRemoteObject(devicePath
, Signal
.Device
.class).removeDevice();
165 public void addDeviceLink(final URI linkUri
) throws IOException
, InvalidDeviceLinkException
{
166 signal
.addDevice(linkUri
.toString());
170 public void setRegistrationLockPin(final Optional
<String
> pin
) throws IOException
, UnauthenticatedResponseException
{
171 if (pin
.isPresent()) {
172 signal
.setPin(pin
.get());
179 public Profile
getRecipientProfile(final RecipientIdentifier
.Single recipient
) throws UnregisteredUserException
{
180 throw new UnsupportedOperationException();
184 public List
<Group
> getGroups() {
185 final var groups
= signal
.listGroups();
186 return groups
.stream().map(Signal
.StructGroup
::getObjectPath
).map(this::getGroup
).collect(Collectors
.toList());
190 public SendGroupMessageResults
quitGroup(
191 final GroupId groupId
, final Set
<RecipientIdentifier
.Single
> groupAdmins
192 ) throws GroupNotFoundException
, IOException
, NotAGroupMemberException
, LastGroupAdminException
{
193 if (groupAdmins
.size() > 0) {
194 throw new UnsupportedOperationException();
196 final var group
= getRemoteObject(signal
.getGroup(groupId
.serialize()), Signal
.Group
.class);
198 return new SendGroupMessageResults(0, List
.of());
202 public void deleteGroup(final GroupId groupId
) throws IOException
{
203 throw new UnsupportedOperationException();
207 public Pair
<GroupId
, SendGroupMessageResults
> createGroup(
208 final String name
, final Set
<RecipientIdentifier
.Single
> members
, final File avatarFile
209 ) throws IOException
, AttachmentInvalidException
{
210 final var newGroupId
= signal
.createGroup(emptyIfNull(name
),
211 members
.stream().map(RecipientIdentifier
.Single
::getIdentifier
).collect(Collectors
.toList()),
212 avatarFile
== null ?
"" : avatarFile
.getPath());
213 return new Pair
<>(GroupId
.unknownVersion(newGroupId
), new SendGroupMessageResults(0, List
.of()));
217 public SendGroupMessageResults
updateGroup(
218 final GroupId groupId
, final UpdateGroup updateGroup
219 ) throws IOException
, GroupNotFoundException
, AttachmentInvalidException
, NotAGroupMemberException
, GroupSendingNotAllowedException
{
220 final var group
= getRemoteObject(signal
.getGroup(groupId
.serialize()), Signal
.Group
.class);
221 if (updateGroup
.getName() != null) {
222 group
.Set("org.asamk.Signal.Group", "Name", updateGroup
.getName());
224 if (updateGroup
.getDescription() != null) {
225 group
.Set("org.asamk.Signal.Group", "Description", updateGroup
.getDescription());
227 if (updateGroup
.getAvatarFile() != null) {
228 group
.Set("org.asamk.Signal.Group",
230 updateGroup
.getAvatarFile() == null ?
"" : updateGroup
.getAvatarFile().getPath());
232 if (updateGroup
.getExpirationTimer() != null) {
233 group
.Set("org.asamk.Signal.Group", "MessageExpirationTimer", updateGroup
.getExpirationTimer());
235 if (updateGroup
.getAddMemberPermission() != null) {
236 group
.Set("org.asamk.Signal.Group", "PermissionAddMember", updateGroup
.getAddMemberPermission().name());
238 if (updateGroup
.getEditDetailsPermission() != null) {
239 group
.Set("org.asamk.Signal.Group", "PermissionEditDetails", updateGroup
.getEditDetailsPermission().name());
241 if (updateGroup
.getIsAnnouncementGroup() != null) {
242 group
.Set("org.asamk.Signal.Group",
243 "PermissionSendMessage",
244 updateGroup
.getIsAnnouncementGroup()
245 ? GroupPermission
.ONLY_ADMINS
.name()
246 : GroupPermission
.EVERY_MEMBER
.name());
248 if (updateGroup
.getMembers() != null) {
249 group
.addMembers(updateGroup
.getMembers()
251 .map(RecipientIdentifier
.Single
::getIdentifier
)
252 .collect(Collectors
.toList()));
254 if (updateGroup
.getRemoveMembers() != null) {
255 group
.removeMembers(updateGroup
.getRemoveMembers()
257 .map(RecipientIdentifier
.Single
::getIdentifier
)
258 .collect(Collectors
.toList()));
260 if (updateGroup
.getAdmins() != null) {
261 group
.addAdmins(updateGroup
.getAdmins()
263 .map(RecipientIdentifier
.Single
::getIdentifier
)
264 .collect(Collectors
.toList()));
266 if (updateGroup
.getRemoveAdmins() != null) {
267 group
.removeAdmins(updateGroup
.getRemoveAdmins()
269 .map(RecipientIdentifier
.Single
::getIdentifier
)
270 .collect(Collectors
.toList()));
272 if (updateGroup
.isResetGroupLink()) {
275 if (updateGroup
.getGroupLinkState() != null) {
276 switch (updateGroup
.getGroupLinkState()) {
277 case DISABLED
-> group
.disableLink();
278 case ENABLED
-> group
.enableLink(false);
279 case ENABLED_WITH_APPROVAL
-> group
.enableLink(true);
282 return new SendGroupMessageResults(0, List
.of());
286 public Pair
<GroupId
, SendGroupMessageResults
> joinGroup(final GroupInviteLinkUrl inviteLinkUrl
) throws IOException
, InactiveGroupLinkException
{
287 final var newGroupId
= signal
.joinGroup(inviteLinkUrl
.getUrl());
288 return new Pair
<>(GroupId
.unknownVersion(newGroupId
), new SendGroupMessageResults(0, List
.of()));
292 public void sendTypingMessage(
293 final TypingAction action
, final Set
<RecipientIdentifier
> recipients
294 ) throws IOException
, UntrustedIdentityException
, NotAGroupMemberException
, GroupNotFoundException
, GroupSendingNotAllowedException
{
295 for (final var recipient
: recipients
) {
296 if (recipient
instanceof RecipientIdentifier
.Single
) {
297 signal
.sendTyping(((RecipientIdentifier
.Single
) recipient
).getIdentifier(),
298 action
== TypingAction
.STOP
);
299 } else if (recipient
instanceof RecipientIdentifier
.Group
) {
300 throw new UnsupportedOperationException();
306 public void sendReadReceipt(
307 final RecipientIdentifier
.Single sender
, final List
<Long
> messageIds
308 ) throws IOException
, UntrustedIdentityException
{
309 signal
.sendReadReceipt(sender
.getIdentifier(), messageIds
);
313 public void sendViewedReceipt(
314 final RecipientIdentifier
.Single sender
, final List
<Long
> messageIds
315 ) throws IOException
, UntrustedIdentityException
{
316 throw new UnsupportedOperationException();
320 public SendMessageResults
sendMessage(
321 final Message message
, final Set
<RecipientIdentifier
> recipients
322 ) throws IOException
, AttachmentInvalidException
, NotAGroupMemberException
, GroupNotFoundException
, GroupSendingNotAllowedException
{
323 return handleMessage(recipients
,
324 numbers
-> signal
.sendMessage(message
.messageText(), message
.attachments(), numbers
),
325 () -> signal
.sendNoteToSelfMessage(message
.messageText(), message
.attachments()),
326 groupId
-> signal
.sendGroupMessage(message
.messageText(), message
.attachments(), groupId
));
330 public SendMessageResults
sendRemoteDeleteMessage(
331 final long targetSentTimestamp
, final Set
<RecipientIdentifier
> recipients
332 ) throws IOException
, NotAGroupMemberException
, GroupNotFoundException
, GroupSendingNotAllowedException
{
333 return handleMessage(recipients
,
334 numbers
-> signal
.sendRemoteDeleteMessage(targetSentTimestamp
, numbers
),
335 () -> signal
.sendRemoteDeleteMessage(targetSentTimestamp
, signal
.getSelfNumber()),
336 groupId
-> signal
.sendGroupRemoteDeleteMessage(targetSentTimestamp
, groupId
));
340 public SendMessageResults
sendMessageReaction(
342 final boolean remove
,
343 final RecipientIdentifier
.Single targetAuthor
,
344 final long targetSentTimestamp
,
345 final Set
<RecipientIdentifier
> recipients
346 ) throws IOException
, NotAGroupMemberException
, GroupNotFoundException
, GroupSendingNotAllowedException
{
347 return handleMessage(recipients
,
348 numbers
-> signal
.sendMessageReaction(emoji
,
350 targetAuthor
.getIdentifier(),
353 () -> signal
.sendMessageReaction(emoji
,
355 targetAuthor
.getIdentifier(),
357 signal
.getSelfNumber()),
358 groupId
-> signal
.sendGroupMessageReaction(emoji
,
360 targetAuthor
.getIdentifier(),
366 public SendMessageResults
sendEndSessionMessage(final Set
<RecipientIdentifier
.Single
> recipients
) throws IOException
{
367 signal
.sendEndSessionMessage(recipients
.stream()
368 .map(RecipientIdentifier
.Single
::getIdentifier
)
369 .collect(Collectors
.toList()));
370 return new SendMessageResults(0, Map
.of());
374 public void setContactName(
375 final RecipientIdentifier
.Single recipient
, final String name
376 ) throws NotMasterDeviceException
, UnregisteredUserException
{
377 signal
.setContactName(recipient
.getIdentifier(), name
);
381 public void setContactBlocked(
382 final RecipientIdentifier
.Single recipient
, final boolean blocked
383 ) throws NotMasterDeviceException
, IOException
{
384 signal
.setContactBlocked(recipient
.getIdentifier(), blocked
);
388 public void setGroupBlocked(
389 final GroupId groupId
, final boolean blocked
390 ) throws GroupNotFoundException
, IOException
{
391 setGroupProperty(groupId
, "IsBlocked", blocked
);
394 private void setGroupProperty(final GroupId groupId
, final String propertyName
, final boolean blocked
) {
395 final var group
= getRemoteObject(signal
.getGroup(groupId
.serialize()), Signal
.Group
.class);
396 group
.Set("org.asamk.Signal.Group", propertyName
, blocked
);
400 public void setExpirationTimer(
401 final RecipientIdentifier
.Single recipient
, final int messageExpirationTimer
402 ) throws IOException
{
403 signal
.setExpirationTimer(recipient
.getIdentifier(), messageExpirationTimer
);
407 public URI
uploadStickerPack(final File path
) throws IOException
, StickerPackInvalidException
{
409 return new URI(signal
.uploadStickerPack(path
.getPath()));
410 } catch (URISyntaxException e
) {
411 throw new AssertionError(e
);
416 public void requestAllSyncData() throws IOException
{
417 signal
.sendSyncRequest();
421 public void addReceiveHandler(final ReceiveMessageHandler handler
) {
422 throw new UnsupportedOperationException();
426 public void removeReceiveHandler(final ReceiveMessageHandler handler
) {
427 throw new UnsupportedOperationException();
431 public boolean isReceiving() {
432 throw new UnsupportedOperationException();
436 public void receiveMessages(final ReceiveMessageHandler handler
) throws IOException
{
437 throw new UnsupportedOperationException();
441 public void receiveMessages(
442 final long timeout
, final TimeUnit unit
, final ReceiveMessageHandler handler
443 ) throws IOException
{
444 throw new UnsupportedOperationException();
448 public void setIgnoreAttachments(final boolean ignoreAttachments
) {
449 throw new UnsupportedOperationException();
453 public boolean hasCaughtUpWithOldMessages() {
454 throw new UnsupportedOperationException();
458 public boolean isContactBlocked(final RecipientIdentifier
.Single recipient
) {
459 return signal
.isContactBlocked(recipient
.getIdentifier());
463 public File
getAttachmentFile(final SignalServiceAttachmentRemoteId attachmentId
) {
464 throw new UnsupportedOperationException();
468 public void sendContacts() throws IOException
{
469 signal
.sendContacts();
473 public List
<Pair
<RecipientAddress
, Contact
>> getContacts() {
474 throw new UnsupportedOperationException();
478 public String
getContactOrProfileName(final RecipientIdentifier
.Single recipient
) {
479 return signal
.getContactName(recipient
.getIdentifier());
483 public Group
getGroup(final GroupId groupId
) {
484 final var groupPath
= signal
.getGroup(groupId
.serialize());
485 return getGroup(groupPath
);
488 @SuppressWarnings("unchecked")
489 private Group
getGroup(final DBusPath groupPath
) {
490 final var group
= getRemoteObject(groupPath
, Signal
.Group
.class).GetAll("org.asamk.Signal.Group");
491 final var id
= (byte[]) group
.get("Id").getValue();
493 return new Group(GroupId
.unknownVersion(id
),
494 (String
) group
.get("Name").getValue(),
495 (String
) group
.get("Description").getValue(),
496 GroupInviteLinkUrl
.fromUri((String
) group
.get("GroupInviteLink").getValue()),
497 ((List
<String
>) group
.get("Members").getValue()).stream()
498 .map(m
-> new RecipientAddress(null, m
))
499 .collect(Collectors
.toSet()),
500 ((List
<String
>) group
.get("PendingMembers").getValue()).stream()
501 .map(m
-> new RecipientAddress(null, m
))
502 .collect(Collectors
.toSet()),
503 ((List
<String
>) group
.get("RequestingMembers").getValue()).stream()
504 .map(m
-> new RecipientAddress(null, m
))
505 .collect(Collectors
.toSet()),
506 ((List
<String
>) group
.get("Admins").getValue()).stream()
507 .map(m
-> new RecipientAddress(null, m
))
508 .collect(Collectors
.toSet()),
509 (boolean) group
.get("IsBlocked").getValue(),
510 (int) group
.get("MessageExpirationTimer").getValue(),
511 GroupPermission
.valueOf((String
) group
.get("PermissionAddMember").getValue()),
512 GroupPermission
.valueOf((String
) group
.get("PermissionEditDetails").getValue()),
513 GroupPermission
.valueOf((String
) group
.get("PermissionSendMessage").getValue()),
514 (boolean) group
.get("IsMember").getValue(),
515 (boolean) group
.get("IsAdmin").getValue());
516 } catch (GroupInviteLinkUrl
.InvalidGroupLinkException
| GroupInviteLinkUrl
.UnknownGroupLinkVersionException e
) {
517 throw new AssertionError(e
);
522 public List
<Identity
> getIdentities() {
523 throw new UnsupportedOperationException();
527 public List
<Identity
> getIdentities(final RecipientIdentifier
.Single recipient
) {
528 throw new UnsupportedOperationException();
532 public boolean trustIdentityVerified(final RecipientIdentifier
.Single recipient
, final byte[] fingerprint
) {
533 throw new UnsupportedOperationException();
537 public boolean trustIdentityVerifiedSafetyNumber(
538 final RecipientIdentifier
.Single recipient
, final String safetyNumber
540 throw new UnsupportedOperationException();
544 public boolean trustIdentityVerifiedSafetyNumber(
545 final RecipientIdentifier
.Single recipient
, final byte[] safetyNumber
547 throw new UnsupportedOperationException();
551 public boolean trustIdentityAllKeys(final RecipientIdentifier
.Single recipient
) {
552 throw new UnsupportedOperationException();
556 public SignalServiceAddress
resolveSignalServiceAddress(final SignalServiceAddress address
) {
561 public void close() throws IOException
{
564 private SendMessageResults
handleMessage(
565 Set
<RecipientIdentifier
> recipients
,
566 Function
<List
<String
>, Long
> recipientsHandler
,
567 Supplier
<Long
> noteToSelfHandler
,
568 Function
<byte[], Long
> groupHandler
571 final var singleRecipients
= recipients
.stream()
572 .filter(r
-> r
instanceof RecipientIdentifier
.Single
)
573 .map(RecipientIdentifier
.Single
.class::cast
)
574 .map(RecipientIdentifier
.Single
::getIdentifier
)
575 .collect(Collectors
.toList());
576 if (singleRecipients
.size() > 0) {
577 timestamp
= recipientsHandler
.apply(singleRecipients
);
580 if (recipients
.contains(RecipientIdentifier
.NoteToSelf
.INSTANCE
)) {
581 timestamp
= noteToSelfHandler
.get();
583 final var groupRecipients
= recipients
.stream()
584 .filter(r
-> r
instanceof RecipientIdentifier
.Group
)
585 .map(RecipientIdentifier
.Group
.class::cast
)
587 .collect(Collectors
.toList());
588 for (final var groupId
: groupRecipients
) {
589 timestamp
= groupHandler
.apply(groupId
.serialize());
591 return new SendMessageResults(timestamp
, Map
.of());
594 private String
emptyIfNull(final String string
) {
595 return string
== null ?
"" : string
;
598 private <T
extends DBusInterface
> T
getRemoteObject(final DBusPath devicePath
, final Class
<T
> type
) {
600 return connection
.getRemoteObject(DbusConfig
.getBusname(), devicePath
.getPath(), type
);
601 } catch (DBusException e
) {
602 throw new AssertionError(e
);