Implement 'msg' command

This commit is contained in:
Tobias Eidelpes 2020-12-30 15:59:23 +01:00
parent a7994736f1
commit 8e35674b55
2 changed files with 76 additions and 1 deletions

View File

@ -8,9 +8,9 @@ import java.nio.file.Paths;
import java.security.*;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.X509EncodedKeySpec;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.logging.Level;
import java.util.logging.Logger;
import at.ac.tuwien.dsg.orvell.Shell;
@ -383,6 +383,7 @@ public class MessageClient implements IMessageClient, Runnable {
String calculatedHash = msg.calculateHash();
logger.info("Calculated hash: " + calculatedHash);
logger.info("Received hash: " + msg.getHash());
if (msg.getHash().equals(calculatedHash)) {
this.shell.out().println("ok");
} else {
@ -405,15 +406,87 @@ public class MessageClient implements IMessageClient, Runnable {
@Command
@Override
public void msg(String to, String subject, String data) {
ArrayList<Email> toAddresses = new ArrayList<>();
for (String address : to.split(",")) {
try {
toAddresses.add(new Email(address));
} catch (MalformedInputException e) {
this.shell.out().println("error malformed 'to' email");
return;
}
}
Message msg = null;
try {
msg = new Message(toAddresses, new Email(this.transferEmail), subject, data, "");
} catch (MalformedInputException e) {
this.shell.out().println("error malformed 'to' email");
return;
}
logger.info("Received 'msg' command:\tto: " + to + "\tsubject: " + subject + "\tdata: " + data);
try {
logger.info("Starting connection to TransferServer on host " + this.transferHost + " on port " + this.transferPort);
this.dmtpSocket = new Socket(this.transferHost, this.transferPort);
this.dmtpIn = new BufferedReader(new InputStreamReader(this.dmtpSocket.getInputStream()));
this.dmtpOut = new PrintWriter(this.dmtpSocket.getOutputStream(), true);
String response = dmtpIn.readLine();
if (!response.startsWith("ok DMTP")) {
this.shell.out().println("error protocol not supported");
return;
}
dmtpOut.println("begin");
response = dmtpIn.readLine();
if (!response.startsWith("ok")) {
this.shell.out().println("error server did not respond with 'ok'");
return;
}
dmtpOut.println("from " + msg.getFrom());
response = dmtpIn.readLine();
if (!response.startsWith("ok")) {
this.shell.out().println("error server did not respond with 'ok'");
return;
}
dmtpOut.println("to " + msg.printTo());
response = dmtpIn.readLine();
if (!response.startsWith("ok")) {
this.shell.out().println("error server did not respond with 'ok'");
return;
}
dmtpOut.println("subject " + msg.getSubject());
response = dmtpIn.readLine();
if (!response.startsWith("ok")) {
this.shell.out().println("error server did not respond with 'ok'");
return;
}
dmtpOut.println("data " + msg.getData());
response = dmtpIn.readLine();
if (!response.startsWith("ok")) {
this.shell.out().println("error server did not respond with 'ok'");
return;
}
msg.setHash(msg.calculateHash());
logger.info("Hash set to: " + msg.getHash());
dmtpOut.println("hash " + msg.getHash());
response = dmtpIn.readLine();
if (!response.startsWith("ok")) {
this.shell.out().println("error server did not respond with 'ok'");
return;
}
dmtpOut.println("send");
response = dmtpIn.readLine();
if (!response.startsWith("ok")) {
this.shell.out().println("error server did not respond with 'ok'");
return;
}
dmtpOut.println("quit");
response = dmtpIn.readLine();
if (!response.startsWith("ok bye")) {
this.shell.out().println("error server did not respond with 'ok bye'");
}
} catch (IOException e) {
logger.severe("Could not connect to TransferHost " + transferHost + " on port " + transferPort);
shutdown();
} catch (NoSuchAlgorithmException | InvalidKeyException e) {
logger.severe("Error during encryption/decryption");
}
}

View File

@ -196,6 +196,8 @@ public class TransferServer implements ITransferServer, Runnable {
socketIn.readLine();
socketOut.println("from " + msg.getFrom().toString());
socketIn.readLine();
socketOut.println("hash " + msg.getHash());
socketIn.readLine();
socketOut.println("send");
String result = socketIn.readLine();
if (result.startsWith("error"))