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
;
42 import java
.io
.IOException
;
44 import java
.net
.URISyntaxException
;
45 import java
.util
.ArrayList
;
46 import java
.util
.HashMap
;
47 import java
.util
.List
;
49 import java
.util
.Optional
;
51 import java
.util
.UUID
;
52 import java
.util
.concurrent
.TimeUnit
;
53 import java
.util
.function
.Function
;
54 import java
.util
.function
.Supplier
;
55 import java
.util
.stream
.Collectors
;
58 * This class implements the Manager interface using the DBus Signal interface, where possible.
59 * It's used for the signal-cli dbus client mode (--dbus, --dbus-system)
61 public class DbusManagerImpl
implements Manager
{
63 private final Signal signal
;
64 private final DBusConnection connection
;
66 public DbusManagerImpl(final Signal signal
, DBusConnection connection
) {
68 this.connection
= connection
;
72 public String
getSelfNumber() {
73 return signal
.getSelfNumber();
77 public void checkAccountState() throws IOException
{
78 throw new UnsupportedOperationException();
82 public Map
<String
, Pair
<String
, UUID
>> areUsersRegistered(final Set
<String
> numbers
) throws IOException
{
83 final var numbersList
= new ArrayList
<>(numbers
);
84 final var registered
= signal
.isRegistered(numbersList
);
86 final var result
= new HashMap
<String
, Pair
<String
, UUID
>>();
87 for (var i
= 0; i
< numbersList
.size(); i
++) {
88 result
.put(numbersList
.get(i
),
89 new Pair
<>(numbersList
.get(i
), registered
.get(i
) ? UuidUtil
.UNKNOWN_UUID
: null));
95 public void updateAccountAttributes(final String deviceName
) throws IOException
{
96 if (deviceName
!= null) {
97 final var devicePath
= signal
.getThisDevice();
98 getRemoteObject(devicePath
, Signal
.Device
.class).Set("org.asamk.Signal.Device", "Name", deviceName
);
103 public void updateConfiguration(
104 final Boolean readReceipts
,
105 final Boolean unidentifiedDeliveryIndicators
,
106 final Boolean typingIndicators
,
107 final Boolean linkPreviews
108 ) throws IOException
{
109 throw new UnsupportedOperationException();
113 public void setProfile(
114 final String givenName
,
115 final String familyName
,
117 final String aboutEmoji
,
118 final Optional
<File
> avatar
119 ) throws IOException
{
120 signal
.updateProfile(emptyIfNull(givenName
),
121 emptyIfNull(familyName
),
123 emptyIfNull(aboutEmoji
),
124 avatar
== null ?
"" : avatar
.map(File
::getPath
).orElse(""),
125 avatar
!= null && !avatar
.isPresent());
129 public void unregister() throws IOException
{
130 throw new UnsupportedOperationException();
134 public void deleteAccount() throws IOException
{
135 throw new UnsupportedOperationException();
139 public void submitRateLimitRecaptchaChallenge(final String challenge
, final String captcha
) throws IOException
{
140 throw new UnsupportedOperationException();
144 public List
<Device
> getLinkedDevices() throws IOException
{
145 final var thisDevice
= signal
.getThisDevice();
146 return signal
.listDevices().stream().map(d
-> {
147 final var device
= getRemoteObject(d
.getObjectPath(),
148 Signal
.Device
.class).GetAll("org.asamk.Signal.Device");
149 return new Device((long) device
.get("Id").getValue(),
150 (String
) device
.get("Name").getValue(),
151 (long) device
.get("Created").getValue(),
152 (long) device
.get("LastSeen").getValue(),
153 thisDevice
.equals(d
.getObjectPath()));
154 }).collect(Collectors
.toList());
158 public void removeLinkedDevices(final long deviceId
) throws IOException
{
159 final var devicePath
= signal
.getDevice(deviceId
);
160 getRemoteObject(devicePath
, Signal
.Device
.class).removeDevice();
164 public void addDeviceLink(final URI linkUri
) throws IOException
, InvalidDeviceLinkException
{
165 signal
.addDevice(linkUri
.toString());
169 public void setRegistrationLockPin(final Optional
<String
> pin
) throws IOException
{
170 if (pin
.isPresent()) {
171 signal
.setPin(pin
.get());
178 public Profile
getRecipientProfile(final RecipientIdentifier
.Single recipient
) throws UnregisteredUserException
{
179 throw new UnsupportedOperationException();
183 public List
<Group
> getGroups() {
184 final var groups
= signal
.listGroups();
185 return groups
.stream().map(Signal
.StructGroup
::getObjectPath
).map(this::getGroup
).collect(Collectors
.toList());
189 public SendGroupMessageResults
quitGroup(
190 final GroupId groupId
, final Set
<RecipientIdentifier
.Single
> groupAdmins
191 ) throws GroupNotFoundException
, IOException
, NotAGroupMemberException
, LastGroupAdminException
{
192 if (groupAdmins
.size() > 0) {
193 throw new UnsupportedOperationException();
195 final var group
= getRemoteObject(signal
.getGroup(groupId
.serialize()), Signal
.Group
.class);
197 return new SendGroupMessageResults(0, List
.of());
201 public void deleteGroup(final GroupId groupId
) throws IOException
{
202 throw new UnsupportedOperationException();
206 public Pair
<GroupId
, SendGroupMessageResults
> createGroup(
207 final String name
, final Set
<RecipientIdentifier
.Single
> members
, final File avatarFile
208 ) throws IOException
, AttachmentInvalidException
{
209 final var newGroupId
= signal
.createGroup(emptyIfNull(name
),
210 members
.stream().map(RecipientIdentifier
.Single
::getIdentifier
).collect(Collectors
.toList()),
211 avatarFile
== null ?
"" : avatarFile
.getPath());
212 return new Pair
<>(GroupId
.unknownVersion(newGroupId
), new SendGroupMessageResults(0, List
.of()));
216 public SendGroupMessageResults
updateGroup(
217 final GroupId groupId
, final UpdateGroup updateGroup
218 ) throws IOException
, GroupNotFoundException
, AttachmentInvalidException
, NotAGroupMemberException
, GroupSendingNotAllowedException
{
219 final var group
= getRemoteObject(signal
.getGroup(groupId
.serialize()), Signal
.Group
.class);
220 if (updateGroup
.getName() != null) {
221 group
.Set("org.asamk.Signal.Group", "Name", updateGroup
.getName());
223 if (updateGroup
.getDescription() != null) {
224 group
.Set("org.asamk.Signal.Group", "Description", updateGroup
.getDescription());
226 if (updateGroup
.getAvatarFile() != null) {
227 group
.Set("org.asamk.Signal.Group",
229 updateGroup
.getAvatarFile() == null ?
"" : updateGroup
.getAvatarFile().getPath());
231 if (updateGroup
.getExpirationTimer() != null) {
232 group
.Set("org.asamk.Signal.Group", "MessageExpirationTimer", updateGroup
.getExpirationTimer());
234 if (updateGroup
.getAddMemberPermission() != null) {
235 group
.Set("org.asamk.Signal.Group", "PermissionAddMember", updateGroup
.getAddMemberPermission().name());
237 if (updateGroup
.getEditDetailsPermission() != null) {
238 group
.Set("org.asamk.Signal.Group", "PermissionEditDetails", updateGroup
.getEditDetailsPermission().name());
240 if (updateGroup
.getIsAnnouncementGroup() != null) {
241 group
.Set("org.asamk.Signal.Group",
242 "PermissionSendMessage",
243 updateGroup
.getIsAnnouncementGroup()
244 ? GroupPermission
.ONLY_ADMINS
.name()
245 : GroupPermission
.EVERY_MEMBER
.name());
247 if (updateGroup
.getMembers() != null) {
248 group
.addMembers(updateGroup
.getMembers()
250 .map(RecipientIdentifier
.Single
::getIdentifier
)
251 .collect(Collectors
.toList()));
253 if (updateGroup
.getRemoveMembers() != null) {
254 group
.removeMembers(updateGroup
.getRemoveMembers()
256 .map(RecipientIdentifier
.Single
::getIdentifier
)
257 .collect(Collectors
.toList()));
259 if (updateGroup
.getAdmins() != null) {
260 group
.addAdmins(updateGroup
.getAdmins()
262 .map(RecipientIdentifier
.Single
::getIdentifier
)
263 .collect(Collectors
.toList()));
265 if (updateGroup
.getRemoveAdmins() != null) {
266 group
.removeAdmins(updateGroup
.getRemoveAdmins()
268 .map(RecipientIdentifier
.Single
::getIdentifier
)
269 .collect(Collectors
.toList()));
271 if (updateGroup
.isResetGroupLink()) {
274 if (updateGroup
.getGroupLinkState() != null) {
275 switch (updateGroup
.getGroupLinkState()) {
276 case DISABLED
-> group
.disableLink();
277 case ENABLED
-> group
.enableLink(false);
278 case ENABLED_WITH_APPROVAL
-> group
.enableLink(true);
281 return new SendGroupMessageResults(0, List
.of());
285 public Pair
<GroupId
, SendGroupMessageResults
> joinGroup(final GroupInviteLinkUrl inviteLinkUrl
) throws IOException
, InactiveGroupLinkException
{
286 final var newGroupId
= signal
.joinGroup(inviteLinkUrl
.getUrl());
287 return new Pair
<>(GroupId
.unknownVersion(newGroupId
), new SendGroupMessageResults(0, List
.of()));
291 public void sendTypingMessage(
292 final TypingAction action
, final Set
<RecipientIdentifier
> recipients
293 ) throws IOException
, UntrustedIdentityException
, NotAGroupMemberException
, GroupNotFoundException
, GroupSendingNotAllowedException
{
294 for (final var recipient
: recipients
) {
295 if (recipient
instanceof RecipientIdentifier
.Single
) {
296 signal
.sendTyping(((RecipientIdentifier
.Single
) recipient
).getIdentifier(),
297 action
== TypingAction
.STOP
);
298 } else if (recipient
instanceof RecipientIdentifier
.Group
) {
299 throw new UnsupportedOperationException();
305 public void sendReadReceipt(
306 final RecipientIdentifier
.Single sender
, final List
<Long
> messageIds
307 ) throws IOException
, UntrustedIdentityException
{
308 signal
.sendReadReceipt(sender
.getIdentifier(), messageIds
);
312 public void sendViewedReceipt(
313 final RecipientIdentifier
.Single sender
, final List
<Long
> messageIds
314 ) throws IOException
, UntrustedIdentityException
{
315 throw new UnsupportedOperationException();
319 public SendMessageResults
sendMessage(
320 final Message message
, final Set
<RecipientIdentifier
> recipients
321 ) throws IOException
, AttachmentInvalidException
, NotAGroupMemberException
, GroupNotFoundException
, GroupSendingNotAllowedException
{
322 return handleMessage(recipients
,
323 numbers
-> signal
.sendMessage(message
.messageText(), message
.attachments(), numbers
),
324 () -> signal
.sendNoteToSelfMessage(message
.messageText(), message
.attachments()),
325 groupId
-> signal
.sendGroupMessage(message
.messageText(), message
.attachments(), groupId
));
329 public SendMessageResults
sendRemoteDeleteMessage(
330 final long targetSentTimestamp
, final Set
<RecipientIdentifier
> recipients
331 ) throws IOException
, NotAGroupMemberException
, GroupNotFoundException
, GroupSendingNotAllowedException
{
332 return handleMessage(recipients
,
333 numbers
-> signal
.sendRemoteDeleteMessage(targetSentTimestamp
, numbers
),
334 () -> signal
.sendRemoteDeleteMessage(targetSentTimestamp
, signal
.getSelfNumber()),
335 groupId
-> signal
.sendGroupRemoteDeleteMessage(targetSentTimestamp
, groupId
));
339 public SendMessageResults
sendMessageReaction(
341 final boolean remove
,
342 final RecipientIdentifier
.Single targetAuthor
,
343 final long targetSentTimestamp
,
344 final Set
<RecipientIdentifier
> recipients
345 ) throws IOException
, NotAGroupMemberException
, GroupNotFoundException
, GroupSendingNotAllowedException
{
346 return handleMessage(recipients
,
347 numbers
-> signal
.sendMessageReaction(emoji
,
349 targetAuthor
.getIdentifier(),
352 () -> signal
.sendMessageReaction(emoji
,
354 targetAuthor
.getIdentifier(),
356 signal
.getSelfNumber()),
357 groupId
-> signal
.sendGroupMessageReaction(emoji
,
359 targetAuthor
.getIdentifier(),
365 public SendMessageResults
sendEndSessionMessage(final Set
<RecipientIdentifier
.Single
> recipients
) throws IOException
{
366 signal
.sendEndSessionMessage(recipients
.stream()
367 .map(RecipientIdentifier
.Single
::getIdentifier
)
368 .collect(Collectors
.toList()));
369 return new SendMessageResults(0, Map
.of());
373 public void setContactName(
374 final RecipientIdentifier
.Single recipient
, final String name
375 ) throws NotMasterDeviceException
, UnregisteredUserException
{
376 signal
.setContactName(recipient
.getIdentifier(), name
);
380 public void setContactBlocked(
381 final RecipientIdentifier
.Single recipient
, final boolean blocked
382 ) throws NotMasterDeviceException
, IOException
{
383 signal
.setContactBlocked(recipient
.getIdentifier(), blocked
);
387 public void setGroupBlocked(
388 final GroupId groupId
, final boolean blocked
389 ) throws GroupNotFoundException
, IOException
{
390 setGroupProperty(groupId
, "IsBlocked", blocked
);
393 private void setGroupProperty(final GroupId groupId
, final String propertyName
, final boolean blocked
) {
394 final var group
= getRemoteObject(signal
.getGroup(groupId
.serialize()), Signal
.Group
.class);
395 group
.Set("org.asamk.Signal.Group", propertyName
, blocked
);
399 public void setExpirationTimer(
400 final RecipientIdentifier
.Single recipient
, final int messageExpirationTimer
401 ) throws IOException
{
402 signal
.setExpirationTimer(recipient
.getIdentifier(), messageExpirationTimer
);
406 public URI
uploadStickerPack(final File path
) throws IOException
, StickerPackInvalidException
{
408 return new URI(signal
.uploadStickerPack(path
.getPath()));
409 } catch (URISyntaxException e
) {
410 throw new AssertionError(e
);
415 public void requestAllSyncData() throws IOException
{
416 signal
.sendSyncRequest();
420 public void addReceiveHandler(final ReceiveMessageHandler handler
) {
421 throw new UnsupportedOperationException();
425 public void removeReceiveHandler(final ReceiveMessageHandler handler
) {
426 throw new UnsupportedOperationException();
430 public boolean isReceiving() {
431 throw new UnsupportedOperationException();
435 public void receiveMessages(final ReceiveMessageHandler handler
) throws IOException
{
436 throw new UnsupportedOperationException();
440 public void receiveMessages(
441 final long timeout
, final TimeUnit unit
, final ReceiveMessageHandler handler
442 ) throws IOException
{
443 throw new UnsupportedOperationException();
447 public void setIgnoreAttachments(final boolean ignoreAttachments
) {
448 throw new UnsupportedOperationException();
452 public boolean hasCaughtUpWithOldMessages() {
453 throw new UnsupportedOperationException();
457 public boolean isContactBlocked(final RecipientIdentifier
.Single recipient
) {
458 return signal
.isContactBlocked(recipient
.getIdentifier());
462 public File
getAttachmentFile(final SignalServiceAttachmentRemoteId attachmentId
) {
463 throw new UnsupportedOperationException();
467 public void sendContacts() throws IOException
{
468 signal
.sendContacts();
472 public List
<Pair
<RecipientAddress
, Contact
>> getContacts() {
473 throw new UnsupportedOperationException();
477 public String
getContactOrProfileName(final RecipientIdentifier
.Single recipient
) {
478 return signal
.getContactName(recipient
.getIdentifier());
482 public Group
getGroup(final GroupId groupId
) {
483 final var groupPath
= signal
.getGroup(groupId
.serialize());
484 return getGroup(groupPath
);
487 @SuppressWarnings("unchecked")
488 private Group
getGroup(final DBusPath groupPath
) {
489 final var group
= getRemoteObject(groupPath
, Signal
.Group
.class).GetAll("org.asamk.Signal.Group");
490 final var id
= (byte[]) group
.get("Id").getValue();
492 return new Group(GroupId
.unknownVersion(id
),
493 (String
) group
.get("Name").getValue(),
494 (String
) group
.get("Description").getValue(),
495 GroupInviteLinkUrl
.fromUri((String
) group
.get("GroupInviteLink").getValue()),
496 ((List
<String
>) group
.get("Members").getValue()).stream()
497 .map(m
-> new RecipientAddress(null, m
))
498 .collect(Collectors
.toSet()),
499 ((List
<String
>) group
.get("PendingMembers").getValue()).stream()
500 .map(m
-> new RecipientAddress(null, m
))
501 .collect(Collectors
.toSet()),
502 ((List
<String
>) group
.get("RequestingMembers").getValue()).stream()
503 .map(m
-> new RecipientAddress(null, m
))
504 .collect(Collectors
.toSet()),
505 ((List
<String
>) group
.get("Admins").getValue()).stream()
506 .map(m
-> new RecipientAddress(null, m
))
507 .collect(Collectors
.toSet()),
508 (boolean) group
.get("IsBlocked").getValue(),
509 (int) group
.get("MessageExpirationTimer").getValue(),
510 GroupPermission
.valueOf((String
) group
.get("PermissionAddMember").getValue()),
511 GroupPermission
.valueOf((String
) group
.get("PermissionEditDetails").getValue()),
512 GroupPermission
.valueOf((String
) group
.get("PermissionSendMessage").getValue()),
513 (boolean) group
.get("IsMember").getValue(),
514 (boolean) group
.get("IsAdmin").getValue());
515 } catch (GroupInviteLinkUrl
.InvalidGroupLinkException
| GroupInviteLinkUrl
.UnknownGroupLinkVersionException e
) {
516 throw new AssertionError(e
);
521 public List
<Identity
> getIdentities() {
522 throw new UnsupportedOperationException();
526 public List
<Identity
> getIdentities(final RecipientIdentifier
.Single recipient
) {
527 throw new UnsupportedOperationException();
531 public boolean trustIdentityVerified(final RecipientIdentifier
.Single recipient
, final byte[] fingerprint
) {
532 throw new UnsupportedOperationException();
536 public boolean trustIdentityVerifiedSafetyNumber(
537 final RecipientIdentifier
.Single recipient
, final String safetyNumber
539 throw new UnsupportedOperationException();
543 public boolean trustIdentityVerifiedSafetyNumber(
544 final RecipientIdentifier
.Single recipient
, final byte[] safetyNumber
546 throw new UnsupportedOperationException();
550 public boolean trustIdentityAllKeys(final RecipientIdentifier
.Single recipient
) {
551 throw new UnsupportedOperationException();
555 public SignalServiceAddress
resolveSignalServiceAddress(final SignalServiceAddress address
) {
560 public void close() throws IOException
{
563 private SendMessageResults
handleMessage(
564 Set
<RecipientIdentifier
> recipients
,
565 Function
<List
<String
>, Long
> recipientsHandler
,
566 Supplier
<Long
> noteToSelfHandler
,
567 Function
<byte[], Long
> groupHandler
570 final var singleRecipients
= recipients
.stream()
571 .filter(r
-> r
instanceof RecipientIdentifier
.Single
)
572 .map(RecipientIdentifier
.Single
.class::cast
)
573 .map(RecipientIdentifier
.Single
::getIdentifier
)
574 .collect(Collectors
.toList());
575 if (singleRecipients
.size() > 0) {
576 timestamp
= recipientsHandler
.apply(singleRecipients
);
579 if (recipients
.contains(RecipientIdentifier
.NoteToSelf
.INSTANCE
)) {
580 timestamp
= noteToSelfHandler
.get();
582 final var groupRecipients
= recipients
.stream()
583 .filter(r
-> r
instanceof RecipientIdentifier
.Group
)
584 .map(RecipientIdentifier
.Group
.class::cast
)
585 .map(RecipientIdentifier
.Group
::groupId
)
586 .collect(Collectors
.toList());
587 for (final var groupId
: groupRecipients
) {
588 timestamp
= groupHandler
.apply(groupId
.serialize());
590 return new SendMessageResults(timestamp
, Map
.of());
593 private String
emptyIfNull(final String string
) {
594 return string
== null ?
"" : string
;
597 private <T
extends DBusInterface
> T
getRemoteObject(final DBusPath devicePath
, final Class
<T
> type
) {
599 return connection
.getRemoteObject(DbusConfig
.getBusname(), devicePath
.getPath(), type
);
600 } catch (DBusException e
) {
601 throw new AssertionError(e
);