Fix inbox command

This commit is contained in:
Tobias Eidelpes 2021-01-06 13:20:04 +01:00
parent a9c4465a24
commit e30359652c

View File

@ -11,6 +11,7 @@ import java.security.spec.X509EncodedKeySpec;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.LinkedList;
import java.util.logging.Logger;
import at.ac.tuwien.dsg.orvell.Shell;
@ -324,34 +325,34 @@ public class MessageClient implements IMessageClient, Runnable {
@Command
@Override
public void inbox() throws BadPaddingException, IllegalBlockSizeException {
public void inbox() {
LinkedList<String> inboxMessages = new LinkedList<>();
String message;
logger.info("Received 'inbox' command");
message = "list";
this.dmapOut.println(getAesCiphertext(message));
try {
String response = getAesPlaintext(this.dmapIn.readLine());
if (response.equals("ok DMAP2.0") || response.equals("ok DMAP")) {
this.dmapOut.println(getAesCiphertext(message));
response = getAesPlaintext(this.dmapIn.readLine());
String response;
while (!(response = getAesPlaintext(this.dmapIn.readLine())).startsWith("ok")) {
if (response.startsWith("You do not have any messages at the moment!")) {
this.shell.out().println(response);
return;
}
String [] temp = response.split("\\s+");
String [] help = new String[temp.length / 3];
int count = 0;
for (int i = 0; i < help.length; i++) {
help[i] = temp[count];
count = count + 3;
inboxMessages.add(response.split("\\s+")[0]); // Get IDs
}
String result = "";
for (int i = 0; i < help.length; i++) {
message = "show " + help[i];
this.dmapOut.println(getAesCiphertext(message));
result = getAesPlaintext(this.dmapIn.readLine() + "\n");
StringBuilder result = new StringBuilder();
for (String id : inboxMessages) {
result.append("\n");
this.dmapOut.println(getAesCiphertext("show " + id));
result.append(getAesPlaintext(this.dmapIn.readLine()));
}
System.out.println("Your Mailbox: \n" + result);
this.shell.out().println("Your Mailbox:" + result);
} catch (IllegalBlockSizeException | BadPaddingException e) {
logger.severe("Error during encryption/decryption");
e.printStackTrace();
} catch (IOException e) {
System.out.println("IO Exception " + e.getMessage());
logger.severe("Could not connect to MailboxHost " + this.mailboxHost + " on port " + this.mailboxPort);
shutdown();
}
}