package com.briman0094.bribot.modules.general;
import java.io.IOException;
import com.briman0094.bribot.BriBot;
import com.briman0094.bribot.modules.IModule;
import com.briman0094.irc.MessageType;
public class ModGeneralActions implements IModule
{
private BriBot bot;
public ModGeneralActions()
{
bot = null;
}
@Override
public void setBotInstance(BriBot bot)
{
this.bot = bot;
}
@Override
public void handleChatMessage(String channel, String user, String message)
{
if (message.startsWith(BriBot.COMMAND_PREFIX))
{
String cmdString = message.substring(1).replace("\r", "").replace("\n", "");
String split[] = cmdString.split(" ");
if (split.length < 1)
return;
String cmd = split[0];
if (user.equalsIgnoreCase(BriBot.OWNER_UN) || user.equalsIgnoreCase(bot.getUsername()))
{
if (cmd.equalsIgnoreCase("quit") || cmd.equalsIgnoreCase("exit"))
{
bot.sendToChannel(channel, "Bye!");
try
{
bot.disconnect();
}
catch (IOException e)
{
e.printStackTrace();
}
}
else if (cmd.equalsIgnoreCase("join"))
{
if (split.length >= 2)
{
bot.joinChannel(split[1]);
bot.sendToChannel(channel, "Joining " + split[1]);
}
}
else if (cmd.equalsIgnoreCase("part"))
{
if (split.length >= 2)
{
if (split[1].equalsIgnoreCase(channel))
{
bot.sendToChannel(channel, "Bye!");
}
else
{
bot.sendToChannel(channel, "Leaving " + split[1]);
}
bot.partChannel(split[1]);
}
else
{
bot.sendToChannel(channel, "Bye!");
bot.partChannel(channel);
}
}
else if (cmd.equalsIgnoreCase("debug"))
{
if (split.length >= 2)
{
if (split[1].equalsIgnoreCase("true") || split[1].equalsIgnoreCase("on"))
{
bot.getIRCConnection().setDebug(true);
bot.sendToChannel(channel, "Debug turned on");
}
else
{
bot.getIRCConnection().setDebug(false);
bot.sendToChannel(channel, "Debug turned off");
}
}
else
{
bot.getIRCConnection().setDebug(!bot.getIRCConnection().isDebug());
bot.sendToChannel(channel, "Debug turned " + (bot.getIRCConnection().isDebug() ? "on" : "off"));
}
}
else if (cmd.equalsIgnoreCase("plugins"))
{
String msg = "Loaded plugins:";
for (String plugin : bot.getLoadedModules())
{
msg += " " + plugin;
}
bot.sendToChannel(channel, msg);
}
else if (cmd.equalsIgnoreCase("msg"))
{
if (split.length > 2)
{
String echo = "";
String to = split[1];
for (int i = 2; i < split.length; i++)
{
echo += (i == 1) ? split[i] : (" " + split[i]);
}
bot.sendToUser(to, echo);
}
}
else if (cmd.equalsIgnoreCase("nick"))
{
if (split.length > 1)
{
String newNick = split[1];
bot.sendToChannel(channel, "Attempting to rename to " + newNick);
try
{
Thread.sleep(250);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
bot.sendRawMessage(MessageType.NICK, new String[] { newNick });
}
}
else if (cmd.equalsIgnoreCase("quote"))
{
if (split.length > 1)
{
MessageType type = MessageType.getMessageType(split[1]);
if (type != null)
{
String[] newArgs = new String[split.length - 2];
for (int i = 0; i < newArgs.length; i++)
{
newArgs[i] = split[i + 2];
}
bot.sendRawMessage(type, newArgs);
}
}
}
}
if (cmd.equalsIgnoreCase("echo"))
{
String echo = "";
String to = channel;
if (!to.startsWith("#"))
to = bot.getCurrentChannel();
if (split.length > 1)
{
for (int i = 1; i < split.length; i++)
{
echo += (i == 1) ? split[i] : (" " + split[i]);
}
bot.sendToChannel(to, echo);
}
}
else if (cmd.equalsIgnoreCase("me") || cmd.equalsIgnoreCase("action"))
{
if (split.length > 1)
{
String[] temp = new String[split.length + 1];
temp[0] = channel;
if (!temp[0].startsWith("#"))
temp[0] = bot.getCurrentChannel();
temp[1] = "ACTION";
for (int i = 1; i < split.length; i++)
{
temp[i + 1] = split[i];
}
if (temp.length > 2)
temp[1] = ":" + temp[1];
temp[temp.length - 1] = temp[temp.length - 1] + "";
bot.sendRawMessage(MessageType.MESSAGE, temp);
}
}
}
}
@Override
public void handleChatAction(String channel, String user, String action)
{
}
@Override
public void handleChatNotice(String user, String notice)
{
}
}