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

Commit

Permalink
Split messages with \r\n and \n.
Browse files Browse the repository at this point in the history
  • Loading branch information
cnaude committed May 20, 2015
1 parent 2a90d6a commit 146c273
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 25 deletions.
2 changes: 0 additions & 2 deletions src/main/java/com/cnaude/purpleirc/BotWatcher.java
Expand Up @@ -39,10 +39,8 @@ public BotWatcher(final PurpleIRC plugin) {
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);
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/cnaude/purpleirc/ChannelWatcher.java
Expand Up @@ -51,4 +51,5 @@ public void run() {
public void cancel() {
bt.cancel();
}

}
9 changes: 8 additions & 1 deletion src/main/java/com/cnaude/purpleirc/Commands/Test.java
Expand Up @@ -16,6 +16,7 @@
*/
package com.cnaude.purpleirc.Commands;

import com.cnaude.purpleirc.PurpleBot;
import com.cnaude.purpleirc.PurpleIRC;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Level;
Expand Down Expand Up @@ -73,9 +74,15 @@ public void dispatch(CommandSender sender, String[] args) {
} 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));
sender.sendMessage("getGroupSuffix : " + plugin.getGroupSuffix(plugin.defaultPlayerWorld, playername));
sender.sendMessage("getPlayerPrefix : " + plugin.getPlayerPrefix(plugin.defaultPlayerWorld, playername));
Expand Down
19 changes: 14 additions & 5 deletions src/main/java/com/cnaude/purpleirc/IRCMessageQueueWatcher.java
Expand Up @@ -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";

/**
*
Expand Down Expand Up @@ -58,10 +61,12 @@ private void queueAndSend() {
IRCMessage ircMessage = queue.poll();
if (ircMessage != null) {
plugin.logDebug("[" + queue.size() + "]: queueAndSend message detected");
if (ircMessage.ctcpResponse) {
blockingCTCPMessage(ircMessage.target, ircMessage.message);
} else {
blockingIRCMessage(ircMessage.target, ircMessage.message);
for (String s : cleanupAndSplitMessage(ircMessage.message)) {
if (ircMessage.ctcpResponse) {
blockingCTCPMessage(ircMessage.target, s);
} else {
blockingIRCMessage(ircMessage.target, s);
}
}
}
}
Expand All @@ -72,7 +77,6 @@ private void blockingIRCMessage(final String target, final String message) {
}
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);
}

Expand All @@ -85,6 +89,10 @@ private void blockingCTCPMessage(final String target, final String message) {
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();
}
Expand All @@ -104,4 +112,5 @@ public String clearQueue() {
public void add(IRCMessage ircMessage) {
queue.offer(ircMessage);
}

}
19 changes: 2 additions & 17 deletions src/main/java/com/cnaude/purpleirc/PurpleBot.java
Expand Up @@ -50,7 +50,6 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
Expand Down Expand Up @@ -159,7 +158,6 @@ public final class PurpleBot {
public List<String> channelCmdNotifyIgnore;
private final ArrayList<ListenerAdapter> ircListeners;
public IRCMessageQueueWatcher messageQueue;
public HashMap<String, IRCMessageQueueWatcher> messageQueueMap;
public FloodChecker floodChecker;
protected boolean floodControlEnabled;
protected int floodControlMaxMessages;
Expand Down Expand Up @@ -251,11 +249,6 @@ public void run() {
}

messageQueue = new IRCMessageQueueWatcher(this, plugin);
messageQueueMap = new HashMap<>();
for (String channelName : botChannels) {
plugin.logDebug("Building message queue for " + channelName);
messageQueueMap.put(channelName, new IRCMessageQueueWatcher(this, plugin));
}
floodChecker = new FloodChecker(this, plugin);

}
Expand Down Expand Up @@ -487,21 +480,13 @@ public void run() {
public void asyncIRCMessage(final String target, final String message) {
plugin.logDebug("Entering aysncIRCMessage");
IRCMessage ircMessage = new IRCMessage(target, plugin.colorConverter.gameColorsToIrc(message), false);
if (messageQueueMap.containsKey(target)) {
messageQueueMap.get(target).add(ircMessage);
} else {
messageQueue.add(ircMessage);
}
messageQueue.add(ircMessage);
}

public void asyncCTCPMessage(final String target, final String message) {
plugin.logDebug("Entering asyncCTCPMessage");
IRCMessage ircMessage = new IRCMessage(target, plugin.colorConverter.gameColorsToIrc(message), true);
if (messageQueueMap.containsKey(target)) {
messageQueueMap.get(target).add(ircMessage);
} else {
messageQueue.add(ircMessage);
}
messageQueue.add(ircMessage);
}

public void asyncCTCPCommand(final String target, final String command) {
Expand Down

0 comments on commit 146c273

Please sign in to comment.