1 package org
.asamk
.signal
.dbus
;
3 import org
.asamk
.Signal
;
4 import org
.asamk
.signal
.BaseConfig
;
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
.Message
;
11 import org
.asamk
.signal
.manager
.api
.RecipientIdentifier
;
12 import org
.asamk
.signal
.manager
.api
.TypingAction
;
13 import org
.asamk
.signal
.manager
.groups
.GroupId
;
14 import org
.asamk
.signal
.manager
.groups
.GroupInviteLinkUrl
;
15 import org
.asamk
.signal
.manager
.groups
.GroupNotFoundException
;
16 import org
.asamk
.signal
.manager
.groups
.GroupSendingNotAllowedException
;
17 import org
.asamk
.signal
.manager
.groups
.LastGroupAdminException
;
18 import org
.asamk
.signal
.manager
.groups
.NotAGroupMemberException
;
19 import org
.asamk
.signal
.manager
.storage
.identities
.IdentityInfo
;
20 import org
.asamk
.signal
.util
.ErrorUtils
;
21 import org
.asamk
.signal
.util
.Util
;
22 import org
.freedesktop
.dbus
.exceptions
.DBusExecutionException
;
23 import org
.whispersystems
.libsignal
.util
.Pair
;
24 import org
.whispersystems
.libsignal
.util
.guava
.Optional
;
25 import org
.whispersystems
.signalservice
.api
.groupsv2
.GroupLinkNotActiveException
;
26 import org
.whispersystems
.signalservice
.api
.messages
.SendMessageResult
;
27 import org
.whispersystems
.signalservice
.api
.push
.exceptions
.UnregisteredUserException
;
28 import org
.whispersystems
.signalservice
.api
.util
.InvalidNumberException
;
29 import org
.whispersystems
.signalservice
.internal
.contacts
.crypto
.UnauthenticatedResponseException
;
32 import java
.io
.IOException
;
33 import java
.util
.ArrayList
;
34 import java
.util
.Collection
;
35 import java
.util
.HashSet
;
36 import java
.util
.List
;
38 import java
.util
.Objects
;
40 import java
.util
.stream
.Collectors
;
41 import java
.util
.stream
.Stream
;
43 import static org
.asamk
.signal
.util
.Util
.getLegacyIdentifier
;
45 public class DbusSignalImpl
implements Signal
{
47 private final Manager m
;
48 private final String objectPath
;
50 public DbusSignalImpl(final Manager m
, final String objectPath
) {
52 this.objectPath
= objectPath
;
56 public boolean isRemote() {
61 public String
getObjectPath() {
66 public long sendMessage(final String message
, final List
<String
> attachments
, final String recipient
) {
67 var recipients
= new ArrayList
<String
>(1);
68 recipients
.add(recipient
);
69 return sendMessage(message
, attachments
, recipients
);
73 public long sendMessage(final String message
, final List
<String
> attachments
, final List
<String
> recipients
) {
75 final var results
= m
.sendMessage(new Message(message
, attachments
),
76 getSingleRecipientIdentifiers(recipients
, m
.getUsername()).stream()
77 .map(RecipientIdentifier
.class::cast
)
78 .collect(Collectors
.toSet()));
80 checkSendMessageResults(results
.getTimestamp(), results
.getResults());
81 return results
.getTimestamp();
82 } catch (AttachmentInvalidException e
) {
83 throw new Error
.AttachmentInvalid(e
.getMessage());
84 } catch (IOException e
) {
85 throw new Error
.Failure(e
.getMessage());
86 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
87 throw new Error
.GroupNotFound(e
.getMessage());
92 public long sendRemoteDeleteMessage(
93 final long targetSentTimestamp
, final String recipient
95 var recipients
= new ArrayList
<String
>(1);
96 recipients
.add(recipient
);
97 return sendRemoteDeleteMessage(targetSentTimestamp
, recipients
);
101 public long sendRemoteDeleteMessage(
102 final long targetSentTimestamp
, final List
<String
> recipients
105 final var results
= m
.sendRemoteDeleteMessage(targetSentTimestamp
,
106 getSingleRecipientIdentifiers(recipients
, m
.getUsername()).stream()
107 .map(RecipientIdentifier
.class::cast
)
108 .collect(Collectors
.toSet()));
109 checkSendMessageResults(results
.getTimestamp(), results
.getResults());
110 return results
.getTimestamp();
111 } catch (IOException e
) {
112 throw new Error
.Failure(e
.getMessage());
113 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
114 throw new Error
.GroupNotFound(e
.getMessage());
119 public long sendGroupRemoteDeleteMessage(
120 final long targetSentTimestamp
, final byte[] groupId
123 final var results
= m
.sendRemoteDeleteMessage(targetSentTimestamp
,
124 Set
.of(new RecipientIdentifier
.Group(getGroupId(groupId
))));
125 checkSendMessageResults(results
.getTimestamp(), results
.getResults());
126 return results
.getTimestamp();
127 } catch (IOException e
) {
128 throw new Error
.Failure(e
.getMessage());
129 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
130 throw new Error
.GroupNotFound(e
.getMessage());
135 public long sendMessageReaction(
137 final boolean remove
,
138 final String targetAuthor
,
139 final long targetSentTimestamp
,
140 final String recipient
142 var recipients
= new ArrayList
<String
>(1);
143 recipients
.add(recipient
);
144 return sendMessageReaction(emoji
, remove
, targetAuthor
, targetSentTimestamp
, recipients
);
148 public long sendMessageReaction(
150 final boolean remove
,
151 final String targetAuthor
,
152 final long targetSentTimestamp
,
153 final List
<String
> recipients
156 final var results
= m
.sendMessageReaction(emoji
,
158 getSingleRecipientIdentifier(targetAuthor
, m
.getUsername()),
160 getSingleRecipientIdentifiers(recipients
, m
.getUsername()).stream()
161 .map(RecipientIdentifier
.class::cast
)
162 .collect(Collectors
.toSet()));
163 checkSendMessageResults(results
.getTimestamp(), results
.getResults());
164 return results
.getTimestamp();
165 } catch (IOException e
) {
166 throw new Error
.Failure(e
.getMessage());
167 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
168 throw new Error
.GroupNotFound(e
.getMessage());
173 public void sendTyping(
174 final String recipient
, final boolean stop
175 ) throws Error
.Failure
, Error
.GroupNotFound
, Error
.UntrustedIdentity
{
177 var recipients
= new ArrayList
<String
>(1);
178 recipients
.add(recipient
);
179 m
.sendTypingMessage(stop ? TypingAction
.STOP
: TypingAction
.START
,
180 getSingleRecipientIdentifiers(recipients
, m
.getUsername()).stream()
181 .map(RecipientIdentifier
.class::cast
)
182 .collect(Collectors
.toSet()));
183 } catch (IOException e
) {
184 throw new Error
.Failure(e
.getMessage());
185 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
186 throw new Error
.GroupNotFound(e
.getMessage());
187 } catch (UntrustedIdentityException e
) {
188 throw new Error
.UntrustedIdentity(e
.getMessage());
193 public void sendReadReceipt(
194 final String recipient
, final List
<Long
> timestamps
195 ) throws Error
.Failure
, Error
.UntrustedIdentity
{
197 m
.sendReadReceipt(getSingleRecipientIdentifier(recipient
, m
.getUsername()), timestamps
);
198 } catch (IOException e
) {
199 throw new Error
.Failure(e
.getMessage());
200 } catch (UntrustedIdentityException e
) {
201 throw new Error
.UntrustedIdentity(e
.getMessage());
206 public void sendContacts() {
209 } catch (IOException e
) {
210 throw new Error
.Failure("SendContacts error: " + e
.getMessage());
215 public void sendSyncRequest() {
217 m
.requestAllSyncData();
218 } catch (IOException e
) {
219 throw new Error
.Failure("Request sync data error: " + e
.getMessage());
224 public long sendNoteToSelfMessage(
225 final String message
, final List
<String
> attachments
226 ) throws Error
.AttachmentInvalid
, Error
.Failure
, Error
.UntrustedIdentity
{
228 final var results
= m
.sendMessage(new Message(message
, attachments
),
229 Set
.of(new RecipientIdentifier
.NoteToSelf()));
230 checkSendMessageResults(results
.getTimestamp(), results
.getResults());
231 return results
.getTimestamp();
232 } catch (AttachmentInvalidException e
) {
233 throw new Error
.AttachmentInvalid(e
.getMessage());
234 } catch (IOException e
) {
235 throw new Error
.Failure(e
.getMessage());
236 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
237 throw new Error
.GroupNotFound(e
.getMessage());
242 public void sendEndSessionMessage(final List
<String
> recipients
) {
244 final var results
= m
.sendEndSessionMessage(getSingleRecipientIdentifiers(recipients
, m
.getUsername()));
245 checkSendMessageResults(results
.getTimestamp(), results
.getResults());
246 } catch (IOException e
) {
247 throw new Error
.Failure(e
.getMessage());
252 public long sendGroupMessage(final String message
, final List
<String
> attachments
, final byte[] groupId
) {
254 var results
= m
.sendMessage(new Message(message
, attachments
),
255 Set
.of(new RecipientIdentifier
.Group(getGroupId(groupId
))));
256 checkSendMessageResults(results
.getTimestamp(), results
.getResults());
257 return results
.getTimestamp();
258 } catch (IOException e
) {
259 throw new Error
.Failure(e
.getMessage());
260 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
261 throw new Error
.GroupNotFound(e
.getMessage());
262 } catch (AttachmentInvalidException e
) {
263 throw new Error
.AttachmentInvalid(e
.getMessage());
268 public long sendGroupMessageReaction(
270 final boolean remove
,
271 final String targetAuthor
,
272 final long targetSentTimestamp
,
276 final var results
= m
.sendMessageReaction(emoji
,
278 getSingleRecipientIdentifier(targetAuthor
, m
.getUsername()),
280 Set
.of(new RecipientIdentifier
.Group(getGroupId(groupId
))));
281 checkSendMessageResults(results
.getTimestamp(), results
.getResults());
282 return results
.getTimestamp();
283 } catch (IOException e
) {
284 throw new Error
.Failure(e
.getMessage());
285 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
286 throw new Error
.GroupNotFound(e
.getMessage());
290 // Since contact names might be empty if not defined, also potentially return
293 public String
getContactName(final String number
) {
294 return m
.getContactOrProfileName(getSingleRecipientIdentifier(number
, m
.getUsername()));
298 public void setContactName(final String number
, final String name
) {
300 m
.setContactName(getSingleRecipientIdentifier(number
, m
.getUsername()), name
);
301 } catch (NotMasterDeviceException e
) {
302 throw new Error
.Failure("This command doesn't work on linked devices.");
303 } catch (UnregisteredUserException e
) {
304 throw new Error
.Failure("Contact is not registered.");
309 public void setContactBlocked(final String number
, final boolean blocked
) {
311 m
.setContactBlocked(getSingleRecipientIdentifier(number
, m
.getUsername()), blocked
);
312 } catch (NotMasterDeviceException e
) {
313 throw new Error
.Failure("This command doesn't work on linked devices.");
314 } catch (IOException e
) {
315 throw new Error
.Failure(e
.getMessage());
320 public void setGroupBlocked(final byte[] groupId
, final boolean blocked
) {
322 m
.setGroupBlocked(getGroupId(groupId
), blocked
);
323 } catch (GroupNotFoundException e
) {
324 throw new Error
.GroupNotFound(e
.getMessage());
325 } catch (IOException e
) {
326 throw new Error
.Failure(e
.getMessage());
331 public List
<byte[]> getGroupIds() {
332 var groups
= m
.getGroups();
333 var ids
= new ArrayList
<byte[]>(groups
.size());
334 for (var group
: groups
) {
335 ids
.add(group
.getGroupId().serialize());
341 public String
getGroupName(final byte[] groupId
) {
342 var group
= m
.getGroup(getGroupId(groupId
));
346 return group
.getTitle();
351 public List
<String
> getGroupMembers(final byte[] groupId
) {
352 var group
= m
.getGroup(getGroupId(groupId
));
356 return group
.getMembers()
358 .map(m
::resolveSignalServiceAddress
)
359 .map(Util
::getLegacyIdentifier
)
360 .collect(Collectors
.toList());
365 public byte[] updateGroup(byte[] groupId
, String name
, List
<String
> members
, String avatar
) {
367 if (groupId
.length
== 0) {
370 if (name
.isEmpty()) {
373 if (avatar
.isEmpty()) {
376 final var memberIdentifiers
= getSingleRecipientIdentifiers(members
, m
.getUsername());
377 if (groupId
== null) {
378 final var results
= m
.createGroup(name
, memberIdentifiers
, avatar
== null ?
null : new File(avatar
));
379 checkSendMessageResults(results
.second().getTimestamp(), results
.second().getResults());
380 return results
.first().serialize();
382 final var results
= m
.updateGroup(getGroupId(groupId
),
393 avatar
== null ?
null : new File(avatar
),
396 if (results
!= null) {
397 checkSendMessageResults(results
.getTimestamp(), results
.getResults());
401 } catch (IOException e
) {
402 throw new Error
.Failure(e
.getMessage());
403 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
404 throw new Error
.GroupNotFound(e
.getMessage());
405 } catch (AttachmentInvalidException e
) {
406 throw new Error
.AttachmentInvalid(e
.getMessage());
411 public boolean isRegistered() {
416 public void updateProfile(
419 final String aboutEmoji
,
421 final boolean removeAvatar
424 if (avatarPath
.isEmpty()) {
427 Optional
<File
> avatarFile
= removeAvatar
429 : avatarPath
== null ?
null : Optional
.of(new File(avatarPath
));
430 m
.setProfile(name
, null, about
, aboutEmoji
, avatarFile
);
431 } catch (IOException e
) {
432 throw new Error
.Failure(e
.getMessage());
437 public void removePin() {
439 m
.setRegistrationLockPin(Optional
.absent());
440 } catch (UnauthenticatedResponseException e
) {
441 throw new Error
.Failure("Remove pin failed with unauthenticated response: " + e
.getMessage());
442 } catch (IOException e
) {
443 throw new Error
.Failure("Remove pin error: " + e
.getMessage());
448 public void setPin(String registrationLockPin
) {
450 m
.setRegistrationLockPin(Optional
.of(registrationLockPin
));
451 } catch (UnauthenticatedResponseException e
) {
452 throw new Error
.Failure("Set pin error failed with unauthenticated response: " + e
.getMessage());
453 } catch (IOException e
) {
454 throw new Error
.Failure("Set pin error: " + e
.getMessage());
458 // Provide option to query a version string in order to react on potential
459 // future interface changes
461 public String
version() {
462 return BaseConfig
.PROJECT_VERSION
;
465 // Create a unique list of Numbers from Identities and Contacts to really get
466 // all numbers the system knows
468 public List
<String
> listNumbers() {
469 return Stream
.concat(m
.getIdentities().stream().map(IdentityInfo
::getRecipientId
),
470 m
.getContacts().stream().map(Pair
::first
))
471 .map(m
::resolveSignalServiceAddress
)
472 .map(a
-> a
.getNumber().orNull())
473 .filter(Objects
::nonNull
)
475 .collect(Collectors
.toList());
479 public List
<String
> getContactNumber(final String name
) {
480 // Contact names have precedence.
481 var numbers
= new ArrayList
<String
>();
482 var contacts
= m
.getContacts();
483 for (var c
: contacts
) {
484 if (name
.equals(c
.second().getName())) {
485 numbers
.add(getLegacyIdentifier(m
.resolveSignalServiceAddress(c
.first())));
488 // Try profiles if no contact name was found
489 for (var identity
: m
.getIdentities()) {
490 final var recipientId
= identity
.getRecipientId();
491 final var address
= m
.resolveSignalServiceAddress(recipientId
);
492 var number
= address
.getNumber().orNull();
493 if (number
!= null) {
494 var profile
= m
.getRecipientProfile(recipientId
);
495 if (profile
!= null && profile
.getDisplayName().equals(name
)) {
504 public void quitGroup(final byte[] groupId
) {
505 var group
= getGroupId(groupId
);
507 m
.quitGroup(group
, Set
.of());
508 } catch (GroupNotFoundException
| NotAGroupMemberException e
) {
509 throw new Error
.GroupNotFound(e
.getMessage());
510 } catch (IOException
| LastGroupAdminException e
) {
511 throw new Error
.Failure(e
.getMessage());
516 public byte[] joinGroup(final String groupLink
) {
518 final var linkUrl
= GroupInviteLinkUrl
.fromUri(groupLink
);
519 if (linkUrl
== null) {
520 throw new Error
.Failure("Group link is invalid:");
522 final var result
= m
.joinGroup(linkUrl
);
523 return result
.first().serialize();
524 } catch (GroupInviteLinkUrl
.InvalidGroupLinkException
| GroupLinkNotActiveException e
) {
525 throw new Error
.Failure("Group link is invalid: " + e
.getMessage());
526 } catch (GroupInviteLinkUrl
.UnknownGroupLinkVersionException e
) {
527 throw new Error
.Failure("Group link was created with an incompatible version: " + e
.getMessage());
528 } catch (IOException e
) {
529 throw new Error
.Failure(e
.getMessage());
534 public boolean isContactBlocked(final String number
) {
535 return m
.isContactBlocked(getSingleRecipientIdentifier(number
, m
.getUsername()));
539 public boolean isGroupBlocked(final byte[] groupId
) {
540 var group
= m
.getGroup(getGroupId(groupId
));
544 return group
.isBlocked();
549 public boolean isMember(final byte[] groupId
) {
550 var group
= m
.getGroup(getGroupId(groupId
));
554 return group
.isMember(m
.getSelfRecipientId());
559 public String
uploadStickerPack(String stickerPackPath
) {
560 File path
= new File(stickerPackPath
);
562 return m
.uploadStickerPack(path
).toString();
563 } catch (IOException e
) {
564 throw new Error
.Failure("Upload error (maybe image size is too large):" + e
.getMessage());
565 } catch (StickerPackInvalidException e
) {
566 throw new Error
.Failure("Invalid sticker pack: " + e
.getMessage());
570 private static void checkSendMessageResult(long timestamp
, SendMessageResult result
) throws DBusExecutionException
{
571 var error
= ErrorUtils
.getErrorMessageFromSendMessageResult(result
);
577 final var message
= timestamp
+ "\nFailed to send message:\n" + error
+ '\n';
579 if (result
.getIdentityFailure() != null) {
580 throw new Error
.UntrustedIdentity(message
);
582 throw new Error
.Failure(message
);
586 private static void checkSendMessageResults(
587 long timestamp
, Map
<RecipientIdentifier
, List
<SendMessageResult
>> results
588 ) throws DBusExecutionException
{
589 final var sendMessageResults
= results
.values().stream().findFirst();
590 if (results
.size() == 1 && sendMessageResults
.get().size() == 1) {
591 checkSendMessageResult(timestamp
, sendMessageResults
.get().stream().findFirst().get());
595 var errors
= ErrorUtils
.getErrorMessagesFromSendMessageResults(results
);
596 if (errors
.size() == 0) {
600 var message
= new StringBuilder();
601 message
.append(timestamp
).append('\n');
602 message
.append("Failed to send (some) messages:\n");
603 for (var error
: errors
) {
604 message
.append(error
).append('\n');
607 throw new Error
.Failure(message
.toString());
610 private static void checkSendMessageResults(
611 long timestamp
, Collection
<SendMessageResult
> results
612 ) throws DBusExecutionException
{
613 if (results
.size() == 1) {
614 checkSendMessageResult(timestamp
, results
.stream().findFirst().get());
618 var errors
= ErrorUtils
.getErrorMessagesFromSendMessageResults(results
);
619 if (errors
.size() == 0) {
623 var message
= new StringBuilder();
624 message
.append(timestamp
).append('\n');
625 message
.append("Failed to send (some) messages:\n");
626 for (var error
: errors
) {
627 message
.append(error
).append('\n');
630 throw new Error
.Failure(message
.toString());
633 private static Set
<RecipientIdentifier
.Single
> getSingleRecipientIdentifiers(
634 final Collection
<String
> recipientStrings
, final String localNumber
635 ) throws DBusExecutionException
{
636 final var identifiers
= new HashSet
<RecipientIdentifier
.Single
>();
637 for (var recipientString
: recipientStrings
) {
638 identifiers
.add(getSingleRecipientIdentifier(recipientString
, localNumber
));
643 private static RecipientIdentifier
.Single
getSingleRecipientIdentifier(
644 final String recipientString
, final String localNumber
645 ) throws DBusExecutionException
{
647 return RecipientIdentifier
.Single
.fromString(recipientString
, localNumber
);
648 } catch (InvalidNumberException e
) {
649 throw new Error
.InvalidNumber(e
.getMessage());
653 private static GroupId
getGroupId(byte[] groupId
) throws DBusExecutionException
{
655 return GroupId
.unknownVersion(groupId
);
656 } catch (Throwable e
) {
657 throw new Error
.InvalidGroupId("Invalid group id: " + e
.getMessage());