Only accept modified files, Initially send files
This commit is contained in:
parent
dd22d3a183
commit
646f617bc1
3 changed files with 46 additions and 20 deletions
|
@ -36,9 +36,9 @@ import org.jxmpp.stringprep.XmppStringprepException;
|
|||
|
||||
public class Client {
|
||||
|
||||
protected final XMPPConnection connection;
|
||||
protected final Path root;
|
||||
protected final Set<FullJid> remotes = new HashSet<>();
|
||||
final XMPPConnection connection;
|
||||
final Path root;
|
||||
final Set<FullJid> 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) {
|
||||
|
|
|
@ -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));
|
||||
jetm.sendEncryptedFile(file, fileName, remote, OmemoManager.getInstanceFor(connection));
|
||||
} catch (Exception 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in a new issue