From 646f617bc195f592617824927f4cafece86f6f4a Mon Sep 17 00:00:00 2001 From: vanitasvitae Date: Sun, 20 Aug 2017 13:45:57 +0200 Subject: [PATCH] Only accept modified files, Initially send files --- .../de/vanitasvitae/sync_client/Client.java | 12 ++--- .../de/vanitasvitae/sync_client/Master.java | 48 +++++++++++++------ .../de/vanitasvitae/sync_client/Slave.java | 6 +++ 3 files changed, 46 insertions(+), 20 deletions(-) diff --git a/src/main/java/de/vanitasvitae/sync_client/Client.java b/src/main/java/de/vanitasvitae/sync_client/Client.java index 9c7d3a3..3e45e2d 100644 --- a/src/main/java/de/vanitasvitae/sync_client/Client.java +++ b/src/main/java/de/vanitasvitae/sync_client/Client.java @@ -36,9 +36,9 @@ import org.jxmpp.stringprep.XmppStringprepException; public class Client { - protected final XMPPConnection connection; - protected final Path root; - protected final Set remotes = new HashSet<>(); + final XMPPConnection connection; + final Path root; + final Set remotes = new HashSet<>(); public Client(String username, String password, String directory) throws XmppStringprepException, CorruptedOmemoKeyException, NoSuchAlgorithmException, UnsupportedEncodingException, @@ -70,13 +70,13 @@ public class Client { OmemoConfiguration.setFileBasedOmemoStoreDefaultPath(storePath); } - public void login() throws InterruptedException, IOException, SmackException, XMPPException, CorruptedOmemoKeyException { + void login() throws InterruptedException, IOException, SmackException, XMPPException, CorruptedOmemoKeyException { ((XMPPTCPConnection) connection).connect().login(); connection.setReplyTimeout(30000); OmemoManager omemoManager = OmemoManager.getInstanceFor(connection); omemoManager.initialize(); - JetManager.getInstanceFor(connection).registerEncryptionMethod(OmemoManager.getInstanceFor(connection)); - JetManager.registerEncryptionMethodProvider(OmemoConstants.OMEMO_NAMESPACE_V_AXOLOTL, new OmemoVAxolotlProvider()); + JetManager.getInstanceFor(connection).registerEnvelopeManager(OmemoManager.getInstanceFor(connection)); + JetManager.registerEnvelopeProvider(OmemoConstants.OMEMO_NAMESPACE_V_AXOLOTL, new OmemoVAxolotlProvider()); } public void addRemote(FullJid remote) { diff --git a/src/main/java/de/vanitasvitae/sync_client/Master.java b/src/main/java/de/vanitasvitae/sync_client/Master.java index 2ae231d..5fdada6 100644 --- a/src/main/java/de/vanitasvitae/sync_client/Master.java +++ b/src/main/java/de/vanitasvitae/sync_client/Master.java @@ -12,6 +12,7 @@ import java.nio.file.FileSystems; import java.nio.file.FileVisitResult; import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.Paths; import java.nio.file.SimpleFileVisitor; import java.nio.file.WatchEvent; import java.nio.file.WatchKey; @@ -55,13 +56,6 @@ public class Master extends Client { registerFileWatcher(); jftm = JingleFileTransferManager.getInstanceFor(connection); jetm = JetManager.getInstanceFor(connection); - jftm.addIncomingFileOfferListener(offer -> { - try { - offer.accept(connection, new File(root.toFile(), offer.getFile().getName())); - } catch (InterruptedException | XMPPException.XMPPErrorException | SmackException.NoResponseException | SmackException.NotConnectedException | IOException e) { - e.printStackTrace(); - } - }); } private void registerFileWatcher() throws IOException { @@ -97,6 +91,14 @@ public class Master extends Client { }); } + private void initSendFiles(Path root, FullJid fullJid) throws IOException { + Files.walk(root) + .filter(Files::isRegularFile) + .forEach(s -> { + sendFile(s.toFile(), fullJid); + }); + } + void processEvents() throws InterruptedException, SmackException.FeatureNotSupportedException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, SmackException.NoResponseException { for (;;) { @@ -180,7 +182,17 @@ public class Master extends Client { } } + public void sendFile(Path path) throws InterruptedException, SmackException.FeatureNotSupportedException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, SmackException.NoResponseException { + sendFile(path.toFile()); + } + public void sendFile(File file) throws InterruptedException, SmackException.FeatureNotSupportedException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, SmackException.NoResponseException { + for (FullJid fullJid : remotes) { + sendFile(file, fullJid); + } + } + + public void sendFile(File file, FullJid remote) { String fileName = file.getAbsolutePath(); String rootPath = root.toAbsolutePath().toString(); if (fileName.startsWith(rootPath)) { @@ -188,13 +200,21 @@ public class Master extends Client { } else { throw new AssertionError("Illegal path! " + fileName); } - for (FullJid recipient : remotes) { - try { - jetm.sendEncryptedFile(file, fileName, recipient, OmemoManager.getInstanceFor(connection)); - } catch (Exception e) { - System.out.println("Could not send file: " + e); - } - //jftm.sendFile(file, fileName, recipient); + + try { + jetm.sendEncryptedFile(file, fileName, remote, OmemoManager.getInstanceFor(connection)); + } catch (Exception e) { + System.out.println("Could not send file: " + e); + } + } + + @Override + public void addRemote(FullJid remote) { + super.addRemote(remote); + try { + initSendFiles(root, remote); + } catch (IOException e) { + e.printStackTrace(); } } } diff --git a/src/main/java/de/vanitasvitae/sync_client/Slave.java b/src/main/java/de/vanitasvitae/sync_client/Slave.java index b940553..e826a8d 100644 --- a/src/main/java/de/vanitasvitae/sync_client/Slave.java +++ b/src/main/java/de/vanitasvitae/sync_client/Slave.java @@ -7,6 +7,7 @@ import java.security.InvalidAlgorithmParameterException; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.security.NoSuchProviderException; +import java.util.logging.Logger; import javax.crypto.BadPaddingException; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; @@ -30,6 +31,11 @@ public class Slave extends Client { jingleFileTransferManager.addIncomingFileOfferListener(offer -> { try { File target = new File(root.toFile(), offer.getFile().getName()); + if (target.exists() && target.length() == offer.getFile().getSize()) { + System.out.println("We already seem to have file " + target.getAbsolutePath()+ ". Cancel."); + offer.cancel(connection); + return; + } target.getParentFile().mkdirs(); offer.accept(connection, target); } catch (InterruptedException | XMPPException.XMPPErrorException | SmackException.NoResponseException | SmackException.NotConnectedException | IOException e) {