Implement 'msg' command
This commit is contained in:
parent
a7994736f1
commit
8e35674b55
@ -8,9 +8,9 @@ import java.nio.file.Paths;
|
|||||||
import java.security.*;
|
import java.security.*;
|
||||||
import java.security.spec.InvalidKeySpecException;
|
import java.security.spec.InvalidKeySpecException;
|
||||||
import java.security.spec.X509EncodedKeySpec;
|
import java.security.spec.X509EncodedKeySpec;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Base64;
|
import java.util.Base64;
|
||||||
import java.util.logging.Level;
|
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import at.ac.tuwien.dsg.orvell.Shell;
|
import at.ac.tuwien.dsg.orvell.Shell;
|
||||||
@ -383,6 +383,7 @@ public class MessageClient implements IMessageClient, Runnable {
|
|||||||
|
|
||||||
String calculatedHash = msg.calculateHash();
|
String calculatedHash = msg.calculateHash();
|
||||||
logger.info("Calculated hash: " + calculatedHash);
|
logger.info("Calculated hash: " + calculatedHash);
|
||||||
|
logger.info("Received hash: " + msg.getHash());
|
||||||
if (msg.getHash().equals(calculatedHash)) {
|
if (msg.getHash().equals(calculatedHash)) {
|
||||||
this.shell.out().println("ok");
|
this.shell.out().println("ok");
|
||||||
} else {
|
} else {
|
||||||
@ -405,15 +406,87 @@ public class MessageClient implements IMessageClient, Runnable {
|
|||||||
@Command
|
@Command
|
||||||
@Override
|
@Override
|
||||||
public void msg(String to, String subject, String data) {
|
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);
|
logger.info("Received 'msg' command:\tto: " + to + "\tsubject: " + subject + "\tdata: " + data);
|
||||||
try {
|
try {
|
||||||
logger.info("Starting connection to TransferServer on host " + this.transferHost + " on port " + this.transferPort);
|
logger.info("Starting connection to TransferServer on host " + this.transferHost + " on port " + this.transferPort);
|
||||||
this.dmtpSocket = new Socket(this.transferHost, this.transferPort);
|
this.dmtpSocket = new Socket(this.transferHost, this.transferPort);
|
||||||
this.dmtpIn = new BufferedReader(new InputStreamReader(this.dmtpSocket.getInputStream()));
|
this.dmtpIn = new BufferedReader(new InputStreamReader(this.dmtpSocket.getInputStream()));
|
||||||
this.dmtpOut = new PrintWriter(this.dmtpSocket.getOutputStream(), true);
|
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) {
|
} catch (IOException e) {
|
||||||
logger.severe("Could not connect to TransferHost " + transferHost + " on port " + transferPort);
|
logger.severe("Could not connect to TransferHost " + transferHost + " on port " + transferPort);
|
||||||
shutdown();
|
shutdown();
|
||||||
|
} catch (NoSuchAlgorithmException | InvalidKeyException e) {
|
||||||
|
logger.severe("Error during encryption/decryption");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -196,6 +196,8 @@ public class TransferServer implements ITransferServer, Runnable {
|
|||||||
socketIn.readLine();
|
socketIn.readLine();
|
||||||
socketOut.println("from " + msg.getFrom().toString());
|
socketOut.println("from " + msg.getFrom().toString());
|
||||||
socketIn.readLine();
|
socketIn.readLine();
|
||||||
|
socketOut.println("hash " + msg.getHash());
|
||||||
|
socketIn.readLine();
|
||||||
socketOut.println("send");
|
socketOut.println("send");
|
||||||
String result = socketIn.readLine();
|
String result = socketIn.readLine();
|
||||||
if (result.startsWith("error"))
|
if (result.startsWith("error"))
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user