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
.groups
.GroupId
;
8 import org
.asamk
.signal
.manager
.groups
.GroupInviteLinkUrl
;
9 import org
.asamk
.signal
.manager
.groups
.GroupNotFoundException
;
10 import org
.asamk
.signal
.manager
.groups
.NotAGroupMemberException
;
11 import org
.asamk
.signal
.util
.ErrorUtils
;
12 import org
.freedesktop
.dbus
.exceptions
.DBusExecutionException
;
13 import org
.whispersystems
.libsignal
.util
.guava
.Optional
;
14 import org
.whispersystems
.signalservice
.api
.groupsv2
.GroupLinkNotActiveException
;
15 import org
.whispersystems
.signalservice
.api
.messages
.SendMessageResult
;
16 import org
.whispersystems
.signalservice
.api
.push
.SignalServiceAddress
;
17 import org
.whispersystems
.signalservice
.api
.util
.InvalidNumberException
;
20 import java
.io
.IOException
;
21 import java
.util
.ArrayList
;
22 import java
.util
.List
;
23 import java
.util
.Objects
;
24 import java
.util
.stream
.Collectors
;
25 import java
.util
.stream
.Stream
;
27 public class DbusSignalImpl
implements Signal
{
29 private final Manager m
;
31 public DbusSignalImpl(final Manager m
) {
36 public boolean isRemote() {
41 public String
getObjectPath() {
46 public long sendMessage(final String message
, final List
<String
> attachments
, final String recipient
) {
47 var recipients
= new ArrayList
<String
>(1);
48 recipients
.add(recipient
);
49 return sendMessage(message
, attachments
, recipients
);
52 private static void checkSendMessageResult(long timestamp
, SendMessageResult result
) throws DBusExecutionException
{
53 var error
= ErrorUtils
.getErrorMessageFromSendMessageResult(result
);
59 final var message
= timestamp
+ "\nFailed to send message:\n" + error
+ '\n';
61 if (result
.getIdentityFailure() != null) {
62 throw new Error
.UntrustedIdentity(message
);
64 throw new Error
.Failure(message
);
68 private static void checkSendMessageResults(
69 long timestamp
, List
<SendMessageResult
> results
70 ) throws DBusExecutionException
{
71 if (results
.size() == 1) {
72 checkSendMessageResult(timestamp
, results
.get(0));
76 var errors
= ErrorUtils
.getErrorMessagesFromSendMessageResults(results
);
77 if (errors
.size() == 0) {
81 var message
= new StringBuilder();
82 message
.append(timestamp
).append('\n');
83 message
.append("Failed to send (some) messages:\n");
84 for (var error
: errors
) {
85 message
.append(error
).append('\n');
88 throw new Error
.Failure(message
.toString());
92 public long sendMessage(final String message
, final List
<String
> attachments
, final List
<String
> recipients
) {
94 final var results
= m
.sendMessage(message
, attachments
, recipients
);
95 checkSendMessageResults(results
.first(), results
.second());
96 return results
.first();
97 } catch (InvalidNumberException e
) {
98 throw new Error
.InvalidNumber(e
.getMessage());
99 } catch (AttachmentInvalidException e
) {
100 throw new Error
.AttachmentInvalid(e
.getMessage());
101 } catch (IOException e
) {
102 throw new Error
.Failure(e
.getMessage());
107 public long sendMessageReaction(
108 final String emoji
, final boolean remove
, final String targetAuthor
, final long targetSentTimestamp
, final String recipient
110 var recipients
= new ArrayList
<String
>(1);
111 recipients
.add(recipient
);
112 return sendMessageReaction(emoji
, remove
, targetAuthor
, targetSentTimestamp
, recipients
);
116 public long sendMessageReaction(
117 final String emoji
, final boolean remove
, final String targetAuthor
, final long targetSentTimestamp
, final List
<String
> recipients
120 final var results
= m
.sendMessageReaction(emoji
, remove
, targetAuthor
, targetSentTimestamp
, recipients
);
121 checkSendMessageResults(results
.first(), results
.second());
122 return results
.first();
123 } catch (InvalidNumberException e
) {
124 throw new Error
.InvalidNumber(e
.getMessage());
125 } catch (IOException e
) {
126 throw new Error
.Failure(e
.getMessage());
131 public long sendNoteToSelfMessage(
132 final String message
, final List
<String
> attachments
133 ) throws Error
.AttachmentInvalid
, Error
.Failure
, Error
.UntrustedIdentity
{
135 final var results
= m
.sendSelfMessage(message
, attachments
);
136 checkSendMessageResult(results
.first(), results
.second());
137 return results
.first();
138 } catch (AttachmentInvalidException e
) {
139 throw new Error
.AttachmentInvalid(e
.getMessage());
140 } catch (IOException e
) {
141 throw new Error
.Failure(e
.getMessage());
146 public void sendEndSessionMessage(final List
<String
> recipients
) {
148 final var results
= m
.sendEndSessionMessage(recipients
);
149 checkSendMessageResults(results
.first(), results
.second());
150 } catch (IOException e
) {
151 throw new Error
.Failure(e
.getMessage());
152 } catch (InvalidNumberException e
) {
153 throw new Error
.InvalidNumber(e
.getMessage());
158 public long sendGroupMessage(final String message
, final List
<String
> attachments
, final byte[] groupId
) {
160 var results
= m
.sendGroupMessage(message
, attachments
, GroupId
.unknownVersion(groupId
));
161 checkSendMessageResults(results
.first(), results
.second());
162 return results
.first();
163 } catch (IOException e
) {
164 throw new Error
.Failure(e
.getMessage());
165 } catch (GroupNotFoundException
| NotAGroupMemberException e
) {
166 throw new Error
.GroupNotFound(e
.getMessage());
167 } catch (AttachmentInvalidException e
) {
168 throw new Error
.AttachmentInvalid(e
.getMessage());
173 public long sendGroupMessageReaction(
174 final String emoji
, final boolean remove
, final String targetAuthor
, final long targetSentTimestamp
, final byte[] groupId
177 final var results
= m
.sendGroupMessageReaction(emoji
, remove
, targetAuthor
, targetSentTimestamp
, GroupId
.unknownVersion(groupId
));
178 checkSendMessageResults(results
.first(), results
.second());
179 return results
.first();
180 } catch (IOException e
) {
181 throw new Error
.Failure(e
.getMessage());
182 } catch (InvalidNumberException e
) {
183 throw new Error
.InvalidNumber(e
.getMessage());
184 } catch (GroupNotFoundException
| NotAGroupMemberException e
) {
185 throw new Error
.GroupNotFound(e
.getMessage());
189 // Since contact names might be empty if not defined, also potentially return
192 public String
getContactName(final String number
) {
194 return m
.getContactOrProfileName(number
);
195 } catch (Exception e
) {
196 throw new Error
.InvalidNumber(e
.getMessage());
201 public void setContactName(final String number
, final String name
) {
203 m
.setContactName(number
, name
);
204 } catch (InvalidNumberException e
) {
205 throw new Error
.InvalidNumber(e
.getMessage());
210 public void setContactBlocked(final String number
, final boolean blocked
) {
212 m
.setContactBlocked(number
, blocked
);
213 } catch (InvalidNumberException e
) {
214 throw new Error
.InvalidNumber(e
.getMessage());
219 public void setGroupBlocked(final byte[] groupId
, final boolean blocked
) {
221 m
.setGroupBlocked(GroupId
.unknownVersion(groupId
), blocked
);
222 } catch (GroupNotFoundException e
) {
223 throw new Error
.GroupNotFound(e
.getMessage());
228 public List
<byte[]> getGroupIds() {
229 var groups
= m
.getGroups();
230 var ids
= new ArrayList
<byte[]>(groups
.size());
231 for (var group
: groups
) {
232 ids
.add(group
.getGroupId().serialize());
238 public String
getGroupName(final byte[] groupId
) {
239 var group
= m
.getGroup(GroupId
.unknownVersion(groupId
));
243 return group
.getTitle();
248 public List
<String
> getGroupMembers(final byte[] groupId
) {
249 var group
= m
.getGroup(GroupId
.unknownVersion(groupId
));
253 return group
.getMembers()
255 .map(m
::resolveSignalServiceAddress
)
256 .map(SignalServiceAddress
::getLegacyIdentifier
)
257 .collect(Collectors
.toList());
262 public byte[] updateGroup(byte[] groupId
, String name
, List
<String
> members
, String avatar
) {
264 if (groupId
.length
== 0) {
267 if (name
.isEmpty()) {
270 if (members
.isEmpty()) {
273 if (avatar
.isEmpty()) {
276 final var results
= m
.updateGroup(groupId
== null ?
null : GroupId
.unknownVersion(groupId
),
279 avatar
== null ?
null : new File(avatar
));
280 checkSendMessageResults(0, results
.second());
281 return results
.first().serialize();
282 } catch (IOException e
) {
283 throw new Error
.Failure(e
.getMessage());
284 } catch (GroupNotFoundException
| NotAGroupMemberException e
) {
285 throw new Error
.GroupNotFound(e
.getMessage());
286 } catch (InvalidNumberException e
) {
287 throw new Error
.InvalidNumber(e
.getMessage());
288 } catch (AttachmentInvalidException e
) {
289 throw new Error
.AttachmentInvalid(e
.getMessage());
294 public boolean isRegistered() {
299 public void updateProfile(
302 final String aboutEmoji
,
304 final boolean removeAvatar
307 if (avatarPath
.isEmpty()) {
310 Optional
<File
> avatarFile
= removeAvatar
312 : avatarPath
== null ?
null : Optional
.of(new File(avatarPath
));
313 m
.setProfile(name
, about
, aboutEmoji
, avatarFile
);
314 } catch (IOException e
) {
315 throw new Error
.Failure(e
.getMessage());
319 // Provide option to query a version string in order to react on potential
320 // future interface changes
322 public String
version() {
323 return BaseConfig
.PROJECT_VERSION
;
326 // Create a unique list of Numbers from Identities and Contacts to really get
327 // all numbers the system knows
329 public List
<String
> listNumbers() {
330 return Stream
.concat(m
.getIdentities().stream().map(i
-> i
.getAddress().getNumber().orNull()),
331 m
.getContacts().stream().map(c
-> c
.number
))
332 .filter(Objects
::nonNull
)
334 .collect(Collectors
.toList());
338 public List
<String
> getContactNumber(final String name
) {
339 // Contact names have precedence.
340 var numbers
= new ArrayList
<String
>();
341 var contacts
= m
.getContacts();
342 for (var c
: contacts
) {
343 if (c
.name
!= null && c
.name
.equals(name
)) {
344 numbers
.add(c
.number
);
347 // Try profiles if no contact name was found
348 for (var identity
: m
.getIdentities()) {
349 final var address
= identity
.getAddress();
350 var number
= address
.getNumber().orNull();
351 if (number
!= null) {
352 var profile
= m
.getRecipientProfile(address
);
353 if (profile
!= null && profile
.getDisplayName().equals(name
)) {
362 public void quitGroup(final byte[] groupId
) {
363 var group
= GroupId
.unknownVersion(groupId
);
365 m
.sendQuitGroupMessage(group
);
366 } catch (GroupNotFoundException
| NotAGroupMemberException e
) {
367 throw new Error
.GroupNotFound(e
.getMessage());
368 } catch (IOException e
) {
369 throw new Error
.Failure(e
.getMessage());
374 public void joinGroup(final String groupLink
) {
376 final var linkUrl
= GroupInviteLinkUrl
.fromUri(groupLink
);
377 if (linkUrl
== null) {
378 throw new Error
.Failure("Group link is invalid:");
380 m
.joinGroup(linkUrl
);
381 } catch (GroupInviteLinkUrl
.InvalidGroupLinkException
| GroupLinkNotActiveException e
) {
382 throw new Error
.Failure("Group link is invalid: " + e
.getMessage());
383 } catch (GroupInviteLinkUrl
.UnknownGroupLinkVersionException e
) {
384 throw new Error
.Failure("Group link was created with an incompatible version: " + e
.getMessage());
385 } catch (IOException e
) {
386 throw new Error
.Failure(e
.getMessage());
391 public boolean isContactBlocked(final String number
) {
392 var contacts
= m
.getContacts();
393 for (var c
: contacts
) {
394 if (c
.number
.equals(number
)) {
402 public boolean isGroupBlocked(final byte[] groupId
) {
403 var group
= m
.getGroup(GroupId
.unknownVersion(groupId
));
407 return group
.isBlocked();
412 public boolean isMember(final byte[] groupId
) {
413 var group
= m
.getGroup(GroupId
.unknownVersion(groupId
));
417 return group
.isMember(m
.getSelfAddress());