X-Git-Url: https://git.nmode.ca/signal-cli/blobdiff_plain/4377a2179b38360b00efc1c1db00a62f3f020db1..a1f0d74a99e4dc5c2e7a7ee79f59684314da4c4f:/src/main/java/org/asamk/signal/Manager.java diff --git a/src/main/java/org/asamk/signal/Manager.java b/src/main/java/org/asamk/signal/Manager.java index c9252f83..dc979930 100644 --- a/src/main/java/org/asamk/signal/Manager.java +++ b/src/main/java/org/asamk/signal/Manager.java @@ -35,7 +35,6 @@ import org.asamk.signal.storage.protocol.JsonIdentityKeyStore; 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.Base64; import org.asamk.signal.util.Util; import org.whispersystems.libsignal.*; import org.whispersystems.libsignal.ecc.Curve; @@ -64,6 +63,7 @@ import org.whispersystems.signalservice.api.util.InvalidNumberException; import org.whispersystems.signalservice.api.util.PhoneNumberFormatter; import org.whispersystems.signalservice.internal.push.SignalServiceProtos; import org.whispersystems.signalservice.internal.push.SignalServiceUrl; +import org.whispersystems.signalservice.internal.util.Base64; import java.io.*; import java.net.URI; @@ -807,9 +807,9 @@ class Manager implements Signal { if (contact == null) { contact = new ContactInfo(); contact.number = number; - System.out.println("Add contact " + number + " named " + name); + System.err.println("Add contact " + number + " named " + name); } else { - System.out.println("Updating contact " + number + " name " + contact.name + " -> " + name); + System.err.println("Updating contact " + number + " name " + contact.name + " -> " + name); } contact.name = name; contactStore.updateContact(contact); @@ -1113,9 +1113,11 @@ class Manager implements Signal { try { Files.delete(fileEntry.toPath()); } catch (IOException e) { - System.out.println("Failed to delete cached message file “" + fileEntry + "”: " + e.getMessage()); + System.err.println("Failed to delete cached message file “" + fileEntry + "”: " + e.getMessage()); } } + // Try to delete directory if empty + dir.delete(); } } @@ -1169,8 +1171,10 @@ class Manager implements Signal { try { cacheFile = getMessageCacheFile(envelope.getSource(), now, envelope.getTimestamp()); Files.delete(cacheFile.toPath()); + // Try to delete directory if empty + new File(getMessageCachePath()).delete(); } catch (IOException e) { - System.out.println("Failed to delete cached message file “" + cacheFile + "”: " + e.getMessage()); + System.err.println("Failed to delete cached message file “" + cacheFile + "”: " + e.getMessage()); } } } @@ -1216,23 +1220,25 @@ class Manager implements Signal { File tmpFile = null; try { tmpFile = Util.createTempFile(); - DeviceGroupsInputStream s = new DeviceGroupsInputStream(retrieveAttachmentAsStream(syncMessage.getGroups().get().asPointer(), tmpFile)); - DeviceGroup g; - while ((g = s.read()) != null) { - GroupInfo syncGroup = groupStore.getGroup(g.getId()); - if (syncGroup == null) { - syncGroup = new GroupInfo(g.getId()); + try (InputStream attachmentAsStream = retrieveAttachmentAsStream(syncMessage.getGroups().get().asPointer(), tmpFile)) { + DeviceGroupsInputStream s = new DeviceGroupsInputStream(attachmentAsStream); + DeviceGroup g; + while ((g = s.read()) != null) { + GroupInfo syncGroup = groupStore.getGroup(g.getId()); + if (syncGroup == null) { + syncGroup = new GroupInfo(g.getId()); + } + if (g.getName().isPresent()) { + syncGroup.name = g.getName().get(); + } + syncGroup.members.addAll(g.getMembers()); + syncGroup.active = g.isActive(); + + if (g.getAvatar().isPresent()) { + retrieveGroupAvatarAttachment(g.getAvatar().get(), syncGroup.groupId); + } + groupStore.updateGroup(syncGroup); } - if (g.getName().isPresent()) { - syncGroup.name = g.getName().get(); - } - syncGroup.members.addAll(g.getMembers()); - syncGroup.active = g.isActive(); - - if (g.getAvatar().isPresent()) { - retrieveGroupAvatarAttachment(g.getAvatar().get(), syncGroup.groupId); - } - groupStore.updateGroup(syncGroup); } } catch (Exception e) { e.printStackTrace(); @@ -1241,7 +1247,7 @@ class Manager implements Signal { try { Files.delete(tmpFile.toPath()); } catch (IOException e) { - System.out.println("Failed to delete temp file “" + tmpFile + "”: " + e.getMessage()); + System.err.println("Failed to delete received groups temp file “" + tmpFile + "”: " + e.getMessage()); } } } @@ -1254,27 +1260,29 @@ class Manager implements Signal { try { tmpFile = Util.createTempFile(); final ContactsMessage contactsMessage = syncMessage.getContacts().get(); - DeviceContactsInputStream s = new DeviceContactsInputStream(retrieveAttachmentAsStream(contactsMessage.getContactsStream().asPointer(), tmpFile)); - if (contactsMessage.isComplete()) { - contactStore.clear(); - } - DeviceContact c; - while ((c = s.read()) != null) { - ContactInfo contact = contactStore.getContact(c.getNumber()); - if (contact == null) { - contact = new ContactInfo(); - contact.number = c.getNumber(); + try (InputStream attachmentAsStream = retrieveAttachmentAsStream(contactsMessage.getContactsStream().asPointer(), tmpFile)) { + DeviceContactsInputStream s = new DeviceContactsInputStream(attachmentAsStream); + if (contactsMessage.isComplete()) { + contactStore.clear(); } - if (c.getName().isPresent()) { - contact.name = c.getName().get(); - } - if (c.getColor().isPresent()) { - contact.color = c.getColor().get(); - } - contactStore.updateContact(contact); - - if (c.getAvatar().isPresent()) { - retrieveContactAvatarAttachment(c.getAvatar().get(), contact.number); + DeviceContact c; + while ((c = s.read()) != null) { + ContactInfo contact = contactStore.getContact(c.getNumber()); + if (contact == null) { + contact = new ContactInfo(); + contact.number = c.getNumber(); + } + if (c.getName().isPresent()) { + contact.name = c.getName().get(); + } + if (c.getColor().isPresent()) { + contact.color = c.getColor().get(); + } + contactStore.updateContact(contact); + + if (c.getAvatar().isPresent()) { + retrieveContactAvatarAttachment(c.getAvatar().get(), contact.number); + } } } } catch (Exception e) { @@ -1284,7 +1292,7 @@ class Manager implements Signal { try { Files.delete(tmpFile.toPath()); } catch (IOException e) { - System.out.println("Failed to delete temp file “" + tmpFile + "”: " + e.getMessage()); + System.err.println("Failed to delete received contacts temp file “" + tmpFile + "”: " + e.getMessage()); } } } @@ -1439,7 +1447,7 @@ class Manager implements Signal { try { Files.delete(tmpFile.toPath()); } catch (IOException e) { - System.out.println("Failed to delete temp file “" + tmpFile + "”: " + e.getMessage()); + System.err.println("Failed to delete received attachment temp file “" + tmpFile + "”: " + e.getMessage()); } } return outputFile; @@ -1493,7 +1501,7 @@ class Manager implements Signal { try { Files.delete(groupsFile.toPath()); } catch (IOException e) { - System.out.println("Failed to delete temp file “" + groupsFile + "”: " + e.getMessage()); + System.err.println("Failed to delete groups temp file “" + groupsFile + "”: " + e.getMessage()); } } } @@ -1525,7 +1533,7 @@ class Manager implements Signal { try { Files.delete(contactsFile.toPath()); } catch (IOException e) { - System.out.println("Failed to delete temp file “" + contactsFile + "”: " + e.getMessage()); + System.err.println("Failed to delete contacts temp file “" + contactsFile + "”: " + e.getMessage()); } } }