Only accept modified files, Initially send files

This commit is contained in:
vanitasvitae 2017-08-20 13:45:57 +02:00
parent dd22d3a183
commit 646f617bc1
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
3 changed files with 46 additions and 20 deletions

View file

@ -36,9 +36,9 @@ import org.jxmpp.stringprep.XmppStringprepException;
public class Client { public class Client {
protected final XMPPConnection connection; final XMPPConnection connection;
protected final Path root; final Path root;
protected final Set<FullJid> remotes = new HashSet<>(); final Set<FullJid> remotes = new HashSet<>();
public Client(String username, String password, String directory) public Client(String username, String password, String directory)
throws XmppStringprepException, CorruptedOmemoKeyException, NoSuchAlgorithmException, UnsupportedEncodingException, throws XmppStringprepException, CorruptedOmemoKeyException, NoSuchAlgorithmException, UnsupportedEncodingException,
@ -70,13 +70,13 @@ public class Client {
OmemoConfiguration.setFileBasedOmemoStoreDefaultPath(storePath); OmemoConfiguration.setFileBasedOmemoStoreDefaultPath(storePath);
} }
public void login() throws InterruptedException, IOException, SmackException, XMPPException, CorruptedOmemoKeyException { void login() throws InterruptedException, IOException, SmackException, XMPPException, CorruptedOmemoKeyException {
((XMPPTCPConnection) connection).connect().login(); ((XMPPTCPConnection) connection).connect().login();
connection.setReplyTimeout(30000); connection.setReplyTimeout(30000);
OmemoManager omemoManager = OmemoManager.getInstanceFor(connection); OmemoManager omemoManager = OmemoManager.getInstanceFor(connection);
omemoManager.initialize(); omemoManager.initialize();
JetManager.getInstanceFor(connection).registerEncryptionMethod(OmemoManager.getInstanceFor(connection)); JetManager.getInstanceFor(connection).registerEnvelopeManager(OmemoManager.getInstanceFor(connection));
JetManager.registerEncryptionMethodProvider(OmemoConstants.OMEMO_NAMESPACE_V_AXOLOTL, new OmemoVAxolotlProvider()); JetManager.registerEnvelopeProvider(OmemoConstants.OMEMO_NAMESPACE_V_AXOLOTL, new OmemoVAxolotlProvider());
} }
public void addRemote(FullJid remote) { public void addRemote(FullJid remote) {

View file

@ -12,6 +12,7 @@ import java.nio.file.FileSystems;
import java.nio.file.FileVisitResult; import java.nio.file.FileVisitResult;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor; import java.nio.file.SimpleFileVisitor;
import java.nio.file.WatchEvent; import java.nio.file.WatchEvent;
import java.nio.file.WatchKey; import java.nio.file.WatchKey;
@ -55,13 +56,6 @@ public class Master extends Client {
registerFileWatcher(); registerFileWatcher();
jftm = JingleFileTransferManager.getInstanceFor(connection); jftm = JingleFileTransferManager.getInstanceFor(connection);
jetm = JetManager.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 { 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 { void processEvents() throws InterruptedException, SmackException.FeatureNotSupportedException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, SmackException.NoResponseException {
for (;;) { 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 { 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 fileName = file.getAbsolutePath();
String rootPath = root.toAbsolutePath().toString(); String rootPath = root.toAbsolutePath().toString();
if (fileName.startsWith(rootPath)) { if (fileName.startsWith(rootPath)) {
@ -188,13 +200,21 @@ public class Master extends Client {
} else { } else {
throw new AssertionError("Illegal path! " + fileName); throw new AssertionError("Illegal path! " + fileName);
} }
for (FullJid recipient : remotes) {
try { try {
jetm.sendEncryptedFile(file, fileName, recipient, OmemoManager.getInstanceFor(connection)); jetm.sendEncryptedFile(file, fileName, remote, OmemoManager.getInstanceFor(connection));
} catch (Exception e) { } catch (Exception e) {
System.out.println("Could not send file: " + e); System.out.println("Could not send file: " + e);
} }
//jftm.sendFile(file, fileName, recipient); }
@Override
public void addRemote(FullJid remote) {
super.addRemote(remote);
try {
initSendFiles(root, remote);
} catch (IOException e) {
e.printStackTrace();
} }
} }
} }

View file

@ -7,6 +7,7 @@ import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException; import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException; import java.security.NoSuchProviderException;
import java.util.logging.Logger;
import javax.crypto.BadPaddingException; import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException; import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException; import javax.crypto.NoSuchPaddingException;
@ -30,6 +31,11 @@ public class Slave extends Client {
jingleFileTransferManager.addIncomingFileOfferListener(offer -> { jingleFileTransferManager.addIncomingFileOfferListener(offer -> {
try { try {
File target = new File(root.toFile(), offer.getFile().getName()); 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(); target.getParentFile().mkdirs();
offer.accept(connection, target); offer.accept(connection, target);
} catch (InterruptedException | XMPPException.XMPPErrorException | SmackException.NoResponseException | SmackException.NotConnectedException | IOException e) { } catch (InterruptedException | XMPPException.XMPPErrorException | SmackException.NoResponseException | SmackException.NotConnectedException | IOException e) {