1 package org
.asamk
.signal
.dbus
;
3 import org
.asamk
.Signal
;
4 import org
.asamk
.signal
.manager
.AttachmentInvalidException
;
5 import org
.asamk
.signal
.manager
.Manager
;
6 import org
.asamk
.signal
.manager
.groups
.GroupId
;
7 import org
.asamk
.signal
.manager
.groups
.GroupNotFoundException
;
8 import org
.asamk
.signal
.manager
.groups
.NotAGroupMemberException
;
9 import org
.asamk
.signal
.manager
.storage
.groups
.GroupInfo
;
10 import org
.asamk
.signal
.util
.ErrorUtils
;
11 import org
.freedesktop
.dbus
.exceptions
.DBusExecutionException
;
12 import org
.whispersystems
.libsignal
.util
.Pair
;
13 import org
.whispersystems
.libsignal
.util
.guava
.Optional
;
14 import org
.whispersystems
.signalservice
.api
.messages
.SendMessageResult
;
15 import org
.whispersystems
.signalservice
.api
.push
.SignalServiceAddress
;
16 import org
.whispersystems
.signalservice
.api
.util
.InvalidNumberException
;
19 import java
.io
.IOException
;
20 import java
.util
.ArrayList
;
21 import java
.util
.List
;
22 import java
.util
.stream
.Collectors
;
24 public class DbusSignalImpl
implements Signal
{
26 private final Manager m
;
28 public DbusSignalImpl(final Manager m
) {
33 public boolean isRemote() {
38 public String
getObjectPath() {
43 public long sendMessage(final String message
, final List
<String
> attachments
, final String recipient
) {
44 List
<String
> recipients
= new ArrayList
<>(1);
45 recipients
.add(recipient
);
46 return sendMessage(message
, attachments
, recipients
);
49 private static void checkSendMessageResult(long timestamp
, SendMessageResult result
) throws DBusExecutionException
{
50 String error
= ErrorUtils
.getErrorMessageFromSendMessageResult(result
);
56 final String message
= timestamp
+ "\nFailed to send message:\n" + error
+ '\n';
58 if (result
.getIdentityFailure() != null) {
59 throw new Error
.UntrustedIdentity(message
);
61 throw new Error
.Failure(message
);
65 private static void checkSendMessageResults(
66 long timestamp
, List
<SendMessageResult
> results
67 ) throws DBusExecutionException
{
68 if (results
.size() == 1) {
69 checkSendMessageResult(timestamp
, results
.get(0));
73 List
<String
> errors
= ErrorUtils
.getErrorMessagesFromSendMessageResults(results
);
74 if (errors
.size() == 0) {
78 StringBuilder message
= new StringBuilder();
79 message
.append(timestamp
).append('\n');
80 message
.append("Failed to send (some) messages:\n");
81 for (String error
: errors
) {
82 message
.append(error
).append('\n');
85 throw new Error
.Failure(message
.toString());
89 public long sendMessage(final String message
, final List
<String
> attachments
, final List
<String
> recipients
) {
91 final Pair
<Long
, List
<SendMessageResult
>> results
= m
.sendMessage(message
, attachments
, recipients
);
92 checkSendMessageResults(results
.first(), results
.second());
93 return results
.first();
94 } catch (InvalidNumberException e
) {
95 throw new Error
.InvalidNumber(e
.getMessage());
96 } catch (AttachmentInvalidException e
) {
97 throw new Error
.AttachmentInvalid(e
.getMessage());
98 } catch (IOException e
) {
99 throw new Error
.Failure(e
.getMessage());
104 public long sendNoteToSelfMessage(
105 final String message
, final List
<String
> attachments
106 ) throws Error
.AttachmentInvalid
, Error
.Failure
, Error
.UntrustedIdentity
{
108 final Pair
<Long
, SendMessageResult
> results
= m
.sendSelfMessage(message
, attachments
);
109 checkSendMessageResult(results
.first(), results
.second());
110 return results
.first();
111 } catch (AttachmentInvalidException e
) {
112 throw new Error
.AttachmentInvalid(e
.getMessage());
113 } catch (IOException e
) {
114 throw new Error
.Failure(e
.getMessage());
119 public void sendEndSessionMessage(final List
<String
> recipients
) {
121 final Pair
<Long
, List
<SendMessageResult
>> results
= m
.sendEndSessionMessage(recipients
);
122 checkSendMessageResults(results
.first(), results
.second());
123 } catch (IOException e
) {
124 throw new Error
.Failure(e
.getMessage());
125 } catch (InvalidNumberException e
) {
126 throw new Error
.InvalidNumber(e
.getMessage());
131 public long sendGroupMessage(final String message
, final List
<String
> attachments
, final byte[] groupId
) {
133 Pair
<Long
, List
<SendMessageResult
>> results
= m
.sendGroupMessage(message
,
135 GroupId
.unknownVersion(groupId
));
136 checkSendMessageResults(results
.first(), results
.second());
137 return results
.first();
138 } catch (IOException e
) {
139 throw new Error
.Failure(e
.getMessage());
140 } catch (GroupNotFoundException
| NotAGroupMemberException e
) {
141 throw new Error
.GroupNotFound(e
.getMessage());
142 } catch (AttachmentInvalidException e
) {
143 throw new Error
.AttachmentInvalid(e
.getMessage());
148 public String
getContactName(final String number
) {
150 return m
.getContactName(number
);
151 } catch (InvalidNumberException e
) {
152 throw new Error
.InvalidNumber(e
.getMessage());
157 public void setContactName(final String number
, final String name
) {
159 m
.setContactName(number
, name
);
160 } catch (InvalidNumberException e
) {
161 throw new Error
.InvalidNumber(e
.getMessage());
166 public void setContactBlocked(final String number
, final boolean blocked
) {
168 m
.setContactBlocked(number
, blocked
);
169 } catch (InvalidNumberException e
) {
170 throw new Error
.InvalidNumber(e
.getMessage());
175 public void setGroupBlocked(final byte[] groupId
, final boolean blocked
) {
177 m
.setGroupBlocked(GroupId
.unknownVersion(groupId
), blocked
);
178 } catch (GroupNotFoundException e
) {
179 throw new Error
.GroupNotFound(e
.getMessage());
184 public List
<byte[]> getGroupIds() {
185 List
<GroupInfo
> groups
= m
.getGroups();
186 List
<byte[]> ids
= new ArrayList
<>(groups
.size());
187 for (GroupInfo group
: groups
) {
188 ids
.add(group
.getGroupId().serialize());
194 public String
getGroupName(final byte[] groupId
) {
195 GroupInfo group
= m
.getGroup(GroupId
.unknownVersion(groupId
));
199 return group
.getTitle();
204 public List
<String
> getGroupMembers(final byte[] groupId
) {
205 GroupInfo group
= m
.getGroup(GroupId
.unknownVersion(groupId
));
209 return group
.getMembers()
211 .map(m
::resolveSignalServiceAddress
)
212 .map(SignalServiceAddress
::getLegacyIdentifier
)
213 .collect(Collectors
.toList());
218 public byte[] updateGroup(byte[] groupId
, String name
, List
<String
> members
, String avatar
) {
220 if (groupId
.length
== 0) {
223 if (name
.isEmpty()) {
226 if (members
.isEmpty()) {
229 if (avatar
.isEmpty()) {
232 final Pair
<GroupId
, List
<SendMessageResult
>> results
= m
.updateGroup(groupId
== null
234 : GroupId
.unknownVersion(groupId
), name
, members
, avatar
== null ?
null : new File(avatar
));
235 checkSendMessageResults(0, results
.second());
236 return results
.first().serialize();
237 } catch (IOException e
) {
238 throw new Error
.Failure(e
.getMessage());
239 } catch (GroupNotFoundException
| NotAGroupMemberException e
) {
240 throw new Error
.GroupNotFound(e
.getMessage());
241 } catch (InvalidNumberException e
) {
242 throw new Error
.InvalidNumber(e
.getMessage());
243 } catch (AttachmentInvalidException e
) {
244 throw new Error
.AttachmentInvalid(e
.getMessage());
249 public boolean isRegistered() {
254 public void updateProfile(
257 final String aboutEmoji
,
259 final boolean removeAvatar
262 if (avatarPath
.isEmpty()) {
265 Optional
<File
> avatarFile
= removeAvatar
267 : avatarPath
== null ?
null : Optional
.of(new File(avatarPath
));
268 m
.setProfile(name
, about
, aboutEmoji
, avatarFile
);
269 } catch (IOException e
) {
270 throw new Error
.Failure(e
.getMessage());