org.mailster.smtp.core.commands.impl.HelpCommand Maven / Gradle / Ivy
package org.mailster.smtp.core.commands.impl;
import java.io.IOException;
import java.util.Set;
import java.util.TreeSet;
import org.apache.mina.core.session.IoSession;
import org.mailster.smtp.SMTPServerConfig;
import org.mailster.smtp.core.SMTPContext;
import org.mailster.smtp.core.commands.AbstractCommand;
import org.mailster.smtp.core.commands.CommandException;
/**
* The HELP command implementation.
*
* @author Ian McFarland <[email protected]>
* @author Jon Stevens
* @author De Oliveira Edouard <[email protected]>
*/
public class HelpCommand extends AbstractCommand {
public HelpCommand() {
super("HELP",
"The HELP command gives help info about the topic specified.\r\n" + "For a list of topics, type HELP by itself.\r\n",
"[]\n topic = the topic we want help info about\n");
}
@Override
public void execute(String commandString, IoSession ioSession, SMTPContext ctx) throws IOException {
var args = getArgPredicate(commandString);
if ("".equals(args)) {
sendResponse(ioSession, getCommandMessage(ctx.getSMTPServerConfig()));
return;
}
try {
sendResponse(ioSession, getHelp(args).toString());
} catch (CommandException e) {
sendResponse(ioSession, "504 HELP topic \"" + args + "\" unknown");
}
}
private String getCommandMessage(SMTPServerConfig cfg) {
var response = new StringBuilder();
response.append("214-This is ");
response.append(cfg.getNameVersion());
response.append(" server running on ");
response.append(cfg.getHostName());
response.append("\r\n");
response.append("214-Available commands:\r\n");
getFormattedCommandsList(response);
response.append("214-For more info use \"HELP \".\r\n");
response.append("214-For more information about this server, visit:\r\n");
response.append("214- http://tedorg.free.fr/projects/projects.php?projects_section=3\r\n");
response.append("214-To report bugs in the implementation, send email to:\r\n");
response.append("214- [email protected]\r\n");
response.append("214-For local information send email to Postmaster at your site.\r\n");
response.append("214 End of HELP info");
return response.toString();
}
private void getFormattedCommandsList(StringBuilder sb) {
Set set = new TreeSet<>(super.getHelp().keySet());
for (var key : set) {
sb.append("214- ");
sb.append(key);
sb.append("\r\n");
}
}
}