📄 remotemanagerhandler.java
字号:
theWatchdog = null; } in = null; out = null; try { if (socket != null) { socket.close(); } } catch (IOException e) { if (getLogger().isErrorEnabled()) { getLogger().error("Exception closing socket: " + e.getMessage()); } } finally { socket = null; } synchronized (this) { handlerThread = null; } // Reset user repository users = theConfigData.getUsersRepository(); inLocalUsers = true; // Clear config data theConfigData = null; } /** * <p>This method parses and processes RemoteManager commands read off the * wire in handleConnection. It returns true if expecting additional * commands, false otherwise.</p> * * @param command the raw command string passed in over the socket * * @return whether additional commands are expected. */ private boolean parseCommand( String rawCommand ) { if (rawCommand == null) { return false; } String command = rawCommand.trim(); String argument = null; int breakIndex = command.indexOf(" "); if (breakIndex > 0) { argument = command.substring(breakIndex + 1); command = command.substring(0, breakIndex); } command = command.toUpperCase(Locale.US); String argument1 = null; if (command.equals(COMMAND_ADDUSER)) { doADDUSER(argument); } else if (command.equals(COMMAND_SETPASSWORD)) { return doSETPASSWORD(argument); } else if (command.equals(COMMAND_DELUSER)) { return doDELUSER(argument); } else if (command.equals(COMMAND_LISTUSERS)) { return doLISTUSERS(argument); } else if (command.equals(COMMAND_COUNTUSERS)) { return doCOUNTUSERS(argument); } else if (command.equals(COMMAND_VERIFY)) { return doVERIFY(argument); } else if (command.equals(COMMAND_HELP)) { return doHELP(argument); } else if (command.equals(COMMAND_SETALIAS)) { return doSETALIAS(argument); } else if (command.equals(COMMAND_SETFORWARDING)) { return doSETFORWARDING(argument); } else if (command.equals(COMMAND_SHOWALIAS)) { return doSHOWALIAS(argument); } else if (command.equals(COMMAND_SHOWFORWARDING)) { return doSHOWFORWARDING(argument); } else if (command.equals(COMMAND_UNSETALIAS)) { return doUNSETALIAS(argument); } else if (command.equals(COMMAND_UNSETFORWARDING)) { return doUNSETFORWARDING(argument); } else if (command.equals(COMMAND_USER)) { return doUSER(argument); } else if (command.equals(COMMAND_QUIT)) { return doQUIT(argument); } else if (command.equals(COMMAND_SHUTDOWN)) { return doSHUTDOWN(argument); } else { return doUnknownCommand(rawCommand); } return true; } /** * Handler method called upon receipt of an ADDUSER command. * Returns whether further commands should be read off the wire. * * @param argument the argument passed in with the command */ private boolean doADDUSER(String argument) { int breakIndex = -1; if ((argument == null) || (argument.equals("")) || ((breakIndex = argument.indexOf(" ")) < 0)) { writeLoggedFlushedResponse("Usage: adduser [username] [password]"); return true; } String username = argument.substring(0,breakIndex); String passwd = argument.substring(breakIndex + 1); if (username.equals("") || passwd.equals("")) { writeLoggedFlushedResponse("Usage: adduser [username] [password]"); return true; } boolean success = false; if (users.contains(username)) { StringBuffer responseBuffer = new StringBuffer(64) .append("User ") .append(username) .append(" already exists"); String response = responseBuffer.toString(); writeLoggedResponse(response); } else if ( inLocalUsers ) { // TODO: Why does the LocalUsers repository get treated differently? // What exactly is the LocalUsers repository? success = theConfigData.getMailServer().addUser(username, passwd); } else { DefaultUser user = new DefaultUser(username, "SHA"); user.setPassword(passwd); success = users.addUser(user); } if ( success ) { StringBuffer responseBuffer = new StringBuffer(64) .append("User ") .append(username) .append(" added"); String response = responseBuffer.toString(); out.println(response); getLogger().info(response); } else { out.println("Error adding user " + username); getLogger().error("Error adding user " + username); } out.flush(); return true; } /** * Handler method called upon receipt of an SETPASSWORD command. * Returns whether further commands should be read off the wire. * * @param argument the argument passed in with the command */ private boolean doSETPASSWORD(String argument) { int breakIndex = -1; if ((argument == null) || (argument.equals("")) || ((breakIndex = argument.indexOf(" ")) < 0)) { writeLoggedFlushedResponse("Usage: setpassword [username] [password]"); return true; } String username = argument.substring(0,breakIndex); String passwd = argument.substring(breakIndex + 1); if (username.equals("") || passwd.equals("")) { writeLoggedFlushedResponse("Usage: adduser [username] [password]"); return true; } User user = users.getUserByName(username); if (user == null) { writeLoggedFlushedResponse("No such user " + username); return true; } boolean success = user.setPassword(passwd); if (success) { users.updateUser(user); StringBuffer responseBuffer = new StringBuffer(64) .append("Password for ") .append(username) .append(" reset"); String response = responseBuffer.toString(); out.println(response); getLogger().info(response); } else { out.println("Error resetting password"); getLogger().error("Error resetting password"); } out.flush(); return true; } /** * Handler method called upon receipt of an DELUSER command. * Returns whether further commands should be read off the wire. * * @param argument the argument passed in with the command */ private boolean doDELUSER(String argument) { String user = argument; if ((user == null) || (user.equals(""))) { writeLoggedFlushedResponse("Usage: deluser [username]"); return true; } if (users.contains(user)) { try { users.removeUser(user); StringBuffer responseBuffer = new StringBuffer(64) .append("User ") .append(user) .append(" deleted"); String response = responseBuffer.toString(); out.println(response); getLogger().info(response); } catch (Exception e) { StringBuffer exceptionBuffer = new StringBuffer(128) .append("Error deleting user ") .append(user) .append(" : ") .append(e.getMessage()); String exception = exceptionBuffer.toString(); out.println(exception); getLogger().error(exception); } } else { StringBuffer responseBuffer = new StringBuffer(64) .append("User ") .append(user) .append(" doesn't exist"); String response = responseBuffer.toString(); out.println(response); } out.flush(); return true; } /** * Handler method called upon receipt of an LISTUSERS command. * Returns whether further commands should be read off the wire. * * @param argument the argument passed in with the command */ private boolean doLISTUSERS(String argument) { writeLoggedResponse("Existing accounts " + users.countUsers()); for (Iterator it = users.list(); it.hasNext();) { writeLoggedResponse("user: " + (String) it.next()); } out.flush(); return true; } /** * Handler method called upon receipt of an COUNTUSERS command. * Returns whether further commands should be read off the wire. * * @param argument the argument passed in with the command */ private boolean doCOUNTUSERS(String argument) { writeLoggedFlushedResponse("Existing accounts " + users.countUsers()); return true; } /** * Handler method called upon receipt of an VERIFY command. * Returns whether further commands should be read off the wire. * * @param argument the argument passed in with the command */ private boolean doVERIFY(String argument) { String user = argument; if (user == null || user.equals("")) { writeLoggedFlushedResponse("Usage: verify [username]"); return true; } if (users.contains(user)) { StringBuffer responseBuffer = new StringBuffer(64) .append("User ") .append(user) .append(" exists"); String response = responseBuffer.toString(); writeLoggedResponse(response); } else { StringBuffer responseBuffer = new StringBuffer(64) .append("User ") .append(user) .append(" does not exist"); String response = responseBuffer.toString(); writeLoggedResponse(response); } out.flush(); return true; } /** * Handler method called upon receipt of an HELP command. * Returns whether further commands should be read off the wire. * * @param argument the argument passed in with the command */ private boolean doHELP(String argument) { out.println("Currently implemented commands:"); out.println("help display this help"); out.println("listusers display existing accounts"); out.println("countusers display the number of existing accounts"); out.println("adduser [username] [password] add a new user"); out.println("verify [username] verify if specified user exist"); out.println("deluser [username] delete existing user"); out.println("setpassword [username] [password] sets a user's password"); out.println("setalias [user] [alias] locally forwards all email for 'user' to 'alias'"); out.println("showalias [username] shows a user's current email alias"); out.println("unsetalias [user] unsets an alias for 'user'"); out.println("setforwarding [username] [emailaddress] forwards a user's email to another email address"); out.println("showforwarding [username] shows a user's current email forwarding"); out.println("unsetforwarding [username] removes a forward"); out.println("user [repositoryname] change to another user repository"); out.println("shutdown kills the current JVM (convenient when James is run as a daemon)"); out.println("quit close connection"); out.flush(); return true; } /** * Handler method called upon receipt of an SETALIAS command. * Returns whether further commands should be read off the wire. * * @param argument the argument passed in with the command */ private boolean doSETALIAS(String argument) { int breakIndex = -1; if ((argument == null) || (argument.equals("")) || ((breakIndex = argument.indexOf(" ")) < 0)) { writeLoggedFlushedResponse("Usage: setalias [username] [emailaddress]"); return true; } String username = argument.substring(0,breakIndex); String alias = argument.substring(breakIndex + 1); if (username.equals("") || alias.equals("")) { writeLoggedFlushedResponse("Usage: setalias [username] [alias]"); return true; } JamesUser user = (JamesUser) users.getUserByName(username); if (user == null) { writeLoggedFlushedResponse("No such user " + username); return true;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -