import org.asamk.signal.storage.protocol.JsonSignalProtocolStore;
import org.asamk.signal.storage.threads.JsonThreadStore;
import org.asamk.signal.storage.threads.ThreadInfo;
+import org.asamk.signal.util.IOUtils;
import org.asamk.signal.util.KeyUtils;
import org.asamk.signal.util.Util;
import org.signal.libsignal.metadata.*;
import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;
-import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.file.Files;
-import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
-import java.nio.file.attribute.PosixFilePermission;
-import java.nio.file.attribute.PosixFilePermissions;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
-import static java.nio.file.attribute.PosixFilePermission.*;
-
class Manager implements Signal {
private final static String URL = "https://textsecure-service.whispersystems.org";
private final static String CDN_URL = "https://cdn.signal.org";
private File getMessageCacheFile(String sender, long now, long timestamp) throws IOException {
String cachePath = getMessageCachePath(sender);
- createPrivateDirectories(cachePath);
+ IOUtils.createPrivateDirectories(cachePath);
return new File(cachePath + "/" + now + "_" + timestamp);
}
- private static void createPrivateDirectories(String path) throws IOException {
- final Path file = new File(path).toPath();
- try {
- Set<PosixFilePermission> perms = EnumSet.of(OWNER_READ, OWNER_WRITE, OWNER_EXECUTE);
- Files.createDirectories(file, PosixFilePermissions.asFileAttribute(perms));
- } catch (UnsupportedOperationException e) {
- Files.createDirectories(file);
- }
- }
-
- private static void createPrivateFile(String path) throws IOException {
- final Path file = new File(path).toPath();
- try {
- Set<PosixFilePermission> perms = EnumSet.of(OWNER_READ, OWNER_WRITE);
- Files.createFile(file, PosixFilePermissions.asFileAttribute(perms));
- } catch (UnsupportedOperationException e) {
- Files.createFile(file);
- }
- }
-
public boolean userExists() {
if (username == null) {
return false;
if (fileChannel != null)
return;
- createPrivateDirectories(dataPath);
+ IOUtils.createPrivateDirectories(dataPath);
if (!new File(getFileName()).exists()) {
- createPrivateFile(getFileName());
+ IOUtils.createPrivateFile(getFileName());
}
fileChannel = new RandomAccessFile(new File(getFileName()), "rw").getChannel();
lock = fileChannel.tryLock();
File attachmentFile = getAttachmentFile(g.getAvatarId());
if (!avatarFile.exists() && attachmentFile.exists()) {
try {
- createPrivateDirectories(avatarsPath);
+ IOUtils.createPrivateDirectories(avatarsPath);
Files.copy(attachmentFile.toPath(), avatarFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
} catch (Exception e) {
// Ignore
accountManager.removeDevice(deviceId);
}
- public static Map<String, String> getQueryMap(String query) {
- String[] params = query.split("&");
- Map<String, String> map = new HashMap<>();
- for (String param : params) {
- String name = null;
- final String[] paramParts = param.split("=");
- try {
- name = URLDecoder.decode(paramParts[0], "utf-8");
- } catch (UnsupportedEncodingException e) {
- // Impossible
- }
- String value = null;
- try {
- value = URLDecoder.decode(paramParts[1], "utf-8");
- } catch (UnsupportedEncodingException e) {
- // Impossible
- }
- map.put(name, value);
- }
- return map;
- }
-
public void addDeviceLink(URI linkUri) throws IOException, InvalidKeyException {
- Map<String, String> query = getQueryMap(linkUri.getRawQuery());
+ Map<String, String> query = Util.getQueryMap(linkUri.getRawQuery());
String deviceIdentifier = query.get("uuid");
String publicKeyEncoded = query.get("pub_key");
sendMessageLegacy(messageBuilder, g.members);
}
- private static String join(CharSequence separator, Iterable<? extends CharSequence> list) {
- StringBuilder buf = new StringBuilder();
- for (CharSequence str : list) {
- if (buf.length() > 0) {
- buf.append(separator);
- }
- buf.append(str);
- }
-
- return buf.toString();
- }
-
public byte[] sendUpdateGroupMessage(byte[] groupId, String name, Collection<String> members, String avatarFile) throws IOException, EncapsulatedExceptions, GroupNotFoundException, AttachmentInvalidException {
GroupInfo g;
if (groupId == null) {
for (ContactTokenDetails contact : contacts) {
newMembers.remove(contact.getNumber());
}
- System.err.println("Failed to add members " + join(", ", newMembers) + " to group: Not registered on Signal");
+ System.err.println("Failed to add members " + Util.join(", ", newMembers) + " to group: Not registered on Signal");
System.err.println("Aborting…");
System.exit(1);
}
}
if (avatarFile != null) {
- createPrivateDirectories(avatarsPath);
+ IOUtils.createPrivateDirectories(avatarsPath);
File aFile = getGroupAvatarFile(g.groupId);
Files.copy(Paths.get(avatarFile), aFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
}
return UnidentifiedAccess.deriveAccessKeyFrom(profileKey);
}
- private static byte[] getTargetUnidentifiedAccessKey(SignalServiceAddress recipient) {
+ private byte[] getTargetUnidentifiedAccessKey(SignalServiceAddress recipient) {
// TODO implement
return null;
}
if (syncMessage.getGroups().isPresent()) {
File tmpFile = null;
try {
- tmpFile = Util.createTempFile();
+ tmpFile = IOUtils.createTempFile();
try (InputStream attachmentAsStream = retrieveAttachmentAsStream(syncMessage.getGroups().get().asPointer(), tmpFile)) {
DeviceGroupsInputStream s = new DeviceGroupsInputStream(attachmentAsStream);
DeviceGroup g;
if (syncMessage.getContacts().isPresent()) {
File tmpFile = null;
try {
- tmpFile = Util.createTempFile();
+ tmpFile = IOUtils.createTempFile();
final ContactsMessage contactsMessage = syncMessage.getContacts().get();
try (InputStream attachmentAsStream = retrieveAttachmentAsStream(contactsMessage.getContactsStream().asPointer(), tmpFile)) {
DeviceContactsInputStream s = new DeviceContactsInputStream(attachmentAsStream);
}
private File retrieveContactAvatarAttachment(SignalServiceAttachment attachment, String number) throws IOException, InvalidMessageException {
- createPrivateDirectories(avatarsPath);
+ IOUtils.createPrivateDirectories(avatarsPath);
if (attachment.isPointer()) {
SignalServiceAttachmentPointer pointer = attachment.asPointer();
return retrieveAttachment(pointer, getContactAvatarFile(number), false);
}
private File retrieveGroupAvatarAttachment(SignalServiceAttachment attachment, byte[] groupId) throws IOException, InvalidMessageException {
- createPrivateDirectories(avatarsPath);
+ IOUtils.createPrivateDirectories(avatarsPath);
if (attachment.isPointer()) {
SignalServiceAttachmentPointer pointer = attachment.asPointer();
return retrieveAttachment(pointer, getGroupAvatarFile(groupId), false);
}
private File retrieveAttachment(SignalServiceAttachmentPointer pointer) throws IOException, InvalidMessageException {
- createPrivateDirectories(attachmentsPath);
+ IOUtils.createPrivateDirectories(attachmentsPath);
return retrieveAttachment(pointer, getAttachmentFile(pointer.getId()), true);
}
final SignalServiceMessageReceiver messageReceiver = new SignalServiceMessageReceiver(serviceConfiguration, username, password, deviceId, signalingKey, USER_AGENT, null, timer);
- File tmpFile = Util.createTempFile();
+ File tmpFile = IOUtils.createTempFile();
try (InputStream input = messageReceiver.retrieveAttachment(pointer, tmpFile, MAX_ATTACHMENT_SIZE)) {
try (OutputStream output = new FileOutputStream(outputFile)) {
byte[] buffer = new byte[4096];
}
private void sendGroups() throws IOException, UntrustedIdentityException {
- File groupsFile = Util.createTempFile();
+ File groupsFile = IOUtils.createTempFile();
try {
try (OutputStream fos = new FileOutputStream(groupsFile)) {
}
private void sendContacts() throws IOException, UntrustedIdentityException {
- File contactsFile = Util.createTempFile();
+ File contactsFile = IOUtils.createTempFile();
try {
try (OutputStream fos = new FileOutputStream(contactsFile)) {