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-13 10:02:48 -05:00
|
|
|
import org.yaaic.command.handler.CloseHandler;
|
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;
|
|
|
|
import org.yaaic.command.handler.JoinHandler;
|
|
|
|
import org.yaaic.command.handler.KickHandler;
|
|
|
|
import org.yaaic.command.handler.MeHandler;
|
|
|
|
import org.yaaic.command.handler.NamesHandler;
|
|
|
|
import org.yaaic.command.handler.NickHandler;
|
|
|
|
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;
|
|
|
|
import org.yaaic.command.handler.TopicHandler;
|
|
|
|
import org.yaaic.command.handler.VoiceHandler;
|
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;
|
|
|
|
|
|
|
|
import android.content.Intent;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parser for commands
|
|
|
|
*
|
|
|
|
* @author Sebastian Kaspari <sebastian@yaaic.org>
|
|
|
|
*/
|
|
|
|
public class CommandParser
|
|
|
|
{
|
|
|
|
public static final String TAG = "Yaaic/CommandParser";
|
|
|
|
|
2010-03-10 03:01:49 -05:00
|
|
|
private HashMap<String, BaseHandler> commands;
|
2010-03-09 13:46:02 -05:00
|
|
|
private static CommandParser instance;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-10 16:15:20 -05:00
|
|
|
|
|
|
|
// Aliases
|
|
|
|
commands.put("j", commands.get("join"));
|
|
|
|
commands.put("q", commands.get("query"));
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Is the given command a valid command?
|
|
|
|
*
|
|
|
|
* @param command
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public boolean isCommand(String command)
|
|
|
|
{
|
|
|
|
return commands.containsKey(command);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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];
|
|
|
|
|
|
|
|
if (isCommand(type)) {
|
2010-03-10 03:01:49 -05:00
|
|
|
BaseHandler command = commands.get(type);
|
2010-03-09 14:13:29 -05:00
|
|
|
try {
|
2010-03-10 14:58:37 -05:00
|
|
|
command.execute(params, server, conversation, service);
|
2010-03-09 14:13:29 -05:00
|
|
|
} catch(CommandException e) {
|
2010-03-09 13:46:02 -05:00
|
|
|
// Wrong number of params
|
2010-03-10 14:58:37 -05:00
|
|
|
if (conversation != null) {
|
2010-03-09 14:13:29 -05:00
|
|
|
Message errorMessage = new Message(type + ": " + e.getMessage());
|
|
|
|
errorMessage.setColor(Message.COLOR_RED);
|
2010-03-10 14:58:37 -05:00
|
|
|
conversation.addMessage(errorMessage);
|
2010-03-09 14:13:29 -05:00
|
|
|
|
2010-03-09 16:00:04 -05:00
|
|
|
Message usageMessage = new Message("Syntax: " + command.getUsage());
|
2010-03-10 14:58:37 -05:00
|
|
|
conversation.addMessage(usageMessage);
|
2010-03-09 13:46:02 -05:00
|
|
|
|
2010-03-10 18:25:53 -05:00
|
|
|
Intent intent = new Intent(Broadcast.CONVERSATION_MESSAGE);
|
2010-03-09 13:46:02 -05:00
|
|
|
intent.putExtra(Broadcast.EXTRA_SERVER, server.getId());
|
2010-03-10 18:25:53 -05:00
|
|
|
intent.putExtra(Broadcast.EXTRA_CONVERSATION, conversation.getName());
|
2010-03-09 13:46:02 -05:00
|
|
|
service.sendBroadcast(intent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Unknown command
|
2010-03-10 14:58:37 -05:00
|
|
|
if (conversation != null) {
|
2010-03-09 13:46:02 -05:00
|
|
|
Message message = new Message("Unknown command: " + type);
|
|
|
|
message.setColor(Message.COLOR_RED);
|
2010-03-10 14:58:37 -05:00
|
|
|
conversation.addMessage(message);
|
2010-03-09 13:46:02 -05:00
|
|
|
|
2010-03-10 18:25:53 -05:00
|
|
|
Intent intent = new Intent(Broadcast.CONVERSATION_MESSAGE);
|
2010-03-09 13:46:02 -05:00
|
|
|
intent.putExtra(Broadcast.EXTRA_SERVER, server.getId());
|
2010-03-10 18:25:53 -05:00
|
|
|
intent.putExtra(Broadcast.EXTRA_CONVERSATION, conversation.getName());
|
2010-03-09 13:46:02 -05:00
|
|
|
service.sendBroadcast(intent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|