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 signal
.setConfiguration(
113 unidentifiedDeliveryIndicators
,
120 public List
<Boolean
> getConfiguration() {
121 return signal
.getConfiguration();
125 public void setProfile(
126 final String givenName
,
127 final String familyName
,
129 final String aboutEmoji
,
130 final Optional
<File
> avatar
131 ) throws IOException
{
132 signal
.updateProfile(emptyIfNull(givenName
),
133 emptyIfNull(familyName
),
135 emptyIfNull(aboutEmoji
),
136 avatar
== null ?
"" : avatar
.transform(File
::getPath
).or(""),
137 avatar
!= null && !avatar
.isPresent());
141 public void unregister() throws IOException
{
142 throw new UnsupportedOperationException();
146 public void deleteAccount() throws IOException
{
147 throw new UnsupportedOperationException();
151 public void submitRateLimitRecaptchaChallenge(final String challenge
, final String captcha
) throws IOException
{
152 throw new UnsupportedOperationException();
156 public List
<Device
> getLinkedDevices() throws IOException
{
157 final var thisDevice
= signal
.getThisDevice();
158 return signal
.listDevices().stream().map(devicePath
-> {
159 final var device
= getRemoteObject(devicePath
, Signal
.Device
.class).GetAll("org.asamk.Signal.Device");
160 return new Device((long) device
.get("Id").getValue(),
161 (String
) device
.get("Name").getValue(),
162 (long) device
.get("Created").getValue(),
163 (long) device
.get("LastSeen").getValue(),
164 thisDevice
.equals(devicePath
));
165 }).collect(Collectors
.toList());
169 public void removeLinkedDevices(final long deviceId
) throws IOException
{
170 final var devicePath
= signal
.getDevice(deviceId
);
171 getRemoteObject(devicePath
, Signal
.Device
.class).removeDevice();
175 public void addDeviceLink(final URI linkUri
) throws IOException
, InvalidKeyException
{
176 signal
.addDevice(linkUri
.toString());
180 public void setRegistrationLockPin(final Optional
<String
> pin
) throws IOException
, UnauthenticatedResponseException
{
181 if (pin
.isPresent()) {
182 signal
.setPin(pin
.get());
189 public Profile
getRecipientProfile(final RecipientIdentifier
.Single recipient
) throws UnregisteredUserException
{
190 throw new UnsupportedOperationException();
194 public List
<Group
> getGroups() {
195 final var groupIds
= signal
.getGroupIds();
196 return groupIds
.stream().map(id
-> getGroup(GroupId
.unknownVersion(id
))).collect(Collectors
.toList());
200 public SendGroupMessageResults
quitGroup(
201 final GroupId groupId
, final Set
<RecipientIdentifier
.Single
> groupAdmins
202 ) throws GroupNotFoundException
, IOException
, NotAGroupMemberException
, LastGroupAdminException
{
203 if (groupAdmins
.size() > 0) {
204 throw new UnsupportedOperationException();
206 signal
.quitGroup(groupId
.serialize());
207 return new SendGroupMessageResults(0, List
.of());
211 public void deleteGroup(final GroupId groupId
) throws IOException
{
212 throw new UnsupportedOperationException();
216 public Pair
<GroupId
, SendGroupMessageResults
> createGroup(
217 final String name
, final Set
<RecipientIdentifier
.Single
> members
, final File avatarFile
218 ) throws IOException
, AttachmentInvalidException
{
219 final var newGroupId
= signal
.updateGroup(new byte[0],
221 members
.stream().map(RecipientIdentifier
.Single
::getIdentifier
).collect(Collectors
.toList()),
222 avatarFile
== null ?
"" : avatarFile
.getPath());
223 return new Pair
<>(GroupId
.unknownVersion(newGroupId
), new SendGroupMessageResults(0, List
.of()));
227 public SendGroupMessageResults
updateGroup(
228 final GroupId groupId
,
230 final String description
,
231 final Set
<RecipientIdentifier
.Single
> members
,
232 final Set
<RecipientIdentifier
.Single
> removeMembers
,
233 final Set
<RecipientIdentifier
.Single
> admins
,
234 final Set
<RecipientIdentifier
.Single
> removeAdmins
,
235 final boolean resetGroupLink
,
236 final GroupLinkState groupLinkState
,
237 final GroupPermission addMemberPermission
,
238 final GroupPermission editDetailsPermission
,
239 final File avatarFile
,
240 final Integer expirationTimer
,
241 final Boolean isAnnouncementGroup
242 ) throws IOException
, GroupNotFoundException
, AttachmentInvalidException
, NotAGroupMemberException
, GroupSendingNotAllowedException
{
243 signal
.updateGroup(groupId
.serialize(),
245 members
.stream().map(RecipientIdentifier
.Single
::getIdentifier
).collect(Collectors
.toList()),
246 avatarFile
== null ?
"" : avatarFile
.getPath());
247 return new SendGroupMessageResults(0, List
.of());
251 public Pair
<GroupId
, SendGroupMessageResults
> joinGroup(final GroupInviteLinkUrl inviteLinkUrl
) throws IOException
, GroupLinkNotActiveException
{
252 final var newGroupId
= signal
.joinGroup(inviteLinkUrl
.getUrl());
253 return new Pair
<>(GroupId
.unknownVersion(newGroupId
), new SendGroupMessageResults(0, List
.of()));
257 public void sendTypingMessage(
258 final TypingAction action
, final Set
<RecipientIdentifier
> recipients
259 ) throws IOException
, UntrustedIdentityException
, NotAGroupMemberException
, GroupNotFoundException
, GroupSendingNotAllowedException
{
260 for (final var recipient
: recipients
) {
261 if (recipient
instanceof RecipientIdentifier
.Single
) {
262 signal
.sendTyping(((RecipientIdentifier
.Single
) recipient
).getIdentifier(),
263 action
== TypingAction
.STOP
);
264 } else if (recipient
instanceof RecipientIdentifier
.Group
) {
265 throw new UnsupportedOperationException();
271 public void sendReadReceipt(
272 final RecipientIdentifier
.Single sender
, final List
<Long
> messageIds
273 ) throws IOException
, UntrustedIdentityException
{
274 signal
.sendReadReceipt(sender
.getIdentifier(), messageIds
);
278 public void sendViewedReceipt(
279 final RecipientIdentifier
.Single sender
, final List
<Long
> messageIds
280 ) throws IOException
, UntrustedIdentityException
{
281 throw new UnsupportedOperationException();
285 public SendMessageResults
sendMessage(
286 final Message message
, final Set
<RecipientIdentifier
> recipients
287 ) throws IOException
, AttachmentInvalidException
, NotAGroupMemberException
, GroupNotFoundException
, GroupSendingNotAllowedException
{
288 return handleMessage(recipients
,
289 numbers
-> signal
.sendMessage(message
.getMessageText(), message
.getAttachments(), numbers
),
290 () -> signal
.sendNoteToSelfMessage(message
.getMessageText(), message
.getAttachments()),
291 groupId
-> signal
.sendGroupMessage(message
.getMessageText(), message
.getAttachments(), groupId
));
295 public SendMessageResults
sendRemoteDeleteMessage(
296 final long targetSentTimestamp
, final Set
<RecipientIdentifier
> recipients
297 ) throws IOException
, NotAGroupMemberException
, GroupNotFoundException
, GroupSendingNotAllowedException
{
298 return handleMessage(recipients
,
299 numbers
-> signal
.sendRemoteDeleteMessage(targetSentTimestamp
, numbers
),
300 () -> signal
.sendRemoteDeleteMessage(targetSentTimestamp
, signal
.getSelfNumber()),
301 groupId
-> signal
.sendGroupRemoteDeleteMessage(targetSentTimestamp
, groupId
));
305 public SendMessageResults
sendMessageReaction(
307 final boolean remove
,
308 final RecipientIdentifier
.Single targetAuthor
,
309 final long targetSentTimestamp
,
310 final Set
<RecipientIdentifier
> recipients
311 ) throws IOException
, NotAGroupMemberException
, GroupNotFoundException
, GroupSendingNotAllowedException
{
312 return handleMessage(recipients
,
313 numbers
-> signal
.sendMessageReaction(emoji
,
315 targetAuthor
.getIdentifier(),
318 () -> signal
.sendMessageReaction(emoji
,
320 targetAuthor
.getIdentifier(),
322 signal
.getSelfNumber()),
323 groupId
-> signal
.sendGroupMessageReaction(emoji
,
325 targetAuthor
.getIdentifier(),
331 public SendMessageResults
sendEndSessionMessage(final Set
<RecipientIdentifier
.Single
> recipients
) throws IOException
{
332 signal
.sendEndSessionMessage(recipients
.stream()
333 .map(RecipientIdentifier
.Single
::getIdentifier
)
334 .collect(Collectors
.toList()));
335 return new SendMessageResults(0, Map
.of());
339 public void setContactName(
340 final RecipientIdentifier
.Single recipient
, final String name
341 ) throws NotMasterDeviceException
, UnregisteredUserException
{
342 signal
.setContactName(recipient
.getIdentifier(), name
);
346 public void setContactBlocked(
347 final RecipientIdentifier
.Single recipient
, final boolean blocked
348 ) throws NotMasterDeviceException
, IOException
{
349 signal
.setContactBlocked(recipient
.getIdentifier(), blocked
);
353 public void setGroupBlocked(
354 final GroupId groupId
, final boolean blocked
355 ) throws GroupNotFoundException
, IOException
{
356 signal
.setGroupBlocked(groupId
.serialize(), blocked
);
360 public void setExpirationTimer(
361 final RecipientIdentifier
.Single recipient
, final int messageExpirationTimer
362 ) throws IOException
{
363 signal
.setExpirationTimer(recipient
.getIdentifier(), messageExpirationTimer
);
367 public URI
uploadStickerPack(final File path
) throws IOException
, StickerPackInvalidException
{
369 return new URI(signal
.uploadStickerPack(path
.getPath()));
370 } catch (URISyntaxException e
) {
371 throw new AssertionError(e
);
376 public void requestAllSyncData() throws IOException
{
377 signal
.sendSyncRequest();
381 public void receiveMessages(
384 final boolean returnOnTimeout
,
385 final boolean ignoreAttachments
,
386 final ReceiveMessageHandler handler
387 ) throws IOException
{
388 throw new UnsupportedOperationException();
392 public boolean hasCaughtUpWithOldMessages() {
393 throw new UnsupportedOperationException();
397 public boolean isContactBlocked(final RecipientIdentifier
.Single recipient
) {
398 return signal
.isContactBlocked(recipient
.getIdentifier());
402 public File
getAttachmentFile(final SignalServiceAttachmentRemoteId attachmentId
) {
403 throw new UnsupportedOperationException();
407 public void sendContacts() throws IOException
{
408 signal
.sendContacts();
412 public List
<Pair
<RecipientAddress
, Contact
>> getContacts() {
413 throw new UnsupportedOperationException();
417 public String
getContactOrProfileName(final RecipientIdentifier
.Single recipient
) {
418 return signal
.getContactName(recipient
.getIdentifier());
422 public Group
getGroup(final GroupId groupId
) {
423 final var id
= groupId
.serialize();
424 return new Group(groupId
,
425 signal
.getGroupName(id
),
428 signal
.getGroupMembers(id
).stream().map(m
-> new RecipientAddress(null, m
)).collect(Collectors
.toSet()),
432 signal
.isGroupBlocked(id
),
435 signal
.isMember(id
));
439 public List
<Identity
> getIdentities() {
440 throw new UnsupportedOperationException();
444 public List
<Identity
> getIdentities(final RecipientIdentifier
.Single recipient
) {
445 throw new UnsupportedOperationException();
449 public boolean trustIdentityVerified(final RecipientIdentifier
.Single recipient
, final byte[] fingerprint
) {
450 throw new UnsupportedOperationException();
454 public boolean trustIdentityVerifiedSafetyNumber(
455 final RecipientIdentifier
.Single recipient
, final String safetyNumber
457 throw new UnsupportedOperationException();
461 public boolean trustIdentityVerifiedSafetyNumber(
462 final RecipientIdentifier
.Single recipient
, final byte[] safetyNumber
464 throw new UnsupportedOperationException();
468 public boolean trustIdentityAllKeys(final RecipientIdentifier
.Single recipient
) {
469 throw new UnsupportedOperationException();
473 public String
computeSafetyNumber(
474 final SignalServiceAddress theirAddress
, final IdentityKey theirIdentityKey
476 throw new UnsupportedOperationException();
480 public SignalServiceAddress
resolveSignalServiceAddress(final SignalServiceAddress address
) {
485 public void close() throws IOException
{
488 private SendMessageResults
handleMessage(
489 Set
<RecipientIdentifier
> recipients
,
490 Function
<List
<String
>, Long
> recipientsHandler
,
491 Supplier
<Long
> noteToSelfHandler
,
492 Function
<byte[], Long
> groupHandler
495 final var singleRecipients
= recipients
.stream()
496 .filter(r
-> r
instanceof RecipientIdentifier
.Single
)
497 .map(RecipientIdentifier
.Single
.class::cast
)
498 .map(RecipientIdentifier
.Single
::getIdentifier
)
499 .collect(Collectors
.toList());
500 if (singleRecipients
.size() > 0) {
501 timestamp
= recipientsHandler
.apply(singleRecipients
);
504 if (recipients
.contains(RecipientIdentifier
.NoteToSelf
.INSTANCE
)) {
505 timestamp
= noteToSelfHandler
.get();
507 final var groupRecipients
= recipients
.stream()
508 .filter(r
-> r
instanceof RecipientIdentifier
.Group
)
509 .map(RecipientIdentifier
.Group
.class::cast
)
511 .collect(Collectors
.toList());
512 for (final var groupId
: groupRecipients
) {
513 timestamp
= groupHandler
.apply(groupId
.serialize());
515 return new SendMessageResults(timestamp
, Map
.of());
518 private String
emptyIfNull(final String string
) {
519 return string
== null ?
"" : string
;
522 private <T
extends DBusInterface
> T
getRemoteObject(final DBusPath devicePath
, final Class
<T
> type
) {
524 return connection
.getRemoteObject(DbusConfig
.getBusname(), devicePath
.getPath(), type
);
525 } catch (DBusException e
) {
526 throw new AssertionError(e
);