diff --git a/src/apdu4j/remote/CmdlineRemoteTerminal.java b/src/apdu4j/remote/CmdlineRemoteTerminal.java index d55592c..ae422a2 100644 --- a/src/apdu4j/remote/CmdlineRemoteTerminal.java +++ b/src/apdu4j/remote/CmdlineRemoteTerminal.java @@ -112,11 +112,27 @@ private void verify(Map msg) throws IOException { private void dialog(Map msg) throws IOException { System.out.println("# " + msg.get("text")); Map< String, Object> m = JSONProtocol.ok(msg); - boolean yes = false; + boolean yes = get_yes_or_no_console("Decision"); - // lanterna requires work as it screws up Console.readPassword() - yes = get_yes_or_no_console(); + if (!yes) { + m.put("button", "red"); + } else { + m.put("button", "green"); + } + pipe.send(m); + } + + private void input(Map msg) throws IOException { + System.out.println("# " + msg.get("text")); + Map< String, Object> m = JSONProtocol.ok(msg); + Console c = System.console(); + String input = c.readLine(msg + "> "); + if (input == null) + input = ""; + input = input.trim(); + System.out.println("> \""+ input + " \""); + boolean yes = get_yes_or_no_console("Confirm"); if (!yes) { m.put("button", "red"); } else { @@ -125,6 +141,7 @@ private void dialog(Map msg) throws IOException { pipe.send(m); } + private void decrypt(Map msg) throws IOException { String cmd = (String) msg.get("bytes"); if (cmd == null) @@ -138,9 +155,7 @@ private void decrypt(Map msg) throws IOException { System.out.println("# " + msg.get("text")); System.out.println("# " + new String(r.getData(), "UTF-8")); Map< String, Object> m = JSONProtocol.ok(msg); - boolean yes = false; - // lanterna requires work as it screws up Console.readPassword() - yes = get_yes_or_no_console(); + boolean yes = get_yes_or_no_console("Confirm"); if (!yes) { m.put("button", "red"); @@ -185,6 +200,8 @@ private boolean processMessage (Map msg) throws IOException { verify(msg); } else if (cmd.equals("DIALOG")) { dialog(msg); + } else if (cmd.equals("INPUT")) { + input(msg); } else if (cmd.equals("STOP")) { stop(msg); return false; @@ -197,10 +214,10 @@ private boolean processMessage (Map msg) throws IOException { return true; } - private boolean get_yes_or_no_console() { + private boolean get_yes_or_no_console(String msg) { Console c = System.console(); while (true) { - String response = c.readLine("y/n ? "); + String response = c.readLine(msg + " y/n ? "); if (response == null) continue; response = response.trim(); diff --git a/src/apdu4j/remote/RemoteTerminal.java b/src/apdu4j/remote/RemoteTerminal.java index 85fff61..13c27c8 100644 --- a/src/apdu4j/remote/RemoteTerminal.java +++ b/src/apdu4j/remote/RemoteTerminal.java @@ -80,7 +80,7 @@ public void statusMessage(String text) throws IOException { } /** - * Shows a dialog message to the user and returnes the pressed button. + * Shows a dialog message to the user and returns the pressed button. * * @param message text to display to the user * @return {@link Button} that was pressed by the user @@ -91,11 +91,28 @@ public Button dialog(String message) throws IOException { m.put("text", message); pipe.send(m); Map r = pipe.recv(); - if (JSONProtocol.check(m, r)) { - return Button.valueOf(((String)r.get("button")).toUpperCase()); - } else { + if (JSONProtocol.check(m, r) || !r.containsKey("button")) { throw new IOException("Unknown button pressed"); } + return Button.valueOf(((String)r.get("button")).toUpperCase()); + } + + /** + * Asks for input from the user. + * + * @param message text to display to the user + * @return null or input + * @throws IOException when communication fails + */ + public String input(String message) throws IOException { + Map m = JSONProtocol.cmd("input"); + m.put("text", message); + pipe.send(m); + Map r = pipe.recv(); + if (!JSONProtocol.check(m, r) || !r.containsKey("value")) { + throw new IOException("No value"); + } + return (String) r.get("value"); } /**