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(d
-> {
159 final var device
= getRemoteObject(d
.getObjectPath(),
160 Signal
.Device
.class).GetAll("org.asamk.Signal.Device");
161 return new Device((long) device
.get("Id").getValue(),
162 (String
) device
.get("Name").getValue(),
163 (long) device
.get("Created").getValue(),
164 (long) device
.get("LastSeen").getValue(),
165 thisDevice
.equals(d
.getObjectPath()));
166 }).collect(Collectors
.toList());
170 public void removeLinkedDevices(final long deviceId
) throws IOException
{
171 final var devicePath
= signal
.getDevice(deviceId
);
172 getRemoteObject(devicePath
, Signal
.Device
.class).removeDevice();
176 public void addDeviceLink(final URI linkUri
) throws IOException
, InvalidKeyException
{
177 signal
.addDevice(linkUri
.toString());
181 public void setRegistrationLockPin(final Optional
<String
> pin
) throws IOException
, UnauthenticatedResponseException
{
182 if (pin
.isPresent()) {
183 signal
.setPin(pin
.get());
190 public Profile
getRecipientProfile(final RecipientIdentifier
.Single recipient
) throws UnregisteredUserException
{
191 throw new UnsupportedOperationException();
195 public List
<Group
> getGroups() {
196 final var groupIds
= signal
.getGroupIds();
197 return groupIds
.stream().map(id
-> getGroup(GroupId
.unknownVersion(id
))).collect(Collectors
.toList());
201 public SendGroupMessageResults
quitGroup(
202 final GroupId groupId
, final Set
<RecipientIdentifier
.Single
> groupAdmins
203 ) throws GroupNotFoundException
, IOException
, NotAGroupMemberException
, LastGroupAdminException
{
204 if (groupAdmins
.size() > 0) {
205 throw new UnsupportedOperationException();
207 signal
.quitGroup(groupId
.serialize());
208 return new SendGroupMessageResults(0, List
.of());
212 public void deleteGroup(final GroupId groupId
) throws IOException
{
213 throw new UnsupportedOperationException();
217 public Pair
<GroupId
, SendGroupMessageResults
> createGroup(
218 final String name
, final Set
<RecipientIdentifier
.Single
> members
, final File avatarFile
219 ) throws IOException
, AttachmentInvalidException
{
220 final var newGroupId
= signal
.updateGroup(new byte[0],
222 members
.stream().map(RecipientIdentifier
.Single
::getIdentifier
).collect(Collectors
.toList()),
223 avatarFile
== null ?
"" : avatarFile
.getPath());
224 return new Pair
<>(GroupId
.unknownVersion(newGroupId
), new SendGroupMessageResults(0, List
.of()));
228 public SendGroupMessageResults
updateGroup(
229 final GroupId groupId
,
231 final String description
,
232 final Set
<RecipientIdentifier
.Single
> members
,
233 final Set
<RecipientIdentifier
.Single
> removeMembers
,
234 final Set
<RecipientIdentifier
.Single
> admins
,
235 final Set
<RecipientIdentifier
.Single
> removeAdmins
,
236 final boolean resetGroupLink
,
237 final GroupLinkState groupLinkState
,
238 final GroupPermission addMemberPermission
,
239 final GroupPermission editDetailsPermission
,
240 final File avatarFile
,
241 final Integer expirationTimer
,
242 final Boolean isAnnouncementGroup
243 ) throws IOException
, GroupNotFoundException
, AttachmentInvalidException
, NotAGroupMemberException
, GroupSendingNotAllowedException
{
244 signal
.updateGroup(groupId
.serialize(),
246 members
.stream().map(RecipientIdentifier
.Single
::getIdentifier
).collect(Collectors
.toList()),
247 avatarFile
== null ?
"" : avatarFile
.getPath());
248 return new SendGroupMessageResults(0, List
.of());
252 public Pair
<GroupId
, SendGroupMessageResults
> joinGroup(final GroupInviteLinkUrl inviteLinkUrl
) throws IOException
, GroupLinkNotActiveException
{
253 final var newGroupId
= signal
.joinGroup(inviteLinkUrl
.getUrl());
254 return new Pair
<>(GroupId
.unknownVersion(newGroupId
), new SendGroupMessageResults(0, List
.of()));
258 public void sendTypingMessage(
259 final TypingAction action
, final Set
<RecipientIdentifier
> recipients
260 ) throws IOException
, UntrustedIdentityException
, NotAGroupMemberException
, GroupNotFoundException
, GroupSendingNotAllowedException
{
261 for (final var recipient
: recipients
) {
262 if (recipient
instanceof RecipientIdentifier
.Single
) {
263 signal
.sendTyping(((RecipientIdentifier
.Single
) recipient
).getIdentifier(),
264 action
== TypingAction
.STOP
);
265 } else if (recipient
instanceof RecipientIdentifier
.Group
) {
266 throw new UnsupportedOperationException();
272 public void sendReadReceipt(
273 final RecipientIdentifier
.Single sender
, final List
<Long
> messageIds
274 ) throws IOException
, UntrustedIdentityException
{
275 signal
.sendReadReceipt(sender
.getIdentifier(), messageIds
);
279 public void sendViewedReceipt(
280 final RecipientIdentifier
.Single sender
, final List
<Long
> messageIds
281 ) throws IOException
, UntrustedIdentityException
{
282 throw new UnsupportedOperationException();
286 public SendMessageResults
sendMessage(
287 final Message message
, final Set
<RecipientIdentifier
> recipients
288 ) throws IOException
, AttachmentInvalidException
, NotAGroupMemberException
, GroupNotFoundException
, GroupSendingNotAllowedException
{
289 return handleMessage(recipients
,
290 numbers
-> signal
.sendMessage(message
.getMessageText(), message
.getAttachments(), numbers
),
291 () -> signal
.sendNoteToSelfMessage(message
.getMessageText(), message
.getAttachments()),
292 groupId
-> signal
.sendGroupMessage(message
.getMessageText(), message
.getAttachments(), groupId
));
296 public SendMessageResults
sendRemoteDeleteMessage(
297 final long targetSentTimestamp
, final Set
<RecipientIdentifier
> recipients
298 ) throws IOException
, NotAGroupMemberException
, GroupNotFoundException
, GroupSendingNotAllowedException
{
299 return handleMessage(recipients
,
300 numbers
-> signal
.sendRemoteDeleteMessage(targetSentTimestamp
, numbers
),
301 () -> signal
.sendRemoteDeleteMessage(targetSentTimestamp
, signal
.getSelfNumber()),
302 groupId
-> signal
.sendGroupRemoteDeleteMessage(targetSentTimestamp
, groupId
));
306 public SendMessageResults
sendMessageReaction(
308 final boolean remove
,
309 final RecipientIdentifier
.Single targetAuthor
,
310 final long targetSentTimestamp
,
311 final Set
<RecipientIdentifier
> recipients
312 ) throws IOException
, NotAGroupMemberException
, GroupNotFoundException
, GroupSendingNotAllowedException
{
313 return handleMessage(recipients
,
314 numbers
-> signal
.sendMessageReaction(emoji
,
316 targetAuthor
.getIdentifier(),
319 () -> signal
.sendMessageReaction(emoji
,
321 targetAuthor
.getIdentifier(),
323 signal
.getSelfNumber()),
324 groupId
-> signal
.sendGroupMessageReaction(emoji
,
326 targetAuthor
.getIdentifier(),
332 public SendMessageResults
sendEndSessionMessage(final Set
<RecipientIdentifier
.Single
> recipients
) throws IOException
{
333 signal
.sendEndSessionMessage(recipients
.stream()
334 .map(RecipientIdentifier
.Single
::getIdentifier
)
335 .collect(Collectors
.toList()));
336 return new SendMessageResults(0, Map
.of());
340 public void setContactName(
341 final RecipientIdentifier
.Single recipient
, final String name
342 ) throws NotMasterDeviceException
, UnregisteredUserException
{
343 signal
.setContactName(recipient
.getIdentifier(), name
);
347 public void setContactBlocked(
348 final RecipientIdentifier
.Single recipient
, final boolean blocked
349 ) throws NotMasterDeviceException
, IOException
{
350 signal
.setContactBlocked(recipient
.getIdentifier(), blocked
);
354 public void setGroupBlocked(
355 final GroupId groupId
, final boolean blocked
356 ) throws GroupNotFoundException
, IOException
{
357 signal
.setGroupBlocked(groupId
.serialize(), blocked
);
361 public void setExpirationTimer(
362 final RecipientIdentifier
.Single recipient
, final int messageExpirationTimer
363 ) throws IOException
{
364 signal
.setExpirationTimer(recipient
.getIdentifier(), messageExpirationTimer
);
368 public URI
uploadStickerPack(final File path
) throws IOException
, StickerPackInvalidException
{
370 return new URI(signal
.uploadStickerPack(path
.getPath()));
371 } catch (URISyntaxException e
) {
372 throw new AssertionError(e
);
377 public void requestAllSyncData() throws IOException
{
378 signal
.sendSyncRequest();
382 public void receiveMessages(
385 final boolean returnOnTimeout
,
386 final boolean ignoreAttachments
,
387 final ReceiveMessageHandler handler
388 ) throws IOException
{
389 throw new UnsupportedOperationException();
393 public boolean hasCaughtUpWithOldMessages() {
394 throw new UnsupportedOperationException();
398 public boolean isContactBlocked(final RecipientIdentifier
.Single recipient
) {
399 return signal
.isContactBlocked(recipient
.getIdentifier());
403 public File
getAttachmentFile(final SignalServiceAttachmentRemoteId attachmentId
) {
404 throw new UnsupportedOperationException();
408 public void sendContacts() throws IOException
{
409 signal
.sendContacts();
413 public List
<Pair
<RecipientAddress
, Contact
>> getContacts() {
414 throw new UnsupportedOperationException();
418 public String
getContactOrProfileName(final RecipientIdentifier
.Single recipient
) {
419 return signal
.getContactName(recipient
.getIdentifier());
423 public Group
getGroup(final GroupId groupId
) {
424 final var id
= groupId
.serialize();
425 return new Group(groupId
,
426 signal
.getGroupName(id
),
429 signal
.getGroupMembers(id
).stream().map(m
-> new RecipientAddress(null, m
)).collect(Collectors
.toSet()),
433 signal
.isGroupBlocked(id
),
436 signal
.isMember(id
));
440 public List
<Identity
> getIdentities() {
441 throw new UnsupportedOperationException();
445 public List
<Identity
> getIdentities(final RecipientIdentifier
.Single recipient
) {
446 throw new UnsupportedOperationException();
450 public boolean trustIdentityVerified(final RecipientIdentifier
.Single recipient
, final byte[] fingerprint
) {
451 throw new UnsupportedOperationException();
455 public boolean trustIdentityVerifiedSafetyNumber(
456 final RecipientIdentifier
.Single recipient
, final String safetyNumber
458 throw new UnsupportedOperationException();
462 public boolean trustIdentityVerifiedSafetyNumber(
463 final RecipientIdentifier
.Single recipient
, final byte[] safetyNumber
465 throw new UnsupportedOperationException();
469 public boolean trustIdentityAllKeys(final RecipientIdentifier
.Single recipient
) {
470 throw new UnsupportedOperationException();
474 public String
computeSafetyNumber(
475 final SignalServiceAddress theirAddress
, final IdentityKey theirIdentityKey
477 throw new UnsupportedOperationException();
481 public SignalServiceAddress
resolveSignalServiceAddress(final SignalServiceAddress address
) {
486 public void close() throws IOException
{
489 private SendMessageResults
handleMessage(
490 Set
<RecipientIdentifier
> recipients
,
491 Function
<List
<String
>, Long
> recipientsHandler
,
492 Supplier
<Long
> noteToSelfHandler
,
493 Function
<byte[], Long
> groupHandler
496 final var singleRecipients
= recipients
.stream()
497 .filter(r
-> r
instanceof RecipientIdentifier
.Single
)
498 .map(RecipientIdentifier
.Single
.class::cast
)
499 .map(RecipientIdentifier
.Single
::getIdentifier
)
500 .collect(Collectors
.toList());
501 if (singleRecipients
.size() > 0) {
502 timestamp
= recipientsHandler
.apply(singleRecipients
);
505 if (recipients
.contains(RecipientIdentifier
.NoteToSelf
.INSTANCE
)) {
506 timestamp
= noteToSelfHandler
.get();
508 final var groupRecipients
= recipients
.stream()
509 .filter(r
-> r
instanceof RecipientIdentifier
.Group
)
510 .map(RecipientIdentifier
.Group
.class::cast
)
512 .collect(Collectors
.toList());
513 for (final var groupId
: groupRecipients
) {
514 timestamp
= groupHandler
.apply(groupId
.serialize());
516 return new SendMessageResults(timestamp
, Map
.of());
519 private String
emptyIfNull(final String string
) {
520 return string
== null ?
"" : string
;
523 private <T
extends DBusInterface
> T
getRemoteObject(final DBusPath devicePath
, final Class
<T
> type
) {
525 return connection
.getRemoteObject(DbusConfig
.getBusname(), devicePath
.getPath(), type
);
526 } catch (DBusException e
) {
527 throw new AssertionError(e
);