Yaaic/src/org/yaaic/command/handler/HelpHandler.java

112 lines
2.9 KiB
Java
Raw Normal View History

2010-04-06 16:12:10 -04:00
/*
Yaaic - Yet Another Android IRC Client
Copyright 2009-2010 Sebastian Kaspari
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/>.
*/
2010-03-26 14:05:37 -04:00
package org.yaaic.command.handler;
import java.util.HashMap;
2010-04-06 14:38:54 -04:00
import java.util.Set;
2010-03-26 14:05:37 -04:00
import org.yaaic.command.BaseHandler;
import org.yaaic.command.CommandParser;
import org.yaaic.exception.CommandException;
import org.yaaic.irc.IRCService;
import org.yaaic.model.Broadcast;
import org.yaaic.model.Conversation;
import org.yaaic.model.Message;
import org.yaaic.model.Server;
import android.content.Intent;
/**
* Command: /help
*
* @author Karol Gliniecki <karol.gliniecki@googlemail.com>
*/
2010-04-06 14:38:31 -04:00
public class HelpHandler extends BaseHandler
{
2010-03-26 14:05:37 -04:00
/**
* Execute /help
*/
@Override
2010-04-06 14:38:31 -04:00
public void execute(String[] params, Server server, Conversation conversation, IRCService service) throws CommandException
{
2010-03-26 14:05:37 -04:00
CommandParser cp = CommandParser.getInstance();
StringBuffer commandList = new StringBuffer("available commands: \n");
HashMap<String, BaseHandler> commands = cp.getCommands();
2010-04-06 14:23:54 -04:00
HashMap<String, String> aliases = cp.getAliases();
2010-03-26 14:05:37 -04:00
2010-04-06 14:38:54 -04:00
Set<String> commandKeys = commands.keySet();
Set<String> aliasesKeys = aliases.keySet();
2010-04-18 08:06:47 -04:00
Message message;
if (params.length == 2) {
try {
message = new Message("Usage:\n"+commands.get(params[1]).getUsage());
message.setColor(Message.COLOR_YELLOW);
} catch (Exception e) {
message = new Message(params[1]+" is not a valid command");
message.setColor(Message.COLOR_RED);
}
} else {
for (Object command: commandKeys) {
String alias = "";
for (Object aliasCommand: aliasesKeys) {
if (command.equals(aliases.get(aliasCommand))) {
alias = " or /" + aliasCommand;
break;
}
2010-04-06 14:23:54 -04:00
}
2010-04-18 08:06:47 -04:00
commandList.append("/" + command.toString() + alias + " - "+commands.get(command).getDescription() + "\n");
2010-04-06 14:23:54 -04:00
}
2010-04-18 08:06:47 -04:00
message = new Message(commandList.toString());
message.setColor(Message.COLOR_YELLOW);
2010-03-26 14:05:37 -04:00
}
conversation.addMessage(message);
Intent intent = Broadcast.createConversationIntent(
2010-04-06 14:23:54 -04:00
Broadcast.CONVERSATION_MESSAGE,
server.getId(),
conversation.getName()
);
2010-03-26 14:05:37 -04:00
service.sendBroadcast(intent);
}
/**
*
*Usage of /help
*/
@Override
2010-04-06 14:23:54 -04:00
public String getUsage()
{
2010-04-18 16:58:13 -04:00
return "/help [<command>]";
2010-03-26 14:05:37 -04:00
}
/**
* Description of /help
*/
@Override
2010-04-06 14:23:54 -04:00
public String getDescription()
{
return "Lists all available commands";
2010-03-26 14:05:37 -04:00
}
}