Improve return codes

Always return non-zero code, when sending failed

Fixes #22
This commit is contained in:
AsamK 2016-07-14 16:20:14 +02:00
parent 9f075da269
commit 0f0d8a873a

View file

@ -64,6 +64,11 @@ public class Main {
System.exit(1); System.exit(1);
} }
int res = handleCommands(ns);
System.exit(res);
}
private static int handleCommands(Namespace ns) {
final String username = ns.getString("username"); final String username = ns.getString("username");
Manager m; Manager m;
Signal ts; Signal ts;
@ -87,8 +92,7 @@ public class Main {
if (dBusConn != null) { if (dBusConn != null) {
dBusConn.disconnect(); dBusConn.disconnect();
} }
System.exit(3); return 3;
return;
} }
} else { } else {
String settingsPath = ns.getString("config"); String settingsPath = ns.getString("config");
@ -109,8 +113,7 @@ public class Main {
m.load(); m.load();
} catch (Exception e) { } catch (Exception e) {
System.err.println("Error loading state file \"" + m.getFileName() + "\": " + e.getMessage()); System.err.println("Error loading state file \"" + m.getFileName() + "\": " + e.getMessage());
System.exit(2); return 2;
return;
} }
} }
} }
@ -119,7 +122,7 @@ public class Main {
case "register": case "register":
if (dBusConn != null) { if (dBusConn != null) {
System.err.println("register is not yet implemented via dbus"); System.err.println("register is not yet implemented via dbus");
System.exit(1); return 1;
} }
if (!m.userHasKeys()) { if (!m.userHasKeys()) {
m.createNewIdentity(); m.createNewIdentity();
@ -128,33 +131,33 @@ public class Main {
m.register(ns.getBoolean("voice")); m.register(ns.getBoolean("voice"));
} catch (IOException e) { } catch (IOException e) {
System.err.println("Request verify error: " + e.getMessage()); System.err.println("Request verify error: " + e.getMessage());
System.exit(3); return 3;
} }
break; break;
case "verify": case "verify":
if (dBusConn != null) { if (dBusConn != null) {
System.err.println("verify is not yet implemented via dbus"); System.err.println("verify is not yet implemented via dbus");
System.exit(1); return 1;
} }
if (!m.userHasKeys()) { if (!m.userHasKeys()) {
System.err.println("User has no keys, first call register."); System.err.println("User has no keys, first call register.");
System.exit(1); return 1;
} }
if (m.isRegistered()) { if (m.isRegistered()) {
System.err.println("User registration is already verified"); System.err.println("User registration is already verified");
System.exit(1); return 1;
} }
try { try {
m.verifyAccount(ns.getString("verificationCode")); m.verifyAccount(ns.getString("verificationCode"));
} catch (IOException e) { } catch (IOException e) {
System.err.println("Verify error: " + e.getMessage()); System.err.println("Verify error: " + e.getMessage());
System.exit(3); return 3;
} }
break; break;
case "link": case "link":
if (dBusConn != null) { if (dBusConn != null) {
System.err.println("link is not yet implemented via dbus"); System.err.println("link is not yet implemented via dbus");
System.exit(1); return 1;
} }
// When linking, username is null and we always have to create keys // When linking, username is null and we always have to create keys
@ -170,48 +173,48 @@ public class Main {
System.out.println("Associated with: " + m.getUsername()); System.out.println("Associated with: " + m.getUsername());
} catch (TimeoutException e) { } catch (TimeoutException e) {
System.err.println("Link request timed out, please try again."); System.err.println("Link request timed out, please try again.");
System.exit(3); return 3;
} catch (IOException e) { } catch (IOException e) {
System.err.println("Link request error: " + e.getMessage()); System.err.println("Link request error: " + e.getMessage());
System.exit(3); return 3;
} catch (InvalidKeyException e) { } catch (InvalidKeyException e) {
e.printStackTrace(); e.printStackTrace();
System.exit(3); return 2;
} catch (UserAlreadyExists e) { } catch (UserAlreadyExists e) {
System.err.println("The user " + e.getUsername() + " already exists\nDelete \"" + e.getFileName() + "\" before trying again."); System.err.println("The user " + e.getUsername() + " already exists\nDelete \"" + e.getFileName() + "\" before trying again.");
System.exit(3); return 1;
} }
break; break;
case "addDevice": case "addDevice":
if (dBusConn != null) { if (dBusConn != null) {
System.err.println("link is not yet implemented via dbus"); System.err.println("link is not yet implemented via dbus");
System.exit(1); return 1;
} }
if (!m.isRegistered()) { if (!m.isRegistered()) {
System.err.println("User is not registered."); System.err.println("User is not registered.");
System.exit(1); return 1;
} }
try { try {
m.addDeviceLink(new URI(ns.getString("uri"))); m.addDeviceLink(new URI(ns.getString("uri")));
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
System.exit(3); return 3;
} catch (InvalidKeyException e) { } catch (InvalidKeyException e) {
e.printStackTrace(); e.printStackTrace();
System.exit(2); return 2;
} catch (URISyntaxException e) { } catch (URISyntaxException e) {
e.printStackTrace(); e.printStackTrace();
System.exit(2); return 2;
} }
break; break;
case "listDevices": case "listDevices":
if (dBusConn != null) { if (dBusConn != null) {
System.err.println("listDevices is not yet implemented via dbus"); System.err.println("listDevices is not yet implemented via dbus");
System.exit(1); return 1;
} }
if (!m.isRegistered()) { if (!m.isRegistered()) {
System.err.println("User is not registered."); System.err.println("User is not registered.");
System.exit(1); return 1;
} }
try { try {
List<DeviceInfo> devices = m.getLinkedDevices(); List<DeviceInfo> devices = m.getLinkedDevices();
@ -223,48 +226,52 @@ public class Main {
} }
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
System.exit(3); return 3;
} }
break; break;
case "removeDevice": case "removeDevice":
if (dBusConn != null) { if (dBusConn != null) {
System.err.println("removeDevice is not yet implemented via dbus"); System.err.println("removeDevice is not yet implemented via dbus");
System.exit(1); return 1;
} }
if (!m.isRegistered()) { if (!m.isRegistered()) {
System.err.println("User is not registered."); System.err.println("User is not registered.");
System.exit(1); return 1;
} }
try { try {
int deviceId = ns.getInt("deviceId"); int deviceId = ns.getInt("deviceId");
m.removeLinkedDevices(deviceId); m.removeLinkedDevices(deviceId);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
System.exit(3); return 3;
} }
break; break;
case "send": case "send":
if (dBusConn == null && !m.isRegistered()) { if (dBusConn == null && !m.isRegistered()) {
System.err.println("User is not registered."); System.err.println("User is not registered.");
System.exit(1); return 1;
} }
if (ns.getBoolean("endsession")) { if (ns.getBoolean("endsession")) {
if (ns.getList("recipient") == null) { if (ns.getList("recipient") == null) {
System.err.println("No recipients given"); System.err.println("No recipients given");
System.err.println("Aborting sending."); System.err.println("Aborting sending.");
System.exit(1); return 1;
} }
try { try {
ts.sendEndSessionMessage(ns.<String>getList("recipient")); ts.sendEndSessionMessage(ns.<String>getList("recipient"));
} catch (IOException e) { } catch (IOException e) {
handleIOException(e); handleIOException(e);
return 3;
} catch (EncapsulatedExceptions e) { } catch (EncapsulatedExceptions e) {
handleEncapsulatedExceptions(e); handleEncapsulatedExceptions(e);
return 3;
} catch (AssertionError e) { } catch (AssertionError e) {
handleAssertionError(e); handleAssertionError(e);
return 1;
} catch (DBusExecutionException e) { } catch (DBusExecutionException e) {
handleDBusExecutionException(e); handleDBusExecutionException(e);
return 1;
} }
} else { } else {
String messageText = ns.getString("message"); String messageText = ns.getString("message");
@ -274,7 +281,7 @@ public class Main {
} catch (IOException e) { } catch (IOException e) {
System.err.println("Failed to read message from stdin: " + e.getMessage()); System.err.println("Failed to read message from stdin: " + e.getMessage());
System.err.println("Aborting sending."); System.err.println("Aborting sending.");
System.exit(1); return 1;
} }
} }
@ -291,18 +298,23 @@ public class Main {
} }
} catch (IOException e) { } catch (IOException e) {
handleIOException(e); handleIOException(e);
return 3;
} catch (EncapsulatedExceptions e) { } catch (EncapsulatedExceptions e) {
handleEncapsulatedExceptions(e); handleEncapsulatedExceptions(e);
return 3;
} catch (AssertionError e) { } catch (AssertionError e) {
handleAssertionError(e); handleAssertionError(e);
return 1;
} catch (GroupNotFoundException e) { } catch (GroupNotFoundException e) {
handleGroupNotFoundException(e); handleGroupNotFoundException(e);
return 1;
} catch (AttachmentInvalidException e) { } catch (AttachmentInvalidException e) {
System.err.println("Failed to add attachment: " + e.getMessage()); System.err.println("Failed to add attachment: " + e.getMessage());
System.err.println("Aborting sending."); System.err.println("Aborting sending.");
System.exit(1); return 1;
} catch (DBusExecutionException e) { } catch (DBusExecutionException e) {
handleDBusExecutionException(e); handleDBusExecutionException(e);
return 1;
} }
} }
@ -330,18 +342,19 @@ public class Main {
}); });
} catch (DBusException e) { } catch (DBusException e) {
e.printStackTrace(); e.printStackTrace();
return 1;
} }
while (true) { while (true) {
try { try {
Thread.sleep(10000); Thread.sleep(10000);
} catch (InterruptedException e) { } catch (InterruptedException e) {
System.exit(0); return 0;
} }
} }
} }
if (!m.isRegistered()) { if (!m.isRegistered()) {
System.err.println("User is not registered."); System.err.println("User is not registered.");
System.exit(1); return 1;
} }
int timeout = 5; int timeout = 5;
if (ns.getInt("timeout") != null) { if (ns.getInt("timeout") != null) {
@ -356,42 +369,47 @@ public class Main {
m.receiveMessages(timeout, returnOnTimeout, new ReceiveMessageHandler(m)); m.receiveMessages(timeout, returnOnTimeout, new ReceiveMessageHandler(m));
} catch (IOException e) { } catch (IOException e) {
System.err.println("Error while receiving messages: " + e.getMessage()); System.err.println("Error while receiving messages: " + e.getMessage());
System.exit(3); return 3;
} catch (AssertionError e) { } catch (AssertionError e) {
handleAssertionError(e); handleAssertionError(e);
return 1;
} }
break; break;
case "quitGroup": case "quitGroup":
if (dBusConn != null) { if (dBusConn != null) {
System.err.println("quitGroup is not yet implemented via dbus"); System.err.println("quitGroup is not yet implemented via dbus");
System.exit(1); return 1;
} }
if (!m.isRegistered()) { if (!m.isRegistered()) {
System.err.println("User is not registered."); System.err.println("User is not registered.");
System.exit(1); return 1;
} }
try { try {
m.sendQuitGroupMessage(decodeGroupId(ns.getString("group"))); m.sendQuitGroupMessage(decodeGroupId(ns.getString("group")));
} catch (IOException e) { } catch (IOException e) {
handleIOException(e); handleIOException(e);
return 3;
} catch (EncapsulatedExceptions e) { } catch (EncapsulatedExceptions e) {
handleEncapsulatedExceptions(e); handleEncapsulatedExceptions(e);
return 3;
} catch (AssertionError e) { } catch (AssertionError e) {
handleAssertionError(e); handleAssertionError(e);
return 1;
} catch (GroupNotFoundException e) { } catch (GroupNotFoundException e) {
handleGroupNotFoundException(e); handleGroupNotFoundException(e);
return 1;
} }
break; break;
case "updateGroup": case "updateGroup":
if (dBusConn != null) { if (dBusConn != null) {
System.err.println("updateGroup is not yet implemented via dbus"); System.err.println("updateGroup is not yet implemented via dbus");
System.exit(1); return 1;
} }
if (!m.isRegistered()) { if (!m.isRegistered()) {
System.err.println("User is not registered."); System.err.println("User is not registered.");
System.exit(1); return 1;
} }
try { try {
@ -405,25 +423,28 @@ public class Main {
} }
} catch (IOException e) { } catch (IOException e) {
handleIOException(e); handleIOException(e);
return 3;
} catch (AttachmentInvalidException e) { } catch (AttachmentInvalidException e) {
System.err.println("Failed to add avatar attachment for group\": " + e.getMessage()); System.err.println("Failed to add avatar attachment for group\": " + e.getMessage());
System.err.println("Aborting sending."); System.err.println("Aborting sending.");
System.exit(1); return 1;
} catch (GroupNotFoundException e) { } catch (GroupNotFoundException e) {
handleGroupNotFoundException(e); handleGroupNotFoundException(e);
return 1;
} catch (EncapsulatedExceptions e) { } catch (EncapsulatedExceptions e) {
handleEncapsulatedExceptions(e); handleEncapsulatedExceptions(e);
return 3;
} }
break; break;
case "daemon": case "daemon":
if (dBusConn != null) { if (dBusConn != null) {
System.err.println("Stop it."); System.err.println("Stop it.");
System.exit(1); return 1;
} }
if (!m.isRegistered()) { if (!m.isRegistered()) {
System.err.println("User is not registered."); System.err.println("User is not registered.");
System.exit(1); return 1;
} }
DBusConnection conn = null; DBusConnection conn = null;
try { try {
@ -439,15 +460,16 @@ public class Main {
conn.requestBusName(SIGNAL_BUSNAME); conn.requestBusName(SIGNAL_BUSNAME);
} catch (DBusException e) { } catch (DBusException e) {
e.printStackTrace(); e.printStackTrace();
System.exit(3); return 2;
} }
try { try {
m.receiveMessages(3600, false, new DbusReceiveMessageHandler(m, conn)); m.receiveMessages(3600, false, new DbusReceiveMessageHandler(m, conn));
} catch (IOException e) { } catch (IOException e) {
System.err.println("Error while receiving messages: " + e.getMessage()); System.err.println("Error while receiving messages: " + e.getMessage());
System.exit(3); return 3;
} catch (AssertionError e) { } catch (AssertionError e) {
handleAssertionError(e); handleAssertionError(e);
return 1;
} }
} finally { } finally {
if (conn != null) { if (conn != null) {
@ -457,7 +479,7 @@ public class Main {
break; break;
} }
System.exit(0); return 0;
} finally { } finally {
if (dBusConn != null) { if (dBusConn != null) {
dBusConn.disconnect(); dBusConn.disconnect();
@ -468,13 +490,11 @@ public class Main {
private static void handleGroupNotFoundException(GroupNotFoundException e) { private static void handleGroupNotFoundException(GroupNotFoundException e) {
System.err.println("Failed to send to group: " + e.getMessage()); System.err.println("Failed to send to group: " + e.getMessage());
System.err.println("Aborting sending."); System.err.println("Aborting sending.");
System.exit(1);
} }
private static void handleDBusExecutionException(DBusExecutionException e) { private static void handleDBusExecutionException(DBusExecutionException e) {
System.err.println("Cannot connect to dbus: " + e.getMessage()); System.err.println("Cannot connect to dbus: " + e.getMessage());
System.err.println("Aborting."); System.err.println("Aborting.");
System.exit(1);
} }
private static byte[] decodeGroupId(String groupId) { private static byte[] decodeGroupId(String groupId) {
@ -617,7 +637,6 @@ public class Main {
System.err.println("Failed to send/receive message (Assertion): " + e.getMessage()); System.err.println("Failed to send/receive message (Assertion): " + e.getMessage());
e.printStackTrace(); e.printStackTrace();
System.err.println("If you use an Oracle JRE please check if you have unlimited strength crypto enabled, see README"); System.err.println("If you use an Oracle JRE please check if you have unlimited strength crypto enabled, see README");
System.exit(1);
} }
private static void handleEncapsulatedExceptions(EncapsulatedExceptions e) { private static void handleEncapsulatedExceptions(EncapsulatedExceptions e) {