1
0
mirror of https://github.com/moparisthebest/Yaaic synced 2024-11-30 04:42:18 -05:00

CommandParser: Splitted handling of client/server/unknown commands

This commit is contained in:
Sebastian Kaspari 2010-03-24 21:06:23 +01:00
parent 3fdd183373
commit 507b3b422b

View File

@ -108,33 +108,44 @@ public class CommandParser
}
/**
* Is the given command a valid command?
* Is the given command a valid client command?
*
* @param command
* @return
* @param command The (client) command to check (/command)
* @return true if the command can be handled by the client, false otherwise
*/
public boolean isCommand(String command)
public boolean isClientCommand(String command)
{
return commands.containsKey(command);
return commands.containsKey(command.toLowerCase());
}
/**
* Parse the given line
* Is the given command a valid server command?
*
* @param line
* @param command The (server) command to check (/command)
* @return true if the command can be handled by a server, false otherwise
*/
public void parse(String line, Server server, Conversation conversation, IRCService service)
public boolean isServerCommand(String command)
{
line = line.trim().substring(1); // cut the slash
String[] params = line.split(" ");
String type = params[0];
// XXX: Implement white list
return true;
}
if (isCommand(type)) {
/**
* Handle a client command
*
* @param type Type of the command (/type param1 param2 ..)
* @param params The parameters of the command (0 is the command itself)
* @param server The current server
* @param conversation The selected conversation
* @param service The service handling the connections
*/
public void handleClientCommand(String type, String[] params, Server server, Conversation conversation, IRCService service)
{
BaseHandler command = commands.get(type);
try {
command.execute(params, server, conversation, service);
} catch(CommandException e) {
// Wrong number of params
// Command could not be executed
if (conversation != null) {
Message errorMessage = new Message(type + ": " + e.getMessage());
errorMessage.setColor(Message.COLOR_RED);
@ -152,14 +163,38 @@ public class CommandParser
service.sendBroadcast(intent);
}
}
} else {
// Unknown command
}
/**
* Handle a server command
*
* @param type Type of the command (/type param1 param2 ..)
* @param params The parameters of the command (0 is the command itself)
* @param server The current server
* @param conversation The selected conversation
* @param service The service handling the connections
*/
public void handleServerCommand(String type, String[] params, Server server, Conversation conversation, IRCService service)
{
if (params.length > 1) {
// Send command to server
service.getConnection(server.getId()).sendRawLineViaQueue(
params[0].toUpperCase() + " " + BaseHandler.mergeParams(params)
type.toUpperCase() + " " + BaseHandler.mergeParams(params)
);
} else {
service.getConnection(server.getId()).sendRawLineViaQueue(type.toUpperCase());
}
}
/**
* Handle an unknown command
*
* @param type Type of the command (/type param1 param2 ..)
* @param server The current server
* @param conversation The selected conversation
* @param service The service handling the connections
*/
public void handleUnknownCommand(String type, Server server, Conversation conversation, IRCService service)
{
if (conversation != null) {
Message message = new Message("Unknown command: " + type);
message.setColor(Message.COLOR_RED);
@ -174,6 +209,24 @@ public class CommandParser
service.sendBroadcast(intent);
}
}
/**
* Parse the given line
*
* @param line
*/
public void parse(String line, Server server, Conversation conversation, IRCService service)
{
line = line.trim().substring(1); // cut the slash
String[] params = line.split(" ");
String type = params[0];
if (isClientCommand(type)) {
handleClientCommand(type, params, server, conversation, service);
} else if (isServerCommand(type)) {
handleServerCommand(type, params, server, conversation, service);
} else {
handleUnknownCommand(type, server, conversation, service);
}
}
}