Skip to content
This repository has been archived by the owner on Jul 27, 2019. It is now read-only.

Commit

Permalink
Add extra_commands list option for individual commands. #94
Browse files Browse the repository at this point in the history
  • Loading branch information
cnaude committed Nov 14, 2014
1 parent d5ba009 commit 3c15801
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 60 deletions.
11 changes: 10 additions & 1 deletion src/main/java/com/cnaude/purpleirc/PurpleBot.java
Expand Up @@ -140,6 +140,7 @@ public final class PurpleBot {
public CaseInsensitiveMap<Collection<String>> muteList;
public CaseInsensitiveMap<Collection<String>> enabledMessages;
public CaseInsensitiveMap<CaseInsensitiveMap<CaseInsensitiveMap<String>>> commandMap;
public CaseInsensitiveMap<CaseInsensitiveMap<List<String>>> extraCommandMap;
public CaseInsensitiveMap<Long> joinNoticeCooldownMap;
public ArrayList<CommandSender> whoisSenders;
public List<String> channelCmdNotifyRecipients;
Expand Down Expand Up @@ -167,6 +168,7 @@ public PurpleBot(File file, PurpleIRC plugin) {
this.channelCmdNotifyRecipients = new ArrayList<>();
this.channelCmdNotifyIgnore = new ArrayList<>();
this.commandMap = new CaseInsensitiveMap<>();
this.extraCommandMap = new CaseInsensitiveMap<>();
this.joinNoticeCooldownMap = new CaseInsensitiveMap<>();
this.enabledMessages = new CaseInsensitiveMap<>();
this.muteList = new CaseInsensitiveMap<>();
Expand Down Expand Up @@ -631,6 +633,7 @@ private void loadConfig() {
enabledMessages.clear();
worldList.clear();
commandMap.clear();
extraCommandMap.clear();

channelCmdNotifyEnabled = config.getBoolean("command-notify.enabled", false);
plugin.logDebug(" CommandNotifyEnabled => " + channelCmdNotifyEnabled);
Expand Down Expand Up @@ -830,27 +833,33 @@ private void loadConfig() {

// build command map
CaseInsensitiveMap<CaseInsensitiveMap<String>> map = new CaseInsensitiveMap<>();
CaseInsensitiveMap<List<String>> extraMap = new CaseInsensitiveMap<>();
try {
for (String command : config.getConfigurationSection("channels." + enChannelName + ".commands").getKeys(false)) {
plugin.logDebug(" Command => " + command);
CaseInsensitiveMap<String> optionPair = new CaseInsensitiveMap<>();
List<String> extraCommands = new ArrayList<>();
String commandKey = "channels." + enChannelName + ".commands." + command + ".";
optionPair.put("modes", config.getString(commandKey + "modes", "*"));
optionPair.put("private", config.getString(commandKey + "private", "false"));
optionPair.put("ctcp", config.getString(commandKey + "ctcp", "false"));
optionPair.put("game_command", config.getString(commandKey + "game_command", "@help"));
optionPair.put("game_command", config.getString(commandKey + "game_command", ""));
extraCommands.addAll(config.getStringList(commandKey + "extra_commands"));
plugin.logDebug("extra_commands: " + extraCommands.toString());
optionPair.put("private_listen", config.getString(commandKey + "private_listen", "true"));
optionPair.put("channel_listen", config.getString(commandKey + "channel_listen", "true"));
optionPair.put("perm", config.getString(commandKey + "perm", ""));
for (String s : optionPair.keySet()) {
config.set(commandKey + s, optionPair.get(s));
}
map.put(command, optionPair);
extraMap.put(command, extraCommands);
}
} catch (Exception ex) {
plugin.logError("No commands found for channel " + enChannelName);
}
commandMap.put(channelName, map);
extraCommandMap.put(channelName, extraMap);
if (map.isEmpty()) {
plugin.logInfo("No commands specified!");
}
Expand Down
118 changes: 61 additions & 57 deletions src/main/java/com/cnaude/purpleirc/Utilities/IRCMessageHandler.java
Expand Up @@ -88,75 +88,79 @@ public void processMessage(PurpleBot ircBot, User user, Channel channel, String
return;
}

String gameCommand = (String) ircBot.commandMap.get(myChannel).get(command).get("game_command");
String gc = (String) ircBot.commandMap.get(myChannel).get(command).get("game_command");
List<String> extraCommands = ircBot.extraCommandMap.get(myChannel).get(command);
List<String> gameCommands = new ArrayList<>();
gameCommands.add(gc);
gameCommands.addAll(extraCommands);
String modes = (String) ircBot.commandMap.get(myChannel).get(command).get("modes");
String perm = (String) ircBot.commandMap.get(myChannel).get(command).get("perm");
boolean privateCommand = Boolean.parseBoolean(ircBot.commandMap.get(myChannel).get(command).get("private"));
boolean ctcpResponse = Boolean.parseBoolean(ircBot.commandMap.get(myChannel).get(command).get("ctcp"));

plugin.logDebug(gameCommand + ":" + modes + ":" + privateCommand);

if (privateCommand || privateMessage) {
target = user.getNick();
}

plugin.logDebug("Target: " + target);

if (isValidMode(modes, user, channel) && checkPerm(perm, user.getNick())) {
switch (gameCommand) {
case "@list":
sendMessage(ircBot, target, plugin.getMCPlayers(ircBot, myChannel), ctcpResponse);
break;
case "@uptime":
sendMessage(ircBot, target, plugin.getMCUptime(), ctcpResponse);
break;
case "@help":
sendMessage(ircBot, target, getCommands(ircBot.commandMap, myChannel), ctcpResponse);
break;
case "@chat":
ircBot.broadcastChat(user, channel, target, commandArgs, false, ctcpResponse);
break;
case "@ochat":
ircBot.broadcastChat(user, channel, target, commandArgs, true, ctcpResponse);
break;
case "@hchat":
ircBot.broadcastHeroChat(user, channel, target, commandArgs);
break;
case "@motd":
sendMessage(ircBot, target, plugin.getServerMotd(), ctcpResponse);
break;
case "@rtsmb":
if (plugin.reportRTSHook != null) {
plugin.reportRTSHook.modBroadcast(user.getNick(), commandArgs);
}
break;
case "@msg":
ircBot.playerChat(user, channel, target, commandArgs);
break;
case "@clearqueue":
sendMessage(ircBot, target, plugin.commandQueue.clearQueue(), ctcpResponse);
sendMessage(ircBot, target, ircBot.messageQueue.clearQueue(), ctcpResponse);
break;
case "@query":
sendMessage(ircBot, target, plugin.getRemotePlayers(commandArgs), ctcpResponse);
break;
default:
if (commandArgs == null) {
commandArgs = "";
}
if (gameCommand.contains("%ARGS%")) {
gameCommand = gameCommand.replace("%ARGS%", commandArgs);
}
if (gameCommand.contains("%NAME%")) {
gameCommand = gameCommand.replace("%NAME%", user.getNick());
}
plugin.logDebug("GM: \"" + gameCommand.trim() + "\"");
try {
plugin.commandQueue.add(new IRCCommand(new IRCCommandSender(ircBot, target, plugin, ctcpResponse), gameCommand.trim()));
} catch (Exception ex) {
plugin.logError(ex.getMessage());
}
break;
for (String gameCommand : gameCommands) {
switch (gameCommand) {
case "@list":
sendMessage(ircBot, target, plugin.getMCPlayers(ircBot, myChannel), ctcpResponse);
break;
case "@uptime":
sendMessage(ircBot, target, plugin.getMCUptime(), ctcpResponse);
break;
case "@help":
sendMessage(ircBot, target, getCommands(ircBot.commandMap, myChannel), ctcpResponse);
break;
case "@chat":
ircBot.broadcastChat(user, channel, target, commandArgs, false, ctcpResponse);
break;
case "@ochat":
ircBot.broadcastChat(user, channel, target, commandArgs, true, ctcpResponse);
break;
case "@hchat":
ircBot.broadcastHeroChat(user, channel, target, commandArgs);
break;
case "@motd":
sendMessage(ircBot, target, plugin.getServerMotd(), ctcpResponse);
break;
case "@rtsmb":
if (plugin.reportRTSHook != null) {
plugin.reportRTSHook.modBroadcast(user.getNick(), commandArgs);
}
break;
case "@msg":
ircBot.playerChat(user, channel, target, commandArgs);
break;
case "@clearqueue":
sendMessage(ircBot, target, plugin.commandQueue.clearQueue(), ctcpResponse);
sendMessage(ircBot, target, ircBot.messageQueue.clearQueue(), ctcpResponse);
break;
case "@query":
sendMessage(ircBot, target, plugin.getRemotePlayers(commandArgs), ctcpResponse);
break;
default:
if (commandArgs == null) {
commandArgs = "";
}
if (gameCommand.contains("%ARGS%")) {
gameCommand = gameCommand.replace("%ARGS%", commandArgs);
}
if (gameCommand.contains("%NAME%")) {
gameCommand = gameCommand.replace("%NAME%", user.getNick());
}
plugin.logDebug("GM: \"" + gameCommand.trim() + "\"");
try {
plugin.commandQueue.add(new IRCCommand(new IRCCommandSender(ircBot, target, plugin, ctcpResponse), gameCommand.trim()));
} catch (Exception ex) {
plugin.logError(ex.getMessage());
}
break;
}
}
} else {
plugin.logDebug("User '" + user.getNick() + "' mode not okay.");
Expand Down
14 changes: 12 additions & 2 deletions src/main/resources/SampleBot.yml
Expand Up @@ -7,8 +7,8 @@
nick: AwesomeBot
# If your bot's nick is in use try these alternates. Leave blank for none.
alt-nicks:
- AwesomeBot_
- AwesomeBotA
- %NICK%_
- %NICK%__
# login - Your bot's login name
login: AwesomeName
# realname
Expand Down Expand Up @@ -305,6 +305,16 @@ channels:
private_listen: 'true'
channel_listen: 'true'
perm: ''
lv:
modes: 'o'
private: 'false'
ctcp: 'false'
game_command: '@list'
extra_commands:
- version
private_listen: 'true'
channel_listen: 'true'
perm: ''
# gamemode:
# modes: 'o'
# private: false
Expand Down

0 comments on commit 3c15801

Please sign in to comment.