1
0
mirror of https://github.com/moparisthebest/Yaaic synced 2025-02-16 15:00:14 -05:00

Check for commands and parse them

This commit is contained in:
Sebastian Kaspari 2010-03-09 19:46:02 +01:00
parent 8dda8861e1
commit d5e72638d1
3 changed files with 180 additions and 4 deletions

View File

@ -0,0 +1,51 @@
/*
Yaaic - Yet Another Android IRC Client
Copyright 2009 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/>.
*/
package org.yaaic.command;
import org.yaaic.irc.IRCService;
import org.yaaic.model.Channel;
import org.yaaic.model.Server;
/**
* Base class for commands
*
* @author Sebastian Kaspari <sebastian@yaaic.org>
*/
public abstract class BaseCommand
{
public abstract void execute(String[] params, Server server, Channel channel, IRCService service);
/**
* Get the usage description for this command
*
* @return The usage description
*/
public abstract String getUsage();
/**
* How much params does this command need?
*
* Default: 0
*
* @return The number of params needed
*/
public abstract int needsParams();
}

View File

@ -0,0 +1,120 @@
/*
Yaaic - Yet Another Android IRC Client
Copyright 2009 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/>.
*/
package org.yaaic.command;
import java.util.HashMap;
import org.yaaic.irc.IRCService;
import org.yaaic.model.Broadcast;
import org.yaaic.model.Channel;
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";
private HashMap<String, BaseCommand> commands;
private static CommandParser instance;
/**
* Create a new CommandParser instance
*/
private CommandParser()
{
commands = new HashMap<String, BaseCommand>();
}
/**
* 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
*/
public void parse(String line, Server server, Channel channel, IRCService service)
{
line = line.trim().substring(1); // cut the slash
String[] params = line.split(" ");
String type = params[0];
if (isCommand(type)) {
BaseCommand command = commands.get(type);
if (command.needsParams() > 0 && params.length - 1 == command.needsParams()) {
command.execute(params, server, channel, service);
} else {
// Wrong number of params
if (channel != null) {
Message message = new Message("Usage of " + type + ": " + command.getUsage());
message.setColor(Message.COLOR_RED);
channel.addMessage(message);
Intent intent = new Intent(Broadcast.CHANNEL_MESSAGE);
intent.putExtra(Broadcast.EXTRA_SERVER, server.getId());
intent.putExtra(Broadcast.EXTRA_CHANNEL, channel.getName());
service.sendBroadcast(intent);
}
}
} else {
// Unknown command
if (channel != null) {
Message message = new Message("Unknown command: " + type);
message.setColor(Message.COLOR_RED);
channel.addMessage(message);
Intent intent = new Intent(Broadcast.CHANNEL_MESSAGE);
intent.putExtra(Broadcast.EXTRA_SERVER, server.getId());
intent.putExtra(Broadcast.EXTRA_CHANNEL, channel.getName());
service.sendBroadcast(intent);
}
}
}
}

View File

@ -51,6 +51,7 @@ import org.yaaic.R;
import org.yaaic.Yaaic;
import org.yaaic.adapter.DeckAdapter;
import org.yaaic.adapter.MessageListAdapter;
import org.yaaic.command.CommandParser;
import org.yaaic.irc.IRCBinder;
import org.yaaic.irc.IRCService;
import org.yaaic.listener.ChannelListener;
@ -325,10 +326,14 @@ public class ServerActivity extends Activity implements ServiceConnection, Chann
Channel channel = deckAdapter.getItem(deck.getSelectedItemPosition());
if (channel != null) {
String nickname = this.binder.getService().getConnection(serverId).getNick();
channel.addMessage(new Message("<" + nickname + "> " + text));
onChannelMessage(channel.getName());
this.binder.getService().getConnection(serverId).sendMessage(channel.getName(), text);
if (!text.trim().startsWith("/")) {
String nickname = this.binder.getService().getConnection(serverId).getNick();
channel.addMessage(new Message("<" + nickname + "> " + text));
onChannelMessage(channel.getName());
this.binder.getService().getConnection(serverId).sendMessage(channel.getName(), text);
} else {
CommandParser.getInstance().parse(text, server, channel, binder.getService());
}
}
return true;