diff --git a/src/main/java/dslab/client/MessageClient.java b/src/main/java/dslab/client/MessageClient.java index 51b56ff..038a282 100644 --- a/src/main/java/dslab/client/MessageClient.java +++ b/src/main/java/dslab/client/MessageClient.java @@ -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 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()); + this.dmapOut.println(getAesCiphertext(message)); + 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; + } + inboxMessages.add(response.split("\\s+")[0]); // Get IDs } - 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; + StringBuilder result = new StringBuilder(); + for (String id : inboxMessages) { + result.append("\n"); + this.dmapOut.println(getAesCiphertext("show " + id)); + result.append(getAesPlaintext(this.dmapIn.readLine())); } - 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"); - } - 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(); } }