Log tailer enhancements.

This commit is contained in:
cnaude 2015-11-28 22:18:49 -07:00
parent f71d7b373c
commit bfeaa76e3e
8 changed files with 90 additions and 24 deletions

View File

@ -141,16 +141,28 @@
<version>1.8.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>17.0</version>
<version>18.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.cnaude.nplugins</groupId>
<artifactId>ntheendagain</artifactId>
<version>0.6.10-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.cnaude.essentials</groupId>
<artifactId>essentials</artifactId>
<version>2.14</version>
<version>2.14.1.3</version>
<scope>compile</scope>
</dependency>
<dependency>
@ -176,7 +188,7 @@
<dependency>
<groupId>net.milkbowl.vault</groupId>
<artifactId>Vault</artifactId>
<version>1.5.3</version>
<version>1.5.6</version>
<scope>compile</scope>
</dependency>
<dependency>

View File

@ -20,6 +20,7 @@ import java.io.File;
import org.apache.commons.io.input.Tailer;
import org.apache.commons.io.input.TailerListener;
import org.apache.commons.io.input.TailerListenerAdapter;
import org.pircbotx.Channel;
/**
*
@ -76,10 +77,31 @@ public class LogTailer {
@Override
public void handle(String line) {
if (ctcp) {
blockingCTCPMessage(target, line);
boolean okayToSend = false;
if (target.startsWith("#")) {
try {
for (Channel channel : ircBot.bot.getUserBot().getChannels()) {
if (channel.getName().equalsIgnoreCase(target)) {
okayToSend = true;
break;
}
}
} catch (Exception ex) {
plugin.logDebug(ex.getMessage());
}
} else {
blockingIRCMessage(target, line);
okayToSend = true;
}
if (okayToSend) {
String template = plugin.getMsgTemplate(ircBot.botNick, target, TemplateName.LOG_TAILER);
String message = plugin.tokenizer.logTailerTokenizer(file.getName(), line, template);
if (ctcp) {
blockingCTCPMessage(target, message);
} else {
blockingIRCMessage(target, message);
}
} else {
plugin.logDebug("[MyTailerListener] Can't send to " + target + " yet.");
}
}
@ -89,23 +111,19 @@ public class LogTailer {
if (!ircBot.isConnected()) {
return;
}
plugin.logDebug("[blockingIRCMessage] About to send IRC message to " + target + ": " + message);
ircBot.bot.sendIRC().message(target, message);
plugin.logDebug("[blockingIRCMessage] Message sent to " + target + ": " + message);
}
private void blockingCTCPMessage(final String target, final String message) {
if (!ircBot.isConnected()) {
return;
}
plugin.logDebug("[blockingCTCPMessage] About to send IRC message to " + target + ": " + message);
ircBot.bot.sendIRC().ctcpResponse(target, message);
plugin.logDebug("[blockingCTCPMessage] Message sent to " + target + ": " + message);
}
protected void stopTailer() {
if (tailer != null) {
plugin.logInfo("Stoping tailer.");
plugin.logInfo("Stopping tailer.");
tailer.stop();
}
}

View File

@ -59,7 +59,6 @@ import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import me.botsko.prism.actionlibs.QueryParameters;
import me.botsko.prism.events.BlockStateChange;
import org.apache.commons.io.input.Tailer;
import org.bukkit.Achievement;
import org.bukkit.ChatColor;
import org.bukkit.GameMode;
@ -189,9 +188,9 @@ public final class PurpleBot {
public CaseInsensitiveMap<String> linkRequests;
public CaseInsensitiveMap<Collection<String>> remotePlayers;
public CaseInsensitiveMap<CaseInsensitiveMap<String>> remoteServerInfo;
private LogTailer tailer;
private final List<LogTailer> tailers;
private boolean tailerEnabled;
private String tailerFile;
private final List<String> tailerFiles;
private String tailerRecipient;
private boolean tailerCtcp;
/**
@ -262,6 +261,8 @@ public final class PurpleBot {
this.remotePlayers = new CaseInsensitiveMap<>();
this.remoteServerInfo = new CaseInsensitiveMap<>();
this.ircPrivateMsgMap = new CaseInsensitiveMap<>();
this.tailers = new ArrayList<>();
this.tailerFiles = new ArrayList<>();
config = new YamlConfiguration();
goodBot = loadConfig();
if (goodBot) {
@ -286,8 +287,10 @@ public final class PurpleBot {
}
public void buildBot(boolean reload) {
if (tailer != null) {
tailer.stopTailer();
if (!tailers.isEmpty()) {
for (LogTailer tailer : tailers) {
tailer.stopTailer();
}
}
Configuration.Builder configBuilder = new Configuration.Builder()
.setName(botNick)
@ -368,14 +371,22 @@ public final class PurpleBot {
plugin.logInfo("Auto-connect is disabled. To connect: /irc connect " + bot.getNick());
}
plugin.logDebug("Max line length: " + configBuilder.getMaxLineLength());
if (tailerEnabled && !tailerFile.isEmpty() && !tailerRecipient.isEmpty()) {
tailer = new LogTailer(this, plugin, tailerRecipient, tailerCtcp, tailerFile);
if (tailerEnabled && !tailerFiles.isEmpty() && !tailerRecipient.isEmpty()) {
for (String tailerFile : tailerFiles) {
LogTailer tailer = new LogTailer(this, plugin, tailerRecipient, tailerCtcp, tailerFile);
tailers.add(tailer);
}
}
}
protected void stopTailer() {
if (tailer != null) {
tailer.stopTailer();
protected void stopTailers() {
if (!tailers.isEmpty()) {
for (LogTailer tailer : tailers) {
if (tailer != null) {
tailer.stopTailer();
}
}
tailers.clear();
}
}
@ -802,7 +813,21 @@ public final class PurpleBot {
// load tailer settings
tailerEnabled = config.getBoolean("file-tailer.enabled", false);
tailerFile = config.getString("file-tailer.file", "server.log");
String tailerFile = config.getString("file-tailer.file", "server.log");
if (!tailerFiles.contains(tailerFile)) {
tailerFiles.add(tailerFile);
plugin.logDebug(" Tailer File => " + tailerFile);
}
for (String f : config.getStringList("file-tailer.extra_files")) {
if (!tailerFiles.contains(f)) {
tailerFiles.add(f);
}
plugin.logDebug(" Tailer File => " + f);
}
if (tailerFiles.isEmpty()) {
plugin.logInfo(" No command recipients defined.");
}
tailerRecipient = config.getString("file-tailer.recipient", "");
tailerCtcp = config.getBoolean("file-tailer.ctcp", false);
@ -2826,7 +2851,7 @@ public final class PurpleBot {
asyncIRCMessage(target, "No message specified.");
}
}
/**
* Send chat messages from IRC to player.
*

View File

@ -348,7 +348,7 @@ public class PurpleIRC extends JavaPlugin {
logInfo("Disconnecting IRC bots.");
for (PurpleBot ircBot : ircBots.values()) {
commandQueue.cancel();
ircBot.stopTailer();
ircBot.stopTailers();
ircBot.saveConfig(getServer().getConsoleSender());
ircBot.quit();
}

View File

@ -146,5 +146,7 @@ public class TemplateName {
public final static String NTHE_END_AGAIN_HARD = "ntheendagain-hard";
public final static String NTHE_END_AGAIN_SOFT = "ntheendagain-soft";
public final static String NTHE_END_AGAIN_CRYSTAL = "ntheendagain-crystal";
public final static String LOG_TAILER = "log-tailer";
}

View File

@ -884,4 +884,10 @@ public class ChatTokenizer {
.replace("%TARGET%", targetPlayer.getName())
.replace("%MESSAGE%", message);
}
public String logTailerTokenizer(String file, String line, String template) {
return plugin.colorConverter.ircColorsToGame(template
.replace("%FILE%", file)
.replace("%LINE%", line));
}
}

View File

@ -70,6 +70,7 @@ command-notify:
file-tailer:
enabled: false
file: 'server.log'
extra_files: []
recipient: '#minecraft-test'
ctcp: false
# Messaging flood control (game and IRC)

View File

@ -233,6 +233,8 @@ message-format:
# Flood control
game-flood-warning: '&3Message not sent to IRC due to spamming. &rCooldown: %COOLDOWN%s'
irc-flood-warning: '&3Message not sent to game due to spamming. &rCooldown: %COOLDOWN%s'
# Log tailer format
log-tailer: '[LOG: %FILE%] %LINE%'
# Format for the @list command in IRC
list-format: '[&9Minecraft&r] &2Online &r(%COUNT%/%MAX%): %PLAYERS%'
list-separator: ', '