Skip to content

Commit

Permalink
Added + tested irc-console-commands list: enables certain console com…
Browse files Browse the repository at this point in the history
…mands for use through IRC via .c <cmd> or .console <cmd>

Added + tested stupid command .botsay <msg> to send a message as the bot, to the main channel

Fixed + tested irc-server-login
  • Loading branch information
Animosity committed Oct 20, 2010
1 parent 4375a0f commit 04e39b1
Showing 1 changed file with 112 additions and 67 deletions.
179 changes: 112 additions & 67 deletions Minebot.java
Expand Up @@ -7,6 +7,7 @@
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.Properties;
import org.jibble.pircbot.*;
Expand Down Expand Up @@ -87,7 +88,7 @@ public void init() {
irc_server_port = ircSettings.getProperty("irc-server-port");
irc_server_pass = ircSettings.getProperty("irc-server-password");
irc_server_ssl = Boolean.parseBoolean(ircSettings.getProperty("irc-server-ssl"));
irc_server_login = ircSettings.getProperty("irc-server-login");


irc_auth_method = ircSettings.getProperty("irc-auth-method");
irc_auth_username = ircSettings.getProperty("irc-auth-username");
Expand Down Expand Up @@ -134,8 +135,18 @@ public void init() {

if (ircSettings.containsKey("irc-console-commands")) {
this.optn_console_commands = this.getCSVArrayList(ircSettings.getProperty("irc-console-commands"));


} else {
log.log(Level.WARNING, CraftIRC.NAME + " no irc-console-commands defined for the Admin IRC channel!");
}

if (ircSettings.containsKey("irc-server-login") && !ircSettings.getProperty("irc-server-login").isEmpty()) {
irc_server_login = ircSettings.getProperty("irc-server-login");
} else { irc_server_login = this.irc_handle; }




}

catch (Exception e) {
Expand All @@ -149,8 +160,7 @@ public void init() {

this.setName(this.irc_handle);
this.setFinger(CraftIRC.NAME + " v" + CraftIRC.VERSION);
if (irc_server_login.isEmpty()) { this.setLogin(this.irc_handle); }
else { this.setLogin(this.irc_server_login); }
this.setLogin(this.irc_server_login);
this.setVersion(CraftIRC.NAME + " v" + CraftIRC.VERSION);

/*
Expand Down Expand Up @@ -411,85 +421,107 @@ public void onKick(String channel, String kickerNick, String kickerLogin,

// IRC commands parsed here
public void onMessage(String channel, String sender, String login, String hostname, String message) {
// Parse admin commands here

if (channel.equalsIgnoreCase(this.irc_admin_channel) && userAuthorized(channel, sender)) {
String[] splitMessage = message.split(" ");
String command = this.combineSplit(1, splitMessage, " ");
try {
// Parse admin commands here
if (channel.equalsIgnoreCase(this.irc_admin_channel) && userAuthorized(channel, sender)) {

if ((message.startsWith(cmd_prefix + "console") || message.startsWith(cmd_prefix + "c"))
&& splitMessage.length > 1
&& this.optn_console_commands.contains(splitMessage[1]))
{
log.info(CraftIRC.NAME + " - " + channel + " - " + sender + " used: " + this.combineSplit(0, splitMessage, " "));
// Have to call parseConsoleCommand first if you want to use any of the hey0 console commands
this.sendNotice(sender, "Executed: " + command); // send notice first, in case you're disabling/reloading the bot
if (!etc.getInstance().parseConsoleCommand(command, etc.getMCServer())) {
etc.getServer().useConsoleCommand(command);
}
return;

}


// Aliased commands
else if ((message.startsWith(cmd_prefix + "kick") && this.optn_console_commands.contains("kick")) && splitMessage.length > 1) {
log.info(CraftIRC.NAME + " - " + channel + " - " + sender + " used: " + this.combineSplit(0, splitMessage, " "));
etc.getServer().useConsoleCommand("kick " + this.combineSplit(1, splitMessage, " "));
this.sendNotice(sender, "Executed: " + command);
return;
}

else if ((message.startsWith(cmd_prefix + "ban") && this.optn_console_commands.contains("ban")) && splitMessage.length > 1) {
log.info(CraftIRC.NAME + " - " + channel + " - " + sender + " used: " + this.combineSplit(0, splitMessage, " "));
etc.getServer().useConsoleCommand("ban " + this.combineSplit(1, splitMessage, " "));
this.sendNotice(sender, "Executed: " + command);
return;
}

String[] splitMessage = message.split(" ");
String command = this.combineSplit(1, splitMessage, " ");

if ((message.startsWith(cmd_prefix + "console") || message.startsWith(cmd_prefix + "c")) && splitMessage.length > 1) {

log.info(CraftIRC.NAME + " - " + channel + " - " + sender + " used: " + this.combineSplit(0, splitMessage, " "));
// Have to call parseConsoleCommand first if you want to use any of the hey0 console commands
if (!etc.getInstance().parseConsoleCommand(command, etc.getMCServer())) {
etc.getServer().useConsoleCommand(command);
}
else if (message.startsWith(cmd_prefix + "broadcast") && this.optn_console_commands.contains("say") && splitMessage.length > 1) {
log.info(CraftIRC.NAME + " - " + channel + " - " + sender + " used: " + this.combineSplit(0, splitMessage, " "));
etc.getServer().useConsoleCommand("say " + this.combineSplit(1, splitMessage, " "));
this.sendNotice(sender, "Executed: " + command);
return;
}

else if (message.startsWith(cmd_prefix + "botsay") && this.getChannelList().contains(this.irc_channel) && splitMessage.length > 1) {
this.sendMessage(this.irc_channel, command);
this.sendNotice(sender, "Sent to main channel: " + command);
return;
}

else { this.sendNotice(sender, "Command not enabled"); }


} // end admin commands

this.sendNotice(sender, "Executed: " + command);
return;

}

// Aliased commands
if ((message.startsWith(cmd_prefix + "kick") && this.optn_console_commands.contains("kick")) && splitMessage.length > 1) {
log.info(CraftIRC.NAME + " - " + channel + " - " + sender + " used: " + this.combineSplit(0, splitMessage, " "));
etc.getServer().useConsoleCommand("kick " + this.combineSplit(1, splitMessage, " "));
this.sendNotice(sender, "Executed: " + command);
// begin public commands
if (message.startsWith(cmd_prefix + "players")) {
String playerlist = this.getPlayerList();
this.sendMessage(channel, playerlist); // set this to reply to the
// channel it was requested
// from
return;
}
if ((message.startsWith(cmd_prefix + "ban") && this.optn_console_commands.contains("ban")) && splitMessage.length > 1) {
log.info(CraftIRC.NAME + " - " + channel + " - " + sender + " used: " + this.combineSplit(0, splitMessage, " "));
etc.getServer().useConsoleCommand("ban " + this.combineSplit(1, splitMessage, " "));
this.sendNotice(sender, "Executed: " + command);

if (channel.equalsIgnoreCase(this.irc_channel)
&& this.optn_send_all_IRC_chat.contains("main")) {
if (message.startsWith(cmd_prefix)) { return; } // don't send command messages to MC
msgToGame(sender, message);
return;
}

if (message.startsWith(cmd_prefix + "broadcast") && this.optn_console_commands.contains("say") && splitMessage.length > 1) {
log.info(CraftIRC.NAME + " - " + channel + " - " + sender + " used: " + this.combineSplit(0, splitMessage, " "));
etc.getServer().useConsoleCommand("say " + this.combineSplit(1, splitMessage, " "));
this.sendNotice(sender, "Executed: " + command);
else if (channel.equalsIgnoreCase(this.irc_admin_channel)
&& this.optn_send_all_IRC_chat.contains("admin")) {
if (message.startsWith(cmd_prefix)) { return; } // don't send command messages to MC
msgToGame(sender, message);
return;
}

}


if (message.startsWith(cmd_prefix + "players")) {
String playerlist = this.getPlayerList();
this.sendMessage(channel, playerlist); // set this to reply to the
// channel it was requested
// from
return;
}

if (channel.equalsIgnoreCase(this.irc_channel)
&& this.optn_send_all_IRC_chat.contains("main")) {
if (message.startsWith(cmd_prefix)) {
return;
} // don't send command messages to MC
msgToGame(sender, message);
}

else if (channel.equalsIgnoreCase(this.irc_admin_channel)
&& this.optn_send_all_IRC_chat.contains("admin")) {
if (message.startsWith(cmd_prefix)) { return; } // don't send command messages to MC
msgToGame(sender, message);
}

else {
if (message.startsWith(cmd_prefix + "say")) {
message = message.substring(message.indexOf(" ")).trim();
msgToGame(sender, message);
this.sendNotice(sender, "Message sent to game");

else if (message.startsWith(cmd_prefix + "say") || message.startsWith(cmd_prefix + "mc")) {
// message = message.substring(message.indexOf(" ")).trim();
if (splitMessage.length > 1) {
msgToGame(sender, command);
this.sendNotice(sender, "Message sent to game");
return;
}
}



} catch (Exception e) {
e.printStackTrace();
log.log(Level.SEVERE, CraftIRC.NAME + " - error while relaying IRC command: " + message);
}

}

// IRC user authorization check against prefixes
// Currently just for admin channel as first-order level of security

// TODO: ricin's fix in Pircbot

public boolean userAuthorized(String channel, String user) {
if (channel.equalsIgnoreCase(this.irc_admin_channel)) {
Expand Down Expand Up @@ -549,6 +581,7 @@ private String getPlayerList() {
}
}

// Combine string array with delimiter
public String combineSplit(int initialPos, String[] parts, String delimiter)
throws ArrayIndexOutOfBoundsException {
String result = "";
Expand All @@ -560,7 +593,19 @@ public String combineSplit(int initialPos, String[] parts, String delimiter)
}
return result;
}




public ArrayList<String> getChannelList() {

try { return new ArrayList<String>(Arrays.asList(this.getChannels())); }
catch (Exception e) {
e.printStackTrace();
return null;

This comment has been minimized.

Copy link
@Animosity

Animosity Oct 20, 2010

Author Owner

change to return an empty AL

}
}


public void onDisconnect() {

// Maybe check if disabled, and if not, start(); depending on a flag set
Expand Down

0 comments on commit 04e39b1

Please sign in to comment.