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
.groups
.GroupId
;
19 import org
.asamk
.signal
.manager
.groups
.GroupInviteLinkUrl
;
20 import org
.asamk
.signal
.manager
.groups
.GroupLinkState
;
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 groupIds
= signal
.getGroupIds();
187 return groupIds
.stream().map(id
-> getGroup(GroupId
.unknownVersion(id
))).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 signal
.quitGroup(groupId
.serialize());
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
.updateGroup(new byte[0],
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
,
221 final String description
,
222 final Set
<RecipientIdentifier
.Single
> members
,
223 final Set
<RecipientIdentifier
.Single
> removeMembers
,
224 final Set
<RecipientIdentifier
.Single
> admins
,
225 final Set
<RecipientIdentifier
.Single
> removeAdmins
,
226 final boolean resetGroupLink
,
227 final GroupLinkState groupLinkState
,
228 final GroupPermission addMemberPermission
,
229 final GroupPermission editDetailsPermission
,
230 final File avatarFile
,
231 final Integer expirationTimer
,
232 final Boolean isAnnouncementGroup
233 ) throws IOException
, GroupNotFoundException
, AttachmentInvalidException
, NotAGroupMemberException
, GroupSendingNotAllowedException
{
234 signal
.updateGroup(groupId
.serialize(),
236 members
.stream().map(RecipientIdentifier
.Single
::getIdentifier
).collect(Collectors
.toList()),
237 avatarFile
== null ?
"" : avatarFile
.getPath());
238 return new SendGroupMessageResults(0, List
.of());
242 public Pair
<GroupId
, SendGroupMessageResults
> joinGroup(final GroupInviteLinkUrl inviteLinkUrl
) throws IOException
, GroupLinkNotActiveException
{
243 final var newGroupId
= signal
.joinGroup(inviteLinkUrl
.getUrl());
244 return new Pair
<>(GroupId
.unknownVersion(newGroupId
), new SendGroupMessageResults(0, List
.of()));
248 public void sendTypingMessage(
249 final TypingAction action
, final Set
<RecipientIdentifier
> recipients
250 ) throws IOException
, UntrustedIdentityException
, NotAGroupMemberException
, GroupNotFoundException
, GroupSendingNotAllowedException
{
251 for (final var recipient
: recipients
) {
252 if (recipient
instanceof RecipientIdentifier
.Single
) {
253 signal
.sendTyping(((RecipientIdentifier
.Single
) recipient
).getIdentifier(),
254 action
== TypingAction
.STOP
);
255 } else if (recipient
instanceof RecipientIdentifier
.Group
) {
256 throw new UnsupportedOperationException();
262 public void sendReadReceipt(
263 final RecipientIdentifier
.Single sender
, final List
<Long
> messageIds
264 ) throws IOException
, UntrustedIdentityException
{
265 signal
.sendReadReceipt(sender
.getIdentifier(), messageIds
);
269 public void sendViewedReceipt(
270 final RecipientIdentifier
.Single sender
, final List
<Long
> messageIds
271 ) throws IOException
, UntrustedIdentityException
{
272 throw new UnsupportedOperationException();
276 public SendMessageResults
sendMessage(
277 final Message message
, final Set
<RecipientIdentifier
> recipients
278 ) throws IOException
, AttachmentInvalidException
, NotAGroupMemberException
, GroupNotFoundException
, GroupSendingNotAllowedException
{
279 return handleMessage(recipients
,
280 numbers
-> signal
.sendMessage(message
.getMessageText(), message
.getAttachments(), numbers
),
281 () -> signal
.sendNoteToSelfMessage(message
.getMessageText(), message
.getAttachments()),
282 groupId
-> signal
.sendGroupMessage(message
.getMessageText(), message
.getAttachments(), groupId
));
286 public SendMessageResults
sendRemoteDeleteMessage(
287 final long targetSentTimestamp
, final Set
<RecipientIdentifier
> recipients
288 ) throws IOException
, NotAGroupMemberException
, GroupNotFoundException
, GroupSendingNotAllowedException
{
289 return handleMessage(recipients
,
290 numbers
-> signal
.sendRemoteDeleteMessage(targetSentTimestamp
, numbers
),
291 () -> signal
.sendRemoteDeleteMessage(targetSentTimestamp
, signal
.getSelfNumber()),
292 groupId
-> signal
.sendGroupRemoteDeleteMessage(targetSentTimestamp
, groupId
));
296 public SendMessageResults
sendMessageReaction(
298 final boolean remove
,
299 final RecipientIdentifier
.Single targetAuthor
,
300 final long targetSentTimestamp
,
301 final Set
<RecipientIdentifier
> recipients
302 ) throws IOException
, NotAGroupMemberException
, GroupNotFoundException
, GroupSendingNotAllowedException
{
303 return handleMessage(recipients
,
304 numbers
-> signal
.sendMessageReaction(emoji
,
306 targetAuthor
.getIdentifier(),
309 () -> signal
.sendMessageReaction(emoji
,
311 targetAuthor
.getIdentifier(),
313 signal
.getSelfNumber()),
314 groupId
-> signal
.sendGroupMessageReaction(emoji
,
316 targetAuthor
.getIdentifier(),
322 public SendMessageResults
sendEndSessionMessage(final Set
<RecipientIdentifier
.Single
> recipients
) throws IOException
{
323 signal
.sendEndSessionMessage(recipients
.stream()
324 .map(RecipientIdentifier
.Single
::getIdentifier
)
325 .collect(Collectors
.toList()));
326 return new SendMessageResults(0, Map
.of());
330 public void setContactName(
331 final RecipientIdentifier
.Single recipient
, final String name
332 ) throws NotMasterDeviceException
, UnregisteredUserException
{
333 signal
.setContactName(recipient
.getIdentifier(), name
);
337 public void setContactBlocked(
338 final RecipientIdentifier
.Single recipient
, final boolean blocked
339 ) throws NotMasterDeviceException
, IOException
{
340 signal
.setContactBlocked(recipient
.getIdentifier(), blocked
);
344 public void setGroupBlocked(
345 final GroupId groupId
, final boolean blocked
346 ) throws GroupNotFoundException
, IOException
{
347 signal
.setGroupBlocked(groupId
.serialize(), blocked
);
351 public void setExpirationTimer(
352 final RecipientIdentifier
.Single recipient
, final int messageExpirationTimer
353 ) throws IOException
{
354 signal
.setExpirationTimer(recipient
.getIdentifier(), messageExpirationTimer
);
358 public URI
uploadStickerPack(final File path
) throws IOException
, StickerPackInvalidException
{
360 return new URI(signal
.uploadStickerPack(path
.getPath()));
361 } catch (URISyntaxException e
) {
362 throw new AssertionError(e
);
367 public void requestAllSyncData() throws IOException
{
368 signal
.sendSyncRequest();
372 public void receiveMessages(
375 final boolean returnOnTimeout
,
376 final boolean ignoreAttachments
,
377 final ReceiveMessageHandler handler
378 ) throws IOException
{
379 throw new UnsupportedOperationException();
383 public boolean hasCaughtUpWithOldMessages() {
384 throw new UnsupportedOperationException();
388 public boolean isContactBlocked(final RecipientIdentifier
.Single recipient
) {
389 return signal
.isContactBlocked(recipient
.getIdentifier());
393 public File
getAttachmentFile(final SignalServiceAttachmentRemoteId attachmentId
) {
394 throw new UnsupportedOperationException();
398 public void sendContacts() throws IOException
{
399 signal
.sendContacts();
403 public List
<Pair
<RecipientAddress
, Contact
>> getContacts() {
404 throw new UnsupportedOperationException();
408 public String
getContactOrProfileName(final RecipientIdentifier
.Single recipient
) {
409 return signal
.getContactName(recipient
.getIdentifier());
413 public Group
getGroup(final GroupId groupId
) {
414 final var id
= groupId
.serialize();
415 return new Group(groupId
,
416 signal
.getGroupName(id
),
419 signal
.getGroupMembers(id
).stream().map(m
-> new RecipientAddress(null, m
)).collect(Collectors
.toSet()),
423 signal
.isGroupBlocked(id
),
426 signal
.isMember(id
));
430 public List
<Identity
> getIdentities() {
431 throw new UnsupportedOperationException();
435 public List
<Identity
> getIdentities(final RecipientIdentifier
.Single recipient
) {
436 throw new UnsupportedOperationException();
440 public boolean trustIdentityVerified(final RecipientIdentifier
.Single recipient
, final byte[] fingerprint
) {
441 throw new UnsupportedOperationException();
445 public boolean trustIdentityVerifiedSafetyNumber(
446 final RecipientIdentifier
.Single recipient
, final String safetyNumber
448 throw new UnsupportedOperationException();
452 public boolean trustIdentityVerifiedSafetyNumber(
453 final RecipientIdentifier
.Single recipient
, final byte[] safetyNumber
455 throw new UnsupportedOperationException();
459 public boolean trustIdentityAllKeys(final RecipientIdentifier
.Single recipient
) {
460 throw new UnsupportedOperationException();
464 public String
computeSafetyNumber(
465 final SignalServiceAddress theirAddress
, final IdentityKey theirIdentityKey
467 throw new UnsupportedOperationException();
471 public SignalServiceAddress
resolveSignalServiceAddress(final SignalServiceAddress address
) {
476 public void close() throws IOException
{
479 private SendMessageResults
handleMessage(
480 Set
<RecipientIdentifier
> recipients
,
481 Function
<List
<String
>, Long
> recipientsHandler
,
482 Supplier
<Long
> noteToSelfHandler
,
483 Function
<byte[], Long
> groupHandler
486 final var singleRecipients
= recipients
.stream()
487 .filter(r
-> r
instanceof RecipientIdentifier
.Single
)
488 .map(RecipientIdentifier
.Single
.class::cast
)
489 .map(RecipientIdentifier
.Single
::getIdentifier
)
490 .collect(Collectors
.toList());
491 if (singleRecipients
.size() > 0) {
492 timestamp
= recipientsHandler
.apply(singleRecipients
);
495 if (recipients
.contains(RecipientIdentifier
.NoteToSelf
.INSTANCE
)) {
496 timestamp
= noteToSelfHandler
.get();
498 final var groupRecipients
= recipients
.stream()
499 .filter(r
-> r
instanceof RecipientIdentifier
.Group
)
500 .map(RecipientIdentifier
.Group
.class::cast
)
502 .collect(Collectors
.toList());
503 for (final var groupId
: groupRecipients
) {
504 timestamp
= groupHandler
.apply(groupId
.serialize());
506 return new SendMessageResults(timestamp
, Map
.of());
509 private String
emptyIfNull(final String string
) {
510 return string
== null ?
"" : string
;
513 private <T
extends DBusInterface
> T
getRemoteObject(final DBusPath devicePath
, final Class
<T
> type
) {
515 return connection
.getRemoteObject(DbusConfig
.getBusname(), devicePath
.getPath(), type
);
516 } catch (DBusException e
) {
517 throw new AssertionError(e
);