Split messages with \r\n and \n.

This commit is contained in:
cnaude 2015-05-19 18:13:18 -07:00
parent 60dcc46328
commit 5654897419
5 changed files with 46 additions and 10 deletions

View File

@ -39,10 +39,8 @@ public class BotWatcher {
bt = this.plugin.getServer().getScheduler().runTaskTimerAsynchronously(this.plugin, new Runnable() {
@Override
public void run() {
//plugin.logDebug("Checking connection status of IRC bots.");
for (PurpleBot ircBot : plugin.ircBots.values()) {
if (ircBot.isConnectedBlocking()) {
//plugin.logDebug("[" + ircBot.botNick + "] CONNECTED");
ircBot.setConnected(true);
} else {
ircBot.setConnected(false);

View File

@ -51,4 +51,5 @@ public class ChannelWatcher {
public void cancel() {
bt.cancel();
}
}

View File

@ -16,7 +16,13 @@
*/
package com.cnaude.purpleirc.Commands;
import com.cnaude.purpleirc.PurpleBot;
import com.cnaude.purpleirc.PurpleIRC;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
@ -27,7 +33,7 @@ import org.bukkit.command.CommandSender;
public class Test implements IRCCommandInterface {
private final PurpleIRC plugin;
private final String usage = "[player name]";
private final String usage = "[name|sslciphers]";
private final String desc = "Testing various Vault methods.";
private final String name = "test";
private final String fullUsage = ChatColor.WHITE + "Usage: " + ChatColor.GOLD + "/irc " + name + " " + usage;
@ -49,12 +55,32 @@ public class Test implements IRCCommandInterface {
public void dispatch(CommandSender sender, String[] args) {
//irc test <username>
if (plugin.vaultHelpers == null) {
sender.sendMessage(ChatColor.RED + "Vault is not enabled!");
sender.sendMessage(ChatColor.RED + "Vault is no enabled!");
return;
}
if (plugin.debugMode()) {
if (args.length >= 2) {
String playername = args[1];
if (playername.equalsIgnoreCase("sslciphers")) {
sender.sendMessage(ChatColor.LIGHT_PURPLE + "Available SSL ciphers");
SSLContext context;
try {
context = SSLContext.getDefault();
SSLSocketFactory sf = context.getSocketFactory();
String[] cipherSuites = sf.getSupportedCipherSuites();
for (String s : cipherSuites) {
sender.sendMessage(ChatColor.WHITE + "-- " + ChatColor.GOLD + s);
}
} catch (NoSuchAlgorithmException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
}
} else if (playername.equalsIgnoreCase("splits")) {
for (PurpleBot ircBot : plugin.ircBots.values()) {
for (String channelName : ircBot.botChannels) {
ircBot.asyncIRCMessage(channelName, "Test\r\nTest2\r\nTest3\nTest4\nTest5");
}
}
} else {
sender.sendMessage(ChatColor.LIGHT_PURPLE + "Testing " + playername);
sender.sendMessage("displayName : " + plugin.getDisplayName(name));
sender.sendMessage("getGroupPrefix : " + plugin.getGroupPrefix(plugin.defaultPlayerWorld, playername));
@ -62,6 +88,7 @@ public class Test implements IRCCommandInterface {
sender.sendMessage("getPlayerPrefix : " + plugin.getPlayerPrefix(plugin.defaultPlayerWorld, playername));
sender.sendMessage("getPlayerSuffix : " + plugin.getPlayerSuffix(plugin.defaultPlayerWorld, playername));
sender.sendMessage("getPlayerGroup : " + plugin.getPlayerGroup(plugin.defaultPlayerWorld, playername));
}
} else {
sender.sendMessage(fullUsage);
}

View File

@ -31,6 +31,9 @@ public class IRCMessageQueueWatcher {
private final PurpleBot ircBot;
private final Timer timer;
private final BlockingQueue<IRCMessage> queue = new LinkedBlockingQueue<>();
private final String REGEX_CLEAN = "^[\\r\\n]|[\\r\\n]$";
private final String REGEX_CRLF = "\\r\\n";
private final String LF = "\\n";
/**
*
@ -58,13 +61,15 @@ public class IRCMessageQueueWatcher {
IRCMessage ircMessage = queue.poll();
if (ircMessage != null) {
plugin.logDebug("[" + queue.size() + "]: queueAndSend message detected");
for (String s : cleanupAndSplitMessage(ircMessage.message)) {
if (ircMessage.ctcpResponse) {
blockingCTCPMessage(ircMessage.target, ircMessage.message);
blockingCTCPMessage(ircMessage.target, s);
} else {
blockingIRCMessage(ircMessage.target, ircMessage.message);
blockingIRCMessage(ircMessage.target, s);
}
}
}
}
private void blockingIRCMessage(final String target, final String message) {
if (!ircBot.isConnected()) {
@ -72,7 +77,6 @@ public class IRCMessageQueueWatcher {
}
plugin.logDebug("[blockingIRCMessage] About to send IRC message to " + target + ": " + message);
ircBot.bot.sendIRC().message(target, message);
//ircBot.bot.sendRaw().rawLineNow("PRIVMSG " + target + " :" + message);
plugin.logDebug("[blockingIRCMessage] Message sent to " + target + ": " + message);
}
@ -85,6 +89,10 @@ public class IRCMessageQueueWatcher {
plugin.logDebug("[blockingCTCPMessage] Message sent to " + target + ": " + message);
}
private String[] cleanupAndSplitMessage(String message) {
return message.replaceAll(REGEX_CLEAN, "").replaceAll(REGEX_CRLF, "\n").split(LF);
}
public void cancel() {
timer.cancel();
}
@ -104,4 +112,5 @@ public class IRCMessageQueueWatcher {
public void add(IRCMessage ircMessage) {
queue.offer(ircMessage);
}
}

View File

@ -479,13 +479,14 @@ public final class PurpleBot {
public void asyncIRCMessage(final String target, final String message) {
plugin.logDebug("Entering aysncIRCMessage");
messageQueue.add(new IRCMessage(target, plugin.colorConverter.
gameColorsToIrc(message), false));
IRCMessage ircMessage = new IRCMessage(target, plugin.colorConverter.gameColorsToIrc(message), false);
messageQueue.add(ircMessage);
}
public void asyncCTCPMessage(final String target, final String message) {
plugin.logDebug("Entering asyncCTCPMessage");
messageQueue.add(new IRCMessage(target, plugin.colorConverter.gameColorsToIrc(message), true));
IRCMessage ircMessage = new IRCMessage(target, plugin.colorConverter.gameColorsToIrc(message), true);
messageQueue.add(ircMessage);
}
public void asyncCTCPCommand(final String target, final String command) {