2010-03-09 13:46:02 -05:00
|
|
|
/*
|
2010-03-12 14:35:25 -05:00
|
|
|
Yaaic - Yet Another Android IRC Client
|
2010-03-09 13:46:02 -05:00
|
|
|
|
2010-03-13 10:52:20 -05:00
|
|
|
Copyright 2009-2010 Sebastian Kaspari
|
2010-03-09 13:46:02 -05:00
|
|
|
|
|
|
|
This file is part of Yaaic.
|
|
|
|
|
|
|
|
Yaaic is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Yaaic is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Yaaic. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
package org.yaaic.command;
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
2010-03-24 16:17:12 -04:00
|
|
|
import android.content.Intent;
|
|
|
|
|
2010-04-12 16:20:26 -04:00
|
|
|
import org.yaaic.command.handler.AwayHandler;
|
2010-03-13 10:02:48 -05:00
|
|
|
import org.yaaic.command.handler.CloseHandler;
|
2010-03-14 17:54:41 -04:00
|
|
|
import org.yaaic.command.handler.DCCHandler;
|
2010-03-10 03:01:49 -05:00
|
|
|
import org.yaaic.command.handler.DeopHandler;
|
|
|
|
import org.yaaic.command.handler.DevoiceHandler;
|
|
|
|
import org.yaaic.command.handler.EchoHandler;
|
2010-03-26 14:05:37 -04:00
|
|
|
import org.yaaic.command.handler.HelpHandler;
|
2010-03-10 03:01:49 -05:00
|
|
|
import org.yaaic.command.handler.JoinHandler;
|
|
|
|
import org.yaaic.command.handler.KickHandler;
|
|
|
|
import org.yaaic.command.handler.MeHandler;
|
2010-03-17 19:04:50 -04:00
|
|
|
import org.yaaic.command.handler.ModeHandler;
|
2010-04-20 14:49:51 -04:00
|
|
|
import org.yaaic.command.handler.MsgHandler;
|
2010-03-10 03:01:49 -05:00
|
|
|
import org.yaaic.command.handler.NamesHandler;
|
|
|
|
import org.yaaic.command.handler.NickHandler;
|
2010-03-13 12:47:47 -05:00
|
|
|
import org.yaaic.command.handler.NoticeHandler;
|
2010-03-10 03:01:49 -05:00
|
|
|
import org.yaaic.command.handler.OpHandler;
|
2010-03-10 18:25:53 -05:00
|
|
|
import org.yaaic.command.handler.PartHandler;
|
2010-03-10 16:07:35 -05:00
|
|
|
import org.yaaic.command.handler.QueryHandler;
|
2010-03-10 03:01:49 -05:00
|
|
|
import org.yaaic.command.handler.QuitHandler;
|
2010-04-20 16:18:53 -04:00
|
|
|
import org.yaaic.command.handler.RawHandler;
|
2010-03-10 03:01:49 -05:00
|
|
|
import org.yaaic.command.handler.TopicHandler;
|
|
|
|
import org.yaaic.command.handler.VoiceHandler;
|
2010-04-12 16:25:26 -04:00
|
|
|
import org.yaaic.command.handler.WhoisHandler;
|
2010-03-14 08:35:16 -04:00
|
|
|
import org.yaaic.exception.CommandException;
|
2010-03-09 13:46:02 -05:00
|
|
|
import org.yaaic.irc.IRCService;
|
|
|
|
import org.yaaic.model.Broadcast;
|
2010-03-10 14:58:37 -05:00
|
|
|
import org.yaaic.model.Conversation;
|
2010-03-09 13:46:02 -05:00
|
|
|
import org.yaaic.model.Message;
|
|
|
|
import org.yaaic.model.Server;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parser for commands
|
|
|
|
*
|
|
|
|
* @author Sebastian Kaspari <sebastian@yaaic.org>
|
|
|
|
*/
|
|
|
|
public class CommandParser
|
|
|
|
{
|
2010-03-10 03:01:49 -05:00
|
|
|
private HashMap<String, BaseHandler> commands;
|
2010-04-06 14:26:42 -04:00
|
|
|
private HashMap<String, String> aliases;
|
2010-03-09 13:46:02 -05:00
|
|
|
private static CommandParser instance;
|
2010-03-24 16:17:12 -04:00
|
|
|
|
|
|
|
private final static String[] serverCommands = {
|
2010-04-17 10:39:31 -04:00
|
|
|
// whitelist of server commands
|
2010-04-12 16:20:26 -04:00
|
|
|
"admin", "motd", "version", "knock", "rules",
|
2010-03-24 16:17:12 -04:00
|
|
|
"vhost", "credits", "license", "setname", "watch", "pong",
|
|
|
|
"cycle", "links", "silence", "who", "dalinfo", "userhost",
|
2010-04-12 16:25:26 -04:00
|
|
|
"list", "stats", "invite", "lusers", "ping",
|
2010-04-17 10:39:31 -04:00
|
|
|
"time", "whowas", "ison", "map",
|
|
|
|
|
|
|
|
// services
|
|
|
|
"nickserv", "ns", "chanserv", "cs", "authserv", "hostserv",
|
|
|
|
"memoserv", "operserv"
|
2010-03-24 16:17:12 -04:00
|
|
|
};
|
2010-03-09 13:46:02 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new CommandParser instance
|
|
|
|
*/
|
|
|
|
private CommandParser()
|
|
|
|
{
|
2010-03-10 03:01:49 -05:00
|
|
|
commands = new HashMap<String, BaseHandler>();
|
2010-03-09 13:58:51 -05:00
|
|
|
|
2010-03-10 16:07:35 -05:00
|
|
|
// Commands
|
2010-03-10 03:01:49 -05:00
|
|
|
commands.put("nick", new NickHandler());
|
|
|
|
commands.put("join", new JoinHandler());
|
|
|
|
commands.put("me", new MeHandler());
|
|
|
|
commands.put("names", new NamesHandler());
|
|
|
|
commands.put("echo", new EchoHandler());
|
|
|
|
commands.put("topic", new TopicHandler());
|
|
|
|
commands.put("quit", new QuitHandler());
|
|
|
|
commands.put("op", new OpHandler());
|
|
|
|
commands.put("voice", new VoiceHandler());
|
|
|
|
commands.put("deop", new DeopHandler());
|
|
|
|
commands.put("devoice", new DevoiceHandler());
|
|
|
|
commands.put("kick", new KickHandler());
|
2010-03-10 16:07:35 -05:00
|
|
|
commands.put("query", new QueryHandler());
|
2010-03-10 18:25:53 -05:00
|
|
|
commands.put("part", new PartHandler());
|
2010-03-13 10:02:48 -05:00
|
|
|
commands.put("close", new CloseHandler());
|
2010-03-13 12:47:47 -05:00
|
|
|
commands.put("notice", new NoticeHandler());
|
2010-03-14 17:54:41 -04:00
|
|
|
commands.put("dcc", new DCCHandler());
|
2010-03-17 19:04:50 -04:00
|
|
|
commands.put("mode", new ModeHandler());
|
2010-03-26 14:05:37 -04:00
|
|
|
commands.put("help", new HelpHandler());
|
2010-04-12 16:20:26 -04:00
|
|
|
commands.put("away", new AwayHandler());
|
2010-04-12 16:25:26 -04:00
|
|
|
commands.put("whois", new WhoisHandler());
|
2010-04-20 14:49:51 -04:00
|
|
|
commands.put("msg", new MsgHandler());
|
2010-04-20 16:18:53 -04:00
|
|
|
commands.put("quote", new RawHandler());
|
2010-03-10 16:15:20 -05:00
|
|
|
|
2010-04-06 14:26:42 -04:00
|
|
|
aliases = new HashMap<String, String>();
|
2010-04-20 16:18:53 -04:00
|
|
|
|
2010-03-10 16:15:20 -05:00
|
|
|
// Aliases
|
2010-04-06 14:26:42 -04:00
|
|
|
aliases.put("j","join");
|
|
|
|
aliases.put("q", "query");
|
|
|
|
aliases.put("h", "help");
|
2010-04-20 16:18:53 -04:00
|
|
|
aliases.put("raw", "quote");
|
2010-03-09 13:46:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the global CommandParser instance
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public static CommandParser getInstance()
|
|
|
|
{
|
|
|
|
if (instance == null) {
|
|
|
|
instance = new CommandParser();
|
|
|
|
}
|
|
|
|
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2010-03-26 14:05:37 -04:00
|
|
|
/**
|
|
|
|
* Get the commands HashMap
|
|
|
|
*
|
|
|
|
* @return HashMap - command, commandHandler
|
|
|
|
*/
|
2010-04-06 14:26:42 -04:00
|
|
|
public HashMap<String, BaseHandler> getCommands()
|
|
|
|
{
|
2010-03-26 14:05:37 -04:00
|
|
|
return commands;
|
|
|
|
}
|
|
|
|
|
2010-04-06 14:26:42 -04:00
|
|
|
/**
|
|
|
|
* Get the command aliases HashMap
|
|
|
|
*
|
|
|
|
* @return HashMap - alias, command the alias belogs to
|
|
|
|
*/
|
|
|
|
public HashMap<String, String> getAliases()
|
|
|
|
{
|
|
|
|
return aliases;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-09 13:46:02 -05:00
|
|
|
/**
|
2010-03-24 16:06:23 -04:00
|
|
|
* Is the given command a valid client command?
|
2010-03-09 13:46:02 -05:00
|
|
|
*
|
2010-03-24 16:06:23 -04:00
|
|
|
* @param command The (client) command to check (/command)
|
|
|
|
* @return true if the command can be handled by the client, false otherwise
|
|
|
|
*/
|
|
|
|
public boolean isClientCommand(String command)
|
|
|
|
{
|
2010-04-06 14:44:22 -04:00
|
|
|
return commands.containsKey(command.toLowerCase()) || aliases.containsKey(command.toLowerCase());
|
2010-03-24 16:06:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Is the given command a valid server command?
|
|
|
|
*
|
|
|
|
* @param command The (server) command to check (/command)
|
|
|
|
* @return true if the command can be handled by a server, false otherwise
|
2010-03-09 13:46:02 -05:00
|
|
|
*/
|
2010-03-24 16:06:23 -04:00
|
|
|
public boolean isServerCommand(String command)
|
2010-03-09 13:46:02 -05:00
|
|
|
{
|
2010-03-24 16:17:12 -04:00
|
|
|
command = command.toLowerCase();
|
|
|
|
for (String validCommand : serverCommands) {
|
|
|
|
if (validCommand.equals(command)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2010-03-24 16:06:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
{
|
2010-04-06 14:26:42 -04:00
|
|
|
BaseHandler command = null;
|
|
|
|
if (commands.containsKey(type.toLowerCase())) {
|
|
|
|
command = commands.get(type);
|
|
|
|
} else if (aliases.containsKey(type.toLowerCase())) {
|
|
|
|
String commandInCommands = aliases.get(type.toLowerCase());
|
|
|
|
command = commands.get(commandInCommands);
|
|
|
|
}
|
2010-03-24 16:06:23 -04:00
|
|
|
try {
|
|
|
|
command.execute(params, server, conversation, service);
|
|
|
|
} catch(CommandException e) {
|
|
|
|
// Command could not be executed
|
|
|
|
if (conversation != null) {
|
|
|
|
Message errorMessage = new Message(type + ": " + e.getMessage());
|
|
|
|
errorMessage.setColor(Message.COLOR_RED);
|
|
|
|
conversation.addMessage(errorMessage);
|
|
|
|
|
|
|
|
Message usageMessage = new Message("Syntax: " + command.getUsage());
|
|
|
|
conversation.addMessage(usageMessage);
|
|
|
|
|
|
|
|
Intent intent = Broadcast.createConversationIntent(
|
|
|
|
Broadcast.CONVERSATION_MESSAGE,
|
|
|
|
server.getId(),
|
|
|
|
conversation.getName()
|
|
|
|
);
|
|
|
|
|
|
|
|
service.sendBroadcast(intent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) {
|
|
|
|
service.getConnection(server.getId()).sendRawLineViaQueue(
|
|
|
|
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
|
2010-03-09 13:46:02 -05:00
|
|
|
*/
|
2010-03-24 16:06:23 -04:00
|
|
|
public void handleUnknownCommand(String type, Server server, Conversation conversation, IRCService service)
|
2010-03-09 13:46:02 -05:00
|
|
|
{
|
2010-03-24 16:06:23 -04:00
|
|
|
if (conversation != null) {
|
|
|
|
Message message = new Message("Unknown command: " + type);
|
|
|
|
message.setColor(Message.COLOR_RED);
|
|
|
|
conversation.addMessage(message);
|
|
|
|
|
|
|
|
Intent intent = Broadcast.createConversationIntent(
|
|
|
|
Broadcast.CONVERSATION_MESSAGE,
|
|
|
|
server.getId(),
|
|
|
|
conversation.getName()
|
|
|
|
);
|
|
|
|
|
|
|
|
service.sendBroadcast(intent);
|
|
|
|
}
|
2010-03-09 13:46:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse the given line
|
|
|
|
*
|
|
|
|
* @param line
|
|
|
|
*/
|
2010-03-10 14:58:37 -05:00
|
|
|
public void parse(String line, Server server, Conversation conversation, IRCService service)
|
2010-03-09 13:46:02 -05:00
|
|
|
{
|
|
|
|
line = line.trim().substring(1); // cut the slash
|
|
|
|
String[] params = line.split(" ");
|
|
|
|
String type = params[0];
|
|
|
|
|
2010-03-24 16:06:23 -04:00
|
|
|
if (isClientCommand(type)) {
|
|
|
|
handleClientCommand(type, params, server, conversation, service);
|
|
|
|
} else if (isServerCommand(type)) {
|
|
|
|
handleServerCommand(type, params, server, conversation, service);
|
2010-03-09 13:46:02 -05:00
|
|
|
} else {
|
2010-03-24 16:06:23 -04:00
|
|
|
handleUnknownCommand(type, server, conversation, service);
|
2010-03-09 13:46:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|